File: OptionsEditor.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 (217 lines) | stat: -rw-r--r-- 9,319 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
/* 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 "OptionsEditor.h"
#include "Flowsheet.h"
#include "ParametersHolder.h"
#include "DyssolUtilities.h"
#include "SignalBlocker.h"

COptionsEditor::COptionsEditor(CFlowsheet* _pFlowsheet, CMaterialsDatabase* _pMaterialsDB, QWidget* _parent, Qt::WindowFlags _flags)
	: CQtDialog{ _parent, _flags }
	, m_pFlowsheet{ _pFlowsheet }
	, m_pParams{ m_pFlowsheet->GetParameters() }
	, m_materialsDB{ _pMaterialsDB }
{
	ui.setupUi(this);
	ui.labelWarningStreams->setVisible(false);
	ui.labelWarningHoldups->setVisible(false);
	ui.labelWarningInternal->setVisible(false);
	ui.labelWarningWindow->setVisible(false);

	SetHelpLink("001_ui/gui.html#sec-gui-menu-setup-options");
}

void COptionsEditor::InitializeConnections() const
{
	connect(ui.pushButtonOk,			  &QPushButton::clicked,	   this, &COptionsEditor::ApplyChangesAndClose);
	connect(ui.pushButtonApply,			  &QPushButton::clicked,	   this, &COptionsEditor::ApplyChanges);
	connect(ui.pushButtonCancel,		  &QPushButton::clicked,	   this, &COptionsEditor::close);
	connect(ui.checkBoxCacheStreamsFlag,  &QCheckBox::toggled,         this, &COptionsEditor::CacheStreamsFlagChanged);
	connect(ui.checkBoxCacheHoldupsFlag,  &QCheckBox::toggled,	       this, &COptionsEditor::CacheHoldupsFlagChanged);
	connect(ui.checkBoxCacheInternalFlag, &QCheckBox::toggled,	       this, &COptionsEditor::CacheInternalFlagChanged);
	connect(ui.lineEditCacheWindow,	      &QLineEdit::editingFinished, this, &COptionsEditor::CacheWindowChanged);
}

void COptionsEditor::UpdateFromFlowsheet()
{
	if (isVisible())
		UpdateWholeView();
}

void COptionsEditor::UpdateWholeView()
{
	CSignalBlocker blocker{ ui.checkBoxCacheStreamsFlag, ui.checkBoxCacheHoldupsFlag, ui.checkBoxCacheInternalFlag, ui.lineEditCacheWindow };

	ShowValueAndLabel(ui.lineEditRTol        , ui.labelRTol        , m_pParams->relTol      );
	ShowValueAndLabel(ui.lineEditATol        , ui.labelATol        , m_pParams->absTol      );
	ShowValueAndLabel(ui.lineEditMinFraction , ui.labelMinFraction , m_pParams->minFraction );
	ShowValueAndLabel(ui.lineEditSaveTimeStep, ui.labelSaveTimeStep, m_pParams->saveTimeStep);
	ShowValueAndLabel(ui.lineEditTMin        , ui.labelTMin        , m_pParams->enthalpyMinT);
	ShowValueAndLabel(ui.lineEditTMax        , ui.labelTMax        , m_pParams->enthalpyMaxT);
	ShowValueAndLabel(ui.lineEditTIntervals  , ui.labelTIntervals  , m_pParams->enthalpyInt );
	ui.checkBoxSaveTimeStepHoldup->setChecked(m_pParams->saveTimeStepFlagHoldups);

	ShowValueAndLabel(ui.lineEditInitialWindow, ui.labelInitialWindow, m_pParams->initTimeWindow    );
	ShowValueAndLabel(ui.lineEditMinWindow    , ui.labelMinWindow    , m_pParams->minTimeWindow     );
	ShowValueAndLabel(ui.lineEditMaxWindow    , ui.labelMaxWindow    , m_pParams->maxTimeWindow     );
	ShowValueAndLabel(ui.lineEditMaxIterNum   , ui.labelMaxIterNum   , m_pParams->maxItersNumber    );
	ShowValueAndLabel(ui.lineEditRatio        , ui.labelRatio        , m_pParams->magnificationRatio);
	ShowValueAndLabel(ui.lineEditUpperLimit   , ui.labelUpperLimit   , m_pParams->itersUpperLimit   );
	ShowValueAndLabel(ui.lineEditLowerLimit   , ui.labelLowerLimit   , m_pParams->itersLowerLimit   );
	ShowValueAndLabel(ui.lineEdit1stUpperLimit, ui.label1stUpperLimit, m_pParams->iters1stUpperLimit);
	ShowValueAndLabel(ui.lineEditAccelParam   , ui.labelAccelParam   , m_pParams->wegsteinAccelParam);
	ShowValueAndLabel(ui.lineEditRelaxParam   , ui.labelRelaxParam   , m_pParams->relaxationParam   );
	ui.comboBoxConvMethod->setCurrentIndex(static_cast<int>(static_cast<EConvergenceMethod>(m_pParams->convergenceMethod)));
	ui.comboBoxExtrapMethod->setCurrentIndex(static_cast<int>(static_cast<EExtrapolationMethod>(m_pParams->extrapolationMethod)));

	ShowValueAndLabel(ui.lineEditCacheWindow, ui.labelCacheWindow, m_pParams->cacheWindowAfterReload);
	ui.checkBoxCacheStreamsFlag->setChecked(m_pParams->cacheFlagStreamsAfterReload);
	ui.checkBoxCacheHoldupsFlag->setChecked(m_pParams->cacheFlagHoldupsAfterReload);
	ui.checkBoxCacheInternalFlag->setChecked(m_pParams->cacheFlagInternalAfterReload);
	ui.checkBoxSplitFile->setChecked(!m_pParams->fileSingleFlag);

	UpdateCacheWindowVisible();
	UpdateWarningsVisible();

	m_cacheWindowSizeBeforeEdit = m_pParams->cacheWindowAfterReload;
}

void COptionsEditor::UpdateCacheWindowVisible() const
{
	ui.lineEditCacheWindow->setEnabled(ui.checkBoxCacheStreamsFlag->isChecked() || ui.checkBoxCacheHoldupsFlag->isChecked() || ui.checkBoxCacheInternalFlag->isChecked());
}

void COptionsEditor::UpdateWarningsVisible() const
{
	const bool bStreams = m_pParams->cacheFlagStreams != ui.checkBoxCacheStreamsFlag->isChecked();
	const bool bHoldups = m_pParams->cacheFlagHoldups != ui.checkBoxCacheHoldupsFlag->isChecked();
	const bool bInternal = m_pParams->cacheFlagInternal != ui.checkBoxCacheInternalFlag->isChecked();
	const bool bWindow = m_pParams->cacheWindow != ui.lineEditCacheWindow->text().toUInt();
	ui.labelWarningStreams->setVisible(bStreams);
	ui.labelWarningHoldups->setVisible(bHoldups);
	ui.labelWarningInternal->setVisible(bInternal);
	ui.labelWarningWindow->setVisible(bWindow);
	ui.labelWarning->setVisible(bStreams || bHoldups || bInternal || bWindow);
}

void COptionsEditor::CacheFlagChanged(QCheckBox* _checkBox, bool _currFlag)
{
	CSignalBlocker blocker{ _checkBox };

	if (_checkBox->isChecked() != _currFlag)
	{
		switch (AskReopen())
		{
		case QMessageBox::Yes:
			ApplyChanges();
			emit NeedSaveAndReopen();
			break;
		case QMessageBox::Cancel:
			_checkBox->toggle();
			break;
		default: ;
		}
	}

	UpdateCacheWindowVisible();
	UpdateWarningsVisible();
}

void COptionsEditor::CacheWindowChanged()
{
	CSignalBlocker blocker{ ui.lineEditCacheWindow };
	ui.lineEditCacheWindow->clearFocus(); // to prevent re-firing of signals

	if (ui.lineEditCacheWindow->text().toUInt() != m_pParams->cacheWindow)
	{
		switch (AskReopen())
		{
		case QMessageBox::Yes:
			ApplyChanges();
			emit NeedSaveAndReopen();
			break;
		case QMessageBox::No:
			m_cacheWindowSizeBeforeEdit = ui.lineEditCacheWindow->text().toUInt();
			break;
		case QMessageBox::Cancel:
			ui.lineEditCacheWindow->setText(QString::number(m_cacheWindowSizeBeforeEdit));
			break;
		default:;
		}
	}

	UpdateWarningsVisible();
}

QMessageBox::StandardButton COptionsEditor::AskReopen()
{
	return QMessageBox::question(this, "Confirm reopen",
		"To apply this setting, the current flowsheet must be saved and reopened. Save and reopen now?",
		QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
}

void COptionsEditor::CacheStreamsFlagChanged()
{
	CacheFlagChanged(ui.checkBoxCacheStreamsFlag, m_pParams->cacheFlagStreams);
}

void COptionsEditor::CacheHoldupsFlagChanged()
{
	CacheFlagChanged(ui.checkBoxCacheHoldupsFlag, m_pParams->cacheFlagHoldups);
}

void COptionsEditor::CacheInternalFlagChanged()
{
	CacheFlagChanged(ui.checkBoxCacheInternalFlag, m_pParams->cacheFlagInternal);
}

void COptionsEditor::ApplyChanges()
{
	m_pParams->RelTol(ReadValue(ui.lineEditRTol));
	m_pParams->AbsTol(ReadValue(ui.lineEditATol));
	m_pParams->MinFraction(ReadValue(ui.lineEditMinFraction));
	m_pParams->SaveTimeStep(ReadValue(ui.lineEditSaveTimeStep));
	m_pParams->EnthalpyMinT(ReadValue(ui.lineEditTMin));
	m_pParams->EnthalpyMaxT(ReadValue(ui.lineEditTMax));
	m_pParams->EnthalpyInt(static_cast<uint32_t>(ReadValue(ui.lineEditTIntervals)));
	m_pParams->SaveTimeStepFlagHoldups(ui.checkBoxSaveTimeStepHoldup->isChecked());

	m_pParams->InitTimeWindow(ReadValue(ui.lineEditInitialWindow));
	m_pParams->MinTimeWindow(ReadValue(ui.lineEditMinWindow));
	m_pParams->MaxTimeWindow(ReadValue(ui.lineEditMaxWindow));
	m_pParams->MaxItersNumber(static_cast<uint32_t>(ReadValue(ui.lineEditMaxIterNum)));
	m_pParams->MagnificationRatio(ReadValue(ui.lineEditRatio));
	m_pParams->ItersUpperLimit(static_cast<uint32_t>(ReadValue(ui.lineEditUpperLimit)));
	m_pParams->ItersLowerLimit(static_cast<uint32_t>(ReadValue(ui.lineEditLowerLimit)));
	m_pParams->Iters1stUpperLimit(static_cast<uint32_t>(ReadValue(ui.lineEdit1stUpperLimit)));
	m_pParams->WegsteinAccelParam(ReadValue(ui.lineEditAccelParam));
	m_pParams->RelaxationParam(ReadValue(ui.lineEditRelaxParam));
	m_pParams->ConvergenceMethod(static_cast<EConvergenceMethod>(ui.comboBoxConvMethod->currentIndex()));
	m_pParams->ExtrapolationMethod(static_cast<EExtrapolationMethod>(ui.comboBoxExtrapMethod->currentIndex()));

	m_pParams->CacheWindowAfterReload(static_cast<uint32_t>(ReadValue(ui.lineEditCacheWindow)));
	m_pParams->CacheFlagStreamsAfterReload(ui.checkBoxCacheStreamsFlag->isChecked());
	m_pParams->CacheFlagHoldupsAfterReload(ui.checkBoxCacheHoldupsFlag->isChecked());
	m_pParams->CacheFlagInternalAfterReload(ui.checkBoxCacheInternalFlag->isChecked());
	m_pParams->FileSingleFlag(!ui.checkBoxSplitFile->isChecked());

	m_pFlowsheet->UpdateToleranceSettings();
	m_pFlowsheet->UpdateThermodynamicsSettings();
	emit DataChanged();
	UpdateWholeView();
}

void COptionsEditor::ApplyChangesAndClose()
{
	ApplyChanges();
	QDialog::accept();
}

void COptionsEditor::setVisible(bool _bVisible)
{
	if (_bVisible && !isVisible())
		UpdateWholeView();
	CQtDialog::setVisible(_bVisible);
}