|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +/*************************************************************************** |
| 5 | +Name : DB Manager |
| 6 | +Description : Database manager plugin for QuantumGIS |
| 7 | +Date : May 23, 2011 |
| 8 | +copyright : (C) 2011 by Giuseppe Sucameli |
| 9 | +email : brush.tyler@gmail.com |
| 10 | +
|
| 11 | +The content of this file is based on |
| 12 | +- QTextEdit with autocompletion using pyqt by rowinggolfer (GPLv2 license) |
| 13 | +see http://rowinggolfer.blogspot.com/2010/08/qtextedit-with-autocompletion-using.html |
| 14 | + ***************************************************************************/ |
| 15 | +
|
| 16 | +/*************************************************************************** |
| 17 | + * * |
| 18 | + * This program is free software; you can redistribute it and/or modify * |
| 19 | + * it under the terms of the GNU General Public License as published by * |
| 20 | + * the Free Software Foundation; either version 2 of the License, or * |
| 21 | + * (at your option) any later version. * |
| 22 | + * * |
| 23 | + ***************************************************************************/ |
| 24 | +""" |
| 25 | + |
| 26 | +from PyQt4.QtGui import * |
| 27 | +from PyQt4.QtCore import * |
| 28 | + |
| 29 | +class SqlCompleter(QCompleter): |
| 30 | + def __init__(self, editor, db=None): |
| 31 | + # get the wordlist |
| 32 | + dictionary = None |
| 33 | + if db: |
| 34 | + dictionary = db.connector.getSqlDictionary() |
| 35 | + if not dictionary: |
| 36 | + # use the generic sql dictionary |
| 37 | + from .sql_dictionary import getSqlDictionary |
| 38 | + dictionary = getSqlDictionary() |
| 39 | + |
| 40 | + wordlist = QStringList() |
| 41 | + for name, value in dictionary.iteritems(): |
| 42 | + wordlist << value |
| 43 | + |
| 44 | + # setup the completer |
| 45 | + QCompleter.__init__(self, sorted(wordlist), editor) |
| 46 | + self.setModelSorting(QCompleter.CaseInsensitivelySortedModel) |
| 47 | + self.setWrapAround(False) |
| 48 | + |
| 49 | + if isinstance(editor, CompletionTextEdit): |
| 50 | + editor.setCompleter(self) |
| 51 | + |
| 52 | + |
| 53 | +class CompletionTextEdit(QTextEdit): |
| 54 | + def __init__(self, *args, **kwargs): |
| 55 | + QTextEdit.__init__(self, *args, **kwargs) |
| 56 | + self.completer = None |
| 57 | + |
| 58 | + def setCompleter(self, completer): |
| 59 | + if self.completer: |
| 60 | + self.disconnect(self.completer, 0, self, 0) |
| 61 | + if not completer: |
| 62 | + return |
| 63 | + |
| 64 | + completer.setWidget(self) |
| 65 | + completer.setCompletionMode(QCompleter.PopupCompletion) |
| 66 | + completer.setCaseSensitivity(Qt.CaseInsensitive) |
| 67 | + self.completer = completer |
| 68 | + self.connect(self.completer, SIGNAL("activated(const QString&)"), self.insertCompletion) |
| 69 | + |
| 70 | + def insertCompletion(self, completion): |
| 71 | + tc = self.textCursor() |
| 72 | + extra = completion.length() - self.completer.completionPrefix().length() |
| 73 | + tc.movePosition(QTextCursor.Left) |
| 74 | + tc.movePosition(QTextCursor.EndOfWord) |
| 75 | + tc.insertText(completion.right(extra)) |
| 76 | + self.setTextCursor(tc) |
| 77 | + |
| 78 | + def textUnderCursor(self): |
| 79 | + tc = self.textCursor() |
| 80 | + tc.select(QTextCursor.WordUnderCursor) |
| 81 | + return tc.selectedText() |
| 82 | + |
| 83 | + def focusInEvent(self, event): |
| 84 | + if self.completer: |
| 85 | + self.completer.setWidget(self) |
| 86 | + QTextEdit.focusInEvent(self, event) |
| 87 | + |
| 88 | + def keyPressEvent(self, event): |
| 89 | + if self.completer and self.completer.popup().isVisible(): |
| 90 | + if event.key() in (Qt.Key_Enter, Qt.Key_Return, Qt.Key_Escape, Qt.Key_Tab, Qt.Key_Backtab): |
| 91 | + event.ignore() |
| 92 | + return |
| 93 | + |
| 94 | + # has ctrl-E or ctrl-space been pressed?? |
| 95 | + isShortcut = event.modifiers() == Qt.ControlModifier and event.key() in (Qt.Key_E, Qt.Key_Space) |
| 96 | + if not self.completer or not isShortcut: |
| 97 | + QTextEdit.keyPressEvent(self, event) |
| 98 | + |
| 99 | + # ctrl or shift key on it's own?? |
| 100 | + ctrlOrShift = event.modifiers() in (Qt.ControlModifier, Qt.ShiftModifier) |
| 101 | + if ctrlOrShift and event.text().isEmpty(): |
| 102 | + # ctrl or shift key on it's own |
| 103 | + return |
| 104 | + |
| 105 | + eow = QString("~!@#$%^&*()+{}|:\"<>?,./;'[]\\-=") # end of word |
| 106 | + |
| 107 | + hasModifier = event.modifiers() != Qt.NoModifier and not ctrlOrShift |
| 108 | + |
| 109 | + completionPrefix = self.textUnderCursor() |
| 110 | + |
| 111 | + if not isShortcut and (hasModifier or event.text().isEmpty() or |
| 112 | + completionPrefix.length() < 3 or eow.contains(event.text().right(1))): |
| 113 | + self.completer.popup().hide() |
| 114 | + return |
| 115 | + |
| 116 | + if completionPrefix != self.completer.completionPrefix(): |
| 117 | + self.completer.setCompletionPrefix(completionPrefix) |
| 118 | + popup = self.completer.popup() |
| 119 | + popup.setCurrentIndex(self.completer.completionModel().index(0,0)) |
| 120 | + |
| 121 | + cr = self.cursorRect() |
| 122 | + cr.setWidth(self.completer.popup().sizeHintForColumn(0) |
| 123 | + + self.completer.popup().verticalScrollBar().sizeHint().width()) |
| 124 | + self.completer.complete(cr) # popup it up! |
| 125 | + |
| 126 | +#if __name__ == "__main__": |
| 127 | +# app = QApplication([]) |
| 128 | +# te = CompletionTextEdit() |
| 129 | +# SqlCompleter( te ) |
| 130 | +# te.show() |
| 131 | +# app.exec_() |
0 commit comments