Diffstat (limited to 'noncore/apps/tableviewer/ui/tvfilterview.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/tableviewer/ui/tvfilterview.cpp | 304 |
1 files changed, 304 insertions, 0 deletions
diff --git a/noncore/apps/tableviewer/ui/tvfilterview.cpp b/noncore/apps/tableviewer/ui/tvfilterview.cpp new file mode 100644 index 0000000..72d39d6 --- a/dev/null +++ b/noncore/apps/tableviewer/ui/tvfilterview.cpp | |||
@@ -0,0 +1,304 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "tvfilterview.h" | ||
21 | #include <qtoolbutton.h> | ||
22 | #include <qcombobox.h> | ||
23 | #include <qlistview.h> | ||
24 | #include <qlayout.h> | ||
25 | #include <qheader.h> | ||
26 | #include <qpushbutton.h> | ||
27 | #include <qlabel.h> | ||
28 | |||
29 | TVFilterView::TVFilterView(TableState *t, QWidget* parent = 0, | ||
30 | const char *name = 0, WFlags fl =0) : QDialog(parent, name, TRUE, fl) | ||
31 | { | ||
32 | if ( !name ) | ||
33 | setName( "Filter View" ); | ||
34 | |||
35 | QVBoxLayout *vlayout = new QVBoxLayout(this); | ||
36 | |||
37 | display = new QListView(this, "display"); | ||
38 | display->addColumn("Key"); | ||
39 | display->addColumn("Constraint"); | ||
40 | display->addColumn("Value"); | ||
41 | display->header()->setClickEnabled(FALSE); | ||
42 | display->header()->setResizeEnabled(FALSE); | ||
43 | |||
44 | vlayout->addWidget(display); | ||
45 | |||
46 | QHBoxLayout *hlayout = new QHBoxLayout; | ||
47 | hlayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum)); | ||
48 | |||
49 | newFilterButton = new QPushButton(this, "new Filter"); | ||
50 | newFilterButton->setMaximumSize(QSize(50, 32767)); | ||
51 | newFilterButton->setText("New"); | ||
52 | hlayout->addWidget(newFilterButton); | ||
53 | |||
54 | deleteFilterButton = new QPushButton(this, "delete Filter"); | ||
55 | deleteFilterButton->setMaximumSize(QSize(50, 32767)); | ||
56 | deleteFilterButton->setText("Delete"); | ||
57 | hlayout->addWidget(deleteFilterButton); | ||
58 | |||
59 | clearFilterButton = new QPushButton(this, "delete Filter"); | ||
60 | clearFilterButton->setMaximumSize(QSize(60, 32767)); | ||
61 | clearFilterButton->setText("Clear All"); | ||
62 | hlayout->addWidget(clearFilterButton); | ||
63 | |||
64 | vlayout->addLayout(hlayout); | ||
65 | |||
66 | QHBoxLayout *hlayout2 = new QHBoxLayout; | ||
67 | |||
68 | keyNameCombo = new QComboBox(FALSE, this, "key name"); | ||
69 | keyNameCombo->setEnabled(FALSE); | ||
70 | hlayout2->addWidget(keyNameCombo); | ||
71 | |||
72 | QLabel *label = new QLabel(this); | ||
73 | label->setText("has value"); | ||
74 | hlayout2->addWidget(label); | ||
75 | |||
76 | keyEntry = new TVFilterKeyEntry(this, "key entry"); | ||
77 | keyEntry->setEnabled(FALSE); | ||
78 | |||
79 | vlayout->addLayout(hlayout2); | ||
80 | vlayout->addWidget(keyEntry); | ||
81 | |||
82 | connect(newFilterButton, SIGNAL( clicked() ), this, SLOT( newTerm() )); | ||
83 | connect(deleteFilterButton, SIGNAL( clicked() ), this, SLOT( deleteTerm())); | ||
84 | connect(clearFilterButton, SIGNAL( clicked() ), this, SLOT( clearTerms())); | ||
85 | |||
86 | connect(keyEntry, SIGNAL(valueChanged()), this, SLOT( updateTerm() )); | ||
87 | connect(keyNameCombo, SIGNAL(activated(int)), this, SLOT( updateTerm() )); | ||
88 | |||
89 | connect(display, SIGNAL(selectionChanged(QListViewItem*)), this, | ||
90 | SLOT(setTerm(QListViewItem *))); | ||
91 | |||
92 | ts = t; | ||
93 | current = 0; | ||
94 | terms.setAutoDelete(true); | ||
95 | do_filter = false; | ||
96 | |||
97 | #ifdef Q_WS_QWS | ||
98 | showMaximized(); | ||
99 | #endif | ||
100 | } | ||
101 | |||
102 | /*! | ||
103 | Destroys the TVFilterView widget | ||
104 | */ | ||
105 | TVFilterView::~TVFilterView() | ||
106 | { | ||
107 | } | ||
108 | |||
109 | void TVFilterView::rebuildData() | ||
110 | { | ||
111 | } | ||
112 | |||
113 | void TVFilterView::reset() | ||
114 | { | ||
115 | keyNameCombo->clear(); | ||
116 | keyIds.clear(); | ||
117 | } | ||
118 | |||
119 | void TVFilterView::rebuildKeys() | ||
120 | { | ||
121 | int i; | ||
122 | |||
123 | if (!ts) return; | ||
124 | if(!ts->kRep) return; | ||
125 | keyEntry->setTableState(ts); | ||
126 | |||
127 | /* set up the list of keys that can be compared on */ | ||
128 | keyNameCombo->clear(); | ||
129 | KeyListIterator it(*ts->kRep); | ||
130 | |||
131 | i = 0; | ||
132 | while(it.current()) { | ||
133 | if(ts->kRep->validIndex(it.currentKey())) { | ||
134 | keyNameCombo->insertItem(it.current()->name()); | ||
135 | keyIds.insert(i, it.currentKey()); | ||
136 | ++i; | ||
137 | } | ||
138 | ++it; | ||
139 | } | ||
140 | } | ||
141 | |||
142 | bool TVFilterView::passesFilter(DataElem *d) { | ||
143 | if (!filterActive()) return true; | ||
144 | |||
145 | |||
146 | FilterTerm *t; | ||
147 | |||
148 | for (t = terms.first(); t != 0; t = terms.next() ) { | ||
149 | /* check against filter */ | ||
150 | switch(t->ct) { | ||
151 | case ct_less: | ||
152 | if (!d->lessThan(t->keyIndex, t->value)) | ||
153 | return false; | ||
154 | break; | ||
155 | case ct_more: | ||
156 | if (!d->moreThan(t->keyIndex, t->value)) | ||
157 | return false; | ||
158 | break; | ||
159 | case ct_equal: | ||
160 | if (!d->equalTo(t->keyIndex, t->value)) | ||
161 | return false; | ||
162 | break; | ||
163 | case ct_contains: | ||
164 | if (!d->contains(t->keyIndex, t->value)) | ||
165 | return false; | ||
166 | break; | ||
167 | case ct_startswith: | ||
168 | if (!d->startsWith(t->keyIndex, t->value)) | ||
169 | return false; | ||
170 | break; | ||
171 | case ct_endswith: | ||
172 | if (!d->endsWith(t->keyIndex, t->value)) | ||
173 | return false; | ||
174 | break; | ||
175 | default: | ||
176 | qWarning("TVFilterView::passesFilter() " | ||
177 | "unrecognized filter type"); | ||
178 | return false; | ||
179 | } | ||
180 | } | ||
181 | return true; | ||
182 | } | ||
183 | |||
184 | bool TVFilterView::filterActive() const | ||
185 | { | ||
186 | /* when button operated, also check the do_filter value | ||
187 | return do_filter; | ||
188 | */ | ||
189 | if (terms.isEmpty()) | ||
190 | return false; | ||
191 | return true; | ||
192 | } | ||
193 | |||
194 | /* SLOTS */ | ||
195 | void TVFilterView::newTerm() | ||
196 | { | ||
197 | if (!ts) return; | ||
198 | |||
199 | FilterTerm *term = new FilterTerm; | ||
200 | current = term; | ||
201 | |||
202 | term->view = 0; | ||
203 | |||
204 | updateTerm(); | ||
205 | |||
206 | display->setSelected(term->view, true); | ||
207 | terms.append(term); | ||
208 | |||
209 | keyEntry->setEnabled(true); | ||
210 | keyNameCombo->setEnabled(true); | ||
211 | } | ||
212 | |||
213 | void TVFilterView::updateTerm() | ||
214 | { | ||
215 | FilterTerm *term; | ||
216 | /* Read the widget values (keyname, compare type, value) | ||
217 | * and build the lists */ | ||
218 | if (!ts) return; | ||
219 | if (!current) return; | ||
220 | |||
221 | QString keyString; | ||
222 | QString cmpString; | ||
223 | QString vString; | ||
224 | |||
225 | term = current; | ||
226 | |||
227 | /* create new list item, set initial values, enable widgets */ | ||
228 | term->keyIndex = keyIds[keyNameCombo->currentItem()]; | ||
229 | keyEntry->setKey(term->keyIndex); /* so the next two items make sense */ | ||
230 | term->ct = keyEntry->getCompareType(), | ||
231 | term->value = keyEntry->getCompareValue(); | ||
232 | |||
233 | keyString = keyNameCombo->currentText(); | ||
234 | |||
235 | switch(term->ct) { | ||
236 | case ct_less: | ||
237 | cmpString = " less than "; | ||
238 | break; | ||
239 | case ct_more: | ||
240 | cmpString = " more than "; | ||
241 | break; | ||
242 | case ct_equal: | ||
243 | cmpString = " equal to "; | ||
244 | break; | ||
245 | case ct_contains: | ||
246 | cmpString = " containing "; | ||
247 | break; | ||
248 | case ct_startswith: | ||
249 | cmpString = " starting with "; | ||
250 | break; | ||
251 | case ct_endswith: | ||
252 | cmpString = " ending with "; | ||
253 | break; | ||
254 | default: | ||
255 | cmpString = " ERROR "; | ||
256 | } | ||
257 | |||
258 | vString = term->value.toString(); | ||
259 | |||
260 | /* remove old view */ | ||
261 | if (term->view) | ||
262 | delete(term->view); | ||
263 | term->view = new QListViewItem(display, 0, keyString, cmpString, vString); | ||
264 | display->setSelected(term->view, true); | ||
265 | } | ||
266 | |||
267 | /* deletes current term */ | ||
268 | void TVFilterView::deleteTerm() | ||
269 | { | ||
270 | if(!current) return; | ||
271 | if (current->view) | ||
272 | delete(current->view); | ||
273 | |||
274 | terms.removeRef(current); | ||
275 | |||
276 | current = terms.first(); | ||
277 | |||
278 | if(terms.isEmpty()) { | ||
279 | keyEntry->setEnabled(false); | ||
280 | keyNameCombo->setEnabled(false); | ||
281 | } | ||
282 | } | ||
283 | |||
284 | /* clears all terminations */ | ||
285 | void TVFilterView::clearTerms() | ||
286 | { | ||
287 | while(current) | ||
288 | deleteTerm(); | ||
289 | } | ||
290 | |||
291 | void TVFilterView::setTerm(QListViewItem *target) | ||
292 | { | ||
293 | /* Iterate through the list to find item with view=target.. | ||
294 | * set as current, delete */ | ||
295 | FilterTerm *term = current; | ||
296 | |||
297 | for (current = terms.first(); current != 0; current = terms.next() ) | ||
298 | if (current->view == target) | ||
299 | break; | ||
300 | |||
301 | if (!current) { | ||
302 | current = term; | ||
303 | } | ||
304 | } | ||