KChart

KChartDatasetSelector.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartDatasetSelector.h"
10
11#include "ui_KChartDatasetSelector.h"
12
13#include "KChartMath_p.h"
14
15using namespace KChart;
16
17DatasetSelectorWidget::DatasetSelectorWidget( QWidget* parent )
18 : QFrame( parent )
19 , mUi( new Ui::DatasetSelector() )
20 , mSourceRowCount( 0 )
21 , mSourceColumnCount( 0 )
22{
23 qWarning( "For DatasetSelectorWidget to become useful, it has to be connected to the proxy model it configures!" );
24
25 mUi->setupUi( this );
26 setMinimumSize( minimumSizeHint() );
27
28 connect( mUi->sbStartColumn, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
29 connect( mUi->sbStartRow, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
30 connect( mUi->sbColumnCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
31 connect( mUi->sbRowCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
32 connect( mUi->cbReverseRows, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
33 connect( mUi->cbReverseColumns, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
34 connect( mUi->groupBox, SIGNAL(toggled(bool)), this, SLOT(updateState(bool)) );
35}
36
37DatasetSelectorWidget::~DatasetSelectorWidget()
38{
39 delete mUi;
40}
41
42void DatasetSelectorWidget::updateState( bool state )
43{
44 if ( state )
45 {
46 calculateMapping();
47 } else {
48 Q_EMIT mappingDisabled();
49 }
50}
51
52
53void DatasetSelectorWidget::setSourceRowCount( const int& rowCount )
54{
55 if ( rowCount != mSourceRowCount )
56 {
57 mSourceRowCount = rowCount;
58 resetDisplayValues();
59 }
60}
61
62void DatasetSelectorWidget::setSourceColumnCount( const int& columnCount )
63{
64 if ( columnCount != mSourceColumnCount )
65 {
66 mSourceColumnCount = columnCount;
67 resetDisplayValues();
68 }
69}
70
71void DatasetSelectorWidget::resetDisplayValues()
72{
73 mUi->sbStartRow->setValue( 0 );
74 mUi->sbStartRow->setMinimum( 0 );
75 mUi->sbStartRow->setMaximum( qMax( mSourceRowCount - 1, 0 ) );
76 mUi->sbStartColumn->setValue( 0 );
77 mUi->sbStartColumn->setMinimum( 0 );
78 mUi->sbStartColumn->setMaximum( qMax( mSourceColumnCount - 1, 0 ) );
79 mUi->sbRowCount->setMinimum( 1 );
80 mUi->sbRowCount->setMaximum( mSourceRowCount );
81 mUi->sbRowCount->setValue( mSourceRowCount );
82 mUi->sbColumnCount->setMinimum( 1 );
83 mUi->sbColumnCount->setMaximum( mSourceColumnCount );
84 mUi->sbColumnCount->setValue( mSourceColumnCount );
85 mUi->groupBox->setChecked( false );
86 Q_EMIT mappingDisabled();
87}
88
89void DatasetSelectorWidget::calculateMapping()
90{
91 if ( mSourceColumnCount < 2 && mSourceRowCount < 2 )
92 {
93 mUi->groupBox->setEnabled( false );
94 Q_EMIT mappingDisabled();
95 } else {
96 mUi->groupBox->setEnabled( true );
97
98 if ( ! mUi->groupBox->isChecked() )
99 {
100 Q_EMIT mappingDisabled();
101 return;
102 }
103
104 // retrieve values:
105 int startRow = mUi->sbStartRow->value();
106 int startColumn = mUi->sbStartColumn->value();
107 int rowCount = mUi->sbRowCount->value();
108 int columnCount = mUi->sbColumnCount->value();
109 bool reverseColumns = mUi->cbReverseColumns->checkState() == Qt::Checked;
110 bool reverseRows = mUi->cbReverseRows->checkState() == Qt::Checked;
111
112 // verify values:
113 startRow = qMin( startRow, mSourceRowCount - 2 );
114 startRow = qMax( 0, startRow );
115 startColumn = qMin( startColumn, mSourceColumnCount - 2 );
116 startColumn = qMax( 0, startColumn );
117
118 rowCount = qMin( rowCount, mSourceRowCount - startRow );
119 rowCount = qMax( 1, rowCount );
120 columnCount = qMin( columnCount, mSourceColumnCount - startColumn );
121 columnCount = qMax( 1, columnCount );
122
123 DatasetDescriptionVector rowConfig( rowCount );
124 Q_ASSERT( rowConfig.size() > 0 );
125 DatasetDescriptionVector columnConfig( columnCount );
126 Q_ASSERT( columnConfig.size() > 0 );
127
128 // fill the dataset description vectors:
129 for ( int row = 0; row < rowCount; ++row )
130 {
131 if ( reverseRows )
132 {
133 rowConfig[row] = startRow + rowCount - row - 1;
134 } else {
135 rowConfig[row] = startRow + row;
136 }
137 }
138
139 for ( int column = 0; column < columnCount; ++ column )
140 {
141 if ( reverseColumns )
142 {
143 columnConfig[column] = startColumn + columnCount - column -1;
144 } else {
145 columnConfig[column] = startColumn + column;
146 }
147 }
148
149 // and tell the world:
150 Q_EMIT configureDatasetProxyModel( rowConfig, columnConfig );
151 }
152}
153
Q_EMITQ_EMIT
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:02:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.