#include "panelbutton.h" #include #include #include PanelButton::PanelButton(QWidget *parent) : QPushButton(parent) { _buttonAlignment = Qt::AlignCenter; } QSize PanelButton::minimumSizeHint() const { QRect rect = fontMetrics().boundingRect(text()); QSize size(rect.width() + 8, rect.height() + 8); if(size.width() < minimumWidth()) { size.setWidth(minimumWidth()); } if(size.height() < minimumHeight()) { size.setHeight(minimumHeight()); } return size; } void PanelButton::setButtonAlignment(int buttonAlignment) { _buttonAlignment = buttonAlignment; } int PanelButton::buttonAlignment() const { return _buttonAlignment; } void PanelButton::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QRect textRect = fontMetrics().boundingRect(text()); QRectF backgroundRect = rect(); QRectF borderRect = rect(); QPoint textPoint; QPainter p(this); QGradientStops stops; QLinearGradient backgroundGradient(QPointF(0, backgroundRect.top()), QPointF(0, backgroundRect.bottom())); stops.append(QGradientStop(0.00f, QColor(0xf3, 0xf3, 0xf3, 0xff))); stops.append(QGradientStop(0.50f, QColor(0xeb, 0xeb, 0xeb, 0xff))); stops.append(QGradientStop(0.51f, QColor(0xdd, 0xdd, 0xdd, 0xff))); stops.append(QGradientStop(1.00f, QColor(0xcd, 0xcd, 0xcd, 0xff))); backgroundGradient.setStops(stops); QPen borderPen; borderPen.setWidth(1); borderPen.setColor(QColor(0x70, 0x70, 0x70, 0xff)); QPen backgroundPen; backgroundPen.setWidth(1); backgroundPen.setColor(QColor(0xff, 0xff, 0xff, 0xef)); textPoint.setX(backgroundRect.width() / 2 - textRect.width() / 2); textPoint.setY(backgroundRect.height() / 2 - textRect.height() / 2); textPoint.setY(fontMetrics().ascent() + textPoint.y()); borderRect.setLeft(borderRect.left() + 0.5f); borderRect.setRight(borderRect.right() - 0.5f); borderRect.setTop(borderRect.top() + 0.5f); borderRect.setBottom(borderRect.bottom() - 0.5f); backgroundRect.setLeft(backgroundRect.left() + 1.5f); backgroundRect.setRight(backgroundRect.right() - 1.5f); backgroundRect.setTop(backgroundRect.top() + 1.5f); backgroundRect.setBottom(backgroundRect.bottom() - 1.5f); p.setPen(Qt::NoPen); p.setBrush(QBrush(backgroundGradient)); p.drawRect(backgroundRect); if(underMouse() && isDown()) { p.setBrush(QColor(0x00, 0x00, 0x00, 0x3F)); p.drawRect(backgroundRect); } else if(underMouse()) { p.setBrush(QColor(0x00, 0x00, 0x00, 0x1F)); p.drawRect(backgroundRect); } p.setPen(backgroundPen); p.setBrush(Qt::NoBrush); p.drawRect(backgroundRect); p.setPen(borderPen); p.setBrush(Qt::NoBrush); p.drawRect(borderRect); if(isDown()) { textPoint.setX(textPoint.x() + 1); textPoint.setY(textPoint.y() + 1); } p.setPen(palette().color(QPalette::Text)); p.drawText(textPoint, text()); }