forked from NathanW2/qgiscommand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgiscommand.py
367 lines (304 loc) · 11.4 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
import sys
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.Qsci import QsciScintilla, QsciLexerCustom, QsciAPIs, QsciLexerPython
import command
import qgis_commands
_start_prompt = "-> "
import logger
# logger.msg('Test message from qgiscommand.py')
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 help for the command bar
"""
# TODO Replace this with a html page for help
helptext = """Command Bar help
To get help on a command type: command-help
Current commands:
{commands}
""".format(commands="\n".join(command.commands.keys()))
name = "Command bar"
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 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 Lexer(QsciLexerCustom):
"""
Note: Crashes at the momemnt
"""
def __init__(self, parent=None):
QsciLexerCustom.__init__(self, parent)
self._styles = {0: 'Default', 1: 'Function', }
for key, value in self._styles.iteritems():
setattr(self, value, key)
def description(self, style):
return self._styles.get(style, 'Default')
def defaultColor(self, style):
if style == self.Default:
return QColor('#000000')
elif style == self.Function:
return QColor('#C0C0C0')
return QsciLexerCustom.defaultColor(self, style)
def styleText(self, start, end):
print start, end
class CommandShell(QsciScintilla):
def __init__(self, parent=None):
super(CommandShell, self).__init__(parent)
self.setMinimumHeight(50)
self.setMaximumHeight(50)
self.settings = QSettings()
self.lex = QsciLexerPython(self)
self.apis = QsciAPIs(self.lex)
self.lex.setAPIs(self.apis)
self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
self.SendScintilla(QsciScintilla.SCI_SETVSCROLLBAR, 0)
self.setFolding(0)
self._start_prompt = _start_prompt
self.prompt = self._start_prompt
self.currentfunction = None
self.setAutoCompletionSource(self.AcsAPIs)
self.setAutoCompletionThreshold(1)
self.setAutoCompletionReplaceWord(True)
self.setCallTipsStyle(QsciScintilla.CallTipsNoContext)
self.parent().installEventFilter(self)
self.textChanged.connect(self.text_changed)
self._lastcompletions = None
self.autocompletemodel = QStandardItemModel()
self.autocompletefilter = QSortFilterProxyModel()
self.autocompletefilter.setSourceModel(self.autocompletemodel)
self.autocompleteview = QListView(self.parent())
self.autocompleteview.setStyleSheet(
"QListView::item:selected {border: 1px solid black; }")
self.autocompleteview.setModel(self.autocompletefilter)
self.autocompleteview.hide()
self.selectionmodel = self.autocompleteview.selectionModel()
self.autocompleteview.setEditTriggers(QAbstractItemView.NoEditTriggers)
# command.register_command(self.run_last_command,
# alias="!!",
# nohistory=True)
self.setLexers()
def adjust_auto_complete(self):
self.autocompleteview.resize(self.parent().width(), 150)
self.autocompleteview.move(0, self.parent().height() - self.height() -
self.autocompleteview.height() + 2)
def add_completions(self, completions):
self.autocompletemodel.clear()
for value in completions:
data = u"{}".format(unicode(value))
self.autocompletemodel.appendRow(QStandardItem(data))
def filter_autocomplete(self, userdata, filteronly=False):
fuzzy = "".join(["{}.*".format(c) for c in userdata])
self.autocompletefilter.setFilterRegExp(fuzzy)
index = self.autocompletefilter.index(0, 0)
if index.isValid():
self.selectionmodel.clear()
self.selectionmodel.select(index, QItemSelectionModel.SelectCurrent)
self.autocompleteview.scrollTo(index,
QAbstractItemView.EnsureVisible)
hasdata = self.autocompletemodel.rowCount() > 0
self.adjust_auto_complete()
self.autocompleteview.setVisible(hasdata)
def text_changed(self):
userdata = self.get_data()
if not self.currentfunction:
completions, userdata = command.completions_for_line(self.get_data())
self.add_completions(completions)
self.filter_autocomplete(userdata)
def end(self):
self.parent().removeEventFilter(self)
self.close()
def eventFilter(self, object, event):
if event.type() == QEvent.Resize:
self.adjust_size()
return QWidget.eventFilter(self, object, event)
def move_autocomplete(self, amount):
try:
index = self.autocompleteview.selectedIndexes()[0]
except IndexError:
return
row = index.row()
row += amount
if row > self.autocompletefilter.rowCount() - 1:
# Wrap to top
row = 0
elif row < 0:
# Wrap to bottom
row = self.autocompletefilter.rowCount() - 1
index = self.autocompletefilter.index(row, 0)
if index.isValid():
self.selectionmodel.select(index, QItemSelectionModel.SelectCurrent)
self.autocompleteview.scrollTo(index,
QAbstractItemView.EnsureVisible)
def keyPressEvent(self, e):
if e.key() in (Qt.Key_Return, Qt.Key_Enter):
self.entered()
elif e.key() == Qt.Key_Escape:
self.close()
elif e.key() == Qt.Key_Tab:
self.complete()
elif e.key() in (Qt.Key_Backspace, Qt.Key_Delete):
_, newindex = self.getCursorPosition()
if newindex > len(self.prompt):
QsciScintilla.keyPressEvent(self, e)
elif e.key() == Qt.Key_Up:
self.move_autocomplete(-1)
elif e.key() == Qt.Key_Down:
self.move_autocomplete(1)
else:
QsciScintilla.keyPressEvent(self, e)
def complete(self):
try:
index = self.autocompleteview.selectedIndexes()[0]
except IndexError:
return
if index.isValid():
text = self.autocompletefilter.data(index)
# Wrap text with space in quotes
if ' ' in text:
text = "'{}'".format(text)
line = self.get_data()
space = line.rfind(' ')
newline = line[:space + 1] + text + " "
self.show_prompt(self.prompt, newline)
def close(self):
if self.currentfunction:
# If there is a current function we don't kill the bar just the
# current function
self.currentfunction = None
self.show_prompt()
return
self.currentfunction = None
self.autocompleteview.close()
super(CommandShell, self).close()
def show_prompt(self, prompt=_start_prompt, data=None):
self.clear()
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"""
line = self.lines() - 1
return (line, len(self.text(line)))
def move_cursor_to_end(self):
"""Move cursor to end of text"""
line, index = self.get_end_pos()
self.setCursorPosition(line, index)
self.ensureCursorVisible()
self.ensureLineVisible(line)
def get_data(self):
line = self.text()
line = line[len(self.prompt):]
return line
def run_last_command(self):
"""
Runs the last command again. (Alias -> !!)
"""
try:
line = command.history[-1]
self.run_line(line)
except IndexError:
pass
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)
self.show_prompt(prompt, data)
self.add_completions(completions)
except StopIteration:
self.currentfunction = None
self.show_prompt()
except command.NoFunction:
self.currentfunction = None
def entered(self):
line = self.get_data()
self.run_line(line)
def setLexers(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.lex.setFont(font)
self.lex.setDefaultFont(font)
self.setLexer(self.lex)
self.autocompleteview.setFont(font)
def adjust_size(self):
fm = QFontMetrics(self.font())
self.setMaximumHeight(20)
self.resize(self.parent().width(), 20)
self.move(0, self.parent().height() - self.height())
self.adjust_auto_complete()
def showEvent(self, event):
self.adjust_size()
self.show_prompt()
self.setFocus()
self.autocompleteview.show()
def activated(self):
visible = self.isVisible()
self.setVisible(not visible)
if __name__ == "__main__":
c = CommandShell(iface.mapCanvas())
c.activated()