2
This is the main module of the OSM Plugin.
4
It shows/hides all tool buttons, widgets and dialogs.
6
After closing dialogs it does all actions related with their return codes.
8
/***************************************************************************
10
* This program is free software; you can redistribute it and/or modify *
11
* it under the terms of the GNU General Public License as published by *
12
* the Free Software Foundation; either version 2 of the License, or *
13
* (at your option) any later version. *
15
***************************************************************************/
19
from PyQt4.QtCore import *
20
from PyQt4.QtGui import *
21
from PyQt4.QtNetwork import *
22
from qgis.core import *
24
from OsmLoadDlg import OsmLoadDlg
25
from OsmSaveDlg import OsmSaveDlg
26
from OsmDownloadDlg import OsmDownloadDlg
27
from OsmUploadDlg import OsmUploadDlg
28
from OsmImportDlg import OsmImportDlg
29
from OsmFeatureDW import *
30
from OsmUndoRedoDW import *
32
# initialize Qt resources from file resouces.py
38
"""Returns current version number of OpenStreetMap plugin.
40
@return current version number of the plugin
47
"""OsmPlugin is the main class OSM Plugin module.
49
It shows/hides all tool buttons, widgets and dialogs and after closing dialogs
50
it does all actions related with their return codes.
53
def __init__(self, iface):
56
@param iface QgisInterface object
60
self.canvas=self.iface.mapCanvas()
64
self.httpRequestAborted=False
69
"""Function initalizes GUI of the OSM Plugin.
72
self.dockWidgetVisible = False
74
# create action for loading OSM file
75
self.actionLoad=QAction(QIcon(":/plugins/osm_plugin/images/osm_load.png")
76
,"Load OSM from file", self.iface.mainWindow())
77
self.actionLoad.setWhatsThis("Load OpenStreetMap from file")
78
# create action for import of a layer into OSM
79
self.actionImport=QAction(QIcon(":/plugins/osm_plugin/images/osm_import.png")
80
,"Import data from a layer", self.iface.mainWindow())
81
self.actionImport.setWhatsThis("Import data from a layer to OpenStreetMap")
82
# create action for saving OSM file
83
self.actionSave=QAction(QIcon(":/plugins/osm_plugin/images/osm_save.png")
84
,"Save OSM to file", self.iface.mainWindow())
85
self.actionSave.setWhatsThis("Save OpenStreetMap to file")
86
# create action for OSM data downloading
87
self.actionDownload=QAction(QIcon(":/plugins/osm_plugin/images/osm_download.png")
88
,"Download OSM data", self.iface.mainWindow())
89
self.actionDownload.setWhatsThis("Download OpenStreetMap data")
90
# create action for OSM data downloading
91
self.actionUpload=QAction(QIcon(":/plugins/osm_plugin/images/osm_upload.png")
92
,"Upload OSM data", self.iface.mainWindow())
93
self.actionUpload.setWhatsThis("Upload OpenStreetMap data")
94
# create action for OSM dockable window
95
self.actionDockWidget=QAction(QIcon(":/plugins/osm_plugin/images/osm_featureManager.png")
96
,"Show/Hide OSM Feature Manager",self.iface.mainWindow())
97
self.actionDockWidget.setWhatsThis("Show/Hide OpenStreetMap Feature Manager")
98
self.actionDockWidget.setCheckable(True)
100
# connect new action to plugin function - when action is triggered
101
QObject.connect(self.actionLoad, SIGNAL("triggered()"), self.loadOsmFromFile)
102
QObject.connect(self.actionSave, SIGNAL("triggered()"), self.saveOsmToFile)
103
QObject.connect(self.actionDownload, SIGNAL("triggered()"), self.downloadOsmData)
104
QObject.connect(self.actionUpload, SIGNAL("triggered()"), self.uploadOsmData)
105
QObject.connect(self.actionDockWidget, SIGNAL("triggered()"), self.showHideDockWidget)
106
QObject.connect(self.actionImport, SIGNAL("triggered()"), self.importData)
109
self.toolBar=self.iface.addToolBar("OpenStreetMap")
110
self.toolBar.setObjectName("OpenStreetMap")
111
self.toolBar.addAction(self.actionLoad)
112
self.toolBar.addAction(self.actionDockWidget)
113
self.toolBar.addAction(self.actionDownload)
114
self.toolBar.addAction(self.actionUpload)
115
self.toolBar.addAction(self.actionImport)
116
self.toolBar.addAction(self.actionSave)
118
# populate plugins menu
119
self.iface.addPluginToMenu("&OpenStreetMap", self.actionLoad)
120
self.iface.addPluginToMenu("&OpenStreetMap", self.actionDockWidget)
121
self.iface.addPluginToMenu("&OpenStreetMap", self.actionDownload)
122
self.iface.addPluginToMenu("&OpenStreetMap", self.actionUpload)
123
self.iface.addPluginToMenu("&OpenStreetMap", self.actionImport)
124
self.iface.addPluginToMenu("&OpenStreetMap", self.actionSave)
126
# create manager of sqlite database(-s)
127
self.dbm=OsmDatabaseManager(self)
132
# create widget for undo/redo actions
133
self.undoredo=OsmUndoRedoDW(self)
134
self.iface.addDockWidget(Qt.LeftDockWidgetArea,self.undoredo)
136
QObject.connect(self.undoredo,SIGNAL("visibilityChanged(bool)"),self.__urVisibilityChanged)
137
self.undoredo.setContentEnabled(False)
139
# create widget for osm feature info
140
self.dockWidget=OsmFeatureDW(self)
141
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockWidget)
142
QObject.connect(self.dockWidget,SIGNAL("visibilityChanged(bool)"),self.__ofVisibilityChanged)
143
self.dockWidget.setContentEnabled(False)
147
"""Function unloads the OSM Plugin.
150
self.canvas.unsetMapTool(self.dockWidget.mapTool)
151
del self.dockWidget.mapTool
152
self.dockWidget.mapTool=None
154
# remove the plugin menu items
155
self.iface.removePluginMenu("&OpenStreetMap",self.actionLoad)
156
self.iface.removePluginMenu("&OpenStreetMap",self.actionSave)
157
self.iface.removePluginMenu("&OpenStreetMap",self.actionDownload)
158
self.iface.removePluginMenu("&OpenStreetMap",self.actionUpload)
159
self.iface.removePluginMenu("&OpenStreetMap",self.actionImport)
160
self.iface.removePluginMenu("&OpenStreetMap",self.actionDockWidget)
162
self.dockWidget.close()
163
if self.dockWidget.rubBand:
164
self.dockWidget.rubBand.reset(False)
165
if self.dockWidget.rubBandPol:
166
self.dockWidget.rubBandPol.reset(True)
168
self.undoredo.clear()
169
self.undoredo.close()
171
self.iface.removeDockWidget(self.dockWidget)
172
self.iface.removeDockWidget(self.undoredo)
182
# w/o osm plugin we don't need osm layers
183
self.dbm.removeAllOsmLayers()
186
def loadOsmFromFile(self):
187
"""Function shows up the "Load OSM from file" dialog.
189
After closing it, function calls the appropriate actions
190
according to dialog's return code.
193
# sanity check whether we're able to load osm data
194
if 'osm' not in QgsProviderRegistry.instance().providerList():
195
QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
198
# show modal dialog with OSM file selection
199
self.dlgLoad=OsmLoadDlg(self)
201
# continue only if OK button was clicked
202
if self.dlgLoad.exec_()==0:
205
self.fname=self.dlgLoad.OSMFileEdit.text()
206
self.dbFileName=self.fname+".db"
207
self.dbm.addDatabase(self.dbFileName,self.dlgLoad.pointLayer,self.dlgLoad.lineLayer,self.dlgLoad.polygonLayer)
208
self.undoredo.clear()
210
self.dockWidget.setContentEnabled(True)
211
self.undoredo.setContentEnabled(True)
216
def saveOsmToFile(self):
217
"""Function shows up the "Save OSM to file" dialog.
219
After closing it, function calls the appropriate actions
220
according to dialog's return code.
223
if 'osm' not in QgsProviderRegistry.instance().providerList():
224
QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
227
if not self.dbm.currentKey:
228
QMessageBox.information(QWidget(), QString("OSM Save to file")
229
,"No OSM data are loaded/downloaded or no OSM layer is selected in Layers panel. \
230
Please change this situation first, because OSM Plugin doesn't know what to save.")
233
# show modal dialog with OSM file selection
234
self.dlgSave=OsmSaveDlg(self)
236
# continue only if OK button was clicked
237
if self.dlgSave.exec_()==0:
241
def downloadOsmData(self):
242
"""Function shows up the "Download OSM data" dialog.
244
After closing it, function calls the appropriate actions
245
according to dialog's return code.
248
if 'osm' not in QgsProviderRegistry.instance().providerList():
249
QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
252
self.dlgDownload=OsmDownloadDlg(self)
253
self.dlgDownload.exec_()
254
if not self.dlgDownload.httpSuccess:
257
if not self.dlgDownload.autoLoadCheckBox.isChecked():
260
# create loading dialog, submit it
261
self.dlgLoad=OsmLoadDlg(self)
262
self.dlgLoad.setModal(True)
265
self.dlgLoad.OSMFileEdit.setText(self.dlgDownload.destdirLineEdit.text())
266
self.dlgLoad.styleCombo.setCurrentIndex(self.dlgDownload.styleCombo.currentIndex())
268
if self.dlgDownload.chkCustomRenderer.isChecked():
269
self.dlgLoad.chkCustomRenderer.setChecked(True)
271
self.dlgLoad.chkCustomRenderer.setChecked(False)
273
for row in xrange(self.dlgLoad.lstTags.count()):
274
self.dlgLoad.lstTags.item(row).setCheckState(Qt.Checked)
276
if self.dlgDownload.chkReplaceData.isChecked():
277
self.dlgLoad.chkReplaceData.setChecked(True)
279
self.dlgLoad.chkReplaceData.setChecked(False)
283
self.fname=self.dlgLoad.OSMFileEdit.text()
284
self.dbFileName=self.fname+".db"
285
self.dbm.addDatabase(self.dbFileName,self.dlgLoad.pointLayer,self.dlgLoad.lineLayer,self.dlgLoad.polygonLayer)
288
def uploadOsmData(self):
289
"""Function shows up the "Upload OSM data" dialog.
291
After closing it, function calls the appropriate actions
292
according to dialog's return code.
295
if 'osm' not in QgsProviderRegistry.instance().providerList():
296
QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
299
# first check if there are some data; if not upload doesn't have sense
300
if not self.dbm.currentKey:
301
QMessageBox.information(QWidget(), QString("OSM Upload")
302
,"No OSM data are loaded/downloaded or no OSM layer is selected in Layers panel. \
303
Please change this situation first, because OSM Plugin doesn't know what to upload.")
306
self.dlgUpload=OsmUploadDlg(self)
307
self.dlgUpload.exec_()
310
def importData(self):
311
"""Function shows up the "Import OSM data" dialog.
313
After closing it, function calls the appropriate actions
314
according to dialog's return code.
317
if 'osm' not in QgsProviderRegistry.instance().providerList():
318
QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
321
if self.dbm.currentKey is None:
322
QMessageBox.information(self.iface.mainWindow(), "OSM Import"
323
,"No OSM data are loaded/downloaded or no OSM layer is selected in Layers panel. \
324
Please change this situation first, because OSM Plugin doesn't know what layer will be destination of the import.")
327
dlg=OsmImportDlg(self)
328
if dlg.cboLayer.count()==0:
329
QMessageBox.information(self.iface.mainWindow(), "OSM Import", "There are currently no available vector layers.")
335
def showHideDockWidget(self):
336
"""Function shows/hides main dockable widget of the plugin ("OSM Feature" widget)
339
if self.dockWidget.isVisible():
340
self.dockWidget.hide()
342
self.dockWidget.show()
345
def __urVisibilityChanged(self):
346
"""Function is called after visibilityChanged(...) signal is emitted on OSM Edit History widget.
348
Function changes state of related checkbox according to the fact
349
if widget is currently visible of not.
352
if self.undoredo.isVisible():
353
self.dockWidget.urDetailsButton.setChecked(True)
355
self.dockWidget.urDetailsButton.setChecked(False)
358
def __ofVisibilityChanged(self):
359
"""Function is called after visibilityChanged(...) signal is emitted on OSM Feature widget.
361
Function changes state of appropriate tool button according to the fact
362
if widget is currently visible of not.
365
if self.dockWidget.isVisible():
366
self.actionDockWidget.setChecked(True)
368
self.actionDockWidget.setChecked(False)