• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

C:/CVUT/diplomka/Automata_editor/sources/editor.h

Go to the documentation of this file.
00001 #ifndef EDITOR_H_1321657634484
00002 #define EDITOR_H_1321657634484
00003 
00004 #include <QList>
00005 #include <QVector>
00006 #include <QGraphicsView>
00007 #include <QUndoView>
00008 
00009 #include "iautomaton.h"
00010 #include "automataCreator.h"
00011 
00012 class IAlgorithm;
00013 
00014 class QGraphicsScene;
00015 class MainWindow;
00016 class QGraphicsLineItem;
00017 class QGraphicsSimpleTextItem;
00018 class QMenu;
00019 class State;
00020 class Transition;
00021 class LabelX;
00022 class QUndoStack;
00023 class QUndoCommand;
00024 class UndoView;
00025 class QSpinBox;
00026 class QComboBox;
00027 class QLineEdit;
00028 class QCheckBox;
00029 class QRubberBand; //!< used for selection implementation
00030 
00031 class OneStateTransition;
00032 class TwoStatesTransition;
00033 class TranstionSerializer;
00034 
00035 struct StatePrevParams;
00036 struct EdgePrevParams;
00037 
00038 class IDrawAlgorithm;
00039 class IGraphViz;
00040 
00041 // logging enabled/disabled
00042 #ifdef TESTING_ADDING
00043 #   define DBGLOG_ADD(x) DBGLOG_("ADDING", x)
00044 #else
00045 #   define DBGLOG_ADD(x)
00046 #endif
00047 
00048 #ifdef TESTING_MOUSE_EVENTS
00049 #   define DBGLOG_ME(x) DBGLOG_("MOUSEEVENTS", x)
00050 #else
00051 #   define DBGLOG_ME(x)
00052 #endif
00053 
00054 enum EMouseClick {eMouseNoClick, eMouseLeftClick, eMouseRightClick, eMouseDoubleClick, eMouseMiddleClick, 
00055                   eMouseLeftShiftClick, eMouseMove};
00056 
00057 class Editor : public QGraphicsView
00058 {
00059   Q_OBJECT
00060 public:
00061 //types
00062     typedef QMap<QString, State*>   TStateMap;
00063     typedef QList<State*>           TStateList;
00064     typedef QList<Transition*>      TTransitionList;
00065 
00066     enum Format {vcg, tex, gml, eps, svg, png, bmp, xpm};
00067 
00068     Editor(MainWindow *parent = 0);
00069     ~Editor();
00070 
00071     friend class Parser;
00072     friend class StateStyleDialog;
00073     friend class StateStyleChangeCommand;
00074     friend class TransitionStyleDialog;
00075     friend class TransitionStyleChangeCommand;
00076     friend class NextLabelsDialog;
00077     friend struct StateSerializer;
00078     friend struct TransitionSerializer;
00079 
00080     QRect getGridRect();                        //!< grid coordinates
00081     void setGridRect(QRect gridRect);           //!< sets new grid rect coordinates
00082     QPointF mapToGrid(const QPointF& scenePos); //!< maps scene positon to grid position
00083 
00084     int getBorder() { return sceneBorder; }
00085     void setBorder(int b) { sceneBorder = b; }
00086 
00087     //! determinec selected action in editor,
00088     //! possible to do all actions in eSelection mode (eSelection is default mode)
00089     enum EAction {eSelection, eInsertState, eInsertTransition};
00090     //! holds current mouse aciton, affected by accelerators (CTRL, SHIFT) and by which mouse button is pressed
00091     enum EMouseAction {eNoMouseAction, eMoveItem, eMoveMultItem,
00092                        eInsertItem, eSelectItem, eSelectMultipleItems, 
00093                        eShowPopup, eShowSelectionPopup};
00094 
00095     void setAction(EAction action);
00096     EAction getAction() {return m_selectedAction;}
00097     void setSelectedState(State *state);
00098     void setSelectedTransition(Transition *transition) {selectedTransition = transition;}
00099 
00100     bool snapped() {return snapToGrid;} //!< \retval true if automatic snapping is used
00101 
00102     //! \retval false if element was deleted!
00103     bool showPopup(State *state);
00104     void showPopup(OneStateTransition *transition, const QPointF &point);
00105     void showPopup(TwoStatesTransition *transition, const QPointF &point);
00106     void insertTransition();
00107 
00108     void slotSnapToGrid();
00109     void slotAlignToGrid();
00110 
00111     void setShowGridFrame(bool grid, bool frame);
00112  
00113     void newFile();
00114     void openFile(const QString &fn);
00115     //! \name Exporting methods
00116     //! \{
00117     void exportToVaucanson(const QString &fn, bool additionals = 0);
00118     void exportToGraphML(const QString &fn = 0);
00119     void exportToEPS(const QString &fn);
00120 #ifndef DONT_USE_SVG_MODULE
00121     void exportToSVG(const QString& fn);
00122 #endif
00123     //void exportToEPS2(const QString &fn);
00124     void exportToPixmap(const QString &fn, Format f);
00125 
00126     void saveToFile(const QString &fn, bool latexHeader = false);
00127     void saveLaTeXHeader(QTextStream &out);
00128     void saveHeader(QTextStream &out);
00129     void saveVCSettings(QTextStream &out);
00130     void saveGraph(QTextStream &out);
00131     void saveFooter(QTextStream &out);
00132     void saveLaTeXFooter(QTextStream &out);
00133     //! \}
00134 
00135     IAutomaton::TAutomataList loadAutomata(const QString &dialogTitle, int count,
00136                                            QStringList &fileNames,
00137                                            IAlgorithm *algorithm = NULL);
00138 
00139     void setStatePrevParamsDef(StatePrevParams &prevParams);
00140     void setTrPrevParamsDef(EdgePrevParams &prevTrParams);
00141 
00142     void removeFromListsAndScene(Transition *transition);
00143     void removeFromListsAndScene(State *state);
00144     void removeFromListAndScene(Transition *transition);
00145     void removeFromListAndScene(LabelX *pLabel);
00146     void addToListsAndScene(Transition *transition);
00147     void addToListsAndScene(State *state);
00148     void addToListAndScene(Transition *transition);
00149     void addToListAndScene(LabelX *pLabel);
00150 
00151     void renameState(State *state, const QString &name);
00152 
00153     void setStatusBar(const QString &text, int ms);
00154 
00155     void update();
00156     void removeTransition(Transition *transition); // for undo and redo
00157 
00158     void setFileName(const QString &fn);
00159     inline QString getFileName() const { return fileName; }
00160     inline bool isAddition() const { return addition; }
00161     inline bool isSaved() const { return saved; }
00162 
00163     bool isChanged() const; //!< returns m_undoStack's clean state
00164 
00165     bool isSomethingSelected();
00166     bool isPastePossible();
00167 
00168     template<typename T>
00169     bool itemsAlignToGrid(QList<T*> items)
00170     {
00171         bool ret = false;
00172         foreach(T *item, items)
00173         {
00174             ret |= itemAlignToGrid(item);
00175         }
00176         return ret;
00177     }
00178     
00179     bool itemAlignToGrid(QGraphicsItem* item);
00180 
00181     QString getUniqueAutoName();
00182     bool testStateName(const QString &s);
00183 
00184     //! returns algorithm index
00185     int addAlgorithm(const QSharedPointer<IAlgorithm> &algorithm);
00186 
00187     //! add GraphVizDrawAlgorithm which uses given GraphViz Wrapper plugin
00188     void addGraphVizDrawAlgorithm(IGraphViz *graphVizWrapper);
00189 
00190     QList<State*> getStateList()
00191     {
00192         // TODO: return states in the same order as were loaded?
00193         return m_stateMap.values();
00194     }
00195     
00196     QList<Transition*> getTransitionList()
00197     {
00198         return m_transitionList;
00199     }
00200 
00201     //! converts \param gridPoc to scene position
00202     QPoint toScenePos(const QPointF &gridPos);
00203 
00204     //! returns state instance from \param stateMap accorgin to given \param name
00205     //! \retval NULL if no state has \param name
00206     State* getStateByName(const TStateMap &stateMap, const QString &name) const;
00207 
00208     //! \name state parameters adjustable
00209     //! \{
00210     Qt::PenStyle stateLineStyle;
00211     float stateLineWidth;
00212     QString stateLineColor;
00213     QString stateLabelColor;
00214     float stateLabelScale;
00215     Qt::BrushStyle stateFillStatus;
00216     QString stateFillColor;
00217     //! \}
00218     //! \name state parameters preset
00219     //! \{
00220     Qt::PenStyle dimStateLineStyle;
00221     QString dimStateLineColor;
00222     float dimStateLineCoef;
00223     QString dimStateLabelColor;
00224     QString dimStateFillColor;
00225     //! \}
00226     //! \name state parameters double
00227     //! \{
00228     float stateLineDoubleCoef;
00229     float stateLineDoubleSep;
00230     //! \}
00231 
00232     //! \name edge parameters adjustable
00233     //! \{
00234     Qt::PenStyle edgeLineStyle;
00235     float edgeLineWidth;
00236     QString edgeLineColor;
00237     QString edgeLabelColor;
00238     float edgeLabelScale;
00239     bool edgeLineDblStatus;
00240     //! \}
00241     //! \name transition parameters dimmed
00242     //! \{
00243     Qt::PenStyle dimEdgeLineStyle;
00244     QString dimEdgeLineColor;
00245     float dimEdgeLineCoef;
00246     QString dimEdgeLabelColor;
00247     //! \}
00248     //! \name transition parameters border
00249     //! \{
00250     float edgeLineBorderCoef;
00251     QString edgeLineBorderColor;
00252     //! \}
00253     //! \name transition parameters double
00254     //! \{
00255     float edgeLineDblCoef;
00256     float edgeLineDblSep;
00257     //! \}
00258 
00259 public slots:
00260     void showGridRectDialog();
00261     void stateMoved(State *state, const QPointF &oldPos); // pro undo a redo na move
00262     void undo();
00263     void redo();
00264     void showUndoView(bool show);
00265     void zoomIn();
00266     void zoomOut();
00267     void resetZoom();
00268 
00269     void moveState();
00270     void cutState();
00271     void copyState();
00272     void copySelection();
00273 
00274     void paste();
00275     void paste(const QPoint &insertPoint);
00276 
00277     void cutSelection();
00278     void removeSelection();
00279 
00280     //! \name editing methods
00281     //! \{
00282     void editState(State *state);
00283     void editTransition(OneStateTransition *tr);
00284     void editTransition(TwoStatesTransition *tr);
00285     void editStateStyleParams();
00286     void editTransitionStyleParams();
00287     //! \}
00288 
00289     //! \name extra functions
00290     //! \{
00291     void switchAntialiasing();
00292     void generateGraph();
00293     void createLaTeXTable();
00294     //! \}
00295 
00296     //! expands grid according to items bounding box
00297     //! \param bound keeps free space around items
00298     void expandGrid(int bound = 0);
00299 
00300     //! sender() is QAction and its data is set as index to algorithmList
00301     void runAlgorithm();
00302     
00303     //! runs SimulationDialog
00304     void simulateAutomatonWork();    
00305 
00306     void canUndoChangedSlot(bool can);    
00307     void canRedoChangedSlot(bool can);
00308 
00309     void slotShowGrid(bool show);
00310     void slotShowFrame(bool show);
00311 
00312 signals:
00313     bool canUndoChanged(bool can);
00314     bool canRedoChanged(bool can);
00315     bool canOpenFile(bool can);
00316     bool canNewFile(bool can);
00317     bool canSaveFile(bool can);
00318     
00319     bool itemsAvailable(bool available);
00320     bool toolsAvailable(bool available);
00321     bool utilsAvailable(bool available);
00322     bool showUndoStackAvailable(bool available);
00323     
00324     void isChanged(bool changed);
00325 
00326     void undoViewClosed();
00327     void showChanged(bool showG, bool showF);
00328     bool canZoomIn(bool can);
00329     bool canZoomOut(bool can);
00330     void actionChanged(int action);
00331   
00332 protected:
00333     void setRightSceneRect();
00334     void resizeEvent(QResizeEvent *event);          //!< holds background large enought (according to scene)
00335     void mousePressEvent(QMouseEvent *event);
00336     void mouseMoveEvent(QMouseEvent *event);
00337     void mouseReleaseEvent(QMouseEvent *event);
00338     
00339     void mouseDoubleClickEvent(QMouseEvent *event);
00340 
00341     void setActionCursor(EAction action);
00342     void setActionInternal(EAction action);
00343 
00344     void wheelEvent(QWheelEvent *event);
00345     void scaleView(qreal scaleFactor);
00346 
00347     //! retunrs expanded gridRect in a way that all states will be in grid with given boundary (int grid coordinates)
00348     QRect getExpandedGridRect(const TStateList &states, int bound);
00349 
00350     //! sets metu items enabled/disabled as necessary
00351     void updateMenuState();
00352 
00353     //! paints grid and labels, if required
00354     void drawBackground(QPainter *painter, const QRectF &rect);
00355 
00356     QList<State *> generateExactMatching(QString string, QPoint startPoint);
00357     QList<State *> generateDistance(int typ, QString string, int distance, QPoint startPoint);
00358 
00359     void addToUndoStack(QUndoCommand *command);
00360 
00361     QRectF getPrintRect();
00362     
00363     int getNewStateNumber() { return newStateNumber; }  // autonaming functions
00364     void increaseNewStateNumber() { newStateNumber++; }
00365 
00366     void itemsInvertSelection(QList<QGraphicsItem* > items);
00367 
00368     QString getUniqueAutoName(const TStateMap &stateMap);
00369     bool testStateName(const QString &s, const TStateMap &stateMap);
00370 
00371     // interface support
00372     QList<State *> getInitialStates(const QList<State*> &stateList);
00373     QList<State *> getFinalStates(const QList<State*> &stateList);
00374 
00375     //! loads single automaton from current file for actions with automaton
00376     //! (use when don't want to modify automaton)
00377     QSharedPointer<AutomatonImpl> loadAutomaton(const QString &dialogTitle);
00378 
00379     QStringList getDrawAlgorithms() const;
00380 
00381 private slots:
00382     void setClean(bool clean); //!< sets mainwindow head according to m_undoStack clean state
00383 
00384     void removeTransition();
00385     void editState();
00386     void removeState();
00387     void editOneStateTransition();
00388     void editTwoStatesTransition();
00389     void editStateParams();
00390     void editTransitionParams();
00391     void editNextLabels();
00392     void pasteOnPos();
00393     
00394     void simulationFinished();
00395     void setStatesMarked(QList<State*> states, bool marked);
00396 
00397     /*!
00398      * due to performance -> only sets up internal flag m_sceneChanged
00399      * when scene signal changed() is emited -> don't forget connect it!
00400      */
00401     void sceneChanged(const QList<QRectF> &rects);
00402 
00403 protected:
00404     MainWindow *mw;         //!< parent window, for display messages with statusBar()
00405     int sceneBorder;
00406 
00407     QPoint br;              //!< bottomRight -> scene usage
00408     QRect gridRect;         //!< VauCanSon-G grid metrics
00409 
00410     State *startState;              //!< for inserting transitions
00411     State *selectedState;           //!< for operations on states
00412     Transition *selectedTransition; //!< for operations on transitions
00413 
00414     void insertState(const QPoint &point);    
00415     State* getStateByName(const QString &name) const
00416     {
00417         return getStateByName(m_stateMap, name);
00418     }
00419 
00420     //! used by parser
00421     State* insertState(State *state);
00422 
00423     EAction         m_selectedAction;       //!< holds selected editor tool
00424     EMouseAction    m_currentMouseAction;   //!< holds current mouse action
00425 
00426     int newStateNumber;                     //!< automatic naming as Q1, Q2, ...
00427 
00428     TStateMap           m_stateMap;
00429     TTransitionList     m_transitionList;
00430 
00431     bool snapToGrid;
00432     bool showGrid;
00433     bool showFrame;
00434 
00435     QUndoStack      *m_undoStack;           //!< undo, redo functionality \sa Qt Undo Framework
00436     UndoView        *m_undoView;            //!< allows to show m_undoStack and operate on it
00437     void createUndoView();
00438 
00439     //! \name popup menu
00440     //! \{
00441     QMenu   *popup;
00442     QAction *popupPasteHere;
00443 
00444     QMenu   *popupSelection;
00445 
00446     QMenu   *popupState;
00447     QAction *popupStateLabel;
00448     
00449     QMenu   *popupTransition;
00450     QAction *popupTransitionLabel;
00451     
00452     QAction *popupTransitionEdit;
00453     QAction *popupOneStateTransitionEdit;
00454     QAction *popupTwoStatesTransitionEdit;
00455 
00456     QPoint  m_popupPoint;
00457     //! \}
00458 
00459     bool antial;    //!< true if scene is rendered in anialiasing mode
00460 
00461     //! \name save
00462     //! \{
00463     QString fileName;
00464     bool saved;
00465     bool addition;
00466     //! \}
00467     
00468     QRubberBand         *m_rubberBand;      //!< determines current selection
00469     QPoint              m_rubberBandOrigin; //!< rubberBand origin point (stored on press event)
00470 
00471     EMouseClick         m_lastMouseAction;  //!< remembers last mouse action
00472     QPointF             m_origPos;          //!< original mouse position for moving with multiple states
00473     QVector<QPointF>    m_origPositions;    //!< original positions of selected items before moving them
00474     bool                m_itemsMoved;       //!< for performance -> no undo command needed if nothing is moved
00475 
00476     //! special selected state of editor, state is moved by mouse move, click to release this mode
00477     bool                m_stateIsMoving;
00478 
00479     //! performance flag for better handling mouse release event -> if scene has been changed,
00480     //! view's sceneRect should be updated
00481     bool                m_sceneChanged;
00482     
00483     bool                m_simulationIsRun; //!< if simulation is running, no operations with object on scene are available
00484 
00485     qreal m_zoomInFactor; //!< zoomOutFactor = 1/m_zoomInFactor;
00486 
00487     typedef QList<QSharedPointer<IAlgorithm> >      TAlgorithmList;
00488     TAlgorithmList  m_algorithmList;            //!< holds list of available algorithms
00489 
00490     typedef QList<QSharedPointer<IDrawAlgorithm> >  TDrawAlgorithmList;
00491     TDrawAlgorithmList  m_drawAlgorithmList;    //!< holds drawing algorithms
00492     int                 m_currentDrawAlgorithm; //!< index of lastly used drawing algorithm
00493     
00494 public: 
00495     QSharedPointer<IAutomataCreator>    getAutomataCreator();           //!< returns public automata creator
00496         
00497     static ITransition::TCharSet   parseCharSet(const QString &text);   //!< primarilly used for parse alphabet
00498     static QString                 parseCharacter(const QString &text); //!< ensures that given text contains just one symbol
00499         
00500 protected:
00501 
00502     //! Parse one automaton from whole scene.
00503     //! Uses getAutomata() and then join parsed automata.
00504     //! Can detect automata setting if needed.
00505     QSharedPointer<AutomatonImpl> getAutomaton(const TStateList &stateList,
00506                                                ITransition::TCharSet &alphabet,
00507                                                QString &alphabetSymbol,
00508                                                QString &epsilonSymbol);
00509 
00510 #if 0
00511     //! Parse automata on scene, each subgraph as automaton.
00512     //! Can detect automata setting if needed.
00513     AutomatonImpl::TAutomatonList getAutomata(const QList<State*> &stateList,
00514                                               ITransition::TCharSet &alphabet,
00515                                               QString &alphabetSymbol,
00516                                               QString &epsilonSymbol);
00517                                               
00518     IAutomaton::TAutomataList getIAutomata(const QList<State*> &stateList,
00519                                            ITransition::TCharSet &alphabet,
00520                                            QString &alphabetSymbol,
00521                                            QString &epsilonSymbol);
00522 #endif
00523 
00524     //! Detects parameters of automata on scene.
00525     //! \sa Transition::getCharactersOccureces().
00526     bool detectAutomataSettings(const QList<State *> &stateList, // in
00527                                 ITransition::TCharSet &alphabet, // out
00528                                 QString &alphabetSymbol, // out
00529                                 QString &epsilonSymbol); // out    
00530     
00531     //! Should be used only if algorithm does positioning
00532     //! \sa IAutomaton::hasPositions()
00533     QList<State*> createGraphicsItems(const QSharedPointer<IAutomaton> &automaton);
00534 };
00535 
00536 
00537 /*!
00538  * Allows to show undo stack and provides operations on it.
00539  */
00540 class UndoView : public QUndoView
00541 {
00542     Q_OBJECT
00543 public:
00544     UndoView(QUndoStack *stack, QWidget *parent = 0);
00545 
00546 protected:
00547     void closeEvent(QCloseEvent *event);
00548     
00549 signals:
00550     void closed();
00551 };
00552 
00553 #endif

Generated on Tue Jan 4 2011 03:03:23 for Autoamata editor by  doxygen 1.7.0