void GuiMainWindow::currentChanged(int index) { if (index < 0) return; if (index < (signed)widgetAtIndex.size()) { auto it = widgetAtIndex[index]; if (it.first) { if (it.first->focusWidget()) it.first->focusWidget()->setFocus(); } else if (it.second) it.second->setFocus(); return; } // slow_mode: if tab is inserted just now, widgetAtIndex may not // be up-to-date. if (tabArea->widget(index)) { QWidget *currWgt = tabArea->widget(index); if (qobject_cast<GuiSplitter*>(currWgt)) { if (currWgt->focusWidget()) currWgt->focusWidget()->setFocus(); } else if (qobject_cast<GuiTerminalWindow*>(currWgt)) currWgt->setFocus(); } }
void QApplicationPrivate::processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent *e) { QWidget *focusW = 0; if (self->inPopupMode()) { QWidget *popupW = qApp->activePopupWidget(); focusW = popupW->focusWidget() ? popupW->focusWidget() : popupW; } if (!focusW) focusW = QApplication::focusWidget(); if (!focusW) { focusW = e->widget.data(); } if (!focusW) focusW = QApplication::activeWindow(); //qDebug() << "handleKeyEvent" << hex << e->key() << e->modifiers() << e->text() << "widget" << focusW; if (!focusW) return; if (app_do_modal && !qt_try_modal(focusW, e->keyType)) return; if (e->nativeScanCode || e->nativeVirtualKey || e->nativeModifiers) { QKeyEventEx ev(e->keyType, e->key, e->modifiers, e->unicode, e->repeat, e->repeatCount, e->nativeScanCode, e->nativeVirtualKey, e->nativeModifiers); QApplication::sendSpontaneousEvent(focusW, &ev); } else { QKeyEvent ev(e->keyType, e->key, e->modifiers, e->unicode, e->repeat, e->repeatCount); QApplication::sendSpontaneousEvent(focusW, &ev); } }
void PrefsDialog::changedTabPanel(int index) { // this all feels a bit hacky, but seems to keep things tidy on Mac OS X at least QWidget *page = tabWidget->widget(index); page->clearFocus(); switch (index) { case 0: // General if (page->focusWidget() != NULL) page->focusWidget()->clearFocus(); break; case 1: // Editor editorFont->setFocus(); editorFont->lineEdit()->selectAll(); break; case 2: // Preview scale->setFocus(); scale->selectAll(); break; case 3: // Typesetting binPathList->setFocus(); break; case 4: // Script if (page->focusWidget() != NULL) page->focusWidget()->clearFocus(); break; } }
void GuiMainWindow::currentChanged(int index) { if (index!=-1 && tabArea->widget(index)) { QWidget *currWgt = tabArea->widget(index); if (dynamic_cast<GuiSplitter*>(currWgt)) { if (currWgt->focusWidget()) currWgt->focusWidget()->setFocus(); } else if (dynamic_cast<GuiTerminalWindow*>(currWgt)) currWgt->setFocus(); } }
void testMoveTab() { // Test inspired by #170470 and #177036 (title messup). // Then expanded to include the problem of #159295 (focus loss). QWidget topLevel; QComboBox* combo = new QComboBox(&topLevel); combo->setEditable(true); KTabWidget* w = new KTabWidget(&topLevel); w->setAutomaticResizeTabs(true); w->resize(300, 400); QResizeEvent e(w->size(), QSize()); QApplication::sendEvent(w, &e); QString prefix = "This is a long prefix for the tab title. "; KLineEdit* lineEdits[4]; for (int i = 0; i < 4; ++i) { QWidget* page = new QWidget; page->setObjectName(QString::number(i)); lineEdits[i] = new KLineEdit(page); // a widget that can take focus lineEdits[i]->setObjectName("LineEdit"+QString::number(i)); w->insertTab(i, page, prefix+QString::number(i)); //kDebug() << i << w->tabText(i); } topLevel.show(); // Necessary after show(), otherwise topLevel.focusWidget() returns NULL QApplication::setActiveWindow(&topLevel); // Make sure the tab order is so that the combo gets focus after this tab QWidget::setTabOrder(lineEdits[0], combo->lineEdit()); QWidget::setTabOrder(combo->lineEdit(), lineEdits[1]); w->setCurrentIndex(0); lineEdits[0]->setFocus(); QCOMPARE(topLevel.focusWidget()->objectName(), lineEdits[0]->objectName()); QVERIFY(lineEdits[0]->isVisible()); w->moveTab(0,3); //for (int i = 0; i < 4; ++i) //kDebug() << i << w->tabText(i); QCOMPARE(w->tabText(0), QString(prefix+QString::number(1))); QCOMPARE(w->tabText(1), QString(prefix+QString::number(2))); QCOMPARE(w->tabText(2), QString(prefix+QString::number(3))); QCOMPARE(w->tabText(3), QString(prefix+QString::number(0))); // Did the focus switch to the lineEdit, due to removeTab+insertTab? Whoops. QCOMPARE(topLevel.focusWidget()->objectName(), lineEdits[0]->objectName()); w->moveTab(3,0); QCOMPARE(topLevel.focusWidget()->objectName(), lineEdits[0]->objectName()); for (int i = 0; i < 4; ++i) { //kDebug() << i << w->tabText(i); QCOMPARE(w->tabText(i), QString(prefix+QString::number(i))); } }
QWidget *QWidgetProto::focusWidget() const { QWidget *item = qscriptvalue_cast<QWidget*>(thisObject()); if (item) return item->focusWidget(); return 0; }
SearchBar::SearchBar(QWidget *parent) :QWidget(parent) { // Get the widget that currently has the focus so we can properly // restore it when the filter bar is closed. QWidget* widgetWindow = (parent ? parent->window() : 0); m_focusWidget = (widgetWindow ? widgetWindow->focusWidget() : 0); // Initialize the user interface... m_ui.setupUi(this); m_ui.optionsButton->addAction(m_ui.actionMatchCase); m_ui.optionsButton->addAction(m_ui.actionHighlightMatch); m_ui.optionsButton->addAction(m_ui.actionSearchAutomatically); m_ui.closeButton->setIcon(KIcon("dialog-close")); m_ui.previousButton->setIcon(KIcon("go-up-search")); m_ui.nextButton->setIcon(KIcon("go-down-search")); m_ui.previousButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); m_ui.nextButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); m_ui.searchInfo->setText(i18nc("label for input line to find text", "&Find:")); setFocusProxy(m_ui.searchComboBox); connect(m_ui.nextButton, SIGNAL(clicked()), this, SLOT(findNext())); connect(m_ui.previousButton, SIGNAL(clicked()), this, SLOT(findPrevious())); connect(m_ui.searchComboBox, SIGNAL(returnPressed()), this, SLOT(findNext())); connect(m_ui.searchComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(textChanged(QString))); // Start off hidden by default... setVisible(false); }
/*! \property QStackedLayout::currentIndex \brief the index position of the widget that is visible The current index is -1 if there is no current widget. \sa currentWidget(), indexOf() */ void QStackedLayout::setCurrentIndex(int index) { Q_D(QStackedLayout); QWidget *prev = currentWidget(); QWidget *next = widget(index); if (!next || next == prev) return; bool reenableUpdates = false; QWidget *parent = parentWidget(); if (parent && parent->updatesEnabled()) { reenableUpdates = true; parent->setUpdatesEnabled(false); } QPointer<QWidget> fw = parent ? parent->window()->focusWidget() : 0; const bool focusWasOnOldPage = fw && (prev && prev->isAncestorOf(fw)); if (prev) { prev->clearFocus(); if (d->stackingMode == StackOne) prev->hide(); } d->index = index; next->raise(); next->show(); // try to move focus onto the incoming widget if focus // was somewhere on the outgoing widget. if (parent) { if (focusWasOnOldPage) { // look for the best focus widget we can find if (QWidget *nfw = next->focusWidget()) nfw->setFocus(); else { // second best: first child widget in the focus chain if (QWidget *i = fw) { while ((i = i->nextInFocusChain()) != fw) { if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled() && next->isAncestorOf(i)) { i->setFocus(); break; } } // third best: incoming widget if (i == fw ) next->setFocus(); } } } } if (reenableUpdates) parent->setUpdatesEnabled(true); emit currentChanged(index); }
void QApplicationPrivate::closePopup(QWidget *popup) { if (!popupWidgets) return; popupWidgets->removeAll(popup); if (popup == qt_popup_down) { qt_button_down = 0; qt_popup_down = 0; } if (QApplicationPrivate::popupWidgets->count() == 0) { // this was the last popup delete QApplicationPrivate::popupWidgets; QApplicationPrivate::popupWidgets = 0; if (popupGrabOk) { popupGrabOk = false; if (popup->geometry().contains(QPoint(QGuiApplicationPrivate::mousePressX, QGuiApplicationPrivate::mousePressY)) || popup->testAttribute(Qt::WA_NoMouseReplay)) { // mouse release event or inside qt_replay_popup_mouse_event = false; } else { // mouse press event QGuiApplicationPrivate::mousePressTime -= 10000; // avoid double click qt_replay_popup_mouse_event = true; } // transfer grab back to mouse grabber if any, otherwise release the grab ungrabMouseForPopup(popup); // transfer grab back to keyboard grabber if any, otherwise release the grab ungrabKeyboardForPopup(popup); } if (active_window) { if (QWidget *fw = active_window->focusWidget()) { if (fw != QApplication::focusWidget()) { fw->setFocus(Qt::PopupFocusReason); } else { QFocusEvent e(QEvent::FocusIn, Qt::PopupFocusReason); QCoreApplication::sendEvent(fw, &e); } } } } else { // A popup was closed, so the previous popup gets the focus. QWidget* aw = QApplicationPrivate::popupWidgets->last(); if (QWidget *fw = aw->focusWidget()) fw->setFocus(Qt::PopupFocusReason); if (QApplicationPrivate::popupWidgets->count() == 1) // grab mouse/keyboard grabForPopup(aw); } }
GuiTerminalWindow * GuiMainWindow::getCurrentTerminal() { GuiTerminalWindow *termWindow; QWidget *widget = tabArea->currentWidget(); if (!widget) return NULL; termWindow = dynamic_cast<GuiTerminalWindow*>(widget->focusWidget()); if (!termWindow || terminalList.indexOf(termWindow) == -1) return NULL; return termWindow; }
void ModeManager::setFocusToCurrentMode() { IMode *mode = findMode(currentMode()); QTC_ASSERT(mode, return); QWidget *widget = mode->widget(); if (widget) { QWidget *focusWidget = widget->focusWidget(); if (!focusWidget) focusWidget = widget; focusWidget->setFocus(); } }
void ModeManager::setFocusToCurrentMode() { IMode *mode = currentMode(); QTC_ASSERT(mode, return); QWidget *widget = mode->widget(); if (widget) { QWidget *focusWidget = widget->focusWidget(); if (!focusWidget) focusWidget = widget; focusWidget->setFocus(); ICore::raiseWindow(focusWidget); } }
GuiTerminalWindow *GuiMainWindow::getCurrentTerminalInTab(int tabIndex) { GuiTerminalWindow *termWindow; QWidget *widget = tabArea->widget(tabIndex); if (!widget) return NULL; if ((termWindow = dynamic_cast<GuiTerminalWindow*>(widget))) return termWindow; termWindow = dynamic_cast<GuiTerminalWindow*>(widget->focusWidget()); if (!termWindow || terminalList.indexOf(termWindow) == -1) return NULL; return termWindow; }
void QApplicationPrivate::closePopup(QWidget *popup) { Q_Q(QApplication); if (!popupWidgets) return; popupWidgets->removeAll(popup); //### // if (popup == qt_popup_down) { // qt_button_down = 0; // qt_popup_down = 0; // } if (QApplicationPrivate::popupWidgets->count() == 0) { // this was the last popup delete QApplicationPrivate::popupWidgets; QApplicationPrivate::popupWidgets = 0; //### replay mouse event? //### transfer/release mouse grab //### transfer/release keyboard grab //give back focus if (active_window) { if (QWidget *fw = active_window->focusWidget()) { if (fw != QApplication::focusWidget()) { fw->setFocus(Qt::PopupFocusReason); } else { QFocusEvent e(QEvent::FocusIn, Qt::PopupFocusReason); q->sendEvent(fw, &e); } } } } else { // A popup was closed, so the previous popup gets the focus. QWidget* aw = QApplicationPrivate::popupWidgets->last(); if (QWidget *fw = aw->focusWidget()) fw->setFocus(Qt::PopupFocusReason); //### regrab the keyboard and mouse in case 'popup' lost the grab } }
void QWidgetWindow::handleKeyEvent(QKeyEvent *event) { if (QApplicationPrivate::instance()->modalState() && !qt_try_modal(m_widget, event->type())) return; QObject *receiver = QWidget::keyboardGrabber(); if (!receiver && QApplicationPrivate::inPopupMode()) { QWidget *popup = QApplication::activePopupWidget(); QWidget *popupFocusWidget = popup->focusWidget(); receiver = popupFocusWidget ? popupFocusWidget : popup; } if (!receiver) receiver = focusObject(); QGuiApplication::sendSpontaneousEvent(receiver, event); }
void QWidgetWindow::handleGestureEvent(QNativeGestureEvent *e) { // copy-pasted code to find correct widget follows: QObject *receiver = 0; if (QApplicationPrivate::inPopupMode()) { QWidget *popup = QApplication::activePopupWidget(); QWidget *popupFocusWidget = popup->focusWidget(); receiver = popupFocusWidget ? popupFocusWidget : popup; } if (!receiver) receiver = QApplication::widgetAt(e->globalPos()); if (!receiver) receiver = m_widget; // last resort QApplication::sendSpontaneousEvent(receiver, e); }
QObject *QWidgetWindow::focusObject() const { QWidget *windowWidget = m_widget; if (!windowWidget) return Q_NULLPTR; QWidget *widget = windowWidget->focusWidget(); if (!widget) widget = windowWidget; QObject *focusObj = QWidgetPrivate::get(widget)->focusObject(); if (focusObj) return focusObj; return widget; }
void QTabContainer::handleCurrentChanged(int index) { QWidget* widget = this->widget(index); if(widget) { if(tabWindow->isActiveWindow()) { QWidget* focusWidget = widget->focusWidget(); if(focusWidget) focusWidget->setFocus(); else widget->setFocus(); } else if(tabWindow != tabWindow->tabFramework) { tabWindow->focusTab = widget; tabWindow->setWindowTitle(widget->windowTitle()); } } }
void StackLayout::setCurrentIndex(int index) { QWidget *prev = currentWidget(); QWidget *next = widget(index); if (!next || next == prev) return; _index = index; if(!parent()) return; bool reenableUpdates = false; QWidget *parent = parentWidget(); if (parent && parent->updatesEnabled()) { reenableUpdates = true; parent->setUpdatesEnabled(false); } QWidget *fw = parent ? parent->window()->focusWidget() : 0; if (prev) { prev->clearFocus(); if (_mode == StackOne) prev->hide(); } next->raise(); next->show(); // try to move focus onto the incoming widget if focus // was somewhere on the outgoing widget. if (parent) { if (fw && (prev && prev->isAncestorOf(fw))) { // focus was on old page // look for the best focus widget we can find if (QWidget *nfw = next->focusWidget()) nfw->setFocus(); else { // second best: first child widget in the focus chain QWidget *i = fw; while ((i = i->nextInFocusChain()) != fw) { if (((i->focusPolicy() & Qt::TabFocus) == Qt::TabFocus) && !i->focusProxy() && i->isVisibleTo(next) && i->isEnabled() && next->isAncestorOf(i)) { i->setFocus(); break; } } // third best: incoming widget if (i == fw ) next->setFocus(); } } } if (reenableUpdates) parent->setUpdatesEnabled(true); if (_mode == StackOne) // expandingDirections() might have changed, so invalidate(): invalidate(); }