HAL  v4.5.0-283-g3f42a39fa
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
clock_tree_viewer.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 
36 #include "gui/gui_def.h"
37 #include "gui/gui_globals.h"
38 #include "gui/gui_utils/graphics.h"
42 #include "gui/toolbar/toolbar.h"
43 #include "hal_core/netlist/gate.h"
46 #include "hal_core/utilities/log.h"
47 
48 #include <QAction>
49 #include <QColor>
50 #include <QCoreApplication>
51 #include <QFileDialog>
52 #include <QFileInfo>
53 #include <QGuiApplication>
54 #include <QLabel>
55 #include <QLocale>
56 #include <QMessageBox>
57 #include <QMetaObject>
58 #include <QPalette>
59 #include <QProgressDialog>
60 #include <QRegularExpression>
61 #include <QStackedWidget>
62 #include <QStatusBar>
63 #include <QStringList>
64 #include <QStyle>
65 #include <QThread>
66 #include <QTimer>
67 #include <QVBoxLayout>
68 #include <algorithm>
69 #include <cmath>
70 #include <exception>
71 #include <memory>
72 #include <utility>
73 
74 namespace hal
75 {
76  namespace
77  {
78  // How many of the gates that cannot be drawn are named in the log.
79  constexpr std::size_t MAX_LISTED_GATES = 20;
80 
81  // The color of a selected item in the graph view. It is GraphicsItem::sSelectionColor, which is protected, so this
82  // is the one place it is repeated: everything in the viewer that shows a highlight takes it from here.
83  constexpr QColor HAL_SELECTION_COLOR( 240, 173, 0 );
84 
85  // For a theme that does not define the colors of warnings and errors in the log.
86  constexpr QColor FALLBACK_WARNING_COLOR( 217, 164, 65 );
87  constexpr QColor FALLBACK_ERROR_COLOR( 217, 83, 79 );
88 
89  QString text_color_style( const QColor &color, const QColor &fallback )
90  {
91  return QString( "color: %1;" ).arg( ( color.isValid() ? color : fallback ).name() );
92  }
93 
98  Result<ctv::Layout> layout_of( const cte::ClockTree &tree,
99  const Netlist *open_netlist,
100  const std::optional<std::string> &x_key,
101  const std::optional<std::string> &y_key )
102  {
103  if( open_netlist != nullptr && tree.get_netlist() != open_netlist )
104  {
105  return ERR( "the clock tree was extracted from another netlist than the one that is open, so its gates "
106  "cannot be matched to the gates in HAL" );
107  }
108 
109  return ctv::build_layout( tree, x_key, y_key );
110  }
111 
115  Result<ctv::Layout> extract_layout( const Netlist *netlist,
116  const std::optional<std::string> &x_key,
117  const std::optional<std::string> &y_key )
118  {
119  // An exception escaping the thread would end the program.
120  try
121  {
122  const Result<std::unique_ptr<cte::ClockTree>> tree = cte::ClockTree::from_netlist( netlist );
123  if( tree.is_error() )
124  {
125  return ERR( tree.get_error().get() );
126  }
127 
128  return layout_of( *tree.get(), netlist, x_key, y_key );
129  } catch( const std::exception &e )
130  {
131  return ERR( std::string( "the extraction failed: " ) + e.what() );
132  }
133  }
134 
135  constexpr auto IDLE_TEXT = "No clock tree to show.\n\n"
136  "Extract the clock tree of the loaded netlist with the button in the toolbar,\n"
137  "or pass a clock tree to clock_tree_viewer.show_clock_tree() in Python.";
138 
139  QString list_gate_ids( const std::vector<u32> &ids )
140  {
141  QStringList listed;
142  for( std::size_t idx = 0; idx < std::min( ids.size(), MAX_LISTED_GATES ); idx++ )
143  {
144  listed.push_back( QString::number( ids[idx] ) );
145  }
146 
147  QString result = listed.join( ", " );
148  if( ids.size() > MAX_LISTED_GATES )
149  {
150  result += QString( " and %1 more" ).arg( ids.size() - MAX_LISTED_GATES );
151  }
152  return result;
153  }
154 
155  // Runs the work in the GUI thread and waits for it, so a script thread can hand work to a viewer. The viewer must
156  // be looked up inside the work: the GUI may delete it at any time between two of its events.
157  template<typename Work>
158  void run_in_gui_thread( Work &&work )
159  {
161  if( QThread::currentThread() == application->thread() )
162  {
163  work();
164  return;
165  }
166  QMetaObject::invokeMethod( application, std::forward<Work>( work ), Qt::BlockingQueuedConnection );
167  }
168 
169  // The viewer that is open, logged as an error if there is none. Only valid in the GUI thread, see
170  // run_in_gui_thread.
171  ClockTreeViewer *open_viewer()
172  {
173  ClockTreeViewer *viewer = ClockTreeViewer::instance();
174  if( viewer == nullptr )
175  {
176  log_error( "clock_tree_viewer", "Cannot find clock tree viewer instance." );
177  }
178  return viewer;
179  }
180  } // namespace
181 
183  : ExternalContentWidget( sPluginName, "ClockTreeViewer", parent )
184  {
185  mExtractAction = new QAction( this );
186  mExtractAction->setToolTip( "Extract the clock tree of the netlist and plot it" );
187  mExtractAction->setIcon( gui_utility::getStyledSvgIcon( "all->#20FF80", ":/icons/run" ) );
188  connect( mExtractAction, &QAction::triggered, this, &ClockTreeViewer::handleExtractClicked );
189 
190  mFitAction = new QAction( this );
191  mFitAction->setToolTip( "Zoom to the whole clock tree" );
192  mFitAction->setIcon( QIcon( ":/icons/zoom_waveform" ) );
193  connect( mFitAction, &QAction::triggered, this, &ClockTreeViewer::handleFitClicked );
194 
195  mZoomToHighlightAction = new QAction( this );
196  mZoomToHighlightAction->setToolTip( "Zoom to the highlighted gates" );
197  mZoomToHighlightAction->setIcon( gui_utility::getStyledSvgIcon(
198  QString( "all->%1" ).arg( QColor( HAL_SELECTION_COLOR ).name() ), ":/icons/to_selection" ) );
199  connect( mZoomToHighlightAction, &QAction::triggered, this, &ClockTreeViewer::handleZoomToHighlightClicked );
200 
201  mSavePictureAction = new QAction( this );
202  mSavePictureAction->setToolTip( "Save a picture of the clock tree" );
203  mSavePictureAction->setIcon( gui_utility::getStyledSvgIcon( "all->#3192C5", ":/icons/save" ) );
204  connect( mSavePictureAction, &QAction::triggered, this, &ClockTreeViewer::handleSavePictureClicked );
205 
206  mScene = new ClockTreeScene( this );
207  mView = new ClockTreeGraphicsView( mScene, this );
208  connect( mView, &ClockTreeGraphicsView::coordinatesChanged, this, &ClockTreeViewer::handleCoordinatesChanged );
209 
210  mMessageLabel = new QLabel( IDLE_TEXT, this );
211  mMessageLabel->setAlignment( Qt::AlignCenter );
212  mMessageLabel->setWordWrap( true );
213  mMessageLabel->setMargin( 20 );
214 
215  // The message takes the place of the drawing, so an error cannot be overlooked behind an old plot.
216  mStack = new QStackedWidget( this );
217  mStack->addWidget( mMessageLabel );
218  mStack->addWidget( mView );
219  mContentLayout->addWidget( mStack, 1 );
220 
221  mCoordinateLabel = new QLabel( " x: 0, y: 0", this );
222  mWarningLabel = new QLabel( this );
223  mWarningLabel->hide();
224 
225  mHighlightLabel = new QLabel( this );
226  mHighlightLabel->hide();
227 
228  applyThemeColors();
229 
230  mStatusBar = new QStatusBar( this );
231  mStatusBar->addWidget( mCoordinateLabel );
232  mStatusBar->addWidget( mWarningLabel, 1 );
233  mStatusBar->addPermanentWidget( mHighlightLabel );
234  mContentLayout->addWidget( mStatusBar );
235 
236  // Many things change the color of a gate at once, e.g., a selection of hundreds of gates, but the plot is only
237  // painted once for all of them.
238  mRecolorTimer = new QTimer( this );
239  mRecolorTimer->setSingleShot( true );
240  mRecolorTimer->setInterval( 0 );
241  connect( mRecolorTimer, &QTimer::timeout, this, &ClockTreeViewer::recolor );
242 
243  // Everything that changes the colors of the plot in HAL's graph view.
244  if( MainWindow::sSettingStyle != nullptr )
245  {
248  this,
249  &ClockTreeViewer::handleStyleChanged );
250  }
251  if( gNetlistRelay != nullptr )
252  {
255  this,
256  &ClockTreeViewer::scheduleRecolor );
257  connect( gNetlistRelay, &NetlistRelay::moduleGateAssigned, this, &ClockTreeViewer::scheduleRecolor );
258  connect( gNetlistRelay, &NetlistRelay::moduleGateRemoved, this, &ClockTreeViewer::scheduleRecolor );
259  connect( gNetlistRelay, &NetlistRelay::groupingColorChanged, this, &ClockTreeViewer::scheduleRecolor );
260  connect( gNetlistRelay, &NetlistRelay::groupingGateAssigned, this, &ClockTreeViewer::scheduleRecolor );
261  connect( gNetlistRelay, &NetlistRelay::groupingGateRemoved, this, &ClockTreeViewer::scheduleRecolor );
262  connect( gNetlistRelay, &NetlistRelay::groupingRemoved, this, &ClockTreeViewer::scheduleRecolor );
263  }
264  if( gSelectionRelay != nullptr )
265  {
266  connect(
267  gSelectionRelay, &SelectionRelay::selectionChanged, this, &ClockTreeViewer::handleSelectionChanged );
268  }
269 
270  // Emitted while the netlist still exists, so a running extraction can be waited for before it is deleted.
273  this,
274  &ClockTreeViewer::handleFileAboutToClose );
275 
276  updateActions();
277  }
278 
280  {
281  if( mExtraction != nullptr )
282  {
283  // The thread deletes itself once it has finished, see handleExtractClicked.
284  mExtraction->wait();
286  }
287  }
288 
290  {
291  return dynamic_cast<ClockTreeViewer *>(
292  ExternalContent::instance()->openWidgets.value( sPluginName, nullptr ) );
293  }
294 
296  {
297  toolbar->addAction( mExtractAction );
298  toolbar->addAction( mFitAction );
299  toolbar->addAction( mZoomToHighlightAction );
300  toolbar->addAction( mSavePictureAction );
301  }
302 
304  const ctv::StyleOverrides &overrides,
305  const std::optional<std::string> &x_key,
306  const std::optional<std::string> &y_key )
307  {
308  // Laying out only reads the clock tree and its gates and touches no widget, so it happens right here. Only
309  // showing the result has to happen in the GUI thread.
310  const Result<ctv::Layout> layout = layout_of( tree, gNetlist, x_key, y_key );
311 
312  bool shown = false;
313  run_in_gui_thread( [&]() {
314  ClockTreeViewer *viewer = open_viewer();
315  if( viewer != nullptr )
316  {
317  shown = viewer->display( layout, overrides, x_key, y_key );
318  }
319  } );
320  return shown;
321  }
322 
323  bool ClockTreeViewer::display( const Result<ctv::Layout> &layout,
324  const ctv::StyleOverrides &overrides,
325  const std::optional<std::string> &x_key,
326  const std::optional<std::string> &y_key )
327  {
328  if( layout.is_error() )
329  {
330  return showError( QString::fromStdString( layout.get_error().get() ) );
331  }
332 
333  const Result<ctv::Style> style = ctv::resolve_style( layout.get(), overrides );
334  if( style.is_error() )
335  {
336  return showError( QString::fromStdString( style.get_error().get() ) );
337  }
338 
339  mOverrides = overrides;
340  mXKey = x_key;
341  mYKey = y_key;
342  mLayout = layout.get();
343 
344  mNodeOfGate.clear();
345  for( std::size_t idx = 0; idx < mLayout.nodes.size(); idx++ )
346  {
347  mNodeOfGate.insert( mLayout.nodes[idx].gate_id, static_cast<int>( idx ) );
348  }
349 
350  // Whatever is selected in HAL when the plot appears is highlighted right away. The highlight is part of the colors,
351  // so they are only worked out after it.
352  highlightFromSelection();
353 
354  mScene->setLayout( mLayout, style.get(), currentColors() );
355  mScene->setHighlight( highlightedNodes() );
356  mShowsError = false;
357  mStack->setCurrentWidget( mView );
358  mView->fitToScene();
359  updateActions();
360 
361  log_info(
362  "clock_tree_viewer", "Showing {} gates and {} connections.", mLayout.nodes.size(), mLayout.edges.size() );
363 
364  if( mLayout.gates_without_coordinates.empty() )
365  {
366  showWarning( QString() );
367  }
368  else
369  {
370  const std::size_t total = mLayout.nodes.size() + mLayout.gates_without_coordinates.size();
371 
372  log_warning(
373  "clock_tree_viewer",
374  "{} of {} gates have no coordinates and are not shown, {} connections are left out. Gate IDs: {}",
375  mLayout.gates_without_coordinates.size(),
376  total,
377  mLayout.omitted_edges,
378  list_gate_ids( mLayout.gates_without_coordinates ).toStdString() );
379 
380  showWarning( QString( "%1 of %2 gates have no coordinates and are not shown (%3 connections left out)" )
381  .arg( mLayout.gates_without_coordinates.size() )
382  .arg( total )
383  .arg( mLayout.omitted_edges ) );
384  }
385 
386  return true;
387  }
388 
389  bool ClockTreeViewer::showError( const QString &message )
390  {
391  log_error( "clock_tree_viewer", "{}", message.toStdString() );
392 
393  // The errors of a Result start with the source location they come from, which says nothing to the user.
394  static const QRegularExpression source_location( R"(\[[^\]\n]*:\d+\]\s*)" );
395 
396  QString text = message;
397  text.remove( source_location );
398 
399  mScene->clearLayout();
400  mLayout = ctv::Layout();
401  mNodeOfGate.clear();
402  mHighlightedGates.clear();
403  mShowsError = true;
404  applyThemeColors();
405  mMessageLabel->setText( "The clock tree cannot be shown:\n\n" + text.trimmed() );
406  mStack->setCurrentWidget( mMessageLabel );
407  showWarning( QString() );
408  updateActions();
409 
410  return false;
411  }
412 
413  void ClockTreeViewer::showWarning( const QString &message ) const
414  {
415  mWarningLabel->setText( message );
416  mWarningLabel->setVisible( !message.isEmpty() );
417  }
418 
419  void ClockTreeViewer::applyThemeColors() const
420  {
421  const LoggerQssAdapter *theme = LoggerQssAdapter::instance();
422 
423  mWarningLabel->setStyleSheet( text_color_style( theme->warningColor(), FALLBACK_WARNING_COLOR ) );
424  mHighlightLabel->setStyleSheet( text_color_style( HAL_SELECTION_COLOR, HAL_SELECTION_COLOR ) );
425  mMessageLabel->setStyleSheet( mShowsError ? text_color_style( theme->errorColor(), FALLBACK_ERROR_COLOR )
426  : QString() );
427  }
428 
429  QVector<int> ClockTreeViewer::highlightedNodes() const
430  {
431  QVector<int> nodes;
432  nodes.reserve( mHighlightedGates.size() );
433  for( const u32 gateId : mHighlightedGates )
434  {
435  nodes.push_back( mNodeOfGate.value( gateId, -1 ) );
436  }
437  return nodes;
438  }
439 
440  void ClockTreeViewer::updateActions() const
441  {
442  mFitAction->setEnabled( mScene->hasLayout() );
443  mSavePictureAction->setEnabled( mScene->hasLayout() );
444  mZoomToHighlightAction->setEnabled( !mScene->highlightedNodes().isEmpty() );
445 
446  const int highlighted = mScene->highlightedNodes().size();
447  mHighlightLabel->setText( QString( "%1 highlighted " ).arg( highlighted ) );
448  mHighlightLabel->setVisible( highlighted > 0 );
449  }
450 
451  QColor ClockTreeViewer::gateColor( u32 gateId, const QColor &fallback ) const
452  {
453  // The same order of precedence as the items of the graph view have, see GraphicsItem::penColor.
454  if( mHighlightedGates.contains( gateId ) )
455  {
456  return HAL_SELECTION_COLOR;
457  }
458 
459  if( gNetlist == nullptr )
460  {
461  return fallback;
462  }
463 
464  const Gate *gate = gNetlist->get_gate_by_id( gateId );
465  if( gate == nullptr )
466  {
467  return fallback;
468  }
469 
470  if( gContentManager != nullptr && gContentManager->getGroupingManagerWidget() != nullptr )
471  {
472  const QColor grouping =
474  if( grouping.isValid() )
475  {
476  return grouping;
477  }
478  }
479 
480  if( const Module *module = gate->get_module(); module != nullptr && gNetlistRelay != nullptr )
481  {
483  }
484 
485  return fallback;
486  }
487 
488  ClockTreeColors ClockTreeViewer::currentColors() const
489  {
490  ClockTreeColors colors;
491 
492  // The color HAL draws its nets in, which depends on the theme.
493  colors.edge = GraphicsQssAdapter::instance()->netBaseColor();
494  colors.highlight = HAL_SELECTION_COLOR;
495 
496  colors.nodes.reserve( static_cast<int>( mLayout.nodes.size() ) );
497  for( const ctv::Node &node : mLayout.nodes )
498  {
499  colors.nodes.push_back( gateColor( node.gate_id, colors.edge ) );
500  }
501 
502  return colors;
503  }
504 
505  QColor ClockTreeViewer::backgroundColor() const
506  {
507  // Nothing in the scene paints a background, the view shows the one the theme gives every widget.
508  return mView->viewport()->palette().color( mView->viewport()->backgroundRole() );
509  }
510 
511  void ClockTreeViewer::handleStyleChanged()
512  {
513  // The theme is a stylesheet that has been applied to the application by now, the adapters read their colors from
514  // it once they are polished again.
516 
517  LoggerQssAdapter *logTheme = LoggerQssAdapter::instance();
518  logTheme->style()->unpolish( logTheme );
519  logTheme->style()->polish( logTheme );
520 
521  applyThemeColors();
522  scheduleRecolor();
523  }
524 
525  void ClockTreeViewer::scheduleRecolor()
526  {
527  if( mScene->hasLayout() )
528  {
529  mRecolorTimer->start();
530  }
531  }
532 
533  void ClockTreeViewer::recolor()
534  {
535  mScene->setColors( currentColors() );
536  mScene->setHighlight( highlightedNodes() );
537 
538  updateActions();
539  }
540 
541  void ClockTreeViewer::highlightFromSelection()
542  {
543  mHighlightedGates.clear();
544 
545  if( gSelectionRelay == nullptr )
546  {
547  return;
548  }
549 
550  // Only gates, and only those in the plot: whatever else is selected is none of the viewer's business.
551  for( const u32 gateId : gSelectionRelay->selectedGatesList() )
552  {
553  if( mNodeOfGate.contains( gateId ) )
554  {
555  mHighlightedGates.insert( gateId );
556  }
557  }
558  }
559 
560  void ClockTreeViewer::handleSelectionChanged()
561  {
562  if( !mScene->hasLayout() )
563  {
564  return;
565  }
566 
567  highlightFromSelection();
568  scheduleRecolor();
569  }
570 
571  std::size_t ClockTreeViewer::applyHighlight( const std::vector<u32> &gateIds )
572  {
573  mHighlightedGates.clear();
574 
575  for( const u32 gateId : gateIds )
576  {
577  if( mNodeOfGate.contains( gateId ) )
578  {
579  mHighlightedGates.insert( gateId );
580  }
581  }
582 
583  // A script waits for the call, so what it asked for is painted when the call returns.
584  recolor();
585 
586  return static_cast<std::size_t>( mHighlightedGates.size() );
587  }
588 
589  std::size_t ClockTreeViewer::highlightGates( const std::vector<u32> &gateIds )
590  {
591  std::size_t highlighted = 0;
592  bool found = false;
593 
594  run_in_gui_thread( [&]() {
595  ClockTreeViewer *viewer = open_viewer();
596  if( viewer != nullptr )
597  {
598  found = true;
599  highlighted = viewer->applyHighlight( gateIds );
600  }
601  } );
602 
603  if( found && !gateIds.empty() )
604  {
605  log_info( "clock_tree_viewer",
606  "Highlighted {} of {} gates, the others are not in the plot.",
607  highlighted,
608  gateIds.size() );
609  }
610 
611  return highlighted;
612  }
613 
614  void ClockTreeViewer::handleZoomToHighlightClicked()
615  {
616  if( mScene->highlightedNodes().isEmpty() )
617  {
618  return;
619  }
620 
621  mView->showArea( mScene->highlightedRect() );
622  }
623 
624  bool ClockTreeViewer::savePicture( const QString &filename )
625  {
626  const Result<QImage> image = mScene->renderPicture( backgroundColor() );
627  if( image.is_error() )
628  {
629  log_error( "clock_tree_viewer", "Cannot save a picture: {}", image.get_error().get() );
630  return false;
631  }
632 
633  if( !image.get().save( filename ) )
634  {
635  log_error( "clock_tree_viewer", "Could not save the picture to '{}'.", filename.toStdString() );
636  return false;
637  }
638 
639  log_info( "clock_tree_viewer", "Saved a picture of the clock tree to '{}'.", filename.toStdString() );
640  return true;
641  }
642 
643  void ClockTreeViewer::handleExtractClicked()
644  {
645  if( gNetlist == nullptr )
646  {
647  showError( "No netlist is loaded." );
648  return;
649  }
650 
651  if( mExtraction != nullptr )
652  {
653  return;
654  }
655 
656  // Everything the thread needs is copied now, it must not read the members of the viewer.
657  const Netlist *netlist = gNetlist;
658  const ctv::StyleOverrides overrides = mOverrides;
659  const std::optional<std::string> x_key = mXKey;
660  const std::optional<std::string> y_key = mYKey;
661 
662  // Result has no empty state, so the thread fills an optional that both sides share.
663  const auto result = std::make_shared<std::optional<Result<ctv::Layout>>>();
664 
665  mExtraction = QThread::create(
666  [netlist, x_key, y_key, result]() { result->emplace( extract_layout( netlist, x_key, y_key ) ); } );
667 
668  // The extraction reads the netlist while it runs, so the dialog is modal and shown right away: a modal dialog only
669  // blocks input once it is visible, and until the extraction is done nothing in the GUI may change the netlist.
670  // The extraction cannot be interrupted, so there is no cancel button.
671  auto *progress = new QProgressDialog( "Extracting the clock tree ...", QString(), 0, 0, this );
672  progress->setWindowTitle( "Clock tree viewer" );
673  progress->setWindowModality( Qt::ApplicationModal );
674  progress->setCancelButton( nullptr );
675  progress->show();
676 
677  // Connected first, so it runs before the thread deletes itself.
678  connect( mExtraction, &QThread::finished, this, [this, result, overrides, x_key, y_key, progress]() {
679  delete progress;
681  mExtraction = nullptr;
682  mExtractAction->setEnabled( true );
683 
684  if( result->has_value() )
685  {
686  display( **result, overrides, x_key, y_key );
687  }
688  else
689  {
690  showError( "The extraction ended without a result." );
691  }
692  } );
693  connect( mExtraction, &QThread::finished, mExtraction, &QObject::deleteLater );
694 
696  mExtractAction->setEnabled( false );
697  mExtraction->start();
698  }
699 
700  void ClockTreeViewer::handleFileAboutToClose()
701  {
702  if( mExtraction != nullptr )
703  {
704  mExtraction->wait();
705  }
706  }
707 
708  void ClockTreeViewer::handleFitClicked() const
709  {
710  mView->fitToScene();
711  }
712 
713  void ClockTreeViewer::handleSavePictureClicked()
714  {
716  this, "Save a picture of the clock tree", "clock_tree.png", "Images (*.png *.jpg *.bmp)" );
717  if( filename.isEmpty() )
718  {
719  return;
720  }
721 
722  // The format is taken from the suffix, so a file without one would not be written.
723  if( QFileInfo( filename ).suffix().isEmpty() )
724  {
725  filename += ".png";
726  }
727 
728  if( !savePicture( filename ) )
729  {
731  this, "Clock tree picture", "The picture could not be saved, see the log for details." );
732  }
733  }
734 
735  void ClockTreeViewer::handleCoordinatesChanged( double x, double y )
736  {
737  // As many decimals as a pixel resolves at the current zoom: none while a pixel covers a unit or more, one more
738  // with every tenfold zoom beyond that.
739  const double pixelsPerUnit = mView->transform().m11();
740  const int decimals =
741  pixelsPerUnit > 1.0 ? std::min( 6, static_cast<int>( std::ceil( std::log10( pixelsPerUnit ) ) ) ) : 0;
742 
743  const QLocale english( QLocale::English );
744  mCoordinateLabel->setText(
745  QString( " x: %1, y: %2" )
746  .arg( english.toString( x, 'f', decimals ), english.toString( y, 'f', decimals ) ) );
747  }
748 } // namespace hal
void showArea(const QRectF &area)
void coordinatesChanged(double x, double y)
Result< QImage > renderPicture(const QColor &background)
const QVector< int > & highlightedNodes() const
void setHighlight(const QVector< int > &nodes)
void setLayout(const ctv::Layout &layout, const ctv::Style &style, const ClockTreeColors &colors)
QRectF highlightedRect() const
void setColors(const ClockTreeColors &colors)
ClockTreeViewer(QWidget *parent=nullptr)
static std::size_t highlightGates(const std::vector< u32 > &gateIds)
static constexpr auto sPluginName
bool savePicture(const QString &filename)
static ClockTreeViewer * instance()
void setupToolbar(Toolbar *toolbar) override
static bool setClockTree(const cte::ClockTree &tree, const ctv::StyleOverrides &overrides=ctv::StyleOverrides(), const std::optional< std::string > &x_key=ctv::DEFAULT_X_KEY, const std::optional< std::string > &y_key=ctv::DEFAULT_Y_KEY)
GroupingManagerWidget * getGroupingManagerWidget()
QString name() const
QVBoxLayout * mContentLayout
const std::string & get() const
Definition: error.h:87
static ExternalContent * instance()
QMap< QString, ExternalContentWidget * > openWidgets
static FileManager * get_instance()
void fileAboutToClose(const QString &fileName)
static GraphicsQssAdapter * instance()
GroupingTableModel * getModel() const
QColor colorForItem(ItemType itemType, u32 itemId) const
static LoggerQssAdapter * instance()
static SettingsItemDropdown * sSettingStyle
Definition: main_window.h:302
void moduleColorChanged(u32 id) const
u32 get_id() const
Definition: module.cpp:83
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:195
void groupingColorChanged(Grouping *grp) const
ModuleColorManager * getModuleColorManager() const
void groupingGateAssigned(Grouping *grp, u32 id) const
void moduleGateRemoved(Module *m, const u32 removed_gate) const
void moduleGateAssigned(Module *m, const u32 assigned_gate) const
void groupingRemoved(Grouping *grp) const
void groupingGateRemoved(Grouping *grp, u32 id) const
QColor getModuleColor(const u32 id)
const T & get() const
Definition: result.h:166
const Error & get_error() const
Definition: result.h:190
bool is_error() const
Definition: result.h:153
void selectionChanged(void *sender)
QList< u32 > selectedGatesList() const
void intChanged(int value)
Toolbar for all ContentFrames and ContentWidgets.
Definition: toolbar.h:39
void shown()
static Result< std::unique_ptr< ClockTree > > from_netlist(const Netlist *netlist)
Definition: clock_tree.cpp:158
uint32_t u32
Definition: defines.h:41
#define log_error(channel,...)
Definition: log.h:78
#define log_info(channel,...)
Definition: log.h:70
#define log_warning(channel,...)
Definition: log.h:76
#define ERR(message)
Definition: result.h:60
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< Layout > build_layout(const cte::ClockTree &tree, const std::optional< std::string > &x_key=std::nullopt, const std::optional< std::string > &y_key=std::nullopt)
Result< Style > resolve_style(const Layout &layout, const StyleOverrides &overrides)
QIcon getStyledSvgIcon(const QString &from_to_colors_enabled, const QString &svg_path, QString from_to_colors_disabled=QString())
Definition: graphics.cpp:60
Definition: defines.h:45
ContentManager * gContentManager
Definition: plugin_gui.cpp:79
SelectionRelay * gSelectionRelay
Definition: plugin_gui.cpp:84
Netlist * gNetlist
Definition: gui_globals.h:69
NetlistRelay * gNetlistRelay
Definition: gui_globals.h:74
QWidget * viewport() const const
void setEnabled(bool)
void setIcon(const QIcon &icon)
void setToolTip(const QString &tip)
void triggered(bool checked)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
bool isValid() const const
QCoreApplication * instance()
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
QTransform transform() const const
void restoreOverrideCursor()
void setOverrideCursor(const QCursor &cursor)
void clear()
bool contains(const Key &key) const const
QHash::iterator insert(const Key &key, const T &value)
const T value(const Key &key) const const
void setAlignment(Qt::Alignment)
void setMargin(int)
void setText(const QString &)
void setWordWrap(bool on)
void push_back(const T &value)
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void deleteLater()
QThread * thread() const const
void clear()
int size() const const
int addWidget(QWidget *widget)
void setCurrentWidget(QWidget *widget)
void addPermanentWidget(QWidget *widget, int stretch)
void addWidget(QWidget *widget, int stretch)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
QString number(int n, int base)
QString & remove(int position, int n)
std::string toStdString() const const
QString trimmed() const const
QString join(const QString &separator) const const
AlignCenter
BlockingQueuedConnection
BusyCursor
ApplicationModal
QThread * create(Function &&f, Args &&... args)
QThread * currentThread()
void finished()
void setInterval(int msec)
void setSingleShot(bool singleShot)
void start(int msec)
void timeout()
QAction * addAction(const QString &text)
qreal m11() const const
bool isEmpty() const const
void push_back(const T &value)
void reserve(int size)
int size() const const
QPalette::ColorRole backgroundRole() const const
void hide()
QLayout * layout() const const
QStyle * style() const const
void setStyleSheet(const QString &styleSheet)
virtual void setVisible(bool visible)
std::vector< u32 > gates_without_coordinates
std::size_t omitted_edges
std::vector< Node > nodes
std::vector< Edge > edges