• 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_bitset.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2012 - 2013 Evan Teran
3  evan.teran@gmail.com
4 
5 Copyright (C) 2006 Michel Marti
6  mma@objectxp.com
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_bitset.h"
23 #include "bitbutton.h"
24 
25 #include <QButtonGroup>
26 #include <QGridLayout>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QPainter>
30 
31 #include "kcalc_bitset.moc"
32 
33 // TODO: I think it would actually be appropriate to use a std::bitset<64>
34 // for the internal representation of this class perhaps
35 // the only real caveat is the conversion to/from quint64
36 
37 //------------------------------------------------------------------------------
38 // Name: paintEvent
39 // Desc: draws the button
40 //------------------------------------------------------------------------------
41 void BitButton::paintEvent(QPaintEvent *) {
42 
43  QPainter painter(this);
44  QPen pen(palette().text(), 2);
45  pen.setJoinStyle(Qt::MiterJoin);
46  painter.setPen(pen);
47 
48  if (on_) {
49  painter.setBrush(palette().text());
50  } else {
51  painter.setBrush(palette().base());
52  }
53 
54  painter.drawRect(rect().adjusted(1, 1, -1, -1));
55 }
56 
57 //------------------------------------------------------------------------------
58 // Name: KCalcBitset
59 // Desc: constructor
60 //------------------------------------------------------------------------------
61 KCalcBitset::KCalcBitset(QWidget *parent) : QFrame(parent), value_(0) {
62 
63  setFrameStyle(QFrame::Panel | QFrame::Sunken);
64 
65  bit_button_group_ = new QButtonGroup(this);
66  connect(bit_button_group_, SIGNAL(buttonClicked(int)), SLOT(slotToggleBit(int)));
67 
68  // smaller label font
69  QFont fnt = font();
70  if (fnt.pointSize() > 6) {
71  fnt.setPointSize(fnt.pointSize() - 1);
72  }
73 
74  // main layout
75  QGridLayout *layout = new QGridLayout(this);
76  layout->setMargin(2);
77  layout->setSpacing(0);
78 
79  // create bits
80  int bitCounter = 63;
81  for (int rows = 0; rows < 2; rows++) {
82  for (int cols = 0; cols < 4; cols++) {
83  // two rows of four words
84  QHBoxLayout *const wordlayout = new QHBoxLayout();
85  wordlayout->setMargin(2);
86  wordlayout->setSpacing(2);
87  layout->addLayout(wordlayout, rows, cols);
88 
89  for (int bit = 0; bit < 8; bit++) {
90  BitButton *const tmpBitButton = new BitButton(this);
91  wordlayout->addWidget(tmpBitButton);
92  bit_button_group_->addButton(tmpBitButton, bitCounter);
93  bitCounter--;
94  }
95 
96  // label word
97  QLabel *label = new QLabel(this);
98  label->setText(QString::number(bitCounter + 1));
99  label->setFont(fnt);
100  wordlayout->addWidget(label);
101  }
102  }
103 }
104 
105 //------------------------------------------------------------------------------
106 // Name: setValue
107 // Desc: set the value of the bitset based on an unsigned 64-bit number
108 //------------------------------------------------------------------------------
109 void KCalcBitset::setValue(quint64 value) {
110 
111  if (value_ == value) {
112  // don't waste time if there was no change..
113  return;
114  }
115 
116  value_ = value;
117 
118  // set each bit button
119  for (int i = 0; i < 64; i++) {
120  if(BitButton *bb = qobject_cast<BitButton*>(bit_button_group_->button(i))) {
121  bb->setOn(value & 1);
122  }
123  value >>= 1;
124  }
125 }
126 
127 //------------------------------------------------------------------------------
128 // Name: getValue
129 // Desc: returns the bitset value as an unsigned 64-bit number
130 //------------------------------------------------------------------------------
131 quint64 KCalcBitset::getValue() const {
132  return value_;
133 }
134 
135 //------------------------------------------------------------------------------
136 // Name: slotToggleBit
137 // Desc: inverts the value of a single bit
138 //------------------------------------------------------------------------------
139 void KCalcBitset::slotToggleBit(int bit) {
140 
141  const quint64 nv = getValue() ^(1LL << bit);
142  setValue(nv);
143  emit valueChanged(value_);
144 }
145 
QWidget::layout
QLayout * layout() const
KCalcBitset::getValue
quint64 getValue() const
Definition: kcalc_bitset.cpp:131
QWidget
QFont::setPointSize
void setPointSize(int pointSize)
QWidget::palette
const QPalette & palette() const
QButtonGroup::addButton
void addButton(QAbstractButton *button)
bitbutton.h
QFont
BitButton::paintEvent
void paintEvent(QPaintEvent *event)
Definition: kcalc_bitset.cpp:41
KCalcBitset::setValue
void setValue(quint64 value)
Definition: kcalc_bitset.cpp:109
QHBoxLayout
QPen::setJoinStyle
void setJoinStyle(Qt::PenJoinStyle style)
QGridLayout
QButtonGroup::button
QAbstractButton * button(int id) const
QFrame::setFrameStyle
void setFrameStyle(int style)
QGridLayout::setSpacing
void setSpacing(int spacing)
QButtonGroup
QPainter::drawRect
void drawRect(const QRectF &rectangle)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString::number
QString number(int n, int base)
QPainter::setPen
void setPen(const QColor &color)
QPainter
QPainter::setBrush
void setBrush(const QBrush &brush)
QLabel::setText
void setText(const QString &)
QLayout::setMargin
void setMargin(int margin)
KCalcBitset::KCalcBitset
KCalcBitset(QWidget *parent=0)
Definition: kcalc_bitset.cpp:61
QWidget::rect
QRect rect() const
QWidget::font
const QFont & font() const
QFrame
KCalcBitset::valueChanged
void valueChanged(quint64 value)
QGridLayout::addLayout
void addLayout(QLayout *layout, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QAbstractButton::text
QString text() const
QPen
QPaintEvent
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QLabel
BitButton
Definition: bitbutton.h:27
kcalc_bitset.h
QBoxLayout::setSpacing
void setSpacing(int spacing)
QFont::pointSize
int pointSize() const
KCalcBitset::slotToggleBit
void slotToggleBit(int bit)
Definition: kcalc_bitset.cpp:139
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