| 1 |
/* |
| 2 |
* Copyright 2014 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Florian Boucault <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "upmtexturefromimage.h" |
| 20 |
|
| 21 |
#include <QtQuick/QQuickWindow> |
| 22 |
|
| 23 |
UPMTextureFromImageTextureProvider::UPMTextureFromImageTextureProvider() : |
| 24 |
QSGTextureProvider(), |
| 25 |
m_texture(NULL) |
| 26 |
{ |
| 27 |
} |
| 28 |
|
| 29 |
UPMTextureFromImageTextureProvider::~UPMTextureFromImageTextureProvider() |
| 30 |
{ |
| 31 |
if (m_texture != NULL) { |
| 32 |
delete m_texture; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
QSGTexture* UPMTextureFromImageTextureProvider::texture() const |
| 37 |
{ |
| 38 |
return m_texture; |
| 39 |
} |
| 40 |
|
| 41 |
void UPMTextureFromImageTextureProvider::setTexture(QSGTexture* texture) |
| 42 |
{ |
| 43 |
if (m_texture != NULL) { |
| 44 |
delete m_texture; |
| 45 |
} |
| 46 |
m_texture = texture; |
| 47 |
|
| 48 |
// FIXME: hardcoded flag |
| 49 |
if (m_texture != NULL) { |
| 50 |
m_texture->setHorizontalWrapMode(QSGTexture::Repeat); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
UPMTextureFromImage::UPMTextureFromImage(QQuickItem* parent) : |
| 57 |
QQuickItem(parent), |
| 58 |
m_textureProvider(NULL), |
| 59 |
m_textureNeedsUpdate(true) |
| 60 |
{ |
| 61 |
setFlag(QQuickItem::ItemHasContents); |
| 62 |
} |
| 63 |
|
| 64 |
UPMTextureFromImage::~UPMTextureFromImage() |
| 65 |
{ |
| 66 |
if (m_textureProvider != NULL) { |
| 67 |
m_textureProvider->deleteLater(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
bool UPMTextureFromImage::isTextureProvider() const |
| 72 |
{ |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
QSGTextureProvider* UPMTextureFromImage::textureProvider() const |
| 77 |
{ |
| 78 |
if (m_textureProvider == NULL) { |
| 79 |
const_cast<UPMTextureFromImage*>(this)->m_textureProvider = new UPMTextureFromImageTextureProvider; |
| 80 |
m_textureProvider->setTexture(window()->createTextureFromImage(m_image)); |
| 81 |
} |
| 82 |
return m_textureProvider; |
| 83 |
} |
| 84 |
|
| 85 |
QSGNode* UPMTextureFromImage::updatePaintNode(QSGNode* oldNode, UpdatePaintNodeData* updatePaintNodeData) |
| 86 |
{ |
| 87 |
Q_UNUSED(oldNode) |
| 88 |
Q_UNUSED(updatePaintNodeData) |
| 89 |
|
| 90 |
if (m_textureNeedsUpdate && m_textureProvider != NULL) { |
| 91 |
m_textureProvider->setTexture(window()->createTextureFromImage(m_image)); |
| 92 |
m_textureNeedsUpdate = false; |
| 93 |
} |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
QImage UPMTextureFromImage::image() const |
| 98 |
{ |
| 99 |
return m_image; |
| 100 |
} |
| 101 |
|
| 102 |
void UPMTextureFromImage::setImage(QImage image) |
| 103 |
{ |
| 104 |
if (image != m_image) { |
| 105 |
m_image = image; |
| 106 |
Q_EMIT imageChanged(); |
| 107 |
m_textureNeedsUpdate = true; |
| 108 |
update(); |
| 109 |
} |
| 110 |
} |