author | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
---|---|---|
committer | kergoth <kergoth> | 2002-01-25 22:14:26 (UTC) |
commit | 15318cad33835e4e2dc620d033e43cd930676cdd (patch) (unidiff) | |
tree | c2fa0399a2c47fda8e2cd0092c73a809d17f68eb /noncore/apps/tableviewer/db/common.h | |
download | opie-15318cad33835e4e2dc620d033e43cd930676cdd.zip opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.gz opie-15318cad33835e4e2dc620d033e43cd930676cdd.tar.bz2 |
Initial revision
Diffstat (limited to 'noncore/apps/tableviewer/db/common.h') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/apps/tableviewer/db/common.h | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/noncore/apps/tableviewer/db/common.h b/noncore/apps/tableviewer/db/common.h new file mode 100644 index 0000000..bb0a953 --- a/dev/null +++ b/noncore/apps/tableviewer/db/common.h | |||
@@ -0,0 +1,285 @@ | |||
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 | |||
21 | |||
22 | /* This file represents shared data structures that will be passed | ||
23 | * around often. | ||
24 | */ | ||
25 | #ifndef __SHAREDDATA_H__ | ||
26 | #define __SHAREDDATA_H__ | ||
27 | |||
28 | // TODO rename this to a sensable class name | ||
29 | |||
30 | #include <qvector.h> | ||
31 | #include <qstring.h> | ||
32 | #include <qdatetime.h> | ||
33 | #include <qcstring.h> | ||
34 | #include <qdatastream.h> | ||
35 | #include <qintdict.h> | ||
36 | |||
37 | class DBStore; | ||
38 | |||
39 | /* helper classes to common classes */ | ||
40 | class QStringVector : public QVector<QString> | ||
41 | { | ||
42 | public: | ||
43 | int compareItems(Item a, Item b); | ||
44 | }; | ||
45 | |||
46 | /* in QT 2.3, dates and times not supported int QVariant. So..... | ||
47 | * for now use my special Variant type which is basically identical | ||
48 | * except that does it for my types. TODO replace with QVariant when | ||
49 | * qvariant supports all the types we require */ | ||
50 | |||
51 | class TVVariantPrivate; | ||
52 | |||
53 | class TVVariant | ||
54 | { | ||
55 | public: | ||
56 | enum KeyType { | ||
57 | Invalid = 0, | ||
58 | Int, | ||
59 | String, | ||
60 | Date, | ||
61 | Time, | ||
62 | }; | ||
63 | |||
64 | TVVariant(); | ||
65 | ~TVVariant(); | ||
66 | |||
67 | TVVariant(const TVVariant&); | ||
68 | TVVariant(QDataStream&); | ||
69 | |||
70 | TVVariant(const QString &); | ||
71 | TVVariant(const int); | ||
72 | TVVariant(const QDate &); | ||
73 | TVVariant(const QTime &); | ||
74 | |||
75 | TVVariant& operator=(const TVVariant& ); | ||
76 | bool operator==(const TVVariant&) const; | ||
77 | bool operator!=(const TVVariant&) const; | ||
78 | bool operator<(const TVVariant&) const; | ||
79 | bool operator>(const TVVariant&) const; | ||
80 | |||
81 | bool closer(TVVariant, TVVariant); | ||
82 | bool close(TVVariant); | ||
83 | |||
84 | KeyType type() const; | ||
85 | const QString typeName() const; | ||
86 | int numTypes() const; | ||
87 | |||
88 | const QString typeName(KeyType) const; | ||
89 | bool canCast(KeyType) const; | ||
90 | bool isValid() const; | ||
91 | void clear(); | ||
92 | |||
93 | const QString toString() const; | ||
94 | const QDate toDate() const; | ||
95 | const QTime toTime() const; | ||
96 | int toInt() const; | ||
97 | |||
98 | QString& asString(); | ||
99 | QDate& asDate(); | ||
100 | QTime& asTime(); | ||
101 | int& asInt(); | ||
102 | |||
103 | void load(QDataStream&); | ||
104 | void save(QDataStream&) const; | ||
105 | |||
106 | static const QString typeToName(KeyType typ); | ||
107 | static KeyType nameToType(const QString &); | ||
108 | private: | ||
109 | void detach(); | ||
110 | |||
111 | TVVariantPrivate *d; | ||
112 | }; | ||
113 | |||
114 | class TVVariantPrivate : public QShared | ||
115 | { | ||
116 | public: | ||
117 | TVVariantPrivate(); | ||
118 | TVVariantPrivate(TVVariantPrivate *); | ||
119 | |||
120 | ~TVVariantPrivate(); | ||
121 | |||
122 | void clear(); | ||
123 | |||
124 | TVVariant::KeyType typ; | ||
125 | |||
126 | union { | ||
127 | int i; | ||
128 | void *ptr; | ||
129 | } value; | ||
130 | }; | ||
131 | |||
132 | inline TVVariant::KeyType TVVariant::type() const | ||
133 | { | ||
134 | return d->typ; | ||
135 | } | ||
136 | |||
137 | inline bool TVVariant::isValid() const | ||
138 | { | ||
139 | return (d->typ != Invalid); | ||
140 | } | ||
141 | |||
142 | inline int TVVariant::numTypes() const | ||
143 | { | ||
144 | return 4; | ||
145 | } | ||
146 | |||
147 | class Key { | ||
148 | public: | ||
149 | Key(); | ||
150 | Key(QString name, TVVariant example, int flags = 0); | ||
151 | Key(const Key &); | ||
152 | Key& operator=(const Key& ); | ||
153 | |||
154 | QString name() const; | ||
155 | TVVariant example() const; | ||
156 | TVVariant::KeyType type() const; | ||
157 | int flags() const; | ||
158 | |||
159 | void setName(const QString &); | ||
160 | void setExample(const TVVariant &); | ||
161 | void setFlags(int); | ||
162 | |||
163 | bool delFlag() const; | ||
164 | bool newFlag() const; | ||
165 | |||
166 | void setDelFlag(bool); | ||
167 | void setNewFlag(bool); | ||
168 | |||
169 | private: | ||
170 | QString kname; | ||
171 | TVVariant kexample; | ||
172 | int kflags; | ||
173 | }; | ||
174 | |||
175 | class KeyList : public QIntDict<Key> { | ||
176 | public: | ||
177 | KeyList(); | ||
178 | KeyList(const KeyList&); | ||
179 | |||
180 | ~KeyList(); | ||
181 | |||
182 | bool operator!=(const KeyList &); | ||
183 | |||
184 | int getNumFields() const; | ||
185 | |||
186 | int addKey(QString KeyName, TVVariant example); | ||
187 | int addKey(QString KeyName, TVVariant::KeyType type); | ||
188 | |||
189 | TVVariant getKeyExample(int ) const; | ||
190 | void setKeyExample(int, TVVariant e); | ||
191 | |||
192 | QString getKeyName(int i) const; | ||
193 | void setKeyName(int i, const QString &n); | ||
194 | |||
195 | TVVariant::KeyType getKeyType(int i) const; | ||
196 | void setKeyType(int i, TVVariant::KeyType); | ||
197 | |||
198 | int getKeyIndex(QString q) const; | ||
199 | |||
200 | int getKeyFlags(int i) const; | ||
201 | void setKeyFlags(int i, int flag); | ||
202 | |||
203 | /* Below should be abstracted a bit more */ | ||
204 | bool checkNewFlag(int i) const; | ||
205 | void setNewFlag(int i, bool f); | ||
206 | bool checkDeleteFlag(int i) const; | ||
207 | void setDeleteFlag(int i, bool f); | ||
208 | |||
209 | bool validIndex(int) const; | ||
210 | }; | ||
211 | |||
212 | class KeyListIterator : public QIntDictIterator<Key> | ||
213 | { | ||
214 | public: | ||
215 | KeyListIterator(const KeyList &k) : QIntDictIterator<Key>(k) {}; | ||
216 | }; | ||
217 | |||
218 | /* TODO start using this */ | ||
219 | class DataElem { | ||
220 | public: | ||
221 | DataElem(DBStore *container); | ||
222 | ~DataElem(); | ||
223 | |||
224 | int getNumFields() const; | ||
225 | KeyList getKeys() const; | ||
226 | |||
227 | bool hasValidValue(int) const; | ||
228 | bool hasValidValue(QString) const; | ||
229 | TVVariant::KeyType getFieldType(int) const; | ||
230 | TVVariant::KeyType getFieldType(QString) const; | ||
231 | TVVariant getField(int) const; | ||
232 | TVVariant getField(QString) const; | ||
233 | |||
234 | void setField(int, QString); | ||
235 | void setField(int, TVVariant); | ||
236 | void setField(QString, QString); | ||
237 | void setField(QString, TVVariant); | ||
238 | void unsetField(int); | ||
239 | void unsetField(QString); | ||
240 | |||
241 | QString toQString() const; | ||
242 | QString toQString(int i) const; | ||
243 | QString toSortableQString(int i) const; | ||
244 | |||
245 | /* compare functions */ | ||
246 | bool lessThan(int i, TVVariant) const; | ||
247 | bool moreThan(int i, TVVariant) const; | ||
248 | bool equalTo(int i, TVVariant) const; | ||
249 | bool contains(int i, TVVariant) const; | ||
250 | bool startsWith(int i, TVVariant) const; | ||
251 | bool endsWith(int i, TVVariant) const; | ||
252 | |||
253 | /* class functions... Compare is based of the primary key, which | ||
254 | is determined by the containing DBStores of each element. */ | ||
255 | static int compare(const TVVariant, const TVVariant, int i); | ||
256 | |||
257 | /* False, second element's primary key is closer to target. | ||
258 | * True, first element's primary key is a closer match to target */ | ||
259 | static bool closer(DataElem*, DataElem *, TVVariant, int column); | ||
260 | private: | ||
261 | QIntDict<TVVariant> values; | ||
262 | DBStore *contained; | ||
263 | }; | ||
264 | |||
265 | typedef struct _TableState { | ||
266 | int current_column; | ||
267 | KeyList *kRep; | ||
268 | DataElem *current_elem; | ||
269 | } TableState; | ||
270 | |||
271 | /* Stream functions */ | ||
272 | #ifndef QT_NO_DATASTREAM | ||
273 | Q_EXPORT QDataStream &operator<<( QDataStream &, const KeyList & ); | ||
274 | Q_EXPORT QDataStream &operator<<( QDataStream &, const DataElem & ); | ||
275 | Q_EXPORT QDataStream &operator>>( QDataStream &, KeyList & ); | ||
276 | Q_EXPORT QDataStream &operator>>( QDataStream &, DataElem & ); | ||
277 | |||
278 | |||
279 | Q_EXPORT QDataStream &operator>>( QDataStream &, TVVariant & ); | ||
280 | Q_EXPORT QDataStream &operator<<( QDataStream &, const TVVariant & ); | ||
281 | Q_EXPORT QDataStream &operator>>( QDataStream &, TVVariant::KeyType& ); | ||
282 | Q_EXPORT QDataStream &operator<<( QDataStream &, const TVVariant::KeyType& ); | ||
283 | #endif | ||
284 | |||
285 | #endif | ||