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

KDEUI

  • 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
29KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader, const QStringList& overlays)
30 : mIconName(iconName),
31 mOverlays(overlays),
32 mIconLoader(iconLoader)
33{
34
35}
36
37KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader)
38 : mIconName(iconName),
39 mIconLoader(iconLoader)
40{
41}
42
43static 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
61QSize 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
69void 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
92QPixmap 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
119QString KIconEngine::key() const
120{
121 return QString::fromLatin1("KIconEngine");
122}
123
124QIconEngineV2 *KIconEngine::clone() const
125{
126 return new KIconEngine(mIconName, mIconLoader.data(), mOverlays);
127}
128
129bool KIconEngine::read(QDataStream &in)
130{
131 in >> mIconName >> mOverlays;
132 return true;
133}
134
135bool KIconEngine::write(QDataStream &out) const
136{
137 out << mIconName << mOverlays;
138 return true;
139}
140
141void 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
Iconloader for KDE.
Definition: kiconloader.h:78
KIconLoader::Group
Group
The group of the icon.
Definition: kiconloader.h:127
KIconLoader::Small
@ Small
Small icons, e.g. for buttons.
Definition: kiconloader.h:139
KIconLoader::Desktop
@ Desktop
Desktop icons.
Definition: kiconloader.h:131
KIconLoader::Toolbar
@ Toolbar
Toolbar icons.
Definition: kiconloader.h:135
KIconLoader::ActiveState
@ ActiveState
Icon is active.
Definition: kiconloader.h:173
KIconLoader::DisabledState
@ DisabledState
Icon is disabled.
Definition: kiconloader.h:174
KIconLoader::DefaultState
@ DefaultState
The default state.
Definition: kiconloader.h:172
QWidget
qIconModeToKIconState
static int qIconModeToKIconState(QIcon::Mode mode)
Definition: kiconengine.cpp:43
kiconloader.h
group
group
KStandardAction::name
const char * name(StandardAction id)
This will return the internal name of a given standard action.
Definition: kstandardaction.cpp:223
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
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