File indexing completed on 2024-04-21 15:08:21

0001 //
0002 // C++ Interface: clist
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 CLIST_H
0024 #define CLIST_H
0025 
0026 #include <qstring.h>
0027 #include <map>
0028 
0029 #include <kmuddy_export.h>
0030 
0031 class cListEditor;
0032 class cListGroup;
0033 class cListObject;
0034 
0035 class QModelIndex;
0036 class QAbstractItemModel;
0037 class QWidget;
0038 class QXmlStreamReader;
0039 class QXmlStreamWriter;
0040 
0041 enum cListPropertyType {
0042   Int = 0,
0043   String,
0044   Bool
0045 };
0046 
0047 /** cListProperty - definition of one object property. */
0048 struct KMUDDY_EXPORT cListProperty {
0049   QString name;
0050   QString desc;      // description of the property
0051   cListPropertyType type;
0052   int defIntValue;  // default int value, only used if type is Int
0053   QString defStrValue;  // default string value, only used if type is String
0054   bool defBoolValue;  // default bool value, only used if type is Bool
0055 };
0056 
0057 
0058 /**
0059 One list of objects. Each existing list should subclass this to provide the desired functionality.
0060 
0061     @author Tomas Mecir <kmuddy@kmuddy.com>
0062 */
0063 class KMUDDY_EXPORT cList {
0064 public:
0065   /** constructor */
0066   cList (const QString &name);
0067   /** destructor */
0068   virtual ~cList ();
0069 
0070   /** Create a new instance of this class. */
0071   static cList *newList () { return nullptr; };
0072 
0073   /** Set session ID of this list. */
0074   void setSession (int sess);
0075   /** Return session ID of this list. */
0076   int session ();
0077 
0078   /** Create a new object belonging to this list. */
0079   virtual cListObject *newObject () = 0;
0080   /** Create a new group belonging to this list. */
0081   virtual cListGroup *newGroup ();
0082   void deleteObject (cListObject *obj);
0083 
0084   /** Returns list name. */
0085   QString name ();
0086   /** Returns the name of objects of this type. */
0087   virtual QString objName () = 0;
0088 
0089   /** Returns a list of all object properties indexed by name. */
0090   const std::map<QString, cListProperty> &getPropertyList ();
0091   int defaultIntValue (const QString &name);
0092   QString defaultStrValue (const QString &name);
0093   bool defaultBoolValue (const QString &name);
0094 
0095   /** Is this list enabled ? */
0096   bool enabled ();
0097   /** Enabled or disables this list. */
0098   virtual void setEnabled (bool en = true);
0099 
0100   /** main group of the list */
0101   cListGroup *rootGroup ();
0102   /** returns a group with the given name */
0103   cListGroup *group (const QString &name);
0104   /** creates a new group with the given name, or returns an existing one, if a group with such a name exists */
0105   cListGroup *addGroup (cListGroup *parent, const QString &name);
0106   /** Rename a group. Returns true if successful, false if the group with the new name already exists. */
0107   bool renameGroup (cListGroup *group, const QString &newName);
0108   /** Remove a group */
0109   void removeGroup (cListGroup *group);
0110   /** Adds an item to the group. Removes it from its existing group, if any. This can also be used to create nested groups - the item
0111   can be a group. */
0112   void addToGroup (cListGroup *group, cListObject *item);
0113 
0114   /** Changes the object name, */
0115   bool setObjectName (cListObject *obj, const QString &name);
0116   /** Returns the object of a given name. */
0117   cListObject *getObject (const QString &name);
0118 
0119   /** Remove everything from the list. */
0120   void clear ();
0121 
0122   /** Create and return a new object editor widget with the given parent. */
0123   virtual cListEditor *editor (QWidget *parent) = 0;
0124 
0125   enum TraverseAction {
0126     Continue=0,  // continue traversing
0127     Stop,        // stop traversing
0128     LeaveGroup   // leave the current group and continue with the next one
0129   };
0130 
0131   /** Traverse the list and call the traverse method on each object. Descends into groups and honors the returnes TraverseAction */
0132   void traverse (int traversalType);
0133 
0134   /** Load the structure from the XML reader. */
0135   void load (QXmlStreamReader *reader);
0136   /** Save the list into a XML writer, including all nested groups. */
0137   void save (QXmlStreamWriter *writer);
0138   /** Did error occur durig load/save? */
0139   bool hasError ();
0140   /** Return the last error that occured. */
0141   const QString lastError ();
0142   /** Clear the last error. */
0143   void clearError ();
0144 
0145   /** interaction with the associated model */
0146   QAbstractItemModel *model ();
0147   cListObject *objectAt (const QModelIndex &index);
0148   QModelIndex indexOf (const cListObject *obj);
0149 protected:
0150   void initRootGroup ();
0151 
0152   /** Adds a new property. The constructors of derived lists should use this. */
0153   void addProperty (const cListProperty &prop);
0154   /** Convenience wrapper around addProperty for int properties. */
0155   void addIntProperty (const QString &name, const QString &desc, int defaultValue = 0);
0156   /** Convenience wrapper around addProperty for string properties. */
0157   void addStringProperty (const QString &name, const QString &desc, QString defaultValue = QString());
0158   /** Convenience wrapper around addProperty for boolean properties. */
0159   void addBoolProperty (const QString &name, const QString &desc, bool defaultValue = false);
0160 
0161   /** Adds a new object to the list. Used by cListObject constructor. */
0162   void addObject (cListObject *obj);
0163   /** Removes an object from the list. Used by cListObject destructor. */
0164   void removeObject (cListObject *obj);
0165 
0166   /** Called when a group is adding a new item at the given position. */
0167   void notifyAdding (cListGroup *group, int pos);
0168   /** Called when the adding is done. */
0169   void addDone ();
0170   /** Called when an item is being removed from its group. */
0171   void notifyRemoving (cListObject *obj);
0172   /** Called when the removal is done. */
0173   void removeDone ();
0174   /** Called when the object  has changed. */
0175   void notifyChanged (cListObject *obj);
0176 
0177   /** Called when the list is fully loaded. Useful to perform initialisation that
0178    requires the objects. */
0179   virtual void listLoaded () {};
0180   /** Called when the list is saved. */
0181   virtual void listSaved () {};
0182   struct Private;
0183   Private *d;
0184 
0185   friend class cListObject;
0186   friend class cListGroup;
0187   friend class cListManager;
0188 };
0189 
0190 #endif