File: FlowsheetViewer.cpp

package info (click to toggle)
dyssol 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,184 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (230 lines) | stat: -rw-r--r-- 8,253 bytes parent folder | download | duplicates (2)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/* Copyright (c) 2021, Dyssol Development Team.
 * Copyright (c) 2023, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "FlowsheetViewer.h"
#include "Flowsheet.h"
#include "DyssolUtilities.h"
#include "DyssolStringConstants.h"
#include <QImageReader>
#include <QMenuBar>
#include <QFileDialog>
#include <QScrollBar>
#include <QWheelEvent>
#include <QSettings>

namespace fs = std::filesystem;

CFlowsheetViewer::CFlowsheetViewer(const CFlowsheet* _flowsheet, QWidget* _parent)
	: QDialog{ _parent }
	, m_flowsheet{ _flowsheet }
{
	ui.setupUi(this);
	ui.scrollArea->viewport()->setStyleSheet("background-color: white;");
	ui.scrollArea->viewport()->installEventFilter(this);
	setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);

	InitializeConnections();
}

CFlowsheetViewer::~CFlowsheetViewer()
{
	try
	{
		// remove image
		if (fs::exists(m_imageFullName.toStdU16String()))
			fs::remove(m_imageFullName.toStdU16String());
	}
	catch (...)
	{
		std::cerr << "Exception thrown in CFlowsheetViewer::~CFlowsheetViewer()" << std::endl;
	}
}

void CFlowsheetViewer::SetPointers(CFlowsheet* _flowsheet, CModelsManager* _modelsManager, QSettings* _settings)
{
	CDyssolBaseWidget::SetPointers(_flowsheet, _modelsManager, _settings);

	// where to temporary store images
	const std::filesystem::path cachePath = m_settings->value(StrConst::Dyssol_ConfigCachePath).toString().toStdString() + StrConst::Dyssol_CacheDir;
	if (!fs::exists(cachePath))
		fs::create_directory(cachePath);
	const fs::path path = fs::exists(cachePath) ? cachePath : fs::current_path();
	m_imageFullName = QString::fromStdU16String((path / (StringFunctions::GenerateRandomKey() + ".png")).u16string());

	LoadSettings();
	CreateMenu();
}

void CFlowsheetViewer::InitializeConnections() const
{
	connect(ui.actionSaveAs                     , &QAction::triggered      , this, &CFlowsheetViewer::SaveAs);
	connect(ui.actionFitToWindow                , &QAction::triggered      , this, &CFlowsheetViewer::FitToWindow);
	connect(ui.actionShowPorts                  , &QAction::triggered      , this, &CFlowsheetViewer::StyleChanged);
	connect(ui.actionHorizontalLayout           , &QAction::triggered      , this, &CFlowsheetViewer::StyleChanged);
	connect(ui.actionVerticalLayout             , &QAction::triggered      , this, &CFlowsheetViewer::StyleChanged);
	connect(ui.scrollArea->horizontalScrollBar(), &QScrollBar::rangeChanged, this, &CFlowsheetViewer::UpdateCursor);
	connect(ui.scrollArea->verticalScrollBar()  , &QScrollBar::rangeChanged, this, &CFlowsheetViewer::UpdateCursor);
}

void CFlowsheetViewer::setVisible(bool _visible)
{
	QDialog::setVisible(_visible);
	if (_visible)
		Update();
}

void CFlowsheetViewer::Update()
{
	if (!isVisible()) return;

	// generate and save image
	if (!m_graphBuilder.SaveToFile(m_imageFullName.toStdString())) return;
	// load image
	m_image = QImageReader{ m_imageFullName }.read();
	// show image with appropriate size
	FitToWindow();
}

void CFlowsheetViewer::CreateMenu()
{
	auto* mainMenu = new QMenuBar{ this };
	ui.mainLayout->setMenuBar(mainMenu);
	auto* fileMenu = mainMenu->addMenu("&File");
	fileMenu->addAction(ui.actionSaveAs);
	auto* viewMenu = mainMenu->addMenu("&View");
	viewMenu->addAction(ui.actionShowPorts);
	ui.actionShowPorts->setChecked(m_graphBuilder.Style() == CGraphvizHandler::EStyle::WITH_PORTS);
	auto* layoutMenu = viewMenu->addMenu("&Layout");
	layoutMenu->addAction(ui.actionHorizontalLayout);
	layoutMenu->addAction(ui.actionVerticalLayout);
	auto* layoutGroup = new QActionGroup{ this };
	layoutGroup->addAction(ui.actionHorizontalLayout);
	layoutGroup->addAction(ui.actionVerticalLayout);
	(m_graphBuilder.Layout() == CGraphvizHandler::ELayout::HORIZONTAL ? ui.actionHorizontalLayout : ui.actionVerticalLayout)->setChecked(true);
	viewMenu->addSeparator();
	viewMenu->addAction(ui.actionFitToWindow);
}

void CFlowsheetViewer::SaveAs()
{
	const auto defaultFileName = m_flowsheet->GetFileName().parent_path() / m_flowsheet->GetFileName().stem();
	const auto dir = QString::fromStdString(defaultFileName.string() + ".png");
	const QString file = QFileDialog::getSaveFileName(this, StrConst::FV_DialogSaveAs, dir, StrConst::FV_DialogSaveAsFilter);
	if (file.isEmpty()) return;
	[[maybe_unused]] const auto res = m_graphBuilder.SaveToFile(file.toStdString());
}

void CFlowsheetViewer::FitToWindow()
{
	const auto factorW = static_cast<double>(ui.scrollArea->viewport()->width () - 2 * ui.gridLayout->margin()) / static_cast<double>(m_image.width ());
	const auto factorH = static_cast<double>(ui.scrollArea->viewport()->height() - 2 * ui.gridLayout->margin()) / static_cast<double>(m_image.height());
	m_scaleFactor = std::min(factorW, factorH);
	ShowImage();
}

void CFlowsheetViewer::ScaleImage(double _factor)
{
	m_scaleFactor = std::clamp(m_scaleFactor * _factor, MIN_SCALE, MAX_SCALE);
	ShowImage();
}

void CFlowsheetViewer::ShowImage() const
{
	if (m_image.isNull()) return;
	ui.labelImage->setPixmap(QPixmap::fromImage(m_image.scaled(m_image.size() * m_scaleFactor, Qt::KeepAspectRatio, Qt::SmoothTransformation)));
	ui.labelImage->adjustSize();
}

void CFlowsheetViewer::UpdateCursor()
{
	setCursor(IsImageMovable() ? Qt::OpenHandCursor : Qt::ArrowCursor);
}

bool CFlowsheetViewer::IsImageMovable() const
{
	return ui.scrollArea->horizontalScrollBar()->maximum() || ui.scrollArea->verticalScrollBar()->maximum();
}

void CFlowsheetViewer::StyleChanged()
{
	m_graphBuilder.SetStyle(ui.actionShowPorts->isChecked() ? CGraphvizHandler::EStyle::WITH_PORTS : CGraphvizHandler::EStyle::SIMPLE);
	m_graphBuilder.SetLayout(ui.actionHorizontalLayout->isChecked() ? CGraphvizHandler::ELayout::HORIZONTAL : CGraphvizHandler::ELayout::VERTICAL);
	SaveSettings();
	Update();
}

void CFlowsheetViewer::LoadSettings()
{
	m_graphBuilder.SetStyle (static_cast<CGraphvizHandler::EStyle >(m_settings->value(StrConst::FV_ConfigStyle ).toUInt()));
	m_graphBuilder.SetLayout(static_cast<CGraphvizHandler::ELayout>(m_settings->value(StrConst::FV_ConfigLayout).toUInt()));
}

void CFlowsheetViewer::SaveSettings() const
{
	m_settings->setValue(StrConst::FV_ConfigStyle , E2I(m_graphBuilder.Style ()));
	m_settings->setValue(StrConst::FV_ConfigLayout, E2I(m_graphBuilder.Layout()));
}

void CFlowsheetViewer::mouseMoveEvent(QMouseEvent* _event)
{
	if (!IsImageMovable()) return;

	if (_event->buttons() & Qt::LeftButton)
	{
		const auto dx = _event->x() - m_lastMousePos.x();
		const auto dy = _event->y() - m_lastMousePos.y();
		ui.scrollArea->horizontalScrollBar()->setValue(ui.scrollArea->horizontalScrollBar()->value() - dx);
		ui.scrollArea->verticalScrollBar()  ->setValue(ui.scrollArea->verticalScrollBar()  ->value() - dy);
	}

	m_lastMousePos = _event->pos();
}

void CFlowsheetViewer::mousePressEvent(QMouseEvent* _event)
{
	if (!IsImageMovable()) return;
	setCursor(Qt::ClosedHandCursor);
	m_lastMousePos = _event->pos();
}

void CFlowsheetViewer::mouseReleaseEvent(QMouseEvent* _event)
{
	if (!IsImageMovable()) return;
	setCursor(Qt::OpenHandCursor);
	QDialog::mouseReleaseEvent(_event);
}

void CFlowsheetViewer::wheelEvent(QWheelEvent* _event)
{
	const double steps = _event->angleDelta().y() / 120.0;	// number of zooming steps
	const auto zoomFactor = steps > 0.0 ? ZOOM_IN_FACTOR : ZOOM_OUT_FACTOR;
	m_scaleFactor = std::clamp(m_scaleFactor * std::pow(zoomFactor, std::abs(steps)), MIN_SCALE, MAX_SCALE);
	ShowImage();
}

void CFlowsheetViewer::resizeEvent(QResizeEvent* _event)
{
	UpdateCursor();
	QDialog::resizeEvent(_event);
}

void CFlowsheetViewer::changeEvent(QEvent* _event)
{
	if (_event->type() == QEvent::WindowStateChange)
	{
		const auto prev = dynamic_cast<QWindowStateChangeEvent*>(_event)->oldState();
		const auto curr = windowState();
		if (prev == Qt::WindowNoState && curr == Qt::WindowMaximized || prev == Qt::WindowMaximized && curr == Qt::WindowNoState)
			FitToWindow();
	}
	QDialog::changeEvent(_event);
}

bool CFlowsheetViewer::eventFilter(QObject* _object, QEvent* _event)
{
	// consume wheel event from scroll area to disable scrolling with the wheel
	if (_object == ui.scrollArea->viewport() && _event->type() == QEvent::Wheel)
		return true;
	return false;
}