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

kcalc

  • sources
  • kde-4.14
  • kdeutils
  • kcalc
kcalc_button.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2001 - 2013 Evan Teran
3  evan.teran@gmail.com
4 
5 Copyright (C) 1996 - 2000 Bernd Johannes Wuebben
6  wuebben@kde.org
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "kcalc_button.h"
23 
24 #include <QAbstractTextDocumentLayout>
25 #include <QApplication>
26 #include <QStyleOptionButton>
27 #include <QStylePainter>
28 #include <QTextDocument>
29 
30 #include "kcalc_button.moc"
31 
32 //------------------------------------------------------------------------------
33 // Name: KCalcButton
34 // Desc: constructor
35 //------------------------------------------------------------------------------
36 KCalcButton::KCalcButton(QWidget *parent) : KPushButton(parent), show_shortcut_mode_(false), mode_flags_(ModeNormal), size_() {
37 
38  setAcceptDrops(true); // allow color drops
39  setFocusPolicy(Qt::TabFocus);
40  setAutoDefault(false);
41 
42  // use preferred size policy for vertical
43  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
44 }
45 
46 //------------------------------------------------------------------------------
47 // Name: KCalcButton
48 // Desc: constructor
49 //------------------------------------------------------------------------------
50 KCalcButton::KCalcButton(const QString &label, QWidget *parent, const QString &tooltip) : KPushButton(label, parent), show_shortcut_mode_(false), mode_flags_(ModeNormal), size_() {
51 
52  setAutoDefault(false);
53  addMode(ModeNormal, label, tooltip);
54 
55  // use preferred size policy for vertical
56  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
57 }
58 
59 //------------------------------------------------------------------------------
60 // Name: addMode
61 // Desc:
62 //------------------------------------------------------------------------------
63 void KCalcButton::addMode(ButtonModeFlags mode, const QString &label, const QString &tooltip) {
64 
65  if (mode_.contains(mode)) {
66  mode_.remove(mode);
67  }
68 
69  mode_[mode] = ButtonMode(label, tooltip);
70  calcSizeHint();
71 
72  // Need to put each button into default mode first
73  if (mode == ModeNormal) {
74  slotSetMode(ModeNormal, true);
75  }
76 }
77 
78 //------------------------------------------------------------------------------
79 // Name: slotSetMode
80 // Desc:
81 //------------------------------------------------------------------------------
82 void KCalcButton::slotSetMode(ButtonModeFlags mode, bool flag) {
83 
84  ButtonModeFlags new_mode;
85 
86  if (flag) { // if the specified mode is to be set (i.e. flag = true)
87  new_mode = ButtonModeFlags(mode_flags_ | mode);
88  } else if (mode_flags_ && mode) { // if the specified mode is to be cleared (i.e. flag = false)
89  new_mode = ButtonModeFlags(mode_flags_ - mode);
90  } else {
91  return; // nothing to do
92  }
93 
94  if (mode_.contains(new_mode)) {
95  // save shortcut, because setting label erases it
96  QKeySequence current_shortcut = shortcut();
97 
98  setText(mode_[new_mode].label);
99  this->setToolTip(mode_[new_mode].tooltip);
100  mode_flags_ = new_mode;
101 
102  // restore shortcut
103  setShortcut(current_shortcut);
104  }
105 
106  // this is necessary for people pressing CTRL and changing mode at
107  // the same time...
108  if (show_shortcut_mode_) {
109  slotSetAccelDisplayMode(true);
110  }
111 
112  update();
113 }
114 
115 //------------------------------------------------------------------------------
116 // Name: slotSetAccelDisplayMode
117 // Desc:
118 //------------------------------------------------------------------------------
119 void KCalcButton::slotSetAccelDisplayMode(bool flag) {
120 
121  show_shortcut_mode_ = flag;
122 
123  // save shortcut, because setting label erases it
124  QKeySequence current_shortcut = shortcut();
125 
126  if (flag) {
127  setText(QString(shortcut()));
128  } else {
129  setText(mode_[mode_flags_].label);
130  }
131 
132  // restore shortcut
133  setShortcut(current_shortcut);
134  update();
135 }
136 
137 //------------------------------------------------------------------------------
138 // Name: paintEvent
139 // Desc: draws the button
140 //------------------------------------------------------------------------------
141 void KCalcButton::paintEvent(QPaintEvent *) {
142 
143  QPainter p(this);
144  QStyleOptionButton option;
145  initStyleOption(&option);
146  const bool is_down = isDown() || isChecked();
147  const int x_offset = is_down ? style()->pixelMetric(QStyle::PM_ButtonShiftHorizontal, &option, this) : 0;
148  const int y_offset = is_down ? style()->pixelMetric(QStyle::PM_ButtonShiftVertical, &option, this) : 0;
149 
150  // draw bevel
151  style()->drawControl(QStyle::CE_PushButtonBevel, &option, &p, this);
152 
153  // draw label...
154  p.save();
155 
156  // rant: Qt4 needs QSimpleRichText, dammit!
157  QTextDocument doc;
158  QAbstractTextDocumentLayout::PaintContext context;
159  doc.setHtml(QLatin1String("<center>") + text() + QLatin1String("</center>"));
160  doc.setDefaultFont(font());
161  context.palette = palette();
162  context.palette.setColor(QPalette::Text, context.palette.buttonText().color());
163 
164  p.translate((width() / 2 - doc.size().width() / 2) + x_offset, (height() / 2 - doc.size().height() / 2) + y_offset);
165  doc.documentLayout()->draw(&p, context);
166  p.restore();
167 
168  // draw focus
169  if (hasFocus()) {
170  QStyleOptionFocusRect fropt;
171  fropt.QStyleOption::operator=(option);
172  fropt.rect = style()->subElementRect(QStyle::SE_PushButtonFocusRect, &option, this);
173  style()->drawPrimitive(QStyle::PE_FrameFocusRect, &fropt, &p, this);
174  }
175 }
176 
177 //------------------------------------------------------------------------------
178 // Name: sizeHint
179 // Desc:
180 //------------------------------------------------------------------------------
181 QSize KCalcButton::sizeHint() const {
182  // reimplemented to provide a smaller button
183  return size_;
184 }
185 
186 //------------------------------------------------------------------------------
187 // Name: calcSizeHint
188 // Desc:
189 //------------------------------------------------------------------------------
190 void KCalcButton::calcSizeHint() {
191 
192  int margin = style()->pixelMetric(QStyle::PM_ButtonMargin, 0, this);
193 
194  // want narrow margin than normal
195  margin = qMax(margin / 2, 3);
196 
197  // approximation because metrics doesn't account for richtext
198  size_ = fontMetrics().size(0, mode_[ModeNormal].label);
199  if (mode_.contains(ModeShift)) {
200  size_ = size_.expandedTo(fontMetrics().size(0, mode_[ModeShift].label));
201  }
202 
203  if (mode_.contains(ModeHyperbolic)) {
204  size_ = size_.expandedTo(fontMetrics().size(0, mode_[ModeHyperbolic].label));
205  }
206 
207  size_ += QSize(margin * 2, margin * 2);
208  size_ = size_.expandedTo(QApplication::globalStrut());
209 }
210 
211 //------------------------------------------------------------------------------
212 // Name: setFont
213 // Desc:
214 //------------------------------------------------------------------------------
215 void KCalcButton::setFont(const QFont &fnt) {
216 
217  KPushButton::setFont(fnt);
218  calcSizeHint();
219 }
220 
221 //------------------------------------------------------------------------------
222 // Name: setText
223 // Desc:
224 //------------------------------------------------------------------------------
225 void KCalcButton::setText(const QString &text) {
226 
227  KPushButton::setText(text);
228 
229  // normal mode may not have been explicitly set
230  if (mode_[ModeNormal].label.isEmpty()) {
231  mode_[ModeNormal].label = text;
232  }
233 
234  calcSizeHint();
235 }
236 
237 //------------------------------------------------------------------------------
238 // Name: setToolTip
239 // Desc:
240 //------------------------------------------------------------------------------
241 void KCalcButton::setToolTip(const QString &tip) {
242 
243  KPushButton::setToolTip(tip);
244 
245  // normal mode may not have been explicitly set
246  if (mode_[ModeNormal].tooltip.isEmpty()) {
247  mode_[ModeNormal].tooltip = tip;
248  }
249 }
KPushButton
QWidget
ButtonMode
Definition: kcalc_button.h:45
QMap::contains
bool contains(const Key &key) const
KCalcButton::sizeHint
virtual QSize sizeHint() const
Definition: kcalc_button.cpp:181
QFont
KCalcButton::KCalcButton
KCalcButton(QWidget *parent)
Definition: kcalc_button.cpp:36
QPainter::save
void save()
KCalcButton::paintEvent
virtual void paintEvent(QPaintEvent *e)
Definition: kcalc_button.cpp:141
QStyleOptionButton
QApplication::globalStrut
QSize globalStrut()
KCalcButton::addMode
void addMode(ButtonModeFlags mode, const QString &label, const QString &tooltip)
Definition: kcalc_button.cpp:63
ButtonModeFlags
ButtonModeFlags
Definition: kcalc_button.h:36
ModeNormal
Definition: kcalc_button.h:37
kcalc_button.h
QPainter
KCalcButton::setText
void setText(const QString &text)
Definition: kcalc_button.cpp:225
QTextDocument::documentLayout
QAbstractTextDocumentLayout * documentLayout() const
QString
ModeHyperbolic
Definition: kcalc_button.h:39
KCalcButton::slotSetAccelDisplayMode
void slotSetAccelDisplayMode(bool flag)
Definition: kcalc_button.cpp:119
ModeShift
Definition: kcalc_button.h:38
QAbstractTextDocumentLayout::PaintContext
QTextDocument::size
size
QStyleOptionFocusRect
QTextDocument::setDefaultFont
void setDefaultFont(const QFont &font)
QSize
QPainter::restore
void restore()
QLatin1String
QKeySequence
QSize::expandedTo
QSize expandedTo(const QSize &otherSize) const
QTextDocument
QPainter::translate
void translate(const QPointF &offset)
KCalcButton::setFont
void setFont(const QFont &fnt)
Definition: kcalc_button.cpp:215
QTextDocument::setHtml
void setHtml(const QString &html)
QAbstractTextDocumentLayout::draw
virtual void draw(QPainter *painter, const PaintContext &context)=0
QMap::isEmpty
bool isEmpty() const
QPaintEvent
KCalcButton::slotSetMode
void slotSetMode(ButtonModeFlags mode, bool flag)
Definition: kcalc_button.cpp:82
KCalcButton::setToolTip
void setToolTip(const QString &tip)
Definition: kcalc_button.cpp:241
QMap::remove
int remove(const Key &key)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcalc

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

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal