MvcCore/models/elementdoc.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  elementdoc.h
00004  *  Digest
00005  * 
00006  *  Imported into Digest by Aidan Lane on Thu Jun 9 2005.
00007  *  Modifications Copyright (c) 2005 Optimisation and Constraint Solving Group,
00008  *  Monash University. All rights reserved.
00009  *
00010  *  Nodal file:
00011 
00012  *    project.h
00013  *    Nodal
00014  *
00015  *    Imported into Nodal by Aidan Lane on Thu Feb 24 2005.
00016  *    Modifications Copyright (c) 2005 CEMA, Monash University. All rights reserved.
00017  *
00018  *    Original file:
00019  *
00020  *      project.h
00021  *      EverGreen
00022  *
00023  *      Created by Aidan Lane on Wed Jul 14 2004.
00024  *      Copyright (c) 2004 Aidan Lane. All rights reserved.
00025  *
00026  *  This program is free software; you can redistribute it and/or modify
00027  *  it under the terms of the GNU General Public License as published by
00028  *  the Free Software Foundation; either version 2 of the License, or
00029  *  (at your option) any later version.
00030  *
00031  *  This program is distributed in the hope that it will be useful,
00032  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00033  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00034  *  GNU General Public License for more details.
00035  *
00036  *  You should have received a copy of the GNU General Public License
00037  *  along with this program; if not, write to the Free Software
00038  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00039  */
00040 
00041 #ifndef ELEMENTDOC_H
00042 #define ELEMENTDOC_H
00043 
00044 
00045 #include "attributedoc.h"
00046 
00047 #include <QHash>
00048 #include <QList>
00049 #include <QMultiHash>
00050 #include <QReadLocker>
00051 #include <QReadWriteLock>
00052 #include <QSet>
00053 
00054 #include "elementdocevents.h"
00055 
00056 class QByteArray;
00057 
00058 class ElementDocController;
00059 
00060 
00069 class ElementDoc : public AttributeDoc {
00070 
00071   Q_OBJECT
00072 
00073 public:
00074   ElementDoc( QObject* parent );
00075   ElementDoc( const QString& name, QObject* parent );
00076 
00077   ElementDocController* elementDocController() const;
00078 
00079   QList<AbstractElement*> elements() const;
00080   QList<AbstractElement*> elements( int type ) const;
00081   template<class T> inline QList<T*> elements() const;
00082   QHash<quint32, AbstractElement*> idToElement() const;
00083 
00084   virtual QDomElement domElement( QDomDocument& doc ) const;
00085   virtual void initFromDomElement( const QDomElement& e, InitModeFlags modeFlags );
00086 
00087   virtual quint32 genElementInstanceId();
00088 
00089 
00090 protected:
00091   virtual void dispatchEvent( MEvent* );
00092 
00093   virtual void changeControllerEvent( MChangeControllerEvent* event );
00094 
00095   virtual void elementAddPtrEvent( MElementEvent* );
00096   virtual void elementRemovePtrEvent( MElementEvent* );
00097   virtual void elementSetChangeOrderEvent( MElementSetChangeOrderEvent* );
00098 
00099   void elementAddPtr( AbstractElement* element );
00100   void elementRemovePtr( AbstractElement* element );
00101 
00102   virtual AbstractElement* createElement( const QByteArray& key,
00103                                           quint32 instanceId ) = 0;
00104 
00105 
00106 private:
00107   /* Persistent (use domElement() and initFromDOMElement()):
00108    *
00109    * Note: We use a list as the primary means of managing the elements, as to
00110    *       allow for ordering of the elements. This is usually required in
00111    *       diagrams, where the order in which elements are drawn is important.
00112    */
00113   QList<AbstractElement*> m_elements;
00114 
00115   // Temporary variables:
00116   mutable QReadWriteLock  m_elementsLock;
00117 
00118   // Temporary caches:
00119   QPointer<ElementDocController>    c_elementDocController;
00120   QMultiHash<int, AbstractElement*> c_typeToElements; // type        -> instance
00121   QHash<quint32, AbstractElement*>  c_idToElement;    // instance id -> instance
00122 };
00123 
00124 
00125 
00132 template<class T> 
00133 QList<T*> ElementDoc::elements() const
00134 {
00135   // Note: the static cast should be fairly safe, given that we specify an enum'd type
00136   QReadLocker locker( &m_elementsLock );
00137   QList<T*> set;
00138   foreach ( AbstractElement* element, c_typeToElements.values(T::classType()) )
00139     set += static_cast<T*>( element );
00140   return set;
00141 }
00142 
00143 
00144 #endif  // ! ELEMENTDOC_H

Generated on Mon Jul 30 09:46:50 2007 for Digest by  doxygen 1.5.2