File indexing completed on 2024-12-08 06:46:34

0001 /***************************************************************************
0002                           csaveablelist.h  -  ancestor of alias/trigger handler
0003     This file is a part of KMuddy distribution.
0004                              -------------------
0005     begin                : Ne sep 8 2002
0006     copyright            : (C) 2002 by Tomas Mecir
0007     email                : kmuddy@kmuddy.com
0008  ***************************************************************************/
0009 
0010 /***************************************************************************
0011  *                                                                         *
0012  *   This program is free software; you can redistribute it and/or modify  *
0013  *   it under the terms of the GNU General Public License as published by  *
0014  *   the Free Software Foundation; either version 2 of the License, or     *
0015  *   (at your option) any later version.                                   *
0016  *                                                                         *
0017  ***************************************************************************/
0018 
0019 #ifndef CSAVEABLELIST_H
0020 #define CSAVEABLELIST_H
0021 
0022 #include <QString>
0023 
0024 class KConfig;
0025 
0026 #include "csaveablefield.h"
0027 
0028 /**
0029 Abstract class that can load/save itself.
0030 Its descendands will handle aliases/triggers.
0031   *@author Tomas Mecir
0032   */
0033 
0034 class cSaveableList {
0035 public: 
0036   /** constructor */
0037   cSaveableList (QString name, QString _objName, cSaveableField *proto);
0038   /** destructor */
0039   ~cSaveableList ();
0040 
0041   /** go back to the first item */
0042   void reset () { cur = first; };
0043   /** advance list one item forwards */
0044   void operator++ (int) { cur = ((cur == nullptr) ? nullptr : cur->next); };
0045   /** advance list one item backwards */
0046   void operator-- (int) { cur = ((cur == nullptr) ? nullptr : cur->prev); };
0047   /** return current field */
0048   cSaveableField * operator* () { return cur; };
0049   /** Is the current item valid? False if we have run away from the list */
0050   operator bool () { return (cur != nullptr); };
0051 
0052   /** is the list empty? */
0053   bool isEmpty () { return (_count == 0); };
0054   /** are we on the first item? */
0055   bool isFirst () { return (cur == first); };
0056   /** are we on the last item? */
0057   bool isLast () { return (cur == last); };
0058   /** returns count of items in the list */
0059   int count () { return _count; };
0060 
0061   /** sets marker to the current item */
0062   void setMarker () { marker = cur; };
0063   /** removes marker */
0064   void unsetMarker () { marker = nullptr; };
0065 
0066   /** Adds a new item to the begin of the list. Returns true if successful. */
0067   bool addToBegin (cSaveableField *newitem);
0068   /** Adds a new item to the end of the list. Returns true if successful. */
0069   bool addToEnd (cSaveableField *newitem);
0070   /** Adds a new item after the current item. Returns true if successful. */
0071   bool addAfterCurrent (cSaveableField *newitem);
0072   /** Adds a new item after the marked item. Returns true if successful. */
0073   bool addAfterMarker (cSaveableField *newitem);
0074   /** Replaces current item with a new one. Returns true if successful. */
0075   bool replaceCurrent (cSaveableField *newitem);
0076   /** Replaces marked item with a new one. Returns true if successful. */
0077   bool replaceMarker (cSaveableField *newitem);
0078   /** moves current item by one item towards the begin of list */
0079   bool moveCurrentToFront ();
0080   /** moves current item by one item towards the end of list */
0081   bool moveCurrentToBack ();
0082   /** Removes first item from the list. Returns true if successful. */
0083   bool removeFirst (bool dontDelete = false);
0084   /** Removes last item from the list. Returns true if successful. */
0085   bool removeLast (bool dontDelete = false);
0086   /** Removes current item from the list. Returns true if successful. */
0087   bool removeCurrent (bool dontDelete = false);
0088   /** Removes marked item from the list. Returns true if successful. */
0089   bool removeMarked (bool dontDelete = false);
0090   /** clears the whole list */
0091   void clear (bool dontDelete = false);
0092   
0093 protected:
0094   void load ();
0095 
0096   QString objName;
0097   cSaveableField *prototype;
0098 
0099   /** config object must be created before calling load/save */
0100   KConfig *config;
0101   
0102   /** required pointers to items */
0103   cSaveableField *first, *last, *cur, *marker;
0104   /** count of items is stored here */
0105   int _count;
0106 
0107 };
0108 
0109 #endif