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;
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
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
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();
00081 void setGridRect(QRect gridRect);
00082 QPointF mapToGrid(const QPointF& scenePos);
00083
00084 int getBorder() { return sceneBorder; }
00085 void setBorder(int b) { sceneBorder = b; }
00086
00087
00088
00089 enum EAction {eSelection, eInsertState, eInsertTransition};
00090
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;}
00101
00102
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
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
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);
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;
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
00185 int addAlgorithm(const QSharedPointer<IAlgorithm> &algorithm);
00186
00187
00188 void addGraphVizDrawAlgorithm(IGraphViz *graphVizWrapper);
00189
00190 QList<State*> getStateList()
00191 {
00192
00193 return m_stateMap.values();
00194 }
00195
00196 QList<Transition*> getTransitionList()
00197 {
00198 return m_transitionList;
00199 }
00200
00201
00202 QPoint toScenePos(const QPointF &gridPos);
00203
00204
00205
00206 State* getStateByName(const TStateMap &stateMap, const QString &name) const;
00207
00208
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
00219
00220 Qt::PenStyle dimStateLineStyle;
00221 QString dimStateLineColor;
00222 float dimStateLineCoef;
00223 QString dimStateLabelColor;
00224 QString dimStateFillColor;
00225
00226
00227
00228 float stateLineDoubleCoef;
00229 float stateLineDoubleSep;
00230
00231
00232
00233
00234 Qt::PenStyle edgeLineStyle;
00235 float edgeLineWidth;
00236 QString edgeLineColor;
00237 QString edgeLabelColor;
00238 float edgeLabelScale;
00239 bool edgeLineDblStatus;
00240
00241
00242
00243 Qt::PenStyle dimEdgeLineStyle;
00244 QString dimEdgeLineColor;
00245 float dimEdgeLineCoef;
00246 QString dimEdgeLabelColor;
00247
00248
00249
00250 float edgeLineBorderCoef;
00251 QString edgeLineBorderColor;
00252
00253
00254
00255 float edgeLineDblCoef;
00256 float edgeLineDblSep;
00257
00258
00259 public slots:
00260 void showGridRectDialog();
00261 void stateMoved(State *state, const QPointF &oldPos);
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
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
00290
00291 void switchAntialiasing();
00292 void generateGraph();
00293 void createLaTeXTable();
00294
00295
00296
00297
00298 void expandGrid(int bound = 0);
00299
00300
00301 void runAlgorithm();
00302
00303
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);
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
00348 QRect getExpandedGridRect(const TStateList &states, int bound);
00349
00350
00351 void updateMenuState();
00352
00353
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; }
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
00372 QList<State *> getInitialStates(const QList<State*> &stateList);
00373 QList<State *> getFinalStates(const QList<State*> &stateList);
00374
00375
00376
00377 QSharedPointer<AutomatonImpl> loadAutomaton(const QString &dialogTitle);
00378
00379 QStringList getDrawAlgorithms() const;
00380
00381 private slots:
00382 void setClean(bool clean);
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
00399
00400
00401 void sceneChanged(const QList<QRectF> &rects);
00402
00403 protected:
00404 MainWindow *mw;
00405 int sceneBorder;
00406
00407 QPoint br;
00408 QRect gridRect;
00409
00410 State *startState;
00411 State *selectedState;
00412 Transition *selectedTransition;
00413
00414 void insertState(const QPoint &point);
00415 State* getStateByName(const QString &name) const
00416 {
00417 return getStateByName(m_stateMap, name);
00418 }
00419
00420
00421 State* insertState(State *state);
00422
00423 EAction m_selectedAction;
00424 EMouseAction m_currentMouseAction;
00425
00426 int newStateNumber;
00427
00428 TStateMap m_stateMap;
00429 TTransitionList m_transitionList;
00430
00431 bool snapToGrid;
00432 bool showGrid;
00433 bool showFrame;
00434
00435 QUndoStack *m_undoStack;
00436 UndoView *m_undoView;
00437 void createUndoView();
00438
00439
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;
00460
00461
00462
00463 QString fileName;
00464 bool saved;
00465 bool addition;
00466
00467
00468 QRubberBand *m_rubberBand;
00469 QPoint m_rubberBandOrigin;
00470
00471 EMouseClick m_lastMouseAction;
00472 QPointF m_origPos;
00473 QVector<QPointF> m_origPositions;
00474 bool m_itemsMoved;
00475
00476
00477 bool m_stateIsMoving;
00478
00479
00480
00481 bool m_sceneChanged;
00482
00483 bool m_simulationIsRun;
00484
00485 qreal m_zoomInFactor;
00486
00487 typedef QList<QSharedPointer<IAlgorithm> > TAlgorithmList;
00488 TAlgorithmList m_algorithmList;
00489
00490 typedef QList<QSharedPointer<IDrawAlgorithm> > TDrawAlgorithmList;
00491 TDrawAlgorithmList m_drawAlgorithmList;
00492 int m_currentDrawAlgorithm;
00493
00494 public:
00495 QSharedPointer<IAutomataCreator> getAutomataCreator();
00496
00497 static ITransition::TCharSet parseCharSet(const QString &text);
00498 static QString parseCharacter(const QString &text);
00499
00500 protected:
00501
00502
00503
00504
00505 QSharedPointer<AutomatonImpl> getAutomaton(const TStateList &stateList,
00506 ITransition::TCharSet &alphabet,
00507 QString &alphabetSymbol,
00508 QString &epsilonSymbol);
00509
00510 #if 0
00511
00512
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
00525
00526 bool detectAutomataSettings(const QList<State *> &stateList,
00527 ITransition::TCharSet &alphabet,
00528 QString &alphabetSymbol,
00529 QString &epsilonSymbol);
00530
00531
00532
00533 QList<State*> createGraphicsItems(const QSharedPointer<IAutomaton> &automaton);
00534 };
00535
00536
00537
00538
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