Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:epopov:branches:openSUSE:Factory
kf6-kconfigwidgets
2001-better-commandbar.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 2001-better-commandbar.patch of Package kf6-kconfigwidgets
diff --git a/src/kcommandbar.cpp b/src/kcommandbar.cpp index 4f602aa736de673e883d369ff197e27f5f7a5b53..c62e36f420b86d47d89e8a671d5e681d5a5fc6e7 100644 --- a/src/kcommandbar.cpp +++ b/src/kcommandbar.cpp @@ -10,7 +10,7 @@ #include <QAction> #include <QActionGroup> #include <QCoreApplication> -#include <QGraphicsOpacityEffect> +#include <QGraphicsEffect> #include <QHeaderView> #include <QKeyEvent> #include <QLabel> @@ -84,7 +84,7 @@ static QRect getCommandBarBoundingRect(KCommandBar *commandBar) } } - return boundingRect; + return boundingRect.translated(mainWindow->geometry().topLeft()); } // BEGIN CommandBarFilterModel @@ -299,10 +299,6 @@ public: formats.append({0, componentIdx, gray}); } - QTextCharFormat fmt; - fmt.setForeground(option.palette.link()); - fmt.setFontWeight(QFont::Bold); - /** * Highlight matches from fuzzy matcher */ @@ -554,43 +550,40 @@ public: void KCommandBarPrivate::slotReturnPressed(KCommandBar *q) { - auto act = m_proxyModel.data(m_treeView.currentIndex(), Qt::UserRole).value<QAction *>(); - if (act) { - // if the action is a menu, we take all its actions - // and reload our dialog with these instead. - if (auto menu = act->menu()) { - auto menuActions = menu->actions(); - KCommandBar::ActionGroup ag; - - // if there are no actions, trigger load actions - // this happens with some menus that are loaded on demand - if (menuActions.size() == 0) { - Q_EMIT menu->aboutToShow(); - ag.actions = menu->actions(); - } + auto action = m_proxyModel.data(m_treeView.currentIndex(), Qt::UserRole).value<QAction *>(); + if (!action) { + return; + } - QString groupName = KLocalizedString::removeAcceleratorMarker(act->text()); - ag.name = groupName; - - m_model.refresh({ag}); - reselectFirst(); - /** - * We want the "textChanged" signal here - * so that proxy model triggers filtering again - * so don't use d->clearLineEdit() - */ - m_lineEdit.clear(); - return; - } else { - m_model.actionTriggered(act->text()); - q->hide(); - act->trigger(); + // if the action is a menu, we take all its actions + // and reload our dialog with these instead. + if (auto menu = action->menu()) { + auto menuActions = menu->actions(); + KCommandBar::ActionGroup ag; + + // if there are no actions, trigger load actions + // this happens with some menus that are loaded on demand + if (menuActions.size() == 0) { + Q_EMIT menu->aboutToShow(); + ag.actions = menu->actions(); } - } - clearLineEdit(); - q->hide(); - q->deleteLater(); + QString groupName = KLocalizedString::removeAcceleratorMarker(action->text()); + ag.name = groupName; + + m_model.refresh({ag}); + reselectFirst(); + /** + * We want the "textChanged" signal here + * so that proxy model triggers filtering again + * so don't use d->clearLineEdit() + */ + m_lineEdit.clear(); + } else { + m_model.actionTriggered(action->text()); + q->close(); + action->trigger(); + } } void KCommandBarPrivate::setLastUsedActions() @@ -611,49 +604,49 @@ QStringList KCommandBarPrivate::lastUsedActions() const // BEGIN KCommandBar KCommandBar::KCommandBar(QWidget *parent) - : QFrame(parent) + : QFrame(parent, Qt::Popup) , d(new KCommandBarPrivate) { - QGraphicsDropShadowEffect *e = new QGraphicsDropShadowEffect(this); - e->setColor(palette().color(QPalette::Shadow)); - e->setOffset(2.); - e->setBlurRadius(8.); - setGraphicsEffect(e); + setAttribute(Qt::WA_DeleteOnClose); + setAttribute(Qt::WA_TranslucentBackground); + setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); + setProperty("_breeze_force_frame", true); + setProperty("_KDE_NET_WM_FORCE_SHADOW", true); - setAutoFillBackground(true); - setFrameShadow(QFrame::Raised); - setFrameShape(QFrame::Box); - - QVBoxLayout *layout = new QVBoxLayout(); + QVBoxLayout *layout = new QVBoxLayout; layout->setSpacing(0); - layout->setContentsMargins(2, 2, 2, 2); + layout->setContentsMargins(QMargins()); setLayout(layout); - setFocusProxy(&d->m_lineEdit); - layout->addWidget(&d->m_lineEdit); + d->m_lineEdit.setPlaceholderText(i18n("Search...")); + d->m_lineEdit.setClearButtonEnabled(true); + d->m_lineEdit.addAction(QIcon::fromTheme(QStringLiteral("search")), QLineEdit::LeadingPosition); + d->m_lineEdit.setFrame(false); + d->m_lineEdit.setTextMargins(QMargins() + style()->pixelMetric(QStyle::PM_ButtonMargin)); + setFocusProxy(&d->m_lineEdit); layout->addWidget(&d->m_treeView); + d->m_treeView.setFocusPolicy(Qt::NoFocus); d->m_treeView.setTextElideMode(Qt::ElideLeft); d->m_treeView.setUniformRowHeights(true); + d->m_treeView.setProperty("_breeze_borders_sides", QVariant::fromValue(QFlags(Qt::TopEdge))); CommandBarStyleDelegate *delegate = new CommandBarStyleDelegate(this); ShortcutStyleDelegate *del = new ShortcutStyleDelegate(this); d->m_treeView.setItemDelegateForColumn(KCommandBarModel::Column_Command, delegate); d->m_treeView.setItemDelegateForColumn(KCommandBarModel::Column_Shortcut, del); - connect(&d->m_lineEdit, &QLineEdit::returnPressed, this, [this]() { - d->slotReturnPressed(this); - }); connect(&d->m_lineEdit, &QLineEdit::textChanged, &d->m_proxyModel, &CommandBarFilterModel::setFilterString); connect(&d->m_lineEdit, &QLineEdit::textChanged, delegate, &CommandBarStyleDelegate::setFilterString); connect(&d->m_lineEdit, &QLineEdit::textChanged, this, [this]() { d->m_treeView.viewport()->update(); d->reselectFirst(); }); - connect(&d->m_treeView, &QTreeView::clicked, this, [this]() { + connect(&d->m_treeView, &QTreeView::activated, this, [this]() { d->slotReturnPressed(this); }); + connect(&d->m_treeView, &QTreeView::clicked, &d->m_treeView, &QTreeView::activated); d->m_proxyModel.setSourceModel(&d->m_model); d->m_treeView.setSortingEnabled(true); @@ -664,8 +657,6 @@ KCommandBar::KCommandBar(QWidget *parent) d->m_treeView.header()->setSectionResizeMode(KCommandBarModel::Column_Command, QHeaderView::Stretch); d->m_treeView.header()->setSectionResizeMode(KCommandBarModel::Column_Shortcut, QHeaderView::ResizeToContents); - parent->installEventFilter(this); - d->m_treeView.installEventFilter(this); d->m_lineEdit.installEventFilter(this); d->m_treeView.setHeaderHidden(true); @@ -677,7 +668,6 @@ KCommandBar::KCommandBar(QWidget *parent) placeholderLabel->setAlignment(Qt::AlignCenter); placeholderLabel->setTextInteractionFlags(Qt::NoTextInteraction); placeholderLabel->setWordWrap(true); - placeholderLabel->setText(i18n("No commands matching the filter")); // To match the size of a level 2 Heading/KTitleWidget QFont placeholderLabelFont = placeholderLabel->font(); placeholderLabelFont.setPointSize(qRound(placeholderLabelFont.pointSize() * 1.3)); @@ -692,11 +682,18 @@ KCommandBar::KCommandBar(QWidget *parent) d->m_treeView.setLayout(placeholderLayout); connect(&d->m_proxyModel, &CommandBarFilterModel::modelReset, this, [this, placeholderLabel]() { - placeholderLabel->setHidden(d->m_proxyModel.rowCount() > 0); + if (d->m_proxyModel.rowCount() > 0) { + placeholderLabel->hide(); + } else { + if (d->m_model.rowCount() == 0) { + placeholderLabel->setText(i18n("No commands to display")); + } else { + placeholderLabel->setText(i18n("No commands matching the filter")); + } + placeholderLabel->show(); + } }); - setHidden(true); - // Migrate last used action config to new location KConfigGroup cg(KSharedConfig::openConfig(), QStringLiteral("General")); if (cg.hasKey("CommandBarLastUsedActions")) { @@ -723,7 +720,6 @@ KCommandBar::~KCommandBar() // class, otherwise while KCommandBar is being torn down, an event could // fire and the eventFilter() accesses d, which would cause a crash // bug 452527 - d->m_treeView.removeEventFilter(this); d->m_lineEdit.removeEventFilter(this); } @@ -734,12 +730,51 @@ void KCommandBar::setActions(const QList<ActionGroup> &actions) d->m_model.refresh(actions); d->reselectFirst(); - - show(); - setFocus(); } void KCommandBar::show() +{ + QWidget::show(); +} + +bool KCommandBar::eventFilter(QObject *obj, QEvent *event) +{ + if (obj == &d->m_lineEdit && event->type() == QEvent::KeyPress) { + const QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event); + if (keyEvent->modifiers() & Qt::MetaModifier) { + return true; + } + + switch (keyEvent->key()) { + case Qt::Key_Up: + case Qt::Key_Down: + case Qt::Key_PageUp: + case Qt::Key_PageDown: + case Qt::Key_Enter: + case Qt::Key_Return: + QCoreApplication::sendEvent(&d->m_treeView, event); + return true; + case Qt::Key_Escape: + close(); + return true; + default: + break; + } + } + + return QWidget::eventFilter(obj, event); +} + +void KCommandBar::showEvent(QShowEvent *event) +{ + adjustSizeAndPosition(); + + QWidget::showEvent(event); + + setFocus(Qt::PopupFocusReason); +} + +void KCommandBar::adjustSizeAndPosition() { const QRect boundingRect = getCommandBarBoundingRect(this); @@ -751,60 +786,13 @@ void KCommandBar::show() const int maxHeight = boundingRect.height(); const int preferredHeight = maxHeight / 2; - const QSize size{std::min(maxWidth, std::max(preferredWidth, minWidth)), std::min(maxHeight, std::max(preferredHeight, minHeight))}; + const QSize size(std::min(maxWidth, std::max(preferredWidth, minWidth)), std::min(maxHeight, std::max(preferredHeight, minHeight))); - setFixedSize(size); + resize(size); // set the position to the top-center of the parent // just below the menubar/toolbar (if any) - const QPoint position{boundingRect.center().x() - size.width() / 2, boundingRect.y()}; - move(position); - - QWidget::show(); -} - -bool KCommandBar::eventFilter(QObject *obj, QEvent *event) -{ - if (event->type() == QEvent::KeyPress || event->type() == QEvent::ShortcutOverride) { - QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event); - if (obj == &d->m_lineEdit) { - const int key = keyEvent->key(); - const bool forward2list = (key == Qt::Key_Up) || (key == Qt::Key_Down) || (key == Qt::Key_PageUp) || (key == Qt::Key_PageDown); - if (forward2list) { - QCoreApplication::sendEvent(&d->m_treeView, event); - return true; - } - } else if (obj == &d->m_treeView) { - const int key = keyEvent->key(); - const bool forward2input = (key != Qt::Key_Up) && (key != Qt::Key_Down) && (key != Qt::Key_PageUp) && (key != Qt::Key_PageDown) - && (key != Qt::Key_Tab) && (key != Qt::Key_Backtab); - if (forward2input) { - QCoreApplication::sendEvent(&d->m_lineEdit, event); - return true; - } - } - - if (keyEvent->key() == Qt::Key_Escape) { - hide(); - deleteLater(); - return true; - } - } - - // hide on focus out, if neither input field nor list have focus! - else if (event->type() == QEvent::FocusOut && isVisible() && !(d->m_lineEdit.hasFocus() || d->m_treeView.hasFocus())) { - d->clearLineEdit(); - deleteLater(); - hide(); - return true; - } - - // handle resizing - if (parent() == obj && event->type() == QEvent::Resize) { - show(); - } - - return QWidget::eventFilter(obj, event); + move(boundingRect.center().x() - size.width() / 2, boundingRect.y() + 6); } // END KCommandBar diff --git a/src/kcommandbar.h b/src/kcommandbar.h index a533630e3eade6e40f538405ea3f5cbb0e1eabc8..c852c292f70ab117aae2ffaba5b0eb01b7412b75 100644 --- a/src/kcommandbar.h +++ b/src/kcommandbar.h @@ -76,12 +76,15 @@ public: void setActions(const QList<ActionGroup> &actions); public Q_SLOTS: - void show(); + void show(); // for compatibility with the original API protected: bool eventFilter(QObject *obj, QEvent *event) override; + void showEvent(QShowEvent *event) override; private: + void adjustSizeAndPosition(); + std::unique_ptr<class KCommandBarPrivate> const d; };
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor