50 #include <QCoreApplication>
51 #include <QFileDialog>
53 #include <QGuiApplication>
56 #include <QMessageBox>
57 #include <QMetaObject>
59 #include <QProgressDialog>
60 #include <QRegularExpression>
61 #include <QStackedWidget>
63 #include <QStringList>
67 #include <QVBoxLayout>
79 constexpr std::size_t MAX_LISTED_GATES = 20;
83 constexpr
QColor HAL_SELECTION_COLOR( 240, 173, 0 );
86 constexpr
QColor FALLBACK_WARNING_COLOR( 217, 164, 65 );
87 constexpr
QColor FALLBACK_ERROR_COLOR( 217, 83, 79 );
91 return QString(
"color: %1;" ).
arg( ( color.
isValid() ? color : fallback ).name() );
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 )
103 if( open_netlist !=
nullptr && tree.get_netlist() != open_netlist )
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" );
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 )
123 if( tree.is_error() )
125 return ERR( tree.get_error().get() );
128 return layout_of( *tree.get(), netlist, x_key, y_key );
129 }
catch(
const std::exception &e )
131 return ERR( std::string(
"the extraction failed: " ) + e.what() );
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.";
139 QString list_gate_ids(
const std::vector<u32> &ids )
142 for( std::size_t idx = 0; idx < std::min( ids.size(), MAX_LISTED_GATES ); idx++ )
148 if( ids.size() > MAX_LISTED_GATES )
150 result +=
QString(
" and %1 more" ).
arg( ids.size() - MAX_LISTED_GATES );
157 template<
typename Work>
158 void run_in_gui_thread( Work &&work )
171 ClockTreeViewer *open_viewer()
174 if( viewer ==
nullptr )
176 log_error(
"clock_tree_viewer",
"Cannot find clock tree viewer instance." );
185 mExtractAction =
new QAction(
this );
186 mExtractAction->
setToolTip(
"Extract the clock tree of the netlist and plot it" );
190 mFitAction =
new QAction(
this );
191 mFitAction->
setToolTip(
"Zoom to the whole clock tree" );
192 mFitAction->
setIcon(
QIcon(
":/icons/zoom_waveform" ) );
195 mZoomToHighlightAction =
new QAction(
this );
196 mZoomToHighlightAction->
setToolTip(
"Zoom to the highlighted gates" );
198 QString(
"all->%1" ).arg(
QColor( HAL_SELECTION_COLOR ).
name() ),
":/icons/to_selection" ) );
201 mSavePictureAction =
new QAction(
this );
202 mSavePictureAction->
setToolTip(
"Save a picture of the clock tree" );
210 mMessageLabel =
new QLabel( IDLE_TEXT,
this );
221 mCoordinateLabel =
new QLabel(
" x: 0, y: 0",
this );
222 mWarningLabel =
new QLabel(
this );
223 mWarningLabel->
hide();
225 mHighlightLabel =
new QLabel(
this );
226 mHighlightLabel->
hide();
231 mStatusBar->
addWidget( mCoordinateLabel );
232 mStatusBar->
addWidget( mWarningLabel, 1 );
238 mRecolorTimer =
new QTimer(
this );
249 &ClockTreeViewer::handleStyleChanged );
256 &ClockTreeViewer::scheduleRecolor );
274 &ClockTreeViewer::handleFileAboutToClose );
281 if( mExtraction !=
nullptr )
299 toolbar->
addAction( mZoomToHighlightAction );
300 toolbar->
addAction( mSavePictureAction );
305 const std::optional<std::string> &x_key,
306 const std::optional<std::string> &y_key )
313 run_in_gui_thread( [&]() {
315 if( viewer !=
nullptr )
317 shown = viewer->display(
layout, overrides, x_key, y_key );
325 const std::optional<std::string> &x_key,
326 const std::optional<std::string> &y_key )
334 if(
style.is_error() )
339 mOverrides = overrides;
345 for( std::size_t idx = 0; idx < mLayout.
nodes.size(); idx++ )
347 mNodeOfGate.
insert( mLayout.
nodes[idx].gate_id,
static_cast<int>( idx ) );
352 highlightFromSelection();
362 "clock_tree_viewer",
"Showing {} gates and {} connections.", mLayout.
nodes.size(), mLayout.
edges.size() );
374 "{} of {} gates have no coordinates and are not shown, {} connections are left out. Gate IDs: {}",
380 showWarning(
QString(
"%1 of %2 gates have no coordinates and are not shown (%3 connections left out)" )
389 bool ClockTreeViewer::showError(
const QString &message )
397 text.remove( source_location );
400 mLayout = ctv::Layout();
402 mHighlightedGates.clear();
405 mMessageLabel->
setText(
"The clock tree cannot be shown:\n\n" + text.
trimmed() );
413 void ClockTreeViewer::showWarning(
const QString &message )
const
415 mWarningLabel->
setText( message );
419 void ClockTreeViewer::applyThemeColors()
const
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 )
433 for(
const u32 gateId : mHighlightedGates )
440 void ClockTreeViewer::updateActions()
const
447 mHighlightLabel->
setText(
QString(
"%1 highlighted " ).arg( highlighted ) );
448 mHighlightLabel->
setVisible( highlighted > 0 );
451 QColor ClockTreeViewer::gateColor(
u32 gateId,
const QColor &fallback )
const
454 if( mHighlightedGates.contains( gateId ) )
456 return HAL_SELECTION_COLOR;
465 if( gate ==
nullptr )
488 ClockTreeColors ClockTreeViewer::currentColors()
const
490 ClockTreeColors colors;
494 colors.highlight = HAL_SELECTION_COLOR;
496 colors.nodes.reserve(
static_cast<int>( mLayout.
nodes.size() ) );
497 for(
const ctv::Node &node : mLayout.
nodes )
499 colors.nodes.push_back( gateColor( node.gate_id, colors.edge ) );
505 QColor ClockTreeViewer::backgroundColor()
const
511 void ClockTreeViewer::handleStyleChanged()
518 logTheme->style()->unpolish( logTheme );
519 logTheme->style()->polish( logTheme );
525 void ClockTreeViewer::scheduleRecolor()
529 mRecolorTimer->
start();
533 void ClockTreeViewer::recolor()
541 void ClockTreeViewer::highlightFromSelection()
543 mHighlightedGates.clear();
553 if( mNodeOfGate.
contains( gateId ) )
555 mHighlightedGates.insert( gateId );
560 void ClockTreeViewer::handleSelectionChanged()
567 highlightFromSelection();
571 std::size_t ClockTreeViewer::applyHighlight(
const std::vector<u32> &gateIds )
573 mHighlightedGates.clear();
575 for(
const u32 gateId : gateIds )
577 if( mNodeOfGate.
contains( gateId ) )
579 mHighlightedGates.insert( gateId );
586 return static_cast<std::size_t
>( mHighlightedGates.size() );
591 std::size_t highlighted = 0;
594 run_in_gui_thread( [&]() {
596 if( viewer !=
nullptr )
599 highlighted = viewer->applyHighlight( gateIds );
603 if( found && !gateIds.empty() )
606 "Highlighted {} of {} gates, the others are not in the plot.",
614 void ClockTreeViewer::handleZoomToHighlightClicked()
633 if( !image.
get().save( filename ) )
635 log_error(
"clock_tree_viewer",
"Could not save the picture to '{}'.", filename.
toStdString() );
639 log_info(
"clock_tree_viewer",
"Saved a picture of the clock tree to '{}'.", filename.
toStdString() );
643 void ClockTreeViewer::handleExtractClicked()
647 showError(
"No netlist is loaded." );
651 if( mExtraction !=
nullptr )
658 const ctv::StyleOverrides overrides = mOverrides;
659 const std::optional<std::string> x_key = mXKey;
660 const std::optional<std::string> y_key = mYKey;
663 const auto result = std::make_shared<std::optional<Result<ctv::Layout>>>();
666 [netlist, x_key, y_key, result]() { result->emplace( extract_layout( netlist, x_key, y_key ) ); } );
672 progress->setWindowTitle(
"Clock tree viewer" );
674 progress->setCancelButton(
nullptr );
681 mExtraction =
nullptr;
684 if( result->has_value() )
686 display( **result, overrides, x_key, y_key );
690 showError(
"The extraction ended without a result." );
697 mExtraction->start();
700 void ClockTreeViewer::handleFileAboutToClose()
702 if( mExtraction !=
nullptr )
708 void ClockTreeViewer::handleFitClicked()
const
713 void ClockTreeViewer::handleSavePictureClicked()
716 this,
"Save a picture of the clock tree",
"clock_tree.png",
"Images (*.png *.jpg *.bmp)" );
723 if(
QFileInfo( filename ).suffix().isEmpty() )
731 this,
"Clock tree picture",
"The picture could not be saved, see the log for details." );
735 void ClockTreeViewer::handleCoordinatesChanged(
double x,
double y )
741 pixelsPerUnit > 1.0 ? std::min( 6,
static_cast<int>( std::ceil( std::log10( pixelsPerUnit ) ) ) ) : 0;
746 .arg( english.toString(
x,
'f', decimals ), english.toString(
y,
'f', decimals ) ) );
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)
~ClockTreeViewer() override
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()
QVBoxLayout * mContentLayout
const std::string & get() const
static ExternalContent * instance()
QMap< QString, ExternalContentWidget * > openWidgets
static FileManager * get_instance()
void fileAboutToClose(const QString &fileName)
static GraphicsQssAdapter * instance()
QColor netBaseColor() const
QColor colorForItem(ItemType itemType, u32 itemId) const
static LoggerQssAdapter * instance()
static SettingsItemDropdown * sSettingStyle
void moduleColorChanged(u32 id) const
Gate * get_gate_by_id(const u32 gate_id) const
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 Error & get_error() const
void selectionChanged(void *sender)
QList< u32 > selectedGatesList() const
void intChanged(int value)
static Result< std::unique_ptr< ClockTree > > from_netlist(const Netlist *netlist)
#define log_error(channel,...)
#define log_info(channel,...)
#define log_warning(channel,...)
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())
ContentManager * gContentManager
SelectionRelay * gSelectionRelay
NetlistRelay * gNetlistRelay
void setIcon(const QIcon &icon)
void triggered(bool checked)
bool isValid() const const
QCoreApplication * instance()
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, QFileDialog::Options options)
void restoreOverrideCursor()
void setOverrideCursor(const QCursor &cursor)
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 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)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QThread * thread() const const
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
QThread * create(Function &&f, Args &&... args)
QThread * currentThread()
void setInterval(int msec)
void setSingleShot(bool singleShot)
bool isEmpty() const const
void push_back(const T &value)
std::vector< u32 > gates_without_coordinates
std::size_t omitted_edges
std::vector< Node > nodes
std::vector< Edge > edges