Diffstat (limited to 'noncore/apps/opie-sheet/sortdlg.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-sheet/sortdlg.cpp | 310 |
1 files changed, 165 insertions, 145 deletions
diff --git a/noncore/apps/opie-sheet/sortdlg.cpp b/noncore/apps/opie-sheet/sortdlg.cpp index c2cdec8..47d666f 100644 --- a/noncore/apps/opie-sheet/sortdlg.cpp +++ b/noncore/apps/opie-sheet/sortdlg.cpp | |||
@@ -1,193 +1,213 @@ | |||
1 | /*************************************************************************** | 1 | /* |
2 | * * | 2 | =. This file is part of the Opie Project |
3 | * This program is free software; you can redistribute it and/or modify * | 3 | .=l. Copyright (C) 2004 Opie Developer Team <opie-devel@handhelds.org> |
4 | * it under the terms of the GNU General Public License as published by * | 4 | .>+-= |
5 | * the Free Software Foundation; either version 2 of the License, or * | 5 | _;:, .> :=|. This program is free software; you can |
6 | * (at your option) any later version. * | 6 | .> <`_, > . <= redistribute it and/or modify it under |
7 | * * | 7 | :`=1 )Y*s>-.-- : the terms of the GNU General Public |
8 | ***************************************************************************/ | 8 | .="- .-=="i, .._ License as published by the Free Software |
9 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
10 | ._= =} : or (at your option) any later version. | ||
11 | .%`+i> _;_. | ||
12 | .i_,=:_. -<s. This program is distributed in the hope that | ||
13 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
14 | : .. .:, . . . without even the implied warranty of | ||
15 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
16 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
17 | ..}^=.= = ; Library General Public License for more | ||
18 | ++= -. .` .: details. | ||
19 | : = ...= . :.=- | ||
20 | -. .:....=;==+<; You should have received a copy of the GNU | ||
21 | -_. . . )=. = Library General Public License along with | ||
22 | -- :-=` this library; see the file COPYING.LIB. | ||
23 | If not, write to the Free Software Foundation, | ||
24 | Inc., 59 Temple Place - Suite 330, | ||
25 | Boston, MA 02111-1307, USA. | ||
26 | |||
27 | */ | ||
9 | 28 | ||
10 | /* | 29 | /* |
11 | * Opie Sheet (formerly Sheet/Qt) | 30 | * Opie Sheet (formerly Sheet/Qt) |
12 | * by Serdar Ozler <sozler@sitebest.com> | 31 | * by Serdar Ozler <sozler@sitebest.com> |
13 | */ | 32 | */ |
14 | 33 | ||
34 | #include "sortdlg.h" | ||
35 | |||
36 | /* QT */ | ||
15 | #include <qlabel.h> | 37 | #include <qlabel.h> |
16 | #include <qradiobutton.h> | 38 | #include <qradiobutton.h> |
17 | #include <qmessagebox.h> | 39 | #include <qmessagebox.h> |
18 | #include "sortdlg.h" | ||
19 | 40 | ||
20 | SortDialog::SortDialog(QWidget *parent) | 41 | SortDialog::SortDialog(QWidget *parent) |
21 | :QDialog(parent, 0, TRUE) | 42 | :QDialog(parent, 0, TRUE) |
22 | { | 43 | { |
23 | // Main widget | 44 | // Main widget |
24 | tabs=new QTabWidget(this); | 45 | tabs=new QTabWidget(this); |
25 | widgetSort=new QWidget(tabs); | 46 | widgetSort=new QWidget(tabs); |
26 | widgetOptions=new QWidget(tabs); | 47 | widgetOptions=new QWidget(tabs); |
27 | tabs->addTab(widgetSort, tr("&Sort")); | 48 | tabs->addTab(widgetSort, tr("&Sort")); |
28 | tabs->addTab(widgetOptions, tr("&Options")); | 49 | tabs->addTab(widgetOptions, tr("&Options")); |
29 | 50 | ||
30 | // Sort tab | 51 | // Sort tab |
31 | comboFieldA=createFieldCombo(tr("&Sort by"), 10); | 52 | comboFieldA=createFieldCombo(tr("&Sort by"), 10); |
32 | groupOrderA=createOrderButtons(10); | 53 | groupOrderA=createOrderButtons(10); |
33 | comboFieldB=createFieldCombo(tr("&Then by"), 90); | 54 | comboFieldB=createFieldCombo(tr("&Then by"), 90); |
34 | groupOrderB=createOrderButtons(90); | 55 | groupOrderB=createOrderButtons(90); |
35 | comboFieldC=createFieldCombo(tr("Then &by"), 170); | 56 | comboFieldC=createFieldCombo(tr("Then &by"), 170); |
36 | groupOrderC=createOrderButtons(170); | 57 | groupOrderC=createOrderButtons(170); |
37 | 58 | ||
38 | // Options tab | 59 | // Options tab |
39 | checkCase=new QCheckBox(tr("&Case Sensitive"), widgetOptions); | 60 | checkCase=new QCheckBox(tr("&Case Sensitive"), widgetOptions); |
40 | checkCase->setGeometry(10, 10, 215, 20); | 61 | checkCase->setGeometry(10, 10, 215, 20); |
41 | checkCase->setChecked(TRUE); | 62 | checkCase->setChecked(TRUE); |
42 | 63 | ||
43 | groupDirection=new QVButtonGroup(tr("&Direction"), widgetOptions); | 64 | groupDirection=new QVButtonGroup(tr("&Direction"), widgetOptions); |
44 | groupDirection->setGeometry(10, 40, 215, 70); | 65 | groupDirection->setGeometry(10, 40, 215, 70); |
45 | QRadioButton *radio=new QRadioButton(tr("&Top to bottom (rows)"), groupDirection); | 66 | QRadioButton *radio=new QRadioButton(tr("&Top to bottom (rows)"), groupDirection); |
46 | radio=new QRadioButton(tr("&Left to right (columns)"), groupDirection); | 67 | radio=new QRadioButton(tr("&Left to right (columns)"), groupDirection); |
47 | groupDirection->setButton(0); | 68 | groupDirection->setButton(0); |
48 | connect(groupDirection, SIGNAL(clicked(int)), this, SLOT(directionChanged(int))); | 69 | connect(groupDirection, SIGNAL(clicked(int)), this, SLOT(directionChanged(int))); |
49 | 70 | ||
50 | // Main widget | 71 | // Main widget |
51 | box=new QVBoxLayout(this); | 72 | box=new QVBoxLayout(this); |
52 | box->addWidget(tabs); | 73 | box->addWidget(tabs); |
53 | 74 | ||
54 | setCaption(tr("Sort")); | 75 | setCaption(tr("Sort")); |
55 | } | 76 | } |
56 | 77 | ||
57 | SortDialog::~SortDialog() | 78 | SortDialog::~SortDialog() |
58 | { | 79 | {} |
59 | } | ||
60 | 80 | ||
61 | QComboBox *SortDialog::createFieldCombo(const QString &caption, int y) | 81 | QComboBox *SortDialog::createFieldCombo(const QString &caption, int y) |
62 | { | 82 | { |
63 | QLabel *label=new QLabel(caption, widgetSort); | 83 | QLabel *label=new QLabel(caption, widgetSort); |
64 | label->setGeometry(10, y+5, 215, 20); | 84 | label->setGeometry(10, y+5, 215, 20); |
65 | QComboBox *combo=new QComboBox(FALSE, widgetSort); | 85 | QComboBox *combo=new QComboBox(FALSE, widgetSort); |
66 | combo->setGeometry(10, y+35, 105, 20); | 86 | combo->setGeometry(10, y+35, 105, 20); |
67 | label->setBuddy(combo); | 87 | label->setBuddy(combo); |
68 | return combo; | 88 | return combo; |
69 | } | 89 | } |
70 | 90 | ||
71 | QVButtonGroup *SortDialog::createOrderButtons(int y) | 91 | QVButtonGroup *SortDialog::createOrderButtons(int y) |
72 | { | 92 | { |
73 | QVButtonGroup *group=new QVButtonGroup(widgetSort); | 93 | QVButtonGroup *group=new QVButtonGroup(widgetSort); |
74 | group->setGeometry(125, y, 100, 60); | 94 | group->setGeometry(125, y, 100, 60); |
75 | QRadioButton *radio=new QRadioButton(tr("&Ascending"), group); | 95 | QRadioButton *radio=new QRadioButton(tr("&Ascending"), group); |
76 | radio=new QRadioButton(tr("&Descending"), group); | 96 | radio=new QRadioButton(tr("&Descending"), group); |
77 | group->setButton(0); | 97 | group->setButton(0); |
78 | return group; | 98 | return group; |
79 | } | 99 | } |
80 | 100 | ||
81 | void SortDialog::directionChanged(int id) | 101 | void SortDialog::directionChanged(int id) |
82 | { | 102 | { |
83 | direction=id; | 103 | direction=id; |
84 | fillFieldCombo(comboFieldA); | 104 | fillFieldCombo(comboFieldA); |
85 | fillFieldCombo(comboFieldB); | 105 | fillFieldCombo(comboFieldB); |
86 | fillFieldCombo(comboFieldC); | 106 | fillFieldCombo(comboFieldC); |
87 | } | 107 | } |
88 | 108 | ||
89 | void SortDialog::fillFieldCombo(QComboBox *combo) | 109 | void SortDialog::fillFieldCombo(QComboBox *combo) |
90 | { | 110 | { |
91 | combo->clear(); | 111 | combo->clear(); |
92 | if (direction) | 112 | if (direction) |
93 | for (int row=row1; row<=row2; ++row) | 113 | for (int row=row1; row<=row2; ++row) |
94 | combo->insertItem("Row "+QString::number(row+1)); | 114 | combo->insertItem("Row "+QString::number(row+1)); |
95 | else | 115 | else |
96 | for (int col=col1; col<=col2; ++col) | 116 | for (int col=col1; col<=col2; ++col) |
97 | combo->insertItem("Column "+Sheet::getHeaderString(col+1)); | 117 | combo->insertItem("Column "+Sheet::getHeaderString(col+1)); |
98 | } | 118 | } |
99 | 119 | ||
100 | int SortDialog::exec(Sheet *s) | 120 | int SortDialog::exec(Sheet *s) |
101 | { | 121 | { |
102 | sheet=s; | 122 | sheet=s; |
103 | sheet->getSelection(&row1, &col1, &row2, &col2); | 123 | sheet->getSelection(&row1, &col1, &row2, &col2); |
104 | 124 | ||
105 | direction=0; | 125 | direction=0; |
106 | fillFieldCombo(comboFieldA); | 126 | fillFieldCombo(comboFieldA); |
107 | fillFieldCombo(comboFieldB); | 127 | fillFieldCombo(comboFieldB); |
108 | fillFieldCombo(comboFieldC); | 128 | fillFieldCombo(comboFieldC); |
109 | 129 | ||
110 | if (row1==row2 && col1==col2) | 130 | if (row1==row2 && col1==col2) |
111 | { | ||
112 | QMessageBox::warning(this, tr("Error"), tr("One cell cannot be sorted!")); | ||
113 | return QDialog::Rejected; | ||
114 | } | ||
115 | if (QDialog::exec()==QDialog::Accepted) | ||
116 | { | ||
117 | QString field1S=comboFieldA->currentText(), field2S=comboFieldB->currentText(), field3S=comboFieldC->currentText(); | ||
118 | field1S=field1S.mid(field1S.find(' ')+1); | ||
119 | field2S=field2S.mid(field2S.find(' ')+1); | ||
120 | field3S=field3S.mid(field3S.find(' ')+1); | ||
121 | int field1, field2, field3; | ||
122 | if (direction) | ||
123 | { | 131 | { |
124 | field1=field1S.toInt()-1; | 132 | QMessageBox::warning(this, tr("Error"), tr("One cell cannot be sorted!")); |
125 | field2=field2S.toInt()-1; | 133 | return QDialog::Rejected; |
126 | field3=field3S.toInt()-1; | ||
127 | int i, j; | ||
128 | for (i=col2; i>=col1; --i) | ||
129 | for (j=col1+1; j<=i; ++j) | ||
130 | { | ||
131 | bool swap=FALSE; | ||
132 | int compareResult=compareItems(s->item(field1, j-1), s->item(field1, j), groupOrderA->id(groupOrderA->selected()), checkCase->isChecked()); | ||
133 | if (compareResult>0) swap=TRUE; | ||
134 | else if (compareResult==0) | ||
135 | { | ||
136 | compareResult=compareItems(s->item(field2, j-1), s->item(field2, j), groupOrderB->id(groupOrderB->selected()), checkCase->isChecked()); | ||
137 | if (compareResult>0) swap=TRUE; | ||
138 | else if (compareResult==0) | ||
139 | { | ||
140 | compareResult=compareItems(s->item(field3, j-1), s->item(field3, j), groupOrderC->id(groupOrderC->selected()), checkCase->isChecked()); | ||
141 | if (compareResult>0) swap=TRUE; | ||
142 | } | ||
143 | } | ||
144 | if (swap) | ||
145 | for (int row=row1; row<=row2; ++row) | ||
146 | s->swapCells(row, j-1, row, j); | ||
147 | } | ||
148 | } | 134 | } |
149 | else | 135 | if (QDialog::exec()==QDialog::Accepted) |
150 | { | 136 | { |
151 | field1=Sheet::getHeaderColumn(field1S)-1; | 137 | QString field1S=comboFieldA->currentText(), field2S=comboFieldB->currentText(), field3S=comboFieldC->currentText(); |
152 | field2=Sheet::getHeaderColumn(field2S)-1; | 138 | field1S=field1S.mid(field1S.find(' ')+1); |
153 | field3=Sheet::getHeaderColumn(field3S)-1; | 139 | field2S=field2S.mid(field2S.find(' ')+1); |
154 | int i, j; | 140 | field3S=field3S.mid(field3S.find(' ')+1); |
155 | for (i=row2; i>=row1; --i) | 141 | int field1, field2, field3; |
156 | for (j=row1+1; j<=i; ++j) | 142 | if (direction) |
143 | { | ||
144 | field1=field1S.toInt()-1; | ||
145 | field2=field2S.toInt()-1; | ||
146 | field3=field3S.toInt()-1; | ||
147 | int i, j; | ||
148 | for (i=col2; i>=col1; --i) | ||
149 | for (j=col1+1; j<=i; ++j) | ||
150 | { | ||
151 | bool swap=FALSE; | ||
152 | int compareResult=compareItems(s->item(field1, j-1), s->item(field1, j), groupOrderA->id(groupOrderA->selected()), checkCase->isChecked()); | ||
153 | if (compareResult>0) swap=TRUE; | ||
154 | else if (compareResult==0) | ||
155 | { | ||
156 | compareResult=compareItems(s->item(field2, j-1), s->item(field2, j), groupOrderB->id(groupOrderB->selected()), checkCase->isChecked()); | ||
157 | if (compareResult>0) swap=TRUE; | ||
158 | else if (compareResult==0) | ||
159 | { | ||
160 | compareResult=compareItems(s->item(field3, j-1), s->item(field3, j), groupOrderC->id(groupOrderC->selected()), checkCase->isChecked()); | ||
161 | if (compareResult>0) swap=TRUE; | ||
162 | } | ||
163 | } | ||
164 | if (swap) | ||
165 | for (int row=row1; row<=row2; ++row) | ||
166 | s->swapCells(row, j-1, row, j); | ||
167 | } | ||
168 | } | ||
169 | else | ||
157 | { | 170 | { |
158 | bool swap=FALSE; | 171 | field1=Sheet::getHeaderColumn(field1S)-1; |
159 | int compareResult=compareItems(s->item(j-1, field1), s->item(j, field1), groupOrderA->id(groupOrderA->selected()), checkCase->isChecked()); | 172 | field2=Sheet::getHeaderColumn(field2S)-1; |
160 | if (compareResult>0) swap=TRUE; | 173 | field3=Sheet::getHeaderColumn(field3S)-1; |
161 | else if (compareResult==0) | 174 | int i, j; |
162 | { | 175 | for (i=row2; i>=row1; --i) |
163 | compareResult=compareItems(s->item(j-1, field2), s->item(j, field2), groupOrderB->id(groupOrderB->selected()), checkCase->isChecked()); | 176 | for (j=row1+1; j<=i; ++j) |
164 | if (compareResult>0) swap=TRUE; | 177 | { |
165 | else if (compareResult==0) | 178 | bool swap=FALSE; |
166 | { | 179 | int compareResult=compareItems(s->item(j-1, field1), s->item(j, field1), groupOrderA->id(groupOrderA->selected()), checkCase->isChecked()); |
167 | compareResult=compareItems(s->item(j-1, field3), s->item(j, field3), groupOrderC->id(groupOrderC->selected()), checkCase->isChecked()); | 180 | if (compareResult>0) swap=TRUE; |
168 | if (compareResult>0) swap=TRUE; | 181 | else if (compareResult==0) |
169 | } | 182 | { |
170 | } | 183 | compareResult=compareItems(s->item(j-1, field2), s->item(j, field2), groupOrderB->id(groupOrderB->selected()), checkCase->isChecked()); |
171 | if (swap) | 184 | if (compareResult>0) swap=TRUE; |
172 | for (int col=col1; col<=col2; ++col) | 185 | else if (compareResult==0) |
173 | s->swapCells(j-1, col, j, col); | 186 | { |
187 | compareResult=compareItems(s->item(j-1, field3), s->item(j, field3), groupOrderC->id(groupOrderC->selected()), checkCase->isChecked()); | ||
188 | if (compareResult>0) swap=TRUE; | ||
189 | } | ||
190 | } | ||
191 | if (swap) | ||
192 | for (int col=col1; col<=col2; ++col) | ||
193 | s->swapCells(j-1, col, j, col); | ||
194 | } | ||
174 | } | 195 | } |
196 | return QDialog::Accepted; | ||
175 | } | 197 | } |
176 | return QDialog::Accepted; | 198 | return QDialog::Rejected; |
177 | } | ||
178 | return QDialog::Rejected; | ||
179 | } | 199 | } |
180 | 200 | ||
181 | int SortDialog::compareItems(QTableItem *item1, QTableItem *item2, | 201 | int SortDialog::compareItems(QTableItem *item1, QTableItem *item2, |
182 | int descending, bool caseSensitive) | 202 | int descending, bool caseSensitive) |
183 | { | 203 | { |
184 | int result=0; | 204 | int result=0; |
185 | if (item1) | 205 | if (item1) |
186 | { | 206 | { |
187 | if (item2) result=(caseSensitive ? item1->text().compare(item2->text()) : item1->text().upper().compare(item2->text().upper())); | 207 | if (item2) result=(caseSensitive ? item1->text().compare(item2->text()) : item1->text().upper().compare(item2->text().upper())); |
188 | else result=-1; | 208 | else result=-1; |
189 | } | 209 | } |
190 | else | 210 | else |
191 | if (item2) result=1; | 211 | if (item2) result=1; |
192 | return (descending ? -result : result); | 212 | return (descending ? -result : result); |
193 | } | 213 | } |