kradio4  r778
gui_list_helper.h
Go to the documentation of this file.
1 /***************************************************************************
2  gui_list_helper.h
3  -------------------
4  begin : Son Sep 26 2004
5  copyright : (C) 2004 by Martin Witte
6  email : emw-kradio@nocabal.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef _KRADIO_LIBKRADIO_GUI_GUI_LIST_HELPER_H_
19 #define _KRADIO_LIBKRADIO_GUI_GUI_LIST_HELPER_H_
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include <kdemacros.h>
26 
27 #include <QtCore/QMap>
28 #include <QtCore/QList>
29 #include <QtCore/QString>
30 #include <QtCore/QObject>
31 #include <QtCore/QVariant>
32 #include <QtCore/QObject>
33 
34 class KDE_EXPORT GUIListHelperQObjectBase : public QObject
35 {
36 Q_OBJECT
37 
38 public:
40  virtual ~GUIListHelperQObjectBase();
41 
42 public slots:
43 
44  virtual void slotOK() = 0;
45  virtual void slotCancel() = 0;
46 
47 protected slots:
48  virtual void slotUserSelection() = 0;
49 
50 protected:
51  void emitSigDirtyChanged(bool d);
52 
53 signals:
54  void sigDirtyChanged (bool dirty);
55 };
56 
57 
58 template <class TLIST, class TID> class KDE_EXPORT GUIListHelper : public GUIListHelperQObjectBase
59 {
60 public:
61  enum SORT_KEY { SORT_BY_ID, SORT_BY_DESCR, SORT_NONE };
62 
63  GUIListHelper (TLIST *list, SORT_KEY skey);
64  GUIListHelper (TLIST *list, const QMap<TID, QString> &data, SORT_KEY skey);
65  GUIListHelper (TLIST *list, const QList<QString> &data, SORT_KEY skey);
67 
68  void setList(TLIST *list) { m_List = list; }
69 
70  void setCurrentItemID(const TID &id);
71  void setOrgItemID (const TID &id);
72 
73  template<class TData>
74  void alternativesChanged(const TData &data);
75 
76  const TID getCurrentItemID() const;
77 
78  int count() const { return m_List ? m_List->count() : 0; }
79 
80 public:
81 
82  virtual void slotOK();
83  virtual void slotCancel();
84  virtual void slotUserSelection();
85 
86 protected:
87  void setData(const QMap<TID, QString> &data); // only updates list elements, no setting/update of current element
88  void setData(const QList<QString> &data); // only updates list elements, no setting/update of current element
89  bool containsItemID(const TID &id) const { return m_List && m_List->findData(id) >= 0; }
90 
91  void setUserDirty() { setDirty(true, m_alternativeDirty); }
92  void setUserDirty(bool dirty) { setDirty(dirty, m_alternativeDirty); }
93  void setAlternativeDirty() { setDirty(m_userDirty, true); }
94  void setAlternativeDirty(bool dirty) { setDirty(m_userDirty, dirty); }
95  void setDirty(bool userDirty, bool alternativesDirty)
96  { m_userDirty = userDirty;
97  m_alternativeDirty = alternativesDirty;
98  emitSigDirtyChanged(m_userDirty || m_alternativeDirty); }
99 
100 protected:
102  TLIST *m_List;
105 
106  TID m_orgID;
108 
110 
111  struct THelpData {
112  TID id;
113  QString descr;
115 
117  : id(),
118  descr(),
119  skey(SORT_BY_ID)
120  {}
121 
122  THelpData(TID _id, const QString &_descr, SORT_KEY _skey)
123  : id(_id),
124  descr(_descr),
125  skey(_skey)
126  {}
127 
128  bool operator > (const THelpData &d) const { return (skey == SORT_BY_ID) ? id > d.id : descr > d.descr; }
129  bool operator < (const THelpData &d) const { return (skey == SORT_BY_ID) ? id < d.id : descr < d.descr; }
130  };
131 };
132 
133 
134 
135 template <class TLIST, class TID>
137  : m_skey(skey),
138  m_List(list),
139  m_userDirty(false),
140  m_alternativeDirty(false),
141  m_ignoreGUIChange(false)
142 {
143  if (list)
144  QObject::connect(list, SIGNAL(activated(int)), this, SLOT(slotUserSelection()));
145 }
146 
147 
148 template <class TLIST, class TID>
149 GUIListHelper<TLIST, TID>::GUIListHelper(TLIST *list, const QMap<TID, QString> &data, SORT_KEY skey)
150  : m_skey(skey),
151  m_List(list),
152  m_userDirty(false),
153  m_alternativeDirty(false),
154  m_ignoreGUIChange(false)
155 {
156  if (list)
157  QObject::connect(list, SIGNAL(activated(int)), this, SLOT(slotUserSelection()));
158  setData(data);
159 }
160 
161 template <class TLIST, class TID>
162 GUIListHelper<TLIST, TID>::GUIListHelper(TLIST *list, const QList<QString> &data, SORT_KEY skey)
163  : m_skey(skey),
164  m_List(list),
165  m_userDirty(false),
166  m_alternativeDirty(false),
167  m_ignoreGUIChange(false)
168 {
169  if (list)
170  QObject::connect(list, SIGNAL(activated(int)), this, SLOT(slotUserSelection()));
171  setData(data);
172 }
173 
174 
175 
176 
177 template <class TLIST, class TID>
179 {
180  if (m_userDirty) {
182  }
184 }
185 
186 template <class TLIST, class TID>
188 {
189  setDirty(false, false);
190  setCurrentItemID(m_orgID); // will set alternativeDirty if org is not available
191 }
192 
193 template <class TLIST, class TID>
195 {
196  if (m_ignoreGUIChange)
197  return;
199  setDirty(true, false);
200 }
201 
202 template <class TLIST, class TID>
203 template <class TData>
205 {
206  setData(data);
207 
208  // m_userDirty is not touched
209  setAlternativeDirty(false); // will be set if no alternative is available
210 
211  if (!m_userDirty) {
212  // try to set original, alternativeDirty will be set if not possible
214  } else {
215  // try to keep user selection. alternativeDirty will be set if not possible
217  }
218 }
219 
220 
221 
222 template <class TLIST, class TID>
223 void GUIListHelper<TLIST, TID>::setData(const QMap<TID, QString> &data) {
224  m_List->clear();
225 
226  QList<THelpData> help_list;
227  for (typename QMap<TID, QString>::const_iterator it = data.begin(); it != data.end(); ++it) {
228  help_list.push_back(THelpData(it.key(), it.value(), m_skey));
229  }
230  if (m_skey != SORT_NONE) {
231  qSort(help_list);
232  }
233 
234  THelpData item;
235  foreach (item, help_list) {
236  m_List->addItem(item.descr, item.id);
237  }
238 }
239 
240 
241 template <class TLIST, class TID>
242 void GUIListHelper<TLIST, TID>::setData (const QList<QString> &_data)
243 {
244  m_List->clear();
245  QList<QString> data = _data;
246  if (m_skey != SORT_NONE) {
247  qSort(data);
248  }
249 
250  QString item;
251  foreach (item, data) {
252  m_List->addItem(item, item);
253  }
254 }
255 
256 
257 template <class TLIST, class TID>
259 {
260  bool oldIgnoreGUIChange = m_ignoreGUIChange;
261  m_ignoreGUIChange = true;
262 
263  int idx = m_List->findData(id);
264  if (idx >= 0) {
265  m_List->setCurrentIndex(idx);
266  } else {
267  m_List->setCurrentIndex(0);
269  }
270 
271  m_ignoreGUIChange = oldIgnoreGUIChange;
272 }
273 
274 template <class TLIST, class TID>
276 {
277  m_orgID = id;
278  if (!m_userDirty) {
280  }
281 }
282 
283 template <class TLIST, class TID>
285 {
286  int idx = m_List->currentIndex();
287  if (idx >= 0) {
288  const QVariant &data = m_List->itemData(idx);
289  return data.value<TID>();
290  } else {
291  return TID();
292  }
293 }
294 
295 
296 
297 #endif
void setUserDirty(bool dirty)
virtual void slotCancel()
GUIListHelper(TLIST *list, SORT_KEY skey)
virtual void slotOK()=0
void setAlternativeDirty(bool dirty)
bool containsItemID(const TID &id) const
void setDirty(bool userDirty, bool alternativesDirty)
void setCurrentItemID(const TID &id)
const TID getCurrentItemID() const
virtual void slotUserSelection()=0
void setData(const QMap< TID, QString > &data)
void setAlternativeDirty()
void setOrgItemID(const TID &id)
virtual void slotUserSelection()
void emitSigDirtyChanged(bool d)
THelpData(TID _id, const QString &_descr, SORT_KEY _skey)
void setList(TLIST *list)
virtual void slotOK()
int count() const
virtual void slotCancel()=0
void alternativesChanged(const TData &data)