/
toolbarwidgetdropdownlistwidgetaction.cpp
118 lines (84 loc) · 4.15 KB
/
toolbarwidgetdropdownlistwidgetaction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://gnu.org/licenses>.
*******************************************************************************/
//==============================================================================
// Tool bar widget drop-down list widget action
//==============================================================================
#include "toolbarwidgetdropdownlistwidgetaction.h"
//==============================================================================
#include <QTimer>
#include <QToolButton>
//==============================================================================
namespace OpenCOR {
namespace ToolBarWidget {
//==============================================================================
ToolBarWidgetDropDownListWidgetAction::ToolBarWidgetDropDownListWidgetAction(QAction *pDefaultAction,
QMenu *pDropDownMenu,
QWidget *pParent)
: QWidgetAction(pParent),
mDefaultAction(pDefaultAction),
mDropDownMenu(pDropDownMenu)
{
}
//==============================================================================
QWidget * ToolBarWidgetDropDownListWidgetAction::createWidget(QWidget *pParent)
{
// Create and return a drop-down list widget
// Note: in some cases, to emit the created() signal directly after creating
// the drop-down list widget may result in the signal being emitted
// before a caller gets a chance to create a connection for it, hence
// we emit the signal through a single shot...
auto res = new QToolButton(pParent);
res->setDefaultAction(mDefaultAction);
res->setMenu(mDropDownMenu);
res->setPopupMode(QToolButton::MenuButtonPopup);
QTimer::singleShot(0, this, std::bind(&ToolBarWidgetDropDownListWidgetAction::emitCreated,
this, res));
return res;
}
//==============================================================================
QList<QToolButton *> ToolBarWidgetDropDownListWidgetAction::dropDownLists() const
{
// Return our created drop-down lists
QList<QToolButton *> res;
for (const auto &dropDownList : createdWidgets()) {
res << static_cast<QToolButton *>(dropDownList);
}
return res;
}
//==============================================================================
bool ToolBarWidgetDropDownListWidgetAction::validDropDownList(QToolButton *pDropDownList) const
{
// Return whether the given drop-down list is (still) valid
// Note: this method is needed so that people who handle the created()
// signal can ensure that the drop-down list is still valid since
// QWidgetAction is in charge of creating/destroying them...
for (const auto &dropDownList : createdWidgets()) {
if (pDropDownList == dropDownList) {
return true;
}
}
return false;
}
//==============================================================================
void ToolBarWidgetDropDownListWidgetAction::emitCreated(QToolButton *pDropDownList)
{
// Let people know that a drop-down list widget has been created
emit created(pDropDownList);
}
//==============================================================================
} // namespace ToolBarWidget
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================