1 #ifndef UISCRIPTTEXTEDIT_H
2 #define UISCRIPTTEXTEDIT_H
6 #include <QAbstractItemView>
8 #include <QPlainTextEdit>
10 #include <QTextCursor>
12 #include <QStringListModel>
16 #include "python/wrappers/py_wrappers.h"
18 class PythonQtObjectPtr;
30 QCompleter* _completer;
32 PythonQtObjectPtr _context;
38 void setContext(PythonQtObjectPtr ctx) { _context = ctx; }
39 PythonQtObjectPtr context() {
return _context; }
45 void insertCompletion(
const QString& completion)
47 QTextCursor tc = this->textCursor();
48 tc.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
49 if (tc.selectedText() ==
".") {
50 tc.insertText(QString(
".") + completion);
52 tc = this->textCursor();
53 tc.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor);
54 tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
55 tc.insertText(completion);
56 this->setTextCursor(tc);
62 void handleTabCompletion()
64 QTextCursor textCursor = this->textCursor();
65 int pos = textCursor.position();
66 textCursor.setPosition(0);
67 textCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
68 int startPos = textCursor.selectionStart();
70 int offset = pos - startPos;
71 QString text = textCursor.selectedText();
73 QString textToComplete;
76 QChar c = text.at(cur);
77 if (c.isLetterOrNumber() || c ==
'.' || c ==
'_') {
78 textToComplete.prepend(c);
85 QString compareText = textToComplete;
86 int dot = compareText.lastIndexOf(
'.');
88 lookup = compareText.mid(0, dot);
89 compareText = compareText.mid(dot + 1, offset);
91 if (!lookup.isEmpty() || !compareText.isEmpty()) {
92 compareText = compareText.toLower();
94 QStringList l = PythonQt::self()->introspection(_context, lookup, PythonQt::Anything);
95 Q_FOREACH (QString n, l) {
96 if (n.toLower().startsWith(compareText)) {
101 if (!found.isEmpty()) {
102 _completer->setCompletionPrefix(compareText);
103 _completer->setCompletionMode(QCompleter::PopupCompletion);
104 _completer->setModel(
new QStringListModel(found, _completer));
105 _completer->setCaseSensitivity(Qt::CaseInsensitive);
106 QTextCursor c = this->textCursor();
107 c.movePosition(QTextCursor::StartOfWord);
108 QRect cr = cursorRect(c);
109 cr.setWidth(_completer->popup()->sizeHintForColumn(0)
110 + _completer->popup()->verticalScrollBar()->sizeHint().width());
112 _completer->complete(cr);
114 _completer->popup()->hide();
117 _completer->popup()->hide();
121 void keyPressEvent(QKeyEvent* event)
124 if (_completer && _completer->popup()->isVisible()) {
126 switch (event->key()) {
128 if (!_completer->popup()->currentIndex().isValid()) {
129 insertCompletion(_completer->currentCompletion());
130 _completer->popup()->hide();
139 case Qt::Key_Backtab:
149 QTextCursor textCursor = this->textCursor();
255 QPlainTextEdit::keyPressEvent(event);
256 QString text =
event->text();
258 if (!text.isEmpty()) {
259 handleTabCompletion();
261 _completer->popup()->hide();
268 void mousePressEvent(QMouseEvent* e)
275 #endif // UISCRIPTEDITOR_H
UIScriptTextEdit(QPlainTextEdit *parent=0)
Definition: UIScriptTextEdit.cpp:8
The text editor for UIScript editor widget.
Definition: UIScriptTextEdit.h:26