From b5b919b73813f4f72def7d475959f241e03b86aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20Br=C3=BCbach?= Date: Mon, 16 Dec 2024 01:30:47 +0100 Subject: [PATCH] Style buttons again. --- .../display/dynamic_font_size_push_button.cpp | 52 +++++++++++++++---- .../display/dynamic_font_size_push_button.h | 1 - .../printing_selector/card_amount_widget.cpp | 4 +- .../printing_selector/card_amount_widget.h | 5 +- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.cpp b/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.cpp index bb43649ca..c3b6c18ed 100644 --- a/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.cpp +++ b/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.cpp @@ -3,39 +3,69 @@ #include #include "dynamic_font_size_label.h" +#include + DynamicFontSizePushButton::DynamicFontSizePushButton(QWidget* parent) - :QPushButton(parent) + : QPushButton(parent) { } void DynamicFontSizePushButton::paintEvent(QPaintEvent *event) { - //QElapsedTimer timer; - //timer.start(); + // Call the base class paintEvent to preserve any other painting behavior + QPushButton::paintEvent(event); + // Adjust the font size dynamically based on the text QFont newFont = font(); float fontSize = DynamicFontSizeLabel::getWidgetMaximumFontSize(this, this->text()); newFont.setPointSizeF(fontSize); setFont(newFont); - QPushButton::paintEvent(event); - //LOG(true, "Paint delay" << ((float)timer.nsecsElapsed())/1000000.0 << " mS"); + // Get painter for custom painting + 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){ - if (color.isValid() && color!=textColor){ +void DynamicFontSizePushButton::setTextColor(QColor color) +{ + if (color.isValid() && color != textColor) { 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); setText(text); } -QColor DynamicFontSizePushButton::getTextColor(){ +QColor DynamicFontSizePushButton::getTextColor() +{ return textColor; } @@ -49,4 +79,4 @@ QSize DynamicFontSizePushButton::minimumSizeHint() const QSize DynamicFontSizePushButton::sizeHint() const { return QWidget::sizeHint(); -} \ No newline at end of file +} diff --git a/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.h b/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.h index 5cf963a55..1ed134e10 100644 --- a/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.h +++ b/cockatrice/src/client/ui/widgets/general/display/dynamic_font_size_push_button.h @@ -13,7 +13,6 @@ public: /* This method overwrite stylesheet */ void setTextColor(QColor color); QColor getTextColor(); - void setTextAndColor(const QString &text, QColor color=QColor::Invalid); // QWidget interface diff --git a/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.cpp b/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.cpp index 5ebfce123..2dc0e5556 100644 --- a/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.cpp +++ b/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.cpp @@ -23,9 +23,9 @@ CardAmountWidget::CardAmountWidget(QWidget *parent, layout->setAlignment(Qt::AlignHCenter); incrementButton = new DynamicFontSizePushButton(this); - incrementButton->setText("+"); + incrementButton->setTextAndColor("+", Qt::white); decrementButton = new DynamicFontSizePushButton(this); - decrementButton->setText("-"); + decrementButton->setTextAndColor("-", Qt::white); incrementButton->setFixedSize(parentWidget()->size().width() / 3, parentWidget()->size().height() / 9); decrementButton->setFixedSize(parentWidget()->size().width() / 3, parentWidget()->size().height() / 9); diff --git a/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.h b/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.h index 1aca49f9c..8866ad9f4 100644 --- a/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.h +++ b/cockatrice/src/client/ui/widgets/printing_selector/card_amount_widget.h @@ -6,6 +6,7 @@ #include "../../../../deck/deck_view.h" #include "../../../../game/cards/card_database.h" #include "../../../tabs/tab_deck_editor.h" +#include "../general/display/dynamic_font_size_push_button.h" #include #include @@ -46,8 +47,8 @@ private: CardInfoPerSet setInfoForCard; QString zoneName; QHBoxLayout *layout; - QPushButton *incrementButton; - QPushButton *decrementButton; + DynamicFontSizePushButton *incrementButton; + DynamicFontSizePushButton *decrementButton; QLabel *cardCountInZone; bool hovered;