1
/***************************************************************************
2
qgsgpsplugin.cpp - GPS related tools
5
Copyright : (C) 2004 by Tim Sutton
6
Email : tim@linfiniti.com
8
***************************************************************************/
10
/***************************************************************************
12
* This program is free software; you can redistribute it and/or modify *
13
* it under the terms of the GNU General Public License as published by *
14
* the Free Software Foundation; either version 2 of the License, or *
15
* (at your option) any later version. *
17
***************************************************************************/
18
/* $Id: qgsgpsplugin.cpp,v 1.2.2.1 2004/12/03 17:17:27 gsherman Exp $ */
22
#include "../../src/qgisapp.h"
23
#include "../../src/qgsmaplayerregistry.h"
24
#include "../../src/qgsmaplayer.h"
25
#include "../../src/qgsvectorlayer.h"
26
#include "../../src/qgsdataprovider.h"
27
#include "qgsgpsplugin.h"
30
#include <qeventloop.h>
31
#include <qfiledialog.h>
34
#include <qmessagebox.h>
35
#include <qpopupmenu.h>
36
#include <qlineedit.h>
38
#include <qapplication.h>
41
#include <qprogressdialog.h>
42
#include <qsettings.h>
43
#include <qstringlist.h>
51
#include "qgsgpsplugingui.h"
53
// xpm for creating the toolbar icon
57
#define QGISEXTERN extern "C" __declspec( dllexport )
59
#define QGISEXTERN extern "C"
63
static const char * const ident_ =
64
"$Id: qgsgpsplugin.cpp,v 1.2.2.1 2004/12/03 17:17:27 gsherman Exp $";
65
static const char * const name_ = "GPS Tools";
66
static const char * const description_ =
67
"Tools for loading and importing GPS data.";
68
static const char * const version_ = "Version 0.1";
69
static const QgisPlugin::PLUGINTYPE type_ = QgisPlugin::UI;
73
* Constructor for the plugin. The plugin is passed a pointer to the main app
74
* and an interface object that provides access to exposed functions in QGIS.
75
* @param qgis Pointer to the QGIS main window
76
* @param _qI Pointer to the QGIS interface object
78
QgsGPSPlugin::QgsGPSPlugin(QgisApp * theQGisApp, QgisIface * theQgisInterFace):
79
mMainWindowPointer(theQGisApp),
80
mQGisInterface(theQgisInterFace),
81
QgisPlugin(name_,description_,version_,type_)
86
QgsGPSPlugin::~QgsGPSPlugin()
88
// delete all our babel formats
89
BabelMap::iterator iter;
90
for (iter = mImporters.begin(); iter != mImporters.end(); ++iter)
92
for (iter = mDevices.begin(); iter != mDevices.end(); ++iter)
98
* Initialize the GUI interface for the plugin
100
void QgsGPSPlugin::initGui()
102
// add a menu with 2 items
103
QPopupMenu *pluginMenu = new QPopupMenu(mMainWindowPointer);
104
pluginMenu->insertItem(QIconSet(icon),"&Gps Tools", this, SLOT(run()));
105
int menuId = pluginMenu->insertItem("&Create new GPX layer", this, SLOT(createGPX()));
106
pluginMenu->setWhatsThis(menuId, "Creates a new GPX layer and displays it on the map canvas");
107
mMenuBarPointer = ((QMainWindow *) mMainWindowPointer)->menuBar();
108
mMenuId = mQGisInterface->addMenu("&Gps", pluginMenu);
110
// add an action to the toolbar
111
mQActionPointer = new QAction("Gps Tools", QIconSet(icon), "&Wmi",0,
113
mQActionPointer->setWhatsThis( "Creates a new GPX layer and displays it on the map canvas");
114
connect(mQActionPointer, SIGNAL(activated()), this, SLOT(run()));
115
mQGisInterface->addToolBarIcon(mQActionPointer);
118
//method defined in interface
119
void QgsGPSPlugin::help()
124
// Slot called when the buffer menu item is activated
125
void QgsGPSPlugin::run()
127
// find all GPX layers
128
std::vector<QgsVectorLayer*> gpxLayers;
129
std::map<QString, QgsMapLayer*>::const_iterator iter;
130
std::cerr<<"LAYERS: "<<mQGisInterface->getLayerRegistry()->
131
mapLayers().size()<<std::endl;
132
for (iter = mQGisInterface->getLayerRegistry()->mapLayers().begin();
133
iter != mQGisInterface->getLayerRegistry()->mapLayers().end(); ++iter) {
134
std::cerr<<iter->second->name()<<std::endl;
135
if (iter->second->type() == QgsMapLayer::VECTOR) {
136
QgsVectorLayer* vLayer = dynamic_cast<QgsVectorLayer*>(iter->second);
137
if (vLayer->providerType() == "gpx")
138
gpxLayers.push_back(vLayer);
141
std::cerr<<std::endl;
143
QgsGPSPluginGui *myPluginGui =
144
new QgsGPSPluginGui(mImporters, mDevices, gpxLayers, mMainWindowPointer,
145
"GPS Tools", true, 0);
146
//listen for when the layer has been made so we can draw it
147
connect(myPluginGui, SIGNAL(drawRasterLayer(QString)),
148
this, SLOT(drawRasterLayer(QString)));
149
connect(myPluginGui, SIGNAL(drawVectorLayer(QString,QString,QString)),
150
this, SLOT(drawVectorLayer(QString,QString,QString)));
151
connect(myPluginGui, SIGNAL(loadGPXFile(QString, bool, bool, bool)),
152
this, SLOT(loadGPXFile(QString, bool, bool, bool)));
153
connect(myPluginGui, SIGNAL(importGPSFile(QString, QgsBabelFormat*, bool,
154
bool, bool, QString, QString)),
155
this, SLOT(importGPSFile(QString, QgsBabelFormat*, bool, bool,
156
bool, QString, QString)));
157
connect(myPluginGui, SIGNAL(downloadFromGPS(QString, QString, bool, bool,
158
bool, QString, QString)),
159
this, SLOT(downloadFromGPS(QString, QString, bool, bool, bool,
161
connect(myPluginGui, SIGNAL(uploadToGPS(QgsVectorLayer*, QString, QString)),
162
this, SLOT(uploadToGPS(QgsVectorLayer*, QString, QString)));
163
connect(this, SIGNAL(closeGui()), myPluginGui, SLOT(close()));
169
void QgsGPSPlugin::createGPX() {
171
QFileDialog::getSaveFileName("." , "GPS eXchange file (*.gpx)",
172
mMainWindowPointer, "OpenFileDialog",
173
"Save new GPX file as...");
174
if (!fileName.isEmpty()) {
175
QFileInfo fileInfo(fileName);
176
std::ofstream ofs((const char*)fileName);
178
QMessageBox::warning(NULL, "Could not create file",
179
"Unable to create a GPX file with the given name. "
180
"Try again with another name or in another "
184
ofs<<"<gpx></gpx>"<<std::endl;
186
emit drawVectorLayer(fileName + "?type=track",
187
fileInfo.baseName() + ", tracks", "gpx");
188
emit drawVectorLayer(fileName + "?type=route",
189
fileInfo.baseName() + ", routes", "gpx");
190
emit drawVectorLayer(fileName + "?type=waypoint",
191
fileInfo.baseName() + ", waypoints", "gpx");
196
void QgsGPSPlugin::drawVectorLayer(QString thePathNameQString,
197
QString theBaseNameQString,
198
QString theProviderQString)
200
mQGisInterface->addVectorLayer(thePathNameQString, theBaseNameQString,
204
// Unload the plugin by cleaning up the GUI
205
void QgsGPSPlugin::unload()
208
mMenuBarPointer->removeItem(mMenuId);
209
mQGisInterface->removeToolBarIcon(mQActionPointer);
210
delete mQActionPointer;
213
void QgsGPSPlugin::loadGPXFile(QString filename, bool loadWaypoints, bool loadRoutes,
216
//check if input file is readable
217
QFileInfo fileInfo(filename);
218
if (!fileInfo.isReadable()) {
219
QMessageBox::warning(NULL, "GPX Loader",
220
"Unable to read the selected file.\n"
221
"Please reselect a valid file." );
225
// remember the directory
227
settings.writeEntry("/qgis/gps/gpxdirectory", fileInfo.dirPath());
229
// add the requested layers
231
emit drawVectorLayer(filename + "?type=track",
232
fileInfo.baseName() + ", tracks", "gpx");
234
emit drawVectorLayer(filename + "?type=route",
235
fileInfo.baseName() + ", routes", "gpx");
237
emit drawVectorLayer(filename + "?type=waypoint",
238
fileInfo.baseName() + ", waypoints", "gpx");
244
void QgsGPSPlugin::importGPSFile(QString inputFilename, QgsBabelFormat* importer,
245
bool importWaypoints, bool importRoutes,
246
bool importTracks, QString outputFilename,
249
// what features does the user want to import?
253
else if (importRoutes)
255
else if (importTracks)
258
// try to start the gpsbabel process
259
QStringList babelArgs =
260
importer->importCommand(mBabelPath, typeArg,
261
inputFilename, outputFilename);
262
QProcess babelProcess(babelArgs);
263
if (!babelProcess.start()) {
264
QMessageBox::warning(NULL, "Could not start process",
265
"Could not start GPSBabel!");
269
// wait for gpsbabel to finish (or the user to cancel)
270
QProgressDialog progressDialog("Importing data...", "Cancel", 0,
272
progressDialog.show();
273
for (int i = 0; babelProcess.isRunning(); ++i) {
274
QApplication::eventLoop()->processEvents(0);
275
progressDialog.setProgress(i/64);
276
if (progressDialog.wasCancelled())
280
// did we get any data?
281
if (babelProcess.exitStatus() != 0) {
282
QString babelError(babelProcess.readStderr());
283
QString errorMsg(QString("Could not import data from %1!\n\n")
284
.arg(inputFilename));
285
errorMsg += babelError;
286
QMessageBox::warning(NULL, "Error importing data", errorMsg);
292
emit drawVectorLayer(outputFilename + "?type=track",
295
emit drawVectorLayer(outputFilename + "?type=route",
298
emit drawVectorLayer(outputFilename + "?type=waypoint",
305
void QgsGPSPlugin::downloadFromGPS(QString device, QString port,
306
bool downloadWaypoints, bool downloadRoutes,
307
bool downloadTracks, QString outputFilename,
310
// what does the user want to download?
312
if (downloadWaypoints)
314
else if (downloadRoutes)
316
else if (downloadTracks)
319
// try to start the gpsbabel process
320
QStringList babelArgs =
321
mDevices[device]->importCommand(mBabelPath, typeArg,
322
port, outputFilename);
323
QProcess babelProcess(babelArgs);
324
if (!babelProcess.start()) {
325
QMessageBox::warning(NULL, "Could not start process",
326
"Could not start GPSBabel!");
330
// wait for gpsbabel to finish (or the user to cancel)
331
QProgressDialog progressDialog("Downloading data...", "Cancel", 0,
333
progressDialog.show();
334
for (int i = 0; babelProcess.isRunning(); ++i) {
335
QApplication::eventLoop()->processEvents(0);
336
progressDialog.setProgress(i/64);
337
if (progressDialog.wasCancelled())
341
// did we get any data?
342
if (babelProcess.exitStatus() != 0) {
343
QString babelError(babelProcess.readStderr());
344
QString errorMsg("Could not download data from GPS!\n\n");
345
errorMsg += babelError;
346
QMessageBox::warning(NULL, "Error downloading data", errorMsg);
351
if (downloadWaypoints)
352
emit drawVectorLayer(outputFilename + "?type=waypoint",
355
emit drawVectorLayer(outputFilename + "?type=route",
358
emit drawVectorLayer(outputFilename + "?type=track",
361
// everything was OK, remember the device and port for next time
363
settings.writeEntry("/qgis/gps/lastdldevice", device);
364
settings.writeEntry("/qgis/gps/lastdlport", port);
370
void QgsGPSPlugin::uploadToGPS(QgsVectorLayer* gpxLayer, QString device,
373
const QString& source(gpxLayer->getDataProvider()->getDataSourceUri());
375
// what kind of data does the user want to upload?
377
if (source.right(8) == "waypoint")
379
else if (source.right(5) == "route")
381
else if (source.right(5) == "track")
384
std::cerr<<source.right(8)<<std::endl;
388
// try to start the gpsbabel process
389
QStringList babelArgs =
390
mDevices[device]->exportCommand(mBabelPath, typeArg,
391
source.left(source.findRev('?')), port);
392
QProcess babelProcess(babelArgs);
393
if (!babelProcess.start()) {
394
QMessageBox::warning(NULL, "Could not start process",
395
"Could not start GPSBabel!");
399
// wait for gpsbabel to finish (or the user to cancel)
400
QProgressDialog progressDialog("Uploading data...", "Cancel", 0,
402
progressDialog.show();
403
for (int i = 0; babelProcess.isRunning(); ++i) {
404
QApplication::eventLoop()->processEvents(0);
405
progressDialog.setProgress(i/64);
406
if (progressDialog.wasCancelled())
410
// did we get an error?
411
if (babelProcess.exitStatus() != 0) {
412
QString babelError(babelProcess.readStderr());
413
QString errorMsg("Error while uploading data to GPS!\n\n");
414
errorMsg += babelError;
415
QMessageBox::warning(NULL, "Error uploading data", errorMsg);
419
// everything was OK, remember this device for next time
421
settings.writeEntry("/qgis/gps/lastuldevice", device);
422
settings.writeEntry("/qgis/gps/lastulport", port);
428
void QgsGPSPlugin::setupBabel() {
430
// where is gpsbabel?
432
mBabelPath = settings.readEntry("/qgis/gps/gpsbabelpath");
433
if (mBabelPath.isEmpty())
434
mBabelPath = "gpsbabel";
435
// the importable formats
436
mImporters["Geocaching.com .loc"] =
437
new QgsSimpleBabelFormat("geo", true, false, false);
438
mImporters["Magellan Mapsend"] =
439
new QgsSimpleBabelFormat("mapsend", true, true, true);
440
mImporters["Garmin PCX5"] =
441
new QgsSimpleBabelFormat("pcx", true, false, true);
442
mImporters["Garmin Mapsource"] =
443
new QgsSimpleBabelFormat("mapsource", true, true, true);
444
mImporters["GPSUtil"] =
445
new QgsSimpleBabelFormat("gpsutil", true, false, false);
446
mImporters["PocketStreets 2002/2003 Pushpin"] =
447
new QgsSimpleBabelFormat("psp", true, false, false);
448
mImporters["CoPilot Flight Planner"] =
449
new QgsSimpleBabelFormat("copilot", true, false, false);
450
mImporters["Magellan Navigator Companion"] =
451
new QgsSimpleBabelFormat("magnav", true, false, false);
452
mImporters["Holux"] =
453
new QgsSimpleBabelFormat("holux", true, false, false);
454
mImporters["Topo by National Geographic"] =
455
new QgsSimpleBabelFormat("tpg", true, false, false);
456
mImporters["TopoMapPro"] =
457
new QgsSimpleBabelFormat("tmpro", true, false, false);
458
mImporters["GeocachingDB"] =
459
new QgsSimpleBabelFormat("gcdb", true, false, false);
460
mImporters["Tiger"] =
461
new QgsSimpleBabelFormat("tiger", true, false, false);
462
mImporters["EasyGPS Binary Format"] =
463
new QgsSimpleBabelFormat("easygps", true, false, false);
464
mImporters["Delorme Routes"] =
465
new QgsSimpleBabelFormat("saroute", false, false, true);
466
mImporters["Navicache"] =
467
new QgsSimpleBabelFormat("navicache", true, false, false);
468
mImporters["PSITrex"] =
469
new QgsSimpleBabelFormat("psitrex", true, true, true);
470
mImporters["Delorme GPS Log"] =
471
new QgsSimpleBabelFormat("gpl", false, false, true);
472
mImporters["OziExplorer"] =
473
new QgsSimpleBabelFormat("ozi", true, false, false);
474
mImporters["NMEA Sentences"] =
475
new QgsSimpleBabelFormat("nmea", true, false, true);
476
mImporters["Delorme Street Atlas 2004 Plus"] =
477
new QgsSimpleBabelFormat("saplus", true, false, false);
478
mImporters["Microsoft Streets and Trips"] =
479
new QgsSimpleBabelFormat("s_and_t", true, false, false);
480
mImporters["NIMA/GNIS Geographic Names"] =
481
new QgsSimpleBabelFormat("nima", true, false, false);
482
mImporters["Maptech"] =
483
new QgsSimpleBabelFormat("mxf", true, false, false);
484
mImporters["Mapopolis.com Mapconverter Application"] =
485
new QgsSimpleBabelFormat("mapconverter", true, false, false);
486
mImporters["GPSman"] =
487
new QgsSimpleBabelFormat("gpsman", true, false, false);
488
mImporters["GPSDrive"] =
489
new QgsSimpleBabelFormat("gpsdrive", true, false, false);
490
mImporters["Fugawi"] =
491
new QgsSimpleBabelFormat("fugawi", true, false, false);
493
new QgsSimpleBabelFormat("dna", true, false, false);
495
// and the GPS devices
496
mDevices["Garmin serial"] =
497
new QgsBabelCommand("%babel -i garmin -o gpx /dev/ttyS0 %out",
498
"%babel -i gpx -o garmin %in /dev/ttyS0");
499
QStringList deviceNames = settings.readListEntry("/qgis/gps/devicelist");
500
QStringList::iterator iter;
501
for (iter = deviceNames.begin(); iter != deviceNames.end(); ++iter) {
502
QString download = settings.
503
readEntry(QString("/qgis/gps/devices/%1/download").arg(*iter), "");
504
QString upload = settings.
505
readEntry(QString("/qgis/gps/devices/%1/upload").arg(*iter), "");
506
mDevices[*iter] = new QgsBabelCommand(download, upload);
514
* Required extern functions needed for every plugin
515
* These functions can be called prior to creating an instance
516
* of the plugin class
518
// Class factory to return a new instance of the plugin class
519
QGISEXTERN QgisPlugin * classFactory(QgisApp * theQGisAppPointer,
520
QgisIface * theQgisInterfacePointer)
522
return new QgsGPSPlugin(theQGisAppPointer, theQgisInterfacePointer);
525
// Return the name of the plugin - note that we do not user class members as
526
// the class may not yet be insantiated when this method is called.
527
QGISEXTERN QString name()
532
// Return the description
533
QGISEXTERN QString description()
538
// Return the type (either UI or MapLayer plugin)
539
QGISEXTERN int type()
544
// Return the version number for the plugin
545
QGISEXTERN QString version()
551
QGISEXTERN void unload(QgisPlugin * thePluginPointer)
553
delete thePluginPointer;