📄 datastoreui.cpp
字号:
/*gpsmgr: A program for managing GPS informationCopyright (C) 2003 Austin BinghamThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.You can reach the author at: abingham@spamcop.net*/#include "dataStoreUI.h"#include "datastorereader.h"#include "datastorewriter.h"#include <gpspoint2/gps.h>#include "groupEditorWidget.h"#include "listItemPopup.h"#include <qaction.h>#include <qcombobox.h>#include <qcursor.h>#include <qfiledialog.h>#include <qinputdialog.h>#include <qlineedit.h>#include <qlistview.h>#include <qmessagebox.h>#include <sstream>#include "trackEditorUI.h"#include "waypointEditorUI.h"namespace{ using namespace gpsmgr; //-------------------------------------------------------------------------- class TrackListItem : public QListViewItem { public: enum { NAME_COL = 0 }; TrackListItem(const DataStore::Tracks& ts, DataStore::Tracks::ID id, QListView * parent) : QListViewItem (parent), mTStore (&ts), mID (id) {} QString text(int col) const { switch (col) { case 0: return mTStore->get(trackID()).name().c_str(); default: return "UNKNOWN COLUMN"; } } void setTrackID(DataStore::Tracks::ID id) { mID = id; } DataStore::Tracks::ID trackID() const { return mID; } void setTracks(const DataStore::Tracks& ts) { mTStore = &ts; } const DataStore::Tracks& trackStore() const { return *mTStore; } private: const DataStore::Tracks* mTStore; DataStore::Tracks::ID mID; }; //-------------------------------------------------------------------------- class WaypointListItem : public QListViewItem { public: enum { NAME_COL = 0, LAT_COL, LON_COL, ALT_COL }; WaypointListItem(const DataStore::Waypoints& wpstore, DataStore::Waypoints::ID id, QListView * parent) : QListViewItem (parent), mWPStore (&wpstore), mID (id) {} QString text(int col) const { switch (col) { case NAME_COL: return mWPStore->get(waypointID()).name().c_str(); case LAT_COL: { ostringstream oss; oss << mWPStore->get(waypointID()).position().lat(); return oss.str().c_str(); } case LON_COL: { ostringstream oss; oss << mWPStore->get(waypointID()).position().lon(); return oss.str().c_str(); } case ALT_COL: { ostringstream oss; oss << mWPStore->get(waypointID()).altitude(); return oss.str().c_str(); } default: return "UNKNOWN COLUMN"; } } void setWaypointID(DataStore::Waypoints::ID id) { mID = id; } DataStore::Waypoints::ID waypointID() const { return mID; } void setWaypointStore(const DataStore::Waypoints& ds) { mWPStore = &ds; } const DataStore::Waypoints& waypointStore() const { return *mWPStore; } private: const DataStore::Waypoints* mWPStore; DataStore::Waypoints::ID mID; }; /** This just checks to see if a string begins with another string */ struct FilterMatcher { FilterMatcher(const string& fs) : mFilterString (fs) {} bool operator()(const string& group) { bool rval = (group.find(mFilterString) == 0); return rval; } string mFilterString; }; bool matchFilterString(const GroupObject& go, const string& filter) { if (filter == "") return true; gpsmgr::GroupObject::Groups::const_iterator grpitr = std::find_if(go.groupsBegin(), go.groupsEnd(), FilterMatcher(filter)); return (grpitr != go.groupsEnd()); } // TODO: For some reason, creating GPS objects and then deleting them from the // stack causes a segfault. Hopefull this will be fixed in future versions of // gpspoint. Until then, I'm stuck with a solution like this...create a global // GPS object and use it everywhere. I guess this really isn't all that awful, // but I don't like it much. gpspoint2::GPS gGPS; }namespace gpsmgr { namespace ui { //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- void DataStoreHolder::reset(const DataStore& ds) { mDS.reset(new DataStore(ds)); } DataStore& DataStoreHolder::dataStore() { if (!mDS.get()) mDS.reset(new DataStore()); return *mDS; } bool DataStoreHolder::haveDataStore() const { return mDS.get(); } //-------------------------------------------------------------------------- //-------------------------------------------------------------------------- DataStoreUI::DataStoreUI(QWidget* parent, const char* name, WFlags fl) : DataStoreUIBase (parent, name, fl), mNeedSave (false) { synchronizeInterface(); } DataStoreUI::DataStoreUI(const DataStore& ds, QWidget* parent, const char* name, WFlags fl) : DataStoreUIBase (parent, name, fl), mNeedSave (false) { setDataStore(ds); synchronizeInterface(); } void DataStoreUI::setDataStore(const DataStore& ds) { reset(ds); mFilterEdit->setText(""); mGroups.clear(); mWaypointListView->clear(); mTrackListView->clear(); for (DataStore::Waypoints::const_iterator itr = dataStore().waypoints().begin(); itr != dataStore().waypoints().end(); ++itr) { for (GroupObject::Groups::const_iterator gitr = itr->second.groupsBegin(); gitr != itr->second.groupsEnd(); ++gitr) { mGroups.insert(*gitr); } new WaypointListItem(dataStore().waypoints(), itr->first, mWaypointListView); } for (DataStore::Tracks::const_iterator itr = dataStore().tracks().begin(); itr != dataStore().tracks().end(); ++itr) { for (GroupObject::Groups::const_iterator gitr = itr->second.groupsBegin(); gitr != itr->second.groupsEnd(); ++gitr) { mGroups.insert(*gitr); } new TrackListItem(dataStore().tracks(), itr->first, mTrackListView); } } void DataStoreUI::fileOpen() { // TODO: Prompt if there are unsaved changes QString filename = QFileDialog::getOpenFileName(); if (!filename) return; DataStore ds; gpsmgr::io::readDataStore(filename, ds); setDataStore(ds); mSaveFile = filename.latin1(); flagChanges(false); } void DataStoreUI::fileSave() { assert(haveDataStore()); if (mSaveFile.size() < 1) fileSaveAs(); gpsmgr::io::writeDataStore(mSaveFile, dataStore()); flagChanges(false); } void DataStoreUI::fileSaveAs() { assert(haveDataStore()); QString filename = QFileDialog::getSaveFileName(); if (!filename) return; gpsmgr::io::writeDataStore(filename, dataStore()); mSaveFile = filename.latin1(); flagChanges(false); } void DataStoreUI::fileExit() { if (mNeedSave) { if (QMessageBox::warning(this, "Unsaved Changes", "Do you wish to save your changes before exiting?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) { fileSave(); } } std::exit(0); } void DataStoreUI::gpsDownload() { bool ok; QString device = QInputDialog::getText( "GPS Device", "Enter the device for the GPS:", QLineEdit::Normal, QString::null, &ok, this ); if ( !ok || device.isEmpty() ) return; gGPS.setDevice(device); Waypointlist wpl; gGPS.downloadWaypoints(wpl); Tracklist tl; gGPS.downloadTracks(tl); Routelist rl; gGPS.downloadRoutes(rl); DataStore ds; // Copy the waypoints into our format
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -