HAL
graph_graphics_view.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
28 #include "hal_core/defines.h"
30 #include "gui/gui_globals.h"
34 
35 #include <QGraphicsView>
36 #include <QAction>
37 #include <QMenu>
38 
39 namespace hal
40 {
41  class GraphicsItem;
42  class GraphWidget;
43  class DragController;
44 
45  namespace graph_widget_constants
46  {
47  enum class grid_type;
48  }
49 
51  {
52  Q_OBJECT
53 
54  u32 mOrigin;
55  bool mPickSuccessor;
56  public:
57  GraphGraphicsViewNeighborSelector(u32 orig, bool pickSuc, QObject* parent = nullptr)
58  : GateSelectReceiver(parent), mOrigin(orig), mPickSuccessor(pickSuc) {;}
59  public Q_SLOTS:
60  void handleGatesPicked(const QSet<u32>& gats) override;
61  };
62 
71  {
72  Q_OBJECT
73 
74  friend class GraphWidget;
75  public:
82 
88  void gentleZoom(const qreal factor);
89 
95  void viewportCenterZoom(const qreal factor);
96 
99 
102 
105 
108 
109  Q_SIGNALS:
117 
122 
123  public Q_SLOTS:
130  void handleShortestPath(u32 idFrom, Node nodeTo);
131 
135  void handleRemoveFromView();
136 
137  private Q_SLOTS:
138  void conditionalUpdate();
139  void handleIsolationViewAction();
140  void adjustMinScale();
141  void handleAddCommentAction();
142 
143  void handleFoldParentSingle();
144  void handleFoldParentAll();
145  void handleUnfoldSingleAction();
146  void handleUnfoldAllAction();
147 
148  void handleShortestPathToView();
149  void handleShortestModulePathToView();
150  void handleQueryShortestPathGate();
151  void handleQueryShortestPathModule();
152  void handleSelectOutputs();
153  void handleSelectInputs();
154 
155  void handleAddModuleToView();
156  void handleAddGateToView();
157 
158  void handleAddSuccessorToView();
159  void handleAddPredecessorToView();
160  void handleAddCommonSuccessorToView();
161  void handleAddCommonPredecessorToView();
162  void handleHighlightSuccessor();
163  void handleHighlightPredecessor();
164  void handleSuccessorDistance();
165  void handlePredecessorDistance();
166  void handleCancelPickMode();
167  void handlePluginContextContributionTriggered();
168  void selectedNodeToItem();
169 
170  protected:
171  void paintEvent(QPaintEvent* event) override;
172  void drawForeground(QPainter* painter, const QRectF& rect) override;
173  void mousePressEvent(QMouseEvent* event) override;
174  void mouseMoveEvent(QMouseEvent* event) override;
175  void mouseReleaseEvent(QMouseEvent *event) override;
176  void mouseDoubleClickEvent(QMouseEvent* event) override;
177  void dragEnterEvent(QDragEnterEvent *event) override;
178  void dragLeaveEvent(QDragLeaveEvent *event) override;
179  void dragMoveEvent(QDragMoveEvent *event) override;
180  void dropEvent(QDropEvent *event) override;
181  void wheelEvent(QWheelEvent* event) override;
182  void keyPressEvent(QKeyEvent* event) override;
183  void keyReleaseEvent(QKeyEvent* event) override;
184  void resizeEvent(QResizeEvent* event) override;
185 
186  private:
187 
188  enum SearchAction {PredecessorGate, SuccessorGate, SuccessorModule};
189  void mousePressEventNotItemDrag(QMouseEvent* event);
190  void showContextMenu(const QPoint& pos);
191 
192  void updateMatrix(const int delta);
193 
194  void toggleAntialiasing();
195 
196  bool itemDraggable(GraphicsItem* item);
197 
198  void addSuccessorToView(int maxLevel, bool succ);
199  void addCommonSuccessorToView(int maxLevel, bool succ);
200 
201  void dragPan(float dpx, float dpy);
202 
203  GraphWidget* mGraphWidget;
204 
205  QSet<u32> getSelectableGates();
206  QSet<u32> getNotSelectableModules();
207 
208  struct LayouterPoint
209  {
210  int mIndex;
211  qreal mPos;
212  };
213  QPair<QPoint, QPointF> closestLayouterPos(const QPointF& scene_pos) const;
214  LayouterPoint closestLayouterPoint(qreal scene_pos, int default_spacing, int min_index, QVector<qreal> sections) const;
215 
216  #ifdef GUI_DEBUG_GRID
217  void debugShowLayouterGridpos(const QPoint& mouse_pos);
218  void debugDrawLayouterGridpos(QPainter* painter);
219  QPoint m_debug_gridpos = QPoint(0,0);
220  bool mDebugGridposEnable = true;
221  #endif
222 
223 
224 
225  GraphicsItem* mItem;
226 
227  bool mMinimapEnabled;
228 
229  bool mGridEnabled;
230  bool mGridClustersEnabled;
231  GraphicsScene::GridType mGridType;
232 
233  DragController* mDragController;
234  Qt::KeyboardModifier mDragModifier;
235 
236  QPoint mMovePosition;
237  Qt::KeyboardModifier mPanModifier;
238 
239  Qt::KeyboardModifier mZoomModifier;
240  qreal mZoomFactorBase;
241  QPointF mTargetScenePos;
242  QPointF mTargetViewportPos;
243 
244  qreal mMinScale;
245 
246  template <typename Func1>
247  void recursionLevelMenu(QMenu* menu, bool succ, Func1 slot, bool addUnlimited=false)
248  {
249  QString s = succ ? "successors" : "predecessors";
250  const char* txt[] = {"All %1", "Depth 1 (direct %1)", "Depth 2",
251  "Depth 3", "Depth 4", "Depth 5", nullptr};
252  for (int inx = 1; txt[inx]; inx++)
253  {
254  QAction* act = menu->addAction(inx==1 ? QString(txt[1]).arg(s) : QString(txt[inx]));
255  act->setData(inx);
256  connect(act, &QAction::triggered, this, slot);
257  }
258  if (addUnlimited)
259  {
260  QAction* act = menu->addAction(QString(txt[0]).arg(s));
261  act->setData(0);
262  connect(act, &QAction::triggered, this, slot);
263  }
264  }
265 
266  static const QString sAssignToGrouping;
267  };
268 }
A view to display the rendered graph (needs a GraphicsScene).
void keyPressEvent(QKeyEvent *event) override
void dragLeaveEvent(QDragLeaveEvent *event) override
void setGridType(GraphicsScene::GridType gridType)
void dropEvent(QDropEvent *event) override
void wheelEvent(QWheelEvent *event) override
void setPanModifier(Qt::KeyboardModifier panModifier)
Qt::KeyboardModifier panModifier()
void keyReleaseEvent(QKeyEvent *event) override
void viewportCenterZoom(const qreal factor)
void setDragModifier(Qt::KeyboardModifier dragModifier)
GraphGraphicsView(GraphWidget *parent)
void paintEvent(QPaintEvent *event) override
void moduleDoubleClicked(u32 id)
void mouseMoveEvent(QMouseEvent *event) override
Qt::KeyboardModifier dragModifier()
void resizeEvent(QResizeEvent *event) override
GraphicsScene::GridType gridType()
void dragEnterEvent(QDragEnterEvent *event) override
void handleShortestPath(u32 idFrom, Node nodeTo)
void mouseDoubleClickEvent(QMouseEvent *event) override
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
void drawForeground(QPainter *painter, const QRectF &rect) override
void gentleZoom(const qreal factor)
void dragMoveEvent(QDragMoveEvent *event) override
GraphGraphicsViewNeighborSelector(u32 orig, bool pickSuc, QObject *parent=nullptr)
void handleGatesPicked(const QSet< u32 > &gats) override
Wraps a GraphContext and a GraphGraphicsView.
Definition: graph_widget.h:55
Superclass for all graphic items used ins the GraphicsScene. It contains information about the underl...
Definition: graphics_item.h:44
The Node class object represents a module or a gate.
Definition: gui_def.h:61
quint32 u32
void setData(const QVariant &userData)
void triggered(bool checked)
virtual bool event(QEvent *event) override
QAction * addAction(const QString &text)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
KeyboardModifier