oopapidocs  2.0
enum.cpp
00001 
00002 #include "enum.h"
00003 #include <QDebug>
00004 
00005 Enum::Enum(int enumval) : m_Val(enumval) {
00006     m_Val = enumval;
00007 }
00008 Enum::Enum(const Enum& other) {
00009         m_Val = other.m_Val;
00010 }
00011 
00012 Enum::~Enum() {}
00013 
00014 bool Enum::operator=(int other)  {
00015     if ((other >= 0) && (other <= nameMap().size())) {
00016         m_Val = other;
00017         return true;
00018     }
00019     qDebug() << "Enum::operator=() Undefined other: " << other;
00020     return false;
00021 }
00022 
00023 QStringList Enum::names() const {
00024     QMap<int, QString> map;
00025     foreach (const QString &n, nameMap().keys()) {
00026         int v = nameMap()[n];
00027         map[v]=n;
00028     }
00029     return map.values();
00030 }
00031 
00032 bool Enum::fromString(QString val) {
00033     const QRegExp numbers("\\d+");
00034     if (numbers.exactMatch(val)) {
00035         m_Val = val.toInt();
00036         return true;
00037     }
00038 
00039     const NameMap& nm = nameMap();
00040     if (nm.contains(val)) {
00041         m_Val = nm[val];
00042 //        qDebug() << " fromString(" << val << ") = " << m_Val;
00043         return true;
00044     } else {
00045         foreach (const QString &name, nm.keys()) {
00046            if (name.toLower()==val.toLower()) {
00047                 m_Val = nm[name];
00048 //                qDebug() << " fromString(" << val << ") = " << m_Val;
00049                 return true;
00050             }
00051         }
00052     }
00053 //    qDebug() << "fromString: undefined " << val;
00054     return false;
00055 }
00056 
00057 
00058 bool Enum::operator=(QString other) {
00059     return fromString(other);
00060 
00061 /*    if (nameMap()names().contains(other)) {
00062         m_Val = names().indexOf(other);
00063         return true;
00064     }
00065     qDebug() << "operator= Undefined other: " << other;
00066     return false;
00067 */
00068 }
00069 
00070 int Enum::intValue() const {
00071     return m_Val;
00072 }
00073 
00074 bool Enum::operator==(const QString& other) const {
00075     int v = nameMap()[other];
00076     return m_Val == v;
00077 }
00078 
00079 QString Enum::toString() const {
00080     foreach (const QString &n , nameMap().keys()) {
00081         if (nameMap()[n] == m_Val)
00082             return n;
00083     }
00084     return QString("Enum::Unknown");
00085 }
00086 
00087 QTextStream& operator<<(QTextStream& ts, const Enum& enumval) {
00088     return ts << enumval.intValue();
00089 }
00090 
00091 QTextStream& operator>>(QTextStream &is, Enum& enumval) {
00092     int i;
00093     is >> i;
00094     enumval.setIntValue(i);
00095     return is;
00096 }
00097 
00098 QDataStream& operator<<(QDataStream& ds, const Enum& e) {
00099     return ds << e.toString();
00100 }
00101 
00102 QDataStream& operator>>(QDataStream& ds, Enum& e) {
00103     QString str;
00104     ds >> str;
00105     bool success = e.fromString(str);
00106     if (!success) {
00107         qCritical() << "Unrecognized enumvalue: " << str;
00108     }
00109     return ds;
00110 }
00111 
00112 NameMap& Enum::initMap(NameMap& nameMap, QStringList nameList,
00113                        bool & initialized, int startIndex) {
00114     if (!initialized) {
00115         for (int i=startIndex; i < nameList.count(); ++i) {
00116             int value = i;
00117             QString key=nameList[i];
00118             if (key.indexOf('=') > 0) {
00119                 QStringList kvl = key.split("=");
00120                 key = kvl[0];
00121                 value = kvl[1].toInt();
00122             }
00123             // qDebug() << "map: " << key << " = " << value;
00124             nameMap[key]=value;
00125         }
00126         initialized=true;
00127     }
00128     return nameMap;
00129 }
00130 
00131 
 All Classes Namespaces Functions Enumerations