File: MDMTable.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 (149 lines) | stat: -rw-r--r-- 4,426 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
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "MDMTable.h"
#include <QHeaderView>

CMDMTable::CMDMTable(QWidget *parent)
	: QWidget(parent)
{
	m_pData = NULL;

	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, &CMDMTable::DataPasted);
}

CMDMTable::~CMDMTable()
{
	m_pData = NULL;
}

void CMDMTable::SetDistribution( CMDMatrix* _pDistribution, const std::vector<std::string>& _sNames )
{
	m_pData = _pDistribution;

	if( m_pData == NULL )
		return;

	m_pTable->setColumnCount( m_pData->GetDimensionSizeByType( DISTR_COMPOUNDS ) + 1 );
	SetHeaders( _sNames );

	UpdateWholeView();
}

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

void CMDMTable::SetHeaders(const std::vector<std::string>& _sNames)
{
	if( m_pData == NULL ) return;

	auto names = _sNames;

	m_pTable->setHorizontalHeaderItem( 0, new QTableWidgetItem( "Time [s]" ) );
	m_pTable->horizontalHeaderItem(0)->setToolTip("Time point");
	m_pTable->horizontalHeaderItem(0)->setWhatsThis("Time point");
	int nCompoundsNumber = (int)m_pData->GetDimensionSizeByType( DISTR_COMPOUNDS );
	if(names.size() != nCompoundsNumber )
		names.resize( nCompoundsNumber );
	for( int i=0; i<nCompoundsNumber; ++i )
	{
		if( m_pTable->columnCount() < i+2 )
			m_pTable->insertColumn( i+2 );
		m_pTable->setHorizontalHeaderItem( i+1, new QTableWidgetItem( QString::fromUtf8(names[i].c_str() ) ) );
		m_pTable->horizontalHeaderItem(i + 1)->setToolTip("Mass fraction (0..1) of compound " + QString::fromStdString(names[i]) + " in the current phase at the corresponding time point");
		m_pTable->horizontalHeaderItem(i + 1)->setWhatsThis("Mass fraction (0..1) of compound " + QString::fromStdString(names[i]) + " in the current phase at the corresponding time point");
	}
}

void CMDMTable::CheckNormalization()
{
	for( unsigned i=0; i<m_pData->GetTimePointsNumber(); ++i )
	{
		std::vector<double> vTemp;
		m_pData->GetVectorValue( i, DISTR_COMPOUNDS, vTemp );
		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( i, j )->setBackground( Qt::lightGray );
		else
			for( int j=0; j<m_pTable->columnCount(); ++j )
				m_pTable->item( i, j )->setBackground( Qt::white );
	}
}

void CMDMTable::UpdateWholeView()
{
	if( m_pData == NULL ) return;

	m_bAvoidSignal = true;

	std::vector<double> vTimes = m_pData->GetAllTimePoints();
	for( int iRow=0; iRow<(int)vTimes.size(); ++iRow )
	{
		if( iRow >= m_pTable->rowCount() )
			m_pTable->insertRow(iRow);
		std::vector<double> vTemp;
		m_pData->GetVectorValue( (unsigned)iRow, DISTR_COMPOUNDS, vTemp );
		m_pTable->setItem( iRow, 0, new QTableWidgetItem( QString::number( vTimes[iRow] ) ) );
		m_pTable->item( iRow, 0 )->setFlags( m_pTable->item( iRow, 0 )->flags() & ~Qt::ItemIsEditable );
		for( unsigned i=0; i<vTemp.size(); ++i )
			m_pTable->setItem( iRow, i+1, new QTableWidgetItem( QString::number( vTemp[i] ) ));
	}
	while( m_pTable->rowCount() > (int)vTimes.size() )
		m_pTable->removeRow( m_pTable->rowCount()-1 );

	CheckNormalization();

	m_bAvoidSignal = false;
}

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

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

	unsigned nTimeIndex = _pItem->row();
	unsigned nCoord = _pItem->column() - 1;
	double dVal = _pItem->text().toDouble();
	if( dVal < 0 ) dVal = 0;
	m_pData->SetValue( nTimeIndex, DISTR_COMPOUNDS, nCoord, dVal );

	UpdateWholeView();

	emit DataChanged();
}

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

	for (int i = 0; i < m_pTable->rowCount(); ++i)
		for (int j = 1; j < m_pTable->columnCount(); ++j)
		{
			const unsigned iTime = i;
			const unsigned coord = j - 1;
			double val = m_pTable->item(i, j)->text().toDouble();
			if (val < 0) val = 0;
			m_pData->SetValue(iTime, DISTR_COMPOUNDS, coord, val);
		}

	UpdateWholeView();
	emit DataChanged();
}