# KDChartDatasetSelector.cpp

Go to the documentation of this file.

    00001 /****************************************************************************
    00002  ** Copyright (C) 2006 Klarälvdalens Datakonsult AB.  All rights reserved.
    00003  **
    00004  ** This file is part of the KD Chart library.
    00005  **
    00006  ** This file may be distributed and/or modified under the terms of the
    00007  ** GNU General Public License version 2 as published by the Free Software
    00008  ** Foundation and appearing in the file LICENSE.GPL included in the
    00009  ** packaging of this file.
    00010  **
    00011  ** Licensees holding valid commercial KD Chart licenses may use this file in
    00012  ** accordance with the KD Chart Commercial License Agreement provided with
    00013  ** the Software.
    00014  **
    00015  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    00016  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    00017  **
    00018  ** See http://www.kdab.net/kdchart for
    00019  **   information about KDChart Commercial License Agreements.
    00020  **
    00021  ** Contact info@kdab.net if any conditions of this
    00022  ** licensing are not clear to you.
    00023  **
    00024  **********************************************************************/
    00025
    00026 #include <QtDebug>
    00027
    00028 #include "KDChartDatasetSelector.h"
    00029
    00030 #include "ui_KDChartDatasetSelector.h"
    00031
    00032 #include <KDABLibFakes>
    00033
    00034 using namespace KDChart;
    00035
    00036 DatasetSelectorWidget::DatasetSelectorWidget ( QWidget* parent )
    00037     : QFrame ( parent )
    00038     , mUi ( new Ui::DatasetSelector () )
    00039     , mSourceRowCount ( 0 )
    00040     , mSourceColumnCount ( 0 )
    00041 {
    00042     qWarning("For DatasetSelectorWidget to become useful, it has to be connected to the proxy model it configures!");
    00043
    00044     mUi->setupUi ( this );
    00045     setMinimumSize ( minimumSizeHint() );
    00046 }
    00047
    00048 void DatasetSelectorWidget::on_sbStartColumn_valueChanged ( const int )
    00049 {
    00050     calculateMapping();
    00051 }
    00052
    00053 void DatasetSelectorWidget::on_sbStartRow_valueChanged( const int )
    00054 {
    00055     calculateMapping();
    00056 }
    00057
    00058 void DatasetSelectorWidget::on_sbColumnCount_valueChanged ( int )
    00059 {
    00060     calculateMapping();
    00061 }
    00062
    00063 void DatasetSelectorWidget::on_sbRowCount_valueChanged ( int )
    00064 {
    00065     calculateMapping();
    00066 }
    00067
    00068 void DatasetSelectorWidget::on_cbReverseRows_stateChanged ( int )
    00069 {
    00070     calculateMapping();
    00071 }
    00072
    00073 void DatasetSelectorWidget::on_cbReverseColumns_stateChanged ( int )
    00074 {
    00075     calculateMapping();
    00076 }
    00077
    00078 void DatasetSelectorWidget::on_groupBox_toggled( bool state )
    00079 {
    00080     if ( state )
    00081     {
    00082         calculateMapping();
    00083     } else {
    00084         emit mappingDisabled();
    00085     }
    00086 }
    00087
    00088
    00089 void DatasetSelectorWidget::setSourceRowCount ( const int& rowCount )
    00090 {
    00091     if ( rowCount != mSourceRowCount )
    00092     {
    00093         mSourceRowCount = rowCount;
    00094         resetDisplayValues();
    00095     }
    00096 }
    00097
    00098 void DatasetSelectorWidget::setSourceColumnCount ( const int& columnCount )
    00099 {
    00100     if ( columnCount != mSourceColumnCount )
    00101     {
    00102         mSourceColumnCount = columnCount;
    00103         resetDisplayValues();
    00104     }
    00105 }
    00106
    00107 void DatasetSelectorWidget::resetDisplayValues()
    00108 {
    00109     mUi->sbStartRow->setValue( 0 );
    00110     mUi->sbStartRow->setMinimum ( 0 );
    00111     mUi->sbStartRow->setMaximum ( qMax ( mSourceRowCount - 1, 0 ) );
    00112     mUi->sbStartColumn->setValue( 0 );
    00113     mUi->sbStartColumn->setMinimum ( 0 );
    00114     mUi->sbStartColumn->setMaximum ( qMax ( mSourceColumnCount - 1, 0 ) );
    00115     mUi->sbRowCount->setMinimum ( 1 );
    00116     mUi->sbRowCount->setMaximum ( mSourceRowCount );
    00117     mUi->sbRowCount->setValue( mSourceRowCount );
    00118     mUi->sbColumnCount->setMinimum ( 1 );
    00119     mUi->sbColumnCount->setMaximum ( mSourceColumnCount );
    00120     mUi->sbColumnCount->setValue( mSourceColumnCount );
    00121     mUi->groupBox->setChecked ( false );
    00122     emit mappingDisabled();
    00123 }
    00124
    00125 void DatasetSelectorWidget::calculateMapping()
    00126 {
    00127     if ( mSourceColumnCount < 2 && mSourceRowCount < 2 )
    00128     {
    00129         mUi->groupBox->setEnabled ( false );
    00130         emit mappingDisabled();
    00131     } else {
    00132         mUi->groupBox->setEnabled ( true );
    00133
    00134         if ( ! mUi->groupBox->isChecked() )
    00135         {
    00136             emit mappingDisabled();
    00137             return;
    00138         }
    00139
    00140         // retrieve values:
    00141         int startRow = mUi->sbStartRow->value();
    00142         int startColumn = mUi->sbStartColumn->value();
    00143         int rowCount = mUi->sbRowCount->value();
    00144         int columnCount = mUi->sbColumnCount->value();
    00145         bool reverseColumns = mUi->cbReverseColumns->checkState() == Qt::Checked;
    00146         bool reverseRows = mUi->cbReverseRows->checkState() == Qt::Checked;
    00147
    00148         // verify values:
    00149         startRow = qMin ( startRow,  mSourceRowCount - 2 );
    00150         startRow = qMax ( 0, startRow );
    00151         startColumn = qMin ( startColumn,  mSourceColumnCount - 2 );
    00152         startColumn = qMax ( 0,  startColumn );
    00153
    00154         rowCount = qMin ( rowCount, mSourceRowCount - startRow );
    00155         rowCount = qMax ( 1, rowCount );
    00156         columnCount = qMin ( columnCount, mSourceColumnCount - startColumn );
    00157         columnCount = qMax ( 1, columnCount );
    00158
    00159         DatasetDescriptionVector rowConfig ( rowCount );
    00160         Q_ASSERT ( rowConfig.size() > 0 );
    00161         DatasetDescriptionVector columnConfig ( columnCount );
    00162         Q_ASSERT ( columnConfig.size() > 0 );
    00163
    00164         // fill the dataset description vectors:
    00165         for ( int row = 0; row < rowCount; ++row )
    00166         {
    00167             if ( reverseRows )
    00168             {
    00169                 rowConfig[row] = startRow + rowCount - row - 1;
    00170             } else {
    00171                 rowConfig[row] = startRow + row;
    00172             }
    00173         }
    00174
    00175         for ( int column = 0; column < columnCount; ++ column )
    00176         {
    00177             if ( reverseColumns )
    00178             {
    00179                 columnConfig[column] = startColumn + columnCount - column -1;
    00180             } else {
    00181                 columnConfig[column] = startColumn + column;
    00182             }
    00183         }
    00184
    00185         // and tell the world:
    00186         emit configureDatasetProxyModel ( rowConfig, columnConfig );
    00187     }
    00188 }
    00189