• Skip to content
  • Skip to link menu
Brand

API Documentation

  1. KDE API Reference
  2. Kirigami
  • KDE Home
  • Contact Us

Quick Links

Skip menu "Kirigami"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Related Pages

Class Picker

About

QtQuick plugins to build user interfaces based on the KDE UX guidelines

Maintainer
Marco Martin
Supported platforms
Android, Linux
Community
IRC: #plasma on Freenode
Mailing list: plasma-devel
Use with CMake
find_package(KF5Kirigami)
target_link_libraries(yourapp KF5::Kirigami)
Clone
git clone git://anongit.kde.org/kirigami1.git
Browse source
Kirigami on cgit.kde.org

Kirigami

  • src
desktopicon.cpp
1 /*
2  * Copyright 2011 Marco Martin <mart@kde.org>
3  * Copyright 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Library General Public License as
7  * published by the Free Software Foundation; either version 2, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #include "desktopicon.h"
22 
23 #include <QSGSimpleTextureNode>
24 #include <qquickwindow.h>
25 #include <QIcon>
26 #include <QSGTexture>
27 #include <QDebug>
28 #include <QSGSimpleTextureNode>
29 #include <QSGTexture>
30 #include <QSharedPointer>
31 
32 
33 class ManagedTextureNode : public QSGSimpleTextureNode
34 {
35 Q_DISABLE_COPY(ManagedTextureNode)
36 public:
37  ManagedTextureNode();
38 
39  void setTexture(QSharedPointer<QSGTexture> texture);
40 
41 private:
42  QSharedPointer<QSGTexture> m_texture;
43 };
44 
45 ManagedTextureNode::ManagedTextureNode()
46 {}
47 
48 void ManagedTextureNode::setTexture(QSharedPointer<QSGTexture> texture)
49 {
50  m_texture = texture;
51  QSGSimpleTextureNode::setTexture(texture.data());
52 }
53 
54 typedef QHash<qint64, QHash<QWindow*, QWeakPointer<QSGTexture> > > TexturesCache;
55 
56 struct ImageTexturesCachePrivate
57 {
58  TexturesCache cache;
59 };
60 
61 class ImageTexturesCache
62 {
63 public:
64  ImageTexturesCache();
65  ~ImageTexturesCache();
66 
73  QSharedPointer<QSGTexture> loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options);
74 
75  QSharedPointer<QSGTexture> loadTexture(QQuickWindow *window, const QImage &image);
76 
77 
78 private:
79  QScopedPointer<ImageTexturesCachePrivate> d;
80 };
81 
82 
83 ImageTexturesCache::ImageTexturesCache()
84  : d(new ImageTexturesCachePrivate)
85 {
86 }
87 
88 ImageTexturesCache::~ImageTexturesCache()
89 {
90 }
91 
92 QSharedPointer<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image, QQuickWindow::CreateTextureOptions options)
93 {
94  qint64 id = image.cacheKey();
95  QSharedPointer<QSGTexture> texture = d->cache.value(id).value(window).toStrongRef();
96 
97  if (!texture) {
98  auto cleanAndDelete = [this, window, id](QSGTexture* texture) {
99  QHash<QWindow*, QWeakPointer<QSGTexture> >& textures = (d->cache)[id];
100  textures.remove(window);
101  if (textures.isEmpty())
102  d->cache.remove(id);
103  delete texture;
104  };
105  texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options), cleanAndDelete);
106  (d->cache)[id][window] = texture.toWeakRef();
107  }
108 
109  //if we have a cache in an atlas but our request cannot use an atlassed texture
110  //create a new texture and use that
111  //don't use removedFromAtlas() as that requires keeping a reference to the non atlased version
112  if (!(options & QQuickWindow::TextureCanUseAtlas) && texture->isAtlasTexture()) {
113  texture = QSharedPointer<QSGTexture>(window->createTextureFromImage(image, options));
114  }
115 
116  return texture;
117 }
118 
119 QSharedPointer<QSGTexture> ImageTexturesCache::loadTexture(QQuickWindow *window, const QImage &image)
120 {
121  return loadTexture(window, image, 0);
122 }
123 
124 Q_GLOBAL_STATIC(ImageTexturesCache, s_iconImageCache)
125 
126 DesktopIcon::DesktopIcon(QQuickItem *parent)
127  : QQuickItem(parent),
128  m_smooth(false),
129  m_changed(false),
130  m_active(false),
131  m_selected(false)
132 {
133  setFlag(ItemHasContents, true);
134 }
135 
136 
137 DesktopIcon::~DesktopIcon()
138 {
139 }
140 
141 void DesktopIcon::setSource(const QVariant &icon)
142 {
143  if(icon.canConvert<QIcon>()) {
144  m_icon = icon.value<QIcon>();
145  } else if(icon.canConvert<QString>()) {
146  m_icon = QIcon::fromTheme(icon.toString());
147  } else {
148  m_icon = QIcon();
149  }
150  m_changed = true;
151  update();
152  emit sourceChanged();
153 }
154 
155 QIcon DesktopIcon::source() const
156 {
157  return m_icon;
158 }
159 
160 void DesktopIcon::setEnabled(const bool enabled)
161 {
162  if (enabled == QQuickItem::isEnabled()) {
163  return;
164  }
165  QQuickItem::setEnabled(enabled);
166  m_changed = true;
167  update();
168  emit enabledChanged();
169 }
170 
171 
172 void DesktopIcon::setActive(const bool active)
173 {
174  if (active == m_active) {
175  return;
176  }
177  m_active = active;
178  m_changed = true;
179  update();
180  emit activeChanged();
181 }
182 
183 bool DesktopIcon::active() const
184 {
185  return m_active;
186 }
187 
188 bool DesktopIcon::valid() const
189 {
190  return !m_icon.isNull();
191 }
192 
193 void DesktopIcon::setSelected(const bool selected)
194 {
195  if (selected == m_selected) {
196  return;
197  }
198  m_selected = selected;
199  m_changed = true;
200  update();
201  emit selectedChanged();
202 }
203 
204 bool DesktopIcon::selected() const
205 {
206  return m_selected;
207 }
208 
209 int DesktopIcon::implicitWidth() const
210 {
211  return 32;
212 }
213 
214 int DesktopIcon::implicitHeight() const
215 {
216  return 32;
217 }
218 
219 void DesktopIcon::setSmooth(const bool smooth)
220 {
221  if (smooth == m_smooth) {
222  return;
223  }
224  m_smooth = smooth;
225  m_changed = true;
226  update();
227  emit smoothChanged();
228 }
229 
230 bool DesktopIcon::smooth() const
231 {
232  return m_smooth;
233 }
234 
235 QSGNode* DesktopIcon::updatePaintNode(QSGNode* node, QQuickItem::UpdatePaintNodeData* /*data*/)
236 {
237  if (m_icon.isNull()) {
238  delete node;
239  return Q_NULLPTR;
240  }
241 
242  if (m_changed || node == 0) {
243  m_changed = false;
244 
245  ManagedTextureNode* mNode = dynamic_cast<ManagedTextureNode*>(node);
246  if(!mNode) {
247  delete node;
248  mNode = new ManagedTextureNode;
249  }
250 
251  QIcon::Mode mode = QIcon::Normal;
252  if (!isEnabled()) {
253  mode = QIcon::Disabled;
254  } else if (m_selected) {
255  mode = QIcon::Selected;
256  } else if (m_active) {
257  mode = QIcon::Active;
258  }
259 
260  QImage img;
261  const QSize size(width(), height());
262  if (!size.isEmpty()) {
263  img = m_icon.pixmap(size, mode, QIcon::On).toImage();
264  }
265  mNode->setTexture(s_iconImageCache->loadTexture(window(), img));
266  mNode->setRect(QRect(QPoint(0,0), size));
267  node = mNode;
268  }
269 
270  return node;
271 }
272 
273 void DesktopIcon::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
274 {
275  if (newGeometry.size() != oldGeometry.size()) {
276  m_changed = true;
277  update();
278  }
279  QQuickItem::geometryChanged(newGeometry, oldGeometry);
280 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2017 The KDE developers.
Generated on Fri Feb 17 2017 11:09:23 by doxygen 1.8.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal