KIconThemes

kiconengine.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2006 Hamish Rodda <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library 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 GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "kiconengine.h"
20 
21 #include <kiconloader.h>
22 
23 #include <KIconTheme>
24 #include <QPainter>
25 
26 KIconEngine::KIconEngine(const QString &iconName, KIconLoader *iconLoader, const QStringList &overlays)
27  : mIconName(iconName),
28  mOverlays(overlays),
29  mIconLoader(iconLoader)
30 {
31 }
32 
34  : mIconName(iconName),
35  mIconLoader(iconLoader)
36 {
37 }
38 
39 static inline int qIconModeToKIconState(QIcon::Mode mode)
40 {
41  int kstate;
42  switch (mode) {
43  case QIcon::Normal:
45  break;
46  case QIcon::Active:
47  kstate = KIconLoader::ActiveState;
48  break;
49  case QIcon::Disabled:
51  break;
52  case QIcon::Selected:
54  break;
55  }
56  return kstate;
57 }
58 
60 {
61  Q_UNUSED(state)
62  Q_UNUSED(mode)
63  const int iconSize = qMin(size.width(), size.height());
64  return QSize(iconSize, iconSize);
65 }
66 
67 void KIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
68 {
69  if (!mIconLoader) {
70  return;
71  }
72 
73  const qreal dpr = painter->device()->devicePixelRatioF();
74  painter->drawPixmap(rect, pixmap(rect.size() * dpr, mode, state));
75 }
76 
77 QPixmap KIconEngine::createPixmap(const QSize &size, qreal scale, QIcon::Mode mode, QIcon::State state)
78 {
79  Q_UNUSED(state)
80 
81  if (scale < 1) {
82  scale = 1;
83  }
84 
85  if (size.isEmpty()) {
86  return QPixmap();
87  }
88 
89  if (!mIconLoader) {
90  QPixmap pm(size);
91  pm.setDevicePixelRatio(scale);
93  return pm;
94  }
95 
96  const QSize scaledSize = size / scale;
97 
98  const int kstate = qIconModeToKIconState(mode);
99  const int iconSize = qMin(scaledSize.width(), scaledSize.height());
100  QPixmap pix = mIconLoader.data()->loadScaledIcon(mIconName, KIconLoader::Desktop, scale, iconSize, kstate, mOverlays);
101 
102  if (pix.size() == size) {
103  return pix;
104  }
105 
106  QPixmap pix2(size);
107  auto dpr = pix.devicePixelRatio();
108  pix2.setDevicePixelRatio(dpr);
109  pix2.fill(QColor(0, 0, 0, 0));
110 
111  QPainter painter(&pix2);
112  const QPoint newTopLeft((pix2.width() - pix.width()) / (2*dpr), (pix2.height() - pix.height()) / (2*dpr));
113  painter.drawPixmap(newTopLeft, pix);
114 
115  return pix2;
116 }
117 
119 {
120  return createPixmap(size, 1 /*scale*/, mode, state);
121 }
122 
124 {
125  if (!mIconLoader || !mIconLoader->hasIcon(mIconName)) {
126  return QString();
127  }
128  return mIconName;
129 }
130 
131 Q_GLOBAL_STATIC_WITH_ARGS(QList<QSize>, sSizes, (QList<QSize>() << QSize(16, 16) << QSize(22, 22) << QSize(32, 32) << QSize(48, 48) << QSize(64, 64) << QSize(128, 128) << QSize(256, 256)))
132 
134 {
135  Q_UNUSED(mode);
136  Q_UNUSED(state);
137 
138  if (!mIconLoader) {
139  return QList<QSize>();
140  }
141 
142  const bool found = mIconLoader->hasIcon(mIconName);
143  return found ? *sSizes : QList<QSize>();
144 }
145 
146 QString KIconEngine::key() const
147 {
148  return QStringLiteral("KIconEngine");
149 }
150 
151 QIconEngine *KIconEngine::clone() const
152 {
153  return new KIconEngine(mIconName, mIconLoader.data(), mOverlays);
154 }
155 
156 bool KIconEngine::read(QDataStream &in)
157 {
158  in >> mIconName >> mOverlays;
159  return true;
160 }
161 
162 bool KIconEngine::write(QDataStream &out) const
163 {
164  out << mIconName << mOverlays;
165  return true;
166 }
167 
168 void KIconEngine::virtual_hook(int id, void *data)
169 {
170  if (id == QIconEngine::IsNullHook) {
171  *reinterpret_cast<bool*>(data) = !mIconLoader || !mIconLoader->hasIcon(mIconName);
172  }
173  if (id == QIconEngine::ScaledPixmapHook) {
174  auto *info = reinterpret_cast<ScaledPixmapArgument *>(data);
175  info->pixmap = createPixmap(info->size, info->scale, info->mode, info->state);
176  return;
177  }
178  QIconEngine::virtual_hook(id, data);
179 }
The default state.
Definition: kiconloader.h:176
QSize size() const const
int width() const const
T * data() const const
int width() const const
void fill(const QColor &color)
KIconEngine(const QString &iconName, KIconLoader *iconLoader, const QStringList &overlays)
Constructs an icon engine for a KDE named icon.
Definition: kiconengine.cpp:26
bool isEmpty() const const
QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Reimplementation.
Definition: kiconengine.cpp:59
void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override
Reimplementation.
Definition: kiconengine.cpp:67
Desktop icons.
Definition: kiconloader.h:133
QList< QSize > availableSizes(QIcon::Mode mode, QIcon::State state) const override
Reimplementation.
Icon is disabled.
Definition: kiconloader.h:178
Icon is active.
Definition: kiconloader.h:177
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
virtual void virtual_hook(int id, void *data)
QPaintDevice * device() const const
qreal devicePixelRatioF() const const
int height() const const
QString iconName() const override
Reimplementation.
int height() const const
Icon is selected.
Definition: kiconloader.h:179
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Reimplementation.
transparent
void setDevicePixelRatio(qreal scaleFactor)
Iconloader for KDE.
Definition: kiconloader.h:78
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 22:40:52 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.