author | zecke <zecke> | 2003-09-12 17:43:22 (UTC) |
---|---|---|
committer | zecke <zecke> | 2003-09-12 17:43:22 (UTC) |
commit | 730d32e606642b37c30a9cb28d6728543a2b6c89 (patch) (unidiff) | |
tree | 87409d891cba5885152c6d000e0ec4b3b8e400f0 | |
parent | 420565dbf396c813e2e657ddf725af075564e59f (diff) | |
download | opie-730d32e606642b37c30a9cb28d6728543a2b6c89.zip opie-730d32e606642b37c30a9cb28d6728543a2b6c89.tar.gz opie-730d32e606642b37c30a9cb28d6728543a2b6c89.tar.bz2 |
More implementation and thoughts
-rw-r--r-- | libopie/big-screen/omodalhelper.h | 199 |
1 files changed, 190 insertions, 9 deletions
diff --git a/libopie/big-screen/omodalhelper.h b/libopie/big-screen/omodalhelper.h index 32d921d..c483403 100644 --- a/libopie/big-screen/omodalhelper.h +++ b/libopie/big-screen/omodalhelper.h | |||
@@ -8,132 +8,313 @@ | |||
8 | .="- .-=="i, .._ License as published by the Free Software | 8 | .="- .-=="i, .._ License as published by the Free Software |
9 | - . .-<_> .<> Foundation; either version 2 of the License, | 9 | - . .-<_> .<> Foundation; either version 2 of the License, |
10 | ._= =} : or (at your option) any later version. | 10 | ._= =} : or (at your option) any later version. |
11 | .%`+i> _;_. | 11 | .%`+i> _;_. |
12 | .i_,=:_. -<s. This library is distributed in the hope that | 12 | .i_,=:_. -<s. This library is distributed in the hope that |
13 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | 13 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; |
14 | : .. .:, . . . without even the implied warranty of | 14 | : .. .:, . . . without even the implied warranty of |
15 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | 15 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A |
16 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | 16 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU |
17 | ..}^=.= = ; Library General Public License for more | 17 | ..}^=.= = ; Library General Public License for more |
18 | ++= -. .` .: details. | 18 | ++= -. .` .: details. |
19 | : = ...= . :.=- | 19 | : = ...= . :.=- |
20 | -. .:....=;==+<; You should have received a copy of the GNU | 20 | -. .:....=;==+<; You should have received a copy of the GNU |
21 | -_. . . )=. = Library General Public License along with | 21 | -_. . . )=. = Library General Public License along with |
22 | -- :-=` this library; see the file COPYING.LIB. | 22 | -- :-=` this library; see the file COPYING.LIB. |
23 | If not, write to the Free Software Foundation, | 23 | If not, write to the Free Software Foundation, |
24 | Inc., 59 Temple Place - Suite 330, | 24 | Inc., 59 Temple Place - Suite 330, |
25 | Boston, MA 02111-1307, USA. | 25 | Boston, MA 02111-1307, USA. |
26 | 26 | ||
27 | */ | 27 | */ |
28 | 28 | ||
29 | #ifndef HAZE_OMODAL_HELPER_H | 29 | #ifndef HAZE_OMODAL_HELPER_H |
30 | #define HAZE_OMODAL_HELPER_H | 30 | #define HAZE_OMODAL_HELPER_H |
31 | 31 | ||
32 | #include <qdialog.h> | ||
32 | #include <qwidget.h> | 33 | #include <qwidget.h> |
33 | #include <qvaluelist.h> | 34 | #include <qvaluelist.h> |
34 | #include <qmap.h> | 35 | #include <qmap.h> |
35 | 36 | ||
36 | typedef int TransactionID; | 37 | typedef int TransactionID; |
37 | 38 | ||
38 | class OModalHelperControler; | 39 | class OModalHelperControler; |
39 | class OModalHelperSignal; | 40 | class OModalHelperSignal; |
40 | class QDialog; | 41 | class QDialog; |
41 | 42 | ||
42 | struct OModalHelperBase { | 43 | struct OModalHelperBase { |
43 | virtual void done( TransactionID id, QWidget* ) = 0; | 44 | virtual void done( TransactionID ) = 0; |
44 | virtual void next( TransactionID, OModalHelperControler * ); | 45 | virtual void next( TransactionID ) = 0; |
45 | virtual void prev( TransactionID, OModalHelperControler * ); | 46 | virtual void prev( TransactionID ) = 0; |
46 | }; | 47 | }; |
47 | 48 | ||
48 | /** | 49 | /** |
49 | * Modality sucks! ;) But it is easy to work with | 50 | * Modality sucks! ;) But it is easy to work with |
50 | * do exec() on a dialog and you know everything is funky. | 51 | * do exec() on a dialog and you know everything is funky. |
51 | * You only need to have one Dialog loaded and so on. | 52 | * You only need to have one Dialog loaded and so on. |
52 | * This class helps you to work like with modality and help | 53 | * This class helps you to work like with modality and help |
53 | * you to keep things in sync | 54 | * you to keep things in sync |
54 | * It's a template class but it sends signals once one Item is ready | 55 | * It's a template class but it sends signals once one Item is ready |
55 | * the signals contains the status and id of the item and then you | 56 | * the signals contains the status and id of the item and then you |
56 | * can fetch it. | 57 | * can fetch it. |
57 | * Also if you edit an record external you can tell this class and it'll | 58 | * Also if you edit an record external you can tell this class and it'll |
58 | * call the merge() function of your widget to maybe merge in these changes. | 59 | * call the merge() function of your widget to maybe merge in these changes. |
59 | * It also supports multiple modes. Either it can create new dialogs | 60 | * It also supports multiple modes. Either it can create new dialogs |
60 | * for each item or it can queue them depending on your usage. But it is | 61 | * for each item or it can queue them depending on your usage. But it is |
61 | * so smart that if only one item is shown that the queue bar is not shown | 62 | * so smart that if only one item is shown that the queue bar is not shown |
62 | * See the example for simple usage. | 63 | * See the example for simple usage. |
63 | * | 64 | * |
64 | * @short helps to life without modaility | 65 | * @short helps to life without modaility |
65 | * @author hOlgAr | 66 | * @author hOlgAr |
66 | * @version 0.01 | 67 | * @version 0.01 |
67 | */ | 68 | */ |
68 | template<class Dialog, class Record, typename Id = QString> | 69 | template<class Dialog, class Record, typename Id = int> |
69 | class OModalHelper : private OModalHelperBase{ | 70 | class OModalHelper : private OModalHelperBase{ |
70 | friend class OModalHelperSignal; | 71 | friend class OModalHelperSignal; |
71 | friend class OModalHelperControler; | 72 | friend class OModalHelperControler; |
72 | public: | 73 | public: |
73 | typedef QValueList<Record> RecordList; | 74 | typedef QValueList<Record> RecordList; |
74 | typedef QMap<Id, Record> IdMap; | 75 | typedef QMap<Id, Record> IdMap; |
75 | typedef QMap<TransactionID, Id> TransactionMap; | 76 | typedef QMap<TransactionID, Id> TransactionMap; |
76 | typedef QMap<QDialog*, TransactionID> DialogMap | 77 | typedef QMap<QDialog*, TransactionID> DialogMap |
77 | enum Mode { Queue, New }; | 78 | enum Mode { Queue, New }; |
78 | OModalHelper(enum Mode mode, QObject* parnet ); | 79 | OModalHelper(enum Mode mode, QObject* parnet ); |
79 | 80 | ||
80 | bool handles( Id id)const; | 81 | bool handles( Id id)const; |
81 | 82 | ||
82 | void cancel(); | 83 | void cancel(); |
83 | void cancel( TransactionID ); | 84 | void cancel( TransactionID ); |
84 | 85 | ||
85 | void connect( QObject* rec, const char* slot ); | 86 | void connectDone( QObject* rec, const char* slot ); |
87 | void connectAccepted( QObject* rec, const char* slot ); | ||
88 | void connectRejected( QObject* rec, const char* slot ); | ||
86 | 89 | ||
87 | TransactionID handle( const Record& rec = Record() ); | 90 | TransactionID handle( Id id, const Record& rec = Record() ); |
88 | 91 | ||
89 | void edited( TransactionID, int what, const QString& data ); | 92 | void edited( TransactionID, int what, const QString& data ); |
90 | 93 | ||
91 | Record record( TransactionID )const; | 94 | Record record( TransactionID )const; |
92 | RecordList done()const; | 95 | RecordList done()const; |
93 | private: | 96 | private: |
97 | virtual void done( TransactionID, QDialog* ); | ||
98 | virtual void next( TransactionID, OModalHelperControler * ); | ||
99 | virtual void prev( TransactionID, OModalHelperControler * ); | ||
100 | |||
101 | Record nextRecord( TransactionID, int * )const; | ||
102 | Record prevRecord( TransactionID, int * )const; | ||
103 | |||
104 | private: | ||
105 | OModalHelperDialog *queuedDialog()const; // generate or recycle | ||
106 | OModalHelperDialog *m_dialog; | ||
107 | OModalHelperSignal *m_signal; // our signal | ||
108 | OModalHelperControler *m_controler; | ||
109 | IdMap m_ids; // maps ids (uids) to a record | ||
110 | TransactionMap m_transactions; // activate transactions | ||
111 | TransactionMap m_done; // done and waiting for getting picked | ||
112 | DialogMap m_editing; // only used for New Mode | ||
113 | enum Mode m_mode; // the mode we're in | ||
94 | }; | 114 | }; |
95 | 115 | ||
96 | 116 | ||
97 | 117 | ||
98 | /* ### FIXME use namespace with Qt3 */ | 118 | /* ### FIXME use namespace with Qt3 */ |
99 | 119 | ||
100 | /* | 120 | /* |
101 | * A note on flow. The Signal is used for QT Signals when | 121 | * A note on flow. The Signal is used for QT Signals when |
102 | * a record is done. | 122 | * a record is done. |
103 | * There is either one controler and this controler slot will | 123 | * There is either one controler and this controler slot will |
104 | * be connected to a dialog signal. | 124 | * be connected to a dialog signal. |
105 | * In Queue we get the next and prev signals and call the Helper. | 125 | * In Queue we get the next and prev signals and call the Helper. |
106 | * this then changes the Record of the dialog and sets the transactionId | 126 | * this then changes the Record of the dialog and sets the transactionId |
107 | * of the controler. | 127 | * of the controler. |
108 | * For the new mode | 128 | * For the new mode |
109 | * | 129 | * |
110 | */ | 130 | */ |
111 | 131 | ||
112 | class OModalHelperSignal : public QObject { | 132 | class OModalHelperSignal : public QObject { |
113 | Q_OBJECT | 133 | Q_OBJECT |
114 | public: | 134 | public: |
115 | OModalHelperSignal(); | 135 | OModalHelperSignal(OModalHelperBase* base, QObject* parent); |
116 | 136 | ||
117 | signals: | 137 | signals: |
118 | done( int status, TransactionID transaction ); | 138 | done( int status, TransactionID transaction ); |
119 | accpeted( TransactionID transaction ); | 139 | accepted( TransactionID transaction ); |
120 | rejected( TransactionID transaction ); | 140 | rejected( TransactionID transaction ); |
141 | |||
142 | private: | ||
143 | OModalHelperBase* m_base; | ||
121 | }; | 144 | }; |
122 | 145 | ||
123 | 146 | ||
124 | class OModalHelperControler : public QObject { | 147 | class OModalHelperControler : public QObject { |
125 | Q_OBJECT | 148 | Q_OBJECT |
126 | public: | 149 | public: |
127 | OModalHelperControler( TransactionID id ); | 150 | OModalHelperControler( OModalHelperBase* , QObject* parent); |
128 | virtual TransactionID transactionID()const; | 151 | virtual TransactionID transactionID()const; |
129 | void setTransactionID( TransactionID id ); | 152 | void setTransactionID( TransactionID id ); |
130 | QDialog* dialog()const; | 153 | QDialog* dialog()const; |
131 | 154 | ||
132 | public slots: | 155 | public slots: |
133 | virtual done(int result ); | 156 | virtual done(int result ); |
134 | private: | 157 | private: |
135 | QDialog *m_dia; | 158 | QDialog *m_dia; |
136 | TransactionID m_id; | 159 | TransactionID m_id; |
160 | OModalHelperBase *m_base; | ||
161 | } | ||
162 | |||
163 | class OModalQueueBar; | ||
164 | class OModalQueuedDialog : public QDialog { | ||
165 | Q_OBJECT | ||
166 | public: | ||
167 | OModalQueuedDialog(QWidget *mainWidget); | ||
168 | ~OModalQueuedDialog(); | ||
169 | |||
170 | QDialog* centerDialog()const; | ||
171 | |||
172 | void setQueueBarEnabled( bool = true ); | ||
173 | void setRecord( int record, int count ); | ||
174 | private: | ||
175 | OModalQueueBar *m_bar; | ||
176 | }; | ||
177 | |||
178 | |||
179 | /* | ||
180 | * Tcpp Template Implementation | ||
181 | */ | ||
182 | |||
183 | /** | ||
184 | * This is the simple Template c'tor. It takes the mode | ||
185 | * this helper should operate in and the parent object. | ||
186 | * This helper will be deleted when the parent gets deleted | ||
187 | * or you delete it yourself. | ||
188 | * | ||
189 | * @param mode The mode this dialog should be in | ||
190 | * @param parent The parent QObject of this helper. | ||
191 | */ | ||
192 | template<class Dialog, class Record, typename Id> | ||
193 | OModalHelper<Dialog, Record, Id>::OModalHelper( enum Mode mode, QObject* parent ) { | ||
194 | m_mode = mode; | ||
195 | m_signal = new OModalHelperSignal( this, parent ); | ||
196 | m_controler = new OModalHelperControler( this, m_signal ); | ||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * This functions looks for your record and sees if it is | ||
202 | * handled with this helper. Note that done and edited records | ||
203 | * will not be returned. But if you edit an already edited record | ||
204 | * the done record will be used | ||
205 | * | ||
206 | * @return true if the record is currenlty edited otherwise false | ||
207 | * | ||
208 | * @param Id The id which might be handled | ||
209 | */ | ||
210 | template<class Dialog, class Record, typename Id> | ||
211 | bool OModalHelper<Dialog, Record, Id>::handles( Id id )const { | ||
212 | if ( m_transactions.isEmpty() ) | ||
213 | return false; | ||
214 | |||
215 | TransactionMap::ConstIterator it = m_transactions.begin(); | ||
216 | for ( ; it != m_transactions.end(); ++it ) | ||
217 | if ( it.data() == id ) | ||
218 | return true; | ||
219 | |||
220 | return false; | ||
221 | } | ||
222 | |||
223 | /** | ||
224 | * Cancel will cancel all current operations and clear the list | ||
225 | * of done operations as well. | ||
226 | */ | ||
227 | template<class Dialog, class Record, typename Id> | ||
228 | void OModalHelper<Dialog, Record, Id>::cancel() { | ||
229 | m_ids.clear(); | ||
230 | m_done.clear(); | ||
231 | m_transactions.clear(); | ||
232 | |||
233 | /* we also need to remove the QDialogs */ | ||
234 | /* and hide the queue dialog if present */ | ||
235 | if (m_mode == New && !m_editing.isEmpty() ) { | ||
236 | for (DialogMap::Iterator it = m_editing.begin(); it != m_editing.end(); ++it ) | ||
237 | delete it.key(); | ||
238 | m_editing.clear(); | ||
239 | }else if (m_dialog ) | ||
240 | queuedDialog()->setRecord( 0, 0 ); | ||
241 | |||
242 | |||
243 | } | ||
244 | |||
245 | |||
246 | /** | ||
247 | * This cancels editing of the record behind the Transaction Number | ||
248 | * Note that if editing is already done it will also be removed from this list | ||
249 | */ | ||
250 | template<class Dialog, class Record, typename Id> | ||
251 | void OModalHelper::cancel( TransactionID tid ) { | ||
252 | /* wrong tid */ | ||
253 | if (!m_transactions.contains( tid ) && !m_done.contains( tid) ) | ||
254 | return; | ||
255 | |||
256 | if (m_mode == New ) | ||
257 | /* reverse map eek */ | ||
258 | for (DialogMap::Iterator it = m_editing.begin(); it != m_editing.end(); ++it ) | ||
259 | if ( it.data() == tid ) { | ||
260 | it.key()->hide(); | ||
261 | delete it.key(); | ||
262 | it = m_editing.remove( it ); | ||
263 | break; | ||
264 | } | ||
265 | else if ( m_transactions.contains( tid ) ) { | ||
266 | /* need to stop editing from the queue block */ | ||
267 | /* if we're currently editing actiavte the previous */ | ||
268 | #if 0 | ||
269 | if (tid == m_controler->transactionID() ) { | ||
270 | |||
271 | } | ||
272 | #endif | ||
273 | /* now either activate the previous again or hide */ | ||
274 | if (! m_transactions.count() -1 ) | ||
275 | queuedDialog()->setRecord( 0, 0 ); | ||
276 | else { | ||
277 | int pos; | ||
278 | Record rec = prevRecord( tid, &pos ); | ||
279 | static_cast<Dialog*>( queuedDialog()->centerDialog() )->setRecord( rec ); | ||
280 | queuedDialog()->setRecord( pos, m_transactions.count() ); | ||
281 | } | ||
282 | } | ||
283 | /* now remove from the various maps */ | ||
284 | m_ids.remove( m_transactions.contains( tid ) ? m_transactions[tid] : m_done[tid ] ); | ||
285 | |||
286 | m_done.remove( tid ); | ||
287 | m_transactions.remove( tid ); | ||
288 | } | ||
289 | |||
290 | /** | ||
291 | * Connect to the done Signal. SIGNAL( done(int, TransactionID ) ) | ||
292 | * This signal gets emitted whenever a Record was accepted or rejected | ||
293 | * | ||
294 | * @param rec The object where the slot belongs to | ||
295 | * @param slot The slot which should be called. See the needed parameter above | ||
296 | */ | ||
297 | template<class Dialog, class Record, typename Id> | ||
298 | void OModalHelper<Dialog, Record, Id>::connectDone( QObject* rec, const char* slot ) { | ||
299 | QObject::connect(m_signal, SIGNAL(done(int, TransactionID) ), | ||
300 | rec, slot ); | ||
301 | } | ||
302 | |||
303 | /** | ||
304 | * Connect to the accepted Signal. SIGNAL( done(int, TransactionID ) ) | ||
305 | * This signal gets emitted whenever a Record was accepted or rejected | ||
306 | * | ||
307 | * @param rec The object where the slot belongs to | ||
308 | * @param slot The slot which should be called. See the needed parameter above | ||
309 | */ | ||
310 | template<class Dialog, class Record, typename Id> | ||
311 | void OModalHelper<Dialog, Record, Id>::( QObject* rec, const char* slot ) { | ||
312 | |||
137 | } | 313 | } |
138 | 314 | ||
315 | |||
316 | template<class Dialog, class Record, typename Id> | ||
317 | void OModalHelper<Dialog, Record, Id>::( QObject* rec, const char* slot ) { | ||
318 | |||
319 | } | ||
139 | #endif | 320 | #endif |