MvcCore/controllers/doccontroller.h

Go to the documentation of this file.
00001 /*  -*- c++ -*-  (for Emacs)
00002  *
00003  *  doccontroller.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  *    projectcontroller.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 DOCCONTROLLER_H
00042 #define DOCCONTROLLER_H
00043 
00044 
00045 #include <QObject>
00046 #include "abstractcontroller.h"
00047 
00048 #include <QCoreApplication> // TODO: move this stack to a separate class
00049 #include <QList>
00050 #include <QPointer>
00051 #include <QStack> // TODO: move this stack to a separate class
00052 #include <QString>
00053 
00054 #include "doc.h"
00055 #include "doccontrollerevents.h"
00056 
00057 
00058 class DocViewBase;
00059 
00060 
00074 class DocController : public QObject,
00075                       public AbstractController {
00076 
00077   Q_OBJECT
00078 
00079 public:
00080   DocController( QObject* parent = 0 );
00081   DocController( AbstractModel* doc, QObject* parent = 0 );
00082 
00083   Doc* doc() const;
00084 
00085   // TODO: move this stack to a separate class
00086 public slots:
00087   void undo()
00088   {
00089     if ( m_undoStack.isEmpty() ) return;
00090 
00091     UndoRedoItem* item = m_undoStack.pop();
00092     Q_ASSERT( item != 0 );
00093 
00094     // use sendEvent, so that other posted events don't jump in front of us
00095     Q_ASSERT( item->event() != 0 );
00096     QCoreApplication::sendEvent( this, item->event() );
00097     delete item->event();
00098     delete item;
00099   }
00100   void redo()
00101   {
00102     if ( m_redoStack.isEmpty() ) return;
00103 
00104     UndoRedoItem* item = m_redoStack.pop();
00105     Q_ASSERT( item != 0 );
00106 
00107     // use sendEvent, so that other posted events don't jump in front of us
00108     Q_ASSERT( item->event() != 0 );
00109     QCoreApplication::sendEvent( this, item->event() );
00110     delete item->event();
00111     delete item;
00112   }
00113 
00114 
00115 protected:
00116   inline virtual void customEvent( QEvent* e ) {
00117     CEvent* ce = dynamic_cast<CEvent*>(e); // slow :-(
00118     if ( ce != 0 ) dispatchEvent(ce);
00119   }
00120   virtual void dispatchEvent( CEvent* );
00121 
00122   virtual void changeModelEvent( CChangeModelEvent* );
00123 
00124   virtual void renameEvent( CRenameEvent* );
00125   virtual void fileSaveToEvent( CFileEvent* );
00126   virtual void fileInitFromEvent( CFileEvent* );
00127 
00128   virtual bool saveDocToFile( const QString& filename ) const;
00129   virtual bool initDocFromFile( const QString& filename, bool resetViews = true );
00130 
00131 
00132   // TODO: move this stack to a separate class
00133   QStack<UndoRedoItem*> undoStack() const // can't return ref if thread-safe
00134   {
00135     // TODO: make me thread-safe
00136     return m_undoStack;
00137   }
00138   QStack<UndoRedoItem*> redoStack() const // can't return ref if thread-safe
00139   {
00140     // TODO: make me thread-safe
00141     return m_redoStack;
00142   }
00143   bool undoAvaliable() const
00144   {
00145     return !m_undoStack.isEmpty();
00146   }
00147   bool redoAvaliable() const
00148   {
00149     return !m_redoStack.isEmpty();
00150   }
00151   // If the event has the IsUndoEvent flag set, then the item will be added to
00152   // the undoStack(). If the IsRedoEvent is set, then it will be added to
00153   // the redoStack(). If both flags are set, then it will be added to the undoStack().
00154   // Note: owenership is transfered to DocController and will be deleted at its will.
00155   // Asserts that item->event() is non-null
00156   virtual void pushUndoRedoItem( UndoRedoItem* item )
00157   {
00158     // TODO: make me thread-safe
00159     Q_ASSERT( item->event() != 0 );
00160     CEvent::Flags flags = item->event()->flags();
00161     if ( flags & CEvent::IsUndoEvent )
00162       m_undoStack.push( item );
00163     else if ( flags & CEvent::IsRedoEvent )
00164       m_redoStack.push( item );
00165   }
00166 
00167 
00168 private:
00169   // TODO: move this stack to a separate class
00170   QStack<UndoRedoItem*> m_undoStack;
00171   QStack<UndoRedoItem*> m_redoStack;
00172 
00173   // Temporary caches:
00174   QPointer<Doc> c_doc;
00175 };
00176 
00177 
00178 #endif // ! DOCCONTROLLER_H

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