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, u32 idTo);
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 handleQueryShortestPath();
150  void handleSelectOutputs();
151  void handleSelectInputs();
152 
153  void handleAddModuleToView();
154  void handleAddGateToView();
155 
156  void handleAddSuccessorToView();
157  void handleAddPredecessorToView();
158  void handleAddCommonSuccessorToView();
159  void handleAddCommonPredecessorToView();
160  void handleHighlightSuccessor();
161  void handleHighlightPredecessor();
162  void handleSuccessorDistance();
163  void handlePredecessorDistance();
164  void handleCancelPickMode();
165  void handlePluginContextContributionTriggered();
166  void selectedNodeToItem();
167 
168  protected:
169  void paintEvent(QPaintEvent* event) override;
170  void drawForeground(QPainter* painter, const QRectF& rect) override;
171  void mousePressEvent(QMouseEvent* event) override;
172  void mouseMoveEvent(QMouseEvent* event) override;
173  void mouseReleaseEvent(QMouseEvent *event) override;
174  void mouseDoubleClickEvent(QMouseEvent* event) override;
175  void dragEnterEvent(QDragEnterEvent *event) override;
176  void dragLeaveEvent(QDragLeaveEvent *event) override;
177  void dragMoveEvent(QDragMoveEvent *event) override;
178  void dropEvent(QDropEvent *event) override;
179  void wheelEvent(QWheelEvent* event) override;
180  void keyPressEvent(QKeyEvent* event) override;
181  void keyReleaseEvent(QKeyEvent* event) override;
182  void resizeEvent(QResizeEvent* event) override;
183 
184  private:
185  void mousePressEventNotItemDrag(QMouseEvent* event);
186  void showContextMenu(const QPoint& pos);
187 
188  void updateMatrix(const int delta);
189 
190  void toggleAntialiasing();
191 
192  bool itemDraggable(GraphicsItem* item);
193 
194  void addSuccessorToView(int maxLevel, bool succ);
195  void addCommonSuccessorToView(int maxLevel, bool succ);
196 
197  void dragPan(float dpx, float dpy);
198 
199  GraphWidget* mGraphWidget;
200 
201  QSet<u32> getSelectableGates();
202  QSet<u32> getNotSelectableModules();
203 
204  struct LayouterPoint
205  {
206  int mIndex;
207  qreal mPos;
208  };
209  QPair<QPoint, QPointF> closestLayouterPos(const QPointF& scene_pos) const;
210  LayouterPoint closestLayouterPoint(qreal scene_pos, int default_spacing, int min_index, QVector<qreal> sections) const;
211 
212  #ifdef GUI_DEBUG_GRID
213  void debugShowLayouterGridpos(const QPoint& mouse_pos);
214  void debugDrawLayouterGridpos(QPainter* painter);
215  QPoint m_debug_gridpos = QPoint(0,0);
216  bool mDebugGridposEnable = true;
217  #endif
218 
219 
220 
221  GraphicsItem* mItem;
222 
223  bool mMinimapEnabled;
224 
225  bool mGridEnabled;
226  bool mGridClustersEnabled;
227  GraphicsScene::GridType mGridType;
228 
229  DragController* mDragController;
230  Qt::KeyboardModifier mDragModifier;
231 
232  QPoint mMovePosition;
233  Qt::KeyboardModifier mPanModifier;
234 
235  Qt::KeyboardModifier mZoomModifier;
236  qreal mZoomFactorBase;
237  QPointF mTargetScenePos;
238  QPointF mTargetViewportPos;
239 
240  qreal mMinScale;
241 
242  template <typename Func1>
243  void recursionLevelMenu(QMenu* menu, bool succ, Func1 slot, bool addUnlimited=false)
244  {
245  QString s = succ ? "successors" : "predecessors";
246  const char* txt[] = {"All %1", "Depth 1 (direct %1)", "Depth 2",
247  "Depth 3", "Depth 4", "Depth 5", nullptr};
248  for (int inx = 1; txt[inx]; inx++)
249  {
250  QAction* act = menu->addAction(inx==1 ? QString(txt[1]).arg(s) : QString(txt[inx]));
251  act->setData(inx);
252  connect(act, &QAction::triggered, this, slot);
253  }
254  if (addUnlimited)
255  {
256  QAction* act = menu->addAction(QString(txt[0]).arg(s));
257  act->setData(0);
258  connect(act, &QAction::triggered, this, slot);
259  }
260  }
261 
262  static const QString sAssignToGrouping;
263  };
264 }
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, u32 idTo)
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
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