-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbimageprovider.cpp
63 lines (42 loc) · 1.56 KB
/
dbimageprovider.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
#include "dbimageprovider.hpp"
#include <QtDebug>
#include <QPainter>
#include <QSqlQuery>
#include <QSqlError>
DBImageProvider::DBImageProvider() : QQuickImageProvider (QQuickImageProvider::Image)
{
}
QImage DBImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
//qDebug() << "ID:" << id << requestedSize;
auto parts = id.split("/");
//qDebug() << "IDX:" << parts[0] << "TYPE:" << parts[1];
QSqlQuery query;
if (parts[1] == "thumb") {
query.prepare(DBImageProvider::queryThumb);
} else {
query.prepare(DBImageProvider::queryImage);
}
query.bindValue(":id", parts[0]);
if (!query.exec()) {
qDebug() << "Error:" << query.lastError().text();
qFatal("Query failed");
}
query.first();
auto value = query.value(1);
auto image = QImage::fromData(value.toByteArray());
//qDebug() << "I" << parts[0] << image.width() << image.height() << image.isNull();
if (size)
*size = QSize(image.width(), image.height());
return image;
//TODO: do something with missing images
/*QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
requestedSize.height() > 0 ? requestedSize.height() : height);
pixmap.fill(Qt::red);
QPainter p(&pixmap);
p.drawText(0, 10, id);
p.end();
return pixmap.toImage();*/
}
const QString DBImageProvider::queryThumb = "SELECT id, thumb FROM recipe WHERE id == :id";
const QString DBImageProvider::queryImage = "SELECT id, image FROM recipe WHERE id == :id";