#include "aerobutton.h" #include #include #include namespace SIF { struct AeroButton::Private { bool hovered; QColor color; QColor highlight; QColor shadow; qreal opacity; int roundness; Private(AeroButton *q); ~Private(); QRect calculateIconPosition(const QRect &button_rect, const QSize &icon_size); }; AeroButton::Private::Private(AeroButton *q) : hovered(false), color(Qt::gray), highlight(Qt::lightGray), shadow(Qt::black), opacity(1.0), roundness(0) { QPalette pal = q->palette(); pal.setBrush(QPalette::WindowText, Qt::white); pal.setBrush(QPalette::Text, Qt::white); pal.setBrush(QPalette::ButtonText, Qt::white); q->setPalette(pal); } QRect AeroButton::Private::calculateIconPosition(const QRect &button_rect, const QSize &icon_size) { int x = (button_rect.width() / 2) - (icon_size.width() / 2); int y = (button_rect.height() / 2) - (icon_size.height() / 2); int width = icon_size.width(); int height = icon_size.height(); QRect icon_position(x, y, width, height); return icon_position; } AeroButton::Private::~Private() { } AeroButton::AeroButton(QWidget * parent) : QPushButton(parent), d(new Private(this)) { } AeroButton::AeroButton(const QString & text, QWidget * parent) : QPushButton(text, parent), d(new Private(this)) { } AeroButton::AeroButton(const QIcon & icon, const QString & text, QWidget * parent) : QPushButton(icon, text, parent), d(new Private(this)) { } AeroButton::~AeroButton() { delete d; } void AeroButton::setColor(const QColor &color) { d->color = color; update(); } void AeroButton::setHighlight(const QColor &highlight) { d->highlight = highlight; update(); } void AeroButton::setShadow(const QColor &shadow) { d->shadow = shadow; update(); } //Range: 0.0 [invisible] - 1.0 [opaque] void AeroButton::setOpacity(qreal opacity) { d->opacity = opacity; update(); } //Range: 0 [rectangle] - 99 [oval] void AeroButton::setRoundness(int roundness) { d->roundness = roundness; update(); } void AeroButton::paintEvent(QPaintEvent * pe) { Q_UNUSED(pe); QRect rect = this->rect(); QStyleOptionButton option; initStyleOption(&option); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); painter.save(); painter.fillRect(rect, Qt::transparent); //test for state changes QColor button_color; if(this->isEnabled()) { d->hovered ? button_color = d->highlight : button_color = d->color; if(option.state & QStyle::State_Sunken ) { button_color = d->highlight.darker(250); } } else { button_color = QColor(50, 50, 50); } QRect button_rect = this->geometry(); //outline painter.setPen(QPen(QBrush(Qt::black), 2.0)); QPainterPath outline; outline.addRoundRect(0, 0, button_rect.width(), button_rect.height(), d->roundness, d->roundness); painter.setOpacity(d->opacity); painter.drawPath(outline); //gradient QLinearGradient gradient(0, 0, 0, button_rect.height()); gradient.setSpread(QGradient::ReflectSpread); gradient.setColorAt(0.0, button_color); gradient.setColorAt(0.4, d->shadow); gradient.setColorAt(0.6, d->shadow); gradient.setColorAt(1.0, button_color); QBrush brush(gradient); painter.setBrush(brush); painter.setPen(QPen(QBrush(button_color), 2.0, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin )); //main button QPainterPath painter_path; painter_path.addRoundRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, d->roundness, d->roundness); painter.setClipPath(painter_path); painter.setOpacity(d->opacity); painter.drawRoundRect(1, 1, button_rect.width() - 2, button_rect.height() - 2, d->roundness, d->roundness); //glass highlight painter.setBrush(QBrush(Qt::white)); painter.setPen(QPen(QBrush(Qt::white), 0.01, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin)); painter.setOpacity(0.30); painter.drawRect(1, 1, button_rect.width() - 2, (button_rect.height() / 2) - 2); painter.restore(); QIcon icon = this->icon(); QString text = this->text(); QRect icon_position; //icon if(!icon.isNull()) { QSize icon_size = this->iconSize(); icon_position = d->calculateIconPosition(button_rect, icon_size); if( !text.isEmpty() ) { icon_position.setX(20); icon_position.setWidth(icon_size.width()); } painter.setOpacity(1.0); painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size))); } //button text if(!text.isNull()) { QFont font = this->font(); font.setBold(true); painter.setFont(font); painter.setOpacity(1.0); painter.drawText(icon_position.width(), 0, button_rect.width() - icon_position.width(), button_rect.height(), Qt::AlignCenter, text); } if(option.state & QStyle::State_On) { QPen old = painter.pen(); QColor h = d->highlight; h.setAlpha(150); QPen pen(h, 2); painter.setPen(pen); painter.drawLine(rect.bottomLeft()-QPoint(-6, 3), rect.bottomRight()-QPoint(6, 3)); painter.setPen(old); } } void AeroButton::enterEvent(QEvent * e) { Q_UNUSED(e); d->hovered = true; this->update(); } void AeroButton::leaveEvent(QEvent * e) { Q_UNUSED(e); d->hovered = false; this->update(); } void AeroButton::mousePressEvent(QMouseEvent * e) { Q_UNUSED(e); this->update(); QPushButton::mousePressEvent(e); } void AeroButton::mouseReleaseEvent(QMouseEvent * e) { Q_UNUSED(e); this->update(); QPushButton::mouseReleaseEvent(e); } }