| 140 | } |
| 141 | |
| 142 | bool BrowseManager::eventFilter(QObject* watched, QEvent* event) |
| 143 | { |
| 144 | QWidget* widget = qobject_cast<QWidget*>(watched); |
| 145 | Q_ASSERT(widget); |
| 146 | |
| 147 | auto* keyEvent = dynamic_cast<QKeyEvent*>(event); |
| 148 | |
| 149 | const int browseKey = Qt::Key_Control; |
| 150 | const int magicModifier = Qt::Key_Alt; |
| 151 | |
| 152 | #if KTEXTEDITOR_VERSION >= QT_VERSION_CHECK(6, 20, 0) |
| 153 | auto* const view = KTextEditor::View::fromEditorWidget(widget); |
| 154 | #else |
| 155 | auto* const view = qobject_cast<KTextEditor::View*>(widget->parent()); |
| 156 | #endif |
| 157 | if (!view) { |
| 158 | qCCritical(PLUGIN_CONTEXTBROWSER) << "the view of the editor widget" << widget << "is null"; |
| 159 | } |
| 160 | |
| 161 | //Eventually start key-browsing |
| 162 | if (keyEvent && (keyEvent->key() == browseKey || keyEvent->key() == magicModifier) && !m_browsingByKey && |
| 163 | keyEvent->type() == QEvent::KeyPress) { |
| 164 | m_delayedBrowsingTimer->start(); // always start the timer, to get consistent behavior regarding the ALT key and the menu activation |
| 165 | m_browsingByKey = keyEvent->key(); |
| 166 | |
| 167 | if (!view) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | if (keyEvent->key() == magicModifier) { |
| 172 | if (view->isCompletionActive()) { |
| 173 | //Completion is active. |
| 174 | avoidMenuAltFocus(); |
| 175 | m_delayedBrowsingTimer->stop(); |
| 176 | } else { |
| 177 | m_browsingStartedInView = view; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if (keyEvent && m_browsingByKey && m_browsingStartedInView && keyEvent->type() == QEvent::KeyPress) { |
| 183 | if (keyEvent->key() >= Qt::Key_1 && keyEvent->key() <= Qt::Key_9) { |
| 184 | // user wants to trigger an action in the code browser |
| 185 | const int index = keyEvent->key() - Qt::Key_1; |
| 186 | emit invokeAction(index); |
| 187 | emit stopDelayedBrowsing(); |
| 188 | return true; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (!view) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | auto* focusEvent = dynamic_cast<QFocusEvent*>(event); |
| 197 | //Eventually stop key-browsing |
| 198 | if ((keyEvent && m_browsingByKey && ( keyEvent->key() == m_browsingByKey || keyEvent->modifiers() == Qt::ControlModifier ) && keyEvent->type() == QEvent::KeyRelease) |
| 199 | || (focusEvent && focusEvent->lostFocus()) || event->type() == QEvent::WindowDeactivate) { |
nothing calls this directly
no test coverage detected