Debian Codegraph @

src/widgets/CustomToolButton.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2016 Damir Porobic <https://github.com/damirporobic>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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 License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21
22 #include "CustomToolButton.h"
23
24 CustomToolButton::CustomToolButton(QWidget* parent) : QToolButton(parent)
25 {
26 setPopupMode(QToolButton::MenuButtonPopup);
27 connect(this, &CustomToolButton::triggered, this, &CustomToolButton::setDefaultAction);
28 }
29
30 //
31 // Public Functions
32 //
33
34 /*
35 * Function used to set main button text, overriding the usual behavior where
36 * the text of the selected action is set.
37 */
38 void CustomToolButton::setButtonText(const QString& text)
39 {
40 mButtonText = text;
41 refreshText();
42 }
43
44 //
45 // Public Slots
46 //
47
48 /*
49 * Overriding setDefaultAction function to prevent changing of text when a new
50 * action is selected, the main text on the button is supposed to stay the same.
51 */
52 void CustomToolButton::setDefaultAction(QAction* action)
53 {
54 QToolButton::setDefaultAction(action);
55 refreshText();
56 }
57
58 void CustomToolButton::trigger()
59 {
60 if (defaultAction() != nullptr) {
61 defaultAction()->trigger();
62 }
63 }
64
65 void CustomToolButton::refreshText()
66 {
67 QToolButton::setText(mButtonText);
68 }
69
70 CustomMenu::CustomMenu(QWidget* parent) : QMenu(parent)
71 {
72 if (auto p = dynamic_cast<CustomToolButton*>(parent)) {
73 connect(this, &CustomMenu::triggered, p, &CustomToolButton::setDefaultAction);
74 }
75 }
76
77 void CustomMenu::showEvent(QShowEvent* event)
78 {
79 QMenu::showEvent(event);
80
81 // Workaround for Qt bug where on first time opening the button text is
82 // changed to the default action, introduced with Qt5
83 if (auto p = dynamic_cast<CustomToolButton*>(parent())) {
84 p->refreshText();
85 }
86 }