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 "dynamic_font_size_label.h"
#include <QPainter>
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();
}
}

View file

@ -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

View file

@ -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);

View file

@ -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 <QHBoxLayout>
#include <QLabel>
@ -46,8 +47,8 @@ private:
CardInfoPerSet setInfoForCard;
QString zoneName;
QHBoxLayout *layout;
QPushButton *incrementButton;
QPushButton *decrementButton;
DynamicFontSizePushButton *incrementButton;
DynamicFontSizePushButton *decrementButton;
QLabel *cardCountInZone;
bool hovered;