mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-18 22:23:56 +08:00
cabana: support drag and drop to merge charts (#26968)
old-commit-hash: 58bd024089
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <QApplication>
|
||||
#include <QCompleter>
|
||||
#include <QDrag>
|
||||
#include <QLineEdit>
|
||||
#include <QFutureSynchronizer>
|
||||
#include <QGraphicsLayout>
|
||||
@@ -331,6 +332,17 @@ void ChartView::msgRemoved(uint32_t address) {
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::addSeries(const QList<QStringList> &series_list) {
|
||||
for (auto &s : series_list) {
|
||||
if (auto m = dbc()->msg(s[0])) {
|
||||
auto it = m->sigs.find(s[2]);
|
||||
if (it != m->sigs.end() && !hasSeries(s[0], &(it->second))) {
|
||||
addSeries(s[0], &(it->second));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::manageSeries() {
|
||||
SeriesSelector dlg(this);
|
||||
for (auto &s : sigs) {
|
||||
@@ -343,14 +355,7 @@ void ChartView::manageSeries() {
|
||||
if (series_list.isEmpty()) {
|
||||
emit remove();
|
||||
} else {
|
||||
for (auto &s : series_list) {
|
||||
if (auto m = dbc()->msg(s[0])) {
|
||||
auto it = m->sigs.find(s[2]);
|
||||
if (it != m->sigs.end() && !hasSeries(s[0], &(it->second))) {
|
||||
addSeries(s[0], &(it->second));
|
||||
}
|
||||
}
|
||||
}
|
||||
addSeries(series_list);
|
||||
for (auto it = sigs.begin(); it != sigs.end(); /**/) {
|
||||
bool exists = std::any_of(series_list.cbegin(), series_list.cend(), [&](auto &s) {
|
||||
return s[0] == it->msg_id && s[2] == it->sig->name.c_str();
|
||||
@@ -495,6 +500,23 @@ void ChartView::leaveEvent(QEvent *event) {
|
||||
QChartView::leaveEvent(event);
|
||||
}
|
||||
|
||||
void ChartView::mousePressEvent(QMouseEvent *event) {
|
||||
if (event->button() == Qt::LeftButton && !chart()->plotArea().contains(event->pos()) &&
|
||||
!manage_btn_proxy->widget()->underMouse() && !close_btn_proxy->widget()->underMouse()) {
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
mimeData->setData(mime_type, QByteArray::number((qulonglong)this));
|
||||
QDrag *drag = new QDrag(this);
|
||||
drag->setMimeData(mimeData);
|
||||
drag->setPixmap(grab());
|
||||
drag->setHotSpot(event->pos());
|
||||
Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
|
||||
if (dropAction == Qt::MoveAction) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
QChartView::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void ChartView::mouseReleaseEvent(QMouseEvent *event) {
|
||||
auto rubber = findChild<QRubberBand *>();
|
||||
if (event->button() == Qt::LeftButton && rubber && rubber->isVisible()) {
|
||||
@@ -550,6 +572,35 @@ void ChartView::mouseMoveEvent(QMouseEvent *ev) {
|
||||
QChartView::mouseMoveEvent(ev);
|
||||
}
|
||||
|
||||
void ChartView::dragMoveEvent(QDragMoveEvent *event) {
|
||||
if (event->mimeData()->hasFormat(mime_type)) {
|
||||
event->setDropAction(event->source() == this ? Qt::MoveAction : Qt::CopyAction);
|
||||
event->accept();
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::dropEvent(QDropEvent *event) {
|
||||
if (event->mimeData()->hasFormat(mime_type)) {
|
||||
if (event->source() == this) {
|
||||
event->setDropAction(Qt::MoveAction);
|
||||
event->accept();
|
||||
} else {
|
||||
ChartView *source_chart = (ChartView *)event->source();
|
||||
QList<QStringList> series;
|
||||
for (auto &s : source_chart->sigs) {
|
||||
series.push_back({s.msg_id, msgName(s.msg_id), QString::fromStdString(s.sig->name)});
|
||||
}
|
||||
addSeries(series);
|
||||
emit source_chart->remove();
|
||||
event->acceptProposedAction();
|
||||
}
|
||||
} else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void ChartView::drawForeground(QPainter *painter, const QRectF &rect) {
|
||||
qreal x = chart()->mapToPosition(QPointF{can->currentSec(), 0}).x();
|
||||
qreal y1 = chart()->plotArea().top() - 2;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QDragEnterEvent>
|
||||
#include <QLabel>
|
||||
#include <QListWidget>
|
||||
#include <QGraphicsProxyWidget>
|
||||
@@ -58,8 +59,11 @@ private slots:
|
||||
|
||||
private:
|
||||
QList<ChartView::SigItem>::iterator removeSeries(const QList<ChartView::SigItem>::iterator &it);
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
void mouseReleaseEvent(QMouseEvent *event) override;
|
||||
void mouseMoveEvent(QMouseEvent *ev) override;
|
||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||
void dropEvent(QDropEvent *event) override;
|
||||
void leaveEvent(QEvent *event) override;
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
void updateAxisY();
|
||||
@@ -68,6 +72,7 @@ private:
|
||||
void drawForeground(QPainter *painter, const QRectF &rect) override;
|
||||
void applyNiceNumbers(qreal min, qreal max);
|
||||
qreal niceNumber(qreal x, bool ceiling);
|
||||
void addSeries(const QList<QStringList> &series_list);
|
||||
|
||||
QValueAxis *axis_x;
|
||||
QValueAxis *axis_y;
|
||||
@@ -76,6 +81,7 @@ private:
|
||||
QGraphicsProxyWidget *manage_btn_proxy;
|
||||
std::pair<double, double> events_range = {0, 0};
|
||||
QList<SigItem> sigs;
|
||||
const QString mime_type = "application/x-cabanachartview";
|
||||
};
|
||||
|
||||
class ChartsWidget : public QWidget {
|
||||
|
||||
Reference in New Issue
Block a user