• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kcalc

kcalc_button.cpp

Go to the documentation of this file.
00001 /*
00002     kCalculator, a simple scientific calculator for KDE
00003 
00004     Copyright (C) 1996-2000 Bernd Johannes Wuebben
00005                             wuebben@kde.org
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020 
00021 */
00022 
00023 #include <QAbstractTextDocumentLayout>
00024 #include <QApplication>
00025 #include <QStyleOptionButton>
00026 #include <QStylePainter>
00027 #include <QTextDocument>
00028 
00029 #include "kcalc_button.h"
00030 
00031 
00032 KCalcButton::KCalcButton(QWidget * parent)
00033   : KPushButton(parent), _show_shortcut_mode(false),
00034     _mode_flags(ModeNormal)
00035 {
00036     setFocusPolicy(Qt::TabFocus);
00037     setAutoDefault(false);
00038 
00039     // use preferred size policy for vertical
00040     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
00041 }
00042 
00043 KCalcButton::KCalcButton(const QString &label, QWidget * parent,
00044              const QString &tooltip)
00045   : KPushButton(label, parent), _show_shortcut_mode(false),
00046     _mode_flags(ModeNormal)
00047 {
00048   setAutoDefault(false);
00049   addMode(ModeNormal, label, tooltip);
00050 
00051   // use preferred size policy for vertical
00052   setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
00053 }
00054 
00055 void KCalcButton::addMode(ButtonModeFlags mode, const QString &label,
00056                           const QString &tooltip, const KIcon &icon)
00057 {
00058   if (_mode.contains(mode)) _mode.remove(mode);
00059 
00060   _mode[mode] = ButtonMode(label, tooltip, icon);
00061 
00062   // Need to put each button into default mode first
00063   if (mode == ModeNormal) slotSetMode(ModeNormal, true);
00064 }
00065 
00066 void KCalcButton::slotSetMode(ButtonModeFlags mode, bool flag)
00067 {
00068   ButtonModeFlags new_mode;
00069 
00070   if (flag) { // if the specified mode is to be set (i.e. flag = true)
00071     new_mode = ButtonModeFlags(_mode_flags | mode);
00072   } else if (_mode_flags && mode) { // if the specified mode is to be cleared (i.e. flag = false)
00073     new_mode = ButtonModeFlags(_mode_flags - mode);
00074   } else {
00075     return; // nothing to do
00076   }
00077 
00078   if (_mode.contains(new_mode)) {
00079     // save shortcut, because setting label erases it
00080     QKeySequence _shortcut = shortcut();
00081 
00082     setText(_mode[new_mode].label);
00083     this->setToolTip( _mode[new_mode].tooltip);
00084     setIcon(_mode[new_mode].icon);
00085     _mode_flags = new_mode;
00086 
00087 
00088     // restore shortcut
00089     setShortcut(_shortcut);
00090   }
00091 
00092   // this is necessary for people pressing CTRL and changing mode at
00093   // the same time...
00094   if (_show_shortcut_mode) slotSetAccelDisplayMode(true);
00095   
00096   update();
00097 }
00098 
00099 void KCalcButton::slotSetAccelDisplayMode(bool flag)
00100 {
00101   _show_shortcut_mode = flag;
00102 
00103   // save shortcut, because setting label erases it
00104   QKeySequence _shortcut = shortcut();
00105   
00106   if (flag == true) {
00107     setText(QString(shortcut()));
00108   } else {
00109     setText(_mode[_mode_flags].label);
00110   }
00111 
00112   // restore shortcut
00113   setShortcut(_shortcut);
00114   update();
00115 }
00116 
00117 void KCalcButton::paintEvent(QPaintEvent *)
00118 {
00119     QPainter p(this);
00120     QStyleOptionButton option;
00121     initStyleOption(&option);
00122 
00123     // draw bevel
00124     style()->drawControl(QStyle::CE_PushButtonBevel, &option, &p, this);
00125 
00126     // draw label...
00127     p.save();
00128 
00129     // rant: Qt4 needs QSimpleRichText, dammit!
00130     QTextDocument doc;
00131     QAbstractTextDocumentLayout::PaintContext context;
00132     doc.setHtml("<center>" + text() + "</center>");
00133     context.palette = palette();
00134         context.palette.setColor(QPalette::Text, context.palette.buttonText().color());
00135 
00136     p.translate(width()/2 - doc.size().width()/2,
00137                 height()/2 - doc.size().height()/2);
00138     doc.documentLayout()->draw(&p, context);
00139     p.restore();
00140 
00141     // draw focus
00142     if (hasFocus()) {
00143         QStyleOptionFocusRect fropt;
00144         fropt.QStyleOption::operator=(option);
00145         fropt.rect = style()->subElementRect(QStyle::SE_PushButtonFocusRect,
00146                                              &option, this);
00147         style()->drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, &p, this);
00148     }
00149 }
00150 
00151 QSize KCalcButton::sizeHint() const
00152 {
00153     // reimplemented to provide a shorter button
00154     int margin = style()->pixelMetric(QStyle::PM_ButtonMargin, 0, this);
00155     int h = fontMetrics().lineSpacing() + margin*2 + 4;
00156 
00157     QSize sz = QPushButton::sizeHint();
00158     sz.setHeight(qMin(sz.height(), h));
00159     return sz.expandedTo(QApplication::globalStrut());
00160 }
00161 
00162 void KCalcButton::setText(const QString &text)
00163 {
00164     KPushButton::setText(text);
00165 
00166     // normal mode may not have been explicitly set
00167     if (_mode[ModeNormal].label.isEmpty()) {
00168         _mode[ModeNormal].label = text;
00169     }
00170 }
00171 
00172 void KCalcButton::setToolTip(const QString &tip)
00173 {
00174     KPushButton::setToolTip(tip);
00175 
00176     // normal mode may not have been explicitly set
00177     if (_mode[ModeNormal].tooltip.isEmpty()) {
00178         _mode[ModeNormal].tooltip = tip;
00179     }
00180 }
00181 
00182 #include "kcalc_button.moc"
00183 

kcalc

Skip menu "kcalc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal