~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to python/plugins/osm/OsmFeatureDW.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
# -*- coding: utf-8 -*-99
 
2
"""@package OsmFeatureDW
 
3
This module is descendant of "OSM Feature" dockable widget and makes user able
 
4
to view and edit information on selected OSM feature.
 
5
 
 
6
OsmFeatureDW module shows details of selected feature - its basic info, tags and relations.
 
7
It provides methods for editing features' tags so that user can edit them directly on the widget.
 
8
 
 
9
There are also some identify and edit buttons on "OsmFeatureDW" - this modul implements all the methods that are called
 
10
after clicking on these buttons. Such methods creates (and set) map tool that coresponds to specified button.
 
11
"""
 
12
 
 
13
 
 
14
from PyQt4.QtCore import *
 
15
from PyQt4.QtGui import *
 
16
from qgis.core import *
 
17
from qgis.gui import *
 
18
 
 
19
from ui_OsmFeatureDW import Ui_OsmFeatureDW
 
20
from OsmAddRelationDlg import OsmAddRelationDlg
 
21
import OsmTags
 
22
 
 
23
# include all available osm map tools
 
24
from map_tools.OsmCreatePointMT import OsmCreatePointMT
 
25
from map_tools.OsmCreateLineMT import OsmCreateLineMT
 
26
from map_tools.OsmCreatePolygonMT import OsmCreatePolygonMT
 
27
from map_tools.OsmMoveMT import OsmMoveMT
 
28
from map_tools.OsmIdentifyMT import OsmIdentifyMT
 
29
import unicodedata
 
30
 
 
31
 
 
32
class OsmFeatureDW(QDockWidget, Ui_OsmFeatureDW,  object):
 
33
    """This class shows details of selected feature - its basic info, tags and relations.
 
34
 
 
35
    It provides methods for editing features' tags so that user can edit them directly on the widget.
 
36
 
 
37
    There are also some identify and edit buttons on "OsmFeatureDW" - this modul implements all the methods that are called
 
38
    after clicking them. Such methods creates (and set) map tool that coresponds to specified button.
 
39
    """
 
40
 
 
41
 
 
42
    def __init__(self, plugin):
 
43
        """The constructor."""
 
44
 
 
45
        QDockWidget.__init__(self, None)
 
46
        self.setupUi(self)
 
47
        self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
 
48
 
 
49
        self.plugin=plugin
 
50
        self.mapTool=None
 
51
        self.__dlgAddRel=None
 
52
 
 
53
        # set icons for tool buttons (identify,move,createPoint,createLine,createPolygon)
 
54
        self.identifyButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_identify.png"))
 
55
        self.moveButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_move.png"))
 
56
        self.createPointButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createPoint.png"))
 
57
        self.createLineButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createLine.png"))
 
58
        self.createPolygonButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createPolygon.png"))
 
59
        self.createRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createRelation.png"))
 
60
        self.removeButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeFeat.png"))
 
61
        self.deleteTagsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeTag.png"))
 
62
        self.undoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_undo.png"))
 
63
        self.redoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_redo.png"))
 
64
        self.addRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_addRelation.png"))
 
65
        self.removeRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeRelation.png"))
 
66
        self.editRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_editRelation.png"))
 
67
        self.urDetailsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_urDetails.png"))
 
68
 
 
69
        # initializing group of edit buttons
 
70
        self.toolButtons=QButtonGroup(self)
 
71
        self.dummyButton.setVisible(False)
 
72
        self.toolButtons.addButton(self.dummyButton)
 
73
        self.toolButtons.addButton(self.identifyButton)
 
74
        self.toolButtons.addButton(self.moveButton)
 
75
        self.toolButtons.addButton(self.createPointButton)
 
76
        self.toolButtons.addButton(self.createLineButton)
 
77
        self.toolButtons.addButton(self.createPolygonButton)
 
78
        self.toolButtons.setExclusive(True)
 
79
 
 
80
        # initializing table of feature tags
 
81
        self.tagTable.setColumnCount(2)
 
82
        self.tagTable.setHorizontalHeaderItem(0,QTableWidgetItem("Key"))
 
83
        self.tagTable.setHorizontalHeaderItem(1,QTableWidgetItem("Value"))
 
84
        self.tagTable.setSelectionMode(QAbstractItemView.ExtendedSelection)
 
85
        self.tagTable.setSelectionBehavior(QAbstractItemView.SelectRows)
 
86
        self.newTagLabel="<new tag here>"
 
87
        self.relTagsTree.setSelectionMode(QAbstractItemView.NoSelection)
 
88
 
 
89
        # initializing rubberbands/vertexmarkers; getting qgis settings of line width and color for rubberbands
 
90
        settings=QSettings()
 
91
        qgsLineWidth=2 # use fixed width
 
92
        qgsLineRed=settings.value( "/qgis/digitizing/line_color_red", QVariant(255) ).toInt()
 
93
        qgsLineGreen=settings.value( "/qgis/digitizing/line_color_green", QVariant(0) ).toInt()
 
94
        qgsLineBlue=settings.value( "/qgis/digitizing/line_color_blue", QVariant(0) ).toInt()
 
95
 
 
96
        self.rubBandPol=QgsRubberBand(plugin.canvas,True)
 
97
        self.rubBandPol.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
 
98
        self.rubBandPol.setWidth(qgsLineWidth)
 
99
 
 
100
        self.rubBand=QgsRubberBand(plugin.canvas,False)
 
101
        self.rubBand.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
 
102
        self.rubBand.setWidth(qgsLineWidth)
 
103
 
 
104
        self.verMarker=QgsVertexMarker(plugin.canvas)
 
105
        self.verMarker.setIconType(2)
 
106
        self.verMarker.setIconSize(13)
 
107
        self.verMarker.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
 
108
        self.verMarker.setPenWidth(qgsLineWidth)
 
109
        self.verMarkers=[]
 
110
 
 
111
        self.relRubBandPol=QgsRubberBand(plugin.canvas,True)
 
112
        self.relRubBandPol.setColor(QColor(qgsLineRed[0],50,50))
 
113
        self.relRubBandPol.setWidth(qgsLineWidth+4)
 
114
 
 
115
        self.relRubBand=QgsRubberBand(plugin.canvas,False)
 
116
        self.relRubBand.setColor(QColor(qgsLineRed[0],50,50))
 
117
        self.relRubBand.setWidth(qgsLineWidth+4)
 
118
 
 
119
        self.relVerMarker=QgsVertexMarker(plugin.canvas)
 
120
        self.relVerMarker.setIconType(2)
 
121
        self.relVerMarker.setIconSize(13)
 
122
        self.relVerMarker.setColor(QColor(qgsLineRed[0],50,50))
 
123
        self.relVerMarker.setPenWidth(qgsLineWidth)
 
124
 
 
125
        # initializing inner variables
 
126
        self.activeEditButton=self.dummyButton
 
127
        self.__tagsLoaded=False
 
128
        self.__relTagsLoaded=False
 
129
        self.feature=None
 
130
        self.featureType=None
 
131
        self.featRels=[]
 
132
        self.featRelTags=[]
 
133
        self.featRelMembers=[]
 
134
 
 
135
        # clear all widget items
 
136
        self.clear()
 
137
 
 
138
        self.__connectWidgetSignals()
 
139
 
 
140
        self.removeButton.setEnabled(False)
 
141
        self.createRelationButton.setCheckable(False)
 
142
 
 
143
        # set current tab to "Properties"
 
144
        self.propRelBox.setCurrentIndex(0)
 
145
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
146
 
 
147
        # init coordinate transform
 
148
        self.projectionChanged()
 
149
 
 
150
        renderer=self.plugin.canvas.mapRenderer()
 
151
        self.connect(renderer, SIGNAL("hasCrsTransformEnabled(bool)"), self.projectionChanged)
 
152
        self.connect(renderer, SIGNAL("destinationSrsChanged()"), self.projectionChanged)
 
153
 
 
154
 
 
155
    def setContentEnabled(self,flag):
 
156
 
 
157
        self.featInfoBox.setEnabled(flag)
 
158
        self.propRelBox.setEnabled(flag)
 
159
        self.identifyButton.setEnabled(flag)
 
160
        self.moveButton.setEnabled(flag)
 
161
        self.createPointButton.setEnabled(flag)
 
162
        self.createLineButton.setEnabled(flag)
 
163
        self.createPolygonButton.setEnabled(flag)
 
164
        self.createRelationButton.setEnabled(flag)
 
165
 
 
166
        if flag:
 
167
            if self.plugin.undoredo.undoCounter>0:
 
168
                self.undoButton.setEnabled(True)
 
169
            if self.plugin.undoredo.redoCounter>0:
 
170
                self.redoButton.setEnabled(True)
 
171
        else:
 
172
            self.undoButton.setEnabled(False)
 
173
            self.redoButton.setEnabled(False)
 
174
 
 
175
        self.urDetailsButton.setEnabled(flag)
 
176
 
 
177
 
 
178
    def projectionChanged(self):
 
179
        """Function is connected to signals from QgsMapRenderer.
 
180
        It updates coordinate transforms.
 
181
        """
 
182
 
 
183
        renderer = self.plugin.canvas.mapRenderer()
 
184
        if renderer.hasCrsTransformEnabled():
 
185
          self.coordXform = QgsCoordinateTransform(renderer.destinationSrs(), QgsCoordinateReferenceSystem(4326))
 
186
        else:
 
187
          self.coordXform = None
 
188
 
 
189
 
 
190
    def canvasToOsmCoords(self, point):
 
191
        """Performs conversion from canvas to map coordinates.
 
192
 
 
193
        @param point canvas coordinates to convert
 
194
        """
 
195
        point = self.plugin.canvas.getCoordinateTransform().toMapCoordinates( point )
 
196
 
 
197
        # optional conversion from map to layer coordinates
 
198
        if self.coordXform is not None:
 
199
            point = self.coordXform.transform( point )
 
200
 
 
201
        return point
 
202
 
 
203
 
 
204
    def __connectWidgetSignals(self):
 
205
        """Function connects all necessary signals to appropriate slots.
 
206
        """
 
207
 
 
208
        # signals emitted on clicking with tag and member tables
 
209
        QObject.connect(self.identifyButton,  SIGNAL("clicked()"), self.__startIdentifyingFeature)
 
210
        QObject.connect(self.moveButton,  SIGNAL("clicked()"), self.__startMovingFeature)
 
211
        QObject.connect(self.createPointButton,  SIGNAL("clicked()"), self.__startPointCreation)
 
212
        QObject.connect(self.createLineButton,  SIGNAL("clicked()"), self.__startLineCreation)
 
213
        QObject.connect(self.createPolygonButton,  SIGNAL("clicked()"), self.__startPolygonCreation)
 
214
        QObject.connect(self.createRelationButton,  SIGNAL("clicked()"), self.__createRelation)
 
215
        QObject.connect(self.removeButton, SIGNAL("clicked()"), self.removeFeature)
 
216
        QObject.connect(self.relListWidget, SIGNAL("currentRowChanged(int)"), self.loadRelationStuff)
 
217
        QObject.connect(self.relMembersList, SIGNAL("currentRowChanged(int)"), self.__showRelMemberOnMap)
 
218
        QObject.connect(self.addRelationButton,  SIGNAL("clicked()"), self.createRelationWithMember)
 
219
        QObject.connect(self.editRelationButton,  SIGNAL("clicked()"), self.editSelectedRelation)
 
220
        QObject.connect(self.removeRelationButton,  SIGNAL("clicked()"), self.removeSelectedRelation)
 
221
        QObject.connect(self.deleteTagsButton, SIGNAL("clicked()"), self.removeSelectedTags)
 
222
        QObject.connect(self.tagTable, SIGNAL("cellChanged(int,int)"), self.__onTagsCellChanged)
 
223
        QObject.connect(self.tagTable, SIGNAL("currentCellChanged(int,int,int,int)"), self.__onCurrentCellChanged)
 
224
        QObject.connect(self.tagTable, SIGNAL("itemDoubleClicked(QTableWidgetItem*)"), self.__onTagsItemDoubleClicked)
 
225
        QObject.connect(self.undoButton,  SIGNAL("clicked()"), self.__undo)
 
226
        QObject.connect(self.redoButton, SIGNAL("clicked()"), self.__redo)
 
227
        QObject.connect(self.urDetailsButton, SIGNAL("clicked()"), self.__urDetailsChecked)
 
228
 
 
229
 
 
230
    def databaseChanged(self,dbKey):
 
231
        """This function is called when current OSM database of plugin changes.
 
232
        The OsmFeatureDW performs necessary actions and tells current map tool about the change.
 
233
 
 
234
        @param dbKey key (name) of new current database
 
235
        """
 
236
 
 
237
        if self.__dlgAddRel and dbKey:
 
238
 
 
239
            self.__dlgAddRel.close()
 
240
            self.__dlgAddRel=None
 
241
            self.setContentEnabled(True)
 
242
            self.plugin.undoredo.setContentEnabled(True)
 
243
 
 
244
            QMessageBox.information(self, self.tr("OSM Plugin")
 
245
                ,self.tr("The 'Create OSM Relation' dialog was closed automatically because current OSM database was changed."))
 
246
 
 
247
        # clear the whole OSM Feature dockwidget as well as all related rubberbands and vertex markers
 
248
        self.clear()
 
249
 
 
250
        # if some mapTool is currently set tell it about database changing
 
251
        if self.mapTool:
 
252
            self.mapTool.databaseChanged(dbKey)
 
253
            self.activeEditButton=self.dummyButton
 
254
 
 
255
        # and if new database is None, disable the whole dockwidget
 
256
        if not dbKey:
 
257
            self.setContentEnabled(False)
 
258
            if self.mapTool:
 
259
                self.plugin.canvas.unsetMapTool(self.mapTool)
 
260
                self.mapTool=None
 
261
                self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
262
            return
 
263
 
 
264
        self.setContentEnabled(True)
 
265
 
 
266
 
 
267
    def clear(self):
 
268
        """Function clears all widget items.
 
269
        It resets rubberbands, vertexmarkers, re-initializes OsmFeatureDW inner structures.
 
270
        """
 
271
 
 
272
        # clear common feature infos
 
273
        self.typeIdLabel.setText("")
 
274
        self.userLabel.setText("")
 
275
        self.createdLabel.setText("")
 
276
 
 
277
        # clear table with information about feature's tags
 
278
        self.tagTable.clear()
 
279
        self.tagTable.setEnabled(False)
 
280
        self.tagTable.setRowCount(0)
 
281
        self.tagTable.setColumnCount(0)
 
282
 
 
283
        # clear table with info about feature's relations
 
284
        self.relListWidget.clear()
 
285
        self.relTagsTree.clear()
 
286
        self.relMembersList.clear()
 
287
        self.relTagsTree.setColumnCount(0)
 
288
 
 
289
        self.relListWidget.setEnabled(False)
 
290
        self.relTagsTree.setEnabled(False)
 
291
        self.relMembersList.setEnabled(False)
 
292
 
 
293
        # disable widget buttons
 
294
        self.deleteTagsButton.setEnabled(False)
 
295
        self.editRelationButton.setEnabled(False)
 
296
        self.removeRelationButton.setEnabled(False)
 
297
        self.addRelationButton.setEnabled(False)
 
298
        self.removeButton.setEnabled(False)
 
299
 
 
300
        # remove previous rubber bands
 
301
        self.rubBand.reset(False)
 
302
        self.rubBandPol.reset(True)
 
303
        self.verMarker.setCenter(QgsPoint(-1000,-1000))
 
304
        self.verMarker.hide()
 
305
        self.__removeMemberMarkers()
 
306
        self.verMarkers=[]
 
307
 
 
308
        self.relRubBand.reset(False)
 
309
        self.relRubBandPol.reset(True)
 
310
        self.relVerMarker.setCenter(QgsPoint(-1000,-1000))
 
311
        self.relVerMarker.hide()
 
312
 
 
313
        # clear member variables
 
314
        self.__tagsLoaded=False
 
315
        self.__relTagsLoaded=False
 
316
        self.feature=None
 
317
        self.featureType=None
 
318
        self.featRels=[]
 
319
        self.featRelTags=[]
 
320
        self.featRelMembers=[]
 
321
 
 
322
 
 
323
    def __createRelation(self):
 
324
        """Function calls relation creating process.
 
325
        Creation is started by displaying appropriate dialog.
 
326
        """
 
327
 
 
328
        # clear dockwidget
 
329
        self.clear()
 
330
        self.plugin.iface.mainWindow().statusBar().showMessage("")
 
331
 
 
332
        self.plugin.canvas.unsetMapTool(self.mapTool)
 
333
        del self.mapTool
 
334
        self.mapTool=None
 
335
 
 
336
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
337
        self.activeEditButton=self.dummyButton
 
338
 
 
339
        self.setContentEnabled(False)
 
340
        self.plugin.undoredo.setContentEnabled(False)
 
341
        self.plugin.toolBar.setEnabled(False)
 
342
 
 
343
        # OsmAddRelationDlg parameters: plugin, newRelationFirstMember, relationToEdit
 
344
        self.__dlgAddRel=OsmAddRelationDlg(self.plugin, None, None)
 
345
        self.__dlgAddRel.setWindowModality(Qt.WindowModal)
 
346
        self.__dlgAddRel.exec_()
 
347
        self.__dlgAddRel=None
 
348
 
 
349
        self.setContentEnabled(True)
 
350
        self.plugin.undoredo.setContentEnabled(True)
 
351
        self.plugin.toolBar.setEnabled(True)
 
352
 
 
353
 
 
354
    def createRelationWithMember(self):
 
355
        """Function calls relation creating process. Creation is started by displaying appropriate dialog.
 
356
        Function pre-fills dialog with information on currently loaded feature.
 
357
        """
 
358
 
 
359
        self.__dlgAddRel=OsmAddRelationDlg(self.plugin, QString(self.featureType+" %1").arg(self.feature.id()), None)
 
360
 
 
361
        # clear dockwidget
 
362
        self.clear()
 
363
        self.plugin.iface.mainWindow().statusBar().showMessage("")
 
364
 
 
365
        self.plugin.canvas.unsetMapTool(self.mapTool)
 
366
        del self.mapTool
 
367
        self.mapTool=None
 
368
 
 
369
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
370
        self.activeEditButton=self.dummyButton
 
371
 
 
372
        self.setContentEnabled(False)
 
373
        self.plugin.undoredo.setContentEnabled(False)
 
374
        self.plugin.toolBar.setEnabled(False)
 
375
 
 
376
        self.__dlgAddRel.setWindowModality(Qt.WindowModal)
 
377
        self.__dlgAddRel.exec_()
 
378
        self.__dlgAddRel=None
 
379
 
 
380
        self.setContentEnabled(True)
 
381
        self.plugin.undoredo.setContentEnabled(True)
 
382
        self.plugin.toolBar.setEnabled(True)
 
383
 
 
384
 
 
385
    def editSelectedRelation(self):
 
386
        """Function calls editing of a relation. Editing is started by displaying appropriate dialog.
 
387
        Relation identifier is not passed to this function. Function has to find it out from current row
 
388
        of appropriate list widget.
 
389
        """
 
390
 
 
391
        # show modal dialog "Edit relation"
 
392
        if not self.feature:
 
393
            QMessageBox.information(self, self.tr("OSM Feature Dock Widget"), self.tr("Choose OSM feature first."))
 
394
            return
 
395
 
 
396
        item=self.relListWidget.item(self.relListWidget.currentRow())
 
397
        if not item:
 
398
            QMessageBox.information(self, self.tr("OSM Feature Dock Widget"), self.tr("Choose relation for editing first."))
 
399
            return
 
400
 
 
401
        relId=self.featRels[self.relListWidget.currentRow()]
 
402
 
 
403
        self.setContentEnabled(False)
 
404
        self.plugin.undoredo.setContentEnabled(False)
 
405
        self.plugin.toolBar.setEnabled(False)
 
406
 
 
407
        self.__dlgAddRel=OsmAddRelationDlg(self.plugin, None, relId)
 
408
        self.__dlgAddRel.setWindowModality(Qt.WindowModal)
 
409
        self.__dlgAddRel.exec_()
 
410
        self.__dlgAddRel=None
 
411
 
 
412
        self.setContentEnabled(True)
 
413
        self.plugin.undoredo.setContentEnabled(True)
 
414
        self.plugin.toolBar.setEnabled(True)
 
415
 
 
416
 
 
417
    def __onTagsCellChanged(self,row,column):
 
418
        """Function is called after cellChanged(int,int) signal is emitted on table of all features' relations.
 
419
        It means that user is changed key or value of some existing tag.
 
420
 
 
421
        @param row index of row in table of tags
 
422
        @param column index of column in table of tags
 
423
        """
 
424
 
 
425
        if not self.__tagsLoaded:
 
426
            # this signal was emitted during table initialization,
 
427
            # but we are interested in user actions only
 
428
            return
 
429
 
 
430
        if row<self.tagTable.rowCount()-1:
 
431
 
 
432
            # changing value of tag that already exists
 
433
            key=self.tagTable.item(row,0).text()
 
434
            value=self.tagTable.item(row,1).text()
 
435
 
 
436
            # store tag's change into database
 
437
            self.plugin.undoredo.startAction("Change tag value.")
 
438
            self.plugin.dbm.changeTagValue(self.feature.id(),self.featureType,key.toAscii().data(),value.toUtf8())
 
439
        else:
 
440
            key = self.tagTable.item(row,0).text()
 
441
            if key=="" or key==self.newTagLabel:
 
442
                return
 
443
 
 
444
            # adding new tag and setting its key
 
445
            if column==0:
 
446
 
 
447
                # only ascii keys are allowed
 
448
                nkfd_form=unicodedata.normalize('NFKD', unicode(key.toUtf8(),'utf-8'))
 
449
                key_only_ascii=nkfd_form.encode('ASCII', 'ignore')
 
450
 
 
451
                # store it into database
 
452
                isAlreadyDef=self.plugin.dbm.isTagDefined(self.feature.id(),self.featureType,key_only_ascii)
 
453
                if isAlreadyDef:
 
454
                    # such a key already exists for this relation
 
455
                    self.tagTable.setItem(row,0,QTableWidgetItem(self.newTagLabel))
 
456
                    QMessageBox.information(self, self.tr("OSM Feature Dock Widget")
 
457
                        ,self.tr(QString("Property '").append(key_only_ascii).append("' cannot be added twice.")))
 
458
                    return
 
459
 
 
460
                # well, insert new tag into database
 
461
                self.plugin.undoredo.startAction("Insert new tag.")
 
462
                self.plugin.dbm.insertTag(self.feature.id(),self.featureType,key_only_ascii,None)
 
463
 
 
464
                self.__tagsLoaded=False
 
465
 
 
466
                self.tagTable.item(row,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
 
467
                self.tagTable.item(row,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)
 
468
                self.tagTable.item(row,0).setText(QString(key_only_ascii))
 
469
 
 
470
                newLastRow = row+1
 
471
                self.tagTable.setRowCount(row+2)
 
472
                self.tagTable.setItem(newLastRow,0,QTableWidgetItem(self.newTagLabel))
 
473
                self.tagTable.setItem(newLastRow,1,QTableWidgetItem(""))
 
474
                self.tagTable.item(newLastRow,0).setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
 
475
                self.tagTable.item(newLastRow,1).setFlags(Qt.ItemIsEnabled)
 
476
 
 
477
                self.__tagsLoaded=True
 
478
 
 
479
        # updating feature status (from Normal to Updated)
 
480
        if self.featureType=='Point':
 
481
            self.plugin.dbm.changePointStatus(self.feature.id(),'N','U')
 
482
 
 
483
        elif self.featureType=='Line':
 
484
            self.plugin.dbm.changeLineStatus(self.feature.id(),'N','U')
 
485
 
 
486
        elif self.featureType=='Polygon':
 
487
            self.plugin.dbm.changePolygonStatus(self.feature.id(),'N','U')
 
488
 
 
489
        elif self.featureType=='Relation':
 
490
            self.plugin.dbm.changeRelationStatus(self.feature.id(),'N','U')
 
491
 
 
492
        self.plugin.dbm.commit()
 
493
 
 
494
        affected=[(self.feature.id(),self.featureType)]
 
495
        self.plugin.undoredo.stopAction(affected)
 
496
        self.plugin.dbm.recacheAffectedNow(affected)
 
497
 
 
498
        # refresh map canvas so that changes take effect
 
499
        if column==1:
 
500
            self.plugin.canvas.refresh()
 
501
 
 
502
 
 
503
    def __onTagsItemDoubleClicked(self,item):
 
504
        """Function is called after itemDoubleClicked(...) signal is emitted on table of feature tags.
 
505
 
 
506
        It shows combobox with possible values for given item of table.
 
507
 
 
508
        @param item item of table of feature tags
 
509
        """
 
510
 
 
511
        if item.column()==0:
 
512
 
 
513
            if item.row()<self.tagTable.rowCount()-1:
 
514
                return
 
515
 
 
516
            tagValues=OsmTags.suitableTagKeys(self.featureType)
 
517
            tagValues.sort()
 
518
            if len(tagValues)>0:
 
519
                valCombo=QComboBox()
 
520
                valCombo.setEditable(True)
 
521
                valCombo.addItems(tagValues)
 
522
                currentComboText=self.tagTable.item(item.row(),0).text()
 
523
                ix=valCombo.findText(currentComboText)
 
524
                valCombo.setCurrentIndex(ix)
 
525
                if ix==-1:
 
526
                    valCombo.setEditText(currentComboText)
 
527
 
 
528
                self.tagTable.setCellWidget(item.row(),0,valCombo)
 
529
                QObject.connect(valCombo, SIGNAL("currentIndexChanged(const QString &)"), self.__onTagKeySelectionChanged)
 
530
            return
 
531
 
 
532
        key=str(self.tagTable.item(item.row(),0).text())
 
533
        tagValues=OsmTags.suitableTagValues(self.featureType,key)
 
534
        tagValues.sort()
 
535
 
 
536
        if len(tagValues)>0:
 
537
            valCombo=QComboBox()
 
538
            valCombo.setEditable(True)
 
539
            valCombo.addItems(tagValues)
 
540
            currentComboText=self.tagTable.item(item.row(),1).text()
 
541
            ix=valCombo.findText(currentComboText)
 
542
            valCombo.setCurrentIndex(ix)
 
543
            if ix==-1:
 
544
                valCombo.setEditText(currentComboText)
 
545
 
 
546
            self.tagTable.setCellWidget(item.row(),1,valCombo)
 
547
            QObject.connect(valCombo, SIGNAL("currentIndexChanged(const QString &)"), self.__onTagValueSelectionChanged)
 
548
 
 
549
 
 
550
    def __onCurrentCellChanged(self,curRow,curCol,prevRow,prevCol):
 
551
        """Function is called after currentCellChanged(...) signal is emitted on table of feature tags.
 
552
 
 
553
        @param curRow current row index to tags table
 
554
        @param curCol current column index to tags table
 
555
        @param prevRow previous row index to tags table
 
556
        @param prevCol previous column index to tags table
 
557
        """
 
558
 
 
559
        cellWidget=self.tagTable.cellWidget(prevRow,prevCol)
 
560
        if cellWidget==None:
 
561
            return
 
562
 
 
563
        self.tagTable.removeCellWidget(prevRow,prevCol)
 
564
 
 
565
 
 
566
    def __onTagKeySelectionChanged(self,key):
 
567
        """Function is called after currentIndexChanged(...) signal is emitted on combobox in 1st column of tags table.
 
568
 
 
569
        @param key key selected in combobox
 
570
        """
 
571
 
 
572
        row=self.tagTable.currentRow()
 
573
        col=self.tagTable.currentColumn()
 
574
 
 
575
        self.tagTable.item(row,col).setText(key)
 
576
        self.tagTable.removeCellWidget(row,col)
 
577
 
 
578
 
 
579
    def __onTagValueSelectionChanged(self,value):
 
580
        """Function is called after currentIndexChanged(...) signal is emitted on combobox in 2nd column of tags table.
 
581
 
 
582
        @param value value selected in combobox
 
583
        """
 
584
 
 
585
        row=self.tagTable.currentRow()
 
586
        col=self.tagTable.currentColumn()
 
587
 
 
588
        self.tagTable.item(row,col).setText(value)
 
589
        self.tagTable.removeCellWidget(row,col)
 
590
 
 
591
 
 
592
 
 
593
    def __startIdentifyingFeature(self):
 
594
        """Function prepares feature identification.
 
595
        The appropriate map tool (OsmIdentifyMT) is set to map canvas.
 
596
        """
 
597
 
 
598
        if self.activeEditButton==self.identifyButton:
 
599
            return
 
600
 
 
601
        self.plugin.canvas.unsetMapTool(self.plugin.canvas.mapTool())
 
602
 
 
603
        # clear dockwidget
 
604
        self.clear()
 
605
 
 
606
        self.plugin.iface.mainWindow().statusBar().showMessage("")
 
607
 
 
608
        self.mapTool=OsmIdentifyMT(self.plugin.canvas, self, self.plugin.dbm)
 
609
        self.plugin.canvas.setMapTool(self.mapTool)
 
610
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
611
        self.activeEditButton=self.identifyButton
 
612
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
613
 
 
614
 
 
615
    def __startMovingFeature(self):
 
616
        """Function prepares feature moving.
 
617
        The appropriate map tool (OsmMoveMT) is set to map canvas.
 
618
        """
 
619
 
 
620
        if self.activeEditButton==self.moveButton:
 
621
            return
 
622
 
 
623
        # clear dockwidget
 
624
        self.clear()
 
625
        self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
 
626
 
 
627
        self.mapTool=OsmMoveMT(self.plugin)
 
628
        self.plugin.canvas.setMapTool(self.mapTool)
 
629
        self.plugin.canvas.setCursor(QCursor(Qt.CrossCursor))
 
630
        self.activeEditButton=self.moveButton
 
631
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
632
 
 
633
 
 
634
    def __startPointCreation(self):
 
635
        """Function prepares point creating operation.
 
636
        The appropriate map tool (OsmCreatePointMT) is set to map canvas.
 
637
        """
 
638
 
 
639
        if self.activeEditButton==self.createPointButton:
 
640
            return
 
641
 
 
642
        self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
 
643
 
 
644
        self.mapTool=OsmCreatePointMT(self.plugin)
 
645
        self.plugin.canvas.setMapTool(self.mapTool)
 
646
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
647
        self.activeEditButton=self.createPointButton
 
648
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
649
 
 
650
 
 
651
    def __startLineCreation(self):
 
652
        """Function prepares line creating operation.
 
653
        The appropriate map tool (OsmCreateLineMT) is set to map canvas.
 
654
        """
 
655
 
 
656
        if self.activeEditButton==self.createLineButton:
 
657
            return
 
658
 
 
659
        self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
 
660
 
 
661
        self.mapTool=OsmCreateLineMT(self.plugin)
 
662
        self.plugin.canvas.setMapTool(self.mapTool)
 
663
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
664
        self.activeEditButton=self.createLineButton
 
665
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
666
 
 
667
 
 
668
    def __startPolygonCreation(self):
 
669
        """Function prepares polygon creating operation.
 
670
        The appropriate map tool (OsmCreatePolygonMT) is set to map canvas.
 
671
        """
 
672
 
 
673
        if self.activeEditButton==self.createPolygonButton:
 
674
            return
 
675
 
 
676
        self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
 
677
 
 
678
        self.mapTool=OsmCreatePolygonMT(self.plugin)
 
679
        self.plugin.canvas.setMapTool(self.mapTool)
 
680
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
681
        self.activeEditButton=self.createPolygonButton
 
682
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
683
 
 
684
 
 
685
    def removeFeature(self):
 
686
        """Function completely removes feature that is currently loaded on "OSM Feature" widget.
 
687
        """
 
688
 
 
689
        self.removeButton.setDown(False)
 
690
        self.removeButton.setChecked(False)
 
691
        self.plugin.iface.mainWindow().statusBar().showMessage("")
 
692
 
 
693
        # remove object that was identified by "identify tool"
 
694
        featId=self.feature.id()
 
695
        featType=self.featureType
 
696
        affected=[]
 
697
 
 
698
        if featType=='Point':
 
699
            self.plugin.undoredo.startAction("Remove point.")
 
700
            affected=self.plugin.dbm.removePoint(featId)
 
701
 
 
702
        elif featType=='Line':
 
703
            self.plugin.undoredo.startAction("Remove line.")
 
704
            affected=self.plugin.dbm.removeLine(featId,True)    # todo: False when Ctrl pressed
 
705
 
 
706
        elif featType=='Polygon':
 
707
            self.plugin.undoredo.startAction("Remove polygon.")
 
708
            affected=self.plugin.dbm.removePolygon(featId,True)    # todo: False when Ctrl pressed
 
709
 
 
710
        elif featType=='Relation':
 
711
            self.plugin.undoredo.startAction("Remove relation.")
 
712
            self.plugin.dbm.removeRelation(featId)
 
713
        else:
 
714
            return    # strange situation
 
715
 
 
716
        self.plugin.undoredo.stopAction(affected)
 
717
        self.plugin.dbm.recacheAffectedNow(affected)
 
718
 
 
719
        # refresh map canvas so that changes take effect
 
720
        self.plugin.canvas.refresh()
 
721
 
 
722
        # clear dockwidget
 
723
        self.clear()
 
724
 
 
725
        self.plugin.iface.mainWindow().statusBar().showMessage("")
 
726
 
 
727
        self.plugin.canvas.unsetMapTool(self.mapTool)
 
728
        del self.mapTool
 
729
        self.mapTool=OsmIdentifyMT(self.plugin.canvas, self, self.plugin.dbm)
 
730
        self.plugin.canvas.setMapTool(self.mapTool)
 
731
        self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
 
732
        self.plugin.canvas.setFocus(Qt.OtherFocusReason)
 
733
        self.activeEditButton=self.identifyButton
 
734
        self.activeEditButton.setChecked(True)
 
735
 
 
736
 
 
737
    def removeSelectedTags(self):
 
738
        """Function completely removes all tags that are currently selected in the appropriate
 
739
        list of the "OSM Feature" widget. More than one tag can be selected using Ctrl and clicking.
 
740
        """
 
741
 
 
742
        # remove selected tags (rows)
 
743
        selectedItems=self.tagTable.selectedItems()
 
744
        selectedRowsIndexes=[]
 
745
        lastRowIndex=self.tagTable.rowCount()-1
 
746
        self.tagTable.setCurrentCell(lastRowIndex,0)
 
747
 
 
748
        for i in selectedItems:
 
749
            if i.column()==0 and not i.row()==lastRowIndex:
 
750
                selectedRowsIndexes.append(i.row())
 
751
 
 
752
        self.plugin.undoredo.startAction("Remove tags.")
 
753
 
 
754
        selectedRowsIndexes.sort()
 
755
        selectedRowsIndexes.reverse()
 
756
 
 
757
        for ix in selectedRowsIndexes:
 
758
 
 
759
            key=self.tagTable.item(ix,0).text()
 
760
            # updating feature status (from Normal to Updated)
 
761
            if self.featureType=='Point':
 
762
                self.plugin.dbm.changePointStatus(self.feature.id(),'N','U')
 
763
 
 
764
            elif self.featureType=='Line':
 
765
                self.plugin.dbm.changeLineStatus(self.feature.id(),'N','U')
 
766
 
 
767
            elif self.featureType=='Polygon':
 
768
                self.plugin.dbm.changePolygonStatus(self.feature.id(),'N','U')
 
769
 
 
770
            elif self.featureType=='Relation':
 
771
                self.plugin.dbm.changeRelationStatus(self.feature.id(),'N','U')
 
772
 
 
773
            # perform tag removing 
 
774
            self.plugin.dbm.removeTag(self.feature.id(),self.featureType,key.toAscii().data())
 
775
 
 
776
            self.tagTable.removeRow(ix)
 
777
 
 
778
        # make this action permanent
 
779
        self.plugin.dbm.commit()
 
780
        affected=[(self.feature.id(),self.featureType)]
 
781
        self.plugin.undoredo.stopAction(affected)
 
782
        self.plugin.dbm.recacheAffectedNow(affected)
 
783
 
 
784
        # refresh map canvas so that changes take effect
 
785
        self.plugin.canvas.refresh()
 
786
 
 
787
 
 
788
    def removeSelectedRelation(self):
 
789
        """Function completely removes all relations that are selected in the appropriate list of the "OSM Feature" widget.
 
790
        More than one relation can be selected using Ctrl and clicking.
 
791
        """
 
792
 
 
793
        # find out id of relation which is selected
 
794
        item=self.relListWidget.item(self.relListWidget.currentRow())
 
795
        if not item or not self.plugin.dbm.currentKey:
 
796
            return
 
797
 
 
798
        relId=self.featRels[self.relListWidget.currentRow()]
 
799
 
 
800
        self.plugin.undoredo.startAction("Remove relation.")
 
801
        self.plugin.dbm.removeRelation(relId)
 
802
        self.plugin.dbm.commit()
 
803
        self.plugin.undoredo.stopAction()
 
804
 
 
805
        # reload list of feature's relations
 
806
        self.__loadFeatureRelations(self.feature.id(),self.featureType)
 
807
 
 
808
 
 
809
    def __showRelMemberOnMap(self,ixRow):
 
810
        """Functions marks relation member on the map.
 
811
 
 
812
        Marking is realized with simple rubberBand (in case of line/polygon) or vertexMarker (for point).
 
813
        Relation member is given by its index to the list of all members of currently loaded relation.
 
814
 
 
815
        @param ixRow index to the list of all relations on mentioned widget
 
816
        """
 
817
 
 
818
        # move rubberband to selected feature
 
819
        self.relRubBand.reset(False)
 
820
        self.relRubBandPol.reset(True)
 
821
        self.relVerMarker.hide()
 
822
 
 
823
        if ixRow==-1 or not self.plugin.dbm.currentKey:
 
824
            return
 
825
        mem=self.featRelMembers[ixRow]
 
826
        self.showFeatureOnMap(mem[0],mem[1])    # id & type
 
827
 
 
828
 
 
829
    def loadRelationStuff(self,ixRow):
 
830
        """Functions loads information on specified relation into "OSM Feature" widget.
 
831
 
 
832
        Relation is given by its index to the list of all relations on mentioned widget.
 
833
        Relation "stuff" means its basic info, tags and members.
 
834
 
 
835
        @param ixRow index to the list of all relations on mentioned widget
 
836
        """
 
837
 
 
838
        if ixRow==-1 or not self.plugin.dbm.currentKey:
 
839
            return
 
840
        relId=self.featRels[ixRow]
 
841
 
 
842
        # show all tags connected to selected relation
 
843
        self.__loadRelationTags(relId)
 
844
 
 
845
        # show all relation members on osm dock widget
 
846
        self.__loadRelationMembers(relId)
 
847
 
 
848
        # enable list of members and buttons for relation removing and editing
 
849
        self.relMembersList.setEnabled(True)
 
850
        self.editRelationButton.setEnabled(True)
 
851
        self.removeRelationButton.setEnabled(True)
 
852
 
 
853
 
 
854
    def __loadRelationMembers(self,relId):
 
855
        """Functions loads the list of members of specified relation.
 
856
        Relation is given by its identifier.
 
857
 
 
858
        Loading is realized into the appropriate QListWidget of "OSM Feature" dockable widget.
 
859
        Resulting list contains info on member's identifier, type and its role in specified relation.
 
860
 
 
861
        @param relId identifier of relation
 
862
        """
 
863
 
 
864
        self.relMembersList.clear()
 
865
        # ask database manager for all relation members
 
866
        self.featRelMembers=self.plugin.dbm.getRelationMembers(relId)
 
867
 
 
868
        # printing members
 
869
        for i in range(0,len(self.featRelMembers)):
 
870
            memId=self.featRelMembers[i][0]
 
871
            memType=self.featRelMembers[i][1]
 
872
            memRole=self.featRelMembers[i][2]
 
873
 
 
874
            listRow=QString("(%1) - %2").arg(memId).arg(memType)
 
875
            if memRole and memRole<>"":
 
876
                listRow=listRow.append(QString(", role:%3").arg(memRole))
 
877
 
 
878
            self.relMembersList.addItem(listRow)
 
879
 
 
880
 
 
881
    def __loadRelationTags(self,relId):
 
882
        """Functions loads the list of tags of specified relation.
 
883
        Relation is given by its identifier.
 
884
 
 
885
        Loading is realized into the appropriate QTreeWidget of "OSM Feature" dockable widget.
 
886
        Resulting tags table has two columns "Key","Value" for easy representing of tag pairs.
 
887
 
 
888
        @param relId identifier of relation
 
889
        """
 
890
 
 
891
        self.relTagsTree.clear()
 
892
        self.__relTagsLoaded=False
 
893
        # ask database manager for all relation tags
 
894
        self.featRelTags=self.plugin.dbm.getFeatureTags(relId,"Relation")
 
895
 
 
896
        self.relTagsTree.setColumnCount(2)
 
897
        self.relTagsTree.setHeaderLabels(["Key","Value"])
 
898
 
 
899
        for i in range(0,len(self.featRelTags)):
 
900
            self.relTagsTree.addTopLevelItem(QTreeWidgetItem([self.featRelTags[i][0],self.featRelTags[i][1]]))
 
901
 
 
902
        self.__relTagsLoaded=True
 
903
 
 
904
 
 
905
    def __loadFeatureInformation(self,featId,featType):
 
906
        """Functions shows up the basic information on feature.
 
907
        Feature is given by its identifier and its type.
 
908
 
 
909
        Info is loaded to appropriate place of the "OSM Feature" widget.
 
910
        Basic info consists (mainly) of feature's identifier, type, owner and timestamp.
 
911
 
 
912
        @param featId identifier of feature to load
 
913
        @param featType type of feature to load - one of 'Point','Line','Polygon'
 
914
        """
 
915
 
 
916
        # asking OsmDatabaseManager for missing information
 
917
        featUser=self.plugin.dbm.getFeatureOwner(featId,featType)
 
918
        featCreated=self.plugin.dbm.getFeatureCreated(featId,featType)    # returned as string
 
919
            # timestamp example: "2008-02-18T15:34:14Z"
 
920
 
 
921
        self.typeIdLabel.setText("")
 
922
        self.userLabel.setText("")
 
923
        self.createdLabel.setText("")
 
924
 
 
925
        # put non-tags feature information on dock widget
 
926
        if featType:
 
927
            self.typeIdLabel.setText(QString("%1  %2").arg(featType).arg(str(featId)))
 
928
        if featUser:
 
929
            self.userLabel.setText(QString("%1").arg(featUser))
 
930
        if featCreated:
 
931
            # format timestamp
 
932
            DT_format_osm=Qt.ISODate
 
933
                # "yyyy-MM-ddThh:mm:ssZ"
 
934
            DT_format_plugin="yy/MM/dd - h:mm"
 
935
            self.createdLabel.setText(QDateTime.fromString(featCreated,DT_format_osm).toString(DT_format_plugin))
 
936
 
 
937
 
 
938
    def __loadFeatureTags(self,featId,featType):
 
939
        """Functions loads the list of tags of specified feature.
 
940
        Feature is given by its identifier and its type.
 
941
 
 
942
        Loading is realized into the appropriate QTableWidget of "OSM Feature" dockable widget.
 
943
        Resulting tags table has two columns "Key","Value" for easy representing of tag pairs.
 
944
 
 
945
        @param featId identifier of feature to load
 
946
        @param featType type of feature to load - one of 'Point','Line','Polygon'
 
947
        """
 
948
 
 
949
        # clear table with information about feature's tags
 
950
        self.tagTable.clear()
 
951
 
 
952
        # fill tableWidget with tags of selected feature
 
953
        tableData=self.plugin.dbm.getFeatureTags(featId,featType)
 
954
        rowCount=len(tableData)
 
955
        self.__tagsLoaded=False
 
956
 
 
957
        self.tagTable.setRowCount(rowCount+1)
 
958
        self.tagTable.setColumnCount(2)
 
959
        self.tagTable.setHorizontalHeaderItem(0,QTableWidgetItem("Key"))
 
960
        self.tagTable.setHorizontalHeaderItem(1,QTableWidgetItem("Value"))
 
961
 
 
962
        for i in range(0,rowCount):
 
963
            self.tagTable.setItem(i,0,QTableWidgetItem(QString.fromUtf8(tableData[i][0])))
 
964
            self.tagTable.setItem(i,1,QTableWidgetItem(QString.fromUtf8(tableData[i][1])))
 
965
            self.tagTable.item(i,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
 
966
            self.tagTable.item(i,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)
 
967
 
 
968
        self.tagTable.setItem(rowCount,0,QTableWidgetItem(self.newTagLabel))
 
969
        self.tagTable.setItem(rowCount,1,QTableWidgetItem(""))
 
970
        self.tagTable.item(rowCount,0).setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
 
971
        self.tagTable.item(rowCount,1).setFlags(Qt.ItemIsEnabled)
 
972
        self.__tagsLoaded=True
 
973
 
 
974
        # enable tags table for editing
 
975
        self.tagTable.setEnabled(True)
 
976
        self.deleteTagsButton.setEnabled(True)
 
977
 
 
978
 
 
979
    def __loadFeatureRelations(self,featId,featType):
 
980
        """Functions loads the list of relations of specified feature.
 
981
        Feature is given by its identifier and its type.
 
982
 
 
983
        Loading is realized into the appropriate QListWidget of "OSM Feature" dockable widget.
 
984
        If no relation exists for specified feature, listWidget is filled with the only row with text: "<no relation>".
 
985
 
 
986
        @param featId identifier of feature to load
 
987
        @param featType type of feature to load - one of 'Point','Line','Polygon'
 
988
        """
 
989
 
 
990
        self.relTagsTree.setColumnCount(0)
 
991
        self.relMembersList.setEnabled(False)
 
992
 
 
993
        # disable widget buttons
 
994
        self.editRelationButton.setEnabled(False)
 
995
        self.removeRelationButton.setEnabled(False)
 
996
 
 
997
        # clear all tables connected to relations
 
998
        self.relListWidget.clear()
 
999
        self.relTagsTree.clear()
 
1000
        self.relMembersList.clear()
 
1001
 
 
1002
        # load relations for selected feature
 
1003
        self.featRels=self.plugin.dbm.getFeatureRelations(featId,featType)
 
1004
 
 
1005
        for i in range(0,len(self.featRels)):
 
1006
            self.relListWidget.addItem(self.__getRelationInfo(self.featRels[i]))
 
1007
 
 
1008
        if len(self.featRels)==0:
 
1009
            self.relListWidget.addItem("<no relation>")
 
1010
            self.addRelationButton.setEnabled(True)
 
1011
            return
 
1012
 
 
1013
        # enable relation tables and button for relation addition
 
1014
        self.addRelationButton.setEnabled(True)
 
1015
        self.relListWidget.setEnabled(True)
 
1016
        self.relTagsTree.setEnabled(True)
 
1017
 
 
1018
 
 
1019
    def reloadFeatureRelations(self):
 
1020
        """Functions reloads the list of relations for currently loaded feature.
 
1021
 
 
1022
        Loading is realized into the appropriate QListWidget of "OSM Feature" dockable widget.
 
1023
        If no relation exists for specified feature, listWidget is filled with the only row with text: "<no relation>".
 
1024
        """
 
1025
 
 
1026
        self.relTagsTree.setColumnCount(0)
 
1027
        self.relMembersList.setEnabled(False)
 
1028
 
 
1029
        # disable widget buttons
 
1030
        self.editRelationButton.setEnabled(False)
 
1031
        self.removeRelationButton.setEnabled(False)
 
1032
 
 
1033
        # clear all tables connected to relations
 
1034
        self.relListWidget.clear()
 
1035
        self.relTagsTree.clear()
 
1036
        self.relMembersList.clear()
 
1037
 
 
1038
        # load relations for selected feature
 
1039
        self.featRels=self.plugin.dbm.getFeatureRelations(self.feature.id(),self.featureType)
 
1040
 
 
1041
        for i in range(0,len(self.featRels)):
 
1042
            self.relListWidget.addItem(self.__getRelationInfo(self.featRels[i]))
 
1043
 
 
1044
        if len(self.featRels)==0:
 
1045
            self.relListWidget.addItem("<no relation>")
 
1046
            self.addRelationButton.setEnabled(True)
 
1047
            return
 
1048
 
 
1049
        # enable relation tables and button for relation addition
 
1050
        self.addRelationButton.setEnabled(True)
 
1051
        self.relListWidget.setEnabled(True)
 
1052
        self.relTagsTree.setEnabled(True)
 
1053
 
 
1054
 
 
1055
    def putMarkersOnMembers(self,feat,featType):
 
1056
        """Function adds additional vertexMarkers to the map.
 
1057
 
 
1058
        Additional vertexMarker are used to provide better marking of line/polygon.
 
1059
        In that case line/polygon geometry is marked with rubberband first, and second one vertexMarker is put
 
1060
        on each its vertex. Such extended marking of line/polygon is used (for example) when calling loadFeature() method with
 
1061
        "markingMode" parameter set to 2.
 
1062
 
 
1063
        @param featId identifier of feature to load
 
1064
        @param featType type of feature to load - one of 'Point','Line','Polygon'
 
1065
        """
 
1066
 
 
1067
        if featType=='Point':
 
1068
            return
 
1069
 
 
1070
        pline=None
 
1071
        if featType=='Line':
 
1072
            pline=feat.geometry().asPolyline()
 
1073
 
 
1074
        elif featType=='Polygon':
 
1075
            pline=feat.geometry().asPolygon()[0]
 
1076
 
 
1077
        # get qgis settings of line width and color for rubberband
 
1078
        settings=QSettings()
 
1079
        qgsLineWidth=2 # use fixed width
 
1080
        qgsLineRed=settings.value("/qgis/digitizing/line_color_red",QVariant(255)).toInt()
 
1081
        qgsLineGreen=settings.value("/qgis/digitizing/line_color_green",QVariant(0)).toInt()
 
1082
        qgsLineBlue=settings.value("/qgis/digitizing/line_color_blue",QVariant(0)).toInt()
 
1083
 
 
1084
        for i in range(0,len(pline)):
 
1085
            verMarker=QgsVertexMarker(self.plugin.canvas)
 
1086
            verMarker.setIconType(3)
 
1087
            verMarker.setIconSize(6)
 
1088
            verMarker.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
 
1089
            verMarker.setPenWidth(qgsLineWidth)
 
1090
            verMarker.setCenter(pline[i])
 
1091
            verMarker.show()
 
1092
            self.verMarkers.append(verMarker)
 
1093
 
 
1094
 
 
1095
    def __removeMemberMarkers(self):
 
1096
        """Function removes additional vertexMarkers from the map.
 
1097
 
 
1098
        Additional vertexMarker are used to provide better marking of line/polygon.
 
1099
        In that case line/polygon geometry is marked with rubberband first, and second one vertexMarker is put
 
1100
        on each its vertex. Such extended marking of line/polygon is used (for example) when calling loadFeature() method with
 
1101
        "markingMode" parameter set to 2.
 
1102
        """
 
1103
 
 
1104
        for verMarker in self.verMarkers:
 
1105
            self.plugin.canvas.scene().removeItem(verMarker)
 
1106
            del verMarker
 
1107
        self.verMarkers=[]
 
1108
 
 
1109
 
 
1110
    def showFeatureOnMap(self,featId,featType):
 
1111
        """Function just shows up specified feature on the map canvas.
 
1112
 
 
1113
        Showing feature up is realized by putting rubberBand (vertexMarker) on its whole geometry.
 
1114
        This rubberBand (vM) is the same as when using loadFeature() method with "markingMode" parameter set to 1.
 
1115
 
 
1116
        Feature is given by its identifier and its type. Feature data are not loaded into "OSM Feature" widget.
 
1117
        Rubberbands (vertexMarkers) of other features are not removed from the map canvas before this action.
 
1118
 
 
1119
        @param featId identifier of feature to load
 
1120
        @param featType type of feature to load - one of 'Point','Line','Polygon'
 
1121
        """
 
1122
 
 
1123
        # we have to know feature's geometry to be able to display it on map
 
1124
        featGeom=self.plugin.dbm.getFeatureGeometry(featId,featType)
 
1125
        if not featGeom:
 
1126
           return    # nothing to show :-/
 
1127
 
 
1128
        if featType=='Polygon':
 
1129
            self.relRubBandPol.setToGeometry(featGeom,self.plugin.canvas.currentLayer())
 
1130
        elif featType=='Point':
 
1131
            self.relVerMarker.setCenter(featGeom.asPoint())
 
1132
            self.relVerMarker.show()
 
1133
        elif featType=='Line':
 
1134
            self.relRubBand.setToGeometry(featGeom,self.plugin.canvas.currentLayer())
 
1135
 
 
1136
 
 
1137
    def loadFeature(self,feat,featType,markingMode=1):
 
1138
        """Function loads information on specified feature into "OSM Feature" widget elements.
 
1139
        It shows up features identifier, type, osm user, timestamp, all its tags and related OSM relations.
 
1140
 
 
1141
        According to the value of parameter "markingMode" is marks feature on the map canvas. If "markingMode" equals to zero
 
1142
        feature is not marked on the map. If it equals to 1, simple rubberband (or vertexMarker) is put on the feature (this is
 
1143
        default behavior). If "markingMode" equals to 2, extended rubberband is shown on the map, especially for non-point features.
 
1144
 
 
1145
        @param feat QgsFeature object of feature to load
 
1146
        @param featType type of feature to load - one of 'Point','Line','Polygon'
 
1147
        @param markingMode not compulsory; defines quality of feature's rubberband on map
 
1148
        """
 
1149
 
 
1150
        if not feat or not featType:
 
1151
            self.clear()
 
1152
            return
 
1153
 
 
1154
        # remember which feature is loaded
 
1155
        self.featureType=featType
 
1156
        self.feature=feat
 
1157
 
 
1158
        # move rubberband to selected feature
 
1159
        self.rubBandPol.reset(True)
 
1160
        self.rubBand.reset(False)
 
1161
        self.verMarker.hide()
 
1162
        self.__removeMemberMarkers()
 
1163
 
 
1164
        if markingMode>0:
 
1165
            if self.featureType=='Polygon':
 
1166
                self.rubBandPol.setToGeometry(feat.geometry(),self.plugin.canvas.currentLayer())
 
1167
                if markingMode>1:
 
1168
                    self.putMarkersOnMembers(feat,featType)
 
1169
            elif self.featureType=='Point':
 
1170
                self.verMarker.setCenter(feat.geometry().asPoint())
 
1171
                self.verMarker.show()
 
1172
            elif self.featureType=='Line':
 
1173
                self.rubBand.setToGeometry(feat.geometry(),self.plugin.canvas.currentLayer())
 
1174
                if markingMode>1:
 
1175
                    self.putMarkersOnMembers(feat,featType)
 
1176
 
 
1177
        # show common feature information (id,feature originator,created,feature type,...)
 
1178
        self.__loadFeatureInformation(feat.id(),featType)
 
1179
 
 
1180
        # show all tags connected to feature onto osm widget dialog
 
1181
        self.__loadFeatureTags(feat.id(),featType)
 
1182
 
 
1183
        # show all relations connected to feature onto osm widget dialog
 
1184
        self.__loadFeatureRelations(feat.id(),featType)
 
1185
 
 
1186
        # feature has been loaded; enable "remove feature" button
 
1187
        self.removeButton.setEnabled(True)
 
1188
 
 
1189
 
 
1190
    def __getRelationInfo(self,relId):
 
1191
        """Function returns brief info on specified relation. Information consists of relation identifier,
 
1192
        its type and other important relation properties and it is returned simply in QString() object.
 
1193
 
 
1194
        Information are good to be shown in some list (of relations).
 
1195
 
 
1196
        @param relId identifier of relation
 
1197
        @return brief info on relation concatenated in string
 
1198
        """
 
1199
 
 
1200
        relInfo=relType=relRoute=relBoundary=relRef=relRestr=""
 
1201
        tags=self.plugin.dbm.getFeatureTags(relId,"Relation")
 
1202
 
 
1203
        for i in range(0,len(tags)):
 
1204
            key=tags[i][0]
 
1205
            value=tags[i][1]
 
1206
 
 
1207
            if key=="type":
 
1208
                relType=value        # type: route / boundary, ...
 
1209
            elif key=="route":
 
1210
                relRoute=value       # route: road / bicycle / foot / hiking / bus / railway / tram, ...
 
1211
            elif key=="boundary":
 
1212
                relBoundary=value    # boundary: administrative, ...
 
1213
            elif key=="ref":
 
1214
                relRef=value
 
1215
            elif key=="restriction":
 
1216
                relRestr=value
 
1217
 
 
1218
        if relType=="route":
 
1219
            relInfo = QString("(%1) "+relRoute+" "+relType+" "+relRef).arg(relId)
 
1220
 
 
1221
        elif relType=="boundary":
 
1222
            relInfo = QString("(%1) "+relBoundary+" "+relType).arg(relId)
 
1223
 
 
1224
        elif relType=="restriction":
 
1225
            relInfo = QString("(%1) "+relRestr+" "+relType).arg(relId)
 
1226
 
 
1227
        elif relType=="":
 
1228
            relInfo = QString("(%1) relation of unknown type").arg(relId)
 
1229
 
 
1230
        else:
 
1231
            relInfo = QString("(%1) "+relType).arg(relId)
 
1232
 
 
1233
        return relInfo
 
1234
 
 
1235
 
 
1236
    def __undo(self):
 
1237
        """Function performs exactly one undo operation.
 
1238
        """
 
1239
 
 
1240
        self.plugin.undoredo.undo()
 
1241
 
 
1242
 
 
1243
    def __redo(self):
 
1244
        """Function performs exactly one redo operation.
 
1245
        """
 
1246
 
 
1247
        self.plugin.undoredo.redo()
 
1248
 
 
1249
 
 
1250
    def __urDetailsChecked(self):
 
1251
        """Function is called after clicking on urDetailsButton checkbox.
 
1252
        It shows or hides OSM Edit History widget.
 
1253
        """
 
1254
 
 
1255
        if self.urDetailsButton.isChecked():
 
1256
            self.plugin.undoredo.show()
 
1257
            self.urDetailsButton.setToolTip("Hide OSM Edit History")
 
1258
        else:
 
1259
            self.plugin.undoredo.hide()
 
1260
            self.urDetailsButton.setToolTip("Show OSM Edit History")
 
1261
 
 
1262