Skip to content

Commit

Permalink
Tool bar widget: added a drop-down list widget (#2413).
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Nov 4, 2020
1 parent a9a9aaa commit c93083c
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/plugins/widget/ToolBarWidget/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ add_plugin(ToolBarWidget
../../plugininfo.cpp

src/toolbarwidget.cpp
src/toolbarwidgetdropdownlistwidgetaction.cpp
src/toolbarwidgetlabelwidgetaction.cpp
src/toolbarwidgetlineeditwidgetaction.cpp
src/toolbarwidgetplugin.cpp
Expand Down
15 changes: 15 additions & 0 deletions src/plugins/widget/ToolBarWidget/src/toolbarwidget.cpp
Expand Up @@ -22,6 +22,7 @@ along with this program. If not, see <https://gnu.org/licenses>.
//==============================================================================

#include "toolbarwidget.h"
#include "toolbarwidgetdropdownlistwidgetaction.h"
#include "toolbarwidgetlabelwidgetaction.h"
#include "toolbarwidgetlineeditwidgetaction.h"
#include "toolbarwidgetwheelwidgetaction.h"
Expand Down Expand Up @@ -77,6 +78,20 @@ QAction * ToolBarWidget::addSpacerWidgetAction(QSizePolicy::Policy pHorizontalSi

//==============================================================================

ToolBarWidgetDropDownListWidgetAction * ToolBarWidget::addDropDownListWidgetAction(QAction *pDefaultAction,
QMenu *pDropDownMenu)
{
// Add and return a drop-down list widget action

auto res = new ToolBarWidgetDropDownListWidgetAction(pDefaultAction, pDropDownMenu, this);

addAction(res);

return res;
}

//==============================================================================

ToolBarWidgetLabelWidgetAction * ToolBarWidget::addLabelWidgetAction()
{
// Add and return a label widget action
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/widget/ToolBarWidget/src/toolbarwidget.h
Expand Up @@ -38,6 +38,7 @@ namespace ToolBarWidget {

//==============================================================================

class ToolBarWidgetDropDownListWidgetAction;
class ToolBarWidgetLabelWidgetAction;
class ToolBarWidgetLineEditWidgetAction;
class ToolBarWidgetWheelWidgetAction;
Expand All @@ -53,6 +54,8 @@ class TOOLBARWIDGET_EXPORT ToolBarWidget : public QToolBar

QAction * addSpacerWidgetAction(QSizePolicy::Policy pHorizontalSizePolicy,
QSizePolicy::Policy pVerticalSizePolicy);
ToolBarWidgetDropDownListWidgetAction * addDropDownListWidgetAction(QAction *pDefaultAction,
QMenu *pDropDownMenu);
ToolBarWidgetLabelWidgetAction * addLabelWidgetAction();
ToolBarWidgetLineEditWidgetAction * addLineEditWidgetAction();
ToolBarWidgetWheelWidgetAction * addWheelWidgetAction();
Expand Down
@@ -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
//==============================================================================
@@ -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
//==============================================================================

0 comments on commit c93083c

Please sign in to comment.