Skip to content

Commit

Permalink
Merge 0e86295 into 645a0ad
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Oct 29, 2020
2 parents 645a0ad + 0e86295 commit d9dc3a5
Show file tree
Hide file tree
Showing 11 changed files with 431 additions and 81 deletions.
220 changes: 213 additions & 7 deletions src/plugins/miscellaneous/Core/src/toolbarwidget.cpp
Expand Up @@ -25,21 +25,177 @@ along with this program. If not, see <https://gnu.org/licenses>.

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

#include <QLabel>
#include <QLineEdit>
#include <QTimer>

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

namespace OpenCOR {
namespace Core {

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

ToolBarLabelWidgetAction::ToolBarLabelWidgetAction(QWidget *pParent)
: QWidgetAction(pParent)
{
}

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

QWidget * ToolBarLabelWidgetAction::createWidget(QWidget *pParent)
{
// Create and return a label widget
// Note: in some cases, to emit the created() signal directly after creating
// the label 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 QLabel(pParent);

QTimer::singleShot(0, this, std::bind(&ToolBarLabelWidgetAction::emitCreated,
this, res));

return res;
}

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

QList<QLabel *> ToolBarLabelWidgetAction::labels() const
{
// Return our created labels

QList<QLabel *> res;

for (const auto &label : createdWidgets()) {
res << static_cast<QLabel *>(label);
}

return res;
}

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

bool ToolBarLabelWidgetAction::validLabel(QLabel *pLabel) const
{
// Return whether the given label is (still) valid
// Note: this method is needed so that people who handle the created()
// signal can ensure that the label is still valid since QWidgetAction
// is in charge of creating/destroying them...

for (const auto &label : createdWidgets()) {
if (pLabel == label) {
return true;
}
}

return false;
}

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

void ToolBarLabelWidgetAction::emitCreated(QLabel *pLabel)
{
// Let people know that a label widget has been created

emit created(pLabel);
}

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

ToolBarLineEditWidgetAction::ToolBarLineEditWidgetAction(QWidget *pParent)
: QWidgetAction(pParent)
{
}

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

QWidget * ToolBarLineEditWidgetAction::createWidget(QWidget *pParent)
{
// Create and return a line edit widget
// Note: in some cases, to emit the created() signal directly after creating
// the line edit 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 QLineEdit(pParent);

connect(res, &QLineEdit::returnPressed,
this, &ToolBarLineEditWidgetAction::emitReturnPressed);

QTimer::singleShot(0, this, std::bind(&ToolBarLineEditWidgetAction::emitCreated,
this, res));

return res;
}

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

QList<QLineEdit *> ToolBarLineEditWidgetAction::lineEdits() const
{
// Return our created line edits

QList<QLineEdit *> res;

for (const auto &lineEdit : createdWidgets()) {
res << static_cast<QLineEdit *>(lineEdit);
}

return res;
}

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

bool ToolBarLineEditWidgetAction::validLineEdit(QLineEdit *pLineEdit) const
{
// Return whether the given line edit is (still) valid
// Note: this method is needed so that people who handle the created()
// signal can ensure that the line edit is still valid since
// QWidgetAction is in charge of creating/destroying them...

for (const auto &lineEdit : createdWidgets()) {
if (pLineEdit == lineEdit) {
return true;
}
}

return false;
}

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

void ToolBarLineEditWidgetAction::setText(const QString &pText)
{
// Set the text of all our created line edits

for (const auto &lineEdit : lineEdits()) {
lineEdit->setText(pText);
}
}

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

void ToolBarLineEditWidgetAction::emitCreated(QLineEdit *pLineEdit)
{
// Let people know that a line edit widget has been created

emit created(pLineEdit);
}

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

void ToolBarLineEditWidgetAction::emitReturnPressed()
{
// Let people know that return was pressed

emit returnPressed(qobject_cast<QLineEdit *>(sender())->text());
}

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

ToolBarWidget::ToolBarWidget(QWidget *pParent)
: QToolBar(pParent)
{
// Note: we do NOT want a parent! Indeed, if we did have a parent, then to
// start OpenCOR using the Simulation Experiment view would, on macOS,
// result in the tool bar having the wrong style!? Yet, the tool bar
// would look fine for other files, just not the one with which
// OpenCOR started!? The only case where it works as expected is when
// we don't have a parent...

// Remove the border which is normally drawn for a tool bar widget (indeed,
// it doesn't look great when on a docked window) and make sure that we have
// a spacing of 4 pixels (indeed, on Windows/Linux, the layout has no
Expand All @@ -58,6 +214,56 @@ ToolBarWidget::ToolBarWidget(QWidget *pParent)

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

QAction * ToolBarWidget::addSpacerWidgetAction(QSizePolicy::Policy pHorizontalSizePolicy,
QSizePolicy::Policy pVerticalSizePolicy)
{
// Add and return a spacer widget action

auto spacer = new QWidget(this);

spacer->setMinimumSize(0, 0);
spacer->setSizePolicy(pHorizontalSizePolicy, pVerticalSizePolicy);

return addWidget(spacer);
}

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

ToolBarLabelWidgetAction * ToolBarWidget::addLabelWidgetAction()
{
// Add and return a label widget action

auto res = new ToolBarLabelWidgetAction(this);

addAction(res);

return res;
}

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

ToolBarLineEditWidgetAction * ToolBarWidget::addLineEditWidgetAction()
{
// Add and return a line edit widget action

auto res = new ToolBarLineEditWidgetAction(this);

addAction(res);

return res;
}

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

QAction * ToolBarWidget::addWidgetAction(QWidget *pWidget)
{
// Add the given widget to ourselves

return addWidget(pWidget);
}

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

} // namespace Core
} // namespace OpenCOR

Expand Down
60 changes: 60 additions & 0 deletions src/plugins/miscellaneous/Core/src/toolbarwidget.h
Expand Up @@ -30,6 +30,12 @@ along with this program. If not, see <https://gnu.org/licenses>.
//==============================================================================

#include <QToolBar>
#include <QWidgetAction>

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

class QLabel;
class QLineEdit;

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

Expand All @@ -38,12 +44,66 @@ namespace Core {

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

class CORE_EXPORT ToolBarLabelWidgetAction : public QWidgetAction
{
Q_OBJECT

public:
ToolBarLabelWidgetAction(QWidget *pParent);

QList<QLabel *> labels() const;
bool validLabel(QLabel *pLabel) const;

protected:
QWidget * createWidget(QWidget *pParent) override;

private:
void emitCreated(QLabel *pLabel);

signals:
void created(QLabel *pLabel);
};

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

class CORE_EXPORT ToolBarLineEditWidgetAction : public QWidgetAction
{
Q_OBJECT

public:
ToolBarLineEditWidgetAction(QWidget *pParent);

QList<QLineEdit *> lineEdits() const;
bool validLineEdit(QLineEdit *pLineEdit) const;

void setText(const QString &pText);

protected:
QWidget * createWidget(QWidget *pParent) override;

private:
void emitCreated(QLineEdit *pLineEdit);
void emitReturnPressed();

signals:
void created(QLineEdit *pLineEdit);
void returnPressed(const QString &pText);
};

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

class CORE_EXPORT ToolBarWidget : public QToolBar
{
Q_OBJECT

public:
explicit ToolBarWidget(QWidget *pParent);

QAction * addSpacerWidgetAction(QSizePolicy::Policy pHorizontalSizePolicy,
QSizePolicy::Policy pVerticalSizePolicy);
ToolBarLabelWidgetAction * addLabelWidgetAction();
ToolBarLineEditWidgetAction * addLineEditWidgetAction();
QAction * addWidgetAction(QWidget *pWidget);
};

//==============================================================================
Expand Down

0 comments on commit d9dc3a5

Please sign in to comment.