Changeset View
Changeset View
Standalone View
Standalone View
plugins/problemreporter/actionmenu.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Anton Anikin <anton@anikin.xyz> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or modify | ||||
5 | * it under the terms of the GNU Library General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of the | ||||
7 | * License, or (at your option) any later version. | ||||
8 | * | ||||
9 | * This program is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | * GNU General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU General Public | ||||
15 | * License along with this program; if not, write to the | ||||
16 | * Free Software Foundation, Inc., | ||||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "actionmenu.h" | ||||
21 | | ||||
22 | #include <KLocalizedString> | ||||
23 | | ||||
24 | #include <QApplication> | ||||
25 | #include <QHelpEvent> | ||||
26 | #include <QMenu> | ||||
27 | #include <QToolButton> | ||||
28 | #include <QToolTip> | ||||
29 | #include <QStyleOption> | ||||
30 | #include <QStylePainter> | ||||
31 | | ||||
32 | namespace KDevelop | ||||
33 | { | ||||
34 | | ||||
35 | class ActionMenuButton : public QToolButton | ||||
36 | { | ||||
37 | Q_OBJECT | ||||
38 | | ||||
39 | public: | ||||
40 | ActionMenuButton(const QString& prefix, QWidget* parent = nullptr) | ||||
41 | : QToolButton(parent) | ||||
42 | , m_prefix(prefix) | ||||
43 | { | ||||
44 | setPopupMode(QToolButton::InstantPopup); | ||||
45 | setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); | ||||
46 | } | ||||
47 | | ||||
48 | ~ActionMenuButton() override | ||||
49 | { | ||||
50 | } | ||||
51 | | ||||
52 | QSize sizeHint() const override | ||||
53 | { | ||||
54 | auto fm = fontMetrics(); | ||||
55 | int textHeight = 0; | ||||
56 | int textWidth = 0; | ||||
57 | int spaceWidth = fm.width(QLatin1Char(' ')); | ||||
58 | | ||||
59 | for (auto action : menu()->actions()) | ||||
60 | { | ||||
61 | auto textSize = fm.size( | ||||
62 | Qt::TextShowMnemonic, | ||||
63 | QStringLiteral("%1: %2").arg(m_prefix, action->text())); | ||||
64 | | ||||
65 | textHeight = qMax(textHeight, textSize.height()); | ||||
66 | textWidth = qMax(textWidth, textSize.width()); | ||||
67 | } | ||||
68 | | ||||
69 | QStyleOptionToolButton opt; | ||||
70 | initStyleOption(&opt); | ||||
71 | opt.rect.setHeight(textHeight); | ||||
72 | opt.rect.setWidth(textWidth + 2 * spaceWidth); | ||||
73 | | ||||
74 | auto fullSize = style()->sizeFromContents(QStyle::CT_ToolButton, &opt, opt.rect.size(), this). | ||||
75 | expandedTo(QApplication::globalStrut()); | ||||
76 | | ||||
77 | m_margin = (fullSize.width() - opt.rect.width()) / 2 + spaceWidth; | ||||
78 | | ||||
79 | return fullSize; | ||||
80 | } | ||||
81 | | ||||
82 | void paintEvent(QPaintEvent* event) override | ||||
83 | { | ||||
84 | Q_UNUSED(event); | ||||
85 | | ||||
86 | QStylePainter sp(this); | ||||
87 | QStyleOptionToolButton opt; | ||||
88 | initStyleOption(&opt); | ||||
89 | | ||||
90 | opt.text.clear(); | ||||
91 | opt.icon = QIcon(); | ||||
92 | sp.drawComplexControl(QStyle::CC_ToolButton, opt); | ||||
93 | | ||||
94 | opt.rect.adjust(m_margin, 0, 0, 0); | ||||
95 | sp.drawText(opt.rect, Qt::AlignLeft | Qt::AlignVCenter, text()); | ||||
96 | } | ||||
97 | | ||||
98 | protected: | ||||
99 | QString m_prefix; | ||||
100 | mutable int m_margin; | ||||
101 | }; | ||||
102 | | ||||
103 | ActionMenu::ActionMenu(const QString& prefix, QObject* parent) | ||||
104 | : QWidgetAction(parent) | ||||
105 | , m_prefix(prefix) | ||||
106 | , m_group(new QActionGroup(this)) | ||||
107 | { | ||||
108 | setMenu(new QMenu); | ||||
109 | menu()->installEventFilter(this); | ||||
110 | } | ||||
111 | | ||||
112 | ActionMenu::~ActionMenu() | ||||
113 | { | ||||
114 | delete menu(); | ||||
115 | } | ||||
116 | | ||||
117 | QAction* ActionMenu::addAction(const QString& text, const QString& toolTip, int id) | ||||
118 | { | ||||
119 | auto action = new QAction(text, this); | ||||
120 | action->setToolTip(toolTip); | ||||
121 | action->setCheckable(true); | ||||
122 | | ||||
123 | m_mapper[id] = action; | ||||
124 | | ||||
125 | connect(action, &QAction::triggered, | ||||
126 | this, [this, id]() { | ||||
127 | emit actionSelected(id); | ||||
128 | }); | ||||
129 | | ||||
130 | menu()->addAction(action); | ||||
131 | m_group->addAction(action); | ||||
132 | | ||||
133 | return action; | ||||
134 | } | ||||
135 | | ||||
136 | void ActionMenu::selectAction(int id) | ||||
137 | { | ||||
138 | if (auto action = m_mapper.value(id, nullptr)) { | ||||
139 | action->setChecked(true); | ||||
140 | setText(QStringLiteral("%1: %2").arg(m_prefix, action->text())); | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
144 | QWidget* ActionMenu::createWidget(QWidget* parent) | ||||
145 | { | ||||
146 | auto button = new ActionMenuButton(m_prefix, parent); | ||||
147 | button->setMenu(menu()); | ||||
148 | button->setDefaultAction(this); | ||||
149 | | ||||
150 | return button; | ||||
151 | } | ||||
152 | | ||||
153 | bool ActionMenu::eventFilter(QObject* obj, QEvent* event) | ||||
154 | { | ||||
155 | if (obj == menu()) { | ||||
156 | if (event->type() == QEvent::ToolTip && menu()->activeAction()) { | ||||
157 | auto helpEvent = static_cast<QHelpEvent*>(event); | ||||
158 | QToolTip::showText(helpEvent->globalPos(), menu()->activeAction()->toolTip()); | ||||
159 | } else { | ||||
160 | QToolTip::hideText(); | ||||
161 | } | ||||
162 | } | ||||
163 | | ||||
164 | return QObject::eventFilter(obj, event); | ||||
165 | } | ||||
166 | | ||||
167 | } | ||||
168 | | ||||
169 | #include "actionmenu.moc" |