Diffstat (limited to 'noncore/apps/tableviewer/ui/tvkeyedit.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/tableviewer/ui/tvkeyedit.cpp | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/noncore/apps/tableviewer/ui/tvkeyedit.cpp b/noncore/apps/tableviewer/ui/tvkeyedit.cpp new file mode 100644 index 0000000..fb7b7fe --- a/dev/null +++ b/noncore/apps/tableviewer/ui/tvkeyedit.cpp | |||
@@ -0,0 +1,254 @@ | |||
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 "tvkeyedit.h" | ||
21 | #include <qtoolbutton.h> | ||
22 | #include <qlineedit.h> | ||
23 | #include <qcombobox.h> | ||
24 | #include <qlistview.h> | ||
25 | #include <qmessagebox.h> | ||
26 | #include <stdlib.h> | ||
27 | #include <qpushbutton.h> | ||
28 | |||
29 | /* QList view item... ?? that can store and update the values that I will | ||
30 | * be changing */ | ||
31 | |||
32 | class TVKEListViewItem : public QListViewItem | ||
33 | { | ||
34 | public: | ||
35 | TVKEListViewItem(QString n, TVVariant::KeyType kt, int p, QListView *parent) : | ||
36 | QListViewItem(parent) | ||
37 | { | ||
38 | name = n; | ||
39 | keyType = kt; | ||
40 | position = p; | ||
41 | } | ||
42 | |||
43 | QString text(int i) const | ||
44 | { | ||
45 | if(i) { | ||
46 | return TVVariant::typeToName(keyType); | ||
47 | } | ||
48 | return name; | ||
49 | } | ||
50 | |||
51 | /* always sort by key index, ignore i */ | ||
52 | QString key(int, bool) const | ||
53 | { | ||
54 | return QString().sprintf("%08d", position); | ||
55 | } | ||
56 | |||
57 | void setText(int i, const QString &) | ||
58 | { | ||
59 | ; | ||
60 | } | ||
61 | |||
62 | QString getName() const | ||
63 | { | ||
64 | return name; | ||
65 | } | ||
66 | |||
67 | void setName(QString n) | ||
68 | { | ||
69 | name = n; | ||
70 | repaint(); | ||
71 | } | ||
72 | |||
73 | TVVariant::KeyType getKeyType() const | ||
74 | { | ||
75 | return keyType; | ||
76 | } | ||
77 | |||
78 | void setKeyType(TVVariant::KeyType k) | ||
79 | { | ||
80 | keyType = k; | ||
81 | repaint(); | ||
82 | } | ||
83 | |||
84 | inline int getPos() const | ||
85 | { | ||
86 | return position; | ||
87 | } | ||
88 | |||
89 | private: | ||
90 | QString name; | ||
91 | TVVariant::KeyType keyType; | ||
92 | int position; | ||
93 | }; | ||
94 | |||
95 | TVKeyEdit::TVKeyEdit(TableState *t, QWidget* parent = 0, const char *name = 0, | ||
96 | WFlags fl = 0) : TVKeyEdit_gen(parent, name, true, fl) | ||
97 | { | ||
98 | int i; | ||
99 | ts = t; | ||
100 | |||
101 | if(!ts) return; | ||
102 | if(!ts->kRep) return; | ||
103 | |||
104 | working_state = *ts->kRep; | ||
105 | |||
106 | i = 1; | ||
107 | keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i)); | ||
108 | i++; | ||
109 | keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i)); | ||
110 | i++; | ||
111 | keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i)); | ||
112 | i++; | ||
113 | keyTypeEdit->insertItem(TVVariant::typeToName((TVVariant::KeyType)i)); | ||
114 | |||
115 | KeyListIterator it(*ts->kRep); | ||
116 | while(it.current()) { | ||
117 | if(t->kRep->validIndex(it.currentKey())) { | ||
118 | new TVKEListViewItem(it.current()->name(), | ||
119 | it.current()->type(), | ||
120 | it.currentKey(), | ||
121 | display); | ||
122 | } | ||
123 | ++it; | ||
124 | } | ||
125 | num_keys = ts->kRep->getNumFields(); | ||
126 | if(display->childCount() > 0) { | ||
127 | display->setCurrentItem(display->firstChild()); | ||
128 | setTerm(display->currentItem()); | ||
129 | } else { | ||
130 | deleteKeyButton->setEnabled(FALSE); | ||
131 | clearKeysButton->setEnabled(FALSE); | ||
132 | keyNameEdit->setEnabled(FALSE); | ||
133 | keyTypeEdit->setEnabled(FALSE); | ||
134 | } | ||
135 | |||
136 | display->setSorting(0); | ||
137 | #ifdef Q_WS_QWS | ||
138 | showMaximized(); | ||
139 | #endif | ||
140 | } | ||
141 | |||
142 | /*! | ||
143 | Destroys the TVKeyEdit widget | ||
144 | */ | ||
145 | TVKeyEdit::~TVKeyEdit() | ||
146 | { | ||
147 | } | ||
148 | |||
149 | /* SLOTS */ | ||
150 | void TVKeyEdit::newTerm() | ||
151 | { | ||
152 | /* new item, make current Item */ | ||
153 | int i; | ||
154 | |||
155 | i = working_state.addKey("<New Key>", TVVariant::String); | ||
156 | //working_state.setNewFlag(i, TRUE); | ||
157 | TVKEListViewItem *nItem = new TVKEListViewItem("<New Key>", | ||
158 | TVVariant::String, | ||
159 | i, | ||
160 | display); | ||
161 | display->setCurrentItem(nItem); | ||
162 | setTerm(nItem); | ||
163 | |||
164 | num_keys++; | ||
165 | if(display->childCount() == 1) { | ||
166 | deleteKeyButton->setEnabled(TRUE); | ||
167 | clearKeysButton->setEnabled(TRUE); | ||
168 | keyNameEdit->setEnabled(TRUE); | ||
169 | keyTypeEdit->setEnabled(TRUE); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | void TVKeyEdit::updateTerm(const QString &newName) | ||
174 | { | ||
175 | /* TODO if name matches a deleted term, prompt for | ||
176 | renewing old data instead */ | ||
177 | TVKEListViewItem *i = (TVKEListViewItem *)display->currentItem(); | ||
178 | if(i) { | ||
179 | i->setName(newName); | ||
180 | working_state.setKeyName(i->getPos(), newName); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | void TVKeyEdit::updateTerm(int t) | ||
185 | { | ||
186 | /* t is an index to a combo in a menu, NOT a type */ | ||
187 | t++; /* menu counts from 0, types count from 1 */ | ||
188 | TVKEListViewItem *i = (TVKEListViewItem *)display->currentItem(); | ||
189 | if (i) { | ||
190 | i->setKeyType((TVVariant::KeyType)t); | ||
191 | working_state.setKeyType(i->getPos(), (TVVariant::KeyType)t); | ||
192 | } | ||
193 | } | ||
194 | |||
195 | /* deletes current term | ||
196 | * really just marks key as deleted so is now invalid. | ||
197 | * the actual delete will happen when data is 'cleaned' | ||
198 | * or when file is saved. | ||
199 | */ | ||
200 | |||
201 | void TVKeyEdit::deleteTerm() | ||
202 | { | ||
203 | TVKEListViewItem *i = (TVKEListViewItem *)display->currentItem(); | ||
204 | if (i) { | ||
205 | working_state.setDeleteFlag(i->getPos(), TRUE); | ||
206 | delete i; | ||
207 | } | ||
208 | if(!display->childCount()) { | ||
209 | /* disable the delete and clear buttons, etc */ | ||
210 | deleteKeyButton->setEnabled(FALSE); | ||
211 | clearKeysButton->setEnabled(FALSE); | ||
212 | keyNameEdit->setEnabled(FALSE); | ||
213 | keyTypeEdit->setEnabled(FALSE); | ||
214 | } | ||
215 | } | ||
216 | |||
217 | /* clears all terminations */ | ||
218 | void TVKeyEdit::clearTerms() | ||
219 | { | ||
220 | /* should pop up a warning */ | ||
221 | if (QMessageBox::warning(this, "Delete all keys", | ||
222 | "Are you sure you want to\ndelete all the keys?", | ||
223 | "Yes", "No") == 0) | ||
224 | { | ||
225 | while(display->currentItem()) | ||
226 | deleteTerm(); | ||
227 | } | ||
228 | } | ||
229 | |||
230 | void TVKeyEdit::setTerm(QListViewItem *target) | ||
231 | { | ||
232 | /* need to update the widgets to show keys values */ | ||
233 | keyNameEdit->setText(((TVKEListViewItem *)target)->getName()); | ||
234 | int t = (int)(((TVKEListViewItem *)target)->getKeyType()); | ||
235 | t--; | ||
236 | keyTypeEdit->setCurrentItem(t); | ||
237 | } | ||
238 | |||
239 | KeyList* TVKeyEdit::openEditKeysDialog(TableState *t, QWidget *parent = 0) | ||
240 | { | ||
241 | if(!t) | ||
242 | return 0; | ||
243 | if(!t->kRep) | ||
244 | return 0; | ||
245 | |||
246 | TVKeyEdit *dlg = new TVKeyEdit(t, parent); | ||
247 | |||
248 | if ((dlg->exec() == QDialog::Accepted) && | ||
249 | (dlg->working_state != *t->kRep)) | ||
250 | { | ||
251 | return (new KeyList(dlg->working_state)); | ||
252 | } | ||
253 | return 0; | ||
254 | } | ||