-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfkTextBrowser.py
309 lines (246 loc) · 10.1 KB
/
vfkTextBrowser.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
vfkPluginDialog
A QGIS plugin
Plugin umoznujici praci s daty katastru nemovitosti
-------------------
begin : 2015-06-11
git sha : $Format:%H$
copyright : (C) 2015 by Stepan Bambula
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 PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import QFile, QIODevice, QUrl, QObject, SIGNAL, SLOT, pyqtSlot, pyqtSignal, QTextStream, qWarning, qDebug
from PyQt4.QtSql import QSqlDatabase
from documentBuilder import *
from htmlDocument import *
from latexDocument import *
from richTextDocument import *
class TPair:
def __init__(self, first=u'', second=u''):
self.first = first
self.second = second
class HistoryRecord(QObject):
def __init__(self):
QObject.__init__(self)
self.html = u''
self.parIds = []
self.budIds = []
self.definitionPoint = Coordinates()
class VfkTextBrowser(QTextBrowser):
class ExportFormat(object):
Html = 0
RichText = 1
Latex = 2
# signals
updateHistory = pyqtSignal(QObject)
showParcely = pyqtSignal(QObject)
showBudovy = pyqtSignal(QObject)
currentParIdsChanged = pyqtSignal(bool)
currentBudIdsChanged = pyqtSignal(bool)
historyBefore = pyqtSignal(bool)
historyAfter = pyqtSignal(bool)
definitionPointAvailable = pyqtSignal(bool)
switchToPanelImport = pyqtSignal()
switchToPanelSearch = pyqtSignal(int)
switchToPanelChanges = pyqtSignal()
def __init__(self, parent=None):
"""
Init function
"""
super(VfkTextBrowser, self).__init__(parent)
self.__mCurrentUrl = QUrl()
self.__mCurrentRecord = HistoryRecord()
self.__mDocumentBuilder = DocumentBuilder()
self.__mUrlHistory = [] # list of history records
self.__mHistoryOrder = -1 # saving current index in history list
self.connect(self, SIGNAL("anchorClicked(QUrl)"), self.onLinkClicked)
self.connect(self, SIGNAL("updateHistory"), self.saveHistory)
self.emit(SIGNAL("currentParIdsChanged"), False)
self.emit(SIGNAL("currentBudIdsChanged"), False)
self.emit(SIGNAL("historyBefore"), False)
self.emit(SIGNAL("historyAfter"), False)
def currentUrl(self):
return self.__mCurrentUrl
def currentParIds(self):
return self.__mCurrentRecord.parIds
def currentBudIds(self):
return self.__mCurrentRecord.budIds
def currentDefinitionPoint(self):
return self.__mCurrentRecord.definitionPoint
def startPage(self):
self.processAction(QUrl("showText?page=allTEL"))
def exportDocument(self, task, fileName, format):
"""
:type task: QUrl
:type fileName: str
:type format: self.ExportFormat
:return: bool
"""
fileOut = QFile(fileName)
if not fileOut.open(QIODevice.WriteOnly | QIODevice.Text):
return False
taskMap = self.__parseTask(task)
text = self.__documentContent(taskMap, format)
streamFileOut = QTextStream(fileOut)
streamFileOut.setCodec("UTF-8")
streamFileOut << text
streamFileOut.flush()
fileOut.close()
return True
def setConnectionName(self, connectionName):
"""
:type connectionName: str
"""
self.__mDocumentBuilder = DocumentBuilder(connectionName)
def __parseTask(self, task):
"""
:type task: QUrl
:return: dict
"""
taskMap = {u'action': task.path()}
for key, value in task.encodedQueryItems():
taskMap[unicode(key)] = QUrl.fromPercentEncoding(unicode(value))
return taskMap
def goBack(self):
if len(self.__mUrlHistory) > 1 and len(self.__mUrlHistory) - self.__mHistoryOrder > 0 and self.__mHistoryOrder > 0:
self.__mCurrentRecord = self.__mUrlHistory[
self.__mHistoryOrder - 1]
self.__mHistoryOrder -= 1 if self.__mHistoryOrder > 0 else self.__mHistoryOrder == 0
self.setHtml(self.__mCurrentRecord.html)
self.updateButtonEnabledState()
def goForth(self):
if len(self.__mUrlHistory) > 1 and len(self.__mUrlHistory) - self.__mHistoryOrder > 1 and self.__mHistoryOrder >= 0:
self.__mCurrentRecord = self.__mUrlHistory[
self.__mHistoryOrder + 1]
self.__mHistoryOrder += 1
self.setHtml(self.__mCurrentRecord.html)
self.updateButtonEnabledState()
def saveHistory(self, record):
if len(self.__mUrlHistory) == 0:
self.__mUrlHistory.append(record)
self.__mHistoryOrder = 0
else:
self.__mUrlHistory.append(record)
self.__mHistoryOrder += 1
self.__mCurrentRecord = self.__mUrlHistory[self.__mHistoryOrder]
self.updateButtonEnabledState()
def showHelpPage(self):
url = "showText?page=help"
self.processAction(QUrl(url))
def showInfoAboutSelection(self, parIds, budIds):
"""
:type parIds: list
:type budIds: list
:return:
"""
url = u''
if len(parIds) + len(budIds) == 1:
if len(parIds) == 1:
url = u"showText?page=par&id={}".format(parIds[0])
else:
url = u"showText?page=bud&id={}".format(budIds[0])
self.processAction(QUrl(url))
return
urlPart = u''
if parIds:
urlPart = u"&parcely={}".format(u",".join(parIds))
if budIds:
urlPart = u"&budovy={}".format(u",".join(budIds))
if urlPart:
url = u"showText?page=seznam&type=id{}".format(urlPart)
self.processAction(QUrl(url))
def postInit(self):
self.emit(SIGNAL("currentParIdsChanged"), False)
self.emit(SIGNAL("currentBudIdsChanged"), False)
self.emit(SIGNAL("historyBefore"), False)
self.emit(SIGNAL("historyAfter"), False)
self.emit(SIGNAL("definitionPointAvailable"), False)
def documentFactory(self, format):
"""
:type format: VfkTextBrowser.ExportFormat
:rtype: VfkDocument
"""
doc = VfkDocument
if format == VfkTextBrowser.ExportFormat.Latex:
doc = LatexDocument()
return doc
elif format == VfkTextBrowser.ExportFormat.Html:
doc = HtmlDocument()
return doc
elif format == VfkTextBrowser.ExportFormat.RichText:
doc = RichTextDocument()
return doc
else:
qDebug("Nejsou podporovany jine formaty pro export")
def updateButtonEnabledState(self):
self.emit(SIGNAL("currentParIdsChanged"),
True if self.__mCurrentRecord.parIds else False)
self.emit(SIGNAL("currentBudIdsChanged"),
True if self.__mCurrentRecord.budIds else False)
self.emit(SIGNAL("historyBefore"), self.__mHistoryOrder > 0)
self.emit(SIGNAL("historyAfter"), len(
self.__mUrlHistory) - self.__mHistoryOrder > 1)
self.emit(SIGNAL("definitionPointAvailable"), True if (self.__mCurrentRecord.definitionPoint.first
and self.__mCurrentRecord.definitionPoint.second) else False)
def onLinkClicked(self, task):
"""
:type task: QUrl
"""
self.processAction(task)
def processAction(self, task):
"""
:type task: QUrl
"""
self.__mCurrentUrl = task
taskMap = self.__parseTask(task)
if taskMap[u"action"] == u"showText":
QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
t = QtCore.QTime()
t.start()
html = self.__documentContent(taskMap, self.ExportFormat.RichText)
qDebug("Total time elapsed: {} ms".format(t.elapsed()))
QApplication.restoreOverrideCursor()
self.setHtml(html)
record = HistoryRecord()
record.html = html
record.parIds = self.__mDocumentBuilder.currentParIds()
record.budIds = self.__mDocumentBuilder.currentBudIds()
record.definitionPoint = self.__mDocumentBuilder.currentDefinitionPoint(
)
self.emit(SIGNAL("updateHistory"), record)
elif taskMap[u"action"] == u"selectInMap":
self.emit(SIGNAL("showParcely"), taskMap[u'ids'].split(u','))
elif taskMap[u"action"] == u"switchPanel":
if taskMap[u"panel"] == u"import":
self.emit(SIGNAL("switchToPanelImport"))
elif taskMap[u"panel"] == u"search":
self.emit(SIGNAL("switchToPanelSearch"), int(taskMap[u'type']))
elif taskMap[u"panel"] == u"changes":
self.emit(SIGNAL("switchToPanelChanges"))
self.setHtml(self.__mCurrentRecord.html)
else:
qDebug("..Jina akce")
def __documentContent(self, taskMap, format):
"""
:type taskMap: dict
:type format: VfkTextBrowser.ExportFormat
:return:
"""
doc = self.documentFactory(format)
if not doc:
return u''
self.__mDocumentBuilder.buildHtml(doc, taskMap)
text = doc.toString()
return u'{}'.format(text)