File: DDTable.cpp

package info (click to toggle)
dyssol 1.5.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,204 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (170 lines) | stat: -rw-r--r-- 5,096 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
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "DDTable.h"
#include "ContainerFunctions.h"
#include "StringFunctions.h"
#include <QHeaderView>

CDDTable::CDDTable( QWidget *parent, Qt::WindowFlags flags ) : QWidget(parent, flags)
{
	m_bNormalize = false;

	m_pTable = new CQtTable( this );
	layout = new QHBoxLayout;
	layout->addWidget(m_pTable);
	setLayout(layout);

	m_pTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

	QObject::connect( m_pTable, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(ItemWasChanged(QTableWidgetItem*)) );
	connect(m_pTable, &CQtTable::DataPasted, this, &CDDTable::DataPasted);
}

CDDTable::~CDDTable()
{
	m_pData.clear();
}

void CDDTable::SetDistribution(const std::vector<CTimeDependentValue*>& _values)
{
	m_pData = _values;

	m_pTable->setColumnCount(static_cast<int>(m_pData.size()) + 1 );
	SetHeaders();

	UpdateWholeView();
}

void CDDTable::SetNormalizationCheck( bool _bAnalyse )
{
	m_bNormalize = _bAnalyse;
	UpdateWholeView();
}

void CDDTable::SetEditable(bool _bEditable)
{
	m_pTable->SetEditable(_bEditable);
}

void CDDTable::SetHeaders()
{
	if (m_pData.empty()) return;

	m_pTable->setHorizontalHeaderItem( 0, new QTableWidgetItem("Time [s]") );
	m_pTable->horizontalHeaderItem(0)->setToolTip("Time point");
	m_pTable->horizontalHeaderItem(0)->setWhatsThis("Time point");
	for (int i = 0; i < static_cast<int>(m_pData.size()); ++i)
	{
		if( m_pTable->columnCount() < i+2 )
			m_pTable->insertColumn( i+2 );
		m_pTable->setHorizontalHeaderItem( i+1, new QTableWidgetItem( QString::fromUtf8( (m_pData[i]->GetName() + " ["  + m_pData[i]->GetUnits() + "]").c_str()) ) );
		// HACK: nasty. have to distinguish between overall and phases in a better way
		if (StringFunctions::StartsWith(m_pData.front()->GetName(), "Mass"))
		{
			m_pTable->horizontalHeaderItem(i + 1)->setToolTip(QString::fromStdString(m_pData[i]->GetName()) + " of material at the corresponding time point");
			m_pTable->horizontalHeaderItem(i + 1)->setWhatsThis(QString::fromStdString(m_pData[i]->GetName()) + " of material at the corresponding time point");
		}
		else
		{
			m_pTable->horizontalHeaderItem(i + 1)->setToolTip(QString::fromStdString("Mass fraction (0..1) of phase " + m_pData[i]->GetName()) + " at the corresponding time point");
			m_pTable->horizontalHeaderItem(i + 1)->setWhatsThis(QString::fromStdString("Mass fraction (0..1) of phase " + m_pData[i]->GetName()) + " at the corresponding time point");
		}
	}
}

void CDDTable::CheckNormalization()
{
	if (m_pData.empty()) return;

	std::vector<std::vector<std::vector<double>>> data;
	for (auto& value : m_pData)
		data.push_back(value->GetRawData());

	for (size_t i = 0; i < m_pData.front()->GetTimePointsNumber(); ++i)
	{
		std::vector<double> vTemp;
		for (size_t j = 0; j < m_pData.size(); ++j)
			vTemp.push_back(data[j][1][i]);

		double dSum = 0;
		for( unsigned j=0; j<vTemp.size(); ++j )
			dSum += vTemp[j];
		if( dSum != 1 )
			for( int j=0; j<m_pTable->columnCount(); ++j )
				m_pTable->item( static_cast<int>(i), j )->setBackground( Qt::lightGray );
		else
			for( int j=0; j<m_pTable->columnCount(); ++j )
				m_pTable->item( static_cast<int>(i), j )->setBackground( Qt::white );
	}
}

void CDDTable::UpdateWholeView()
{
	if (m_pData.empty()) return;

	m_bAvoidSignal = true;

	std::vector<std::vector<std::vector<double>>> data;
	for (auto& value : m_pData)
		data.push_back(value->GetRawData());

	for (int iRow = 0; iRow < static_cast<int>(data.front()[0].size()); ++iRow)
	{
		if( iRow >= m_pTable->rowCount() )
			m_pTable->insertRow(iRow);

		std::vector<double> vTemp;
		for (size_t j = 0; j < m_pData.size(); ++j)
			vTemp.push_back(data[j][1][iRow]);
		m_pTable->SetItemNotEditable(iRow, 0, data.front()[0][iRow]);
		for (unsigned i = 0; i < vTemp.size(); ++i)
			m_pTable->SetItemEditable(iRow, i + 1, vTemp[i]);
	}
	while( m_pTable->rowCount() > (int)m_pData.front()->GetTimePointsNumber())
		m_pTable->removeRow( m_pTable->rowCount()-1 );

	if( m_bNormalize )
		CheckNormalization();

	m_bAvoidSignal = false;
}

//void CDDTable::setVisible( bool _bVisible )
//{
//	if ( _bVisible )
//		UpdateWholeView();
//	QWidget::setVisible( _bVisible );
//}

void CDDTable::ItemWasChanged( QTableWidgetItem* _pItem )
{
	if( m_bAvoidSignal ) return;

	unsigned nTimeIndex = _pItem->row();
	const double time = m_pTable->item(nTimeIndex, 0)->text().toDouble();
	unsigned nDimIndex = _pItem->column() - 1;
	double dVal = _pItem->text().toDouble();
	if( dVal < 0 ) dVal = 0;
	m_pData[nDimIndex]->SetValue(time, dVal);

	UpdateWholeView();

	emit DataChanged();
}

void CDDTable::DataPasted()
{
	if (m_bAvoidSignal) return;

	for (int i = 0; i < m_pTable->rowCount(); ++i)
		for (int j = 1; j < m_pTable->columnCount(); ++j)
		{
			const double time = m_pTable->item(i, 0)->text().toDouble();
			const int iDim = j - 1;
			const double val = m_pTable->item(i, j)->text().toDouble();
			m_pData[iDim]->SetValue(time, val);
		}

	UpdateWholeView();
	emit DataChanged();
}