-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrajectoryEditor.cpp
153 lines (122 loc) · 4.13 KB
/
TrajectoryEditor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "TrajectoryEditor.h"
#include "ui_TrajectoryEditor.h"
#include "PointsTableModel.h"
#include "PointsModel.h"
TrajectoryEditor::TrajectoryEditor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TrajectoryEditor)
{
ui->setupUi(this);
}
TrajectoryEditor::~TrajectoryEditor()
{
delete ui;
}
void TrajectoryEditor::init(PointsModel &pointsModel)
{
this->pointsModel = &pointsModel;
initMenuActions();
initChartView();
initEditor();
}
void TrajectoryEditor::pointsTableModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
Q_UNUSED(bottomRight);
QAbstractItemModel* tableModel = ui->pointsTableView->model();
QModelIndex topLeftX = tableModel->index(topLeft.row(),0);
float x = tableModel->data(topLeftX,Qt::DisplayRole).toFloat();
QModelIndex topLeftY = tableModel->index(topLeft.row(),1);
float y = tableModel->data(topLeftY, Qt::DisplayRole).toFloat();
pointsModel->modifyPoint(topLeft.row(),QPointF(x,y));
}
void TrajectoryEditor::initMenuActions()
{
initShowEditorAction();
}
void TrajectoryEditor::initShowEditorAction()
{
ui->actionShowEditor->setCheckable(true);
ui->actionShowEditor->setChecked(true);
connect(ui->editorDockWidget, &QDockWidget::visibilityChanged, [this]{
ui->actionShowEditor->blockSignals(true);
ui->actionShowEditor->setChecked(!ui->editorDockWidget->isHidden());
ui->actionShowEditor->blockSignals(false);
});
connect(ui->actionShowEditor, &QAction::changed, [this]{
ui->actionShowEditor->blockSignals(true);
ui->editorDockWidget->setHidden(!ui->editorDockWidget->isHidden());
ui->actionShowEditor->blockSignals(false);
});
}
void TrajectoryEditor::initChartView()
{
QLineSeries* lineSeries = createLineSeries();
QChart* chart = createChart(lineSeries);
connect(pointsModel, &PointsModel::pointAppended, [lineSeries]{
lineSeries->append(std::move(QPointF()));
});
connect(pointsModel, &PointsModel::pointModified, [lineSeries](uint pos, const QPointF& point){
lineSeries->replace(pos,point);
});
connect(pointsModel, &PointsModel::pointRemoved, [lineSeries](uint pos){
lineSeries->remove(pos);
});
ui->chartView->setChart(chart);
ui->chartView->setRenderHint(QPainter::Antialiasing);
ui->chartView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
}
QLineSeries *TrajectoryEditor::createLineSeries() const
{
QLineSeries *lineSeries = new QLineSeries();
lineSeries->setName("line");
foreach(const QPointF& point, pointsModel->getPoints()){
lineSeries->append(point.x(),point.y());
}
return lineSeries;
}
QChart* TrajectoryEditor::createChart(QLineSeries *lineSeries) const
{
QChart* chart = new QChart();
chart->addSeries(lineSeries);
chart->legend()->hide();
chart->setTitle(tr("Trajectory"));
chart->createDefaultAxes();
return chart;
}
void TrajectoryEditor::initEditor()
{
initAddButton();
initRemoveButton();
initPointsTable();
}
void TrajectoryEditor::initAddButton()
{
connect(ui->addPushButton, SIGNAL(pressed()), pointsModel, SLOT(appendPoint()));
}
void TrajectoryEditor::initRemoveButton()
{
connect(ui->removePushButton, &QPushButton::pressed, [this]{
QModelIndexList selectedIndexes = ui->pointsTableView->selectionModel()->selectedRows();
if(!selectedIndexes.empty()){
pointsModel->removePoint(selectedIndexes.first().row());
}
});
}
void TrajectoryEditor::initPointsTable()
{
PointsTableModel* pointsTableModel = new PointsTableModel();
pointsTableModel->initPoints(pointsModel->getPoints());
ui->pointsTableView->setModel(pointsTableModel);
ui->pointsTableView->setSelectionMode(QTableView::SingleSelection);
ui->pointsTableView->setSelectionBehavior(QTableView::SelectRows);
ui->pointsTableView->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContentsOnFirstShow);
connect(pointsModel, &PointsModel::pointAppended, [pointsTableModel]{
pointsTableModel->appendPoint();
});
connect(pointsTableModel,
SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
SLOT(pointsTableModelDataChanged(const QModelIndex&,const QModelIndex&)));
connect(pointsModel, &PointsModel::pointRemoved, [pointsTableModel](uint pos){
pointsTableModel->removePoint(pos);
});
}