-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagemodel.cpp
More file actions
78 lines (65 loc) · 1.61 KB
/
Copy pathimagemodel.cpp
File metadata and controls
78 lines (65 loc) · 1.61 KB
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
#include "imagemodel.h"
ImageModel::ImageModel(QObject *parent) :
images{},
mThumbnails()
{
}
int ImageModel::rowCount(const QModelIndex &parent) const
{
return images.size();
}
QVariant ImageModel::data(const QModelIndex &index, int role) const
{
if (!isIndexValid(index)) {
return QVariant();
}
const Image& picture = *images.at(index.row());
switch (role) {
case Qt::DisplayRole:
return QString::fromStdString(picture.fileUrl());
break;
case Qt::DecorationRole: {
auto filepath = QString::fromStdString(picture.fileUrl());
return *mThumbnails[filepath];
break;
}
case Qt::SizeHintRole:
return QSize(picture.width(), picture.height()) ;
break;
default:
return QVariant();
}
}
QHash<int, QByteArray> ImageModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "filepathToDisplay";
roles[Qt::SizeHintRole] = "size";
return roles;
}
void ImageModel::setImageList(std::vector<Image*> newImages)
{
beginResetModel();
images = newImages;
endResetModel();
}
void ImageModel::resetThumbnails()
{
qDeleteAll(mThumbnails);
mThumbnails.clear();
}
void ImageModel::generateThumbnail(QString filePath, QSize size)
{
QPixmap original(filePath);
auto thumbnail = new QPixmap(original.scaled(size));
mThumbnails.insert(filePath, thumbnail);
}
bool ImageModel::isIndexValid(const QModelIndex& index) const
{
if (index.row() < 0
|| index.row() >= rowCount()
|| !index.isValid()) {
return false;
}
return true;
}