KDEUI
kguiitem.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2001 Holger Freyther (freyher@yahoo.com) 00003 based on ideas from Martijn and Simon 00004 many thanks to Simon 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kguiitem.h" 00022 00023 #include <kiconloader.h> // TODO remove 00024 #include <kdebug.h> 00025 #include <kicon.h> 00026 #include <kcomponentdata.h> 00027 00028 #include <assert.h> 00029 00030 class KGuiItem::KGuiItemPrivate 00031 { 00032 public: 00033 KGuiItemPrivate() 00034 { 00035 m_enabled = true; 00036 m_hasIcon = false; 00037 } 00038 00039 KGuiItemPrivate( const KGuiItemPrivate &rhs ) 00040 { 00041 ( *this ) = rhs; 00042 } 00043 00044 KGuiItemPrivate &operator=( const KGuiItemPrivate &rhs ) 00045 { 00046 m_text = rhs.m_text; 00047 m_icon = rhs.m_icon; 00048 m_iconName = rhs.m_iconName; 00049 m_toolTip = rhs.m_toolTip; 00050 m_whatsThis = rhs.m_whatsThis; 00051 m_statusText = rhs.m_statusText; 00052 m_enabled = rhs.m_enabled; 00053 m_hasIcon = rhs.m_hasIcon; 00054 00055 return *this; 00056 } 00057 00058 QString m_text; 00059 QString m_toolTip; 00060 QString m_whatsThis; 00061 QString m_statusText; 00062 QString m_iconName; 00063 KIcon m_icon; 00064 bool m_hasIcon : 1; 00065 bool m_enabled : 1; 00066 }; 00067 00068 00069 KGuiItem::KGuiItem() { 00070 d = new KGuiItemPrivate; 00071 } 00072 00073 KGuiItem::KGuiItem( const QString &text, const QString &iconName, 00074 const QString &toolTip, const QString &whatsThis ) 00075 { 00076 d = new KGuiItemPrivate; 00077 d->m_text = text; 00078 d->m_toolTip = toolTip; 00079 d->m_whatsThis = whatsThis; 00080 setIconName( iconName ); 00081 } 00082 00083 KGuiItem::KGuiItem( const QString &text, const KIcon &icon, 00084 const QString &toolTip, const QString &whatsThis ) 00085 { 00086 d = new KGuiItemPrivate; 00087 d->m_text = text; 00088 d->m_toolTip = toolTip; 00089 d->m_whatsThis = whatsThis; 00090 setIcon( icon ); 00091 } 00092 00093 KGuiItem::KGuiItem( const KGuiItem &rhs ) 00094 : d( 0 ) 00095 { 00096 ( *this ) = rhs; 00097 } 00098 00099 KGuiItem &KGuiItem::operator=( const KGuiItem &rhs ) 00100 { 00101 if ( d == rhs.d ) 00102 return *this; 00103 00104 assert( rhs.d ); 00105 00106 delete d; 00107 d = new KGuiItemPrivate( *rhs.d ); 00108 00109 return *this; 00110 } 00111 00112 KGuiItem::~KGuiItem() 00113 { 00114 delete d; 00115 } 00116 00117 QString KGuiItem::text() const 00118 { 00119 return d->m_text; 00120 } 00121 00122 00123 QString KGuiItem::plainText() const 00124 { 00125 const int len = d->m_text.length(); 00126 00127 if (len == 0) 00128 return d->m_text; 00129 00130 //Can assume len >= 1 from now on. 00131 QString stripped; 00132 00133 int resultLength = 0; 00134 stripped.resize(len); 00135 00136 const QChar* data = d->m_text.unicode(); 00137 for ( int pos = 0; pos < len; ++pos ) 00138 { 00139 if ( data[ pos ] != '&' ) 00140 stripped[ resultLength++ ] = data[ pos ]; 00141 else if ( pos + 1 < len && data[ pos + 1 ] == '&' ) 00142 stripped[ resultLength++ ] = data[ pos++ ]; 00143 } 00144 00145 stripped.truncate(resultLength); 00146 00147 return stripped; 00148 } 00149 00150 00151 KIcon KGuiItem::icon( ) const 00152 { 00153 if (d->m_hasIcon) { 00154 if (!d->m_iconName.isEmpty()) { 00155 return KIcon(d->m_iconName, KGlobal::mainComponent().isValid() ? KIconLoader::global() : 0); 00156 } else { 00157 return d->m_icon; 00158 } 00159 } 00160 return KIcon(); 00161 } 00162 00163 // deprecated 00164 #ifndef KDE_NO_DEPRECATED 00165 QIcon KGuiItem::iconSet( KIconLoader::Group group, int size ) const 00166 { 00167 if (d->m_hasIcon && KGlobal::mainComponent().isValid()) { 00168 if( !d->m_iconName.isEmpty()) { 00169 KIconLoader* iconLoader = KIconLoader::global(); 00170 return iconLoader->loadIconSet( d->m_iconName, group, size ); 00171 } else { 00172 return d->m_icon; 00173 } 00174 } else 00175 return QIcon(); 00176 } 00177 #endif 00178 00179 QString KGuiItem::iconName() const 00180 { 00181 return d->m_iconName; 00182 } 00183 00184 QString KGuiItem::toolTip() const 00185 { 00186 return d->m_toolTip; 00187 } 00188 00189 QString KGuiItem::whatsThis() const 00190 { 00191 return d->m_whatsThis; 00192 } 00193 00194 bool KGuiItem::isEnabled() const 00195 { 00196 return d->m_enabled; 00197 } 00198 00199 bool KGuiItem::hasIcon() const 00200 { 00201 return d->m_hasIcon; 00202 } 00203 00204 void KGuiItem::setText( const QString &text ) 00205 { 00206 d->m_text = text; 00207 } 00208 00209 void KGuiItem::setIcon( const KIcon &icon ) 00210 { 00211 d->m_icon = icon; 00212 d->m_iconName.clear(); 00213 d->m_hasIcon = !icon.isNull(); 00214 } 00215 00216 void KGuiItem::setIconName( const QString &iconName ) 00217 { 00218 d->m_iconName = iconName; 00219 d->m_icon = KIcon(); 00220 d->m_hasIcon = !iconName.isEmpty(); 00221 } 00222 00223 void KGuiItem::setToolTip( const QString &toolTip ) 00224 { 00225 d->m_toolTip = toolTip; 00226 } 00227 00228 void KGuiItem::setWhatsThis( const QString &whatsThis ) 00229 { 00230 d->m_whatsThis = whatsThis; 00231 } 00232 00233 void KGuiItem::setEnabled( bool enabled ) 00234 { 00235 d->m_enabled = enabled; 00236 } 00237 00238 // vim: set et sw=4:
KDE 4.6 API Reference