kspread

kspread_dlg_sort.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2006 Robert Knight <robertknight@gmail.com>
00003              (C) 2002-2003 Norbert Andres <nandres@web.de>
00004              (C) 2002 Ariya Hidayat <ariya@kde.org>
00005              (C) 2002 John Dailey <dailey@vt.edu>
00006              (C) 2002 Werner Trobin <trobin@kde.org>
00007              (C) 2001-2002 Philipp Mueller <philipp.mueller@gmx.de>
00008              (C) 1999-2002 Laurent Montel <montel@kde.org>
00009              (C) 2000 David Faure <faure@kde.org>
00010              (C) 1998-2000 Torben Weis <weis@kde.org>
00011 
00012    This library is free software; you can redistribute it and/or
00013    modify it under the terms of the GNU Library General Public
00014    License as published by the Free Software Foundation; either
00015    version 2 of the License, or (at your option) any later version.
00016 
00017    This library is distributed in the hope that it will be useful,
00018    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020    Library General Public License for more details.
00021 
00022    You should have received a copy of the GNU Library General Public License
00023    along with this library; see the file COPYING.LIB.  If not, write to
00024    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00025  * Boston, MA 02110-1301, USA.
00026 */
00027 
00028 
00029 #include <qbuttongroup.h>
00030 #include <qhbuttongroup.h>
00031 #include <qcheckbox.h>
00032 #include <qcombobox.h>
00033 #include <qgroupbox.h>
00034 #include <qlabel.h>
00035 #include <qlayout.h>
00036 #include <qlineedit.h>
00037 #include <qpushbutton.h>
00038 #include <qradiobutton.h>
00039 #include <qrect.h>
00040 #include <qtabwidget.h>
00041 #include <qwidget.h>
00042 #include <qvbox.h>
00043 
00044 #include <kconfig.h>
00045 #include <kdebug.h>
00046 #include <klocale.h>
00047 #include <kmessagebox.h>
00048 
00049 #include "kspread_dlg_sort.h"
00050 #include "kspread_doc.h"
00051 #include "kspread_map.h"
00052 #include "kspread_sheet.h"
00053 #include "kspread_view.h"
00054 #include "kspread_util.h"
00055 #include "selection.h"
00056 
00057 using namespace KSpread;
00058 
00059 SortDialog::SortDialog( View * parent,  const char * name,
00060                                 bool modal )
00061     : KDialogBase( parent, name, modal,"Sort",Ok|Cancel ),
00062     m_pView( parent )
00063 {
00064   if ( !name )
00065     setName( "SortDialog" );
00066 
00067   resize( 528, 316 );
00068   setCaption( i18n( "Sorting" ) );
00069   //setSizeGripEnabled( true );
00070 
00071   QVBox *page = makeVBoxMainWidget();
00072 
00073   m_tabWidget = new QTabWidget( page, "m_tabWidget" );
00074 
00075   m_page1 = new QWidget( m_tabWidget, "m_page1" );
00076   QGridLayout * page1Layout
00077     = new QGridLayout( m_page1, 1, 1, 11, 6, "page1Layout");
00078 
00079 //---------------- Sort Layout & Header Row/Column Toggle
00080 
00081   //Sort orientation selector (for selecting Left-To-Right or Top-To-Bottom sorting of the selection)
00082   QGroupBox* layoutGroup = new QGroupBox(2 , Qt::Vertical,  m_page1, "layoutGroup");
00083   layoutGroup->setTitle( i18n("Layout") );
00084   
00085   QHButtonGroup * orientationGroup = new QHButtonGroup( layoutGroup, "orientationGroup" );
00086   orientationGroup->setLineWidth(0);
00087   orientationGroup->setMargin(0);
00088   orientationGroup->layout()->setMargin(0);
00089 
00090   m_sortColumn = new QRadioButton( orientationGroup, "m_sortColumn" );
00091   m_sortColumn->setText( i18n( "Sort &Rows" ) );
00092 
00093   m_sortRow = new QRadioButton( orientationGroup, "m_sortRow" );
00094   m_sortRow->setText( i18n( "Sort &Columns" ) );
00095     
00096   //First row / column contains header toggle
00097   m_firstRowOrColHeader = new QCheckBox( layoutGroup, "m_copyLayout" );
00098   //m_firstRowOrColHeader->setText( i18n( "&First row contains headers" ) );
00099   m_firstRowOrColHeader->setChecked(true);
00100   page1Layout->addWidget(layoutGroup,0,0);
00101 
00102 //----------------
00103 
00104   page1Layout->addRowSpacing(2,10);
00105   
00106 
00107   QGroupBox * sort1Box = new QGroupBox( m_page1, "sort1Box" );
00108   sort1Box->setTitle( i18n( "Sort By" ) );
00109   sort1Box->setFlat(true);
00110   sort1Box->setColumnLayout(0, Qt::Vertical );
00111   sort1Box->layout()->setSpacing( KDialog::spacingHint() );
00112   sort1Box->layout()->setMargin( KDialog::marginHint() );
00113   QHBoxLayout * sort1BoxLayout = new QHBoxLayout( sort1Box->layout() );
00114   sort1BoxLayout->setAlignment( Qt::AlignTop );
00115 
00116   m_sortKey1 = new QComboBox( false, sort1Box, "m_sortKey1" );
00117   sort1BoxLayout->addWidget( m_sortKey1 );
00118 
00119   m_sortOrder1 = new QComboBox( false, sort1Box, "m_sortOrder1" );
00120   m_sortOrder1->insertItem( i18n( "Ascending" ) );
00121   m_sortOrder1->insertItem( i18n( "Descending" ) );
00122   sort1BoxLayout->addWidget( m_sortOrder1 );
00123 
00124   page1Layout->addWidget( sort1Box, 3, 0 );
00125 
00126   QGroupBox * sort2Box = new QGroupBox( m_page1, "sort2Box" );
00127   sort2Box->setTitle( i18n( "Then By" ) );
00128   sort2Box->setFlat(true);
00129   sort2Box->setColumnLayout(0, Qt::Vertical );
00130   sort2Box->layout()->setSpacing( KDialog::spacingHint() );
00131   sort2Box->layout()->setMargin( KDialog::marginHint() );
00132   QHBoxLayout * sort2BoxLayout = new QHBoxLayout( sort2Box->layout() );
00133   sort2BoxLayout->setAlignment( Qt::AlignTop );
00134 
00135   m_sortKey2 = new QComboBox( false, sort2Box, "m_sortKey2" );
00136   m_sortKey2->insertItem( i18n( "None" ) );
00137   sort2BoxLayout->addWidget( m_sortKey2 );
00138 
00139   m_sortOrder2 = new QComboBox( false, sort2Box, "m_sortOrder2" );
00140   m_sortOrder2->insertItem( i18n( "Ascending" ) );
00141   m_sortOrder2->insertItem( i18n( "Descending" ) );
00142   sort2BoxLayout->addWidget( m_sortOrder2 );
00143 
00144   page1Layout->addWidget( sort2Box, 4, 0 );
00145 
00146   QGroupBox * sort3Box = new QGroupBox( m_page1, "sort3Box" );
00147   sort3Box->setTitle( i18n( "Then By" ) );
00148   sort3Box->setFlat(true);
00149   sort3Box->setColumnLayout(0, Qt::Vertical );
00150   sort3Box->layout()->setSpacing( KDialog::spacingHint() );
00151   sort3Box->layout()->setMargin( KDialog::marginHint() );
00152   QHBoxLayout * sort3BoxLayout = new QHBoxLayout( sort3Box->layout() );
00153   sort3BoxLayout->setAlignment( Qt::AlignTop );
00154 
00155   m_sortKey3 = new QComboBox( false, sort3Box, "m_sortKey3" );
00156   m_sortKey3->insertItem( i18n( "None" ) );
00157   m_sortKey3->setEnabled( false );
00158   sort3BoxLayout->addWidget( m_sortKey3 );
00159 
00160   m_sortOrder3 = new QComboBox( false, sort3Box, "m_sortOrder3" );
00161   m_sortOrder3->insertItem( i18n( "Ascending" ) );
00162   m_sortOrder3->insertItem( i18n( "Descending" ) );
00163   m_sortOrder3->setEnabled( false );
00164   sort3BoxLayout->addWidget( m_sortOrder3 );
00165 
00166   page1Layout->addWidget( sort3Box, 5, 0 );
00167   m_tabWidget->insertTab( m_page1, i18n( "Sort Criteria" ) );
00168 
00169 
00170   //---------------- options page
00171 
00172   m_page2 = new QWidget( m_tabWidget, "m_page2" );
00173   QGridLayout * page2Layout = new QGridLayout( m_page2, 1, 1, 11, 6, "page2Layout");
00174   page2Layout->setAlignment(Qt::AlignTop);
00175 
00176   QGroupBox * firstKeyBox = new QGroupBox( m_page2, "firstKeyBox" );
00177   firstKeyBox->setTitle( i18n( "First Key" ) );
00178   firstKeyBox->setColumnLayout(0, Qt::Vertical );
00179   firstKeyBox->layout()->setSpacing( KDialog::spacingHint() );
00180   firstKeyBox->layout()->setMargin( KDialog::marginHint() );
00181   QVBoxLayout * firstKeyBoxLayout = new QVBoxLayout( firstKeyBox->layout() );
00182   firstKeyBoxLayout->setAlignment( Qt::AlignTop );
00183 
00184   m_useCustomLists = new QCheckBox( firstKeyBox, "m_useCustomLists_2" );
00185   m_useCustomLists->setText( i18n( "&Use custom list" ) );
00186   firstKeyBoxLayout->addWidget( m_useCustomLists );
00187 
00188   m_customList = new QComboBox( false, firstKeyBox, "m_customList" );
00189   m_customList->setEnabled( false );
00190   m_customList->setMaximumSize( 230, 30 );
00191   firstKeyBoxLayout->addWidget( m_customList );
00192 
00193   page2Layout->addWidget( firstKeyBox, 0, 0 );
00194 
00195 
00196  /* 
00197   This doesn't work properly, and as a bug report pointed out, it isn't that useful since it is easy
00198   to just copy and paste the data and then sort the newly pasted data in place.
00199   -- Robert Knight
00200   
00201   QGroupBox * resultToBox = new QGroupBox( m_page2, "resultToBox" );
00202   resultToBox->setTitle( i18n( "Location to Store Sort Results" ) );
00203   resultToBox->setColumnLayout(0, Qt::Vertical );
00204   resultToBox->layout()->setSpacing( KDialog::spacingHint() );
00205   resultToBox->layout()->setMargin( KDialog::marginHint() );
00206   
00207   
00208   QHBoxLayout * resultToBoxLayout = new QHBoxLayout( resultToBox->layout() );
00209   resultToBoxLayout->setAlignment( Qt::AlignTop );
00210 
00211   QLabel * destinationSheet=new QLabel(resultToBox,"destinationSheet");
00212   destinationSheet->setText("Destination Sheet:");
00213   resultToBoxLayout->addWidget(destinationSheet);
00214   
00215   m_outputSheet = new QComboBox( false, resultToBox, "m_outputSheet" );
00216   resultToBoxLayout->addWidget( m_outputSheet );
00217   QSpacerItem * spacer = new QSpacerItem( 20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum );
00218   resultToBoxLayout->addItem( spacer );
00219 
00220   QLabel * startingCellLabel = new QLabel( resultToBox, "destinationCellLabel" );
00221   startingCellLabel->setText( i18n( "Destination Cell:" ) );
00222   resultToBoxLayout->addWidget( startingCellLabel );
00223 
00224   m_outputCell = new QLineEdit( resultToBox, "m_outputCell" );
00225   m_outputCell->setMaximumSize( QSize( 60, 32767 ) );
00226   resultToBoxLayout->addWidget( m_outputCell );
00227 
00228   page2Layout->addWidget( resultToBox, 1,0 );*/
00229   
00230   
00231   m_tabWidget->insertTab( m_page2, i18n( "Options" ) );
00232 
00233   QHBoxLayout * Layout1 = new QHBoxLayout( 0, 0, 6, "Layout1");
00234   QSpacerItem * spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Expanding,
00235                                             QSizePolicy::Minimum );
00236   Layout1->addItem( spacer_2 );
00237   
00238 
00239   m_copyLayout = new QCheckBox( m_page2, "m_copyLayout" );
00240   m_copyLayout->setText( i18n( "Copy cell &formatting (Borders, Colours, Text Style)" ) );
00241 
00242   page2Layout->addWidget( m_copyLayout, 1, 0 );
00243 
00244   m_respectCase = new QCheckBox( m_page2, "m_copyLayout" );
00245   m_respectCase->setText( i18n( "Case sensitive sort" ) );
00246   m_respectCase->setChecked( true );
00247 
00248   page2Layout->addWidget( m_respectCase, 2,0 );
00249 
00250   connect( m_sortKey2, SIGNAL( activated( int ) ), this,
00251            SLOT( sortKey2textChanged( int ) ) );
00252   connect( m_useCustomLists, SIGNAL( stateChanged(int) ), this,
00253            SLOT( useCustomListsStateChanged(int) ) );
00254   connect( m_firstRowOrColHeader, SIGNAL( stateChanged(int) ), this,
00255            SLOT( firstRowHeaderChanged(int) ) );
00256   connect( orientationGroup, SIGNAL( pressed(int) ), this,
00257            SLOT( slotOrientationChanged(int) ) );
00258 
00259   init();
00260 }
00261 
00262 QRect SortDialog::sourceArea()
00263 {
00264     return m_pView->selectionInfo()->selection();
00265 }
00266 
00267 SortDialog::Orientation SortDialog::guessDataOrientation()
00268 {
00269     const QRect selection=sourceArea();
00270     
00271     if (selection.width() >= selection.height())
00272         return SortColumns;
00273     else
00274         return SortRows;
00275 }
00276 
00277 SortDialog::~SortDialog()
00278 {
00279   // no need to delete child widgets, Qt does it all for us
00280 }
00281 
00282 void SortDialog::init()
00283 {
00284     QStringList lst;
00285     lst<<i18n("January");
00286     lst<<i18n("February");
00287     lst<<i18n("March");
00288     lst<<i18n("April");
00289     lst<<i18n("May");
00290     lst<<i18n("June");
00291     lst<<i18n("July");
00292     lst<<i18n("August");
00293     lst<<i18n("September");
00294     lst<<i18n("October");
00295     lst<<i18n("November");
00296     lst<<i18n("December");
00297 
00298     lst<<i18n("Monday");
00299     lst<<i18n("Tuesday");
00300     lst<<i18n("Wednesday");
00301     lst<<i18n("Thursday");
00302     lst<<i18n("Friday");
00303     lst<<i18n("Saturday");
00304     lst<<i18n("Sunday");
00305 
00306   KConfig * config = Factory::global()->config();
00307   config->setGroup( "Parameters" );
00308   QStringList other = config->readListEntry("Other list");
00309   QString tmp;
00310   for ( QStringList::Iterator it = other.begin(); it != other.end(); ++it )
00311   {
00312     if((*it) != "\\")
00313       tmp += (*it) + ", ";
00314     else if( it != other.begin())
00315     {
00316       tmp = tmp.left(tmp.length() - 2);
00317       lst.append(tmp);
00318       tmp = "";
00319     }
00320   }
00321   m_customList->insertStringList(lst);
00322 
00323   /*QPtrList<Sheet> sheetList = m_pView->doc()->map()->sheetList();
00324   for (unsigned int c = 0; c < sheetList.count(); ++c)
00325   {
00326     Sheet * t = sheetList.at(c);
00327     if (!t)
00328       continue;
00329     m_outputSheet->insertItem( t->sheetName() );
00330   }
00331   m_outputSheet->setCurrentText( m_pView->activeSheet()->sheetName() );*/
00332 
00333   QRect r = sourceArea();
00334   /*QString cellArea;
00335   cellArea += Cell::columnName(r.left());
00336   cellArea += QString::number( r.top() );
00337   m_outputCell->setText( cellArea );*/
00338   
00339   //If the top-most row or left-most column all contain text items (as opposed to numbers, dates etc.)
00340   //then the dialog will guess that the top row (or left column) is a header.  
00341   //The user can always change this if we get this wrong.
00342   bool selectionMayHaveHeader = true;
00343 
00344   // Entire columns selected ?
00345   if ( util_isColumnSelected(r) )
00346   {
00347     m_sortRow->setEnabled(false);
00348     m_sortColumn->setChecked(true);
00349 
00350     int right = r.right();
00351     for (int i = r.left(); i <= right; ++i)
00352     {  
00353         QString guessName=m_pView->activeSheet()->guessColumnTitle(r,i);
00354         QString colName=i18n(" (Column %1)").arg(Cell::columnName(i));
00355         
00356         if (!guessName.isEmpty())
00357         {
00358             m_listColumn += guessName + colName;
00359         }
00360         else
00361         {
00362         m_listColumn += i18n("Column %1").arg(Cell::columnName(i));
00363         
00364         if ( i == r.left() )
00365           selectionMayHaveHeader=false;
00366             }
00367     }
00368      // m_listColumn += i18n("Column %1").arg(Cell::columnName(i));
00369   }
00370   // Entire rows selected ?
00371   else if ( util_isRowSelected(r) )
00372   {
00373     m_sortColumn->setEnabled(false);
00374     m_sortRow->setChecked(true);
00375 
00376     int bottom = r.bottom();
00377     for (int i = r.top(); i <= bottom; ++i)
00378     {
00379         QString guessName=m_pView->activeSheet()->guessRowTitle(r,i);
00380         QString rowName=i18n(" (Row %1)").arg(i);
00381         
00382         if (!guessName.isEmpty())
00383         {
00384             m_listRow += guessName + rowName;
00385         }
00386         else
00387         {
00388         m_listRow += i18n("Row %1").arg(i);
00389         
00390         if ( i == r.top() )
00391           selectionMayHaveHeader=false;
00392             }
00393     }
00394   }
00395   else
00396   {
00397     // Selection is only one row
00398     if ( r.top() == r.bottom() )
00399     {
00400       m_sortColumn->setEnabled(false);
00401       m_sortRow->setChecked(true);
00402     }
00403     // only one column
00404     else if (r.left() == r.right())
00405     {
00406       m_sortRow->setEnabled(false);
00407       m_sortColumn->setChecked(true);
00408     }
00409     else
00410     {
00411         if (guessDataOrientation() == SortColumns)
00412             m_sortRow->setChecked(true);
00413         else
00414             m_sortColumn->setChecked(true);
00415     }
00416 
00417     int right  = r.right();
00418     int bottom = r.bottom();
00419     for (int i = r.left(); i <= right; ++i)
00420     {  
00421         QString guessName=m_pView->activeSheet()->guessColumnTitle(r,i);
00422         QString colName=i18n(" (Column %1)").arg(Cell::columnName(i));
00423         
00424         if (!guessName.isEmpty())
00425             m_listColumn += guessName + colName;
00426         else
00427         {
00428             m_listColumn += i18n("Column %1").arg(Cell::columnName(i));
00429             
00430             if (i == r.left())
00431               selectionMayHaveHeader=false;
00432             }
00433     }
00434 
00435     for (int i = r.top(); i <= bottom; ++i) 
00436     {
00437         QString guessName=m_pView->activeSheet()->guessRowTitle(r,i);
00438         QString rowName=i18n(" (Row %1)").arg(i);
00439         
00440         if (!guessName.isEmpty())
00441             m_listRow += guessName + rowName;
00442         else
00443         {
00444             m_listRow += i18n("Row %1").arg(i);
00445             
00446             if (i == r.top())
00447               selectionMayHaveHeader=false;
00448             }
00449     }
00450   }
00451 
00452   if ( selectionMayHaveHeader )
00453     m_firstRowOrColHeader->setChecked( true );
00454   else
00455     m_firstRowOrColHeader->setChecked( false );  
00456 
00457   // Initialize the combo box
00458   if ( m_sortRow->isChecked() )
00459   {
00460     slotOrientationChanged( SortRows );
00461   }
00462   else
00463   {
00464     slotOrientationChanged( SortColumns );
00465   }
00466 }
00467 
00468 void SortDialog::slotOrientationChanged(int id)
00469 {
00470   switch( id )
00471   {
00472    case SortColumns :
00473     m_sortKey1->clear();
00474     m_sortKey2->clear();
00475     m_sortKey3->clear();
00476     m_sortKey1->insertStringList(m_listColumn);
00477     m_sortKey2->insertItem( i18n("None") );
00478     m_sortKey2->insertStringList(m_listColumn);
00479     m_sortKey3->insertItem( i18n("None") );
00480     m_sortKey3->insertStringList(m_listColumn);
00481     m_firstRowOrColHeader->setText( i18n( "&First row contains headers" ) );
00482     break;
00483 
00484    case SortRows :
00485     m_sortKey1->clear();
00486     m_sortKey2->clear();
00487     m_sortKey3->clear();
00488     m_sortKey1->insertStringList(m_listRow);
00489     m_sortKey2->insertItem( i18n("None") );
00490     m_sortKey2->insertStringList(m_listRow);
00491     m_sortKey3->insertItem( i18n("None") );
00492     m_sortKey3->insertStringList(m_listRow);
00493     m_firstRowOrColHeader->setText( i18n( "&First column contains headers" ) );
00494 
00495     /*if (m_firstRowOrColHeader->isChecked())
00496     {
00497       int k1 = m_sortKey1->currentItem();
00498       int k2 = m_sortKey2->currentItem();
00499       int k3 = m_sortKey3->currentItem();
00500       m_sortKey1->removeItem( 0 );
00501       m_sortKey2->removeItem( 1 ); // because there is "None" in there
00502       m_sortKey3->removeItem( 1 );
00503       if (k1 > 0)
00504         m_sortKey1->setCurrentItem(--k1);
00505       else
00506         m_sortKey1->setCurrentItem( 0 );
00507       if (k2 > 0)
00508         m_sortKey2->setCurrentItem(--k2);
00509       if (k3 > 0)
00510         m_sortKey3->setCurrentItem(--k3);
00511     }*/
00512 
00513     break;
00514 
00515    default :
00516     kdDebug(36001) << "Error in signal : pressed(int id)" << endl;
00517     break;
00518   }
00519 }
00520 
00521 void SortDialog::slotOk()
00522 {
00523   m_pView->doc()->emitBeginOperation( false );
00524   
00525   Orientation sortOrientation;
00526   if (m_sortRow->isChecked())
00527     sortOrientation=SortColumns;
00528   else
00529     sortOrientation=SortRows;
00530 
00531   Sheet * sheet = m_pView->activeSheet(); 
00532   /*m_pView->doc()->map()->findSheet( m_outputSheet->currentText() );
00533   if ( !sheet )
00534   {
00535     KMessageBox::error( this, i18n("The destination sheet does not exist.") );
00536     m_outputSheet->setFocus();
00537     m_tabWidget->setTabEnabled(m_page2, true);
00538     m_pView->slotUpdateView( m_pView->activeSheet() );
00539     return;
00540   }  */
00541 
00542   /*if ( !outputPoint.isValid() || outputPoint.isSheetKnown() )
00543   {
00544     KMessageBox::error( this, i18n("The destination cell does not exist.") );
00545     m_outputCell->setFocus();
00546     m_tabWidget->setTabEnabled(m_page2, true);
00547     m_pView->slotUpdateView( m_pView->activeSheet() );
00548     return;
00549   }*/
00550   //outputPoint.setSheet(sheet);
00551 
00552   QRect sortArea = sourceArea();
00553   Point outputPoint;
00554     outputPoint.setPos(sortArea.topLeft());
00555     outputPoint.setSheet(sheet);  
00556   bool hasHeader=m_firstRowOrColHeader->isChecked();
00557   
00558   if ( hasHeader )
00559   {
00560     if (sortOrientation == SortColumns)
00561     {
00562         sortArea.setLeft( sortArea.left()+1 );
00563         outputPoint.setColumn( outputPoint.column()+1 ); 
00564     }
00565     else
00566     {
00567         sortArea.setTop( sortArea.top()+1 );
00568         outputPoint.setRow( outputPoint.row()+1 );
00569     }
00570   }
00571   
00572   /*if ( sortArea.topLeft() != outputPoint.pos() )
00573   {
00574     int h = outputPoint.pos().y() + sortArea.height();
00575     int w = outputPoint.pos().x() + sortArea.width();
00576 
00577     if ( sortArea.contains(outputPoint.pos())
00578          || ( w >= sortArea.left() && w <= sortArea.right() )
00579          || ( h >= sortArea.top()  && h <= sortArea.bottom() ) )
00580     {
00581       KMessageBox::error( this, i18n("If the destination and source regions are different, they must not overlap.") );
00582       m_outputCell->setFocus();
00583       m_pView->slotUpdateView( m_pView->activeSheet() );
00584       // TODO: set right tab
00585       return;
00586     }
00587   }*/
00588 
00589   int key1 = 1;
00590   int key2 = 0;
00591   int key3 = 0;
00592   QStringList * firstKey = 0L;
00593   Sheet::SortingOrder order1;
00594   Sheet::SortingOrder order2;
00595   Sheet::SortingOrder order3;
00596 
00597   order1 = ( m_sortOrder1->currentItem() == 0 ? Sheet::Increase
00598              : Sheet::Decrease );
00599   order2 = ( m_sortOrder2->currentItem() == 0 ? Sheet::Increase
00600              : Sheet::Decrease );
00601   order3 = ( m_sortOrder3->currentItem() == 0 ? Sheet::Increase
00602              : Sheet::Decrease );
00603 
00604   if ( m_sortRow->isChecked() )
00605   {
00606     key1 = m_sortKey1->currentItem() + sortArea.top();
00607     if (m_sortKey2->currentItem() > 0)
00608       key2 = m_sortKey2->currentItem() + sortArea.top() - 1; // cause there is "None"
00609     if (m_sortKey3->currentItem() > 0)
00610       key3 = m_sortKey3->currentItem() + sortArea.top() - 1; // cause there is "None"
00611   }
00612   else
00613   {
00614     key1 = m_sortKey1->currentItem() + sortArea.left();
00615     if (m_sortKey2->currentItem() > 0)
00616       key2 = m_sortKey2->currentItem() + sortArea.left() - 1; // cause there is "None"
00617     if (m_sortKey3->currentItem() > 0)
00618       key3 = m_sortKey3->currentItem() + sortArea.left() - 1; // cause there is "None"
00619       
00620   }
00621   /*
00622   if (m_firstRowOrColHeader->isChecked())
00623     {
00624       if (key1 >= 0)
00625         ++key1;
00626       if (key2 > 0)
00627         ++key2;
00628       if (key3 > 0)
00629         ++key3;
00630     }*/
00631 
00632   if ( m_useCustomLists->isChecked() )
00633   {
00634     firstKey = new QStringList();
00635     QString list = m_customList->currentText();
00636     QString tmp;
00637     int l = list.length();
00638     for ( int i = 0; i < l; ++i )
00639     {
00640       if ( list[i] == ',' )
00641       {
00642         firstKey->append( tmp.stripWhiteSpace() );
00643         tmp = "";
00644       }
00645       else
00646         tmp += list[i];
00647     }
00648   }
00649   
00650 
00651 
00652   if (key1 == key2)
00653     key2 = 0;
00654 
00655   if (key1 == key3)
00656     key3 = 0;
00657 
00658   if (key2 == 0 && key3 > 0)
00659   {
00660     key2 = key3;
00661     key3 = 0;
00662   }
00663 
00664 
00665   
00666   if ( m_sortRow->isChecked() )
00667   {    
00668     m_pView->activeSheet()->sortByRow( sortArea/*sourceArea*/, key1, key2, key3,
00669                                        order1, order2, order3,
00670                                        firstKey, m_copyLayout->isChecked(),
00671                                        false /*m_firstRowOrColHeader->isChecked()*/,
00672                                        outputPoint, m_respectCase->isChecked() );
00673   }
00674   else if (m_sortColumn->isChecked())
00675   {     
00676     m_pView->activeSheet()->sortByColumn( sortArea /*sourceArea*/, key1, key2, key3,
00677                                           order1, order2, order3,
00678                                           firstKey, m_copyLayout->isChecked(),
00679                                           false/*m_firstRowOrColHeader->isChecked()*/,
00680                                           outputPoint, m_respectCase->isChecked() );
00681   }
00682   else
00683   {
00684     kdDebug(36001) << "Err in radiobutton" << endl;
00685   }
00686 
00687   delete firstKey;
00688   firstKey = 0L;
00689 
00690   m_pView->slotUpdateView( m_pView->activeSheet() );
00691   accept();
00692 }
00693 
00694 void SortDialog::sortKey2textChanged( int i )
00695 {
00696   m_sortKey3->setEnabled( ( i!=0 ) );
00697   m_sortOrder3->setEnabled( ( i!=0 ) );
00698 }
00699 
00700 void SortDialog::useCustomListsStateChanged( int state )
00701 {
00702   if (state == 0)
00703     m_customList->setEnabled(false);
00704   else if (state == 2)
00705     m_customList->setEnabled(true);
00706 }
00707 
00708 void SortDialog::firstRowHeaderChanged( int /*state*/ )
00709 {
00710  /* if (m_sortColumn->isChecked())
00711     return;
00712 
00713   if (state == 0) // off
00714   {
00715     int k1 = m_sortKey1->currentItem();
00716     int k2 = m_sortKey2->currentItem();
00717     int k3 = m_sortKey3->currentItem();
00718     m_sortKey1->clear();
00719     m_sortKey2->clear();
00720     m_sortKey3->clear();
00721     m_sortKey1->insertStringList( m_listRow );
00722     m_sortKey2->insertItem( i18n("None") );
00723     m_sortKey2->insertStringList( m_listRow );
00724     m_sortKey3->insertItem( i18n("None") );
00725     m_sortKey3->insertStringList( m_listRow );
00726 
00727     m_sortKey1->setCurrentItem(++k1);
00728     m_sortKey2->setCurrentItem(++k2);
00729     m_sortKey3->setCurrentItem(++k3);
00730   }
00731   else if (state == 2) // on
00732   {
00733     int k1 = m_sortKey1->currentItem();
00734     int k2 = m_sortKey2->currentItem();
00735     int k3 = m_sortKey3->currentItem();
00736     m_sortKey1->removeItem( 0 );
00737     m_sortKey2->removeItem( 1 ); // because there is "None" in there
00738     m_sortKey3->removeItem( 1 );
00739     if (k1 > 0)
00740       m_sortKey1->setCurrentItem(--k1);
00741     if (k2 > 0)
00742       m_sortKey2->setCurrentItem(--k2);
00743     if (k3 > 0)
00744       m_sortKey3->setCurrentItem(--k3);
00745   }*/
00746 }
00747 
00748 #include "kspread_dlg_sort.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys