00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
00080
00081
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
00097 m_firstRowOrColHeader = new QCheckBox( layoutGroup, "m_copyLayout" );
00098
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
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
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
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
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
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333 QRect r = sourceArea();
00334
00335
00336
00337
00338
00339
00340
00341
00342 bool selectionMayHaveHeader = true;
00343
00344
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
00369 }
00370
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
00398 if ( r.top() == r.bottom() )
00399 {
00400 m_sortColumn->setEnabled(false);
00401 m_sortRow->setChecked(true);
00402 }
00403
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
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
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
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
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
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
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
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;
00609 if (m_sortKey3->currentItem() > 0)
00610 key3 = m_sortKey3->currentItem() + sortArea.top() - 1;
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;
00617 if (m_sortKey3->currentItem() > 0)
00618 key3 = m_sortKey3->currentItem() + sortArea.left() - 1;
00619
00620 }
00621
00622
00623
00624
00625
00626
00627
00628
00629
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, key1, key2, key3,
00669 order1, order2, order3,
00670 firstKey, m_copyLayout->isChecked(),
00671 false ,
00672 outputPoint, m_respectCase->isChecked() );
00673 }
00674 else if (m_sortColumn->isChecked())
00675 {
00676 m_pView->activeSheet()->sortByColumn( sortArea , key1, key2, key3,
00677 order1, order2, order3,
00678 firstKey, m_copyLayout->isChecked(),
00679 false,
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 )
00709 {
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746 }
00747
00748 #include "kspread_dlg_sort.moc"