gui_list_helper.h

Go to the documentation of this file.
00001 /***************************************************************************
00002                               gui_list_helper.h
00003                              -------------------
00004     begin                : Son Sep 26 2004
00005     copyright            : (C) 2004 by Martin Witte
00006     email                : emw-kradio@nocabal.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef _KRADIO_LIBKRADIO_GUI_GUI_LIST_HELPER_H_
00019 #define _KRADIO_LIBKRADIO_GUI_GUI_LIST_HELPER_H_
00020 
00021 #include <QtCore/QMap>
00022 #include <QtCore/QList>
00023 #include <QtCore/QString>
00024 
00025 template <class TLIST> class KDE_EXPORT GUISimpleListHelper
00026 {
00027 public:
00028     GUISimpleListHelper(TLIST *list) : m_List(list) {}
00029     ~GUISimpleListHelper() {}
00030 
00031     void setList(TLIST *list);
00032 
00033     void     setData(const QList<QString> &data);
00034     QString  getCurrentText() const           { return m_List->currentText(); }
00035     void     setCurrentText(const QString &s) { m_List->setCurrentIndex(m_revData.contains(s) ? m_revData[s] : 0); }
00036 
00037     int count() const { return m_revData.count(); }
00038     bool contains(const QString &id) const { return m_revData.contains(id); }
00039 
00040 protected:
00041     TLIST              *m_List;
00042     QMap<QString, int>  m_revData;
00043 };
00044 
00045 template <class TLIST>
00046 void GUISimpleListHelper<TLIST>::setList(TLIST *list)
00047 {
00048     m_List = list;
00049 }
00050 
00051 template <class TLIST>
00052 void GUISimpleListHelper<TLIST>::setData(const QList<QString> &data)
00053 {
00054     m_List->clear();
00055     m_revData.clear();
00056 
00057     QList<QString>::const_iterator it  = data.begin();
00058     QList<QString>::const_iterator end = data.end();
00059     for (int i = 0; it != end; ++it, ++i) {
00060         m_revData[*it] = i;
00061         m_List->insertItem(i, *it);
00062     }
00063 }
00064 
00065 
00066 
00067 
00068 
00069 
00070 
00071 
00072 
00073 template <class TLIST, class TID> class GUIListHelper
00074 {
00075 public:
00076     enum SORT_KEY { SORT_BY_ID, SORT_BY_DESCR };
00077 
00078     GUIListHelper(TLIST *list, SORT_KEY skey);
00079     GUIListHelper(TLIST *list, const QMap<TID, QString> &data, SORT_KEY skey);
00080     ~GUIListHelper();
00081 
00082     void setList(TLIST *list);
00083 
00084     void setData(const QMap<TID, QString> &data);
00085 
00086     void       setCurrentItem(const TID &) const;
00087     const TID  getCurrentItem()    const;
00088 
00089     int count() const { return m_Index2ID.count(); }
00090 
00091     bool contains(const TID &id) const { return m_ID2Index.contains(id); }
00092 
00093 protected:
00094     SORT_KEY           m_skey;
00095     TLIST             *m_List;
00096     QMap<int, TID>     m_Index2ID;
00097     QMap<TID, int>     m_ID2Index;
00098     QMap<TID, QString> m_ID2Description;
00099 
00100     struct THelpData {
00101         TID      id;
00102         QString  descr;
00103         SORT_KEY skey;
00104 
00105         THelpData() : id(), descr(), skey(SORT_BY_ID) {}
00106         THelpData(TID _id, const QString &_descr, SORT_KEY _skey)
00107             : id(_id),
00108               descr(_descr),
00109               skey(_skey)
00110           {}
00111         bool operator > (const THelpData &d) const { return (skey == SORT_BY_ID) ? id > d.id : descr > d.descr; }
00112         bool operator < (const THelpData &d) const { return (skey == SORT_BY_ID) ? id < d.id : descr < d.descr; }
00113     };
00114 };
00115 
00116 
00117 
00118 template <class TLIST, class TID>
00119 GUIListHelper<TLIST, TID>::GUIListHelper(TLIST *list, SORT_KEY skey)
00120     : m_skey(skey),
00121       m_List(list)
00122 {
00123 }
00124 
00125 
00126 template <class TLIST, class TID>
00127 GUIListHelper<TLIST, TID>::GUIListHelper(TLIST *list, const QMap<TID, QString> &data, SORT_KEY skey)
00128     : m_skey(skey),
00129       m_List(list)
00130 {
00131     setData(data);
00132 }
00133 
00134 template <class TLIST, class TID>
00135 void GUIListHelper<TLIST, TID>::setList(TLIST *list)
00136 {
00137     m_List = list;
00138 }
00139 
00140 
00141 template <class TLIST, class TID>
00142 GUIListHelper<TLIST, TID>::~GUIListHelper()
00143 {
00144 }
00145 
00146 template <class TLIST, class TID>
00147 void GUIListHelper<TLIST, TID>::setData (const QMap<TID, QString> &data)
00148 {
00149 
00150     m_List->clear();
00151 
00152     m_ID2Description = data;
00153     QList<THelpData>          help_list;
00154     QMapIterator<TID, QString> it(data);
00155     while(it.hasNext()) {
00156         it.next();
00157         help_list.push_back(THelpData(it.key(), it.value(), m_skey));
00158     }
00159     qSort(help_list);
00160 
00161     m_Index2ID.clear();
00162     m_ID2Index.clear();
00163 
00164     int idx = 0;
00165     QListIterator<THelpData> it2(help_list);
00166     while(it2.hasNext()) {
00167         const THelpData &hd = it2.next();
00168         m_Index2ID.insert(idx, hd.id);
00169         m_ID2Index.insert(hd.id, idx);
00170         m_List->insertItem(idx, hd.descr);
00171         ++idx;
00172     }
00173 }
00174 
00175 
00176 template <class TLIST, class TID>
00177 void GUIListHelper<TLIST, TID>::setCurrentItem(const TID &id) const
00178 {
00179     if (m_ID2Index.contains(id))
00180         m_List->setCurrentIndex(m_ID2Index[id]);
00181     else
00182         m_List->setCurrentIndex(0);
00183 }
00184 
00185 template <class TLIST, class TID>
00186 const TID GUIListHelper<TLIST, TID>::getCurrentItem() const
00187 {
00188     int idx = m_List->currentIndex();
00189     return (idx >= 0) ? (m_Index2ID.begin() + idx).value() : TID();
00190 }
00191 
00192 #endif

Generated on Tue Jun 2 19:19:57 2009 for kradio4 by  doxygen 1.5.8