-
Notifications
You must be signed in to change notification settings - Fork 3
/
qcpl_graph_grid.cpp
188 lines (161 loc) · 4.91 KB
/
qcpl_graph_grid.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "qcpl_graph_grid.h"
#include <QAbstractItemModel>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QTableView>
#include <QMenu>
#include "qcpl_types.h"
#include "qcustomplot/qcustomplot.h"
namespace {
class GraphDataModel : public QAbstractItemModel
{
public:
GraphDataModel(QObject* parent) : QAbstractItemModel(parent) {}
QModelIndex index(int row, int col, const QModelIndex &parent) const override
{
Q_UNUSED(parent)
return createIndex(row, col);
}
QModelIndex parent(const QModelIndex &child) const override
{
Q_UNUSED(child)
return QModelIndex();
}
int rowCount(const QModelIndex &parent) const override
{
Q_UNUSED(parent)
return _data ? _data->size() : _x.size();
}
int columnCount(const QModelIndex &parent) const override
{
Q_UNUSED(parent)
return 2;
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if (role != Qt::DisplayRole) return QVariant();
switch (orientation)
{
case Qt::Vertical:
return section + 1;
case Qt::Horizontal:
return section == 0 ? QStringLiteral("X") : QStringLiteral("Y");
}
return QVariant();
}
QVariant data(const QModelIndex &index, int role) const override
{
if (role != Qt::DisplayRole && role != Qt::UserRole) return QVariant();
double value;
if (!_data)
{
const QCPL::ValueArray& vals = index.column() == 0 ? _x : _y;
value = vals.at(index.row());
}
else
{
auto it = _data->at(index.row());
value = index.column() == 0 ? it->key : it->value;
}
if (role == Qt::DisplayRole)
return QString::number(value, 'g', _numberPrecision);
else return value;
}
void setGraphData(const QCPL::ValueArray& x, const QCPL::ValueArray& y)
{
beginResetModel();
_data.reset();
_x = x;
_y = y;
endResetModel();
}
void setGraphData(QSharedPointer<QCPGraphDataContainer> data)
{
beginResetModel();
_x.clear();
_y.clear();
_data = data;
endResetModel();
}
void setNumberPrecision(int value)
{
beginResetModel();
_numberPrecision = value;
endResetModel();
}
private:
QCPL::ValueArray _x, _y;
QSharedPointer<QCPGraphDataContainer> _data;
int _numberPrecision = 6;
};
} // namespace
namespace QCPL {
GraphDataGrid::GraphDataGrid(QWidget *parent) : QTableView(parent)
{
auto model = new GraphDataModel(this);
setModel(model);
setShowGrid(false);
setAlternatingRowColors(true);
setSelectionMode(QAbstractItemView::ContiguousSelection);
horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
model->setGraphData({0.0}, {0.0});
resizeRowsToContents();
verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
verticalHeader()->setDefaultSectionSize(rowHeight(0));
model->setGraphData({}, {});
}
void GraphDataGrid::setNumberPrecision(int value)
{
dynamic_cast<GraphDataModel*>(model())->setNumberPrecision(value);
}
void GraphDataGrid::setData(const ValueArray& x, const ValueArray& y)
{
dynamic_cast<GraphDataModel*>(model())->setGraphData(x, y);
}
void GraphDataGrid::setData(QCPGraph* graph)
{
dynamic_cast<GraphDataModel*>(model())->setGraphData(graph->data());
}
void GraphDataGrid::contextMenuEvent(QContextMenuEvent* event)
{
QTableView::contextMenuEvent(event);
if (!_contextMenu)
{
_contextMenu = new QMenu(this);
_contextMenu->addAction(tr("Copy"), this, SLOT(copy()));
_contextMenu->addAction(tr("Select All"), this, SLOT(selectAll()));
}
_contextMenu->popup(mapToGlobal(event->pos()));
}
void GraphDataGrid::keyPressEvent(QKeyEvent *event)
{
if (event->matches(QKeySequence::Copy))
copy();
else
QTableView::keyPressEvent(event);
}
void GraphDataGrid::copy()
{
auto selection = selectionModel()->selection();
if (selection.isEmpty()) return;
auto range = selection.first();
int row1 = range.top();
int row2 = range.bottom();
int col1 = range.left();
int col2 = range.right();
auto exporter = GraphDataExporter(getExportSettings ? getExportSettings() : GraphDataExportSettings());
if (col1 != col2)
{
for (int row = row1; row <= row2; row++)
exporter.add(model()->data(model()->index(row, col1), Qt::UserRole).toDouble(),
model()->data(model()->index(row, col2), Qt::UserRole).toDouble());
}
else
{
for (int row = row1; row <= row2; row++)
exporter.add(model()->data(model()->index(row, col1), Qt::UserRole).toDouble());
}
exporter.toClipboard();
}
} // namespace QCPL