-
Notifications
You must be signed in to change notification settings - Fork 0
/
PointsTableModel.cpp
103 lines (88 loc) · 1.85 KB
/
PointsTableModel.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
#include "PointsTableModel.h"
#include <QString>
PointsTableModel::PointsTableModel()
{
}
void PointsTableModel::initPoints(const QList<QPointF> &points)
{
beginResetModel();
this->points = points;
endResetModel();
}
int PointsTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED( parent );
return points.size();
}
int PointsTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED( parent );
return 2;
}
QVariant PointsTableModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::DisplayRole || role == Qt::EditRole){
switch(index.column()){
case 0:
return points[index.row()].x();
case 1:
return points[index.row()].y();
default:
;
}
}
return QVariant();
}
bool PointsTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(role == Qt::EditRole){
switch (index.column()) {
case 0:
points[index.row()].setX(value.toDouble());
emit dataChanged(index,index);
return true;
case 1:
points[index.row()].setY(value.toDouble());
emit dataChanged(index,index);
return true;
default:
;
}
}
return false;
}
QVariant PointsTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole){
if(orientation == Qt::Horizontal){
switch (section) {
case 0:
return tr("X");
case 1:
return tr("Y");
default:
;
}
}else{
return QString::number(section);
}
}
return QVariant();
}
Qt::ItemFlags PointsTableModel::flags(const QModelIndex &index) const
{
Q_UNUSED(index);
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ;
}
void PointsTableModel::appendPoint()
{
beginResetModel();
points.push_back(std::move(QPointF()));
endResetModel();
}
void PointsTableModel::removePoint(uint pos)
{
beginResetModel();
points.removeAt(pos);
endResetModel();
}