File: ParamsItem.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 (109 lines) | stat: -rw-r--r-- 2,956 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "ParamsItem.h"
#include "MultidimensionalGrid.h"

CParamsItem::CParamsItem(const CMultidimensionalGrid *_pGrid, QComboBox *_pCombo, QSlider *_pSlider, QWidget *parent, Qt::WindowFlags flags )
	: QWidget( parent, flags )
{
	m_pGrid = _pGrid;
	m_pCombo = _pCombo;
	m_pSlider = _pSlider;
	m_pSlider->setMaximum(0);
	m_pSlider->setTickPosition( QSlider::TicksBelow );
	m_pLayout = new QHBoxLayout();
	m_pLabel = new QLabel("0");
	m_pLayout->addWidget( m_pSlider );
	m_pLayout->addWidget( m_pLabel );
	m_pLayout->addWidget( m_pCombo );
	setLayout(m_pLayout);

	QObject::connect( m_pCombo, SIGNAL( currentIndexChanged(int) ), this, SLOT( DistrTypeChanged() ));
	QObject::connect( m_pSlider, SIGNAL( valueChanged(int) ), this, SLOT( SliderValueChanged() ));
}

int CParamsItem::GetDistrIndex()
{
	return m_pCombo->itemData( m_pCombo->currentIndex() ).toInt();
}

void CParamsItem::SetupSlider()
{
	// get distribution index
	int nDistrIndex = GetDistrIndex();
	if( nDistrIndex == -1 ) // clear
	{
		m_pSlider->setMaximum( 0 );
		m_pSlider->setSliderPosition( -1 );
		return;
	}

	const int distr = m_pCombo->itemData(m_pCombo->currentIndex()).toInt();
	const auto* gridDim = m_pGrid->GetGridDimension(static_cast<EDistrTypes>(distr));
	const size_t nMax = gridDim ? gridDim->ClassesNumber() : 0;

	int nLastPos = m_pSlider->sliderPosition();
	if( nMax != 0 )
		m_pSlider->setMaximum( (int)nMax-1 );
	else
		m_pSlider->setMaximum( 0 );
	if( nMax > 100 )
		m_pSlider->setTickInterval( (int)(nMax/100) );
	else
		m_pSlider->setTickInterval( 1 );
	if( nLastPos < m_pSlider->maximum() )
		m_pSlider->setSliderPosition( nLastPos );
	else
		m_pSlider->setSliderPosition( 0 );
}

void CParamsItem::UpdateLabel()
{
	QString sLabelText;
	std::vector<double> vNumGrid;
	std::vector<std::string> vStrGrid;

	const int distr = m_pCombo->itemData(m_pCombo->currentIndex()).toInt();
	const auto* gridDim = m_pGrid->GetGridDimension(static_cast<EDistrTypes>(distr));

	if (!gridDim) // clear
	{
		m_pLabel->setText( "" );
		return;
	}

	int nCurrIndex = m_pSlider->sliderPosition();
	const EGridEntry gridEntry = gridDim->GridType();

	switch( gridEntry )
	{
	case EGridEntry::GRID_NUMERIC:
		vNumGrid = dynamic_cast<const CGridDimensionNumeric*>(gridDim)->Grid();
		sLabelText = "[" + QString::number( vNumGrid[nCurrIndex] ) + " : " + QString::number( vNumGrid[nCurrIndex+1] ) + "]";
		break;
	case EGridEntry::GRID_SYMBOLIC:
		vStrGrid = dynamic_cast<const CGridDimensionSymbolic*>(gridDim)->Grid();
		sLabelText = QString::fromStdString(vStrGrid[nCurrIndex]);
		break;
	default:
		break;
	}

	m_pLabel->setText( sLabelText );
}

void CParamsItem::UpdateWholeView()
{
	SetupSlider();
	UpdateLabel();
}

void CParamsItem::DistrTypeChanged()
{
	UpdateWholeView();
}

void CParamsItem::SliderValueChanged()
{
	UpdateLabel();
}