-
Notifications
You must be signed in to change notification settings - Fork 16
/
pgVersion.py
824 lines (693 loc) · 33.2 KB
/
pgVersion.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Plugin for the Postgres Versioning System
-----------------------------------------------------------------------------------------------------------------
begin : 2010-07-31
copyright : (C) 2010 by Dr. Horst Duester
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
from qgis.core import *
from qgis.gui import *
from .dbtools.dbtools import *
from .forms.CommitMessage import CommitMessageDialog
from .pgVersionConflictWindow import ConflictWindow
from .forms.incrementalLayerUpdate import IncrementalLayerUpdateDialog
from .forms.pgLoadVersion import PgVersionLoadDialog
from .forms.help import HelpDialog
from .forms.LogView import LogView
from .pgVersionTools import PgVersionTools
from .about.doAbout import DlgAbout
import os
class PgVersion(QObject):
def __init__(self, iface):
QObject.__init__(self)
# Save reference to the QGIS interface
self.iface = iface
self.w = None
self.vsCheck = None
self.layer_list = []
self.tools = PgVersionTools(self)
# Initialise thetranslation environment
self.plugin_path = os.path.dirname(__file__)
self.locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_path,
'i18n',
'pgVersion_{}.qm'.format(self.locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
def initGui(self):
self.helpDialog = HelpDialog()
self.LogViewDialog = LogView(self)
self.toolBar = self.iface.addToolBar("PG Version")
self.toolBar.setObjectName("PG Version")
self.actionInit = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-init.svg"),
self.tr("Prepare Layer for Versioning"),
self.iface.mainWindow())
self.actionLoad = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-commit.svg"),
self.tr("Load Versioned Layer"), self.iface.mainWindow())
self.actionCommit = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-load.svg"),
self.tr("Commit Changes"), self.iface.mainWindow())
self.actionRevert = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-revert.svg"),
self.tr("Revert to HEAD Revision"), self.iface.mainWindow())
self.actionLogView = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-log.svg"),
self.tr("Show Logs"), self.iface.mainWindow())
self.actionDiff = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-diff.svg"),
self.tr("Show Diffs"), self.iface.mainWindow())
self.actionDrop = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-drop.svg"),
self.tr("Drop Versioning from Layer"), self.iface.mainWindow())
self.actionHelp = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-help.svg"),
self.tr("Help"),
self.iface.mainWindow())
self.actionAbout = QAction(QIcon(""), self.tr("About"),
self.iface.mainWindow())
self.actionDelete = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-drop.svg"),
self.tr("Bulk delete directly in the database"),
self.iface.mainWindow())
self.actionIncrementalUpdate = QAction(
QIcon(":/plugins/pgversion/icons/pgversion-incremental_update.svg"),
self.tr("Incremental Layer Update"),
self.iface.mainWindow())
self.actionDelete.setEnabled(False)
self.set_actions(False)
self.actionList = [self.actionInit,
self.actionLoad,
self.actionCommit,
self.actionDiff,
self.actionRevert,
self.actionLogView,
self.actionDrop,
self.actionLogView,
self.actionDelete,
self.actionIncrementalUpdate,
self.actionHelp,
self.actionAbout]
self.toolBar.addAction(self.actionInit)
self.toolBar.addAction(self.actionLoad)
self.toolBar.addAction(self.actionCommit)
self.toolBar.addAction(self.actionRevert)
self.toolBar.addAction(self.actionDiff)
self.toolBar.addAction(self.actionIncrementalUpdate)
self.toolBar.addAction(self.actionLogView)
self.toolBar.addAction(self.actionDelete)
self.toolBar.addAction(self.actionHelp)
try:
for a in self.actionList:
self.iface.addPluginToDatabaseMenu("PG Version", a)
except AttributeError:
self.menu = QMenu()
self.menu.setTitle("PG Version")
self.menu.addActions(self.actionList)
self.menuBar = self.iface.mainWindow().menuBar()
self.menuBar.addMenu(self.menu)
# connect the action to the run method
self.actionInit.triggered.connect(self.doInit)
self.actionLoad.triggered.connect(self.doLoad)
self.actionDiff.triggered.connect(self.doDiff)
self.actionCommit.triggered.connect(self.doCommit)
self.actionRevert.triggered.connect(self.doRevert)
self.actionLogView.triggered.connect(self.doLogView)
self.actionDrop.triggered.connect(self.doDrop)
self.actionHelp.triggered.connect(self.doHelp)
self.actionAbout.triggered.connect(self.doAbout)
self.actionDelete.triggered.connect(self.doDelete)
self.actionIncrementalUpdate.triggered.connect(self.doIncrementalUpdate)
self.LogViewDialog.diffLayer.connect(self.doDiff)
self.LogViewDialog.rollbackLayer.connect(self.doRollback)
self.LogViewDialog.checkoutLayer.connect(self.doCheckout)
self.LogViewDialog.checkoutTag.connect(self.doCheckout)
for a in self.iface.digitizeToolBar().actions():
if a.objectName() == 'mActionToggleEditing':
a.triggered.connect(self.SelectionChanged)
self.iface.mapCanvas().selectionChanged.connect(self.SelectionChanged)
self.iface.currentLayerChanged.connect(self.layer_changed)
QgsProject().instance().layerWasAdded.connect(self.add_layer)
QgsProject().instance().layerWillBeRemoved.connect(self.remove_layer)
def datasource_changed(self, l):
if self.tools.hasVersion(l):
self.add_layer(l)
self.set_actions(True)
if self.tools.isModified(l):
if '(modified)' not in l.name():
l.setName(l.name() + ' (modified)')
else:
l.setName(l.name().replace(' (modified)', ''))
def layer_changed(self):
if self.tools.hasVersion(self.iface.activeLayer()):
self.set_actions(True)
else:
self.set_actions(False)
def SelectionChanged(self):
current_layer = self.iface.activeLayer()
if current_layer is not None:
if (current_layer.type() == QgsMapLayer.VectorLayer) and current_layer.selectedFeatureCount() == 0:
self.actionDelete.setEnabled(False)
else:
if self.tools.hasVersion(current_layer):
if current_layer.isEditable():
self.actionDelete.setEnabled(True)
else:
self.actionDelete.setEnabled(False)
else:
self.actionDelete.setEnabled(False)
def set_actions(self, isActive):
# self.actionDelete.setEnabled(isActive)
# self.actionLoad.setEnabled(isActive)
self.actionCommit.setEnabled(isActive)
self.actionRevert.setEnabled(isActive)
self.actionDiff.setEnabled(isActive)
self.actionLogView.setEnabled(isActive)
self.actionIncrementalUpdate.setEnabled(isActive)
def add_layer(self, l):
l.dataSourceChanged.connect(lambda my_layer=l: self.datasource_changed(my_layer))
if self.tools.hasVersion(l):
if l.id not in self.layer_list:
l.editingStopped.connect(
lambda my_list=self.layer_list: self.tools.setModified(
my_list))
self.layer_list.append(l.id())
self.tools.setModified(self.layer_list)
def remove_layer(self, id):
self.layer_list = list(set(self.layer_list))
if id in set(self.layer_list):
self.layer_list.remove(id)
def unload(self):
QSettings().setValue("/pgVersion/commit_messages", "")
try:
for a in self.actionList:
self.iface.removePluginDatabaseMenu("PG Version", a)
except:
del self.menuBar
del self.toolBar
def doDelete(self):
res = QMessageBox.question(
None,
self.tr("Question"),
self.tr(
"Are you sure to delete all selected features. "
"You cannot undo this action!"),
QMessageBox.No | QMessageBox.Yes)
if res == QMessageBox.Yes:
QApplication.restoreOverrideCursor()
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
mySchema = self.tools.layerSchema(currentLayer)
myTable = self.tools.layerTable(currentLayer)
myPkey = QgsDataSourceUri(
currentLayer.dataProvider().dataSourceUri()).keyColumn()
myDb = self.tools.layerDB('doInit', currentLayer)
selectedFeatures = currentLayer.selectedFeatures()
delete_list = "("
if currentLayer.selectedFeatureCount() > 0:
for feature in selectedFeatures:
delete_list += str(feature.attributes()[0]) + ", "
delete_list = delete_list[:-2]
delete_list += ")"
sql = "delete from \"%s\".\"%s\" where \"%s\" in %s" % (
mySchema, myTable, myPkey, delete_list)
QApplication.setOverrideCursor(Qt.WaitCursor)
success, error = myDb.run(sql)
QApplication.restoreOverrideCursor()
currentLayer.removeSelection()
currentLayer.triggerRepaint()
self.tools.setModified(self.layer_list)
def doInit(self):
currentLayer = self.iface.activeLayer()
if currentLayer is None:
QMessageBox.warning(
None, '', self.tr('Please select a layer for versioning'))
return
elif currentLayer.dataProvider().name() != 'postgres':
QMessageBox.warning(
None, '', self.tr('Please select a postgres layer '
'for versioning'))
return
elif self.tools.hasVersion(currentLayer):
QMessageBox.warning(
None, self.tr('Warning'),
self.tr('The selected layer is already under versioning!'))
return
else:
mySchema = self.tools.layerSchema(currentLayer)
myTable = self.tools.layerTable(currentLayer)
myDb = self.tools.layerDB('doInit', currentLayer)
if not self.tools.check_PGVS_revision(myDb):
return
if not self.tools.versionExists(currentLayer):
answer = QMessageBox.question(
None, '', self.tr(
'Do you want to create the version environment\
for the table {0}?').format(
mySchema + '.' + myTable),
QMessageBox.Yes | QMessageBox.No)
if answer == QMessageBox.No:
return
QApplication.setOverrideCursor(Qt.WaitCursor)
sql = "select * from versions.pgvsinit('%s.%s')" % (
mySchema, myTable)
result, error = myDb.run(sql)
QApplication.restoreOverrideCursor()
if result is True:
QMessageBox. information(None, 'Init', self.tr(
"""
Init was successful!
Please set the user permissions for table {0} and reload it via Database -> PG Version!
Further information on rights management can be found in the help in section 1.3.3""").format(myTable))
QgsProject().instance().removeMapLayer(
currentLayer.id())
self.iface.mapCanvas().refreshAllLayers()
else:
QMessageBox.warning(None,
self.tr('Init Error'),
self.tr("""
Initialisation of table {0} failed!
Error:
{1}
""").format(
mySchema + "." + myTable,
error))
else:
QMessageBox.information(
None,
"",
self.tr("""
The layer: {0} was already versioned.
If you want to use the versioned layer, then add the
following table from the DB: {0}_version or
add the layer through the Load Versioned Layer button""".format(self.iface.activeLayer().name())))
def doLoad(self):
self.dlg = PgVersionLoadDialog(self)
result = self.dlg.exec_()
if result == 1:
self.set_actions(True)
def doRollback(self, item):
if item is None:
QMessageBox.information(None, self.tr('Error'),
self.tr('Please select a valid revision'))
return
revision = item.text(0)
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
if currentLayer is None:
QMessageBox.information(
None, '', self.tr('Please select a versioned layer'))
return
else:
answer = QMessageBox.question(
None, '', self.tr(
'Are you sure to rollback to revision {0}?').format(
revision), QMessageBox.Yes | QMessageBox.Cancel)
if answer == QMessageBox.Cancel:
return
if self.tools.isModified(currentLayer):
answer = QMessageBox.question(
None, '',
self.tr("""Layer {0} has modifications which will be lost after rollback! If you want to keep this modifications please commit them before you execute the rollback.
Are you sure to rollback to revision {1}?""").format(currentLayer.name(), revision),QMessageBox.Yes | QMessageBox.Cancel)
if answer == QMessageBox.Cancel:
return
QApplication.setOverrideCursor(Qt.WaitCursor)
provider = currentLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('doRollback', currentLayer)
if not self.tools.check_PGVS_revision(myDb):
return
mySchema = QgsDataSourceUri(uri).schema()
if len(mySchema) == 0:
mySchema = 'public'
myTable = QgsDataSourceUri(uri).table()
sql = "select * from versions.pgvsrollback('" + mySchema + "." + myTable.replace('_version', '') + "'," + revision + ")"
success, error = myDb.run(sql)
self.LogViewDialog.close()
currentLayer.triggerRepaint()
QApplication.restoreOverrideCursor()
self.tools.setModified(self.layer_list)
self.LogViewDialog.close()
self.iface.messageBar().pushMessage(
'Info',
self.tr('Rollback to revision {0} was successful!').format(
revision), level=Qgis.MessageLevel(0), duration=3)
def doCommit(self):
canvas = self.iface.mapCanvas()
if canvas.currentLayer() is None:
self.iface.messageBar().pushMessage(
'Warning', self.tr(
'Please select a versioned layer for committing'),
level=Qgis.MessageLevel(1), duration=3)
return
canvas = self.iface.mapCanvas()
theLayer = canvas.currentLayer()
mySchema = self.tools.layerSchema(theLayer)
myTable = self.tools.layerTable(theLayer).replace('_version', '')
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(None, self.tr('Warning'),
self.tr('Please select a versioned layer!'))
else:
if self.tools.isModified(theLayer):
confRecords = self.tools.confRecords(theLayer)
if confRecords is None:
self.dlgCommitMessage = CommitMessageDialog()
self.dlgCommitMessage.show()
result = self.dlgCommitMessage.exec_()
if result == QDialog.Accepted:
QApplication.setOverrideCursor(Qt.WaitCursor)
sql = "select * from versions.pgvscommit('%s.%s','%s')\
" % (
mySchema, myTable,
self.dlgCommitMessage.textEdit.toPlainText())
myDB = self.tools.layerDB('commit', theLayer)
success, error = myDB.run(sql)
self.tools.layerRepaint()
self.iface.messageBar().pushMessage(
"Info",
self.tr('Commit of your changes was successful'),
level=Qgis.MessageLevel(0), duration=3)
self.tools.setModified(self.layer_list)
QApplication.restoreOverrideCursor()
else:
if self.w is not None:
self.w = None
self.w = ConflictWindow(theLayer, 'conflict', self)
self.w.show()
self.tools.setModified(self.layer_list)
else:
self.iface.messageBar().pushMessage(
'Info',
self.tr('No layer changes for committing, everything is\
OK'), level=Qgis.MessageLevel(0), duration=3)
self.tools.setModified(self.layer_list)
def doCheckout(self, revision, tag=None):
if revision is None:
QMessageBox.information(
None, self.tr('Error'),
self.tr('Please select a valid revision'))
return
canvas = self.iface.mapCanvas()
currentLayer = canvas.currentLayer()
if currentLayer is None:
QMessageBox.information(
None, '', self.tr('Please select a versioned layer'))
return
else:
answer = QMessageBox.question(
None, '',
self.tr('Are you sure to checkout the layer to '
'revision {0}?').format(revision),
QMessageBox.Yes | QMessageBox.Cancel)
if answer == QMessageBox.Yes:
QApplication.setOverrideCursor(Qt.WaitCursor)
provider = currentLayer.dataProvider()
uri = provider.dataSourceUri()
uniqueCol = QgsDataSourceUri(uri).keyColumn()
geomCol = QgsDataSourceUri(uri).geometryColumn()
mySchema = QgsDataSourceUri(uri).schema()
myTable = QgsDataSourceUri(uri).table()
if len(mySchema) == 0:
mySchema = 'public'
sql = """select * from versions.pgvscheckout(NULL::"{0}"."{1}", {2})""".format(
mySchema,
myTable.replace('_version', ''),
revision)
myUri = QgsDataSourceUri(uri)
myUri.setDataSource("", u"(%s\n)" % (sql), geomCol, "",
uniqueCol)
layer = None
if tag:
table_ext = " (Tag: %s)" % tag
else:
table_ext = " (Revision: %s)" % revision
layer = QgsVectorLayer(
myUri.uri(), myTable + table_ext, "postgres")
QgsProject().instance().addMapLayer(layer)
QApplication.restoreOverrideCursor()
if layer.isValid():
self.iface.messageBar().pushMessage(
'Info', self.tr('Checkout to revision {0} was \
successful!').format(revision),
level=Qgis.MessageLevel(0), duration=3)
layer.triggerRepaint()
else:
self.iface.messageBar().pushMessage(
'Info', self.tr('Something went wrong during checkout \
to revision {0}!').format(
revision),
level=Qgis.MessageLevel(0), duration=3)
self.LogViewDialog.close()
return
def doRevert(self):
theLayer = self.iface.activeLayer()
if theLayer is None:
QMessageBox.warning(None,
self.tr("Warning"),
self.tr("No Layer was selected. "
"Please select one Layer."))
return
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('revert', theLayer)
mySchema = QgsDataSourceUri(uri).schema()
myTable = QgsDataSourceUri(uri).table()
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(
None, self.tr(
'Warning'), self.tr('Please select a versioned layer!'))
else:
if not self.tools.isModified(theLayer):
self.iface.messageBar().pushMessage(
"Warning",
self.tr(
'The selected Layer is already at the HEAD Revision.'),
level=Qgis.MessageLevel(1), duration=3)
return
if len(mySchema) == 1:
mySchema = 'public'
answer = QMessageBox.question(
None, '', self.tr(
'Are you sure to revert to the HEAD revision?'),
QMessageBox.Yes | QMessageBox.Cancel)
if answer == QMessageBox.Yes:
sql = "select * from versions.pgvsrevert('" + mySchema + "." + myTable.replace('_version', '') + "')"
result, error = myDb.read(sql)
success, error = myDb.run(sql)
if not success:
QMessageBox.information(None, '', result)
else:
self.iface.messageBar().pushMessage(
"Info",
self.tr('All changes are set back to the HEAD \
revision: {0}').format(
str(result["PGVSREVERT"][0])),
level=Qgis.MessageLevel(0), duration=3)
self.tools.setModified(self.layer_list)
theLayer.triggerRepaint()
def doLogView(self):
canvas = self.iface.mapCanvas()
theLayer = self.iface.activeLayer()
if not self.tools.hasVersion(theLayer):
QMessageBox.warning(None, self.tr('Warning'), self.tr(
'Please select a versioned layer!'))
return
if theLayer is not None:
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('logview', theLayer)
mySchema = QgsDataSourceUri(uri).schema()
myTable = QgsDataSourceUri(uri).table()
if len(mySchema) == 0:
mySchema = 'public'
sql = "select * from versions.pgvslogview('" + mySchema + "." + myTable.replace(
'_version', '') + "') order by revision desc"
result, error = myDb.read(sql)
self.LogViewDialog.setLayer(theLayer)
self.LogViewDialog.createTagList()
self.LogViewDialog.treeWidget.clear()
itemList = []
for i in range(len(result["PROJECT"])):
myItem = QTreeWidgetItem()
myItem.setText(0, str(result["REVISION"][i]))
myItem.setText(1, str(result["DATUM"][i]))
myItem.setText(2, str(result["PROJECT"][i]))
myItem.setText(3, str(result["LOGMSG"][i]))
itemList.append(myItem)
self.LogViewDialog.treeWidget.addTopLevelItems(itemList)
self.LogViewDialog.show()
myDb.close()
canvas.refresh()
else:
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
myDb = self.tools.layerDB('logview', theLayer)
mySchema = QgsDataSourceUri(uri).schema()
myTable = QgsDataSourceUri(uri).table()
if len(mySchema) == 0:
mySchema = 'public'
sql = "select * from versions.pgvslogview('" + mySchema + "." + myTable.replace(
'_version', '') + "') order by revision desc"
result, error = myDb.read(sql)
self.LogViewDialog.setLayer(theLayer)
self.LogViewDialog.createTagList()
self.LogViewDialog.treeWidget.clear()
itemList = []
for i in range(len(result["PROJECT"])):
myItem = QTreeWidgetItem()
myItem.setText(0, str(result["REVISION"][i]))
myItem.setText(1, str(result["DATUM"][i]))
myItem.setText(2, str(result["PROJECT"][i]))
myItem.setText(3, str(result["LOGMSG"][i]))
itemList.append(myItem)
self.LogViewDialog.treeWidget.addTopLevelItems(itemList)
self.LogViewDialog.show()
myDb.close()
canvas.refresh()
def doDiff(self):
currentLayer = self.iface.activeLayer()
if currentLayer is None or currentLayer.dataProvider().name() == "memory":
self.iface.messageBar().pushMessage(
'Warning',
self.tr('Please select a versioned layer.'),
level=Qgis.MessageLevel(1), duration=3)
return
if not self.tools.isModified(currentLayer):
self.iface.messageBar().pushMessage(
"Warning",
self.tr(
'The selected Layer is at the HEAD Revision.'),
level=Qgis.MessageLevel(1), duration=3)
return
QApplication.setOverrideCursor(Qt.WaitCursor)
geomCol = self.tools.layerGeomCol(currentLayer)
geometryType = self.tools.layerGeometryType(currentLayer)
mySchema = self.tools.layerSchema(currentLayer)
myTable = self.tools.layerTable(currentLayer)
if len(mySchema) == 0:
mySchema = 'public'
extent = self.iface.mapCanvas().extent().toString().replace(
':', ', ')
authority, crs = currentLayer.crs().authid().split(':')
# geo_idx = '%s && ST_MakeEnvelope(%s,%s)' % (geomCol, extent, crs)
with open('{0}/sql/diff.sql'.format(os.path.dirname(os.path.abspath(__file__))), 'r') as sql_file:
sql = sql_file.read().format(
schema=mySchema,
table=myTable,
origin=myTable.replace(
'_version', ''))
# , geo_idx=geo_idx)
myUri = QgsDataSourceUri(self.tools.layerUri(currentLayer))
myUri.setDataSource("", u"(%s\n)" % sql, geomCol, "", "rownum")
myUri.setSrid(crs)
layer_name = myTable + " (Diff to HEAD Revision)"
layer = QgsVectorLayer(myUri.uri(), layer_name, "postgres")
mem_layer = self.tools.create_memory_layer(layer, layer_name)
if mem_layer != None:
if not mem_layer.isValid():
self.iface.messageBar().pushMessage(
'Warning',
self.tr('No diffs to HEAD in current extend detected!'),
level=Qgis.MessageLevel(1), duration=3)
else:
legends_path = os.path.dirname(__file__)
if geometryType == 0:
mem_layer.loadNamedStyle(
legends_path + "/legends/diff_point.qml")
elif geometryType == 1:
mem_layer.loadNamedStyle(
legends_path + "/legends/diff_linestring.qml")
elif geometryType == 2:
mem_layer.loadNamedStyle(
legends_path + "/legends/diff_polygon.qml")
QgsProject().instance().addMapLayer(mem_layer)
self.iface.messageBar().pushMessage(
'Info',
self.tr('Diff to HEAD revision was successful!'),
level=Qgis.MessageLevel(0), duration=3)
QApplication.restoreOverrideCursor()
self.LogViewDialog.close()
return
def doUpdate(self):
pass
def doDrop(self):
theLayer = self.iface.activeLayer()
provider = theLayer.dataProvider()
uri = provider.dataSourceUri()
mySchema = QgsDataSourceUri(uri).schema()
if len(mySchema) == 0:
mySchema = 'public'
myTable = QgsDataSourceUri(uri).table()
if theLayer is None:
QMessageBox.information(None, '', self.tr(
'Please select a layer for versioning'))
return
else:
answer = QMessageBox.question(
None, '',
self.tr('Are you sure to to drop pgvs from the table '
'{0}?').format(mySchema + "." + myTable.replace(
'_version', '')),
QMessageBox.Yes | QMessageBox.Cancel)
if answer == QMessageBox.Yes:
if self.tools.isModified(theLayer):
QMessageBox.warning(
None,
self.tr('Warning'),
self.tr('Layer %s has uncommited changes, please'
'commit them or revert to HEAD revision' % (
theLayer.name())))
else:
myDb = self.tools.layerDB('doDrop', theLayer)
sql = """select versions.pgvsdrop('{0}.{1}')""".format(
mySchema, myTable.replace('_version', ''))
myDb.read(sql)
layer_name = theLayer.name()
QgsProject().instance().removeMapLayer(
theLayer.id())
self.iface.messageBar().pushMessage(
'Info',
self.tr('Versioning for layer {0} dropped!').format(
layer_name), level=Qgis.MessageLevel(0),
duration=3)
self.iface.mapCanvas().refreshAllLayers()
def doIncrementalUpdate(self):
if len(self.iface.layerTreeView().selectedLayers()) > 0:
theLayer = self.iface.layerTreeView().selectedLayers()[0]
if self.tools.hasVersion(theLayer):
self.upgradeDlg = IncrementalLayerUpdateDialog(self)
self.upgradeDlg.show()
else:
QMessageBox.information(None, self.tr('Error'), self.tr('Please select a versioned layer to upgrade'))
else:
QMessageBox.information(None, self.tr('Error'), self.tr('Please select a versioned layer to upgrade'))
#
def doHelp(self):
helpUrl = QUrl()
if self.locale != 'de':
self.locale = 'en'
absolut_path_string = '%s/docs/help/dokumentation_pgversion_QGIS3_%s.html' % (os.path.dirname(__file__), self.locale)
helpUrl.setUrl(QUrl.fromLocalFile(absolut_path_string).toString())
self.helpDialog.webView.load(helpUrl)
self.helpDialog.show()
def doAbout(self):
self.about = DlgAbout(self.plugin_path)
self.about.show()