kexi

kexismalltoolbutton.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexismalltoolbutton.h"
00021 
00022 #include <qtooltip.h>
00023 #include <qwhatsthis.h>
00024 #include <qstyle.h>
00025 
00026 #include <kiconloader.h>
00027 #include <kglobalsettings.h>
00028 
00029 #include <core/kexi.h>
00030 
00031 KexiSmallToolButton::KexiSmallToolButton(QWidget* parent, const QString& text,
00032     const QString& icon, const char* name)
00033  : QToolButton(parent, name)
00034 {
00035     init();
00036     update(text, SmallIconSet(icon));
00037 }
00038 
00039 KexiSmallToolButton::KexiSmallToolButton(QWidget* parent, const QString& text,
00040     const QIconSet& iconSet, const char* name)
00041  : QToolButton(parent, name)
00042 {
00043     init();
00044     update(text, iconSet);
00045 }
00046 
00047 KexiSmallToolButton::KexiSmallToolButton(QWidget* parent, KAction* action)
00048  : QToolButton(parent, action->name())
00049  , m_action(action)
00050 {
00051     init();
00052     connect(this, SIGNAL(clicked()), action, SLOT(activate()));
00053     connect(action, SIGNAL(enabled(bool)), this, SLOT(setEnabled(bool)));
00054     updateAction();
00055 }
00056 
00057 KexiSmallToolButton::~KexiSmallToolButton()
00058 {
00059 }
00060 
00061 void KexiSmallToolButton::updateAction()
00062 {
00063     if (!m_action)
00064         return;
00065     update(m_action->text(), m_action->iconSet(KIcon::Small));
00066     setAccel(m_action->shortcut());
00067     QToolTip::add(this, m_action->toolTip());
00068     QWhatsThis::add(this, m_action->whatsThis());
00069 }
00070 
00071 void KexiSmallToolButton::init()
00072 {
00073     setPaletteBackgroundColor(palette().active().background());
00074     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
00075     QFont f(KGlobalSettings::toolBarFont());
00076     f.setPixelSize(Kexi::smallFont().pixelSize());
00077     setFont(f);
00078     setAutoRaise(true);
00079 }
00080 
00081 void KexiSmallToolButton::update(const QString& text, const QIconSet& iconSet, bool tipToo)
00082 {
00083     int width = 0;
00084     if (text.isEmpty()) {
00085         width = 10;
00086         setUsesTextLabel(false);
00087     }
00088     else {
00089         width += QFontMetrics(font()).width(text+" ");
00090         setUsesTextLabel(true);
00091         setTextPosition(QToolButton::Right);
00092         QToolButton::setTextLabel(text, tipToo);
00093     }
00094     if (!iconSet.isNull()) {
00095         width += IconSize(KIcon::Small);
00096         QToolButton::setIconSet(iconSet);
00097     }
00098     setFixedWidth( width );
00099 }
00100 
00101 void KexiSmallToolButton::setIconSet( const QIconSet& iconSet )
00102 {
00103     update(textLabel(), iconSet);
00104 }
00105 
00106 void KexiSmallToolButton::setIconSet( const QString& icon )
00107 {
00108     setIconSet( SmallIconSet(icon) );
00109 }
00110 
00111 void KexiSmallToolButton::setTextLabel( const QString & newLabel, bool tipToo )
00112 {
00113     Q_UNUSED( tipToo );
00114 
00115     update(newLabel, iconSet());
00116 }
00117 
00118 void KexiSmallToolButton::drawButton( QPainter *_painter )
00119 {
00120     QToolButton::drawButton(_painter);
00121     if (QToolButton::popup()) {
00122         QStyle::SFlags arrowFlags = QStyle::Style_Default;
00123         if (isDown())
00124             arrowFlags |= QStyle::Style_Down;
00125         if (isEnabled())
00126             arrowFlags |= QStyle::Style_Enabled;
00127         style().drawPrimitive(QStyle::PE_ArrowDown, _painter,
00128             QRect(width()-7, height()-7, 5, 5), colorGroup(),
00129             arrowFlags, QStyleOption() );
00130     }
00131 }
00132 
00133 #include "kexismalltoolbutton.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys