File indexing completed on 2024-05-12 15:54:15

0001 /*
0002  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "KChartDatasetSelector.h"
0021 
0022 #include "ui_KChartDatasetSelector.h"
0023 
0024 #include "KChartMath_p.h"
0025 
0026 using namespace KChart;
0027 
0028 DatasetSelectorWidget::DatasetSelectorWidget( QWidget* parent )
0029     : QFrame( parent )
0030     , mUi( new Ui::DatasetSelector() )
0031     , mSourceRowCount( 0 )
0032     , mSourceColumnCount( 0 )
0033 {
0034     qWarning( "For DatasetSelectorWidget to become useful, it has to be connected to the proxy model it configures!" );
0035 
0036     mUi->setupUi( this );
0037     setMinimumSize( minimumSizeHint() );
0038 
0039     connect( mUi->sbStartColumn, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0040     connect( mUi->sbStartRow, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0041     connect( mUi->sbColumnCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0042     connect( mUi->sbRowCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0043     connect( mUi->cbReverseRows, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
0044     connect( mUi->cbReverseColumns, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
0045     connect( mUi->groupBox, SIGNAL(toggled(bool)), this, SLOT(updateState(bool)) );
0046 }
0047 
0048 DatasetSelectorWidget::~DatasetSelectorWidget()
0049 {
0050     delete mUi;
0051 }
0052 
0053 void DatasetSelectorWidget::updateState( bool state )
0054 {
0055     if ( state )
0056     {
0057         calculateMapping();
0058     } else {
0059         Q_EMIT mappingDisabled();
0060     }
0061 }
0062 
0063 
0064 void DatasetSelectorWidget::setSourceRowCount( const int& rowCount )
0065 {
0066     if ( rowCount != mSourceRowCount )
0067     {
0068         mSourceRowCount = rowCount;
0069         resetDisplayValues();
0070     }
0071 }
0072 
0073 void DatasetSelectorWidget::setSourceColumnCount( const int& columnCount )
0074 {
0075     if ( columnCount != mSourceColumnCount )
0076     {
0077         mSourceColumnCount = columnCount;
0078         resetDisplayValues();
0079     }
0080 }
0081 
0082 void DatasetSelectorWidget::resetDisplayValues()
0083 {
0084     mUi->sbStartRow->setValue( 0 );
0085     mUi->sbStartRow->setMinimum( 0 );
0086     mUi->sbStartRow->setMaximum( qMax( mSourceRowCount - 1, 0 ) );
0087     mUi->sbStartColumn->setValue( 0 );
0088     mUi->sbStartColumn->setMinimum( 0 );
0089     mUi->sbStartColumn->setMaximum( qMax( mSourceColumnCount - 1, 0 ) );
0090     mUi->sbRowCount->setMinimum( 1 );
0091     mUi->sbRowCount->setMaximum( mSourceRowCount );
0092     mUi->sbRowCount->setValue( mSourceRowCount );
0093     mUi->sbColumnCount->setMinimum( 1 );
0094     mUi->sbColumnCount->setMaximum( mSourceColumnCount );
0095     mUi->sbColumnCount->setValue( mSourceColumnCount );
0096     mUi->groupBox->setChecked( false );
0097     Q_EMIT mappingDisabled();
0098 }
0099 
0100 void DatasetSelectorWidget::calculateMapping()
0101 {
0102     if ( mSourceColumnCount < 2 && mSourceRowCount < 2 )
0103     {
0104         mUi->groupBox->setEnabled( false );
0105         Q_EMIT mappingDisabled();
0106     } else {
0107         mUi->groupBox->setEnabled( true );
0108 
0109         if ( ! mUi->groupBox->isChecked() )
0110         {
0111             Q_EMIT mappingDisabled();
0112             return;
0113         }
0114 
0115         // retrieve values:
0116         int startRow = mUi->sbStartRow->value();
0117         int startColumn = mUi->sbStartColumn->value();
0118         int rowCount = mUi->sbRowCount->value();
0119         int columnCount = mUi->sbColumnCount->value();
0120         bool reverseColumns = mUi->cbReverseColumns->checkState() == Qt::Checked;
0121         bool reverseRows = mUi->cbReverseRows->checkState() == Qt::Checked;
0122 
0123         // verify values:
0124         startRow = qMin( startRow,  mSourceRowCount - 2 );
0125         startRow = qMax( 0, startRow );
0126         startColumn = qMin( startColumn,  mSourceColumnCount - 2 );
0127         startColumn = qMax( 0,  startColumn );
0128 
0129         rowCount = qMin( rowCount, mSourceRowCount - startRow );
0130         rowCount = qMax( 1, rowCount );
0131         columnCount = qMin( columnCount, mSourceColumnCount - startColumn );
0132         columnCount = qMax( 1, columnCount );
0133 
0134         DatasetDescriptionVector rowConfig( rowCount );
0135         Q_ASSERT( rowConfig.size() > 0 );
0136         DatasetDescriptionVector columnConfig( columnCount );
0137         Q_ASSERT( columnConfig.size() > 0 );
0138 
0139         // fill the dataset description vectors:
0140         for ( int row = 0; row < rowCount; ++row )
0141         {
0142             if ( reverseRows )
0143             {
0144                 rowConfig[row] = startRow + rowCount - row - 1;
0145             } else {
0146                 rowConfig[row] = startRow + row;
0147             }
0148         }
0149 
0150         for ( int column = 0; column < columnCount; ++ column )
0151         {
0152             if ( reverseColumns )
0153             {
0154                 columnConfig[column] = startColumn + columnCount - column -1;
0155             } else {
0156                 columnConfig[column] = startColumn + column;
0157             }
0158         }
0159 
0160         // and tell the world:
0161         Q_EMIT configureDatasetProxyModel( rowConfig, columnConfig );
0162     }
0163 }
0164