forked from NathanW2/qgiscommand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgiscommand.py
422 lines (341 loc) · 13.1 KB
/
qgiscommand.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
import sys
import os
from string import Template
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.Qsci import QsciScintilla, QsciLexerPython
import command
import qgis_commands
import layer_commands
import package_commands
_start_prompt = "> "
_ui_settings = {
"Background": '#36454f',
"Font_Color": 'white',
"Header_Background": '#5c8f0f'
}
class Notify(QObject):
settingChanged = pyqtSignal()
notify = Notify()
class SourceViewer(QsciScintilla):
def __init__(self, syntax='Python', parent=None):
super(SourceViewer, self).__init__(parent)
if syntax == 'Python':
self.lex = QsciLexerPython(self)
self.setLexer(self.lex)
self.setReadOnly(True)
def setText(self, text, line=0):
super(SourceViewer, self).setText(text)
self.setFirstVisibleLine(line)
def show_viewer(text, lineno=0, title='', syntax='Python'):
dlg = QDialog()
dlg.setWindowTitle(title)
dlg.setLayout(QGridLayout())
dlg.layout().setContentsMargins(0, 0, 0, 0)
view = SourceViewer(syntax)
dlg.layout().addWidget(view)
view.setText(text, lineno)
view.resize(500, 500)
dlg.exec_()
@command.command()
def help():
"""
Show home page for the command bar
"""
QDesktopServices.openUrl(QUrl("http://qgiscommand.readthedocs.org/en/latest/"))
@command.command("Prompt?")
def set_commandbar_prompt(char):
global _start_prompt
_start_prompt = "{} ".format(char)
@command.complete_name("UI Items")
def list_ui_items(argname, data):
return _ui_settings.keys()
@command.command("Item?", "Hex color for background?")
@command.complete_with(item=list_ui_items)
def set_commandbar_color(item, color):
_ui_settings[item] = color
notify.settingChanged.emit()
@command.command("Command name")
@command.check(name=command.is_comamnd)
@command.complete_with(name=command.commandlist)
def command_help(name):
"""
Lookup the help for a given command
"""
helptext = command.help_text[name]
show_viewer(helptext, title="Help for {}".format(name), syntax='')
@command.command("Command name")
@command.check(name=command.is_comamnd)
@command.complete_with(name=command.commandlist)
def view_source(name):
"""
Lookup the source code for a given command
"""
func = command.commands[name]
filename, lineno = command.sourcelookup[name]
with open(filename, "r") as f:
source = f.read()
show_viewer(source, lineno, filename)
class CompletionModel(QStandardItemModel):
def __init__(self, items=None, parent=None):
super(CompletionModel, self).__init__(parent)
self.filtermodel = QSortFilterProxyModel()
self.filtermodel.setSourceModel(self)
if not items:
items = []
self.add_entries(items)
def add_entry(self, data, helptext):
data = u"{}".format(unicode(data))
item = QStandardItem(data)
item.setData(helptext, Qt.UserRole + 1)
self.appendRow(item)
def add_entries(self, entries):
for entry in entries:
if isinstance(entry, tuple):
data, helptext = entry[0], entry[1]
else:
data, helptext = entry, ''
self.add_entry(data, helptext)
def set_filter(self, userdata):
fuzzy = "".join(["{}.*".format(c) for c in userdata])
self.filtermodel.setFilterRegExp(fuzzy)
index = self.filtermodel.index(0, 0)
return index
@property
def filtered_item_count(self):
return self.filtermodel.rowCount()
def filtered_item_data(self, index):
if index.isValid():
return self.filtermodel.data(index)
else:
return None
class CompletionView(QWidget):
completeRequest = pyqtSignal()
def __init__(self, mainwindow, completions=None, parent=None):
super(CompletionView, self).__init__(parent)
self.mainwindow = mainwindow
self.autocompletemodel = CompletionModel(completions)
self.autocompleteview = QListView(self)
self.autocompleteview.setModel(self.autocompletemodel.filtermodel)
self.setWindowFlags(Qt.Popup)
self.setFocusPolicy(Qt.NoFocus)
self.setFocusProxy(self)
self.autocompleteview.setMouseTracking(True)
self.autocompleteview.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.autocompleteview.installEventFilter(self)
self.selectionmodel = self.autocompleteview.selectionModel()
layout = QVBoxLayout()
self.setLayout(layout)
self.headerlabel = QLabel(self)
self.headerlabel.setText("Commands")
self.layout().setSpacing(0)
self.layout().setContentsMargins(0,0,0,0)
self.layout().addWidget(self.headerlabel)
self.layout().addWidget(self.autocompleteview)
notify.settingChanged.connect(self.update_ui)
self.styletemplate = Template("""
QLabel { font: bold 12pt; color: ${Font_Color} ; background-color: ${Header_Background} }
QListView:item:selected { color: #36454f; background: #7abd14 }
QListView:item { color: ${Font_Color} }
QListView {background-color: $Background; border: none; }""")
self.update_ui()
def update_ui(self):
style = self.styletemplate.safe_substitute(_ui_settings)
self.setStyleSheet(style)
def move_down(self):
index = self.autocompleteview.currentIndex()
row = index.row() + 1
newindex = self.autocompletemodel.filtermodel.index(row, 0)
if newindex.isValid():
self.autocompleteview.setCurrentIndex(newindex)
def move_up(self):
index = self.autocompleteview.currentIndex()
row = index.row() - 1
newindex = self.autocompletemodel.filtermodel.index(row, 0)
if newindex.isValid():
self.autocompleteview.setCurrentIndex(newindex)
def eventFilter(self, obj, event):
# WAT!? If this isn't here then the plugin crashes on unload
if not QEvent:
return False
if event.type() == QEvent.MouseButtonPress:
self.parent().setFocus()
return True
if event.type() == QEvent.KeyPress:
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_N:
self.move_down()
if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_P:
self.move_up()
if event.key() in [Qt.Key_Tab, Qt.Key_Enter, Qt.Key_Return]:
self.completeRequest.emit()
self.hide()
self.parent().setFocus()
return True
if event.key() in [Qt.Key_Up, Qt.Key_Down]:
return False
else:
self.parent().setFocus()
self.parent().event(event)
return False
def filter_autocomplete(self, userdata):
index = self.autocompletemodel.set_filter(userdata)
if index.isValid():
self.selectionmodel.clear()
self.selectionmodel.select(index, QItemSelectionModel.SelectCurrent)
self.autocompleteview.scrollTo(index,
QAbstractItemView.EnsureVisible)
self.show_completion()
def add_completions(self, completions, header):
self.headerlabel.setText(header)
self.autocompletemodel.clear()
self.autocompletemodel.add_entries(completions)
def show_completion(self):
hasdata = self.autocompletemodel.filtered_item_count > 0
size = self.mainwindow.height() / 100 * 15
rowsize = self.autocompleteview.sizeHintForRow(0)
newheight = self.autocompletemodel.filtered_item_count * rowsize
if newheight < size:
size = newheight + rowsize
self.resize(self.parent().width(), size)
self.move(self.parent().mapToGlobal(QPoint(0, 0 - self.height())))
self.autocompleteview.setFocus()
self.setVisible(hasdata)
@property
def selected_completion(self):
try:
index = self.autocompleteview.selectedIndexes()[0]
except IndexError:
return None
text = self.autocompletemodel.filtered_item_data(index)
return text
class CommandShell(QLineEdit):
def __init__(self, mainwindow, parent=None):
super(CommandShell, self).__init__(parent)
self.mainwindow = mainwindow
self.settings = QSettings()
self.prompt = _start_prompt
self.currentfunction = None
self.textChanged.connect(self.text_changed)
self.autocompleteview = CompletionView(self.mainwindow, parent=self)
self.autocompleteview.completeRequest.connect(self.complete)
self.show_prompt()
notify.settingChanged.connect(self.update_ui)
self.styletemplate = Template("""
QLineEdit { border: none; background: $Background; color: ${Font_Color} }""")
self.update_ui()
def update_ui(self):
style = self.styletemplate.safe_substitute(_ui_settings)
self.setStyleSheet(style)
def text_changed(self):
userdata = self.get_data()
if not self.currentfunction:
completions, userdata, header = command.completions_for_line(self.get_data())
self.autocompleteview.add_completions(completions, header)
self.autocompleteview.filter_autocomplete(userdata)
def finsihed(self):
self.parent().removeEventFilter(self)
def event(self, event):
if not QEvent:
return False
if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
self.complete()
return True
return super(CommandShell, self).event(event)
def keyPressEvent(self, e):
if e.key() in (Qt.Key_Return, Qt.Key_Enter):
self.entered()
elif e.key() == Qt.Key_Escape:
self.end_current()
elif e.key() in (Qt.Key_Backspace, Qt.Key_Delete):
newindex = self.cursorPosition()
if newindex > len(self.prompt):
super(CommandShell, self).keyPressEvent(e)
else:
super(CommandShell, self).keyPressEvent(e)
def complete(self):
text = self.autocompleteview.selected_completion
if not text:
return False
# Wrap text with space in quotes
if ' ' in text and not self.currentfunction:
# TODO Handle escaping better
text = "'{}'".format(text)
line = self.get_data()
space = line.rfind(' ')
newline = line[:space + 1] + text + " "
self.show_prompt(self.prompt, newline)
return True
def end_current(self):
self.currentfunction = None
self.show_prompt()
def show_prompt(self, prompt=None, data=None):
self.clear()
if not prompt:
prompt = _start_prompt
if not prompt == _start_prompt:
prompt += ":"
text = prompt
if data:
text = prompt + str(data)
self.setText(text)
self.prompt = prompt
self.move_cursor_to_end()
def get_end_pos(self):
"""Return (line, index) position of the last character"""
return len(self.text())
def move_cursor_to_end(self):
"""Move cursor to end of text"""
self.end(False)
def get_data(self):
line = self.text()
line = line[len(self.prompt):]
return line
def run_line(self, line):
if not line:
return
if not self.currentfunction:
try:
gen = command.parse_line_data(line)
except command.NoFunction:
return
if gen:
self.currentfunction = gen
line = None
else:
self.currentfunction = None
self.show_prompt()
return
try:
prompt, data, completions = self.currentfunction.send(line)
completions, header = completions[0], completions[1]
self.show_prompt(prompt, data)
self.autocompleteview.add_completions(completions, header)
self.autocompleteview.filter_autocomplete("")
except StopIteration:
self.currentfunction = None
self.show_prompt()
except command.NoFunction:
self.currentfunction = None
def show_completion(self):
self.autocompleteview.show_completion()
def entered(self):
line = self.get_data()
self.run_line(line)
def set_settings(self):
loadFont = self.settings.value("pythonConsole/fontfamilytext",
"Monospace")
fontSize = self.settings.value("pythonConsole/fontsize", 10, type=int)
font = QFont(loadFont)
font.setFixedPitch(True)
font.setPointSize(fontSize)
font.setStyleHint(QFont.TypeWriter)
font.setStretch(QFont.SemiCondensed)
font.setLetterSpacing(QFont.PercentageSpacing, 87.0)
font.setBold(False)
self.setFont(font)
def activated(self):
visible = self.isVisible()
self.setVisible(not visible)
if __name__ == "__main__":
c = CommandShell(iface.mapCanvas())
c.activated()