00001 #ifndef GRAPHWIDGET_H
00002 #define GRAPHWIDGET_H
00003
00004 #include <QGraphicsColorizeEffect>
00005 #include <QGraphicsView>
00006 #include <QPointF>
00007 #include <QComboBox>
00008 #include <QColor>
00009 #include <QPushButton>
00010 #include <QLabel>
00011
00012 #include "System.h"
00013 #include "Graph.h"
00014 #include "ExtendedGraph.h"
00015 #include <interfaces/LayoutPlugin.h>
00016 #include <interfaces/GraphWidgetListener.h>
00017 #include <interfaces/LookFactoryPlugin.h>
00018
00019 namespace Elve {
00020
00021 class MainWindow;
00022
00023 enum SelectionMode {
00024 CLEAR,
00025 ADD,
00026 SUB,
00027 MULT
00028 };
00029
00030 class GraphWidget : public QGraphicsView
00031 {
00032 Q_OBJECT
00033 public:
00034 GraphWidget(QWidget *parent = nullptr, GraphWidgetListener* listener = new DummyListener());
00035
00036 void setGraph(SharedEGraph graph, unsigned quickTicks = 500);
00037
00038
00039 void tick(float dt, bool update = true);
00040
00041
00042 void init();
00043
00044
00045 void timerEvent(QTimerEvent *event) override;
00046 void wheelEvent(QWheelEvent *event) override;
00047 void showEvent(QShowEvent *event);
00048 void mousePressEvent(QMouseEvent *event) override;
00049 void mouseReleaseEvent(QMouseEvent *event) override;
00050 void mouseDoubleClickEvent(QMouseEvent* event) override;
00051 void mouseMoveEvent(QMouseEvent *event) override;
00052 void keyPressEvent(QKeyEvent *event) override;
00053 void paintEvent(QPaintEvent* ev) override;
00054
00055 void group(const Selection &names, const NodeName& groupName = "group");
00056 void ungroup(const NodeIDSet &names);
00057
00058 void quickSim(unsigned ticks);
00059 void setLayout(const SharedLayout &l);
00060 void reflect(System &sys, SharedGraph g, SharedLook lf);
00061
00062 void select(const NodeIDSet& names, SelectionMode mode);
00063
00064 void drawBackground(QPainter *painter, const QRectF &rect) override;
00065 void drawForeground(QPainter *painter, const QRectF &rect) override;
00066
00067 void fit();
00068
00069 void updateSelectionColor();
00070
00071 const SharedEGraph& graph() const;
00072 QGraphicsScene* scene();
00073
00074
00075 ~GraphWidget();
00076 public slots:
00077 void borderSelect();
00078 void toggleSelection();
00079 void group();
00080 void ungroup();
00081 void setCurrentMask(int i);
00082 void start();
00083 void stop();
00084 void toggleSim(bool sim);
00085 void quickSim();
00086 signals:
00087 void maskChanged(int i);
00088 private:
00089 class Behaviour
00090 {
00091 public:
00092 Behaviour(GraphWidget* parent) : gw(*parent) {}
00093 virtual void onStart(){}
00094 virtual bool mousePressEvent(QMouseEvent *){return false;}
00095 virtual bool mouseReleaseEvent(QMouseEvent *){return false;}
00096 virtual bool mouseMoveEvent(QMouseEvent *){return false;}
00097 virtual void onEnd(){}
00098 protected:
00099 GraphWidget& gw;
00100 };
00101
00102 class BorderSelect : public Behaviour
00103 {
00104 public:
00105 BorderSelect(GraphWidget* parent);
00106 void onStart() override;
00107 bool mousePressEvent(QMouseEvent *event) override;
00108 bool mouseReleaseEvent(QMouseEvent *event) override;
00109 bool mouseMoveEvent(QMouseEvent *event) override;
00110 void onEnd() override;
00111 private:
00112 QPointF mOrigin;
00113 QGraphicsRectItem* mRectangle;
00114 QGraphicsPathItem* mCross;
00115 };
00116
00117 void setBehaviour(Behaviour* b);
00118 void clear();
00119 void clearScene();
00120 void unsetGraph();
00121 void clearEdgesPaths();
00122 void flushPen(QPen& pen, QPainterPath& path, const QPen& newPen);
00123 void updateEdges();
00124
00125 QGraphicsScene* mScene;
00126 bool mDrag;
00127 qreal mScale;
00128 QPointF mLastPos;
00129 QGraphicsPixmapItem* mPlayPauseIcon;
00130 SharedEGraph mGraph;
00131 Behaviour* mBehaviour;
00132 QComboBox* mSelectionBox;
00133
00134 std::vector<EdgeLook*> mEdges;
00135 std::vector<NodeLook*> mNodes;
00136 QList<QGraphicsPathItem*> mEdgesPaths;
00137 GraphWidgetListener* mListener;
00138 static std::array<QColor,10> mSelectionColors;
00139 qreal mTargetScale;
00140 int mTimerId;
00141 bool mSim;
00142 };
00143
00144 }
00145
00146 #endif // GRAPHWIDGET_H