forked from andijakl/nfcinteractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagimagecache.cpp
72 lines (59 loc) · 2.07 KB
/
tagimagecache.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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Andreas Jakl ([email protected])
**
** Released under Nokia Example Code License.
** See license.txt in the main project folder.
**
****************************************************************************/
#include "tagimagecache.h"
TagImageCache::TagImageCache() :
QDeclarativeImageProvider(QDeclarativeImageProvider::Image)
{
}
TagImageCache::~TagImageCache()
{
}
/*!
\brief Add an image to the cache. Returns the id, which can then be requested from QML.
\param img the image to add to the cache.
*/
int TagImageCache::addImage(QImage img)
{
// Add the image to our list to cache it
m_imageCache.append(img);
return m_imageCache.size() - 1;
}
/*!
\brief Request an image from the cache by specifying its \a id.
Request the image if possible in \a requestedSize. The image will be scaled
keeping its aspect ratio. The final size is stored in \a size.
\return the requested image from the cache, or an empty QImage() otherwise.
*/
QImage TagImageCache::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
qDebug() << "TagImageCache: Requesting image Id: " << id << ", requested size: " << requestedSize;
// Convert id to a number
bool ok;
int imgId = id.toInt(&ok);
if (!ok)
// Conversion to a number failed
return QImage();
// See if we have an image stored with the requested ID
if (imgId < 0 || imgId >= m_imageCache.size())
// Out of range
return QImage();
QImage finalImg;
if (requestedSize.isValid())
{
// Need to resize the image
finalImg = m_imageCache.at(imgId).scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
} else {
// Send back the original image
finalImg = m_imageCache.at(imgId);
}
*size = finalImg.size();
return finalImg;
}