KDEUI
kguiitem.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "kguiitem.h"
00022
00023 #include <kiconloader.h>
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
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
00164 QIcon KGuiItem::iconSet( KIconLoader::Group group, int size ) const
00165 {
00166 if (d->m_hasIcon && KGlobal::mainComponent().isValid()) {
00167 if( !d->m_iconName.isEmpty()) {
00168 KIconLoader* iconLoader = KIconLoader::global();
00169 return iconLoader->loadIconSet( d->m_iconName, group, size );
00170 } else {
00171 return d->m_icon;
00172 }
00173 } else
00174 return QIcon();
00175 }
00176
00177 QString KGuiItem::iconName() const
00178 {
00179 return d->m_iconName;
00180 }
00181
00182 QString KGuiItem::toolTip() const
00183 {
00184 return d->m_toolTip;
00185 }
00186
00187 QString KGuiItem::whatsThis() const
00188 {
00189 return d->m_whatsThis;
00190 }
00191
00192 bool KGuiItem::isEnabled() const
00193 {
00194 return d->m_enabled;
00195 }
00196
00197 bool KGuiItem::hasIcon() const
00198 {
00199 return d->m_hasIcon;
00200 }
00201
00202 void KGuiItem::setText( const QString &text )
00203 {
00204 d->m_text = text;
00205 }
00206
00207 void KGuiItem::setIcon( const KIcon &icon )
00208 {
00209 d->m_icon = icon;
00210 d->m_iconName.clear();
00211 d->m_hasIcon = !icon.isNull();
00212 }
00213
00214 void KGuiItem::setIconName( const QString &iconName )
00215 {
00216 d->m_iconName = iconName;
00217 d->m_icon = KIcon();
00218 d->m_hasIcon = !iconName.isEmpty();
00219 }
00220
00221 void KGuiItem::setToolTip( const QString &toolTip )
00222 {
00223 d->m_toolTip = toolTip;
00224 }
00225
00226 void KGuiItem::setWhatsThis( const QString &whatsThis )
00227 {
00228 d->m_whatsThis = whatsThis;
00229 }
00230
00231 void KGuiItem::setEnabled( bool enabled )
00232 {
00233 d->m_enabled = enabled;
00234 }
00235
00236