HAL  v4.5.0-283-g3f42a39fa
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
clock_tree_graphics_view.cpp
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 // Copyright (c) 2025-2026 Sascha Tommasone. All rights reserved.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in all
17 // copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 // SOFTWARE.
26 
28 
30 
31 #include <QMouseEvent>
32 #include <QPainter>
33 #include <QPointF>
34 #include <algorithm>
35 #include <cmath>
36 
37 namespace hal
38 {
39  namespace
40  {
41  // The factor the zoom changes by with one notch of the mouse wheel or one key press.
42  constexpr double ZOOM_STEP = 1.25;
43 
44  // The tree may be zoomed out to half of the scale at which it fills the view ...
45  constexpr double MIN_ZOOM_OF_FIT = 0.5;
46 
47  // ... and in until a single gate is this many pixels across.
48  constexpr double MAX_NODE_DIAMETER_PIXELS = 400.0;
49 
50  // Around an area that is shown there is at least this many gate diameters of air.
51  constexpr double AREA_AIR_NODE_DIAMETERS = 10.0;
52  } // namespace
53 
55  : QGraphicsView( scene, parent )
56  , mScene( scene )
57  , mFitPending( false )
58  {
63 
64  // Without it, the view only hears about the mouse while a button is down.
65  setMouseTracking( true );
66  }
67 
69  {
70  if( !isVisible() || viewport()->width() < 2 || viewport()->height() < 2 )
71  {
72  mFitPending = true;
73  return;
74  }
75 
76  mFitPending = false;
77  fitRect( sceneRect() );
78  }
79 
80  void ClockTreeGraphicsView::fitRect( const QRectF &rect )
81  {
82  // fitInView measures the viewport with the scroll bars that happen to be showing, and they are gone once
83  // everything fits, which leaves a gap around the tree. So fit without them.
86 
89 
91 
92  setHorizontalScrollBarPolicy( horizontal );
93  setVerticalScrollBarPolicy( vertical );
94  }
95 
97  {
98  if( !mScene->hasLayout() )
99  {
100  return;
101  }
102 
103  // Some air around the gates, so they are not at the edge of the view: a tenth of the area, but at least a few
104  // gates wide, which is all there is around a single gate.
105  const double air = std::max( 0.1 * std::max( area.width(), area.height() ),
106  AREA_AIR_NODE_DIAMETERS * 2.0 * mScene->style().node_radius );
107 
108  fitRect( area.adjusted( -air, -air, air, air ) );
109 
110  // Fitting a few gates zooms in further than is of use, bring the zoom back within its limits.
111  zoomBy( 1.0 );
112  centerOn( area.center() );
113  }
114 
115  double ClockTreeGraphicsView::fitScale() const
116  {
117  const QRectF rect = sceneRect();
118  if( rect.isEmpty() )
119  {
120  return 1.0;
121  }
122 
123  return std::min( viewport()->width() / rect.width(), viewport()->height() / rect.height() );
124  }
125 
126  void ClockTreeGraphicsView::zoomBy( double factor )
127  {
128  if( !mScene->hasLayout() )
129  {
130  return;
131  }
132 
133  const double current = transform().m11();
134  const double fit = fitScale();
135 
136  const double minimum = MIN_ZOOM_OF_FIT * fit;
137  const double maximum = std::max( 2.0 * fit, MAX_NODE_DIAMETER_PIXELS / ( 2.0 * mScene->style().node_radius ) );
138 
139  const double target = std::clamp( current * factor, minimum, maximum );
140  if( target == current )
141  {
142  return;
143  }
144 
145  scale( target / current, target / current );
146  }
147 
149  {
150  // 120 is one notch of a mouse wheel. A trackpad reports many small steps, which zoom correspondingly little.
151  const double notches = event->angleDelta().y() / 120.0;
152  if( notches == 0.0 )
153  {
154  event->ignore();
155  return;
156  }
157 
158  zoomBy( std::pow( ZOOM_STEP, notches ) );
159  event->accept();
160  }
161 
163  {
164  const QPointF chip = ClockTreeScene::toChip( mapToScene( event->position().toPoint() ) );
165  Q_EMIT coordinatesChanged( chip.x(), chip.y() );
166 
168  }
169 
171  {
172  if( event->key() == Qt::Key_Plus )
173  {
174  zoomBy( ZOOM_STEP );
175  }
176  else if( event->key() == Qt::Key_Minus )
177  {
178  zoomBy( 1.0 / ZOOM_STEP );
179  }
180  else
181  {
183  }
184  }
185 
187  {
189 
190  if( mFitPending )
191  {
192  fitToScene();
193  }
194  }
195 
197  {
199 
200  // The size a view has when it is first shown is not necessarily its final one.
201  if( mFitPending )
202  {
203  fitToScene();
204  }
205  }
206 } // namespace hal
void showArea(const QRectF &area)
ClockTreeGraphicsView(ClockTreeScene *scene, QWidget *parent=nullptr)
void coordinatesChanged(double x, double y)
void mouseMoveEvent(QMouseEvent *event) override
void keyPressEvent(QKeyEvent *event) override
void resizeEvent(QResizeEvent *event) override
void showEvent(QShowEvent *event) override
void wheelEvent(QWheelEvent *event) override
const ctv::Style & style() const
static QPointF toChip(const QPointF &scenePosition)
Definition: defines.h:45
QWidget * viewport() const const
void centerOn(const QPointF &pos)
void setDragMode(QGraphicsView::DragMode mode)
virtual bool event(QEvent *event) override
void fitInView(const QRectF &rect, Qt::AspectRatioMode aspectRatioMode)
virtual void keyPressEvent(QKeyEvent *event) override
QPointF mapToScene(const QPoint &point) const const
virtual void mouseMoveEvent(QMouseEvent *event) override
virtual void resizeEvent(QResizeEvent *event) override
void scale(qreal sx, qreal sy)
void setRenderHint(QPainter::RenderHint hint, bool enabled)
virtual void showEvent(QShowEvent *event) override
QTransform transform() const const
void setTransformationAnchor(QGraphicsView::ViewportAnchor anchor)
void setViewportUpdateMode(QGraphicsView::ViewportUpdateMode mode)
Q_EMITQ_EMIT
qreal x() const const
qreal y() const const
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
QPointF center() const const
qreal height() const const
qreal width() const const
KeepAspectRatio
Key_Plus
ScrollBarPolicy
qreal m11() const const
void setMouseTracking(bool enable)
bool isVisible() const const