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 : mIconName(iconName), 00031 mOverlays(overlays), 00032 mIconLoader(iconLoader) 00033 { 00034 00035 } 00036 00037 KIconEngine::KIconEngine(const QString& iconName, KIconLoader* iconLoader) 00038 : mIconName(iconName), 00039 mIconLoader(iconLoader) 00040 { 00041 } 00042 00043 static inline int qIconModeToKIconState( QIcon::Mode mode ) 00044 { 00045 int kstate; 00046 switch (mode) { 00047 default: 00048 case QIcon::Normal: 00049 kstate = KIconLoader::DefaultState; 00050 break; 00051 case QIcon::Active: 00052 kstate = KIconLoader::ActiveState; 00053 break; 00054 case QIcon::Disabled: 00055 kstate = KIconLoader::DisabledState; 00056 break; 00057 } 00058 return kstate; 00059 } 00060 00061 QSize KIconEngine::actualSize( const QSize & size, QIcon::Mode mode, QIcon::State state ) 00062 { 00063 Q_UNUSED(state) 00064 Q_UNUSED(mode) 00065 const int iconSize = qMin(size.width(), size.height()); 00066 return QSize(iconSize, iconSize); 00067 } 00068 00069 void KIconEngine::paint(QPainter * painter, const QRect & rect, QIcon::Mode mode, QIcon::State state) 00070 { 00071 if (!mIconLoader) { 00072 return; 00073 } 00074 00075 Q_UNUSED(state) 00076 00077 const int kstate = qIconModeToKIconState(mode); 00078 KIconLoader::Group group = KIconLoader::Desktop; 00079 00080 if (QWidget* targetWidget = dynamic_cast<QWidget*>(painter->device())) { 00081 if (qobject_cast<QMenu*>(targetWidget)) 00082 group = KIconLoader::Small; 00083 else if (qobject_cast<QToolBar*>(targetWidget->parent())) 00084 group = KIconLoader::Toolbar; 00085 } 00086 00087 const int iconSize = qMin(rect.width(), rect.height()); 00088 const QPixmap pix = mIconLoader.data()->loadIcon(mIconName, group, iconSize, kstate, mOverlays); 00089 painter->drawPixmap(rect, pix); 00090 } 00091 00092 QPixmap KIconEngine::pixmap(const QSize & size, QIcon::Mode mode, QIcon::State state) 00093 { 00094 Q_UNUSED(state) 00095 00096 if (!mIconLoader) { 00097 QPixmap pm(size); 00098 pm.fill(Qt::transparent); 00099 return pm; 00100 } 00101 00102 const int kstate = qIconModeToKIconState(mode); 00103 const int iconSize = qMin(size.width(), size.height()); 00104 QPixmap pix = mIconLoader.data()->loadIcon(mIconName, KIconLoader::Desktop, iconSize, kstate, mOverlays); 00105 00106 if (pix.size() == size) { 00107 return pix; 00108 } 00109 00110 QPixmap pix2(size); 00111 pix2.fill(QColor(0,0,0,0)); 00112 00113 QPainter painter(&pix2); 00114 painter.drawPixmap(QPoint(), pix); 00115 00116 return pix2; 00117 } 00118 00119 QString KIconEngine::key() const 00120 { 00121 return QString::fromLatin1("KIconEngine"); 00122 } 00123 00124 QIconEngineV2 *KIconEngine::clone() const 00125 { 00126 return new KIconEngine(mIconName, mIconLoader.data(), mOverlays); 00127 } 00128 00129 bool KIconEngine::read(QDataStream &in) 00130 { 00131 in >> mIconName >> mOverlays; 00132 return true; 00133 } 00134 00135 bool KIconEngine::write(QDataStream &out) const 00136 { 00137 out << mIconName << mOverlays; 00138 return true; 00139 } 00140 00141 void KIconEngine::virtual_hook(int id, void *data) 00142 { 00143 switch (id) { 00144 case IconNameHook: { 00145 QString *name = reinterpret_cast<QString*>(data); 00146 *name = mIconName; 00147 break; 00148 } 00149 default: 00150 QIconEngineV2::virtual_hook(id, data); 00151 break; 00152 } 00153 }
KDE 4.7 API Reference