Style buttons again.

This commit is contained in:
Lukas Brübach 2024-12-16 01:30:47 +01:00 committed by ZeldaZach
parent f674d601ab
commit b5b919b738
No known key found for this signature in database
4 changed files with 46 additions and 16 deletions

View file

@ -3,39 +3,69 @@
#include <QDebug> #include <QDebug>
#include "dynamic_font_size_label.h" #include "dynamic_font_size_label.h"
#include <QPainter>
DynamicFontSizePushButton::DynamicFontSizePushButton(QWidget* parent) DynamicFontSizePushButton::DynamicFontSizePushButton(QWidget* parent)
:QPushButton(parent) : QPushButton(parent)
{ {
} }
void DynamicFontSizePushButton::paintEvent(QPaintEvent *event) void DynamicFontSizePushButton::paintEvent(QPaintEvent *event)
{ {
//QElapsedTimer timer; // Call the base class paintEvent to preserve any other painting behavior
//timer.start(); QPushButton::paintEvent(event);
// Adjust the font size dynamically based on the text
QFont newFont = font(); QFont newFont = font();
float fontSize = DynamicFontSizeLabel::getWidgetMaximumFontSize(this, this->text()); float fontSize = DynamicFontSizeLabel::getWidgetMaximumFontSize(this, this->text());
newFont.setPointSizeF(fontSize); newFont.setPointSizeF(fontSize);
setFont(newFont); setFont(newFont);
QPushButton::paintEvent(event); // Get painter for custom painting
//LOG(true, "Paint delay" << ((float)timer.nsecsElapsed())/1000000.0 << " mS"); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// Paint the background with a linear gradient (normal state)
QLinearGradient gradient(0, 0, 0, height());
if (isDown()) {
// Pressed state
gradient.setColorAt(0, QColor(128, 128, 128));
gradient.setColorAt(1, QColor(64, 64, 64));
} else if (underMouse()) {
// Hover state
gradient.setColorAt(0, QColor(96, 96, 96));
gradient.setColorAt(1, QColor(48, 48, 48));
} else {
// Normal state
gradient.setColorAt(0, QColor(64, 64, 64)); // start color
gradient.setColorAt(1, QColor(32, 32, 32)); // end color
}
painter.setBrush(gradient);
painter.setPen(Qt::NoPen); // No border
painter.drawRect(rect());
// Paint the button text
painter.setPen(QPen(textColor.isValid() ? textColor : QColor(255, 255, 255))); // Set text color
painter.drawText(rect(), Qt::AlignCenter, text());
} }
void DynamicFontSizePushButton::setTextColor(QColor color){ void DynamicFontSizePushButton::setTextColor(QColor color)
if (color.isValid() && color!=textColor){ {
if (color.isValid() && color != textColor) {
textColor = color; textColor = color;
setStyleSheet("color : "+color.name()+";"); update(); // Request a repaint to update the text color
} }
} }
void DynamicFontSizePushButton::setTextAndColor(const QString &text, QColor color){ void DynamicFontSizePushButton::setTextAndColor(const QString &text, QColor color)
{
setTextColor(color); setTextColor(color);
setText(text); setText(text);
} }
QColor DynamicFontSizePushButton::getTextColor(){ QColor DynamicFontSizePushButton::getTextColor()
{
return textColor; return textColor;
} }
@ -49,4 +79,4 @@ QSize DynamicFontSizePushButton::minimumSizeHint() const
QSize DynamicFontSizePushButton::sizeHint() const QSize DynamicFontSizePushButton::sizeHint() const
{ {
return QWidget::sizeHint(); return QWidget::sizeHint();
} }

View file

@ -13,7 +13,6 @@ public:
/* This method overwrite stylesheet */ /* This method overwrite stylesheet */
void setTextColor(QColor color); void setTextColor(QColor color);
QColor getTextColor(); QColor getTextColor();
void setTextAndColor(const QString &text, QColor color=QColor::Invalid); void setTextAndColor(const QString &text, QColor color=QColor::Invalid);
// QWidget interface // QWidget interface

View file

@ -23,9 +23,9 @@ CardAmountWidget::CardAmountWidget(QWidget *parent,
layout->setAlignment(Qt::AlignHCenter); layout->setAlignment(Qt::AlignHCenter);
incrementButton = new DynamicFontSizePushButton(this); incrementButton = new DynamicFontSizePushButton(this);
incrementButton->setText("+"); incrementButton->setTextAndColor("+", Qt::white);
decrementButton = new DynamicFontSizePushButton(this); decrementButton = new DynamicFontSizePushButton(this);
decrementButton->setText("-"); decrementButton->setTextAndColor("-", Qt::white);
incrementButton->setFixedSize(parentWidget()->size().width() / 3, parentWidget()->size().height() / 9); incrementButton->setFixedSize(parentWidget()->size().width() / 3, parentWidget()->size().height() / 9);
decrementButton->setFixedSize(parentWidget()->size().width() / 3, parentWidget()->size().height() / 9); decrementButton->setFixedSize(parentWidget()->size().width() / 3, parentWidget()->size().height() / 9);

View file

@ -6,6 +6,7 @@
#include "../../../../deck/deck_view.h" #include "../../../../deck/deck_view.h"
#include "../../../../game/cards/card_database.h" #include "../../../../game/cards/card_database.h"
#include "../../../tabs/tab_deck_editor.h" #include "../../../tabs/tab_deck_editor.h"
#include "../general/display/dynamic_font_size_push_button.h"
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
@ -46,8 +47,8 @@ private:
CardInfoPerSet setInfoForCard; CardInfoPerSet setInfoForCard;
QString zoneName; QString zoneName;
QHBoxLayout *layout; QHBoxLayout *layout;
QPushButton *incrementButton; DynamicFontSizePushButton *incrementButton;
QPushButton *decrementButton; DynamicFontSizePushButton *decrementButton;
QLabel *cardCountInZone; QLabel *cardCountInZone;
bool hovered; bool hovered;