File indexing completed on 2024-05-12 04:20:32

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "KChartDatasetSelector.h"
0010 
0011 #include "ui_KChartDatasetSelector.h"
0012 
0013 #include "KChartMath_p.h"
0014 
0015 using namespace KChart;
0016 
0017 DatasetSelectorWidget::DatasetSelectorWidget( QWidget* parent )
0018     : QFrame( parent )
0019     , mUi( new Ui::DatasetSelector() )
0020     , mSourceRowCount( 0 )
0021     , mSourceColumnCount( 0 )
0022 {
0023     qWarning( "For DatasetSelectorWidget to become useful, it has to be connected to the proxy model it configures!" );
0024 
0025     mUi->setupUi( this );
0026     setMinimumSize( minimumSizeHint() );
0027 
0028     connect( mUi->sbStartColumn, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0029     connect( mUi->sbStartRow, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0030     connect( mUi->sbColumnCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0031     connect( mUi->sbRowCount, SIGNAL(valueChanged(int)), this, SLOT(calculateMapping()) );
0032     connect( mUi->cbReverseRows, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
0033     connect( mUi->cbReverseColumns, SIGNAL(stateChanged(int)), this, SLOT(calculateMapping()) );
0034     connect( mUi->groupBox, SIGNAL(toggled(bool)), this, SLOT(updateState(bool)) );
0035 }
0036 
0037 DatasetSelectorWidget::~DatasetSelectorWidget()
0038 {
0039     delete mUi;
0040 }
0041 
0042 void DatasetSelectorWidget::updateState( bool state )
0043 {
0044     if ( state )
0045     {
0046         calculateMapping();
0047     } else {
0048         Q_EMIT mappingDisabled();
0049     }
0050 }
0051 
0052 
0053 void DatasetSelectorWidget::setSourceRowCount( const int& rowCount )
0054 {
0055     if ( rowCount != mSourceRowCount )
0056     {
0057         mSourceRowCount = rowCount;
0058         resetDisplayValues();
0059     }
0060 }
0061 
0062 void DatasetSelectorWidget::setSourceColumnCount( const int& columnCount )
0063 {
0064     if ( columnCount != mSourceColumnCount )
0065     {
0066         mSourceColumnCount = columnCount;
0067         resetDisplayValues();
0068     }
0069 }
0070 
0071 void DatasetSelectorWidget::resetDisplayValues()
0072 {
0073     mUi->sbStartRow->setValue( 0 );
0074     mUi->sbStartRow->setMinimum( 0 );
0075     mUi->sbStartRow->setMaximum( qMax( mSourceRowCount - 1, 0 ) );
0076     mUi->sbStartColumn->setValue( 0 );
0077     mUi->sbStartColumn->setMinimum( 0 );
0078     mUi->sbStartColumn->setMaximum( qMax( mSourceColumnCount - 1, 0 ) );
0079     mUi->sbRowCount->setMinimum( 1 );
0080     mUi->sbRowCount->setMaximum( mSourceRowCount );
0081     mUi->sbRowCount->setValue( mSourceRowCount );
0082     mUi->sbColumnCount->setMinimum( 1 );
0083     mUi->sbColumnCount->setMaximum( mSourceColumnCount );
0084     mUi->sbColumnCount->setValue( mSourceColumnCount );
0085     mUi->groupBox->setChecked( false );
0086     Q_EMIT mappingDisabled();
0087 }
0088 
0089 void DatasetSelectorWidget::calculateMapping()
0090 {
0091     if ( mSourceColumnCount < 2 && mSourceRowCount < 2 )
0092     {
0093         mUi->groupBox->setEnabled( false );
0094         Q_EMIT mappingDisabled();
0095     } else {
0096         mUi->groupBox->setEnabled( true );
0097 
0098         if ( ! mUi->groupBox->isChecked() )
0099         {
0100             Q_EMIT mappingDisabled();
0101             return;
0102         }
0103 
0104         // retrieve values:
0105         int startRow = mUi->sbStartRow->value();
0106         int startColumn = mUi->sbStartColumn->value();
0107         int rowCount = mUi->sbRowCount->value();
0108         int columnCount = mUi->sbColumnCount->value();
0109         bool reverseColumns = mUi->cbReverseColumns->checkState() == Qt::Checked;
0110         bool reverseRows = mUi->cbReverseRows->checkState() == Qt::Checked;
0111 
0112         // verify values:
0113         startRow = qMin( startRow,  mSourceRowCount - 2 );
0114         startRow = qMax( 0, startRow );
0115         startColumn = qMin( startColumn,  mSourceColumnCount - 2 );
0116         startColumn = qMax( 0,  startColumn );
0117 
0118         rowCount = qMin( rowCount, mSourceRowCount - startRow );
0119         rowCount = qMax( 1, rowCount );
0120         columnCount = qMin( columnCount, mSourceColumnCount - startColumn );
0121         columnCount = qMax( 1, columnCount );
0122 
0123         DatasetDescriptionVector rowConfig( rowCount );
0124         Q_ASSERT( rowConfig.size() > 0 );
0125         DatasetDescriptionVector columnConfig( columnCount );
0126         Q_ASSERT( columnConfig.size() > 0 );
0127 
0128         // fill the dataset description vectors:
0129         for ( int row = 0; row < rowCount; ++row )
0130         {
0131             if ( reverseRows )
0132             {
0133                 rowConfig[row] = startRow + rowCount - row - 1;
0134             } else {
0135                 rowConfig[row] = startRow + row;
0136             }
0137         }
0138 
0139         for ( int column = 0; column < columnCount; ++ column )
0140         {
0141             if ( reverseColumns )
0142             {
0143                 columnConfig[column] = startColumn + columnCount - column -1;
0144             } else {
0145                 columnConfig[column] = startColumn + column;
0146             }
0147         }
0148 
0149         // and tell the world:
0150         Q_EMIT configureDatasetProxyModel( rowConfig, columnConfig );
0151     }
0152 }
0153