Diffstat (limited to 'noncore/apps/tableviewer/ui/tvlistview.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/tableviewer/ui/tvlistview.cpp | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/noncore/apps/tableviewer/ui/tvlistview.cpp b/noncore/apps/tableviewer/ui/tvlistview.cpp new file mode 100644 index 0000000..82d67c6 --- a/dev/null +++ b/noncore/apps/tableviewer/ui/tvlistview.cpp | |||
@@ -0,0 +1,315 @@ | |||
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 "tvlistview.h" | ||
21 | #include "../db/common.h" | ||
22 | #include <qtoolbutton.h> | ||
23 | #include <qlistview.h> | ||
24 | #include <qlayout.h> | ||
25 | |||
26 | void TVListViewPrivate::setColumnWidth(int column, int width) | ||
27 | { | ||
28 | if(width > 70) width = 70; | ||
29 | QListView::setColumnWidth(column, width); | ||
30 | } | ||
31 | |||
32 | void TVListViewPrivate::setSorting(int column, bool increasing) | ||
33 | { | ||
34 | emit sortChanged(column); | ||
35 | QListView::setSorting(column, increasing); | ||
36 | } | ||
37 | |||
38 | TVListViewPrivate::TVListViewPrivate(QWidget *parent, const char* name, | ||
39 | WFlags fl) : QListView(parent, name, fl) { | ||
40 | ; | ||
41 | } | ||
42 | |||
43 | class TVListViewItem : public QListViewItem | ||
44 | { | ||
45 | public: | ||
46 | |||
47 | TVListViewItem(QListView *parent, DataElem *d); | ||
48 | ~TVListViewItem(); | ||
49 | |||
50 | QString text(int i) const | ||
51 | { | ||
52 | return data_reference->toQString(i); | ||
53 | } | ||
54 | |||
55 | /* Do nothing... all data for this item should be generated */ | ||
56 | void setText(int i, const QString &) | ||
57 | { | ||
58 | ; | ||
59 | } | ||
60 | QString key(int i, bool a) const | ||
61 | { | ||
62 | return data_reference->toSortableQString(i); | ||
63 | } | ||
64 | |||
65 | void setDataElem(DataElem *d) | ||
66 | { | ||
67 | data_reference = d; | ||
68 | } | ||
69 | |||
70 | DataElem *getDataElem() { | ||
71 | return data_reference; | ||
72 | } | ||
73 | private: | ||
74 | DataElem *data_reference; | ||
75 | }; | ||
76 | |||
77 | TVListViewItem::TVListViewItem(QListView *parent, DataElem *d) | ||
78 | : QListViewItem(parent) | ||
79 | { | ||
80 | data_reference = d; | ||
81 | } | ||
82 | |||
83 | TVListViewItem::~TVListViewItem() | ||
84 | { | ||
85 | data_reference = 0; | ||
86 | } | ||
87 | |||
88 | TVListView::TVListView(TableState *t, QWidget* parent = 0, | ||
89 | const char *name = 0, WFlags fl =0) : QWidget(parent, name, fl) | ||
90 | { | ||
91 | if (!name) | ||
92 | setName("TVListView"); | ||
93 | |||
94 | // the next two lines need to be rationalized. | ||
95 | resize(318,457); | ||
96 | setSizePolicy(QSizePolicy((QSizePolicy::SizeType)7, | ||
97 | (QSizePolicy::SizeType)7, sizePolicy().hasHeightForWidth())); | ||
98 | setCaption(tr("List View")); | ||
99 | |||
100 | QVBoxLayout *layout = new QVBoxLayout(this); | ||
101 | layout->setSpacing(0); | ||
102 | layout->setMargin(0); | ||
103 | |||
104 | listViewDisplay = new TVListViewPrivate(this, "listViewDisplay"); | ||
105 | layout->addWidget(listViewDisplay); | ||
106 | |||
107 | connect(listViewDisplay, SIGNAL(currentChanged(QListViewItem *)), this, | ||
108 | SLOT(setCurrent(QListViewItem *))); | ||
109 | connect(listViewDisplay, SIGNAL(sortChanged(int)), this, | ||
110 | SLOT(setSorting(int))); | ||
111 | |||
112 | listViewDisplay->setShowSortIndicator(true); | ||
113 | |||
114 | it = new QListViewItemIterator(listViewDisplay); | ||
115 | ts = t; | ||
116 | } | ||
117 | |||
118 | TVListView::~TVListView() | ||
119 | { | ||
120 | } | ||
121 | |||
122 | void TVListView::addItem(DataElem *d) | ||
123 | { | ||
124 | TVListViewItem *i = new TVListViewItem(listViewDisplay, d); | ||
125 | |||
126 | delete it; | ||
127 | it = new QListViewItemIterator(i); | ||
128 | } | ||
129 | |||
130 | /* remove current (it) item */ | ||
131 | void TVListView::removeItem() | ||
132 | { | ||
133 | QListViewItemIterator other(*it); | ||
134 | |||
135 | QListViewItemIterator tmp = *it; | ||
136 | (*it)++; | ||
137 | if (!it->current()) { | ||
138 | *it = tmp; | ||
139 | (*it)--; | ||
140 | if (!it->current()) { | ||
141 | delete it; | ||
142 | it = 0; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | delete other.current(); | ||
147 | } | ||
148 | |||
149 | void TVListView::clearItems() | ||
150 | { | ||
151 | /* This is ok since the destructor for TVListItem does not know about | ||
152 | the data_reference pointer.. and hence will leave it alone */ | ||
153 | listViewDisplay->clear(); | ||
154 | delete it; | ||
155 | it = new QListViewItemIterator(listViewDisplay); | ||
156 | } | ||
157 | |||
158 | void TVListView::first() | ||
159 | { | ||
160 | delete it; | ||
161 | it = new QListViewItemIterator(listViewDisplay); | ||
162 | } | ||
163 | |||
164 | void TVListView::last() | ||
165 | { | ||
166 | qWarning("TVListView::last not yet implemented"); | ||
167 | } | ||
168 | |||
169 | void TVListView::next() | ||
170 | { | ||
171 | QListViewItemIterator tmp = *it; | ||
172 | (*it)++; | ||
173 | if (!it->current()) { | ||
174 | *it = tmp; | ||
175 | } | ||
176 | } | ||
177 | |||
178 | void TVListView::previous() | ||
179 | { | ||
180 | QListViewItemIterator tmp = *it; | ||
181 | (*it)--; | ||
182 | if (!it->current()) { | ||
183 | *it = tmp; | ||
184 | } | ||
185 | } | ||
186 | |||
187 | DataElem *TVListView::getCurrentData() { | ||
188 | if (it->current()) { | ||
189 | return ((TVListViewItem *)it->current())->getDataElem(); | ||
190 | } | ||
191 | return NULL; | ||
192 | } | ||
193 | |||
194 | /*! Now to implement the closest match function */ | ||
195 | void TVListView::findItem(int keyId, TVVariant value) | ||
196 | { | ||
197 | QListViewItem *i; | ||
198 | TVListViewItem *best_so_far = NULL; | ||
199 | /* start at the beginning... go through till find the closest elem */ | ||
200 | i = listViewDisplay->firstChild(); | ||
201 | while (i) { | ||
202 | /* search stuff */ | ||
203 | if(best_so_far) { | ||
204 | if (DataElem::closer( | ||
205 | ((TVListViewItem *)i)->getDataElem(), | ||
206 | best_so_far->getDataElem(), value, keyId)) | ||
207 | best_so_far = (TVListViewItem *)i; | ||
208 | } else { | ||
209 | if (DataElem::closer( | ||
210 | ((TVListViewItem *)i)->getDataElem(), | ||
211 | NULL, value, keyId)) | ||
212 | best_so_far = (TVListViewItem *)i; | ||
213 | } | ||
214 | |||
215 | i = i->itemBelow(); | ||
216 | } | ||
217 | if (best_so_far) { | ||
218 | /* set best_so_far to current element */ | ||
219 | delete it; | ||
220 | it = new QListViewItemIterator(best_so_far); | ||
221 | } | ||
222 | } | ||
223 | |||
224 | void TVListView::rebuildKeys() | ||
225 | { | ||
226 | int i; | ||
227 | if(!ts) return; | ||
228 | if(!ts->kRep) return; | ||
229 | |||
230 | i = listViewDisplay->columns(); | ||
231 | |||
232 | while(i > 0) | ||
233 | listViewDisplay->removeColumn(--i); | ||
234 | |||
235 | KeyListIterator kit(*ts->kRep); | ||
236 | i = 0; | ||
237 | while(kit.current()) { | ||
238 | if(!kit.current()->delFlag()) { | ||
239 | listViewDisplay->addColumn(kit.current()->name()); | ||
240 | keyIds.insert(i, kit.currentKey()); | ||
241 | ++i; | ||
242 | } | ||
243 | ++kit; | ||
244 | } | ||
245 | } | ||
246 | |||
247 | |||
248 | void TVListView::setSorting(int column) | ||
249 | { | ||
250 | /* Without table state can't do anything */ | ||
251 | if (ts == 0) | ||
252 | return; | ||
253 | if (keyIds[column] != ts->current_column) { | ||
254 | ts->current_column = keyIds[column]; | ||
255 | } | ||
256 | } | ||
257 | |||
258 | void TVListView::rebuildData() { | ||
259 | int i; | ||
260 | QMap<int, int>::Iterator kit; | ||
261 | /* Need to set sort order */ | ||
262 | if(!ts) | ||
263 | return; | ||
264 | |||
265 | /* revers lookup the column */ | ||
266 | i = -1; | ||
267 | for(kit = keyIds.begin(); kit != keyIds.end(); ++kit) { | ||
268 | if (kit.data() == ts->current_column) { | ||
269 | i = kit.key(); | ||
270 | break; | ||
271 | } | ||
272 | } | ||
273 | if (i == -1) | ||
274 | return; | ||
275 | |||
276 | listViewDisplay->setSorting(i); | ||
277 | listViewDisplay->sort(); | ||
278 | |||
279 | /* reset current element */ | ||
280 | listViewDisplay->setCurrentItem(it->current()); | ||
281 | listViewDisplay->setSelected(it->current(), true); | ||
282 | listViewDisplay->ensureItemVisible(it->current()); | ||
283 | } | ||
284 | |||
285 | void TVListView::reset() | ||
286 | { | ||
287 | int i; | ||
288 | listViewDisplay->clear(); | ||
289 | |||
290 | i = listViewDisplay->columns(); | ||
291 | while (i > 0) | ||
292 | listViewDisplay->removeColumn(--i); | ||
293 | |||
294 | keyIds.clear(); | ||
295 | } | ||
296 | |||
297 | void TVListView::setCurrent(QListViewItem *i) | ||
298 | { | ||
299 | /* cast */ | ||
300 | TVListViewItem *t = (TVListViewItem *)i; | ||
301 | |||
302 | if(!t) { | ||
303 | /* set current to null */ | ||
304 | ts->current_elem = 0; | ||
305 | return; | ||
306 | } | ||
307 | |||
308 | ts->current_elem = t->getDataElem(); | ||
309 | /* now also set up the iterator */ | ||
310 | |||
311 | delete it; | ||
312 | it = new QListViewItemIterator(i); | ||
313 | |||
314 | //emit browseView(); | ||
315 | } | ||