Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
KCalc
Commits
576c1ba0
Commit
576c1ba0
authored
Jun 18, 2021
by
Laurent Montel
😁
Browse files
Use clang format => normalize formatting
parent
ad3960de
Changes
33
Expand all
Hide whitespace changes
Inline
Side-by-side
bitbutton.cpp
View file @
576c1ba0
...
...
@@ -7,7 +7,7 @@ Copyright (C) 2006 Michel Marti
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
...
...
@@ -22,70 +22,69 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "bitbutton.h"
#include <QApplication>
//------------------------------------------------------------------------------
// Name: BitButton
// Desc: constructor
//------------------------------------------------------------------------------
BitButton
::
BitButton
(
QWidget
*
parent
)
:
QAbstractButton
(
parent
)
{
BitButton
::
BitButton
(
QWidget
*
parent
)
:
QAbstractButton
(
parent
)
{
// too many bits for tab focus
setFocusPolicy
(
Qt
::
ClickFocus
);
// too many bits for tab focus
setFocusPolicy
(
Qt
::
ClickFocus
);
// size button by font
QSize
size
=
fontMetrics
().
size
(
0
,
QStringLiteral
(
"M"
)
);
// size button by font
QSize
size
=
fontMetrics
().
size
(
0
,
QStringLiteral
(
"M"
));
if
(
size
.
width
()
<
size
.
height
())
{
size
.
setHeight
(
size
.
width
());
}
else
{
size
.
setWidth
(
size
.
height
());
}
setFixedSize
(
size
);
if
(
size
.
width
()
<
size
.
height
())
{
size
.
setHeight
(
size
.
width
());
}
else
{
size
.
setWidth
(
size
.
height
());
}
this
->
setAttribute
(
Qt
::
WA_Hover
,
true
);
setFixedSize
(
size
);
this
->
setAttribute
(
Qt
::
WA_Hover
,
true
);
}
//------------------------------------------------------------------------------
// Name: isOn
// Desc: returns true if this bit-button is "on"
//------------------------------------------------------------------------------
bool
BitButton
::
isOn
()
const
{
return
on_
;
bool
BitButton
::
isOn
()
const
{
return
on_
;
}
//------------------------------------------------------------------------------
// Name: setOn
// Desc: changes the "on" value for the bitset
//------------------------------------------------------------------------------
void
BitButton
::
setOn
(
bool
value
)
{
on_
=
value
;
update
();
void
BitButton
::
setOn
(
bool
value
)
{
on_
=
value
;
update
();
}
//------------------------------------------------------------------------------
// Name: enterEvent
// Desc: sets to true the "over" variable on Enter event
//------------------------------------------------------------------------------
void
BitButton
::
enterEvent
(
QEvent
*
event
)
{
if
(
event
->
type
()
==
QEvent
::
Enter
)
{
over_
=
true
;
update
();
}
void
BitButton
::
enterEvent
(
QEvent
*
event
)
{
if
(
event
->
type
()
==
QEvent
::
Enter
)
{
over_
=
true
;
update
();
}
}
//------------------------------------------------------------------------------
// Name: leaveEvent
// Desc: sets to false the "over" variable on Leave event
//------------------------------------------------------------------------------
void
BitButton
::
leaveEvent
(
QEvent
*
event
)
{
if
(
event
->
type
()
==
QEvent
::
Leave
)
{
over_
=
false
;
update
();
}
void
BitButton
::
leaveEvent
(
QEvent
*
event
)
{
if
(
event
->
type
()
==
QEvent
::
Leave
)
{
over_
=
false
;
update
();
}
}
bitbutton.h
View file @
576c1ba0
...
...
@@ -7,7 +7,7 @@ Copyright (C) 2006 Michel Marti
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
...
...
@@ -24,20 +24,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <QAbstractButton>
class
BitButton
:
public
QAbstractButton
{
Q_OBJECT
class
BitButton
:
public
QAbstractButton
{
Q_OBJECT
public:
explicit
BitButton
(
QWidget
*
parent
=
nullptr
);
Q_REQUIRED_RESULT
bool
isOn
()
const
;
void
setOn
(
bool
value
);
void
setOn
(
bool
value
);
protected:
void
paintEvent
(
QPaintEvent
*
event
)
override
;
void
paintEvent
(
QPaintEvent
*
event
)
override
;
private:
void
enterEvent
(
QEvent
*
event
)
override
;
void
leaveEvent
(
QEvent
*
event
)
override
;
void
enterEvent
(
QEvent
*
event
)
override
;
void
leaveEvent
(
QEvent
*
event
)
override
;
bool
on_
=
false
;
bool
over_
=
false
;
};
...
...
kcalc.cpp
View file @
576c1ba0
This diff is collapsed.
Click to expand it.
kcalc.h
View file @
576c1ba0
...
...
@@ -7,7 +7,7 @@ Copyright (C) 1996 - 2000 Bernd Johannes Wuebben
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
...
...
@@ -47,33 +47,37 @@ class KCalcStatusBar;
operation button is pressed, e.g. "core.Plus(display_number)".
*/
#include "kcalc_core.h"
#include "kcalc_button.h"
#include "kcalc_const_button.h"
#include "kcalc_core.h"
#include "ui_kcalc.h"
#include "ui_general.h"
#include "ui_fonts.h"
#include "ui_constants.h"
#include "ui_colors.h"
#include "ui_constants.h"
#include "ui_fonts.h"
#include "ui_general.h"
#include "ui_kcalc.h"
#include <array>
#include <kxmlguiwindow.h>
class
General
:
public
QWidget
,
public
Ui
::
General
class
General
:
public
QWidget
,
public
Ui
::
General
{
Q_OBJECT
public:
explicit
General
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
explicit
General
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
setupUi
(
this
);
}
};
class
Fonts
:
public
QWidget
,
public
Ui
::
Fonts
class
Fonts
:
public
QWidget
,
public
Ui
::
Fonts
{
Q_OBJECT
public:
explicit
Fonts
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
explicit
Fonts
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
setupUi
(
this
);
}
};
...
...
@@ -82,7 +86,9 @@ class Constants : public QWidget, public Ui::Constants
{
Q_OBJECT
public:
explicit
Constants
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
explicit
Constants
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
setupUi
(
this
);
}
};
...
...
@@ -91,12 +97,13 @@ class Colors : public QWidget, public Ui::Colors
{
Q_OBJECT
public:
explicit
Colors
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
explicit
Colors
(
QWidget
*
parent
)
:
QWidget
(
parent
)
{
setupUi
(
this
);
}
};
class
KCalculator
:
public
KXmlGuiWindow
,
private
Ui
::
KCalculator
{
Q_OBJECT
...
...
@@ -110,12 +117,8 @@ Q_SIGNALS:
void
switchShowAccels
(
bool
);
public:
enum
UpdateFlag
{
UPDATE_FROM_CORE
=
1
,
UPDATE_STORE_RESULT
=
2
};
enum
UpdateFlag
{
UPDATE_FROM_CORE
=
1
,
UPDATE_STORE_RESULT
=
2
};
Q_DECLARE_FLAGS
(
UpdateFlags
,
UpdateFlag
)
private:
...
...
@@ -124,14 +127,14 @@ private:
void
updateGeometry
();
void
setupMainActions
();
void
setupKeys
();
void
setupNumberKeys
();
void
setupRightKeypad
();
void
setupNumericKeypad
();
void
setupLogicKeys
();
void
setupScientificKeys
();
void
setupStatisticKeys
();
void
setupConstantsKeys
();
void
setupMiscKeys
();
void
setupNumberKeys
();
void
setupRightKeypad
();
void
setupNumericKeypad
();
void
setupLogicKeys
();
void
setupScientificKeys
();
void
setupStatisticKeys
();
void
setupConstantsKeys
();
void
setupMiscKeys
();
void
keyPressEvent
(
QKeyEvent
*
e
)
override
;
void
keyReleaseEvent
(
QKeyEvent
*
e
)
override
;
void
setPrecision
();
...
...
@@ -140,14 +143,14 @@ private:
void
updateDisplay
(
UpdateFlags
flags
);
KCalcStatusBar
*
statusBar
();
// button sets
void
showMemButtons
(
bool
toggled
);
void
showStatButtons
(
bool
toggled
);
void
showScienceButtons
(
bool
toggled
);
void
showLogicButtons
(
bool
toggled
);
KCalcConstMenu
*
createConstantsMenu
();
KCalcConstMenu
*
createConstantsMenu
();
protected
Q_SLOTS
:
void
changeButtonNames
();
...
...
@@ -211,7 +214,7 @@ protected Q_SLOTS:
void
slotStatClearDataclicked
();
void
slotHyptoggled
(
bool
flag
);
void
slotConstclicked
(
int
);
void
slotBackspaceclicked
();
void
slotBackspaceclicked
();
void
slotConstantToDisplay
(
const
science_constant
&
const_chosen
);
void
slotChooseScientificConst0
(
const
science_constant
&
);
...
...
@@ -229,25 +232,11 @@ protected Q_SLOTS:
void
slotPaste
();
private:
enum
StatusField
{
ShiftField
=
0
,
BaseField
,
AngleField
,
MemField
};
enum
AngleMode
{
DegMode
=
0
,
RadMode
,
GradMode
};
enum
BaseMode
{
BinMode
=
2
,
OctMode
=
8
,
DecMode
=
10
,
HexMode
=
16
};
enum
StatusField
{
ShiftField
=
0
,
BaseField
,
AngleField
,
MemField
};
enum
AngleMode
{
DegMode
=
0
,
RadMode
,
GradMode
};
enum
BaseMode
{
BinMode
=
2
,
OctMode
=
8
,
DecMode
=
10
,
HexMode
=
16
};
private:
bool
shift_mode_
=
false
;
...
...
@@ -256,21 +245,21 @@ private:
int
angle_mode_
;
// angle modes for trigonometric values
KCalcConstMenu
*
constants_menu_
=
nullptr
;
KCalcConstMenu
*
constants_menu_
=
nullptr
;
Constants
*
constants_
=
nullptr
;
// this is the dialog for configuring const buttons
Constants
*
constants_
=
nullptr
;
// this is the dialog for configuring const buttons
QButtonGroup
*
angle_choose_group_
=
nullptr
;
QButtonGroup
*
base_choose_group_
=
nullptr
;
QButtonGroup
*
angle_choose_group_
=
nullptr
;
QButtonGroup
*
base_choose_group_
=
nullptr
;
// num_button_group_: 0-9 = digits, 0xA-0xF = hex-keys
QButtonGroup
*
num_button_group_
=
nullptr
;
QButtonGroup
*
num_button_group_
=
nullptr
;
QList
<
QAbstractButton
*>
logic_buttons_
;
QList
<
QAbstractButton
*>
scientific_buttons_
;
QList
<
QAbstractButton
*>
stat_buttons_
;
QList
<
QAbstractButton
*>
const_buttons_
;
QList
<
QAbstractButton
*>
logic_buttons_
;
QList
<
QAbstractButton
*>
scientific_buttons_
;
QList
<
QAbstractButton
*>
stat_buttons_
;
QList
<
QAbstractButton
*>
const_buttons_
;
std
::
array
<
QLabel
*
,
4
>
base_conversion_labels_
;
std
::
array
<
QLabel
*
,
4
>
base_conversion_labels_
;
KToggleAction
*
action_bitset_show_
=
nullptr
;
KToggleAction
*
action_constants_show_
=
nullptr
;
...
...
@@ -280,10 +269,10 @@ private:
KToggleAction
*
action_mode_statistic_
=
nullptr
;
KToggleAction
*
action_mode_numeral_
=
nullptr
;
QList
<
QAbstractButton
*>
function_button_list_
;
QList
<
QAbstractButton
*>
stat_button_list_
;
QList
<
QAbstractButton
*>
mem_button_list_
;
QList
<
QAbstractButton
*>
operation_button_list_
;
QList
<
QAbstractButton
*>
function_button_list_
;
QList
<
QAbstractButton
*>
stat_button_list_
;
QList
<
QAbstractButton
*>
mem_button_list_
;
QList
<
QAbstractButton
*>
operation_button_list_
;
CalcEngine
core
;
};
...
...
kcalc_bitset.cpp
View file @
576c1ba0
...
...
@@ -7,7 +7,7 @@ Copyright (C) 2006 Michel Marti
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
...
...
@@ -22,14 +22,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "kcalc_bitset.h"
#include "bitbutton.h"
#include <KLocalizedString>
#include <QButtonGroup>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPainter>
#include <KLocalizedString>
// TODO: I think it would actually be appropriate to use a std::bitset<64>
// for the internal representation of this class perhaps
...
...
@@ -39,107 +37,108 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// Name: paintEvent
// Desc: draws the button
//------------------------------------------------------------------------------
void
BitButton
::
paintEvent
(
QPaintEvent
*
)
{
uint8_t
alpha
=
0x60
;
QPainter
painter
(
this
);
painter
.
setRenderHint
(
QPainter
::
Antialiasing
,
true
);
QPen
pen
(
palette
().
text
(),
1
);
pen
.
setJoinStyle
(
Qt
::
MiterJoin
);
painter
.
setPen
(
pen
);
if
(
on_
)
{
painter
.
setBrush
(
palette
().
text
());
alpha
=
0xB0
;
}
else
{
painter
.
setBrush
(
palette
().
base
());
}
if
(
over_
)
{
painter
.
setBrush
(
QColor
(
palette
().
text
().
color
().
red
(),
palette
().
text
().
color
().
green
(),
palette
().
text
().
color
().
blue
(),
alpha
));
}
painter
.
drawRect
(
rect
().
adjusted
(
1
,
1
,
-
1
,
-
1
));
void
BitButton
::
paintEvent
(
QPaintEvent
*
)
{
uint8_t
alpha
=
0x60
;
QPainter
painter
(
this
);
painter
.
setRenderHint
(
QPainter
::
Antialiasing
,
true
);
QPen
pen
(
palette
().
text
(),
1
);
pen
.
setJoinStyle
(
Qt
::
MiterJoin
);
painter
.
setPen
(
pen
);
if
(
on_
)
{
painter
.
setBrush
(
palette
().
text
());
alpha
=
0xB0
;
}
else
{
painter
.
setBrush
(
palette
().
base
());
}
if
(
over_
)
{
painter
.
setBrush
(
QColor
(
palette
().
text
().
color
().
red
(),
palette
().
text
().
color
().
green
(),
palette
().
text
().
color
().
blue
(),
alpha
));
}
painter
.
drawRect
(
rect
().
adjusted
(
1
,
1
,
-
1
,
-
1
));
}
//------------------------------------------------------------------------------
// Name: KCalcBitset
// Desc: constructor
//------------------------------------------------------------------------------
KCalcBitset
::
KCalcBitset
(
QWidget
*
parent
)
:
QFrame
(
parent
),
bit_button_group_
(
new
QButtonGroup
(
this
)),
value_
(
0
)
{
setFrameStyle
(
QFrame
::
Panel
|
QFrame
::
Sunken
);
connect
(
bit_button_group_
,
QOverload
<
QAbstractButton
*>::
of
(
&
QButtonGroup
::
buttonClicked
),
this
,
&
KCalcBitset
::
slotToggleBit
);
// smaller label font
QFont
fnt
=
font
();
if
(
fnt
.
pointSize
()
>
6
)
{
fnt
.
setPointSize
(
fnt
.
pointSize
()
-
1
);
}
KCalcBitset
::
KCalcBitset
(
QWidget
*
parent
)
:
QFrame
(
parent
)
,
bit_button_group_
(
new
QButtonGroup
(
this
))
,
value_
(
0
)
{
setFrameStyle
(
QFrame
::
Panel
|
QFrame
::
Sunken
);
connect
(
bit_button_group_
,
QOverload
<
QAbstractButton
*>::
of
(
&
QButtonGroup
::
buttonClicked
),
this
,
&
KCalcBitset
::
slotToggleBit
);
// smaller label font
QFont
fnt
=
font
();
if
(
fnt
.
pointSize
()
>
6
)
{
fnt
.
setPointSize
(
fnt
.
pointSize
()
-
1
);
}
// main layout
auto
layout
=
new
QGridLayout
(
this
);
// main layout
auto
layout
=
new
QGridLayout
(
this
);
layout
->
setContentsMargins
(
2
,
2
,
2
,
2
);
layout
->
setSpacing
(
0
);
// create bits
int
bitCounter
=
63
;
for
(
int
rows
=
0
;
rows
<
2
;
rows
++
)
{
for
(
int
cols
=
0
;
cols
<
4
;
cols
++
)
{
// two rows of four words
auto
const
wordlayout
=
new
QHBoxLayout
();
layout
->
setSpacing
(
0
);
// create bits
int
bitCounter
=
63
;
for
(
int
rows
=
0
;
rows
<
2
;
rows
++
)
{
for
(
int
cols
=
0
;
cols
<
4
;
cols
++
)
{
// two rows of four words
auto
const
wordlayout
=
new
QHBoxLayout
();
wordlayout
->
setContentsMargins
(
2
,
2
,
2
,
2
);
wordlayout
->
setSpacing
(
2
);
layout
->
addLayout
(
wordlayout
,
rows
,
cols
);
for
(
int
bit
=
0
;
bit
<
8
;
bit
++
)
{
auto
const
tmpBitButton
=
new
BitButton
(
this
);
tmpBitButton
->
setToolTip
(
i18n
(
"Bit %1 = %2"
,
bitCounter
,
1ULL
<<
bitCounter
));
wordlayout
->
addWidget
(
tmpBitButton
);
bit_button_group_
->
addButton
(
tmpBitButton
,
bitCounter
);
bitCounter
--
;
}
// label word
auto
label
=
new
QLabel
(
this
);
label
->
setText
(
QString
::
number
(
bitCounter
+
1
));
label
->
setFont
(
fnt
);
wordlayout
->
addWidget
(
label
);
}
}
wordlayout
->
setSpacing
(
2
);
layout
->
addLayout
(
wordlayout
,
rows
,
cols
);
for
(
int
bit
=
0
;
bit
<
8
;
bit
++
)
{
auto
const
tmpBitButton
=
new
BitButton
(
this
);
tmpBitButton
->
setToolTip
(
i18n
(
"Bit %1 = %2"
,
bitCounter
,
1ULL
<<
bitCounter
));
wordlayout
->
addWidget
(
tmpBitButton
);
bit_button_group_
->
addButton
(
tmpBitButton
,
bitCounter
);
bitCounter
--
;
}
// label word
auto
label
=
new
QLabel
(
this
);
label
->
setText
(
QString
::
number
(
bitCounter
+
1
));
label
->
setFont
(
fnt
);
wordlayout
->
addWidget
(
label
);
}
}
}
//------------------------------------------------------------------------------
// Name: setValue
// Desc: set the value of the bitset based on an unsigned 64-bit number
//------------------------------------------------------------------------------
void
KCalcBitset
::
setValue
(
quint64
value
)
{
if
(
value_
==
value
)
{
// don't waste time if there was no change..
return
;
}
value_
=
value
;
// set each bit button
for
(
int
i
=
0
;
i
<
64
;
i
++
)
{
if
(
auto
bb
=
qobject_cast
<
BitButton
*>
(
bit_button_group_
->
button
(
i
)))
{
bb
->
setOn
(
value
&
1
);
}
value
>>=
1
;
}
void
KCalcBitset
::
setValue
(
quint64
value
)
{
if
(
value_
==
value
)
{
// don't waste time if there was no change..
return
;
}
value_
=
value
;
// set each bit button
for
(
int
i
=
0
;
i
<
64
;
i
++
)
{
if
(
auto
bb
=
qobject_cast
<
BitButton
*>
(
bit_button_group_
->
button
(
i
)))
{
bb
->
setOn
(
value
&
1
);
}
value
>>=
1
;
}
}
//------------------------------------------------------------------------------
// Name: getValue
// Desc: returns the bitset value as an unsigned 64-bit number
//------------------------------------------------------------------------------
quint64
KCalcBitset
::
getValue
()
const
{
quint64
KCalcBitset
::
getValue
()
const
{
return
value_
;
}
...
...
@@ -147,13 +146,12 @@ quint64 KCalcBitset::getValue() const {
// Name: slotToggleBit
// Desc: inverts the value of a single bit
//------------------------------------------------------------------------------
void
KCalcBitset
::
slotToggleBit
(
QAbstractButton
*
button
)
{
void
KCalcBitset
::
slotToggleBit
(
QAbstractButton
*
button
)
{
if
(
button
)
{
const
int
bit
=
bit_button_group_
->
id
(
button
);
const
quint64
nv
=
getValue
()
^
(
1LL
<<
bit
);
const
quint64
nv
=
getValue
()
^
(
1LL
<<
bit
);
setValue
(
nv
);
Q_EMIT
valueChanged
(
value_
);
}
}
kcalc_bitset.h
View file @
576c1ba0
...
...
@@ -7,7 +7,7 @@ Copyright (C) 2006 Michel Marti
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
...
...
@@ -26,23 +26,24 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
class
QButtonGroup
;
class
QAbstractButton
;
class
KCalcBitset
:
public
QFrame
{
Q_OBJECT
class
KCalcBitset
:
public
QFrame
{
Q_OBJECT
public:
explicit
KCalcBitset
(
QWidget
*
parent
=
nullptr
);
explicit
KCalcBitset
(
QWidget
*
parent
=
nullptr
);
Q_REQUIRED_RESULT
quint64
getValue
()
const
;
public
Q_SLOTS
:
void
setValue
(
quint64
value
);
void
slotToggleBit
(
QAbstractButton
*
button
);