-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathheightmaptablemodel.cpp
94 lines (72 loc) · 2.31 KB
/
heightmaptablemodel.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
// This file is a part of "grblControl" application.
// Copyright 2015 Hayrullin Denis Ravilevich
#include "heightmaptablemodel.h"
HeightMapTableModel::HeightMapTableModel(QObject *parent) : QAbstractTableModel(parent)
{
m_data.append(QVector<double>());
}
void HeightMapTableModel::resize(int cols, int rows)
{
foreach (QVector<double> row, m_data) row.clear();
m_data.clear();
for (int i = 0; i < rows; i++) {
QVector<double> row;
for (int j = 0; j < cols; j++) {
row.append(NAN);
}
m_data.append(row);
}
}
QVariant HeightMapTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (index.row() >= m_data.count() || index.column() >= m_data[0].count()) return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
return QString::number(m_data[(m_data.count() - 1) - index.row()][index.column()], 'f', 3);
}
if (role == Qt::UserRole) {
return m_data[index.row()][index.column()];
}
if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
}
return QVariant();
}
bool HeightMapTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
m_data[role == Qt::EditRole ? (m_data.count() - 1) - index.row() : index.row()][index.column()] = value.toDouble();
if (role == Qt::EditRole) emit dataChangedByUserInput();
return true;
}
bool HeightMapTableModel::insertRow(int row, const QModelIndex &parent)
{
m_data.insert(row, QVector<double>());
return true;
}
bool HeightMapTableModel::removeRow(int row, const QModelIndex &parent)
{
m_data.remove(row);
return true;
}
void HeightMapTableModel::clear()
{
m_data.clear();
}
int HeightMapTableModel::rowCount(const QModelIndex &parent) const
{
return m_data.count();
}
int HeightMapTableModel::columnCount(const QModelIndex &parent) const
{
return m_data[0].count();
}
QVariant HeightMapTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QVariant();
return QString::number(section + 1);
}
Qt::ItemFlags HeightMapTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) return NULL;
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
}