1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KChartDatasetSelector.h"
21 
22 #include "ui_KChartDatasetSelector.h"
23 
24 #include "KChartMath_p.h"
25 
26 using namespace KChart;
27 
DatasetSelectorWidget(QWidget * parent)28 DatasetSelectorWidget::DatasetSelectorWidget( QWidget* parent )
29     : QFrame( parent )
30     , mUi( new Ui::DatasetSelector() )
31     , mSourceRowCount( 0 )
32     , mSourceColumnCount( 0 )
33 {
34     qWarning( "For DatasetSelectorWidget to become useful, it has to be connected to the proxy model it configures!" );
35 
36     mUi->setupUi( this );
37     setMinimumSize( minimumSizeHint() );
38 
39     connect( mUi->sbStartColumn, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
40     connect( mUi->sbStartRow, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
41     connect( mUi->sbColumnCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
42     connect( mUi->sbRowCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
43     connect( mUi->cbReverseRows, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
44     connect( mUi->cbReverseColumns, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
45     connect( mUi->groupBox, SIGNAL(toggled(bool)), this, SLOT(updateState(bool)) );
46 }
47 
~DatasetSelectorWidget()48 DatasetSelectorWidget::~DatasetSelectorWidget()
49 {
50     delete mUi;
51 }
52 
updateState(bool state)53 void DatasetSelectorWidget::updateState( bool state )
54 {
55     if ( state )
56     {
57         calculateMapping();
58     } else {
59         Q_EMIT mappingDisabled();
60     }
61 }
62 
63 
setSourceRowCount(const int & rowCount)64 void DatasetSelectorWidget::setSourceRowCount( const int& rowCount )
65 {
66     if ( rowCount != mSourceRowCount )
67     {
68         mSourceRowCount = rowCount;
69         resetDisplayValues();
70     }
71 }
72 
setSourceColumnCount(const int & columnCount)73 void DatasetSelectorWidget::setSourceColumnCount( const int& columnCount )
74 {
75     if ( columnCount != mSourceColumnCount )
76     {
77         mSourceColumnCount = columnCount;
78         resetDisplayValues();
79     }
80 }
81 
resetDisplayValues()82 void DatasetSelectorWidget::resetDisplayValues()
83 {
84     mUi->sbStartRow->setValue( 0 );
85     mUi->sbStartRow->setMinimum( 0 );
86     mUi->sbStartRow->setMaximum( qMax( mSourceRowCount - 1, 0 ) );
87     mUi->sbStartColumn->setValue( 0 );
88     mUi->sbStartColumn->setMinimum( 0 );
89     mUi->sbStartColumn->setMaximum( qMax( mSourceColumnCount - 1, 0 ) );
90     mUi->sbRowCount->setMinimum( 1 );
91     mUi->sbRowCount->setMaximum( mSourceRowCount );
92     mUi->sbRowCount->setValue( mSourceRowCount );
93     mUi->sbColumnCount->setMinimum( 1 );
94     mUi->sbColumnCount->setMaximum( mSourceColumnCount );
95     mUi->sbColumnCount->setValue( mSourceColumnCount );
96     mUi->groupBox->setChecked( false );
97     Q_EMIT mappingDisabled();
98 }
99 
calculateMapping()100 void DatasetSelectorWidget::calculateMapping()
101 {
102     if ( mSourceColumnCount < 2 && mSourceRowCount < 2 )
103     {
104         mUi->groupBox->setEnabled( false );
105         Q_EMIT mappingDisabled();
106     } else {
107         mUi->groupBox->setEnabled( true );
108 
109         if ( ! mUi->groupBox->isChecked() )
110         {
111             Q_EMIT mappingDisabled();
112             return;
113         }
114 
115         // retrieve values:
116         int startRow = mUi->sbStartRow->value();
117         int startColumn = mUi->sbStartColumn->value();
118         int rowCount = mUi->sbRowCount->value();
119         int columnCount = mUi->sbColumnCount->value();
120         bool reverseColumns = mUi->cbReverseColumns->checkState() == Qt::Checked;
121         bool reverseRows = mUi->cbReverseRows->checkState() == Qt::Checked;
122 
123         // verify values:
124         startRow = qMin( startRow,  mSourceRowCount - 2 );
125         startRow = qMax( 0, startRow );
126         startColumn = qMin( startColumn,  mSourceColumnCount - 2 );
127         startColumn = qMax( 0,  startColumn );
128 
129         rowCount = qMin( rowCount, mSourceRowCount - startRow );
130         rowCount = qMax( 1, rowCount );
131         columnCount = qMin( columnCount, mSourceColumnCount - startColumn );
132         columnCount = qMax( 1, columnCount );
133 
134         DatasetDescriptionVector rowConfig( rowCount );
135         Q_ASSERT( rowConfig.size() > 0 );
136         DatasetDescriptionVector columnConfig( columnCount );
137         Q_ASSERT( columnConfig.size() > 0 );
138 
139         // fill the dataset description vectors:
140         for ( int row = 0; row < rowCount; ++row )
141         {
142             if ( reverseRows )
143             {
144                 rowConfig[row] = startRow + rowCount - row - 1;
145             } else {
146                 rowConfig[row] = startRow + row;
147             }
148         }
149 
150         for ( int column = 0; column < columnCount; ++ column )
151         {
152             if ( reverseColumns )
153             {
154                 columnConfig[column] = startColumn + columnCount - column -1;
155             } else {
156                 columnConfig[column] = startColumn + column;
157             }
158         }
159 
160         // and tell the world:
161         Q_EMIT configureDatasetProxyModel( rowConfig, columnConfig );
162     }
163 }
164 
165