Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:epopov:branches:openSUSE:Factory
kconfigwidgets
2000-commandbar-checkmarks.patch
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 2000-commandbar-checkmarks.patch of Package kconfigwidgets
diff --git a/src/kcommandbar.cpp b/src/kcommandbar.cpp index c9eb8654003d4267f004a1a414e9f24e2ba34f69..1090a0972f2a80996433e0a955bbb6ac33fcd1c2 100644 --- a/src/kcommandbar.cpp +++ b/src/kcommandbar.cpp @@ -8,6 +8,7 @@ #include "kconfigwidgets_debug.h" #include <QAction> +#include <QActionGroup> #include <QCoreApplication> #include <QGraphicsOpacityEffect> #include <QHeaderView> @@ -37,6 +38,7 @@ public: { connect(this, &CommandBarFilterModel::modelAboutToBeReset, this, [this]() { m_hasActionsWithIcons = false; + m_hasCheckableActions = false; }); } @@ -45,6 +47,11 @@ public: return m_hasActionsWithIcons; } + bool hasCheckableActions() const + { + return m_hasCheckableActions; + } + Q_SLOT void setFilterString(const QString &string) { // MUST reset the model here, we want to repopulate @@ -89,8 +96,14 @@ protected: } } - if (accept && !m_hasActionsWithIcons) { - m_hasActionsWithIcons |= !index.data(Qt::DecorationRole).isNull(); + if (accept) { + if (!m_hasActionsWithIcons) { + m_hasActionsWithIcons |= !index.data(Qt::DecorationRole).isNull(); + } + + if (!m_hasCheckableActions) { + m_hasCheckableActions |= !index.data(Qt::CheckStateRole).isNull(); + } } return accept; @@ -99,6 +112,7 @@ protected: private: QString m_pattern; mutable bool m_hasActionsWithIcons = false; + mutable bool m_hasCheckableActions = false; }; // END CommandBarFilterModel @@ -164,20 +178,56 @@ public: initStyleOption(&option, index); option.text.clear(); // clear old text QStyle *style = option.widget->style(); - style->drawControl(QStyle::CE_ItemViewItem, &option, painter, option.widget); - - const int hMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, option.widget); - - QRect textRect = option.rect; + style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter); const CommandBarFilterModel *model = static_cast<const CommandBarFilterModel *>(index.model()); + const bool isRightToLeft = option.direction == Qt::RightToLeft; + const int hMargin = 2 * style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, option.widget); + + QRect outputRect = option.rect; + outputRect.adjust(hMargin, 0, -hMargin, 0); + + if (model->hasCheckableActions()) { + // draw checkbox + QRect checkMarkRect = style->subElementRect(QStyle::SE_CheckBoxIndicator, &option); + if (option.features & QStyleOptionViewItem::HasCheckIndicator) { + checkMarkRect = QStyle::alignedRect(option.direction, option.displayAlignment, checkMarkRect.size(), outputRect); + QStyleOptionViewItem optionCopy = option; + optionCopy.rect = checkMarkRect; + optionCopy.state = option.state & ~QStyle::State_HasFocus; + switch (option.checkState) { + case Qt::Unchecked: + optionCopy.state |= QStyle::State_Off; + break; + case Qt::PartiallyChecked: + optionCopy.state |= QStyle::State_NoChange; + break; + case Qt::Checked: + optionCopy.state |= QStyle::State_On; + break; + } + const QAction *action = index.data(Qt::UserRole).value<QAction *>(); + Q_ASSERT(action); + const QActionGroup *actionGroup = action->actionGroup(); + if (actionGroup && actionGroup->isExclusive()) { + style->drawPrimitive(QStyle::PE_IndicatorRadioButton, &optionCopy, painter); + } else { + style->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &optionCopy, painter); + } + } + adjustRect(outputRect, checkMarkRect.width() + hMargin, isRightToLeft); + } + if (model->hasActionsWithIcons()) { - const int iconWidth = option.decorationSize.width() + (hMargin * 2); - if (option.direction == Qt::RightToLeft) { - textRect.adjust(0, 0, -iconWidth, 0); - } else { - textRect.adjust(iconWidth, 0, 0, 0); + // draw icon + QRect iconRect(QPoint(0, 0), option.decorationSize); + if (option.features & QStyleOptionViewItem::HasDecoration) { + iconRect = QStyle::alignedRect(option.direction, option.displayAlignment, iconRect.size(), outputRect); + const QIcon::Mode iconMode = option.state & QStyle::State_Selected ? QIcon::Selected : QIcon::Normal; + const QIcon::State iconState = option.state & QStyle::State_Open ? QIcon::On : QIcon::Off; + option.icon.paint(painter, iconRect, option.decorationAlignment, iconMode, iconState); } + adjustRect(outputRect, iconRect.width() + hMargin, isRightToLeft); } const QString original = index.data().toString(); @@ -212,8 +262,7 @@ public: return QTextLayout::FormatRange{fr.start + actionNameStart, fr.length, f}; }); - textRect.adjust(hMargin, 0, -hMargin, 0); - paintItemText(painter, original, textRect, option, std::move(formats)); + paintItemText(painter, original, outputRect, option, std::move(formats)); painter->restore(); } @@ -225,6 +274,19 @@ public Q_SLOTS: } private: + /** + * Increases rect.left or decreases rect.right by x + * depending on the layout direction + */ + static void adjustRect(QRect &rect, int x, bool isRightToLeft) + { + if (isRightToLeft) { + rect.adjust(0, 0, -x, 0); + } else { + rect.adjust(x, 0, 0, 0); + } + } + QString m_filterString; }; @@ -399,7 +461,7 @@ private: int horizontalMargin(const QStyleOptionViewItem &option) const { - return option.widget->style()->pixelMetric(QStyle::PM_FocusFrameHMargin, &option) + 2; + return 2 * option.widget->style()->pixelMetric(QStyle::PM_FocusFrameHMargin, &option); } }; diff --git a/src/kcommandbarmodel_p.cpp b/src/kcommandbarmodel_p.cpp index 279fe4cd1fe0bda828dd988a3ecf89835bc31d3a..f9e9da0371b7fd50e7f3d2c4768aadd36e27a9b8 100644 --- a/src/kcommandbarmodel_p.cpp +++ b/src/kcommandbarmodel_p.cpp @@ -111,6 +111,20 @@ void KCommandBarModel::refresh(const QVector<KCommandBar::ActionGroup> &actionGr endResetModel(); } +Qt::ItemFlags KCommandBarModel::flags(const QModelIndex &index) const +{ + if (!index.isValid()) { + return Qt::NoItemFlags; + } + + Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren; + if (m_rows.at(index.row()).action->isCheckable()) { + flags |= Qt::ItemIsUserCheckable; + } + + return flags; +} + QVariant KCommandBarModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { @@ -140,6 +154,12 @@ QVariant KCommandBarModel::data(const QModelIndex &index, int role) const } return toolTip; } + case Qt::CheckStateRole: { + if (entry.action->isCheckable()) { + return entry.action->isChecked() ? Qt::Checked : Qt::Unchecked; + } + break; + } case Qt::UserRole: { return QVariant::fromValue(entry.action); } diff --git a/src/kcommandbarmodel_p.h b/src/kcommandbarmodel_p.h index c049d34070f588a0f0d0bc1ca4af831710bd7452..6706e7aa681b4d5ce47b4bb053e926a8a18f972f 100644 --- a/src/kcommandbarmodel_p.h +++ b/src/kcommandbarmodel_p.h @@ -53,6 +53,8 @@ public: return Column_Count; } + Qt::ItemFlags flags(const QModelIndex &index) const override; + /** * reimplemented function to update score that is calculated by KFuzzyMatcher */ diff --git a/tests/kcommandbartest.cpp b/tests/kcommandbartest.cpp index 1c631bbba35806574db36bae4a55484113d26dbd..eeaf94dace751a9d51b3bd577ae9b11ee93dba03 100644 --- a/tests/kcommandbartest.cpp +++ b/tests/kcommandbartest.cpp @@ -106,6 +106,7 @@ public: acts[1].actions[0]->setShortcut(QStringLiteral("G")); acts[1].actions[1]->setCheckable(true); acts[1].actions[1]->setShortcut(QStringLiteral("Ctrl++")); + acts[1].actions[1]->setCheckable(true); acts[2].name = QStringLiteral("Second Menu Group - Disabled acts"); for (; i < 4; ++i) { @@ -121,6 +122,7 @@ public: } acts[3].actions[0]->setCheckable(true); acts[3].actions[0]->setShortcut(QStringLiteral("Ctrl+,, Ctrl++, Ctrl+K")); + acts[3].actions[0]->setCheckable(true); return acts; } @@ -135,12 +137,14 @@ public: new QAction(QIcon::fromTheme("folder"), QStringLiteral("یہ ایک اور فولڈر ایکشن ہے"), this)}; acts[0].actions[1]->setCheckable(true); acts[0].actions[1]->setShortcut(QStringLiteral("Ctrl+Shift++")); + acts[0].actions[1]->setCheckable(true); acts[1].name = QStringLiteral("گروپ"); acts[1].actions = {new QAction(QIcon::fromTheme("zoom-out"), QStringLiteral("یہ فولڈر ایکشن ہے"), this), new QAction(QIcon::fromTheme("security-low"), QStringLiteral("یہ ایک اور فولڈر ایکشن ہے"), this)}; acts[1].actions[1]->setCheckable(true); acts[1].actions[1]->setShortcut(QStringLiteral("Ctrl+-")); + acts[1].actions[1]->setCheckable(true); return acts; }
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