File indexing completed on 2024-04-14 14:32:21

0001 //
0002 // C++ Interface: clistobject
0003 //
0004 // Description: 
0005 //
0006 /*
0007 Copyright 2007-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #ifndef CLISTOBJECT_H
0024 #define CLISTOBJECT_H
0025 
0026 #include <qstring.h>
0027 #include <QModelIndex>
0028 #include <QObject>
0029 #include <list>
0030 #include <map>
0031 #include <kmuddy_export.h>
0032 
0033 // needed for cList::TraverseAction
0034 #include <clist.h>
0035 
0036 class cListGroup;
0037 
0038 class QXmlStreamReader;
0039 class QXmlStreamWriter;
0040 
0041 #define DEFAULT_OBJECT_PRIORITY 50
0042 
0043 /** cListObjectData - data of the object. */
0044 struct KMUDDY_EXPORT cListObjectData {
0045   QString name;
0046   int priority;
0047   bool enabled;
0048   std::map<QString, int> intValues;
0049   std::map<QString, QString> strValues;
0050   std::map<QString, bool> boolValues;
0051   int intValue (const QString &name) const {
0052     if (intValues.count (name))
0053       return intValues.find (name)->second;
0054     return 0;
0055   }
0056   QString strValue (const QString &name) const {
0057     if (strValues.count (name))
0058       return strValues.find (name)->second;
0059     return QString();
0060   }
0061   bool boolValue (const QString &name) const {
0062     if (boolValues.count (name))
0063       return boolValues.find (name)->second;
0064     return false;
0065   }
0066 };
0067 
0068 /**
0069 cListObject - Base class for all objects. Holds all data. Can load/save itself.
0070 
0071 Child classes (other than cListGroup) only need to add their own things,
0072 the only thing that they may want to override is attribChanged().
0073 
0074 @author Tomas Mecir <kmuddy@kmuddy.com>
0075 */
0076 
0077 class KMUDDY_EXPORT cListObject : public QObject {
0078 Q_OBJECT
0079 public:
0080   /** Returns parent group. */
0081   cListGroup *parentGroup ();
0082 
0083   /** Rreturns list where this object belongs to. */
0084   cList *list ();
0085 
0086   /** Returns object priority. */
0087   int priority ();
0088   /** Sets object priority. */
0089   void setPriority (int p);
0090 
0091   /** Returns name visible in the object list. */
0092   QString visibleName ();
0093   /** Sets the visible name. */
0094   void setVisibleName (const QString &name);
0095 
0096   /** Returns the object name. */
0097   QString name ();
0098 
0099   /** Returns the value of an integer property. If it's not specified
0100   and the object specifies a default value, then that gets returned.
0101   If no default value is provided, 0 is returned. */
0102   int intVal (const QString &name);
0103   /** Sets an integer property value. */
0104   void setInt (const QString &name, int value);
0105   /** Clears an integer property value back to default. */
0106   void clearInt (const QString &name);
0107 
0108   /** Returns the value of a string property. If it's not specified
0109   and the object specifies a default value, then that gets returned.
0110   If no default value is provided, empty string is returned. */
0111   QString strVal (const QString &name);
0112   /** Sets a string property value. */
0113   void setStr (const QString &name, const QString &value);
0114   /** Clears a string property value back to default. */
0115   void clearStr (const QString &name);
0116 
0117   /** Returns the value of  boolean property. If it's not specified
0118   and the object specifies a default value, then that gets returned.
0119   If no default value is provided, false is returned. */
0120   bool boolVal (const QString &name);
0121   /** Sets a boolean property value. */
0122   void setBool (const QString &name, bool value);
0123   /** Clears  boolean property value back to default. */
0124   void clearBool (const QString &name);
0125 
0126   // list of strings - convenience wrappers around the string routines
0127   int strListCount (const QString &name);
0128   void setStrListCount (const QString &name, int count);
0129   QString strListValue (const QString &name, int which);
0130   void setStrListValue (const QString &name, int which, const QString &value);
0131   void clearStrList (const QString &name);
0132 
0133   /** Returns a -copy- of the object data. */
0134   cListObjectData data ();
0135 
0136   /** Is this object enabled ? */
0137   bool enabled ();
0138   /** Enabled or disables an object. */
0139   virtual void setEnabled (bool en = true);
0140 
0141   virtual bool isGroup () { return false; }
0142 
0143   int positionInGroup () const;
0144   int priorityInGroup () const;
0145 
0146   /** Returns index of this object within the model. */
0147   QModelIndex itemIndex ();
0148 signals:
0149   /** Emitted whenever this object is changed. */
0150   void changed (cListObject *);
0151 protected:
0152   /** constructor - only cList can call it */
0153   cListObject (cList *list);
0154   /** destructor - only cList can call it */
0155   ~cListObject () override;
0156 
0157   /** Sets the object name. Functions and macros can access the object by it. Used by cList. */
0158   virtual void setName (const QString &n);
0159   /** Generate the visible name. */
0160   virtual void updateVisibleName ();
0161 
0162   /** Change parent group. Used by cList. */
0163   void setParentGroup (cListGroup *group);
0164   /** Change position in group. Used by cListGroup. */
0165   virtual void setPositionInGroup (int pos);
0166   /** Change priority position in group. Used by cListGroup. */
0167   virtual void setPriorityInGroup (int pos);
0168   /** Load the object from the XML reader. */
0169   virtual void load (QXmlStreamReader *reader);
0170   /** Save the object into a XML writer. */
0171   virtual void save (QXmlStreamWriter *writer);
0172 
0173   /** React on attribute change. Does nothing, child classes can override. */
0174   virtual void attribChanged (const QString &name);
0175   /** React on the fact that the object has moved. */
0176   virtual void objectMoved ();
0177   /** React on the object being enabled. */
0178   virtual void objectEnabled () {};
0179   /** React on the object being disabled. */
0180   virtual void objectDisabled () {};
0181 
0182   /** Recursive traversal of the list, called by cList::traverse.
0183   Redefine with actual implementation. The parameter can be used
0184   to distinguish if traversal is used for multiple purposes within
0185   the same object. */
0186   virtual cList::TraverseAction traverse (int traversalType);
0187 
0188   friend class cList;
0189   friend class cListGroup;  // needed to allow cListGroup to call setPositionInGroup and load/save
0190   struct Private;
0191   Private *d;
0192 };
0193 // Qt metatype, needed so the model can return these in data()
0194 Q_DECLARE_METATYPE(cListObject *)
0195 
0196 #endif