/
roadgraphplugin.cpp
357 lines (300 loc) · 11.5 KB
/
roadgraphplugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
roadgraphplugin.cpp - implemention of plugin
--------------------------------------
Date : 2010-10-10
Copyright : (C) 2010 by Yakushev Sergey
Email : YakushevS <at> list.ru
****************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// QGIS Specific includes
#include <qgsapplication.h>
#include <qgisinterface.h>
#include <qgisgui.h>
#include <qgsmapcanvas.h>
#include <qgsproject.h>
#include <qgsmaptoolemitpoint.h>
#include <qgsmaprenderer.h>
#include <qgsmaplayerregistry.h>
#include <qgsvectorlayer.h>
#include <qgsvectordataprovider.h>
#include <qgslinevectorlayerdirector.h>
#include <qgsgraphbuilder.h>
#include <qgsgraph.h>
#include <qgsdistancearcproperter.h>
// Road grap plugin includes
#include "roadgraphplugin.h"
#include "shortestpathwidget.h"
#include "settingsdlg.h"
#include "speedproperter.h"
#include "units.h"
#include "linevectorlayersettings.h"
//
// Qt4 Related Includes
//
#include <QAction>
#include <QLabel>
#include <QLocale>
#include <QToolBar>
#include <QPushButton>
#include <QDockWidget>
#include <QVBoxLayout>
#include <QDebug>
// standard includes
static const QString sName = QObject::tr( "Road graph plugin" );
static const QString sDescription = QObject::tr( "It solves the shortest path problem." );
static const QString sPluginVersion = QObject::tr( "Version 0.1" );
static const QgisPlugin::PLUGINTYPE sPluginType = QgisPlugin::UI;
//////////////////////////////////////////////////////////////////////
//
// THE FOLLOWING METHODS ARE MANDATORY FOR ALL PLUGINS
//
//////////////////////////////////////////////////////////////////////
/**
* Constructor for the plugin. The plugin is passed a pointer
* an interface object that provides access to exposed functions in QGIS.
* @param theQgisInterface - Pointer to the QGIS interface object
*/
RoadGraphPlugin::RoadGraphPlugin( QgisInterface * theQgisInterface ):
QgisPlugin( sName, sDescription, sPluginVersion, sPluginType ),
mQGisIface( theQgisInterface )
{
mQShortestPathDock = NULL;
mSettings = new RgLineVectorLayerSettings();
mTimeUnitName = "h";
mDistanceUnitName = "km";
mTopologyToleranceFactor = 0.0;
}
RoadGraphPlugin::~RoadGraphPlugin()
{
}
/*
* Initialize the GUI interface for the plugin - this is only called once when the plugin is
* added to the plugin registry in the QGIS application.
*/
void RoadGraphPlugin::initGui()
{
// create shorttest path dock
mQShortestPathDock = new RgShortestPathWidget( mQGisIface->mainWindow() , this );
mQGisIface->addDockWidget( Qt::LeftDockWidgetArea, mQShortestPathDock );
// Create the action for tool
mQSettingsAction = new QAction( QIcon( ":/roadgraph/road.png" ), tr( "Road graph settings" ), this );
mInfoAction = new QAction( QIcon( ":/roadgraph/about.png" ), tr( "About" ), this );
// Set the what's this text
mQSettingsAction->setWhatsThis( tr( "Road graph plugin settings" ) );
mInfoAction->setWhatsThis( tr( "About Road graph plugin" ) );
setGuiElementsToDefault();
// Connect the action to slots
connect( mQSettingsAction, SIGNAL( triggered() ), this, SLOT( property() ) );
connect( mInfoAction, SIGNAL( triggered() ), SLOT( about() ) );
mQGisIface->addPluginToMenu( tr( "Road graph" ), mQSettingsAction );
mQGisIface->addPluginToMenu( tr( "Road graph" ), mInfoAction );
connect( mQGisIface, SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
connect( mQGisIface, SIGNAL( newProjectCreated() ), this, SLOT( newProject() ) );
connect( mQGisIface, SIGNAL( projectRead() ), mQShortestPathDock, SLOT( clear() ) );
connect( mQGisIface, SIGNAL( newProjectCreated() ), mQShortestPathDock, SLOT( clear() ) );
// load settings
projectRead();
} // RoadGraphPlugin::initGui()
// Unload the plugin by cleaning up the GUI
void RoadGraphPlugin::unload()
{
// remove the GUI
mQGisIface->removePluginMenu( tr( "Road graph" ), mQSettingsAction );
mQGisIface->removePluginMenu( tr( "Road graph" ), mInfoAction );
// disconnect
disconnect( mQGisIface->mainWindow(), SIGNAL( projectRead() ), this, SLOT( projectRead() ) );
disconnect( mQGisIface->mainWindow(), SIGNAL( newProject() ), this, SLOT( newProject() ) );
delete mQSettingsAction;
delete mQShortestPathDock;
} // RoadGraphPlugin::unload()
void RoadGraphPlugin::setGuiElementsToDefault()
{
} // RoadGraphPlugin::setGuiElementsToDefault()
//method defined in interface
void RoadGraphPlugin::help()
{
//implement me!
} // RoadGraphPlugin::help()
void RoadGraphPlugin::onShowDirection()
{
mQGisIface->mapCanvas()->refresh();
} // RoadGraphPlugin::onShowDirection()
void RoadGraphPlugin::newProject()
{
setGuiElementsToDefault();
}
void RoadGraphPlugin::property()
{
RgSettingsDlg dlg( mSettings, mQGisIface->mainWindow(), QgisGui::ModalDialogFlags );
dlg.setTimeUnitName( mTimeUnitName );
dlg.setDistanceUnitName( mDistanceUnitName );
dlg.setTopologyTolerance( mTopologyToleranceFactor );
if ( !dlg.exec() )
return;
mTimeUnitName = dlg.timeUnitName();
mDistanceUnitName = dlg.distanceUnitName();
mTopologyToleranceFactor = dlg.topologyTolerance();
mSettings->write( QgsProject::instance() );
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginTimeUnit", mTimeUnitName );
QgsProject::instance()->writeEntry( "roadgraphplugin", "/pluginDistanceUnit", mDistanceUnitName );
QgsProject::instance()->writeEntry( "roadgraphplugin", "/topologyToleranceFactor", mTopologyToleranceFactor );
setGuiElementsToDefault();
} //RoadGraphPlugin::property()
void RoadGraphPlugin::about()
{
QDialog dlg( mQGisIface->mainWindow() );
dlg.setWindowFlags( dlg.windowFlags() | Qt::MSWindowsFixedSizeDialogHint );
dlg.setWindowFlags( dlg.windowFlags() &~ Qt::WindowContextHelpButtonHint );
dlg.setWindowTitle( tr( "About RoadGraph" ) );
QVBoxLayout *lines = new QVBoxLayout( &dlg );
QLabel *title = new QLabel( "<b>RoadGraph plugin</b>" );
title->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
QLabel *version = new QLabel( sPluginVersion );
version->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
lines->addWidget( title );
lines->addWidget( version );
lines->addWidget( new QLabel( tr( "Find shortest path on road's graph" ) ) );
lines->addWidget( new QLabel( tr( "<b>Developers:</b>" ) ) );
lines->addWidget( new QLabel( " Sergey Yakushev" ) );
lines->addWidget( new QLabel( tr( "<b>Homepage:</b>" ) ) );
QSettings settings;
QString localeFullName, localeShortName;
bool overrideLocale = settings.value( "locale/overrideFlag", QVariant( false ) ).toBool();
if ( !overrideLocale )
{
localeFullName = QLocale().system().name();
}
else
{
localeFullName = settings.value( "locale/userLocale", QVariant( "" ) ).toString();
}
localeShortName = localeFullName.left( 2 );
QLabel *link = new QLabel();
if ( localeShortName == "ru" || localeShortName == "uk" )
{
link->setText( "<a href=\"http://gis-lab.info/qa/road-graph.html\">http://gis-lab.info/qa/road-graph.html</a>" );
}
else
{
link->setText( "<a href=\"http://gis-lab.info/qa/road-graph-eng.html\">http://gis-lab.info/qa/road-graph-eng.html</a>" );
}
link->setOpenExternalLinks( true );
lines->addWidget( link );
QPushButton *btnClose = new QPushButton( tr( "Close" ) );
lines->addWidget( btnClose );
QObject::connect( btnClose, SIGNAL( clicked() ), &dlg, SLOT( close() ) );
dlg.exec();
} //RoadGraphPlugin::about()
void RoadGraphPlugin::projectRead()
{
mSettings->read( QgsProject::instance() );
mTimeUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginTimeUnit", "h" );
mDistanceUnitName = QgsProject::instance()->readEntry( "roadgraphplugin", "/pluginDistanceUnit", "km" );
mTopologyToleranceFactor =
QgsProject::instance()->readDoubleEntry( "roadgraphplugin", "/topologyToleranceFactor", 0.0 );
setGuiElementsToDefault();
}// RoadGraphplguin::projectRead()
QgisInterface* RoadGraphPlugin::iface()
{
return mQGisIface;
}
const QgsGraphDirector* RoadGraphPlugin::director() const
{
QString layerId;
QgsVectorLayer *layer = NULL;
QMap< QString, QgsMapLayer* > mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap< QString, QgsMapLayer* >::const_iterator it;
for ( it = mapLayers.begin(); it != mapLayers.end(); ++it )
{
if ( it.value()->name() != mSettings->mLayer )
continue;
layerId = it.key();
layer = dynamic_cast< QgsVectorLayer* >( it.value() );
break;
}
if ( layer == NULL )
return NULL;
if ( layer->geometryType() == QGis::Line )
{
QgsVectorDataProvider *provider = dynamic_cast< QgsVectorDataProvider* >( layer->dataProvider() );
if ( provider == NULL )
return NULL;
SpeedUnit speedUnit = SpeedUnit::byName( mSettings->mSpeedUnitName );
QgsLineVectorLayerDirector * director =
new QgsLineVectorLayerDirector( layerId,
provider->fieldNameIndex( mSettings->mDirection ),
mSettings->mFirstPointToLastPointDirectionVal,
mSettings->mLastPointToFirstPointDirectionVal,
mSettings->mBothDirectionVal,
mSettings->mDefaultDirection
);
director->addProperter( new QgsDistanceArcProperter() );
director->addProperter( new RgSpeedProperter( provider->fieldNameIndex( mSettings->mSpeed ),
mSettings->mDefaultSpeed, speedUnit.multipler() ) );
return director;
}
return NULL;
}
QString RoadGraphPlugin::timeUnitName()
{
return mTimeUnitName;
}
QString RoadGraphPlugin::distanceUnitName()
{
return mDistanceUnitName;
}
double RoadGraphPlugin::topologyToleranceFactor()
{
return mTopologyToleranceFactor;
}
//////////////////////////////////////////////////////////////////////////
//
//
// THE FOLLOWING CODE IS AUTOGENERATED BY THE PLUGIN BUILDER SCRIPT
// YOU WOULD NORMALLY NOT NEED TO MODIFY THIS, AND YOUR PLUGIN
// MAY NOT WORK PROPERLY IF YOU MODIFY THIS INCORRECTLY
//
//
//////////////////////////////////////////////////////////////////////////
/**
* Required extern functions needed for every plugin
* These functions can be called prior to creating an instance
* of the plugin class
*/
// Class factory to return a new instance of the plugin class
QGISEXTERN QgisPlugin * classFactory( QgisInterface * theQgisInterfacePointer )
{
return new RoadGraphPlugin( theQgisInterfacePointer );
}
// Return the name of the plugin - note that we do not user class members as
// the class may not yet be insantiated when this method is called.
QGISEXTERN QString name()
{
return sName;
}
// Return the description
QGISEXTERN QString description()
{
return sDescription;
}
// Return the type (either UI or MapLayer plugin)
QGISEXTERN int type()
{
return sPluginType;
}
// Return the version number for the plugin
QGISEXTERN QString version()
{
return sPluginVersion;
}
// Delete ourself
QGISEXTERN void unload( QgisPlugin * thePluginPointer )
{
delete thePluginPointer;
}