Diffstat (limited to 'noncore/apps/opie-sheet/sortdlg.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/opie-sheet/sortdlg.cpp | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/noncore/apps/opie-sheet/sortdlg.cpp b/noncore/apps/opie-sheet/sortdlg.cpp new file mode 100644 index 0000000..f46562e --- a/dev/null +++ b/noncore/apps/opie-sheet/sortdlg.cpp | |||
@@ -0,0 +1,178 @@ | |||
1 | #include <qlabel.h> | ||
2 | #include <qradiobutton.h> | ||
3 | #include <qmessagebox.h> | ||
4 | #include "sortdlg.h" | ||
5 | |||
6 | SortDialog::SortDialog(QWidget *parent=0) | ||
7 | :QDialog(parent, 0, TRUE) | ||
8 | { | ||
9 | // Main widget | ||
10 | tabs=new QTabWidget(this); | ||
11 | widgetSort=new QWidget(tabs); | ||
12 | widgetOptions=new QWidget(tabs); | ||
13 | tabs->addTab(widgetSort, tr("&Sort")); | ||
14 | tabs->addTab(widgetOptions, tr("&Options")); | ||
15 | |||
16 | // Sort tab | ||
17 | comboFieldA=createFieldCombo(tr("&Sort by"), 10); | ||
18 | groupOrderA=createOrderButtons(10); | ||
19 | comboFieldB=createFieldCombo(tr("&Then by"), 90); | ||
20 | groupOrderB=createOrderButtons(90); | ||
21 | comboFieldC=createFieldCombo(tr("Then &by"), 170); | ||
22 | groupOrderC=createOrderButtons(170); | ||
23 | |||
24 | // Options tab | ||
25 | checkCase=new QCheckBox(tr("&Case Sensitive"), widgetOptions); | ||
26 | checkCase->setGeometry(10, 10, 215, 20); | ||
27 | checkCase->setChecked(TRUE); | ||
28 | |||
29 | groupDirection=new QVButtonGroup(tr("&Direction"), widgetOptions); | ||
30 | groupDirection->setGeometry(10, 40, 215, 70); | ||
31 | QRadioButton *radio=new QRadioButton(tr("&Top to bottom (rows)"), groupDirection); | ||
32 | radio=new QRadioButton(tr("&Left to right (columns)"), groupDirection); | ||
33 | groupDirection->setButton(0); | ||
34 | connect(groupDirection, SIGNAL(clicked(int)), this, SLOT(directionChanged(int))); | ||
35 | |||
36 | // Main widget | ||
37 | box=new QVBoxLayout(this); | ||
38 | box->addWidget(tabs); | ||
39 | |||
40 | setCaption(tr("Sort")); | ||
41 | } | ||
42 | |||
43 | SortDialog::~SortDialog() | ||
44 | { | ||
45 | } | ||
46 | |||
47 | QComboBox *SortDialog::createFieldCombo(const QString &caption, int y) | ||
48 | { | ||
49 | QLabel *label=new QLabel(caption, widgetSort); | ||
50 | label->setGeometry(10, y+5, 215, 20); | ||
51 | QComboBox *combo=new QComboBox(FALSE, widgetSort); | ||
52 | combo->setGeometry(10, y+35, 105, 20); | ||
53 | label->setBuddy(combo); | ||
54 | return combo; | ||
55 | } | ||
56 | |||
57 | QVButtonGroup *SortDialog::createOrderButtons(int y) | ||
58 | { | ||
59 | QVButtonGroup *group=new QVButtonGroup(widgetSort); | ||
60 | group->setGeometry(125, y, 100, 60); | ||
61 | QRadioButton *radio=new QRadioButton(tr("&Ascending"), group); | ||
62 | radio=new QRadioButton(tr("&Descending"), group); | ||
63 | group->setButton(0); | ||
64 | return group; | ||
65 | } | ||
66 | |||
67 | void SortDialog::directionChanged(int id) | ||
68 | { | ||
69 | direction=id; | ||
70 | fillFieldCombo(comboFieldA); | ||
71 | fillFieldCombo(comboFieldB); | ||
72 | fillFieldCombo(comboFieldC); | ||
73 | } | ||
74 | |||
75 | void SortDialog::fillFieldCombo(QComboBox *combo) | ||
76 | { | ||
77 | combo->clear(); | ||
78 | if (direction) | ||
79 | for (int row=row1; row<=row2; ++row) | ||
80 | combo->insertItem("Row "+QString::number(row+1)); | ||
81 | else | ||
82 | for (int col=col1; col<=col2; ++col) | ||
83 | combo->insertItem("Column "+Sheet::getHeaderString(col+1)); | ||
84 | } | ||
85 | |||
86 | int SortDialog::exec(Sheet *s) | ||
87 | { | ||
88 | sheet=s; | ||
89 | sheet->getSelection(&row1, &col1, &row2, &col2); | ||
90 | |||
91 | direction=0; | ||
92 | fillFieldCombo(comboFieldA); | ||
93 | fillFieldCombo(comboFieldB); | ||
94 | fillFieldCombo(comboFieldC); | ||
95 | |||
96 | if (row1==row2 && col1==col2) | ||
97 | { | ||
98 | QMessageBox::warning(this, tr("Error"), tr("One cell cannot be sorted!")); | ||
99 | return QDialog::Rejected; | ||
100 | } | ||
101 | if (QDialog::exec()==QDialog::Accepted) | ||
102 | { | ||
103 | QString field1S=comboFieldA->currentText(), field2S=comboFieldA->currentText(), field3S=comboFieldA->currentText(); | ||
104 | field1S=field1S.mid(field1S.find(' ')+1); | ||
105 | field2S=field2S.mid(field2S.find(' ')+1); | ||
106 | field3S=field3S.mid(field3S.find(' ')+1); | ||
107 | int field1, field2, field3; | ||
108 | if (direction) | ||
109 | { | ||
110 | field1=field1S.toInt()-1; | ||
111 | field2=field2S.toInt()-1; | ||
112 | field3=field3S.toInt()-1; | ||
113 | int i, j; | ||
114 | for (i=col2; i>=col1; --i) | ||
115 | for (j=col1+1; j<=i; ++j) | ||
116 | { | ||
117 | bool swap=FALSE; | ||
118 | int compareResult=compareItems(s->item(field1, j-1), s->item(field1, j), groupOrderA->id(groupOrderA->selected()), checkCase->isChecked()); | ||
119 | if (compareResult>0) swap=TRUE; | ||
120 | else if (compareResult==0) | ||
121 | { | ||
122 | compareResult=compareItems(s->item(field2, j-1), s->item(field2, j), groupOrderA->id(groupOrderB->selected()), checkCase->isChecked()); | ||
123 | if (compareResult>0) swap=TRUE; | ||
124 | else if (compareResult==0) | ||
125 | { | ||
126 | compareResult=compareItems(s->item(field3, j-1), s->item(field3, j), groupOrderA->id(groupOrderC->selected()), checkCase->isChecked()); | ||
127 | if (compareResult>0) swap=TRUE; | ||
128 | } | ||
129 | } | ||
130 | if (swap) | ||
131 | for (int row=row1; row<=row2; ++row) | ||
132 | s->swapCells(row, j-1, row, j); | ||
133 | } | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | field1=Sheet::getHeaderColumn(field1S)-1; | ||
138 | field2=Sheet::getHeaderColumn(field2S)-1; | ||
139 | field3=Sheet::getHeaderColumn(field3S)-1; | ||
140 | int i, j; | ||
141 | for (i=row2; i>=row1; --i) | ||
142 | for (j=row1+1; j<=i; ++j) | ||
143 | { | ||
144 | bool swap=FALSE; | ||
145 | int compareResult=compareItems(s->item(j-1, field1), s->item(j, field1), groupOrderA->id(groupOrderA->selected()), checkCase->isChecked()); | ||
146 | if (compareResult>0) swap=TRUE; | ||
147 | else if (compareResult==0) | ||
148 | { | ||
149 | compareResult=compareItems(s->item(j-1, field2), s->item(j, field2), groupOrderA->id(groupOrderB->selected()), checkCase->isChecked()); | ||
150 | if (compareResult>0) swap=TRUE; | ||
151 | else if (compareResult==0) | ||
152 | { | ||
153 | compareResult=compareItems(s->item(j-1, field3), s->item(j, field3), groupOrderA->id(groupOrderC->selected()), checkCase->isChecked()); | ||
154 | if (compareResult>0) swap=TRUE; | ||
155 | } | ||
156 | } | ||
157 | if (swap) | ||
158 | for (int col=col1; col<=col2; ++col) | ||
159 | s->swapCells(j-1, col, j, col); | ||
160 | } | ||
161 | } | ||
162 | return QDialog::Accepted; | ||
163 | } | ||
164 | return QDialog::Rejected; | ||
165 | } | ||
166 | |||
167 | int SortDialog::compareItems(QTableItem *item1, QTableItem *item2, int descending=0, bool caseSensitive=TRUE) | ||
168 | { | ||
169 | int result=0; | ||
170 | if (item1) | ||
171 | { | ||
172 | if (item2) result=(caseSensitive ? item1->text().compare(item2->text()) : item1->text().upper().compare(item2->text().upper())); | ||
173 | else result=-1; | ||
174 | } | ||
175 | else | ||
176 | if (item2) result=1; | ||
177 | return (descending ? -result : result); | ||
178 | } | ||