/
OsmFeatureDW.py
1262 lines (936 loc) · 49.2 KB
/
OsmFeatureDW.py
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-99
"""@package OsmFeatureDW
This module is descendant of "OSM Feature" dockable widget and makes user able
to view and edit information on selected OSM feature.
OsmFeatureDW module shows details of selected feature - its basic info, tags and relations.
It provides methods for editing features' tags so that user can edit them directly on the widget.
There are also some identify and edit buttons on "OsmFeatureDW" - this modul implements all the methods that are called
after clicking on these buttons. Such methods creates (and set) map tool that coresponds to specified button.
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from ui_OsmFeatureDW import Ui_OsmFeatureDW
from OsmAddRelationDlg import OsmAddRelationDlg
import OsmTags
# include all available osm map tools
from map_tools.OsmCreatePointMT import OsmCreatePointMT
from map_tools.OsmCreateLineMT import OsmCreateLineMT
from map_tools.OsmCreatePolygonMT import OsmCreatePolygonMT
from map_tools.OsmMoveMT import OsmMoveMT
from map_tools.OsmIdentifyMT import OsmIdentifyMT
import unicodedata
class OsmFeatureDW(QDockWidget, Ui_OsmFeatureDW, object):
"""This class shows details of selected feature - its basic info, tags and relations.
It provides methods for editing features' tags so that user can edit them directly on the widget.
There are also some identify and edit buttons on "OsmFeatureDW" - this modul implements all the methods that are called
after clicking them. Such methods creates (and set) map tool that coresponds to specified button.
"""
def __init__(self, plugin):
"""The constructor."""
QDockWidget.__init__(self, None)
self.setupUi(self)
self.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
self.plugin=plugin
self.mapTool=None
self.__dlgAddRel=None
# set icons for tool buttons (identify,move,createPoint,createLine,createPolygon)
self.identifyButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_identify.png"))
self.moveButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_move.png"))
self.createPointButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createPoint.png"))
self.createLineButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createLine.png"))
self.createPolygonButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createPolygon.png"))
self.createRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_createRelation.png"))
self.removeButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeFeat.png"))
self.deleteTagsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeTag.png"))
self.undoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_undo.png"))
self.redoButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_redo.png"))
self.addRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_addRelation.png"))
self.removeRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_removeRelation.png"))
self.editRelationButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_editRelation.png"))
self.urDetailsButton.setIcon(QIcon(":/plugins/osm_plugin/images/osm_urDetails.png"))
# initializing group of edit buttons
self.toolButtons=QButtonGroup(self)
self.dummyButton.setVisible(False)
self.toolButtons.addButton(self.dummyButton)
self.toolButtons.addButton(self.identifyButton)
self.toolButtons.addButton(self.moveButton)
self.toolButtons.addButton(self.createPointButton)
self.toolButtons.addButton(self.createLineButton)
self.toolButtons.addButton(self.createPolygonButton)
self.toolButtons.setExclusive(True)
# initializing table of feature tags
self.tagTable.setColumnCount(2)
self.tagTable.setHorizontalHeaderItem(0,QTableWidgetItem("Key"))
self.tagTable.setHorizontalHeaderItem(1,QTableWidgetItem("Value"))
self.tagTable.setSelectionMode(QAbstractItemView.ExtendedSelection)
self.tagTable.setSelectionBehavior(QAbstractItemView.SelectRows)
self.newTagLabel="<new tag here>"
self.relTagsTree.setSelectionMode(QAbstractItemView.NoSelection)
# initializing rubberbands/vertexmarkers; getting qgis settings of line width and color for rubberbands
settings=QSettings()
qgsLineWidth=2 # use fixed width
qgsLineRed=settings.value( "/qgis/digitizing/line_color_red", QVariant(255) ).toInt()
qgsLineGreen=settings.value( "/qgis/digitizing/line_color_green", QVariant(0) ).toInt()
qgsLineBlue=settings.value( "/qgis/digitizing/line_color_blue", QVariant(0) ).toInt()
self.rubBandPol=QgsRubberBand(plugin.canvas,True)
self.rubBandPol.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
self.rubBandPol.setWidth(qgsLineWidth)
self.rubBand=QgsRubberBand(plugin.canvas,False)
self.rubBand.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
self.rubBand.setWidth(qgsLineWidth)
self.verMarker=QgsVertexMarker(plugin.canvas)
self.verMarker.setIconType(2)
self.verMarker.setIconSize(13)
self.verMarker.setColor(QColor(qgsLineRed[0],qgsLineGreen[0],qgsLineBlue[0]))
self.verMarker.setPenWidth(qgsLineWidth)
self.verMarkers=[]
self.relRubBandPol=QgsRubberBand(plugin.canvas,True)
self.relRubBandPol.setColor(QColor(qgsLineRed[0],50,50))
self.relRubBandPol.setWidth(qgsLineWidth+4)
self.relRubBand=QgsRubberBand(plugin.canvas,False)
self.relRubBand.setColor(QColor(qgsLineRed[0],50,50))
self.relRubBand.setWidth(qgsLineWidth+4)
self.relVerMarker=QgsVertexMarker(plugin.canvas)
self.relVerMarker.setIconType(2)
self.relVerMarker.setIconSize(13)
self.relVerMarker.setColor(QColor(qgsLineRed[0],50,50))
self.relVerMarker.setPenWidth(qgsLineWidth)
# initializing inner variables
self.activeEditButton=self.dummyButton
self.__tagsLoaded=False
self.__relTagsLoaded=False
self.feature=None
self.featureType=None
self.featRels=[]
self.featRelTags=[]
self.featRelMembers=[]
# clear all widget items
self.clear()
self.__connectWidgetSignals()
self.removeButton.setEnabled(False)
self.createRelationButton.setCheckable(False)
# set current tab to "Properties"
self.propRelBox.setCurrentIndex(0)
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
# init coordinate transform
self.projectionChanged()
renderer=self.plugin.canvas.mapRenderer()
self.connect(renderer, SIGNAL("hasCrsTransformEnabled(bool)"), self.projectionChanged)
self.connect(renderer, SIGNAL("destinationSrsChanged()"), self.projectionChanged)
def setContentEnabled(self,flag):
self.featInfoBox.setEnabled(flag)
self.propRelBox.setEnabled(flag)
self.identifyButton.setEnabled(flag)
self.moveButton.setEnabled(flag)
self.createPointButton.setEnabled(flag)
self.createLineButton.setEnabled(flag)
self.createPolygonButton.setEnabled(flag)
self.createRelationButton.setEnabled(flag)
if flag:
if self.plugin.undoredo.undoCounter>0:
self.undoButton.setEnabled(True)
if self.plugin.undoredo.redoCounter>0:
self.redoButton.setEnabled(True)
else:
self.undoButton.setEnabled(False)
self.redoButton.setEnabled(False)
self.urDetailsButton.setEnabled(flag)
def projectionChanged(self):
"""Function is connected to signals from QgsMapRenderer.
It updates coordinate transforms.
"""
renderer = self.plugin.canvas.mapRenderer()
if renderer.hasCrsTransformEnabled():
self.coordXform = QgsCoordinateTransform(renderer.destinationSrs(), QgsCoordinateReferenceSystem(4326))
else:
self.coordXform = None
def canvasToOsmCoords(self, point):
"""Performs conversion from canvas to map coordinates.
@param point canvas coordinates to convert
"""
point = self.plugin.canvas.getCoordinateTransform().toMapCoordinates( point )
# optional conversion from map to layer coordinates
if self.coordXform is not None:
point = self.coordXform.transform( point )
return point
def __connectWidgetSignals(self):
"""Function connects all necessary signals to appropriate slots.
"""
# signals emitted on clicking with tag and member tables
QObject.connect(self.identifyButton, SIGNAL("clicked()"), self.__startIdentifyingFeature)
QObject.connect(self.moveButton, SIGNAL("clicked()"), self.__startMovingFeature)
QObject.connect(self.createPointButton, SIGNAL("clicked()"), self.__startPointCreation)
QObject.connect(self.createLineButton, SIGNAL("clicked()"), self.__startLineCreation)
QObject.connect(self.createPolygonButton, SIGNAL("clicked()"), self.__startPolygonCreation)
QObject.connect(self.createRelationButton, SIGNAL("clicked()"), self.__createRelation)
QObject.connect(self.removeButton, SIGNAL("clicked()"), self.removeFeature)
QObject.connect(self.relListWidget, SIGNAL("currentRowChanged(int)"), self.loadRelationStuff)
QObject.connect(self.relMembersList, SIGNAL("currentRowChanged(int)"), self.__showRelMemberOnMap)
QObject.connect(self.addRelationButton, SIGNAL("clicked()"), self.createRelationWithMember)
QObject.connect(self.editRelationButton, SIGNAL("clicked()"), self.editSelectedRelation)
QObject.connect(self.removeRelationButton, SIGNAL("clicked()"), self.removeSelectedRelation)
QObject.connect(self.deleteTagsButton, SIGNAL("clicked()"), self.removeSelectedTags)
QObject.connect(self.tagTable, SIGNAL("cellChanged(int,int)"), self.__onTagsCellChanged)
QObject.connect(self.tagTable, SIGNAL("currentCellChanged(int,int,int,int)"), self.__onCurrentCellChanged)
QObject.connect(self.tagTable, SIGNAL("itemDoubleClicked(QTableWidgetItem*)"), self.__onTagsItemDoubleClicked)
QObject.connect(self.undoButton, SIGNAL("clicked()"), self.__undo)
QObject.connect(self.redoButton, SIGNAL("clicked()"), self.__redo)
QObject.connect(self.urDetailsButton, SIGNAL("clicked()"), self.__urDetailsChecked)
def databaseChanged(self,dbKey):
"""This function is called when current OSM database of plugin changes.
The OsmFeatureDW performs necessary actions and tells current map tool about the change.
@param dbKey key (name) of new current database
"""
if self.__dlgAddRel and dbKey:
self.__dlgAddRel.close()
self.__dlgAddRel=None
self.setContentEnabled(True)
self.plugin.undoredo.setContentEnabled(True)
QMessageBox.information(self, self.tr("OSM Plugin")
,self.tr("The 'Create OSM Relation' dialog was closed automatically because current OSM database was changed."))
# clear the whole OSM Feature dockwidget as well as all related rubberbands and vertex markers
self.clear()
# if some mapTool is currently set tell it about database changing
if self.mapTool:
self.mapTool.databaseChanged(dbKey)
self.activeEditButton=self.dummyButton
# and if new database is None, disable the whole dockwidget
if not dbKey:
self.setContentEnabled(False)
if self.mapTool:
self.plugin.canvas.unsetMapTool(self.mapTool)
self.mapTool=None
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
return
self.setContentEnabled(True)
def clear(self):
"""Function clears all widget items.
It resets rubberbands, vertexmarkers, re-initializes OsmFeatureDW inner structures.
"""
# clear common feature infos
self.typeIdLabel.setText("")
self.userLabel.setText("")
self.createdLabel.setText("")
# clear table with information about feature's tags
self.tagTable.clear()
self.tagTable.setEnabled(False)
self.tagTable.setRowCount(0)
self.tagTable.setColumnCount(0)
# clear table with info about feature's relations
self.relListWidget.clear()
self.relTagsTree.clear()
self.relMembersList.clear()
self.relTagsTree.setColumnCount(0)
self.relListWidget.setEnabled(False)
self.relTagsTree.setEnabled(False)
self.relMembersList.setEnabled(False)
# disable widget buttons
self.deleteTagsButton.setEnabled(False)
self.editRelationButton.setEnabled(False)
self.removeRelationButton.setEnabled(False)
self.addRelationButton.setEnabled(False)
self.removeButton.setEnabled(False)
# remove previous rubber bands
self.rubBand.reset(False)
self.rubBandPol.reset(True)
self.verMarker.setCenter(QgsPoint(-1000,-1000))
self.verMarker.hide()
self.__removeMemberMarkers()
self.verMarkers=[]
self.relRubBand.reset(False)
self.relRubBandPol.reset(True)
self.relVerMarker.setCenter(QgsPoint(-1000,-1000))
self.relVerMarker.hide()
# clear member variables
self.__tagsLoaded=False
self.__relTagsLoaded=False
self.feature=None
self.featureType=None
self.featRels=[]
self.featRelTags=[]
self.featRelMembers=[]
def __createRelation(self):
"""Function calls relation creating process.
Creation is started by displaying appropriate dialog.
"""
# clear dockwidget
self.clear()
self.plugin.iface.mainWindow().statusBar().showMessage("")
self.plugin.canvas.unsetMapTool(self.mapTool)
del self.mapTool
self.mapTool=None
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.activeEditButton=self.dummyButton
self.setContentEnabled(False)
self.plugin.undoredo.setContentEnabled(False)
self.plugin.toolBar.setEnabled(False)
# OsmAddRelationDlg parameters: plugin, newRelationFirstMember, relationToEdit
self.__dlgAddRel=OsmAddRelationDlg(self.plugin, None, None)
self.__dlgAddRel.setWindowModality(Qt.WindowModal)
self.__dlgAddRel.exec_()
self.__dlgAddRel=None
self.setContentEnabled(True)
self.plugin.undoredo.setContentEnabled(True)
self.plugin.toolBar.setEnabled(True)
def createRelationWithMember(self):
"""Function calls relation creating process. Creation is started by displaying appropriate dialog.
Function pre-fills dialog with information on currently loaded feature.
"""
self.__dlgAddRel=OsmAddRelationDlg(self.plugin, QString(self.featureType+" %1").arg(self.feature.id()), None)
# clear dockwidget
self.clear()
self.plugin.iface.mainWindow().statusBar().showMessage("")
self.plugin.canvas.unsetMapTool(self.mapTool)
del self.mapTool
self.mapTool=None
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.activeEditButton=self.dummyButton
self.setContentEnabled(False)
self.plugin.undoredo.setContentEnabled(False)
self.plugin.toolBar.setEnabled(False)
self.__dlgAddRel.setWindowModality(Qt.WindowModal)
self.__dlgAddRel.exec_()
self.__dlgAddRel=None
self.setContentEnabled(True)
self.plugin.undoredo.setContentEnabled(True)
self.plugin.toolBar.setEnabled(True)
def editSelectedRelation(self):
"""Function calls editing of a relation. Editing is started by displaying appropriate dialog.
Relation identifier is not passed to this function. Function has to find it out from current row
of appropriate list widget.
"""
# show modal dialog "Edit relation"
if not self.feature:
QMessageBox.information(self, self.tr("OSM Feature Dock Widget"), self.tr("Choose OSM feature first."))
return
item=self.relListWidget.item(self.relListWidget.currentRow())
if not item:
QMessageBox.information(self, self.tr("OSM Feature Dock Widget"), self.tr("Choose relation for editing first."))
return
relId=self.featRels[self.relListWidget.currentRow()]
self.setContentEnabled(False)
self.plugin.undoredo.setContentEnabled(False)
self.plugin.toolBar.setEnabled(False)
self.__dlgAddRel=OsmAddRelationDlg(self.plugin, None, relId)
self.__dlgAddRel.setWindowModality(Qt.WindowModal)
self.__dlgAddRel.exec_()
self.__dlgAddRel=None
self.setContentEnabled(True)
self.plugin.undoredo.setContentEnabled(True)
self.plugin.toolBar.setEnabled(True)
def __onTagsCellChanged(self,row,column):
"""Function is called after cellChanged(int,int) signal is emitted on table of all features' relations.
It means that user is changed key or value of some existing tag.
@param row index of row in table of tags
@param column index of column in table of tags
"""
if not self.__tagsLoaded:
# this signal was emitted during table initialization,
# but we are interested in user actions only
return
if row<self.tagTable.rowCount()-1:
# changing value of tag that already exists
key=self.tagTable.item(row,0).text()
value=self.tagTable.item(row,1).text()
# store tag's change into database
self.plugin.undoredo.startAction("Change tag value.")
self.plugin.dbm.changeTagValue(self.feature.id(),self.featureType,key.toAscii().data(),value.toUtf8())
else:
key = self.tagTable.item(row,0).text()
if key=="" or key==self.newTagLabel:
return
# adding new tag and setting its key
if column==0:
# only ascii keys are allowed
nkfd_form=unicodedata.normalize('NFKD', unicode(key.toUtf8(),'utf-8'))
key_only_ascii=nkfd_form.encode('ASCII', 'ignore')
# store it into database
isAlreadyDef=self.plugin.dbm.isTagDefined(self.feature.id(),self.featureType,key_only_ascii)
if isAlreadyDef:
# such a key already exists for this relation
self.tagTable.setItem(row,0,QTableWidgetItem(self.newTagLabel))
QMessageBox.information(self, self.tr("OSM Feature Dock Widget")
,self.tr(QString("Property '").append(key_only_ascii).append("' cannot be added twice.")))
return
# well, insert new tag into database
self.plugin.undoredo.startAction("Insert new tag.")
self.plugin.dbm.insertTag(self.feature.id(),self.featureType,key_only_ascii,None)
self.__tagsLoaded=False
self.tagTable.item(row,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.tagTable.item(row,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.tagTable.item(row,0).setText(QString(key_only_ascii))
newLastRow = row+1
self.tagTable.setRowCount(row+2)
self.tagTable.setItem(newLastRow,0,QTableWidgetItem(self.newTagLabel))
self.tagTable.setItem(newLastRow,1,QTableWidgetItem(""))
self.tagTable.item(newLastRow,0).setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.tagTable.item(newLastRow,1).setFlags(Qt.ItemIsEnabled)
self.__tagsLoaded=True
# updating feature status (from Normal to Updated)
if self.featureType=='Point':
self.plugin.dbm.changePointStatus(self.feature.id(),'N','U')
elif self.featureType=='Line':
self.plugin.dbm.changeLineStatus(self.feature.id(),'N','U')
elif self.featureType=='Polygon':
self.plugin.dbm.changePolygonStatus(self.feature.id(),'N','U')
elif self.featureType=='Relation':
self.plugin.dbm.changeRelationStatus(self.feature.id(),'N','U')
self.plugin.dbm.commit()
affected=[(self.feature.id(),self.featureType)]
self.plugin.undoredo.stopAction(affected)
self.plugin.dbm.recacheAffectedNow(affected)
# refresh map canvas so that changes take effect
if column==1:
self.plugin.canvas.refresh()
def __onTagsItemDoubleClicked(self,item):
"""Function is called after itemDoubleClicked(...) signal is emitted on table of feature tags.
It shows combobox with possible values for given item of table.
@param item item of table of feature tags
"""
if item.column()==0:
if item.row()<self.tagTable.rowCount()-1:
return
tagValues=OsmTags.suitableTagKeys(self.featureType)
tagValues.sort()
if len(tagValues)>0:
valCombo=QComboBox()
valCombo.setEditable(True)
valCombo.addItems(tagValues)
currentComboText=self.tagTable.item(item.row(),0).text()
ix=valCombo.findText(currentComboText)
valCombo.setCurrentIndex(ix)
if ix==-1:
valCombo.setEditText(currentComboText)
self.tagTable.setCellWidget(item.row(),0,valCombo)
QObject.connect(valCombo, SIGNAL("currentIndexChanged(const QString &)"), self.__onTagKeySelectionChanged)
return
key=str(self.tagTable.item(item.row(),0).text())
tagValues=OsmTags.suitableTagValues(self.featureType,key)
tagValues.sort()
if len(tagValues)>0:
valCombo=QComboBox()
valCombo.setEditable(True)
valCombo.addItems(tagValues)
currentComboText=self.tagTable.item(item.row(),1).text()
ix=valCombo.findText(currentComboText)
valCombo.setCurrentIndex(ix)
if ix==-1:
valCombo.setEditText(currentComboText)
self.tagTable.setCellWidget(item.row(),1,valCombo)
QObject.connect(valCombo, SIGNAL("currentIndexChanged(const QString &)"), self.__onTagValueSelectionChanged)
def __onCurrentCellChanged(self,curRow,curCol,prevRow,prevCol):
"""Function is called after currentCellChanged(...) signal is emitted on table of feature tags.
@param curRow current row index to tags table
@param curCol current column index to tags table
@param prevRow previous row index to tags table
@param prevCol previous column index to tags table
"""
cellWidget=self.tagTable.cellWidget(prevRow,prevCol)
if cellWidget==None:
return
self.tagTable.removeCellWidget(prevRow,prevCol)
def __onTagKeySelectionChanged(self,key):
"""Function is called after currentIndexChanged(...) signal is emitted on combobox in 1st column of tags table.
@param key key selected in combobox
"""
row=self.tagTable.currentRow()
col=self.tagTable.currentColumn()
self.tagTable.item(row,col).setText(key)
self.tagTable.removeCellWidget(row,col)
def __onTagValueSelectionChanged(self,value):
"""Function is called after currentIndexChanged(...) signal is emitted on combobox in 2nd column of tags table.
@param value value selected in combobox
"""
row=self.tagTable.currentRow()
col=self.tagTable.currentColumn()
self.tagTable.item(row,col).setText(value)
self.tagTable.removeCellWidget(row,col)
def __startIdentifyingFeature(self):
"""Function prepares feature identification.
The appropriate map tool (OsmIdentifyMT) is set to map canvas.
"""
if self.activeEditButton==self.identifyButton:
return
self.plugin.canvas.unsetMapTool(self.plugin.canvas.mapTool())
# clear dockwidget
self.clear()
self.plugin.iface.mainWindow().statusBar().showMessage("")
self.mapTool=OsmIdentifyMT(self.plugin.canvas, self, self.plugin.dbm)
self.plugin.canvas.setMapTool(self.mapTool)
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.activeEditButton=self.identifyButton
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
def __startMovingFeature(self):
"""Function prepares feature moving.
The appropriate map tool (OsmMoveMT) is set to map canvas.
"""
if self.activeEditButton==self.moveButton:
return
# clear dockwidget
self.clear()
self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
self.mapTool=OsmMoveMT(self.plugin)
self.plugin.canvas.setMapTool(self.mapTool)
self.plugin.canvas.setCursor(QCursor(Qt.CrossCursor))
self.activeEditButton=self.moveButton
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
def __startPointCreation(self):
"""Function prepares point creating operation.
The appropriate map tool (OsmCreatePointMT) is set to map canvas.
"""
if self.activeEditButton==self.createPointButton:
return
self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
self.mapTool=OsmCreatePointMT(self.plugin)
self.plugin.canvas.setMapTool(self.mapTool)
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.activeEditButton=self.createPointButton
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
def __startLineCreation(self):
"""Function prepares line creating operation.
The appropriate map tool (OsmCreateLineMT) is set to map canvas.
"""
if self.activeEditButton==self.createLineButton:
return
self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
self.mapTool=OsmCreateLineMT(self.plugin)
self.plugin.canvas.setMapTool(self.mapTool)
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.activeEditButton=self.createLineButton
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
def __startPolygonCreation(self):
"""Function prepares polygon creating operation.
The appropriate map tool (OsmCreatePolygonMT) is set to map canvas.
"""
if self.activeEditButton==self.createPolygonButton:
return
self.plugin.iface.mainWindow().statusBar().showMessage("Snapping ON. Hold Ctrl to disable it.")
self.mapTool=OsmCreatePolygonMT(self.plugin)
self.plugin.canvas.setMapTool(self.mapTool)
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.activeEditButton=self.createPolygonButton
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
def removeFeature(self):
"""Function completely removes feature that is currently loaded on "OSM Feature" widget.
"""
self.removeButton.setDown(False)
self.removeButton.setChecked(False)
self.plugin.iface.mainWindow().statusBar().showMessage("")
# remove object that was identified by "identify tool"
featId=self.feature.id()
featType=self.featureType
affected=[]
if featType=='Point':
self.plugin.undoredo.startAction("Remove point.")
affected=self.plugin.dbm.removePoint(featId)
elif featType=='Line':
self.plugin.undoredo.startAction("Remove line.")
affected=self.plugin.dbm.removeLine(featId,True) # todo: False when Ctrl pressed
elif featType=='Polygon':
self.plugin.undoredo.startAction("Remove polygon.")
affected=self.plugin.dbm.removePolygon(featId,True) # todo: False when Ctrl pressed
elif featType=='Relation':
self.plugin.undoredo.startAction("Remove relation.")
self.plugin.dbm.removeRelation(featId)
else:
return # strange situation
self.plugin.undoredo.stopAction(affected)
self.plugin.dbm.recacheAffectedNow(affected)
# refresh map canvas so that changes take effect
self.plugin.canvas.refresh()
# clear dockwidget
self.clear()
self.plugin.iface.mainWindow().statusBar().showMessage("")
self.plugin.canvas.unsetMapTool(self.mapTool)
del self.mapTool
self.mapTool=OsmIdentifyMT(self.plugin.canvas, self, self.plugin.dbm)
self.plugin.canvas.setMapTool(self.mapTool)
self.plugin.canvas.setCursor(QCursor(Qt.ArrowCursor))
self.plugin.canvas.setFocus(Qt.OtherFocusReason)
self.activeEditButton=self.identifyButton
self.activeEditButton.setChecked(True)
def removeSelectedTags(self):
"""Function completely removes all tags that are currently selected in the appropriate
list of the "OSM Feature" widget. More than one tag can be selected using Ctrl and clicking.
"""
# remove selected tags (rows)
selectedItems=self.tagTable.selectedItems()
selectedRowsIndexes=[]
lastRowIndex=self.tagTable.rowCount()-1
self.tagTable.setCurrentCell(lastRowIndex,0)
for i in selectedItems:
if i.column()==0 and not i.row()==lastRowIndex:
selectedRowsIndexes.append(i.row())
self.plugin.undoredo.startAction("Remove tags.")
selectedRowsIndexes.sort()
selectedRowsIndexes.reverse()
for ix in selectedRowsIndexes:
key=self.tagTable.item(ix,0).text()
# updating feature status (from Normal to Updated)
if self.featureType=='Point':
self.plugin.dbm.changePointStatus(self.feature.id(),'N','U')
elif self.featureType=='Line':
self.plugin.dbm.changeLineStatus(self.feature.id(),'N','U')
elif self.featureType=='Polygon':
self.plugin.dbm.changePolygonStatus(self.feature.id(),'N','U')
elif self.featureType=='Relation':
self.plugin.dbm.changeRelationStatus(self.feature.id(),'N','U')
# perform tag removing
self.plugin.dbm.removeTag(self.feature.id(),self.featureType,key.toAscii().data())
self.tagTable.removeRow(ix)
# make this action permanent
self.plugin.dbm.commit()
affected=[(self.feature.id(),self.featureType)]
self.plugin.undoredo.stopAction(affected)
self.plugin.dbm.recacheAffectedNow(affected)
# refresh map canvas so that changes take effect
self.plugin.canvas.refresh()
def removeSelectedRelation(self):
"""Function completely removes all relations that are selected in the appropriate list of the "OSM Feature" widget.
More than one relation can be selected using Ctrl and clicking.
"""
# find out id of relation which is selected
item=self.relListWidget.item(self.relListWidget.currentRow())
if not item or not self.plugin.dbm.currentKey:
return
relId=self.featRels[self.relListWidget.currentRow()]
self.plugin.undoredo.startAction("Remove relation.")
self.plugin.dbm.removeRelation(relId)
self.plugin.dbm.commit()
self.plugin.undoredo.stopAction()
# reload list of feature's relations
self.__loadFeatureRelations(self.feature.id(),self.featureType)
def __showRelMemberOnMap(self,ixRow):
"""Functions marks relation member on the map.
Marking is realized with simple rubberBand (in case of line/polygon) or vertexMarker (for point).
Relation member is given by its index to the list of all members of currently loaded relation.
@param ixRow index to the list of all relations on mentioned widget
"""
# move rubberband to selected feature
self.relRubBand.reset(False)
self.relRubBandPol.reset(True)
self.relVerMarker.hide()
if ixRow==-1 or not self.plugin.dbm.currentKey:
return
mem=self.featRelMembers[ixRow]
self.showFeatureOnMap(mem[0],mem[1]) # id & type
def loadRelationStuff(self,ixRow):
"""Functions loads information on specified relation into "OSM Feature" widget.
Relation is given by its index to the list of all relations on mentioned widget.
Relation "stuff" means its basic info, tags and members.
@param ixRow index to the list of all relations on mentioned widget
"""
if ixRow==-1 or not self.plugin.dbm.currentKey:
return
relId=self.featRels[ixRow]
# show all tags connected to selected relation
self.__loadRelationTags(relId)
# show all relation members on osm dock widget
self.__loadRelationMembers(relId)
# enable list of members and buttons for relation removing and editing
self.relMembersList.setEnabled(True)
self.editRelationButton.setEnabled(True)
self.removeRelationButton.setEnabled(True)
def __loadRelationMembers(self,relId):
"""Functions loads the list of members of specified relation.
Relation is given by its identifier.
Loading is realized into the appropriate QListWidget of "OSM Feature" dockable widget.
Resulting list contains info on member's identifier, type and its role in specified relation.
@param relId identifier of relation
"""
self.relMembersList.clear()
# ask database manager for all relation members
self.featRelMembers=self.plugin.dbm.getRelationMembers(relId)
# printing members
for i in range(0,len(self.featRelMembers)):
memId=self.featRelMembers[i][0]
memType=self.featRelMembers[i][1]
memRole=self.featRelMembers[i][2]
listRow=QString("(%1) - %2").arg(memId).arg(memType)
if memRole and memRole<>"":
listRow=listRow.append(QString(", role:%3").arg(memRole))
self.relMembersList.addItem(listRow)
def __loadRelationTags(self,relId):
"""Functions loads the list of tags of specified relation.
Relation is given by its identifier.
Loading is realized into the appropriate QTreeWidget of "OSM Feature" dockable widget.
Resulting tags table has two columns "Key","Value" for easy representing of tag pairs.
@param relId identifier of relation
"""
self.relTagsTree.clear()
self.__relTagsLoaded=False
# ask database manager for all relation tags
self.featRelTags=self.plugin.dbm.getFeatureTags(relId,"Relation")
self.relTagsTree.setColumnCount(2)
self.relTagsTree.setHeaderLabels(["Key","Value"])
for i in range(0,len(self.featRelTags)):
self.relTagsTree.addTopLevelItem(QTreeWidgetItem([self.featRelTags[i][0],self.featRelTags[i][1]]))
self.__relTagsLoaded=True
def __loadFeatureInformation(self,featId,featType):
"""Functions shows up the basic information on feature.
Feature is given by its identifier and its type.
Info is loaded to appropriate place of the "OSM Feature" widget.
Basic info consists (mainly) of feature's identifier, type, owner and timestamp.
@param featId identifier of feature to load
@param featType type of feature to load - one of 'Point','Line','Polygon'
"""
# asking OsmDatabaseManager for missing information
featUser=self.plugin.dbm.getFeatureOwner(featId,featType)
featCreated=self.plugin.dbm.getFeatureCreated(featId,featType) # returned as string
# timestamp example: "2008-02-18T15:34:14Z"
self.typeIdLabel.setText("")
self.userLabel.setText("")
self.createdLabel.setText("")
# put non-tags feature information on dock widget
if featType:
self.typeIdLabel.setText(QString("%1 %2").arg(featType).arg(str(featId)))
if featUser:
self.userLabel.setText(QString("%1").arg(featUser))
if featCreated:
# format timestamp
DT_format_osm=Qt.ISODate
# "yyyy-MM-ddThh:mm:ssZ"
DT_format_plugin="yy/MM/dd - h:mm"
self.createdLabel.setText(QDateTime.fromString(featCreated,DT_format_osm).toString(DT_format_plugin))
def __loadFeatureTags(self,featId,featType):
"""Functions loads the list of tags of specified feature.
Feature is given by its identifier and its type.
Loading is realized into the appropriate QTableWidget of "OSM Feature" dockable widget.
Resulting tags table has two columns "Key","Value" for easy representing of tag pairs.
@param featId identifier of feature to load
@param featType type of feature to load - one of 'Point','Line','Polygon'
"""
# clear table with information about feature's tags
self.tagTable.clear()
# fill tableWidget with tags of selected feature
tableData=self.plugin.dbm.getFeatureTags(featId,featType)
rowCount=len(tableData)
self.__tagsLoaded=False
self.tagTable.setRowCount(rowCount+1)
self.tagTable.setColumnCount(2)
self.tagTable.setHorizontalHeaderItem(0,QTableWidgetItem("Key"))
self.tagTable.setHorizontalHeaderItem(1,QTableWidgetItem("Value"))
for i in range(0,rowCount):
self.tagTable.setItem(i,0,QTableWidgetItem(QString.fromUtf8(tableData[i][0])))
self.tagTable.setItem(i,1,QTableWidgetItem(QString.fromUtf8(tableData[i][1])))
self.tagTable.item(i,0).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled)
self.tagTable.item(i,1).setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.tagTable.setItem(rowCount,0,QTableWidgetItem(self.newTagLabel))
self.tagTable.setItem(rowCount,1,QTableWidgetItem(""))
self.tagTable.item(rowCount,0).setFlags(Qt.ItemIsEnabled | Qt.ItemIsEditable)
self.tagTable.item(rowCount,1).setFlags(Qt.ItemIsEnabled)
self.__tagsLoaded=True
# enable tags table for editing
self.tagTable.setEnabled(True)
self.deleteTagsButton.setEnabled(True)
def __loadFeatureRelations(self,featId,featType):
"""Functions loads the list of relations of specified feature.
Feature is given by its identifier and its type.
Loading is realized into the appropriate QListWidget of "OSM Feature" dockable widget.
If no relation exists for specified feature, listWidget is filled with the only row with text: "<no relation>".
@param featId identifier of feature to load
@param featType type of feature to load - one of 'Point','Line','Polygon'
"""
self.relTagsTree.setColumnCount(0)
self.relMembersList.setEnabled(False)
# disable widget buttons
self.editRelationButton.setEnabled(False)
self.removeRelationButton.setEnabled(False)
# clear all tables connected to relations
self.relListWidget.clear()
self.relTagsTree.clear()
self.relMembersList.clear()