forked from GLDsuh-a/qt-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layertablemodel.cpp
199 lines (165 loc) · 4.16 KB
/
layertablemodel.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
189
190
191
192
193
194
195
196
197
198
#include "layertablemodel.h"
LayerTableModel::LayerTableModel(QObject *parent)
: QAbstractTableModel(parent)
{
QImage image("images/sample.jpg");
layerList.reserve(3);
selectedRow = 0;
for (int i = 0; i < 5; i )
{
addItem(QString(), image, true);
}
}
LayerTableModel::~LayerTableModel()
{
}
QVariant LayerTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
{
return QVariant();
}
int column = index.column();
if (column == 0)
{
if(role == Qt::CheckStateRole)
{
return layerList.at(index.row()).isShow ? Qt::Checked : Qt::Unchecked;
}
if (role == Qt::SizeHintRole)
{
return QSize(20, 50);
}
}
else
{
if (role == Qt::EditRole)
{
return QVariant(layerList.at(index.row()).layerName);
}
if (role == Qt::DisplayRole)
{
return QVariant(layerList.at(index.row()).layerName);
}
if (role == Qt::DecorationRole)
{
if (layerList.at(index.row()).thumbnail.isNull())
{
return layerList.at(index.row()).thumbnail;
}
else
{
return layerList.at(index.row()).thumbnail.scaledToHeight(40);
}
}
if (role == Qt::SizeHintRole)
{
return QSize(200, 50);
}
if (role == Qt::TextAlignmentRole)
{
return int(Qt::AlignVCenter);
}
}
return QVariant();
}
int LayerTableModel::rowCount(const QModelIndex &parent) const
{
return (parent.isValid() && parent.column() != 0) ? 0 : layerList.size();
}
int LayerTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant LayerTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
return QString::number(section);
}
//if (role == Qt::DecorationRole)
//return QVariant::fromValue(services);
return QAbstractItemModel::headerData(section, orientation, role);
}
Qt::ItemFlags LayerTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
{
return 0;
}
if (index.column() == 0)
{
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
bool LayerTableModel::setData(const QModelIndex &index, const
QVariant &value, int role)
{
if (!index.isValid())
{
return false;
}
if (role == Qt::CheckStateRole && value.type() == QVariant::Bool)
{
layerList[index.row()].isShow = value.toBool();
emit(dataChanged(index, index));
return true;
}
if (role == Qt::EditRole && index.column() == 1)
{
layerList[index.row()].layerName = value.toString();
emit(dataChanged(index, index));
return true;
}
return false;;
}
void LayerTableModel::changeLayerVisibility(const QModelIndex& index)
{
if (index.isValid()&&index.column() == 0)
{
setData(index, !(layerList.at(index.row()).isShow), Qt::CheckStateRole);
}
}
void LayerTableModel::deleteItem(int index)
{
QList<LayerItem>::iterator it = layerList.begin();
layerList.erase(it index);
}
void LayerTableModel::addItem(QString &name, QImage &thumb, bool show)
{
LayerItem item;
if (name.size() == 0)
{
item.layerName = QString("Layer ") QString::number(layerList.size());
}
else
{
item.layerName = name;
}
item.isShow = show;
item.thumbnail = thumb;
layerList.append(item);
//this->insertRow()
//emit(dataChanged(index, index));
qDebug() << layerList.size();
}
void LayerTableModel::refreshModel()
{
beginResetModel();
endResetModel();
//emit updateCount(this->rowCount(QModelIndex()));
}
QModelIndex& LayerTableModel::selecttedIndex(int row)
{
return createIndex(row, 1);
}
void LayerTableModel::setSelecttedRow(int row)
{
selectedRow = row;
}
int LayerTableModel::getSelecttedRow() const
{
return selectedRow;
}