Skip to content

Commit 689863b

Browse files
committed
gui: allow gui clients to make their own charts
Similar to the way Painter works (hides the QPainter) Signed-off-by: Matt Liberty <mliberty@precisioninno.com>
1 parent 44bc564 commit 689863b

4 files changed

Lines changed: 123 additions & 12 deletions

File tree

src/gui/include/gui/gui.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ class Painter
158158
static inline const Color kHighlight = kYellow;
159159
static inline const Color kPersistHighlight = kYellow;
160160

161-
Painter(Options* options, const odb::Rect& bounds, double pixels_per_dbu)
162-
: options_(options), bounds_(bounds), pixels_per_dbu_(pixels_per_dbu)
163-
{
164-
}
165161
virtual ~Painter() = default;
166162

167163
// Get the current pen color
@@ -280,6 +276,12 @@ class Painter
280276
Options* getOptions() { return options_; }
281277
const odb::Rect& getBounds() { return bounds_; }
282278

279+
protected:
280+
Painter(Options* options, const odb::Rect& bounds, double pixels_per_dbu)
281+
: options_(options), bounds_(bounds), pixels_per_dbu_(pixels_per_dbu)
282+
{
283+
}
284+
283285
private:
284286
Options* options_;
285287
const odb::Rect bounds_;
@@ -564,6 +566,23 @@ class SpectrumGenerator
564566
double scale_;
565567
};
566568

569+
class Chart
570+
{
571+
public:
572+
virtual ~Chart() = default;
573+
574+
virtual void setAxisLabel(const std::string& label,
575+
odb::Orientation2D orientation)
576+
= 0;
577+
virtual void setAxisFormat(const std::string& format,
578+
odb::Orientation2D orientation)
579+
= 0;
580+
virtual void addPoint(double x, double y) = 0;
581+
582+
protected:
583+
Chart() = default;
584+
};
585+
567586
// This is the API for the rest of the program to interact with the
568587
// GUI. This class is accessed by the GUI implementation to interact
569588
// with the rest of the system. This class itself doesn't hold the
@@ -703,6 +722,8 @@ class Gui
703722
void removeNetTracks(odb::dbNet* net);
704723
void clearNetTracks();
705724

725+
Chart* addChart(const std::string& name);
726+
706727
// show/hide widgets
707728
void showWidget(const std::string& name, bool show);
708729

src/gui/src/chartsWidget.cpp

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QFrame>
88
#include <QHBoxLayout>
99
#include <QString>
10+
#include <QValueAxis>
1011
#include <QWidget>
1112
#include <QtCharts>
1213
#include <algorithm>
@@ -25,11 +26,75 @@
2526

2627
namespace gui {
2728

29+
class GuiChart : public Chart
30+
{
31+
public:
32+
GuiChart(QChart* chart);
33+
34+
void setAxisLabel(const std::string& label,
35+
odb::Orientation2D orientation) override;
36+
void setAxisFormat(const std::string& label,
37+
odb::Orientation2D orientation) override;
38+
void addPoint(const double x, const double y) override;
39+
40+
private:
41+
QChart* chart_;
42+
QLineSeries* series_;
43+
QValueAxis* x_axis_;
44+
QValueAxis* y_axis_;
45+
double x_min_{std::numeric_limits<double>::max()};
46+
double x_max_{std::numeric_limits<double>::lowest()};
47+
double y_min_{std::numeric_limits<double>::max()};
48+
double y_max_{std::numeric_limits<double>::lowest()};
49+
};
50+
51+
GuiChart::GuiChart(QChart* chart) : chart_(chart)
52+
{
53+
series_ = new QLineSeries();
54+
chart->addSeries(series_);
55+
chart->createDefaultAxes();
56+
57+
x_axis_ = qobject_cast<QValueAxis*>(chart_->axes(Qt::Horizontal).first());
58+
y_axis_ = qobject_cast<QValueAxis*>(chart_->axes(Qt::Vertical).first());
59+
}
60+
61+
void GuiChart::setAxisLabel(const std::string& label,
62+
odb::Orientation2D orientation)
63+
{
64+
QValueAxis* axis = (orientation == odb::horizontal) ? x_axis_ : y_axis_;
65+
axis->setTitleText(QString::fromStdString(label));
66+
}
67+
68+
void GuiChart::setAxisFormat(const std::string& format,
69+
odb::Orientation2D orientation)
70+
{
71+
QValueAxis* axis = (orientation == odb::horizontal) ? x_axis_ : y_axis_;
72+
axis->setLabelFormat(QString::fromStdString(format));
73+
}
74+
75+
void GuiChart::addPoint(const double x, const double y)
76+
{
77+
series_->append(x, y);
78+
x_min_ = std::min(x_min_, x);
79+
x_max_ = std::max(x_max_, x);
80+
y_min_ = std::min(y_min_, x);
81+
y_max_ = std::max(y_max_, y);
82+
83+
// Adjust the axes to match the data range
84+
x_axis_->setMin(x_min_);
85+
x_axis_->setMax(x_max_);
86+
y_axis_->setMin(y_min_);
87+
y_axis_->setMax(y_max_);
88+
}
89+
90+
//////////////////////////////////////////////////
91+
2892
ChartsWidget::ChartsWidget(QWidget* parent)
2993
: QDockWidget("Charts", parent),
3094
logger_(nullptr),
3195
sta_(nullptr),
3296
stagui_(nullptr),
97+
chart_tabs_(new QTabWidget(this)),
3398
mode_menu_(new QComboBox(this)),
3499
filters_menu_(new QComboBox(this)),
35100
display_(new HistogramView(this)),
@@ -39,29 +104,37 @@ ChartsWidget::ChartsWidget(QWidget* parent)
39104
label_(new QLabel(this))
40105
{
41106
setObjectName("charts_widget"); // for settings
107+
chart_tabs_->setTabBarAutoHide(true);
42108

43-
QWidget* container = new QWidget(this);
44109
QHBoxLayout* controls_layout = new QHBoxLayout;
45110
controls_layout->addWidget(label_);
46111

47-
QVBoxLayout* layout = new QVBoxLayout;
48-
QFrame* controls_frame = new QFrame;
49-
50-
controls_layout->insertWidget(0, mode_menu_);
112+
controls_layout->addWidget(mode_menu_);
51113
setModeMenu();
52-
controls_layout->insertWidget(1, filters_menu_);
114+
controls_layout->addWidget(filters_menu_);
53115
filters_menu_->hide();
54116
controls_layout->addWidget(refresh_filters_button_);
55117
refresh_filters_button_->hide();
56118
controls_layout->insertStretch(2);
57119

120+
QFrame* controls_frame = new QFrame;
58121
controls_frame->setLayout(controls_layout);
59122
controls_frame->setFrameShape(QFrame::StyledPanel);
60123
controls_frame->setFrameShadow(QFrame::Raised);
61124

62-
layout->addWidget(controls_frame);
63-
layout->addWidget(display_);
125+
QVBoxLayout* slack_layout = new QVBoxLayout;
126+
slack_layout->addWidget(controls_frame);
127+
slack_layout->addWidget(display_);
64128

129+
QWidget* slack_container = new QWidget(this);
130+
slack_container->setLayout(slack_layout);
131+
132+
chart_tabs_->addTab(slack_container, "Slack");
133+
134+
QVBoxLayout* layout = new QVBoxLayout;
135+
layout->addWidget(chart_tabs_);
136+
137+
QWidget* container = new QWidget(this);
65138
container->setLayout(layout);
66139

67140
connect(refresh_filters_button_,
@@ -81,6 +154,15 @@ ChartsWidget::ChartsWidget(QWidget* parent)
81154
setWidget(container);
82155
}
83156

157+
Chart* ChartsWidget::addChart(const std::string& name)
158+
{
159+
QChart* chart = new QChart;
160+
QChartView* view = new QChartView(chart);
161+
const int tab_index = chart_tabs_->addTab(view, QString::fromStdString(name));
162+
chart_tabs_->setCurrentIndex(tab_index);
163+
return new GuiChart(chart);
164+
}
165+
84166
void ChartsWidget::changeMode()
85167
{
86168
filters_menu_->clear();

src/gui/src/chartsWidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class ChartsWidget : public QDockWidget
137137
const std::optional<int>& height_px);
138138

139139
Mode modeFromString(const std::string& mode) const;
140+
Chart* addChart(const std::string& name);
140141

141142
signals:
142143
void endPointsToReport(const std::set<const sta::Pin*>& report_pins,
@@ -164,6 +165,8 @@ class ChartsWidget : public QDockWidget
164165
sta::dbSta* sta_;
165166
std::unique_ptr<STAGuiInterface> stagui_;
166167

168+
QTabWidget* chart_tabs_;
169+
167170
QComboBox* mode_menu_;
168171
QComboBox* filters_menu_;
169172
HistogramView* display_;

src/gui/src/gui.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,11 @@ void Gui::addRouteGuides(odb::dbNet* net)
12371237
main_window->getLayoutTabs()->addRouteGuides(net);
12381238
}
12391239

1240+
Chart* Gui::addChart(const std::string& name)
1241+
{
1242+
return main_window->getChartsWidget()->addChart(name);
1243+
}
1244+
12401245
void Gui::removeRouteGuides(odb::dbNet* net)
12411246
{
12421247
main_window->getLayoutTabs()->removeRouteGuides(net);

0 commit comments

Comments
 (0)