Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tool bar widget: added a drop-down list widget (#2413).
- Loading branch information
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/plugins/widget/ToolBarWidget/src/toolbarwidgetdropdownlistwidgetaction.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
//============================================================================== |
77 changes: 77 additions & 0 deletions
77
src/plugins/widget/ToolBarWidget/src/toolbarwidgetdropdownlistwidgetaction.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/******************************************************************************* | ||
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 | ||
//============================================================================== | ||
|
||
#pragma once | ||
|
||
//============================================================================== | ||
|
||
#include "toolbarwidgetglobal.h" | ||
|
||
//============================================================================== | ||
|
||
#include <QWidgetAction> | ||
|
||
//============================================================================== | ||
|
||
class QToolButton; | ||
|
||
//============================================================================== | ||
|
||
namespace OpenCOR { | ||
namespace ToolBarWidget { | ||
|
||
//============================================================================== | ||
|
||
class TOOLBARWIDGET_EXPORT ToolBarWidgetDropDownListWidgetAction : public QWidgetAction | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
ToolBarWidgetDropDownListWidgetAction(QAction *pDefaultAction, | ||
QMenu *pDropDownMenu, | ||
QWidget *pParent); | ||
|
||
QList<QToolButton *> dropDownLists() const; | ||
bool validDropDownList(QToolButton *pDropDownList) const; | ||
|
||
protected: | ||
QWidget * createWidget(QWidget *pParent) override; | ||
|
||
private: | ||
QAction *mDefaultAction; | ||
QMenu *mDropDownMenu; | ||
|
||
void emitCreated(QToolButton *pDropDownList); | ||
|
||
signals: | ||
void created(QToolButton *pDropDownList); | ||
}; | ||
|
||
//============================================================================== | ||
|
||
} // namespace ToolBarWidget | ||
} // namespace OpenCOR | ||
|
||
//============================================================================== | ||
// End of file | ||
//============================================================================== |