File indexing completed on 2024-04-28 16:13:35
0001 /* 0002 * This file is part of the KDE project 0003 * 0004 * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl> 0005 * 0006 * This library is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU Library General Public 0008 * License as published by the Free Software Foundation; either 0009 * version 2 of the License, or (at your option) any later version. 0010 * 0011 * This library is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 * Library General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU Library General Public License 0017 * along with this library; see the file COPYING.LIB. If not, write to 0018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0019 * Boston, MA 02110-1301, USA. 0020 * 0021 */ 0022 0023 #include "ImageDataItem.h" 0024 0025 #include <QPixmap> 0026 #include <QSGSimpleTextureNode> 0027 #include <QQuickWindow> 0028 0029 using namespace Calligra::Components; 0030 0031 class ImageDataItem::Private 0032 { 0033 public: 0034 Private() : imageChanged(false) 0035 { } 0036 0037 QImage data; 0038 bool imageChanged; 0039 }; 0040 0041 ImageDataItem::ImageDataItem(QQuickItem* parent) 0042 : QQuickItem{parent}, d{new Private} 0043 { 0044 setFlag(QQuickItem::ItemHasContents, true); 0045 } 0046 0047 ImageDataItem::~ImageDataItem() 0048 { 0049 delete d; 0050 } 0051 0052 QImage ImageDataItem::data() const 0053 { 0054 return d->data; 0055 } 0056 0057 void ImageDataItem::setData(const QImage& newValue) 0058 { 0059 if(newValue != d->data) { 0060 d->data = newValue; 0061 setImplicitWidth(d->data.width()); 0062 setImplicitHeight(d->data.height()); 0063 d->imageChanged = true; 0064 update(); 0065 emit dataChanged(); 0066 } 0067 } 0068 0069 QSGNode* ImageDataItem::updatePaintNode(QSGNode* node, QQuickItem::UpdatePaintNodeData*) 0070 { 0071 if(d->data.isNull()) { 0072 return node; 0073 } 0074 0075 float w = widthValid() ? width() : d->data.width(); 0076 float h = heightValid() ? height() : d->data.height(); 0077 0078 auto texNode = static_cast<QSGSimpleTextureNode*>(node); 0079 if(!texNode) { 0080 texNode = new QSGSimpleTextureNode{}; 0081 } 0082 texNode->setRect(0, 0, w, h); 0083 0084 if (!texNode->texture() || d->imageChanged) { 0085 delete texNode->texture(); 0086 auto texture = window()->createTextureFromImage(d->data); 0087 texNode->setTexture(texture); 0088 d->imageChanged = false; 0089 } 0090 0091 return texNode; 0092 }