osbcombos.h

Go to the documentation of this file.
00001 // OSB library ************************************************ -*- C++ -*-
00006 /*
00007   AUTHOR(S): Stephan Broennimann (vb)
00008 
00009   RCS information
00010    $Name: OSB_060808 $
00011    $Revision: 1.14 $
00012 
00013   License
00014    OSB rating and billing library for communication networks
00015    Copyright (C) 2004, 2005, 2006  OSB systems
00016 
00017    This file may be distributed and/or modify under the terms of the
00018    GNU General Public License (GPL) as published by the Free Software
00019    Foundation which is provided in the file LICENSE.GPL included in the
00020    packaging of this file.
00021 
00022    The file is distributed in the hope that it will be useful, but WITHOUT
00023    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025    for more details.
00026 
00027    Holders of a OSB Commercial License may use this file under the terms
00028    and conditions of this commercial license.
00029  */
00030 #ifndef _OSBCOMBOS_H_
00031 #define _OSBCOMBOS_H_
00032 
00033 // ************************************************************************
00034 // included header files
00035 // + standard includes
00036 #include <vector>
00037 
00038 // + libraries
00039 #include <osbtype.h>
00040 #include <qcombobox.h>
00041 
00042 // + local headers
00043 
00044 // ************************************************************************
00045 // class declarations
00046 namespace OSB_LIB {
00047     class Unit;
00048     class RatingFn;
00049 }
00050 
00051 // ************************************************************************
00052 // namespace extensions
00053 namespace OSB_GUI {
00054 
00055 // ************************************************************************
00056 // forward declarations
00057 
00058 // ************************************************************************
00059 // type definitions
00060 
00061 // ************************************************************************
00062 // class definitions
00063 
00067     class FocusOutCombo : public QComboBox {
00068         Q_OBJECT                        // for vim ->;
00069     public:
00071         FocusOutCombo(
00072                   bool     editable,
00073                   QWidget* parent,
00074             const char*    name
00075         );
00077         virtual ~FocusOutCombo();
00078 
00079     public:
00086         QSize sizeHint() const;
00087 
00088     signals:
00100         void sigFocusOut(QFocusEvent* e);
00101 
00102     private:
00108         void focusOutEvent(QFocusEvent* e);
00109     };                                  // class FocusOutCombo
00110 
00119     template<typename Value>
00120     class ValueCombo : public FocusOutCombo {
00121     public:
00123         ValueCombo(
00124                   QWidget* parent,
00125             const char*    name
00126         );
00128         ~ValueCombo() = 0;
00129     public:
00137         int addValue(
00138             const Value&       value,
00139             const std::string& text
00140         );
00149         int addValue(
00150             const Value&       value,
00151             const QPixmap&     pixmap,
00152             const std::string& text
00153         );
00161         int setText(
00162             const Value&       value,
00163             const std::string& text
00164         );
00171         Value getValue() const;
00178         Value getValue(int index) const;
00184         int getIndex(const Value& value) const;
00191         int select(const Value& value);
00200         void clear();
00201     protected:                          // support for derived classes
00203         void clearValueMap();
00204     private:
00206         typedef std::vector<Value> ValueMap;
00208         typedef typename ValueMap::size_type Size;
00209     private:
00211         ValueMap valueMap_;
00212     };                                  // class ValueCombo
00213 
00215     typedef ValueCombo< OSB_LIB::TypeId<OSB_LIB::Unit> > CmbUnitsBase;
00219     class CmbUnits : public CmbUnitsBase {
00220         Q_OBJECT                        // for vim ->;
00221     public:
00223         CmbUnits(
00224                   QWidget* parent,
00225             const char*    name = "cmbUnit_"
00226         );
00227     public:
00237         void populate();
00238     public slots:
00245         void activated();
00246     signals:
00258         void activated(const OSB_LIB::Unit& unit);
00259     };                                  // class CmbUnits
00260 }                                       // namespace OSB_GUI
00261 
00262 // ************************************************************************
00263 // template definitions
00264 namespace OSB_GUI {
00265 
00266     // ********************************************************************
00267     // template ValueCombo<Value>
00268     template<typename Value>
00269     ValueCombo<Value>::ValueCombo(
00270               QWidget* parent,
00271         const char*    name
00272     ) : FocusOutCombo(false, parent, name)
00273     {
00274         // empty
00275     }
00276 
00277     template<typename Value>
00278     ValueCombo<Value>::~ValueCombo()
00279     {
00280         // empty
00281     }
00282 
00283     template<typename Value>
00284     Value ValueCombo<Value>::getValue(int index) const
00285     {
00286         if (index < 0 || static_cast<Size>(index) >= valueMap_.size()) {
00287             return Value();
00288         }
00289         return valueMap_[index];
00290     }
00291 
00292     template<typename Value>
00293     Value ValueCombo<Value>::getValue() const
00294     {
00295         int i = currentItem();
00296         return getValue(i);
00297     }
00298 
00299     template<typename Value>
00300     int ValueCombo<Value>::getIndex(const Value& value) const
00301     {
00302         int rc = -1;
00303 
00304         Size s = valueMap_.size();
00305         for (Size i = 0; -1 == rc && i < s; ++i) {
00306             if (valueMap_[i] == value) rc = i;
00307         }
00308 
00309         return rc;
00310     }
00311 
00312     template<typename Value>
00313     int ValueCombo<Value>::select(const Value& value)
00314     {
00315         int ret = getIndex(value);
00316         setCurrentItem(ret);
00317         return currentItem();
00318     }
00319 
00320     template<typename Value>
00321     void ValueCombo<Value>::clear()
00322     {
00323         QComboBox::clear();
00324         clearValueMap();
00325     }
00326 
00327     template<typename Value>
00328     void ValueCombo<Value>::clearValueMap()
00329     {
00330         valueMap_.clear();
00331     }
00332 
00333     template<typename Value>
00334     int ValueCombo<Value>::addValue(
00335         const Value&       value,
00336         const std::string& text
00337     )
00338     {
00339         int rc = static_cast<int>(valueMap_.size());
00340         valueMap_.push_back(value);
00341         insertItem(text, rc);
00342         return rc;
00343     }
00344 
00345     template<typename Value>
00346     int ValueCombo<Value>::addValue(
00347         const Value&       value,
00348         const QPixmap&     pixmap,
00349         const std::string& text
00350     )
00351     {
00352         int rc = static_cast<int>(valueMap_.size());
00353         valueMap_.push_back(value);
00354         insertItem(pixmap, text, rc);
00355         return rc;
00356     }
00357 
00358     template<typename Value>
00359     int ValueCombo<Value>::setText(
00360         const Value&       value,
00361         const std::string& text
00362     )
00363     {
00364         int rc = getIndex(value);
00365         changeItem(text, rc);
00366         return rc;
00367     }
00368 }                                       // namespace OSB_GUI
00369 #endif                                  // #ifndef _OSBCOMBOS_H_

Generated on Sat Sep 2 14:17:37 2006 for OSB Library by  doxygen 1.4.7