QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
gui
qgstoolbuttonaction.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
qgstoolbuttonaction.cpp
3
---------------------
4
begin : October 2026
5
copyright : (C) 2026 by Alexander Bruy
6
email : alexander dot bruy at gmail dot com
7
***************************************************************************/
8
9
/***************************************************************************
10
* *
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU General Public License as published by *
13
* the Free Software Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
15
* *
16
***************************************************************************/
17
18
#include "
qgstoolbuttonaction.h
"
19
20
#include <QMenu>
21
#include <QToolBar>
22
23
#include "moc_qgstoolbuttonaction.cpp"
24
25
QgsToolButtonAction::QgsToolButtonAction
( QObject *parent )
26
: QWidgetAction( parent )
27
{
28
// keep icon/tooltip/enabled state in sync on all live buttons
29
connect(
this
, &QAction::changed,
this
, &QgsToolButtonAction::updateButtons );
30
}
31
32
void
QgsToolButtonAction::setAutoRaise
(
bool
autoRaise )
33
{
34
mAutoRaise = autoRaise;
35
36
const
QList< QWidget * > widgets( createdWidgets() );
37
for
( QWidget *widget : std::as_const( widgets ) )
38
{
39
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
40
{
41
button->setAutoRaise( autoRaise );
42
}
43
}
44
}
45
46
void
QgsToolButtonAction::setPopupMode
( QToolButton::ToolButtonPopupMode mode )
47
{
48
mPopupMode = mode;
49
50
const
QList< QWidget * > widgets( createdWidgets() );
51
for
( QWidget *widget : std::as_const( widgets ) )
52
{
53
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
54
{
55
button->setPopupMode( mode );
56
}
57
}
58
}
59
60
void
QgsToolButtonAction::setToolButtonStyle
( Qt::ToolButtonStyle style )
61
{
62
mToolButtonStyle = style;
63
64
const
QList< QWidget * > widgets( createdWidgets() );
65
for
( QWidget *widget : std::as_const( widgets ) )
66
{
67
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
68
{
69
button->setToolButtonStyle( style );
70
}
71
}
72
}
73
74
void
QgsToolButtonAction::setMenu
( QMenu *menu )
75
{
76
mMenu = menu;
77
78
const
QList< QWidget * > widgets( createdWidgets() );
79
for
( QWidget *widget : std::as_const( widgets ) )
80
{
81
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
82
{
83
button->setMenu( menu );
84
}
85
}
86
}
87
88
void
QgsToolButtonAction::addActions
(
const
QList< QAction * > &actions )
89
{
90
for
( QAction *action : std::as_const( actions ) )
91
{
92
mButtonActions.append( action );
93
}
94
95
const
QList< QWidget * > widgets( createdWidgets() );
96
for
( QWidget *widget : std::as_const( widgets ) )
97
{
98
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
99
{
100
button->addActions( actions );
101
}
102
}
103
}
104
105
void
QgsToolButtonAction::setDefaultAction
( QAction *action )
106
{
107
if
( !action )
108
{
109
return
;
110
}
111
112
mDefaultAction = action;
113
114
const
QList< QWidget * > widgets( createdWidgets() );
115
for
( QWidget *widget : std::as_const( widgets ) )
116
{
117
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
118
{
119
button->setDefaultAction( action );
120
}
121
}
122
}
123
124
QAction *
QgsToolButtonAction::defaultAction
()
const
125
{
126
return
mDefaultAction;
127
}
128
129
QWidget *
QgsToolButtonAction::createWidget
( QWidget *parent )
130
{
131
QToolButton *button =
new
QToolButton( parent );
132
button->setAutoRaise( mAutoRaise );
133
button->setPopupMode( mPopupMode );
134
button->setToolButtonStyle( mToolButtonStyle );
135
136
bool
isInExtensionPopup =
false
;
137
for
( QWidget *w = parent; w; w = w->parentWidget() )
138
{
139
if
( qobject_cast< QMenu * >( w ) )
140
{
141
isInExtensionPopup =
true
;
142
break
;
143
}
144
}
145
146
if
( isInExtensionPopup )
147
{
148
button->setToolButtonStyle( Qt::ToolButtonStyle::ToolButtonTextBesideIcon );
149
}
150
else
151
{
152
button->setToolButtonStyle( mToolButtonStyle );
153
}
154
155
if
( QToolBar *toolBar = qobject_cast< QToolBar * >( parent ) )
156
{
157
button->setIconSize( toolBar->iconSize() );
158
connect( toolBar, &QToolBar::iconSizeChanged, button, &QToolButton::setIconSize );
159
160
if
( !isInExtensionPopup )
161
{
162
button->setToolButtonStyle( toolBar->toolButtonStyle() );
163
connect( toolBar, &QToolBar::toolButtonStyleChanged, button, &QToolButton::setToolButtonStyle );
164
}
165
}
166
167
if
( mMenu )
168
{
169
button->setMenu( mMenu );
170
}
171
172
if
( !mButtonActions.isEmpty() )
173
{
174
button->addActions( mButtonActions );
175
}
176
177
if
( mDefaultAction )
178
{
179
button->setDefaultAction( mDefaultAction );
180
}
181
else
182
{
183
applyState( button );
184
connect( button, &QToolButton::clicked,
this
, &QAction::trigger );
185
}
186
187
return
button;
188
}
189
190
void
QgsToolButtonAction::applyState( QToolButton *button )
const
191
{
192
button->setEnabled( isEnabled() );
193
194
if
( !mDefaultAction )
195
{
196
button->setIcon( icon() );
197
button->setText( text() );
198
button->setToolTip( toolTip() );
199
}
200
}
201
202
void
QgsToolButtonAction::updateButtons()
203
{
204
const
QList< QWidget * > widgets( createdWidgets() );
205
for
( QWidget *widget : std::as_const( widgets ) )
206
{
207
if
(
auto
*button = qobject_cast<QToolButton *>( widget ) )
208
{
209
applyState( button );
210
}
211
}
212
}
QgsToolButtonAction::addActions
void addActions(const QList< QAction * > &actions)
Appends actions to the button's list of actions.
Definition
qgstoolbuttonaction.cpp:88
QgsToolButtonAction::setPopupMode
void setPopupMode(QToolButton::ToolButtonPopupMode mode)
Sets the popup mode to use for button's popup menu.
Definition
qgstoolbuttonaction.cpp:46
QgsToolButtonAction::defaultAction
QAction * defaultAction() const
Returns the default action.
Definition
qgstoolbuttonaction.cpp:124
QgsToolButtonAction::setMenu
void setMenu(QMenu *menu)
Associates a menu with this tool button.
Definition
qgstoolbuttonaction.cpp:74
QgsToolButtonAction::QgsToolButtonAction
QgsToolButtonAction(QObject *parent=nullptr)
Constructor for QgsToolButtonAction.
Definition
qgstoolbuttonaction.cpp:25
QgsToolButtonAction::setToolButtonStyle
void setToolButtonStyle(Qt::ToolButtonStyle style)
Sets the toolbutton style, e.g.
Definition
qgstoolbuttonaction.cpp:60
QgsToolButtonAction::createWidget
QWidget * createWidget(QWidget *parent) override
Definition
qgstoolbuttonaction.cpp:129
QgsToolButtonAction::setDefaultAction
void setDefaultAction(QAction *action)
Sets the default action to action.
Definition
qgstoolbuttonaction.cpp:105
QgsToolButtonAction::setAutoRaise
void setAutoRaise(bool autoRaise)
Sets whether auto-raise is enabled.
Definition
qgstoolbuttonaction.cpp:32
qgstoolbuttonaction.h
Generated on
for QGIS API Documentation by
1.17.0