~ubuntu-branches/ubuntu/utopic/qgis/utopic

« back to all changes in this revision

Viewing changes to python/plugins/osm/OsmPlugin.py

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""@package OsmPlugin
 
2
This is the main module of the OSM Plugin.
 
3
 
 
4
It shows/hides all tool buttons, widgets and dialogs.
 
5
 
 
6
After closing dialogs it does all actions related with their return codes.
 
7
 
 
8
/***************************************************************************
 
9
 *                                                                         *
 
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.                                   *
 
14
 *                                                                         *
 
15
 ***************************************************************************/
 
16
"""
 
17
 
 
18
 
 
19
from PyQt4.QtCore import *
 
20
from PyQt4.QtGui import *
 
21
from PyQt4.QtNetwork import *
 
22
from qgis.core import *
 
23
 
 
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 *
 
31
 
 
32
# initialize Qt resources from file resouces.py
 
33
import resources_rc
 
34
 
 
35
 
 
36
 
 
37
def versionNumber():
 
38
    """Returns current version number of OpenStreetMap plugin.
 
39
 
 
40
    @return current version number of the plugin
 
41
    """
 
42
    return "0.5"
 
43
 
 
44
 
 
45
 
 
46
class OsmPlugin:
 
47
    """OsmPlugin is the main class OSM Plugin module.
 
48
 
 
49
    It shows/hides all tool buttons, widgets and dialogs and after closing dialogs
 
50
    it does all actions related with their return codes.
 
51
    """
 
52
 
 
53
    def __init__(self, iface):
 
54
        """The constructor.
 
55
 
 
56
        @param iface QgisInterface object
 
57
        """
 
58
 
 
59
        self.iface=iface
 
60
        self.canvas=self.iface.mapCanvas()
 
61
        self.http=QHttp()
 
62
        self.outFile=None
 
63
        self.httpGetId=0
 
64
        self.httpRequestAborted=False
 
65
        self.fname=""
 
66
 
 
67
 
 
68
    def initGui(self):
 
69
        """Function initalizes GUI of the OSM Plugin.
 
70
        """
 
71
 
 
72
        self.dockWidgetVisible = False
 
73
 
 
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)
 
99
 
 
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)
 
107
 
 
108
        # create a toolbar
 
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)
 
117
 
 
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)
 
125
 
 
126
        # create manager of sqlite database(-s)
 
127
        self.dbm=OsmDatabaseManager(self)
 
128
 
 
129
        self.undoredo=None
 
130
        self.dockWidget=None
 
131
 
 
132
        # create widget for undo/redo actions
 
133
        self.undoredo=OsmUndoRedoDW(self)
 
134
        self.iface.addDockWidget(Qt.LeftDockWidgetArea,self.undoredo)
 
135
        self.undoredo.hide()
 
136
        QObject.connect(self.undoredo,SIGNAL("visibilityChanged(bool)"),self.__urVisibilityChanged)
 
137
        self.undoredo.setContentEnabled(False)
 
138
 
 
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)
 
144
 
 
145
 
 
146
    def unload(self):
 
147
        """Function unloads the OSM Plugin.
 
148
        """
 
149
 
 
150
        self.canvas.unsetMapTool(self.dockWidget.mapTool)
 
151
        del self.dockWidget.mapTool
 
152
        self.dockWidget.mapTool=None
 
153
 
 
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)
 
161
 
 
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)
 
167
 
 
168
        self.undoredo.clear()
 
169
        self.undoredo.close()
 
170
 
 
171
        self.iface.removeDockWidget(self.dockWidget)
 
172
        self.iface.removeDockWidget(self.undoredo)
 
173
 
 
174
        del self.dockWidget
 
175
        del self.undoredo
 
176
        self.dockWidget=None
 
177
        self.undoredo=None
 
178
 
 
179
        # remove toolbar
 
180
        del self.toolBar
 
181
 
 
182
        # w/o osm plugin we don't need osm layers
 
183
        self.dbm.removeAllOsmLayers()
 
184
 
 
185
 
 
186
    def loadOsmFromFile(self):
 
187
        """Function shows up the "Load OSM from file" dialog.
 
188
 
 
189
        After closing it, function calls the appropriate actions
 
190
        according to dialog's return code.
 
191
        """
 
192
 
 
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!")
 
196
            return
 
197
 
 
198
        # show modal dialog with OSM file selection
 
199
        self.dlgLoad=OsmLoadDlg(self)
 
200
 
 
201
        # continue only if OK button was clicked
 
202
        if self.dlgLoad.exec_()==0:
 
203
            return
 
204
 
 
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()
 
209
 
 
210
        self.dockWidget.setContentEnabled(True)
 
211
        self.undoredo.setContentEnabled(True)
 
212
 
 
213
        self.dataLoaded=True
 
214
 
 
215
 
 
216
    def saveOsmToFile(self):
 
217
        """Function shows up the "Save OSM to file" dialog.
 
218
 
 
219
        After closing it, function calls the appropriate actions
 
220
        according to dialog's return code.
 
221
        """
 
222
 
 
223
        if 'osm' not in QgsProviderRegistry.instance().providerList():
 
224
            QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
 
225
            return
 
226
 
 
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.")
 
231
            return
 
232
 
 
233
        # show modal dialog with OSM file selection
 
234
        self.dlgSave=OsmSaveDlg(self)
 
235
 
 
236
        # continue only if OK button was clicked
 
237
        if self.dlgSave.exec_()==0:
 
238
            return
 
239
 
 
240
 
 
241
    def downloadOsmData(self):
 
242
        """Function shows up the "Download OSM data" dialog.
 
243
 
 
244
        After closing it, function calls the appropriate actions
 
245
        according to dialog's return code.
 
246
        """
 
247
 
 
248
        if 'osm' not in QgsProviderRegistry.instance().providerList():
 
249
            QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
 
250
            return
 
251
 
 
252
        self.dlgDownload=OsmDownloadDlg(self)
 
253
        self.dlgDownload.exec_()
 
254
        if not self.dlgDownload.httpSuccess:
 
255
            return
 
256
 
 
257
        if not self.dlgDownload.autoLoadCheckBox.isChecked():
 
258
            return
 
259
 
 
260
        # create loading dialog, submit it
 
261
        self.dlgLoad=OsmLoadDlg(self)
 
262
        self.dlgLoad.setModal(True)
 
263
        self.dlgLoad.show()
 
264
        self.dlgLoad.close()
 
265
        self.dlgLoad.OSMFileEdit.setText(self.dlgDownload.destdirLineEdit.text())
 
266
        self.dlgLoad.styleCombo.setCurrentIndex(self.dlgDownload.styleCombo.currentIndex())
 
267
 
 
268
        if self.dlgDownload.chkCustomRenderer.isChecked():
 
269
            self.dlgLoad.chkCustomRenderer.setChecked(True)
 
270
        else:
 
271
            self.dlgLoad.chkCustomRenderer.setChecked(False)
 
272
 
 
273
        for row in xrange(self.dlgLoad.lstTags.count()):
 
274
            self.dlgLoad.lstTags.item(row).setCheckState(Qt.Checked)
 
275
 
 
276
        if self.dlgDownload.chkReplaceData.isChecked():
 
277
            self.dlgLoad.chkReplaceData.setChecked(True)
 
278
        else:
 
279
            self.dlgLoad.chkReplaceData.setChecked(False)
 
280
 
 
281
        self.dlgLoad.onOK()
 
282
 
 
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)
 
286
 
 
287
 
 
288
    def uploadOsmData(self):
 
289
        """Function shows up the "Upload OSM data" dialog.
 
290
 
 
291
        After closing it, function calls the appropriate actions
 
292
        according to dialog's return code.
 
293
        """
 
294
 
 
295
        if 'osm' not in QgsProviderRegistry.instance().providerList():
 
296
            QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
 
297
            return
 
298
 
 
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.")
 
304
            return
 
305
 
 
306
        self.dlgUpload=OsmUploadDlg(self)
 
307
        self.dlgUpload.exec_()
 
308
 
 
309
 
 
310
    def importData(self):
 
311
        """Function shows up the "Import OSM data" dialog.
 
312
 
 
313
        After closing it, function calls the appropriate actions
 
314
        according to dialog's return code.
 
315
        """
 
316
 
 
317
        if 'osm' not in QgsProviderRegistry.instance().providerList():
 
318
            QMessageBox.critical(None, "Sorry", "You don't have OSM provider installed!")
 
319
            return
 
320
 
 
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.")
 
325
            return
 
326
 
 
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.")
 
330
            return
 
331
 
 
332
        dlg.exec_()
 
333
 
 
334
 
 
335
    def showHideDockWidget(self):
 
336
        """Function shows/hides main dockable widget of the plugin ("OSM Feature" widget)
 
337
        """
 
338
 
 
339
        if self.dockWidget.isVisible():
 
340
            self.dockWidget.hide()
 
341
        else:
 
342
            self.dockWidget.show()
 
343
 
 
344
 
 
345
    def __urVisibilityChanged(self):
 
346
        """Function is called after visibilityChanged(...) signal is emitted on OSM Edit History widget.
 
347
 
 
348
        Function changes state of related checkbox according to the fact
 
349
        if widget is currently visible of not.
 
350
        """
 
351
 
 
352
        if self.undoredo.isVisible():
 
353
            self.dockWidget.urDetailsButton.setChecked(True)
 
354
        else:
 
355
            self.dockWidget.urDetailsButton.setChecked(False)
 
356
 
 
357
 
 
358
    def __ofVisibilityChanged(self):
 
359
        """Function is called after visibilityChanged(...) signal is emitted on OSM Feature widget.
 
360
 
 
361
        Function changes state of appropriate tool button according to the fact
 
362
        if widget is currently visible of not.
 
363
        """
 
364
 
 
365
        if self.dockWidget.isVisible():
 
366
            self.actionDockWidget.setChecked(True)
 
367
        else:
 
368
            self.actionDockWidget.setChecked(False)
 
369
 
 
370