KDeclarative

qiconitem.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "qiconitem.h"
9 
10 #include <QQuickWindow>
11 #include <QIcon>
12 #include <quickaddons/imagetexturescache.h>
13 #include <quickaddons/managedtexturenode.h>
14 
15 Q_GLOBAL_STATIC(ImageTexturesCache, s_iconImageCache)
16 
17 QIconItem::QIconItem(QQuickItem *parent)
18  : QQuickItem(parent),
19  m_smooth(false),
20  m_state(DefaultState),
21  m_changed(false)
22 {
23  setFlag(ItemHasContents, true);
24 }
25 
26 
27 QIconItem::~QIconItem()
28 {
29 }
30 
31 void QIconItem::setIcon(const QVariant &icon)
32 {
33  if(icon.canConvert<QIcon>()) {
34  m_icon = icon.value<QIcon>();
35  } else if(icon.canConvert<QString>()) {
36  m_icon = QIcon::fromTheme(icon.toString());
37  } else {
38  m_icon = QIcon();
39  }
40  m_changed = true;
41  update();
42  Q_EMIT iconChanged();
43 }
44 
45 QIcon QIconItem::icon() const
46 {
47  return m_icon;
48 }
49 
50 QIconItem::State QIconItem::state() const
51 {
52  return m_state;
53 }
54 
55 void QIconItem::setState(QIconItem::State state)
56 {
57  if (m_state == state) {
58  return;
59  }
60 
61  m_state = state;
62  m_changed = true;
63  Q_EMIT stateChanged(state);
64  update();
65 }
66 
67 bool QIconItem::enabled() const
68 {
69  return (m_state == DefaultState);
70 }
71 
72 void QIconItem::setEnabled(bool enabled)
73 {
74  if (enabled) {
75  setState(DefaultState);
76  } else {
77  setState(DisabledState);
78  }
79 }
80 
81 int QIconItem::implicitWidth() const
82 {
83  return 32;
84 }
85 
86 int QIconItem::implicitHeight() const
87 {
88  return 32;
89 }
90 
91 void QIconItem::setSmooth(const bool smooth)
92 {
93  if (smooth == m_smooth) {
94  return;
95  }
96  m_smooth = smooth;
97  m_changed = true;
98  update();
99  Q_EMIT smoothChanged();
100 }
101 
102 bool QIconItem::smooth() const
103 {
104  return m_smooth;
105 }
106 
107 QSGNode* QIconItem::updatePaintNode(QSGNode* node, QQuickItem::UpdatePaintNodeData* /*data*/)
108 {
109  if (m_icon.isNull()) {
110  delete node;
111  return nullptr;
112  }
113 
114  if (m_changed || node == nullptr) {
115  m_changed = false;
116 
117  ManagedTextureNode* mNode = dynamic_cast<ManagedTextureNode*>(node);
118  if(!mNode) {
119  delete node;
120  mNode = new ManagedTextureNode;
121  }
122 
123  QIcon::Mode mode;
124  switch(m_state) {
125  case DefaultState:
126  mode = QIcon::Normal;
127  break;
128  case ActiveState:
129  mode = QIcon::Active;
130  break;
131  case DisabledState:
132  mode = QIcon::Disabled;
133  break;
134  case SelectedState:
135  mode = QIcon::Selected;
136  break;
137  }
138 
139  QImage img;
140  const QSize size(width(), height());
141  if (!size.isEmpty()) {
142  img = m_icon.pixmap(size, mode, QIcon::On).toImage();
143  }
144  mNode->setTexture(s_iconImageCache->loadTexture(window(), img));
145  mNode->setRect(QRect(QPoint(0,0), size));
146  node = mNode;
147  }
148 
149  return node;
150 }
151 
152 void QIconItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
153 {
154  if (newGeometry.size() != oldGeometry.size()) {
155  m_changed = true;
156  update();
157  }
158  QQuickItem::geometryChanged(newGeometry, oldGeometry);
159 }
Helps to manage textures by creating images and reference counts them.
bool canConvert(int targetTypeId) const const
KJOBWIDGETS_EXPORT QWidget * window(KJob *job)
void setRect(const QRectF &r)
Node that contains a reference counted texture.
QSizeF size() const const
T value() const const
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
void setState(const QString &)
virtual QSGNode * updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *updatePaintNodeData)
void update(Part *part, const QByteArray &data, qint64 dataSize)
QIcon fromTheme(const QString &name)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.