• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • kdeui
  • icons
kiconengine.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
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_p.h"
20 
21 #include <kiconloader.h>
22 
23 #include <QtGui/QPainter>
24 #include <QtGui/QMenu>
25 #include <QtGui/QToolBar>
26 #include <QtGui/QApplication>
27 
28 
29 KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader, const QStringList& overlays)
30  : mIconName(iconName),
31  mOverlays(overlays),
32  mIconLoader(iconLoader)
33 {
34 
35 }
36 
37 KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader)
38  : mIconName(iconName),
39  mIconLoader(iconLoader)
40 {
41 }
42 
43 static inline int qIconModeToKIconState( QIcon::Mode mode )
44 {
45  int kstate;
46  switch (mode) {
47  default:
48  case QIcon::Normal:
49  kstate = KIconLoader::DefaultState;
50  break;
51  case QIcon::Active:
52  kstate = KIconLoader::ActiveState;
53  break;
54  case QIcon::Disabled:
55  kstate = KIconLoader::DisabledState;
56  break;
57  }
58  return kstate;
59 }
60 
61 QSize KIconEngine::actualSize( const QSize & size, QIcon::Mode mode, QIcon::State state )
62 {
63  Q_UNUSED(state)
64  Q_UNUSED(mode)
65  const int iconSize = qMin(size.width(), size.height());
66  return QSize(iconSize, iconSize);
67 }
68 
69 void KIconEngine::paint(QPainter * painter, const QRect & rect, QIcon::Mode mode, QIcon::State state)
70 {
71  if (!mIconLoader) {
72  return;
73  }
74 
75  Q_UNUSED(state)
76 
77  const int kstate = qIconModeToKIconState(mode);
78  KIconLoader::Group group = KIconLoader::Desktop;
79 
80  if (QWidget* targetWidget = dynamic_cast<QWidget*>(painter->device())) {
81  if (qobject_cast<QMenu*>(targetWidget))
82  group = KIconLoader::Small;
83  else if (qobject_cast<QToolBar*>(targetWidget->parent()))
84  group = KIconLoader::Toolbar;
85  }
86 
87  const int iconSize = qMin(rect.width(), rect.height());
88  const QPixmap pix = mIconLoader.data()->loadIcon(mIconName, group, iconSize, kstate, mOverlays);
89  painter->drawPixmap(rect, pix);
90 }
91 
92 QPixmap KIconEngine::pixmap(const QSize & size, QIcon::Mode mode, QIcon::State state)
93 {
94  Q_UNUSED(state)
95 
96  if (!mIconLoader) {
97  QPixmap pm(size);
98  pm.fill(Qt::transparent);
99  return pm;
100  }
101 
102  const int kstate = qIconModeToKIconState(mode);
103  const int iconSize = qMin(size.width(), size.height());
104  QPixmap pix = mIconLoader.data()->loadIcon(mIconName, KIconLoader::Desktop, iconSize, kstate, mOverlays);
105 
106  if (pix.size() == size) {
107  return pix;
108  }
109 
110  QPixmap pix2(size);
111  pix2.fill(QColor(0,0,0,0));
112 
113  QPainter painter(&pix2);
114  painter.drawPixmap(QPoint(), pix);
115 
116  return pix2;
117 }
118 
119 QString KIconEngine::key() const
120 {
121  return QString::fromLatin1("KIconEngine");
122 }
123 
124 QIconEngineV2 *KIconEngine::clone() const
125 {
126  return new KIconEngine(mIconName, mIconLoader.data(), mOverlays);
127 }
128 
129 bool KIconEngine::read(QDataStream &in)
130 {
131  in >> mIconName >> mOverlays;
132  return true;
133 }
134 
135 bool KIconEngine::write(QDataStream &out) const
136 {
137  out << mIconName << mOverlays;
138  return true;
139 }
140 
141 void KIconEngine::virtual_hook(int id, void *data)
142 {
143  switch (id) {
144  case IconNameHook: {
145  QString *name = reinterpret_cast<QString*>(data);
146  *name = mIconName;
147  break;
148  }
149  default:
150  QIconEngineV2::virtual_hook(id, data);
151  break;
152  }
153 }
KIconLoader::DefaultState
The default state.
Definition: kiconloader.h:172
QWidget
QSize::width
int width() const
group
QDataStream
qIconModeToKIconState
static int qIconModeToKIconState(QIcon::Mode mode)
Definition: kiconengine.cpp:43
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
kiconloader.h
QPoint
KIconLoader::Desktop
Desktop icons.
Definition: kiconloader.h:131
QRect
KIconLoader::DisabledState
Icon is disabled.
Definition: kiconloader.h:174
KIconLoader::Small
Small icons, e.g. for buttons.
Definition: kiconloader.h:139
KIconLoader::ActiveState
Icon is active.
Definition: kiconloader.h:173
QPainter
KStandardAction::actualSize
KAction * actualSize(const QObject *recvr, const char *slot, QObject *parent)
View the document at its actual size.
Definition: kstandardaction.cpp:349
QIconEngineV2::virtual_hook
virtual void virtual_hook(int id, void *data)
QString
QColor
QStringList
QPixmap
QSize
KIconLoader::Toolbar
Toolbar icons.
Definition: kiconloader.h:135
QSize::height
int height() const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QIconEngineV2
QIcon
KIconLoader
Iconloader for KDE.
Definition: kiconloader.h:77
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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