-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistorize.py
195 lines (164 loc) · 7.79 KB
/
historize.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
"""
/***************************************************************************
QGIS Historize Plugin
-------------------------------------------------------------------
Date : 09 Mai 2017
Copyright : (C) 2017 by William Habelt
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.core import QgsFeature, QgsMapLayerRegistry
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import os
from importUpdateDialog import ImportUpdateDialog
from selectDateDialog import SelectDateDialog
from aboutDialog import AboutDialog
from dbconn import DBConn
from sqlexecute import SQLExecute
class Historize:
"""This class handles the initialization and calls of the menus"""
def __init__(self, iface):
self.iface = iface
self.dbconn = DBConn(iface)
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'Historize_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
def tr(self, message):
return QCoreApplication.translate('Historize', message)
def initGui(self):
self.menu = QMenu()
self.menu.setTitle("Historize")
self.lyrMenu = QMenu()
self.lyrMenu.setTitle("Layer")
# Create menu actions
self.actionInit = QAction(self.tr(u"Initialize Database"), self.iface.mainWindow())
self.actionLyrInit = QAction(self.tr(u"Initialize Layer"), self.iface.mainWindow())
self.actionLyrUpdate = QAction(self.tr(u"Update Layer"), self.iface.mainWindow())
self.actionLyrLoad = QAction(self.tr(u"Load Layer"), self.iface.mainWindow())
self.actionAbout = QAction(self.tr(u"About"), self.iface.mainWindow())
# Connect menu actions
self.actionInit.triggered.connect(self.doInit)
self.actionLyrInit.triggered.connect(self.doLyrInit)
self.actionLyrLoad.triggered.connect(self.doLyrLoad)
self.actionLyrUpdate.triggered.connect(self.doLyrUpdate)
self.actionAbout.triggered.connect(self.doAbout)
QObject.connect(self.iface.mapCanvas(), SIGNAL("currentLayerChanged(QgsMapLayer *)"), self.setMenuOptions)
# Add actions to menu
self.lyrMenu.addActions([self.actionLyrInit, self.actionLyrLoad, self.actionLyrUpdate])
self.menu.addAction(self.actionInit)
self.menu.addMenu(self.lyrMenu)
self.menu.addAction(self.actionAbout)
self.menu.insertSeparator(self.actionAbout)
menuBar = self.iface.mainWindow().menuBar()
menuBar.addMenu(self.menu)
# Disable unusable actions
self.actionInit.setEnabled(False)
self.actionLyrInit.setEnabled(False)
self.actionLyrUpdate.setEnabled(False)
self.actionLyrLoad.setEnabled(False)
def unload(self):
self.menu.deleteLater()
def doInit(self):
"""Use Database info from layer and run historisation.sql on it."""
selectedLayer = self.iface.activeLayer()
provider = selectedLayer.dataProvider()
if provider.name() != 'postgres':
QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Invalid Layer"), self.tr(u"Layer must be provided by postgres!"))
return
uri = QgsDataSourceURI(provider.dataSourceUri())
conn = self.dbconn.connectToDb(uri)
cur = conn.cursor()
if conn is False:
return
result = QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Initialize Historisation"), self.tr(u"Initialize historisation on this layers database?"), QMessageBox.No | QMessageBox.Yes)
if result == QMessageBox.Yes:
sqlPath = os.path.dirname(os.path.realpath(__file__)) + '/sql/historisierung.sql'
fd = open(sqlPath, 'r')
sqlFile = fd.read()
fd.close()
try:
# Ignore first three characters which invalidate the SQL command
cur.execute(sqlFile[3:])
conn.commit()
QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Success"), self.tr(u"Database initialized successfully!"))
except:
conn.rollback()
QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Error"), self.tr(u"Unable to initialize database!"))
conn.close()
else:
return
def doLyrInit(self):
"""Use Layer info and run init() .sql query"""
selectedLayer = self.iface.activeLayer()
provider = selectedLayer.dataProvider()
uri = QgsDataSourceURI(provider.dataSourceUri())
conn = self.dbconn.connectToDb(uri)
cur = conn.cursor()
if conn is False:
return
result = QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Initialize Layer"), self.tr(u"Are you sure you wish to proceed?"), QMessageBox.No | QMessageBox.Yes)
if result == QMessageBox.Yes:
# Get SQL vars
hasGeometry = selectedLayer.hasGeometryType()
schema = uri.schema()
table = uri.table()
self.execute = SQLExecute(conn, selectedLayer)
success = self.execute.histTabsInit(hasGeometry, schema, table)
if success:
QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Success"), self.tr(u"Layer successfully initialized!"))
else:
QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Error"), self.tr(u"Initialization failed!"))
else:
return
def doLyrUpdate(self):
"""Open ImportUpdate dialog"""
self.updateDialog = ImportUpdateDialog(self.iface)
self.updateDialog.show()
def doLyrLoad(self):
"""Open selectDate dialog"""
self.dateDialog = SelectDateDialog(self.iface)
self.dateDialog.show()
def doAbout(self):
"""Show About dialog"""
self.aboutDialog = AboutDialog()
self.aboutDialog.show()
def setMenuOptions(self, layer):
"""Enable/Disable menu options based on selected layer"""
self.actionLyrInit.setEnabled(False)
self.actionLyrUpdate.setEnabled(False)
self.actionLyrLoad.setEnabled(False)
self.actionInit.setEnabled(False)
selectedLayer = self.iface.activeLayer()
if selectedLayer:
provider = layer.dataProvider()
if provider.name() == "postgres":
self.actionInit.setEnabled(True)
uri = QgsDataSourceURI(provider.dataSourceUri())
conn = self.dbconn.connectToDb(uri)
cur = conn.cursor()
self.execute = SQLExecute(conn)
result = self.execute.checkIfHistorised(uri.schema(), self.iface.activeLayer().name())
if result:
self.actionLyrUpdate.setEnabled(True)
self.actionLyrLoad.setEnabled(True)
else:
self.actionLyrInit.setEnabled(True)