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