Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 62 additions & 2 deletions examples/calculator/FlowchartManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

#include <QtWidgets/QAction>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QRadioButton>
#include <QtWidgets/QVBoxLayout>


Expand All @@ -20,6 +22,7 @@ QString createDefaultName(int index)

FlowchartPage::FlowchartPage(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> registry,
QString displayName,
Qt::Orientation orientation,
QWidget *parent)
: QWidget(parent)
, _model(std::move(registry))
Expand All @@ -43,6 +46,7 @@ FlowchartPage::FlowchartPage(std::shared_ptr<QtNodes::NodeDelegateModelRegistry>
layout->addWidget(controlsWidget);

_scene = new QtNodes::DataFlowGraphicsScene(_model, this);
_scene->setOrientation(orientation);
_view = new QtNodes::GraphicsView(_scene);

layout->addWidget(_view);
Expand All @@ -60,6 +64,15 @@ FlowchartPage::FlowchartPage(std::shared_ptr<QtNodes::NodeDelegateModelRegistry>
connect(_runButton, &QPushButton::clicked, this, &FlowchartPage::runFlowchart);
}

void FlowchartPage::setOrientation(Qt::Orientation orientation)
{
if (_scene)
_scene->setOrientation(orientation);

if (_view)
_view->centerScene();
}

bool FlowchartPage::save()
{
if (_scene->save()) {
Expand Down Expand Up @@ -137,6 +150,14 @@ FlowchartManager::FlowchartManager(std::shared_ptr<QtNodes::NodeDelegateModelReg
controlsLayout->addWidget(_runAllButton);
controlsLayout->addStretch();

auto *orientationLabel = new QLabel(tr("Orientation:"), controlsWidget);
_horizontalOrientationButton = new QRadioButton(tr("Horizontal"), controlsWidget);
_verticalOrientationButton = new QRadioButton(tr("Vertical"), controlsWidget);

controlsLayout->addWidget(orientationLabel);
controlsLayout->addWidget(_horizontalOrientationButton);
controlsLayout->addWidget(_verticalOrientationButton);

layout->addWidget(controlsWidget);

_tabWidget = new QTabWidget(this);
Expand All @@ -151,6 +172,14 @@ FlowchartManager::FlowchartManager(std::shared_ptr<QtNodes::NodeDelegateModelReg
connect(_runCurrentButton, &QPushButton::clicked, this, &FlowchartManager::runCurrentFlowchart);
connect(_runAllButton, &QPushButton::clicked, this, &FlowchartManager::runAllFlowcharts);

connect(_horizontalOrientationButton, &QRadioButton::clicked, this, [this]() {
setOrientation(Qt::Horizontal);
});

connect(_verticalOrientationButton, &QRadioButton::clicked, this, [this]() {
setOrientation(Qt::Vertical);
});

connect(_tabWidget, &QTabWidget::currentChanged, this, [this](int) {
updateActionsState();
});
Expand All @@ -165,6 +194,7 @@ FlowchartManager::FlowchartManager(std::shared_ptr<QtNodes::NodeDelegateModelReg
createFlowchart();

updateActionsState();
updateOrientationButtons();
}

void FlowchartManager::addFlowchart()
Expand All @@ -183,8 +213,10 @@ void FlowchartManager::saveCurrentFlowchart()
void FlowchartManager::loadCurrentFlowchart()
{
if (auto *page = currentPage()) {
if (page->load())
if (page->load()) {
updateTabText(page);
page->setOrientation(_orientation);
}
}
}

Expand Down Expand Up @@ -247,7 +279,7 @@ FlowchartPage *FlowchartManager::createFlowchart(QString const &displayName)
++_flowchartCount;
auto title = displayName.isEmpty() ? createDefaultName(_flowchartCount) : displayName;

auto *page = new FlowchartPage(_registry, title, _tabWidget);
auto *page = new FlowchartPage(_registry, title, _orientation, _tabWidget);

auto index = _tabWidget->addTab(page, title);
_tabWidget->setCurrentIndex(index);
Expand All @@ -260,10 +292,38 @@ FlowchartPage *FlowchartManager::createFlowchart(QString const &displayName)
updateTabText(page);
updateWindowModifiedFlag();
updateActionsState();
page->setOrientation(_orientation);

return page;
}

void FlowchartManager::setOrientation(Qt::Orientation orientation)
{
if (_orientation == orientation)
return;

_orientation = orientation;
updateOrientationButtons();
applyOrientationToAllPages();
}

void FlowchartManager::applyOrientationToAllPages()
{
for (int i = 0; i < _tabWidget->count(); ++i) {
if (auto *page = pageAt(i))
page->setOrientation(_orientation);
}
}

void FlowchartManager::updateOrientationButtons()
{
if (_horizontalOrientationButton)
_horizontalOrientationButton->setChecked(_orientation == Qt::Horizontal);

if (_verticalOrientationButton)
_verticalOrientationButton->setChecked(_orientation == Qt::Vertical);
}

void FlowchartManager::updateTabText(FlowchartPage *page)
{
if (!page)
Expand Down
13 changes: 13 additions & 0 deletions examples/calculator/FlowchartManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <QtNodes/GraphicsView>
#include <QtNodes/NodeDelegateModelRegistry>

#include <QtCore/Qt>

#include <QtWidgets/QMenuBar>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QWidget>
Expand All @@ -13,6 +15,7 @@

class QAction;
class QPushButton;
class QRadioButton;

class FlowchartPage : public QWidget
{
Expand All @@ -21,6 +24,7 @@ class FlowchartPage : public QWidget
public:
FlowchartPage(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> registry,
QString displayName,
Qt::Orientation orientation,
QWidget *parent = nullptr);

bool save();
Expand All @@ -32,6 +36,8 @@ class FlowchartPage : public QWidget

void setDisplayName(QString name);

void setOrientation(Qt::Orientation orientation);

public Q_SLOTS:
void runFlowchart();

Expand Down Expand Up @@ -73,6 +79,10 @@ private Q_SLOTS:

FlowchartPage *createFlowchart(QString const &displayName = QString());

void setOrientation(Qt::Orientation orientation);
void applyOrientationToAllPages();
void updateOrientationButtons();

void updateTabText(FlowchartPage *page);
void updateWindowModifiedFlag();
void updateActionsState();
Expand All @@ -87,5 +97,8 @@ private Q_SLOTS:
QAction *_closeAction = nullptr;
QPushButton *_runCurrentButton = nullptr;
QPushButton *_runAllButton = nullptr;
QRadioButton *_horizontalOrientationButton = nullptr;
QRadioButton *_verticalOrientationButton = nullptr;
Qt::Orientation _orientation = Qt::Horizontal;
int _flowchartCount = 0;
};