KDEUI
kiconengine.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2006 Hamish Rodda <rodda@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "kiconengine_p.h" 00020 00021 #include <kiconloader.h> 00022 00023 #include <QtGui/QPainter> 00024 #include <QtGui/QMenu> 00025 #include <QtGui/QToolBar> 00026 #include <QtGui/QApplication> 00027 00028 00029 KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader, const QStringList& overlays) 00030 { 00031 mIconName = iconName; 00032 mIconLoader = iconLoader; 00033 mOverlays = overlays; 00034 } 00035 00036 KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader) 00037 { 00038 mIconName = iconName; 00039 mIconLoader = iconLoader; 00040 } 00041 00042 static inline int qIconModeToKIconState( QIcon::Mode mode ) 00043 { 00044 int kstate; 00045 switch (mode) { 00046 default: 00047 case QIcon::Normal: 00048 kstate = KIconLoader::DefaultState; 00049 break; 00050 case QIcon::Active: 00051 kstate = KIconLoader::ActiveState; 00052 break; 00053 case QIcon::Disabled: 00054 kstate = KIconLoader::DisabledState; 00055 break; 00056 } 00057 return kstate; 00058 } 00059 00060 QSize KIconEngine::actualSize( const QSize & size, QIcon::Mode mode, QIcon::State state ) 00061 { 00062 Q_UNUSED(state) 00063 Q_UNUSED(mode) 00064 const int iconSize = qMin(size.width(), size.height()); 00065 return QSize(iconSize, iconSize); 00066 } 00067 00068 void KIconEngine::paint( QPainter * painter, const QRect & rect, QIcon::Mode mode, QIcon::State state ) 00069 { 00070 Q_UNUSED(state) 00071 00072 const int kstate = qIconModeToKIconState(mode); 00073 KIconLoader::Group group = KIconLoader::Desktop; 00074 00075 if (QWidget* targetWidget = dynamic_cast<QWidget*>(painter->device())) { 00076 if (qobject_cast<QMenu*>(targetWidget)) 00077 group = KIconLoader::Small; 00078 else if (qobject_cast<QToolBar*>(targetWidget->parent())) 00079 group = KIconLoader::Toolbar; 00080 } 00081 00082 const int iconSize = qMin(rect.width(), rect.height()); 00083 const QPixmap pix = mIconLoader->loadIcon(mIconName, group, iconSize, kstate, mOverlays); 00084 painter->drawPixmap(rect, pix); 00085 } 00086 00087 QPixmap KIconEngine::pixmap( const QSize & size, QIcon::Mode mode, QIcon::State state ) 00088 { 00089 Q_UNUSED(state) 00090 00091 const int kstate = qIconModeToKIconState(mode); 00092 const int iconSize = qMin(size.width(), size.height()); 00093 QPixmap pix = mIconLoader->loadIcon(mIconName, KIconLoader::Desktop, iconSize, kstate, mOverlays); 00094 00095 if(pix.size() == size) 00096 return pix; 00097 00098 QPixmap pix2(size); 00099 pix2.fill(QColor(0,0,0,0)); 00100 00101 QPainter painter(&pix2); 00102 painter.drawPixmap(QPoint(), pix); 00103 00104 return pix2; 00105 } 00106 00107 QString KIconEngine::key() const 00108 { 00109 return QString::fromLatin1("KIconEngine"); 00110 } 00111 00112 QIconEngineV2 *KIconEngine::clone() const 00113 { 00114 return new KIconEngine(mIconName, mIconLoader, mOverlays); 00115 } 00116 00117 bool KIconEngine::read(QDataStream &in) 00118 { 00119 in >> mIconName >> mOverlays; 00120 return true; 00121 } 00122 00123 bool KIconEngine::write(QDataStream &out) const 00124 { 00125 out << mIconName << mOverlays; 00126 return true; 00127 } 00128 00129 void KIconEngine::virtual_hook(int id, void *data) 00130 { 00131 switch (id) { 00132 case IconNameHook: { 00133 QString *name = reinterpret_cast<QString*>(data); 00134 *name = mIconName; 00135 break; 00136 } 00137 default: 00138 QIconEngineV2::virtual_hook(id, data); 00139 break; 00140 } 00141 }
KDE 4.6 API Reference