-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
297 lines (219 loc) · 7.07 KB
/
utils.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
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.gui import QgsMessageBar
import logging
import os.path
#
# Utility funcions
#
def setGlobals(p_dialog, p_iface):
global _dialog, _iface, MSG_DURATION
_dialog = p_dialog
_iface = p_iface
MSG_DURATION = 5
def setDialog(p_dialog):
global _dialog
_dialog = p_dialog
def setInterface(p_iface):
global _iface, MSG_DURATION
_iface = p_iface
MSG_DURATION = 5
def isFirstTime():
global first
if not 'first' in globals():
first = True
else:
first = False
return first
def setCursor(p_cursor):
global cursor
cursor = p_cursor
def fillComboBox(widget, sql):
elem = _dialog.findChild(QComboBox, widget)
elem.clear()
elem.addItem("")
print sql
if not 'cursor' in globals():
print "cursor error"
return
else:
print "defined"
return
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
elem.addItem(row[0])
def setComboModel(widget, vector):
completer = QCompleter()
completer.setCompletionColumn(0)
completer.setMaxVisibleItems(10)
completer.setCaseSensitivity(Qt.CaseInsensitive)
model = QStringListModel()
model.setStringList(vector)
completer.setModel(model)
widget.setModel(model)
widget.setCompleter(completer)
def sqlToList(sql):
vector = []
vector.append('')
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
vector.append(unicode(row[0]))
return vector
def setSelectedItem(widget, sql):
cursor.execute(sql)
row = cursor.fetchone()
if not row:
return
elem = _dialog.findChild(QComboBox, widget)
if elem:
index = elem.findText(row[0])
elem.setCurrentIndex(index);
def getSelectedItem(widget):
elem = _dialog.findChild(QComboBox, widget)
if not elem.currentText():
elem_text = "null"
else:
elem_text = "'"+elem.currentText()+"'"
elem_text = widget+"="+elem_text
return elem_text
def getSelectedItem2(widget):
elem = _dialog.findChild(QComboBox, widget)
if not elem.currentText():
elem_text = "null"
else:
elem_text = "'"+elem.currentText().replace("'", "''")+"'"
return elem_text
def getValue(widget):
elem = _dialog.findChild(QLineEdit, widget)
if elem:
if elem.text():
elem_text = widget + " = "+elem.text().replace(",", ".")
else:
elem_text = widget + " = null"
else:
elem_text = widget + " = null"
return elem_text
def getFloat(widgetName):
widget = _dialog.findChild(QLineEdit, widgetName)
value = 0.0
if widget:
if widget.text():
value = float(widget.text().replace(",", "."))
return value
def getStringValue(widget):
elem = _dialog.findChild(QLineEdit, widget)
if elem:
if (not elem.text() or elem.text().lower() == "null"):
elem_text = widget + " = null"
else:
elem_text = widget + " = '"+elem.text().replace("'", "''")+"'"
else:
elem = _dialog.findChild(QTextEdit, widget)
if elem:
if (not elem.toPlainText() or elem.toPlainText().lower() == "null"):
elem_text = widget + " = null"
else:
elem_text = widget + " = '"+elem.toPlainText().replace("'", "''")+"'"
else:
elem_text = widget + " = null"
return elem_text
def getStringValue2(widget):
elem = _dialog.findChild(QLineEdit, widget)
if elem:
if (not elem.text() or elem.text().lower() == "null"):
elem_text = "null"
else:
elem_text = "'"+elem.text().replace("'", "''")+"'"
else:
elem = _dialog.findChild(QTextEdit, widget)
if elem:
if (not elem.toPlainText() or elem.toPlainText().lower() == "null"):
elem_text = "null"
else:
elem_text = "'"+elem.toPlainText().replace("'", "''")+"'"
else:
elem_text = "null"
return elem_text
def getDate(widget, fieldName):
try:
date_widget = _dialog.findChild(QDateEdit, widget)
if not date_widget:
print "not found"
return
dateAux = date_widget.date()
aux = dateAux.toString("yyyy-MM-dd")
if aux:
value = aux
text = fieldName+"='"+value+"'"
else:
value = "null"
text = fieldName+"=null"
except ValueError:
value = "null"
text = ""
return dict(value = value, text = text)
def isNull(widget):
elem = _dialog.findChild(QLineEdit, widget)
empty = True
if elem:
if elem.text():
empty = False
return empty
def isNumber(elem):
try:
float(elem)
return True
except ValueError:
return False
def isChecked(widgetName):
widget = _dialog.findChild(QCheckBox, widgetName)
value = False
if widget:
value = widget.isChecked()
return str(value)
def setText(widget, text):
elem = _dialog.findChild(QLineEdit, widget)
if elem:
elem.setText(str(text))
else:
print "setText"
def showInfo(text, duration = None):
if duration is None:
_iface.messageBar().pushMessage("", text, QgsMessageBar.INFO, MSG_DURATION)
else:
_iface.messageBar().pushMessage("", text, QgsMessageBar.INFO, duration)
def showWarning(text, duration = None):
if duration is None:
_iface.messageBar().pushMessage("", text, QgsMessageBar.WARNING, MSG_DURATION)
else:
_iface.messageBar().pushMessage("", text, QgsMessageBar.WARNING, duration)
def setLogger(name, folder, filename):
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
# create file handler
if not os.path.exists(folder):
os.makedirs(folder)
filepath = folder + "/" + filename
fh = logging.FileHandler(filepath)
fh.setLevel(logging.INFO)
# create console handler
#ch = logging.StreamHandler()
#ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter_fh = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#formatter_ch = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter_fh)
#ch.setFormatter(formatter_ch)
# add the handlers to logger
if (len(logger.handlers) > 0):
removeHandlers(logger)
logger.addHandler(fh)
#logger.addHandler(ch)
return logger
def removeHandlers(logger):
#logger.info('Total: %d'%len(logger.handlers))
for h in logger.handlers:
#logger.info('removing handler %s'%str(h))
logger.removeHandler(h)