Commit 73596fd2 authored by Niklas Freund's avatar Niklas Freund
Browse files

Fixed bitset buttons having minimally different sizes

parent 46043ff1
......@@ -28,6 +28,7 @@ BitButton::BitButton(QWidget *parent)
}
setMinimumSize(size);
setRenderSize(size);
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
this->setAttribute(Qt::WA_Hover, true);
......@@ -52,6 +53,27 @@ void BitButton::setOn(bool value)
update();
}
//------------------------------------------------------------------------------
// Name: renderSize
// Desc: returns current render size
//------------------------------------------------------------------------------
void BitButton::setRenderSize(const QSize &size)
{
if (renderSize_) {
delete renderSize_;
}
renderSize_ = new QSize(size);
}
//------------------------------------------------------------------------------
// Name: renderSize
// Desc: returns current render size
//------------------------------------------------------------------------------
const QSize& BitButton::renderSize() const
{
return *renderSize_;
}
//------------------------------------------------------------------------------
// Name: enterEvent
// Desc: sets to true the "over" variable on Enter event
......
......@@ -17,6 +17,8 @@ public:
explicit BitButton(QWidget *parent = nullptr);
Q_REQUIRED_RESULT bool isOn() const;
void setOn(bool value);
void setRenderSize(const QSize &size);
const QSize &renderSize() const;
protected:
void paintEvent(QPaintEvent *event) override;
......@@ -26,5 +28,6 @@ private:
void leaveEvent(QEvent *event) override;
bool on_ = false;
bool over_ = false;
QSize* renderSize_ = nullptr;
};
......@@ -43,12 +43,9 @@ void BitButton::paintEvent(QPaintEvent *)
painter.setBrush(QColor(palette().text().color().red(), palette().text().color().green(), palette().text().color().blue(), alpha));
}
// Calculate button size (make it square)
// Prepare button size (should be square)
QRect square = rect();
if (square.width() > square.height())
square.setWidth(square.height());
else if (square.height() > square.width())
square.setHeight(square.width());
square.setSize(renderSize());
// Draw button
painter.translate(QPoint(0, (rect().height() - square.height()) / 2)); // center button
......@@ -159,3 +156,42 @@ void KCalcBitset::slotToggleBit(QAbstractButton *button)
Q_EMIT valueChanged(value_);
}
}
//------------------------------------------------------------------------------
// Name: resizeEvent
// Desc: make sure all bitButtons have the same size
//------------------------------------------------------------------------------
void KCalcBitset::resizeEvent(QResizeEvent *event)
{
// Call the overridden resize event
QFrame::resizeEvent(event);
// Get the minimum size of all buttons
int minWidth = INT_MAX;
int minHeight = INT_MAX;
for (QObject *obj : bit_button_group_->buttons()) {
if (auto const button = qobject_cast<BitButton *>(obj)) {
minWidth = qMin(minWidth, button->rect().width());
minHeight = qMin(minHeight, button->rect().height());
}
}
// If this worked, set the renderSize for all BitButtons
if (minWidth != INT_MAX && minHeight != INT_MAX) {
// Make sure the size is square
if (minWidth > minHeight)
minWidth = minHeight;
else if (minHeight > minWidth)
minHeight = minWidth;
// Set it for all buttons
for (QObject *obj : bit_button_group_->buttons()) {
if (auto const button = qobject_cast<BitButton *>(obj)) {
QSize size = QSize(button->renderSize());
size.setWidth(minWidth);
size.setHeight(minHeight);
button->setRenderSize(size);
}
}
}
}
......@@ -19,6 +19,9 @@ public:
explicit KCalcBitset(QWidget *parent = nullptr);
Q_REQUIRED_RESULT quint64 getValue() const;
protected:
void resizeEvent(QResizeEvent *event) override;
public Q_SLOTS:
void setValue(quint64 value);
void slotToggleBit(QAbstractButton *button);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment