mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-19 08:53:54 +08:00
cabana: click on legend to show/hide series (#27231)
* click on legend to show/hide series
* update axis y
* add move icon
old-commit-hash: f9de6a0dd8
This commit is contained in:
@@ -317,6 +317,9 @@ ChartView::ChartView(QWidget *parent) : QChartView(nullptr, parent) {
|
||||
background->setPen(Qt::NoPen);
|
||||
background->setZValue(chart->zValue() - 1);
|
||||
|
||||
move_icon = new QGraphicsPixmapItem(utils::icon("grip-horizontal"), chart);
|
||||
move_icon->setToolTip(tr("Drag and drop to combine charts"));
|
||||
|
||||
QToolButton *remove_btn = new QToolButton();
|
||||
remove_btn->setIcon(utils::icon("x"));
|
||||
remove_btn->setAutoRaise(true);
|
||||
@@ -460,6 +463,7 @@ void ChartView::resizeEvent(QResizeEvent *event) {
|
||||
int x = event->size().width() - close_btn_proxy->size().width() - 11;
|
||||
close_btn_proxy->setPos(x, 8);
|
||||
manage_btn_proxy->setPos(x - manage_btn_proxy->size().width() - 5, 8);
|
||||
move_icon->setPos(11, 8);
|
||||
}
|
||||
|
||||
void ChartView::updatePlotArea(int left) {
|
||||
@@ -472,8 +476,12 @@ void ChartView::updatePlotArea(int left) {
|
||||
}
|
||||
|
||||
void ChartView::updateTitle() {
|
||||
for (QLegendMarker *marker : chart()->legend()->markers()) {
|
||||
QObject::connect(marker, &QLegendMarker::clicked, this, &ChartView::handleMarkerClicked, Qt::UniqueConnection);
|
||||
}
|
||||
for (auto &s : sigs) {
|
||||
s.series->setName(QString("<b>%1</b> <font color=\"gray\">%2 %3</font>").arg(s.sig->name.c_str()).arg(msgName(s.msg_id)).arg(s.msg_id));
|
||||
auto decoration = s.series->isVisible() ? "none" : "line-through";
|
||||
s.series->setName(QString("<span style=\"text-decoration:%1\"><b>%2</b><font color=\"gray\">%3 %4</font></span>").arg(decoration).arg(s.sig->name.c_str()).arg(msgName(s.msg_id)).arg(s.msg_id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -578,6 +586,8 @@ void ChartView::updateAxisY() {
|
||||
double min = std::numeric_limits<double>::max();
|
||||
double max = std::numeric_limits<double>::lowest();
|
||||
for (auto &s : sigs) {
|
||||
if (!s.series->isVisible()) continue;
|
||||
|
||||
auto first = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->min(), [](auto &p, double x) { return p.x() < x; });
|
||||
auto last = std::lower_bound(s.vals.begin(), s.vals.end(), axis_x->max(), [](auto &p, double x) { return p.x() < x; });
|
||||
for (auto it = first; it != last; ++it) {
|
||||
@@ -634,8 +644,7 @@ void ChartView::leaveEvent(QEvent *event) {
|
||||
}
|
||||
|
||||
void ChartView::mousePressEvent(QMouseEvent *event) {
|
||||
if (event->button() == Qt::LeftButton && !chart()->plotArea().contains(event->pos()) &&
|
||||
!manage_btn_proxy->geometry().contains(event->pos()) && !close_btn_proxy->geometry().contains(event->pos())) {
|
||||
if (event->button() == Qt::LeftButton && move_icon->sceneBoundingRect().contains(event->pos())) {
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData(mime_type, QByteArray::number((qulonglong)this));
|
||||
QDrag *drag = new QDrag(this);
|
||||
@@ -810,6 +819,18 @@ void ChartView::setSeriesType(QAbstractSeries::SeriesType type) {
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::handleMarkerClicked() {
|
||||
auto marker = qobject_cast<QLegendMarker *>(sender());
|
||||
Q_ASSERT(marker);
|
||||
if (sigs.size() > 1) {
|
||||
auto series = marker->series();
|
||||
series->setVisible(!series->isVisible());
|
||||
marker->setVisible(true);
|
||||
updateAxisY();
|
||||
updateTitle();
|
||||
}
|
||||
}
|
||||
|
||||
// SeriesSelector
|
||||
|
||||
SeriesSelector::SeriesSelector(QWidget *parent) {
|
||||
|
||||
@@ -5,9 +5,11 @@
|
||||
#include <QGridLayout>
|
||||
#include <QLabel>
|
||||
#include <QListWidget>
|
||||
#include <QGraphicsPixmapItem>
|
||||
#include <QGraphicsProxyWidget>
|
||||
#include <QSlider>
|
||||
#include <QtCharts/QChartView>
|
||||
#include <QtCharts/QLegendMarker>
|
||||
#include <QtCharts/QLineSeries>
|
||||
#include <QtCharts/QScatterSeries>
|
||||
#include <QtCharts/QValueAxis>
|
||||
@@ -56,6 +58,7 @@ private slots:
|
||||
void signalUpdated(const Signal *sig);
|
||||
void signalRemoved(const Signal *sig);
|
||||
void manageSeries();
|
||||
void handleMarkerClicked();
|
||||
|
||||
private:
|
||||
QList<ChartView::SigItem>::iterator removeItem(const QList<ChartView::SigItem>::iterator &it);
|
||||
@@ -80,6 +83,7 @@ private:
|
||||
QValueAxis *axis_x;
|
||||
QValueAxis *axis_y;
|
||||
QVector<QPointF> track_pts;
|
||||
QGraphicsPixmapItem *move_icon;
|
||||
QGraphicsProxyWidget *close_btn_proxy;
|
||||
QGraphicsProxyWidget *manage_btn_proxy;
|
||||
QGraphicsRectItem *background;
|
||||
|
||||
Reference in New Issue
Block a user