-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcptmodel.cpp
188 lines (171 loc) · 4.91 KB
/
cptmodel.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
/**
* \author trungvv1
*
* \date 4/1/2015
* \class CptModel
*
* \brief write something about your class
*
*
*/
#include "cptmodel.h"
#include <QColor>
CptModel::CptModel(QObject* parent)
: QAbstractTableModel(parent)
{
mValues = nullptr;
initValues(1, 2, 0);
}
QVariant CptModel::data(const QModelIndex &index, int role) const
{
int row = index.row();
int col = index.column();
if (index.isValid())
{
if (role == Qt::DisplayRole) {
return mValues[row][col];
} else if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
} else if (role == Qt::BackgroundRole && col < mNodeparents) {
return QColor(180,255,255);
}
}
return QVariant();
}
bool CptModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
if (value.toString().isEmpty()) {
return false;
}
double newValue = value.toDouble();
if (newValue == mValues[index.row()][index.column()] || index.column() < mNodeparents) {
return false;
}
mValues[index.row()][index.column()] = newValue;
if (mNColumns-mNodeparents == 2)
{
mValues[index.row()][mNColumns-1-(index.column()-mNodeparents)] = 1 - newValue;
}
emit dataChanged(index, index);
return true;
// double sumProb = 0;
// for (int i = nparents; i < index.column(); ++i) {
// sumProb += values[index.row()][i];
// }
// double oldValue = values[index.row()][index.column()];
// if (sumProb + newValue <= 1) {
// values[index.row()][index.column()] = newValue;
// if (index.column() == ncolumns-1) {
// values[index.row()][index.column()-1] -= (newValue - oldValue);
// } else {
// values[index.row()][index.column()+1] -= (newValue - oldValue);
// }
// emit dataChanged(index, index);
// return true;
// } else if (index.column() == ncolumns-1) {
// if ( values[index.row()][index.column()-1] > (newValue-oldValue) ) {
// values[index.row()][index.column()] = newValue;
// values[index.row()][index.column()-1] -= (newValue - oldValue);
// }
// emit dataChanged(index, index);
// return true;
// }
}
return false;
}
QVariant CptModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal)
{
if(section >=0 && section < mHeaders.count())
return mHeaders[section];
}
} else if (role == Qt::TextAlignmentRole) {
return Qt::AlignCenter;
} else if (role == Qt::BackgroundRole) {
if (orientation == Qt::Horizontal) {
if (section < mNodeparents) {
return QColor(120,210,210);
} else {
return QColor(150,150,250);
}
}
}
return QVariant();
}
int CptModel::rowCount(const QModelIndex &parent) const
{
return mNRows;
}
int CptModel::columnCount(const QModelIndex &parent) const
{
return mNColumns;
}
bool CptModel::insertRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginInsertRows(QModelIndex(), row, row + count - 1);
endInsertRows();
return true;
}
bool CptModel::removeRows(int row, int count, const QModelIndex &parent)
{
Q_UNUSED(parent);
beginRemoveRows(QModelIndex(), row, row + count - 1);
endRemoveRows();
return true;
}
Qt::ItemFlags CptModel::flags(const QModelIndex &index) const
{
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
void CptModel::initValues(int nrows, int ncolumns, int nparents)
{
beginResetModel();
/// delete old data
clear();
///
this->mNRows = nrows;
this->mNColumns = ncolumns;
this->mNodeparents = nparents;
mValues = new double*[nrows];
for (int i = 0; i < nrows; ++i) {
mValues[i] = new double[ncolumns];
for (int j = 0; j < ncolumns; ++j) {
mValues[i][j] = 0;
// mValues[i][j] = 1.0 / mNColumns;
}
// values[i][0] = 1;
}
endResetModel();
}
void CptModel::setValue(int row, int column, double value)
{
mValues[row][column] = value;
}
double CptModel::getValue(int row, int column)
{
if (0 <= row && row < mNRows && 0 <= column && column < mNColumns)
return mValues[row][column];
else
return 0;
}
void CptModel::setHeader(int column, QString value)
{
mHeaders.insert(column, value);
}
void CptModel::clear()
{
if (mValues) {
for (int i = 0; i < this->mNRows; ++i) {
delete mValues[i];
mValues[i] = nullptr;
}
delete mValues;
mValues = nullptr;
}
mHeaders.clear();
mNRows = mNColumns = mNodeparents = 0;
}