Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Examples  

object.h

Go to the documentation of this file.
00001 
00002 //
00003 // YASA application config
00004 //
00005 // Project: Yasa 2
00006 // Author : Jan Blumenthal
00007 // Start  : 2002/02/28
00008 // $Header: /sources/yasa/yasagui/object.h,v 1.6 2003/01/24 15:47:41 bj Exp $
00009 //
00011 //
00012 // This program is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU General Public License as published by
00014 // the Free Software Foundation; either version 2 of the License, or
00015 // (at your option) any later version.
00016 //
00017 // This program is distributed in the hope that it will be useful,
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU General Public License
00023 // along with this program (See the included file COPYING);
00024 // if not, write to the Free Software Foundation, Inc.,
00025 // 675 Mass Ave, Cambridge, MA 02139, USA.
00026 //
00028 #ifndef YASAGUI_OBJECT_INCLUDE
00029 #define YASAGUI_OBJECT_INCLUDE
00030 
00031 
00033 #include <yasagui/yasagui.h>
00034 #include <qobject.h>
00035 #include "translations.h"
00036 #include "config.h"
00037 
00038 
00040 class YInputParser;
00041 class YOutputParser;
00042 class YActions;
00043 class YSet;
00044 
00048 class YObjectArgs
00049 {   
00050     QString             name;
00052     YActions            *actions;
00054     YSet                *set;
00055 public:
00057 
00058 
00059 
00060                         YObjectArgs( const YObjectArgs &i, YActions *a=0, YSet *s=0 ) :
00061                         name(i.name),
00062                         actions(a),
00063                         set(s)
00064                         {}
00065                         YObjectArgs( const QString &str=QString::null, YActions *a=0, YSet *s=0 ) :
00066                         name(str),
00067                         actions(a),
00068                         set(s)
00069                         {}
00071 
00072     const QString&      GetName() const             {   return name;                                }
00074     YActions*           GetActions() const          {   return actions;                             }
00076     YSet*               GetSet() const              {   return set;                                 }
00077 };
00078 
00079 
00083 class YObject : public QObject, protected QString
00084 {   Q_OBJECT
00086     QString             description;
00088     YActions            *actions;
00090     YSet                *set;
00092     bool                changed;
00093 public:                 YObject(const YObjectArgs &i=YObjectArgs() ) :
00094                         QString( i.GetName() ),
00095                         actions( i.GetActions() ),
00096                         set( i.GetSet() )
00097                         {}
00098                         /*~YObject()
00099                         {
00100                             //emit DestroyObject();
00101                             //YActions::ObjectDestroyed(this);
00102                         }*/
00104     void                SetChanged(bool b)          {   changed=b;                              }
00105     //\return true if the object flag is set.
00106     bool                GetChanged() const          {   return changed;                         }
00108     const QString&      GetName() const             {   return *this;                           }
00110     const QString&      GetDescription() const      {   return description;                     }
00112     //\return Error or NULL
00113     int                 SetDescription(const QString &d);
00115     //\return Error or NULL
00116     int                 SetName(const QString &n);
00118     //\return Error or NULL
00119     int                 SetName(const char *n);
00120     virtual int         SetConfig(YInputParser &parser);
00121     virtual int         GetConfig(YOutputParser &parser);
00123     YActions*           GetActions() const          {   return actions;                         }
00125     YSet*               GetSet() const              {   return set;                             }
00127     void                UpdateConnect(const QObject *o, const char *member) const
00128                         {
00129                             connect( this, SIGNAL(UpdateObject() ), o, member);
00130                         }
00132     void                UpdateArgConnect(const QObject *o, const char *member) const
00133                         {
00134                             connect( this, SIGNAL(UpdateObject(YObject *) ), o, member);
00135                         }
00136 signals:
00138     void                UpdateObject();
00140     void                UpdateObject(YObject *);
00141 public slots:
00143     void                ObjectUpdated()             {   changed=true;
00144                                                         emit UpdateObject();
00145                                                         emit UpdateObject(this);                }
00146 };
00147 
00148 
00149 
00150 
00153 class YDerivedObject : public YObject
00154 {   Q_OBJECT
00155 protected:              YDerivedObject(const YObjectArgs &args) :
00156                         YObject(args)
00157                         {}
00160     void                ConnectDerivedParent(const YObject *p)
00161                         {   if ( p )
00162                             {
00163                                 connect(p, SIGNAL( UpdateObject() ), this, SLOT( DerivedParentContentChanged() ) );
00164                                 connect(p, SIGNAL( DisableObjects(bool) ), this, SLOT( ObjectDisabled(bool) ) );
00165                             }
00166                         }
00167     void                DisconnectDerivedParent(const YObject *p)
00168                         {   if ( p )
00169                             {
00170                                 disconnect(p, SIGNAL( UpdateObject() ), this, SLOT( DerivedParentContentChanged() ) );
00171                                 disconnect(p, SIGNAL( DisableObjects(bool) ), this, SLOT( ObjectDisabled(bool) ) );
00172                             }
00173                         }
00174 protected:
00177     void                DisableChildObjects(bool b)             {   emit DisableObjects(b);         };
00178 public:
00180     virtual QString     GetContentString()  const               {   return QString::null;           }
00183     virtual QString     GetVisibleContentString() const         {   return QString::null;           }
00185     virtual int         ReadFromString(const QString &s)        {   return 0;
00186                                                                     Q_CONSTUNUSED(s);               }
00188     virtual bool        IsValid() const=0;
00189 protected slots:
00195     virtual void        DerivedParentContentChanged()           {   /*qWarning("Not used by this YObject");*/   }
00199     virtual void        ObjectDisabled(bool)                    {                                               }
00200 signals:
00202     void                DisableObjects(bool);
00203 };
00204 
00205 
00208 
00217 class DerivedTest
00218 {
00219     int a;
00220 };
00221 
00224 
00233 template<class T>
00234 class DerivedTest2
00235 {
00236     T           content;
00237 };
00238 
00241 
00250 template<class T>
00251 class YDerivedType : public YDerivedObject
00252 {   
00255     T                   content;
00259     bool                valid;
00261     bool                disabled;
00263     bool                childsdisabled;
00265     const YDerivedType<T> *derivedparent;
00266 public:                 YDerivedType(const T &c, const YDerivedType<T> *p=0, const YObjectArgs &args=YObjectArgs() ):
00267                         YDerivedObject(args),
00268                         //content(c),
00269                         valid(true),
00270                         disabled(false),
00271                         childsdisabled(false),
00272                         derivedparent(0)    // will be set later !
00273                         {
00274                             SetContent(c);
00275                             if (p)
00276                             {
00277                                 disabled=p->IsDisabled();
00278                                 SetParent(p);
00279                             }
00280                         }
00282                         YDerivedType(const YDerivedType<T> *p, const YObjectArgs &args=YObjectArgs() ):
00283                         YDerivedObject(args),
00284                         //content(0),   // not possible due to using types of structs or classes
00285                         valid(false),
00286                         disabled(false),
00287                         childsdisabled(false),
00288                         derivedparent(0)    // will be set later !
00289                         {
00290                             if (!p)
00291                                 qWarning("YDerivedType::YDerivedType() called with illegal parent!");
00292                             else
00293                             {
00294                                 //disabled=p->AreChildsDisabled();
00295                                 SetParent(p);
00296                                 //ConnectDerivedParent(p);
00297                             }
00298                         }
00305                         YDerivedType(const YDerivedType<T> &p) :
00306                         YDerivedObject( YObjectArgs(p.GetName(), p.GetActions(), 0 ) ), // copied instances have not the same set !
00307                         valid( p.IsValid() ),
00308                         disabled( p.IsDisabled() ),
00309                         childsdisabled( p.childsdisabled ),
00310                         derivedparent( 0 ) //will be set later p.GetDerivedParent() )
00311                         {
00312                             if ( p.GetDerivedParent() )
00313                             {
00314                                 //ConnectDerivedParent( derivedparent );
00315                                 SetParent( p.GetDerivedParent() );
00316                             }
00317                             if ( p.IsValid() )
00318                                 SetContent( p.GetContent() );
00319                             qWarning("YDerivedType::YDerivedType(const YDerivedType<T> &p): Use copy constructors with care!");
00320                         }
00323                         /*YDerivedType(const YDerivedType<T> &p) :
00324                         YDerivedObject( YObjectArgs(p.GetName(), p.GetActions(), 0 ) ), // copied instances have not the same set !
00325                         //content(p.content)
00326                         //valid(p.set),
00327                         //parent(p.parent)
00328                         {
00329                             qWarning("YDerivedType::YDerivedType(const YDerivedType<T> &p): Use Copy constructors with care!");
00330                             Q_CONSTUNUSED(p);
00331                         }*/
00332                         /*
00333                         ~YDerivedType()
00334                         {
00335                             if ( className() == "YASA_PRIORITY" )
00336                                 qWarning("YDerivedType::YDerivedType(const YDerivedType<T> &p): Use Copy constructors with care!");
00337                         }*/
00341     void                SetParent(const YDerivedType<T> *p)
00342                         {
00343                             if ( p )
00344                             {
00345                                 if ( derivedparent )
00346                                 {
00347                                     DisconnectDerivedParent(derivedparent);
00348                                 }
00349                                 derivedparent=p;
00350                                 ConnectDerivedParent(derivedparent);
00351                                 //if ( derivedparent->AreChildsDisabled() )
00352                                 ObjectDisabled( derivedparent->AreChildsDisabled() );
00353                             }
00354                             else
00355                                 // do not detach here due to avoid changing the contents
00356                                 qWarning("YDerivedType<T>::SetParent() called with illegal arguments! That is real problem!");
00357                         }
00363     void                DetachParent()
00364                         {
00365                             if ( derivedparent )
00366                             {
00367                                 T       old_content=GetContent();
00368                                 bool    old_disabled=disabled;
00369                                 bool    old_childsdisabled=childsdisabled;
00370                                 DisconnectDerivedParent(derivedparent);
00371                                 disabled=false;
00372                                 derivedparent=0;
00373                                 if ( IsValid() )
00374                                     SetContent(old_content);
00375                                 SetChildsDisabled( old_disabled || old_childsdisabled );
00376                             }
00377                         }
00378 
00380     virtual bool        IsValid() const                     {   return valid;                                       }
00381 
00383     bool                IsDisabled() const                  {   return disabled;                                    }
00385     bool                AreChildsDisabled() const           {   return disabled || childsdisabled;                  }
00388     void                SetChildsDisabled(bool b)           {   if (childsdisabled!=b)
00389                                                                 {
00390                                                                     childsdisabled=b;
00391                                                                     DisableChildObjects(b);
00392                                                                 }
00393                                                             }
00394 
00396     void                Invalid()                           {   if (derivedparent && valid)
00397                                                                 {
00398                                                                     valid=false;
00399                                                                     ObjectUpdated();
00400                                                                 }
00401                                                             }
00402 
00406     const YDerivedType<T>*  GetValidContent() const         {   const YDerivedType<T>   *derivedtype=this;
00407                                                                 do
00408                                                                 {
00409                                                                     if (! derivedtype->IsDisabled() )
00410                                                                         break;
00411                                                                     derivedtype=derivedtype->derivedparent;
00412                                                                 } while ( derivedtype && derivedtype->derivedparent );
00413                                                                 while ( derivedtype->derivedparent && !derivedtype->IsValid() )
00414                                                                     derivedtype=derivedtype->derivedparent;
00415                                                                 return derivedtype;
00416                                                             }
00417 
00418     const YDerivedType<T>*  GetDerivedParent() const        {   return derivedparent;                               }
00422     const T&            GetContent() const                  {   return GetValidContent()->content;                  }
00423 
00427     const T&            SetContent(const T &c)              {   T newcontent;
00428                                                                 if ( CheckLimits(newcontent,c) )
00429                                                                 {
00430                                                                     if ( (content!=newcontent) || (!valid) )
00431                                                                     {
00432                                                                         valid=true;
00433                                                                         content=newcontent;
00434                                                                         ObjectUpdated();
00435                                                                     }
00436                                                                 }
00437                                                                 //else
00438                                                                 //  Invalid();      // check limits returns false->content invalid ?
00439                                                                 return content;
00440                                                             }
00441 
00445     //\example QString name<<ysettingname;
00446     const T&            operator >> (T& dest) const         {   dest=GetContent();
00447                                                                 return dest;                                        }
00449     const T&            operator = (const T &c)             {   return SetContent(c);                               }
00450 
00453     const YDerivedType<T>& operator = (const YDerivedType<T> &c)
00454                                                             {   operator =(c.GetContent() );
00455                                                                 return *this;                                       }
00457     const T&            operator <<(const T &c)             {   return operator =(c);                               }
00458 
00460     bool                operator !=(const YDerivedType<T> &c) const
00461                                                             {   return GetContent() != c.GetContent();              }
00463     bool                operator !=(const T &c) const       {   return GetContent() != c;                           }
00465     bool                operator ==(const YDerivedType<T> &c) const
00466                                                             {   return GetContent() == c.GetContent();              }
00468     bool                operator ==(const T &c) const       {   return GetContent() == c;                           }
00470                         operator bool()                     {   return IsValid();                                   }
00471 
00476     virtual bool        CheckLimits(T& dest, const T &source) const
00477                                                             {   dest=source;
00478                                                                 return true;                                        }
00484     virtual void        DerivedParentContentChanged()       {   /*  qWarning("Name:%s", GetName()); */
00485                                                                 // if there is a valid content it must be set again
00486                                                                 // to check the content limits
00487                                                                 if (derivedparent)
00488                                                                 {
00489                                                                     if ( IsValid() )
00490                                                                     {
00491                                                                         //Invalid();            // force UpdateObject for child objects
00492                                                                         valid=false;        
00493                                                                         SetContent( content );  // check current content again, maybe limits have changed
00494                                                                     }
00495                                                                     else
00496                                                                         ObjectUpdated();
00497                                                                 }
00498                                                             }
00502     virtual void        ObjectDisabled(bool b)              {
00503                                                                 if ( disabled!=b)
00504                                                                 {
00505                                                                     disabled=b;
00506                                                                     DerivedParentContentChanged();
00507                                                                     DisableChildObjects(b);     // inform child objects
00508                                                                 }
00509                                                             }
00510 };
00511 
00512 
00513 
00514 #define Typedef_YDerivedObject(type, name)  typedef YDerivedType<type> name; inline const type &    operator << (type &dest, const name &source) {  return source.operator >>(dest); }
00515 
00516 
00517 
00518 
00520 #endif                                                                  // ifndef YASAGUI_OBJECT_INCLUDE

Generated on Thu Feb 13 23:43:54 2003 for Yasa by doxygen1.3-rc3