File: PropertyEditor.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 (231 lines) | stat: -rw-r--r-- 7,660 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
231
/* Copyright (c) 2020, Dyssol Development Team.
 * Copyright (c) 2023, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "PropertyEditor.h"
#include "CorrelationEditor.h"
#include <QMessageBox>

CPropertyEditor::CPropertyEditor(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);
	ui.plot->SetLegendPosition(POSITION_TOP_RIGHT);
	m_pProperty = nullptr;
	m_bAvoidSignal = false;
	InitializeConnections();
}

void CPropertyEditor::InitializeConnections() const
{
	// signals from buttons
	connect(ui.buttonAdd,		&QPushButton::clicked,		this, &CPropertyEditor::AddCorrelation);
	connect(ui.buttonRemove,	&QPushButton::clicked,		this, &CPropertyEditor::RemoveCorrelation);
	connect(ui.buttonUp,		&QPushButton::clicked,		this, &CPropertyEditor::UpCorrelation);
	connect(ui.buttonDown,		&QPushButton::clicked,		this, &CPropertyEditor::DownCorrelation);

	// signals from plot
	connect(ui.radioButtonT,	&QRadioButton::clicked,		this, &CPropertyEditor::PlotTypeChanged);
	connect(ui.radioButtonP,	&QRadioButton::clicked,		this, &CPropertyEditor::PlotTypeChanged);
	connect(ui.linePlotValue,	&QLineEdit::textChanged,	this, &CPropertyEditor::UpdatePlot);

	// signals from calculator
	connect(ui.lineCalcT,		&QLineEdit::textChanged,	this, &CPropertyEditor::UpdateCalculator);
	connect(ui.lineCalcP,		&QLineEdit::textChanged,	this, &CPropertyEditor::UpdateCalculator);
}

void CPropertyEditor::setVisible(const bool _bVisible)
{
	if (_bVisible && !this->isVisible())
		UpdateWholeView();
	QWidget::setVisible(_bVisible);
}

void CPropertyEditor::SetProperty(CTPDProperty* _pProperty)
{
	if(_pProperty)
	{
		m_pProperty = _pProperty;
		UpdateWholeView();
	}
	else
		Clear();
}

void CPropertyEditor::Clear()
{
	m_bAvoidSignal = true;
	ui.listCorrelations->clear();
	ui.radioButtonT->setChecked(true);
	ui.plot->ClearPlot();
	ui.lineCalcT->clear();
	ui.lineCalcP->clear();
	ui.lineCalcVal->clear();
	m_bAvoidSignal = false;
}

void CPropertyEditor::UpdateWholeView()
{
	UpdateCorrelations();
	UpdatePropertyName();
	UpdateCalculator();
	UpdatePlot();
	PlotTypeChanged();
}

void CPropertyEditor::UpdateCorrelations()
{
	if (!m_pProperty) return;
	m_bAvoidSignal = true;

	ui.listCorrelations->clear();
	ui.listCorrelations->setColumnCount(1);
	ui.listCorrelations->setRowCount(static_cast<int>(m_pProperty->CorrelationsNumber()));
	for (size_t i = 0; i < m_pProperty->CorrelationsNumber(); ++i)
	{
		auto* pEditor = new CCorrelationEditor(m_pProperty->GetCorrelation(i), ui.listCorrelations);
		ui.listCorrelations->setCellWidget(static_cast<int>(i), 0, pEditor);
		connect(pEditor, &CCorrelationEditor::CorrelationChanged, this, &CPropertyEditor::CorrelationChanged);
	}
	ui.listCorrelations->resizeRowsToContents();

	m_bAvoidSignal = false;
}

void CPropertyEditor::UpdatePropertyName() const
{
	if (!m_pProperty)
		ui.labelPropertyName->clear();
	else
		ui.labelPropertyName->setText(QString::fromStdString(m_pProperty->GetName()) + " [" + QString::fromStdWString(m_pProperty->GetUnits()) + "]:");
}

void CPropertyEditor::UpdateCalculator() const
{
	if (!m_pProperty) return;
	if (m_bAvoidSignal) return;
	if (ui.lineCalcT->text().isEmpty() || ui.lineCalcP->text().isEmpty())
	{
		ui.lineCalcVal->clear();
		return;
	}
	const double dT = ui.lineCalcT->text().toDouble();
	const double dP = ui.lineCalcP->text().toDouble();
	const double dV = m_pProperty->GetValue(dT, dP);
	ui.lineCalcVal->setText(QString::number(dV));
}

void CPropertyEditor::UpdatePlot() const
{
	static unsigned pointsNumber = 1000;

	if (!m_pProperty) return;
	if (m_bAvoidSignal) return;
	ui.plot->ClearPlot();

	const bool bTemperature = ui.radioButtonT->isChecked();
	for (size_t i = 0; i < m_pProperty->CorrelationsNumber(); ++i)
	{
		const CCorrelation *pCorrelation = m_pProperty->GetCorrelation(i);
		const double dV1 = bTemperature ? pCorrelation->GetTInterval().min : pCorrelation->GetPInterval().min;
		const double dV2 = bTemperature ? pCorrelation->GetTInterval().max : pCorrelation->GetPInterval().max;
		const double dStep = (dV2 - dV1) / pointsNumber;
		const SInterval interv = bTemperature ? pCorrelation->GetPInterval() : pCorrelation->GetTInterval();
		const double dCurrSecond = ui.linePlotValue->text().toDouble();
		if ((bTemperature && !pCorrelation->IsPInInterval(dCurrSecond)) || (!bTemperature && !pCorrelation->IsTInInterval(dCurrSecond)))
			continue;
		const QString sName = "#" + QString::number(i + 1) + (bTemperature ? ": P=" : ": T=") + QString::number(dCurrSecond) + ", [" + QString::number(interv.min) + ".." + QString::number(interv.max) + (bTemperature ? "] Pa" : "] K");
		const QColor color = m_pProperty->CorrelationsNumber() == 1 ? Qt::blue : Qt::GlobalColor(Qt::red + i % (Qt::transparent - Qt::red));
		QtPlot::SCurve* pCurve = new QtPlot::SCurve(sName, color, 3, true, true, bTemperature ? QtPlot::LABEL_TEMPERATURE : QtPlot::LABEL_PRESSURE, QtPlot::LABEL_MANUAL);
		const unsigned nCurve = ui.plot->AddCurve(pCurve);

		if(pCorrelation->GetType() != ECorrelationTypes::LIST_OF_T_VALUES && pCorrelation->GetType() != ECorrelationTypes::LIST_OF_P_VALUES)
		{
			for (unsigned j = 0; j <= pointsNumber; ++j)
			{
				const double dCurrT = bTemperature ? dV1 + dStep*j : dCurrSecond;
				const double dCurrP = !bTemperature ? dV1 + dStep*j : dCurrSecond;
				ui.plot->AddPoint(nCurve, QPointF(dV1 + dStep*j, pCorrelation->GetValue(dCurrT, dCurrP)));
			}
		}
		else
		{
			std::vector<double> vParams = pCorrelation->GetParameters();
			for (size_t j = 0; j < vParams.size(); j+=2)
			{
				const double dCurrT = bTemperature ? vParams[j] : dCurrSecond;
				const double dCurrP = !bTemperature ? vParams[j] : dCurrSecond;
				ui.plot->AddPoint(nCurve, QPointF(vParams[j], pCorrelation->GetValue(dCurrT, dCurrP)));
			}
		}
	}

	ui.plot->SetManualLabelsNames("", QString::fromStdString(m_pProperty->GetName()) + " [" + QString::fromStdWString(m_pProperty->GetUnits()) + "]");
}

void CPropertyEditor::UpdatePlotType() const
{
	if (!m_pProperty) return;
	if (m_bAvoidSignal) return;
	QSignalBlocker blocker(ui.linePlotValue);
	ui.labelPlotValue->setText(ui.radioButtonT->isChecked() ? "P [Pa]:" : "T [K]:");
	ui.linePlotValue->setText(QString::number(ui.radioButtonT->isChecked() ? STANDARD_CONDITION_P : STANDARD_CONDITION_T));
}

void CPropertyEditor::AddCorrelation()
{
	if (!m_pProperty) return;
	m_pProperty->AddCorrelation(ECorrelationTypes::CONSTANT, { 0 });
	UpdateCorrelations();
	UpdateCalculator();
	UpdatePlot();
	emit MDBPropertyChanged();
}

void CPropertyEditor::RemoveCorrelation()
{
	if (!m_pProperty) return;

	if (QMessageBox::question(this, tr("Remove correlation"), tr("Remove selected correlation?"), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel) == QMessageBox::Yes)
	{
		m_pProperty->RemoveCorrelation(ui.listCorrelations->currentRow());
		UpdateCorrelations();
		UpdateCalculator();
		UpdatePlot();
		emit MDBPropertyChanged();
	}
}

void CPropertyEditor::UpCorrelation()
{
	const int iRow = ui.listCorrelations->currentRow();
	if (m_pProperty->ShiftCorrelationUp(iRow))
	{
		UpdateCorrelations();
		ui.listCorrelations->SetCurrentCellPos(iRow - 1, 0);
		emit MDBPropertyChanged();
	}
}

void CPropertyEditor::DownCorrelation()
{
	const int iRow = ui.listCorrelations->currentRow();
	if (m_pProperty->ShiftCorrelationDown(iRow))
	{
		UpdateCorrelations();
		ui.listCorrelations->SetCurrentCellPos(iRow + 1, 0);
		emit MDBPropertyChanged();
	}
}

void CPropertyEditor::CorrelationChanged()
{
	UpdateCalculator();
	UpdatePlot();
	emit MDBPropertyChanged();
}

void CPropertyEditor::PlotTypeChanged() const
{
	UpdatePlotType();
	UpdatePlot();
}