00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _KRADIO_LIBKRADIO_GUI_GUI_LIST_HELPER_H_
00019 #define _KRADIO_LIBKRADIO_GUI_GUI_LIST_HELPER_H_
00020
00021 #include <qmap.h>
00022 #include <qvaluelist.h>
00023
00024
00025 template <class TLIST> class GUISimpleListHelper
00026 {
00027 public:
00028 GUISimpleListHelper(TLIST *list) : m_List(list) {}
00029 ~GUISimpleListHelper() {}
00030
00031 void setData(const QValueList<QString> &data);
00032 QString getCurrentText() const { return m_List->currentText(); }
00033 void setCurrentText(const QString &s) { m_List->setCurrentItem(m_revData.contains(s) ? m_revData[s] : 0); }
00034
00035 int count() const { return m_revData.count(); }
00036 bool contains(const QString &id) const { return m_revData.contains(id); }
00037
00038 protected:
00039 TLIST *m_List;
00040 QMap<QString, int> m_revData;
00041 };
00042
00043
00044 template <class TLIST>
00045 void GUISimpleListHelper<TLIST>::setData(const QValueList<QString> &data)
00046 {
00047 m_List->clear();
00048 m_revData.clear();
00049
00050 QValueListConstIterator<QString> it = data.begin();
00051 QValueListConstIterator<QString> end = data.end();
00052 for (int i = 0; it != end; ++it, ++i) {
00053 m_revData[*it] = i;
00054 m_List->insertItem(*it);
00055 }
00056 }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 template <class TLIST, class TID> class GUIListHelper
00067 {
00068 public:
00069 enum SORT_KEY { SORT_BY_ID, SORT_BY_DESCR };
00070
00071 GUIListHelper(TLIST *list, SORT_KEY skey);
00072 GUIListHelper(TLIST *list, const QMap<TID, QString> &data, SORT_KEY skey);
00073 ~GUIListHelper();
00074
00075 void setData(const QMap<TID, QString> &data);
00076
00077 void setCurrentItem(const TID &) const;
00078 const TID &getCurrentItem() const;
00079
00080 int count() const { return m_Index2ID.count(); }
00081
00082 bool contains(const TID &id) const { return m_ID2Index.contains(id); }
00083
00084 protected:
00085 SORT_KEY m_skey;
00086 TLIST *m_List;
00087 QMap<int, TID> m_Index2ID;
00088 QMap<TID, int> m_ID2Index;
00089 QMap<TID, QString> m_ID2Description;
00090
00091 struct THelpData {
00092 TID id;
00093 QString descr;
00094 SORT_KEY skey;
00095
00096 THelpData() : id(), descr(), skey(SORT_BY_ID) {}
00097 THelpData(TID _id, const QString &_descr, SORT_KEY _skey)
00098 : id(_id),
00099 descr(_descr),
00100 skey(_skey)
00101 {}
00102 bool operator > (const THelpData &d) { return (skey == SORT_BY_ID) ? id > d.id : descr > d.descr; }
00103 bool operator < (const THelpData &d) { return (skey == SORT_BY_ID) ? id < d.id : descr < d.descr; }
00104 };
00105 };
00106
00107
00108
00109 template <class TLIST, class TID>
00110 GUIListHelper<TLIST, TID>::GUIListHelper(TLIST *list, SORT_KEY skey)
00111 : m_skey(skey),
00112 m_List(list)
00113 {
00114 }
00115
00116
00117 template <class TLIST, class TID>
00118 GUIListHelper<TLIST, TID>::GUIListHelper(TLIST *list, const QMap<TID, QString> &data, SORT_KEY skey)
00119 : m_skey(skey),
00120 m_List(list)
00121 {
00122 setData(data);
00123 }
00124
00125
00126 template <class TLIST, class TID>
00127 GUIListHelper<TLIST, TID>::~GUIListHelper()
00128 {
00129 }
00130
00131
00132 template <class TLIST, class TID>
00133 void GUIListHelper<TLIST, TID>::setData (const QMap<TID, QString> &data)
00134 {
00135 m_List->clear();
00136
00137 m_ID2Description = data;
00138 QValueList<THelpData> help_list;
00139 QMapConstIterator<TID, QString> end = data.end();
00140 for (QMapConstIterator<TID, QString> it = data.begin(); it != end; ++it) {
00141 help_list.push_back(THelpData(it.key(), *it, m_skey));
00142 }
00143 qHeapSort(help_list);
00144
00145 m_Index2ID.clear();
00146 m_ID2Index.clear();
00147
00148 int idx = 0;
00149 QValueListIterator<THelpData> end_hlp = help_list.end();
00150 for (QValueListIterator<THelpData> it = help_list.begin(); it != end_hlp; ++it, ++idx) {
00151 m_Index2ID.insert(idx, (*it).id);
00152 m_ID2Index.insert((*it).id, idx);
00153 m_List->insertItem((*it).descr);
00154 }
00155 }
00156
00157
00158 template <class TLIST, class TID>
00159 void GUIListHelper<TLIST, TID>::setCurrentItem(const TID &id) const
00160 {
00161 if (m_ID2Index.contains(id))
00162 m_List->setCurrentItem(m_ID2Index[id]);
00163 else
00164 m_List->setCurrentItem(0);
00165 }
00166
00167 template <class TLIST, class TID>
00168 const TID &GUIListHelper<TLIST, TID>::getCurrentItem() const
00169 {
00170 int idx = m_List->currentItem();
00171 return m_Index2ID[idx];
00172 }
00173
00174 #endif