-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimagemodel.cpp
326 lines (280 loc) · 7.88 KB
/
imagemodel.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "imagemodel.h"
#include <QBuffer>
#include <QDebug>
#include <QIcon>
#include <QPixmap>
#include <QVariant>
#define TOOLTIP_SIZE 256
ImageModel::ImageModel(QObject* parent) : QAbstractTableModel(parent)
{
m_column << tr("Key") << tr("Filename") << tr("Is Background");
}
QVariant ImageModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(Qt::Horizontal == orientation)
{
if(Qt::DisplayRole == role)
{
return m_column.at(section);
}
}
return QVariant();
}
int ImageModel::rowCount(const QModelIndex& parent) const
{
if(parent.isValid())
return 0;
return static_cast<int>(m_data.size());
}
int ImageModel::columnCount(const QModelIndex& parent) const
{
if(parent.isValid())
return 0;
return m_column.size();
}
QVariant ImageModel::data(const QModelIndex& index, int role) const
{
if(!index.isValid())
return QVariant();
QVariant var;
const auto& info= m_data[static_cast<std::size_t>(index.row())];
auto trueRole= role;
auto col= index.column();
if(Qt::ToolTipRole == role)
{
auto key= info.key;
const auto& image= info.pixmap;
auto pixmap= image.scaledToWidth(TOOLTIP_SIZE);
QByteArray data;
QBuffer buffer(&data);
pixmap.save(&buffer, "PNG", 100);
return QVariant::fromValue(QString("<img src='data:image/png;base64, %0'>").arg(QString(data.toBase64())));
}
std::vector<int> ap({Qt::EditRole, Qt::DisplayRole, FilenameRole, BackgroundRole, UrlRole, KeyRole});
if(std::find(ap.begin(), ap.end(), role) == ap.end())
return {};
if(role == Qt::EditRole || role == Qt::DisplayRole)
{
if(col == 0)
trueRole= (Qt::EditRole == role) ? KeyRole : UrlRole;
else if(col == 1)
trueRole= FilenameRole;
else if(col == 2)
trueRole= BackgroundRole;
}
else
trueRole= role;
switch(trueRole)
{
case KeyRole:
var= info.key;
break;
case UrlRole:
var= QStringLiteral("image://rcs/%1").arg(info.key);
break;
case FilenameRole:
var= info.filename;
break;
case BackgroundRole:
var= info.isBackground;
break;
}
return var;
}
bool ImageModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
bool val= false;
if((Qt::DisplayRole == role) || (Qt::EditRole == role))
{
auto& info= m_data[static_cast<std::size_t>(index.row())];
switch(index.column())
{
case 0:
info.key= value.toString();
val= true;
break;
case 1:
info.isBackground= value.toBool();
val= true;
break;
case 2:
info.filename= value.toString();
break;
}
}
return val;
}
void ImageModel::save(QJsonArray& array) const
{
for(const auto& info : m_data)
{
QJsonObject oj;
QByteArray bytes;
QBuffer buffer(&bytes);
buffer.open(QIODevice::WriteOnly);
info.pixmap.save(&buffer, "PNG");
oj["bin"]= QString(buffer.data().toBase64());
oj["key"]= info.key;
oj["isBg"]= info.isBackground;
oj["filename"]= info.filename;
array.append(oj);
}
}
bool ImageModel::insertImage(const QPixmap& pix, const QString& key, const QString& filename, bool isBg)
{
auto rect= pix.rect();
if(isBg && !m_data.empty())
{
auto b= std::all_of(m_data.begin(), m_data.end(), [rect](const ImageInfo& info) {
return info.isBackground ? rect == info.pixmap.rect() : true;
});
if(!b)
return false;
}
auto it= std::find_if(m_data.begin(), m_data.end(), [key](const ImageInfo& info) { return info.key == key; });
if(it != m_data.end()) // already set
{
it->pixmap= pix;
it->filename= filename;
it->isBackground= isBg;
emit internalDataChanged();
return true;
}
ImageInfo info{pix, isBg, filename, key};
auto size= static_cast<int>(m_data.size());
beginInsertRows(QModelIndex(), size, size);
m_data.push_back(info);
endInsertRows();
emit internalDataChanged();
return true;
}
Qt::ItemFlags ImageModel::flags(const QModelIndex& index) const
{
if(index.column() == 0 || index.column() == 1)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
else
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
}
void ImageModel::clear()
{
beginResetModel();
m_data.clear();
endResetModel();
}
QPixmap ImageModel::pixmapFromKey(QString id)
{
auto it= std::find_if(m_data.begin(), m_data.end(), [id](const ImageInfo& info) { return id == info.key; });
if(it == m_data.end())
return {};
return it->pixmap;
}
bool ImageModel::isBackgroundById(QString id) const
{
auto it= std::find_if(m_data.begin(), m_data.end(), [id](const ImageInfo& info) { return id == info.key; });
return it != m_data.end();
}
void ImageModel::removeImageAt(const QModelIndex& index)
{
if(!index.isValid())
return;
auto i= index.row();
removeImage(i);
}
void ImageModel::setPathFor(const QModelIndex& idx, const QString& path)
{
if(!idx.isValid())
return;
auto row= idx.row();
if(row < 0 || row >= m_data.size())
return;
auto& info= m_data[static_cast<int>(row)];
QPixmap pix(path);
if(pix.isNull())
{
qWarning() << "Can't open image: " << path;
return;
}
info.pixmap= pix;
emit dataChanged(idx, idx, QVector<int>() << Qt::DisplayRole);
}
void ImageModel::removeImageByKey(const QString& key)
{
auto it= std::find_if(m_data.begin(), m_data.end(), [key](const ImageInfo& info) {
return key == info.key;
}); // auto index= m_keyList.indexOf(key);
if(it == m_data.end())
return; // unfound
removeImage(std::distance(m_data.begin(), it));
}
QSize ImageModel::backgroundSize() const
{
auto it= std::find_if(m_data.begin(), m_data.end(), [](const ImageInfo& info) { return info.isBackground; });
if(it == m_data.end())
return {};
return it->pixmap.rect().size();
}
void ImageModel::removeImage(int i)
{
if(i < 0 || static_cast<int>(m_data.size()) <= i)
return;
beginRemoveRows(QModelIndex(), i, i);
m_data.erase(m_data.begin() + i);
endRemoveRows();
emit internalDataChanged();
}
void ImageModel::reloadImage(const QModelIndex& idx)
{
if(!idx.isValid())
return;
auto row= idx.row();
qDebug() << "reload image" << row << m_data.size();
if(row < 0 || row >= m_data.size())
return;
qDebug() << "reload image" << row;
auto& info= m_data[static_cast<int>(row)];
QPixmap pix(info.filename);
if(pix.isNull())
{
qWarning() << "Can't open image: " << info.filename;
return;
}
qDebug() << "reload image" << row;
info.pixmap= pix;
emit dataChanged(idx, idx, QVector<int>() << Qt::DisplayRole);
}
#ifndef RCSE
void ImageModel::fill(NetworkMessageWriter& msg) const
{
msg.uint32(static_cast<unsigned int>(m_data.size()));
for(const auto& info : m_data)
{
const auto& pix= info.pixmap;
QByteArray array;
QBuffer buffer(&array);
buffer.open(QIODevice::WriteOnly);
pix.toImage().save(&buffer, "jpg");
msg.byteArray32(array);
msg.uint8(info.isBackground);
msg.string32(info.key);
msg.string32(info.filename);
}
}
void ImageModel::read(NetworkMessageReader& msg)
{
auto size= msg.uint32();
for(unsigned int i= 0; i < size; ++i)
{
ImageInfo info;
auto array= msg.byteArray32();
info.pixmap.loadFromData(array);
info.isBackground= msg.uint8();
info.key= msg.string32();
info.filename= msg.string32();
m_data.push_back(info);
}
}
#endif