KIconThemes

kiconengine.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2006 Hamish Rodda <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-only
6 */
7 
8 #include "kiconengine.h"
9 
10 #include <kiconloader.h>
11 
12 #include <KIconTheme>
13 #include <QPainter>
14 
15 KIconEngine::KIconEngine(const QString &iconName, KIconLoader *iconLoader, const QStringList &overlays)
16  : mIconName(iconName)
17  , mOverlays(overlays)
18  , mIconLoader(iconLoader)
19 {
20 }
21 
23  : mIconName(iconName)
24  , mIconLoader(iconLoader)
25 {
26 }
27 
28 static inline int qIconModeToKIconState(QIcon::Mode mode)
29 {
30  int kstate;
31  switch (mode) {
32  case QIcon::Normal:
34  break;
35  case QIcon::Active:
36  kstate = KIconLoader::ActiveState;
37  break;
38  case QIcon::Disabled:
40  break;
41  case QIcon::Selected:
43  break;
44  }
45  return kstate;
46 }
47 
49 {
50  Q_UNUSED(state)
51  Q_UNUSED(mode)
52  const int iconSize = qMin(size.width(), size.height());
53  return QSize(iconSize, iconSize);
54 }
55 
56 void KIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
57 {
58  if (!mIconLoader) {
59  return;
60  }
61 
62  const qreal dpr = painter->device()->devicePixelRatioF();
63  painter->drawPixmap(rect, pixmap(rect.size() * dpr, mode, state));
64 }
65 
66 QPixmap KIconEngine::createPixmap(const QSize &size, qreal scale, QIcon::Mode mode, QIcon::State state)
67 {
68  Q_UNUSED(state)
69 
70  if (scale < 1) {
71  scale = 1;
72  }
73 
74  if (size.isEmpty()) {
75  return QPixmap();
76  }
77 
78  if (!mIconLoader) {
79  QPixmap pm(size);
80  pm.setDevicePixelRatio(scale);
82  return pm;
83  }
84 
85  const QSize scaledSize = size / scale;
86 
87  const int kstate = qIconModeToKIconState(mode);
88  const int iconSize = qMin(scaledSize.width(), scaledSize.height());
89  QPixmap pix = mIconLoader.data()->loadScaledIcon(mIconName, KIconLoader::Desktop, scale, iconSize, kstate, mOverlays);
90 
91  if (pix.size() == size) {
92  return pix;
93  }
94 
95  QPixmap pix2(size);
96  auto dpr = pix.devicePixelRatio();
97  pix2.setDevicePixelRatio(dpr);
98  pix2.fill(QColor(0, 0, 0, 0));
99 
100  QPainter painter(&pix2);
101  const QPoint newTopLeft((pix2.width() - pix.width()) / (2 * dpr), (pix2.height() - pix.height()) / (2 * dpr));
102  painter.drawPixmap(newTopLeft, pix);
103 
104  return pix2;
105 }
106 
108 {
109  return createPixmap(size, 1 /*scale*/, mode, state);
110 }
111 
113 {
114  if (!mIconLoader || !mIconLoader->hasIcon(mIconName)) {
115  return QString();
116  }
117  return mIconName;
118 }
119 
120 Q_GLOBAL_STATIC_WITH_ARGS(QList<QSize>,
121  sSizes,
122  (QList<QSize>() << QSize(16, 16) << QSize(22, 22) << QSize(32, 32) << QSize(48, 48) << QSize(64, 64) << QSize(128, 128)
123  << QSize(256, 256)))
124 
126 {
127  Q_UNUSED(mode);
128  Q_UNUSED(state);
129 
130  if (!mIconLoader) {
131  return QList<QSize>();
132  }
133 
134  const bool found = mIconLoader->hasIcon(mIconName);
135  return found ? *sSizes : QList<QSize>();
136 }
137 
138 QString KIconEngine::key() const
139 {
140  return QStringLiteral("KIconEngine");
141 }
142 
143 QIconEngine *KIconEngine::clone() const
144 {
145  return new KIconEngine(mIconName, mIconLoader.data(), mOverlays);
146 }
147 
148 bool KIconEngine::read(QDataStream &in)
149 {
150  in >> mIconName >> mOverlays;
151  return true;
152 }
153 
154 bool KIconEngine::write(QDataStream &out) const
155 {
156  out << mIconName << mOverlays;
157  return true;
158 }
159 
160 void KIconEngine::virtual_hook(int id, void *data)
161 {
162  if (id == QIconEngine::IsNullHook) {
163  *reinterpret_cast<bool *>(data) = !mIconLoader || !mIconLoader->hasIcon(mIconName);
164  }
165  if (id == QIconEngine::ScaledPixmapHook) {
166  auto *info = reinterpret_cast<ScaledPixmapArgument *>(data);
167  info->pixmap = createPixmap(info->size, info->scale, info->mode, info->state);
168  return;
169  }
170  QIconEngine::virtual_hook(id, data);
171 }
The default state.
Definition: kiconloader.h:165
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:15
bool isEmpty() const const
QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Reimplementation.
Definition: kiconengine.cpp:48
void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override
Reimplementation.
Definition: kiconengine.cpp:56
Desktop icons.
Definition: kiconloader.h:122
QList< QSize > availableSizes(QIcon::Mode mode, QIcon::State state) const override
Reimplementation.
Icon is disabled.
Definition: kiconloader.h:167
Icon is active.
Definition: kiconloader.h:166
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:168
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Reimplementation.
transparent
void setDevicePixelRatio(qreal scaleFactor)
Iconloader for KDE.
Definition: kiconloader.h:67
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Mar 4 2021 22:42:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.