| 158 | } |
| 159 | |
| 160 | void ExpressionTextEdit::keyPressEvent(QKeyEvent* e) { |
| 161 | switch (e->key()) { |
| 162 | case Qt::Key_Enter: |
| 163 | case Qt::Key_Return: |
| 164 | e->ignore(); |
| 165 | return; |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | const bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_E); // CTRL+E |
| 171 | if (!isShortcut) // do not process the shortcut when we have a completer |
| 172 | QTextEdit::keyPressEvent(e); |
| 173 | |
| 174 | const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier); |
| 175 | if ((ctrlOrShift && e->text().isEmpty())) |
| 176 | return; |
| 177 | |
| 178 | static QString eow(QStringLiteral("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-=")); // end of word |
| 179 | const bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift; |
| 180 | QTextCursor tc = textCursor(); |
| 181 | tc.select(QTextCursor::WordUnderCursor); |
| 182 | const QString& completionPrefix = tc.selectedText(); |
| 183 | |
| 184 | if (!isShortcut && (hasModifier || e->text().isEmpty() || completionPrefix.length() < 1 || eow.contains(e->text().right(1)))) { |
| 185 | m_completer->popup()->hide(); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (completionPrefix != m_completer->completionPrefix()) { |
| 190 | m_completer->setCompletionPrefix(completionPrefix); |
| 191 | m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0)); |
| 192 | } |
| 193 | QRect cr{cursorRect()}; |
| 194 | cr.setWidth(m_completer->popup()->sizeHintForColumn(0) + m_completer->popup()->verticalScrollBar()->sizeHint().width()); |
| 195 | m_completer->complete(cr); // popup it up! |
| 196 | } |
| 197 | |
| 198 | void ExpressionTextEdit::mouseMoveEvent(QMouseEvent* e) { |
| 199 | QTextCursor tc = cursorForPosition(e->pos()); |