File: DustFormationTesterTab.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 (323 lines) | stat: -rw-r--r-- 9,940 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* 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 "DustFormationTesterTab.h"
#include "Flowsheet.h"
#include "BaseUnit.h"
#include "Stream.h"
#include "MaterialsDatabase.h"

CDustFormationTesterTab::CDustFormationTesterTab(const CFlowsheet* _pFlowsheet, const CMaterialsDatabase* _matrialsDB, QWidget *_parent)
	: CQtDialog{ _parent }
	, m_pFlowsheet{ _pFlowsheet }
	, m_matrialsDB{ _matrialsDB }
{
	ui.setupUi(this);
	ui.tableWidgetData->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint | Qt::WindowMinimizeButtonHint);

	SetHelpLink("");
}

void CDustFormationTesterTab::InitializeConnections()
{
	connect(ui.toolBox,								&QToolBox::currentChanged,			this, &CDustFormationTesterTab::StreamsUnitsFocusChanged);
	connect(ui.listWidgetStreams,					&QListWidget::currentRowChanged,	this, &CDustFormationTesterTab::SelectedStreamChanged);
	connect(ui.listWidgetUnits,						&QListWidget::currentRowChanged,	this, &CDustFormationTesterTab::SelectedUnitChanged);
	connect(ui.listWidgetHoldups,					&QListWidget::currentRowChanged,	this, &CDustFormationTesterTab::SelectedHoldupChanged);
	connect(ui.lineEditPorosity,					&QLineEdit::editingFinished,		this, &CDustFormationTesterTab::PorosityChanged);
	connect(ui.lineEditMoisture,					&QLineEdit::editingFinished,		this, &CDustFormationTesterTab::MoistureChanged);
	connect(ui.lineEditMoisture90,					&QLineEdit::editingFinished,		this, &CDustFormationTesterTab::Moisture90Changed);
	connect(ui.comboBoxCompound, QOverload<int>::of(&QComboBox::currentIndexChanged),	this, &CDustFormationTesterTab::CompoundChanged);
}

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

void CDustFormationTesterTab::UpdateWholeView()
{
	if (!isVisible()) return;
	UpdateStreams();
	UpdateUnits();
	UpdateHoldups();
	UpdatePorosity();
	UpdateMoisture();
	UpdateMoisture90();
	UpdateCompounds();
	UpdateDataTable();
}

void CDustFormationTesterTab::setVisible(bool _bVisible)
{
	QDialog::setVisible(_bVisible);
	if (_bVisible)
		UpdateWholeView();
}

void CDustFormationTesterTab::UpdateStreams() const
{
	// block signals from the table
	const bool bOldBlock = ui.listWidgetStreams->blockSignals(true);

	// save selected row
	const int iOldRow = ui.listWidgetStreams->currentRow();

	// update table
	ui.listWidgetStreams->clear();
	for (const auto& stream : m_pFlowsheet->GetAllStreams())
	{
		AddItemToList(ui.listWidgetStreams, stream->GetName(), stream->GetKey());
	}

	// if streams block is selected, unblock the table here
	if (m_focusType == EType::STREAMS)
		ui.listWidgetStreams->blockSignals(bOldBlock);

	// restore selection
	RestoreSelectedRow(ui.listWidgetStreams, iOldRow);

	// if units block is selected, unblock the table here
	if (m_focusType == EType::UNITS)
		ui.listWidgetStreams->blockSignals(bOldBlock);
}

void CDustFormationTesterTab::UpdateUnits() const
{
	// block signals from the table
	const bool bOldBlock = ui.listWidgetUnits->blockSignals(true);

	// save selected row
	const int iOldRow = ui.listWidgetUnits->currentRow();

	// update table
	ui.listWidgetUnits->clear();
	for (const auto& unit : m_pFlowsheet->GetAllUnits())
	{
		AddItemToList(ui.listWidgetUnits, unit->GetName(), unit->GetKey());
	}

	// if units block is selected, unblock the table here
	if (m_focusType == EType::UNITS)
		ui.listWidgetUnits->blockSignals(bOldBlock);

	// restore selection
	RestoreSelectedRow(ui.listWidgetUnits, iOldRow);

	// if streams block is selected, unblock the table here
	if (m_focusType == EType::STREAMS)
		ui.listWidgetUnits->blockSignals(bOldBlock);
}

void CDustFormationTesterTab::UpdateHoldups() const
{
	// block signals from the table
	const bool bOldBlock = ui.listWidgetHoldups->blockSignals(true);

	// save selected row
	const int iOldRow = ui.listWidgetHoldups->currentRow();

	// clear old data
	ui.listWidgetHoldups->clear();

	// prepare stuff
	const CUnitContainer* pModel = GetSelectedModel();
	if(!pModel || !pModel->GetModel())
	{
		ui.listWidgetHoldups->blockSignals(bOldBlock);
		return;
	}

	// update table
	for (const auto& holdup : pModel->GetModel()->GetStreamsManager().GetHoldups())
	{
		AddItemToList(ui.listWidgetHoldups, holdup->GetName(), holdup->GetKey());
	}

	// if units block is selected, unblock the table here
	if (m_focusType == EType::UNITS)
		ui.listWidgetHoldups->blockSignals(bOldBlock);

	// restore selection
	RestoreSelectedRow(ui.listWidgetHoldups, iOldRow);

	// if streams block is selected, unblock the table here
	if (m_focusType == EType::STREAMS)
		ui.listWidgetHoldups->blockSignals(bOldBlock);
}

void CDustFormationTesterTab::UpdatePorosity() const
{
	QSignalBlocker blocker(ui.lineEditPorosity);
	ui.lineEditPorosity->setText(QString::number(m_tester.GetBulkPorosity()));
}

void CDustFormationTesterTab::UpdateMoisture() const
{
	QSignalBlocker blocker(ui.lineEditMoisture);
	ui.lineEditMoisture->setText(QString::number(m_tester.GetMoistureContent()));
}

void CDustFormationTesterTab::UpdateMoisture90() const
{
	QSignalBlocker blocker(ui.lineEditMoisture90);
	ui.lineEditMoisture90->setText(QString::number(m_tester.GetMoistureContent90()));
}

void CDustFormationTesterTab::UpdateCompounds() const
{
	QSignalBlocker blocker(ui.comboBoxCompound);

	// clear old
	ui.comboBoxCompound->clear();
	// create entry for mixture
	ui.comboBoxCompound->addItem("Total mixture", "");
	// add compounds
	for (const auto& key : m_pFlowsheet->GetCompounds())
		ui.comboBoxCompound->addItem(QString::fromStdString(m_matrialsDB->GetCompound(key) ? m_matrialsDB->GetCompound(key)->GetName() : ""), QString::fromStdString(key));
}

void CDustFormationTesterTab::UpdateDataTable()
{
	QSignalBlocker blocker(ui.tableWidgetData);

	// remove old data
	ui.tableWidgetData->setRowCount(0);

	// prepare stuff
	const CBaseStream* pStream = GetSelectedStream();
	if (!pStream)
		return;

	const std::vector<double> vTPs = pStream->GetAllTimePoints();
	const std::string sCompound = GetSelectedCompound();
	std::vector<std::string> vCompounds;
	if (!sCompound.empty())
		vCompounds.push_back(sCompound);
	m_tester.SetGrid(m_pFlowsheet->GetGrid().GetPSDGrid());
	ui.tableWidgetData->setRowCount(static_cast<int>(vTPs.size()));

	for (int i = 0; i < static_cast<int>(vTPs.size()); ++i)
	{
		// set PSD to tester
		m_tester.SetPSD(pStream->GetPSD(vTPs[i], PSD_MassFrac, vCompounds));
		// obtain new values
		std::vector<double> values = m_tester.GetAll();
		// set time point to table
		ui.tableWidgetData->SetItemNotEditable(i, 0, vTPs[i]);
		// set data to table
		for (int j = 0; j < static_cast<int>(values.size()); ++j)
			ui.tableWidgetData->SetItemNotEditable(i, j + 1, values[j] >= 0 ? QString::number(values[j]) : "N/A");
	}
}

void CDustFormationTesterTab::AddItemToList(QListWidget* _pList, const std::string& _sName, const std::string& _sKey)
{
	QListWidgetItem* pItem = new QListWidgetItem(QString::fromStdString(_sName));
	pItem->setData(Qt::UserRole, QString::fromStdString(_sKey));
	_pList->addItem(pItem);
}

void CDustFormationTesterTab::RestoreSelectedRow(QListWidget* _pList, int iRow)
{
	if (iRow == -1 && _pList->count() != 0)
		_pList->setCurrentRow(0, QItemSelectionModel::SelectCurrent);
	else if (iRow < _pList->count())
		_pList->setCurrentRow(iRow, QItemSelectionModel::SelectCurrent);
	else
		_pList->setCurrentRow(_pList->count() - 1, QItemSelectionModel::SelectCurrent);
}

const CBaseStream* CDustFormationTesterTab::GetSelectedStream() const
{
	switch (m_focusType)
	{
	case EType::STREAMS:	return GetSelectedMaterialStream();
	case EType::UNITS:		return GetSelectedHoldup();
	default:				return nullptr;
	}
}

const CUnitContainer* CDustFormationTesterTab::GetSelectedModel() const
{
	const QListWidgetItem* pItem = ui.listWidgetUnits->currentItem();
	if (!pItem) return nullptr;
	const std::string sKey = pItem->data(Qt::UserRole).toString().toStdString();
	return m_pFlowsheet->GetUnit(sKey);
}

const CHoldup* CDustFormationTesterTab::GetSelectedHoldup() const
{
	const CUnitContainer* pModel = GetSelectedModel();
	if (!pModel) return nullptr;
	const QListWidgetItem* pItem = ui.listWidgetHoldups->currentItem();
	if (!pItem) return nullptr;
	const std::string sKey = pItem->data(Qt::UserRole).toString().toStdString();
	const CBaseUnit* model = pModel->GetModel();
	if (!model) return nullptr;
	return model->GetStreamsManager().GetHoldup(sKey);
}

const CStream* CDustFormationTesterTab::GetSelectedMaterialStream() const
{
	const QListWidgetItem* pItem = ui.listWidgetStreams->currentItem();
	if (!pItem) return nullptr;
	const std::string sKey = pItem->data(Qt::UserRole).toString().toStdString();
	return m_pFlowsheet->GetStream(sKey);
}

std::string CDustFormationTesterTab::GetSelectedCompound() const
{
	return ui.comboBoxCompound->currentData().toString().toStdString();
}

void CDustFormationTesterTab::StreamsUnitsFocusChanged(int _index)
{
	m_focusType = static_cast<EType>(_index);
	UpdateDataTable();
}

void CDustFormationTesterTab::SelectedStreamChanged()
{
	UpdateDataTable();
}

void CDustFormationTesterTab::SelectedUnitChanged()
{
	UpdateHoldups();
	UpdateDataTable();
}

void CDustFormationTesterTab::SelectedHoldupChanged()
{
	UpdateDataTable();
}

void CDustFormationTesterTab::PorosityChanged()
{
	m_tester.SetBulkPorosity(ui.lineEditPorosity->text().toDouble());
	UpdatePorosity();
	UpdateDataTable();
}

void CDustFormationTesterTab::MoistureChanged()
{
	m_tester.SetMoistureContent(ui.lineEditMoisture->text().toDouble());
	UpdateMoisture();
	UpdateDataTable();
}

void CDustFormationTesterTab::Moisture90Changed()
{
	m_tester.SetMoistureContent90(ui.lineEditMoisture90->text().toDouble());
	UpdateMoisture90();
	UpdateDataTable();
}

void CDustFormationTesterTab::CompoundChanged()
{
	UpdateDataTable();
}