| |   |
| 4 | 4 | |
| 5 | 5 | #include <QPainter> |
| 6 | 6 | #include <QApplication> |
| 7 | #include <QStyleOptionButton> |
| 7 | 8 | |
| 8 | 9 | namespace SIF { |
| 9 | 10 | |
| 10 | 11 | |
| 11 | 12 | struct AeroButton::Private { |
| 12 | 13 | bool hovered; |
| 13 | | bool pressed; |
| 14 | 14 | |
| 15 | 15 | QColor color; |
| 16 | 16 | QColor highlight; |
| … | … | |
| 20 | 20 | |
| 21 | 21 | int roundness; |
| 22 | 22 | |
| 23 | | Private(); |
| 23 | Private(AeroButton *q); |
| 24 | 24 | ~Private(); |
| 25 | 25 | |
| 26 | 26 | QRect calculateIconPosition(const QRect &button_rect, const QSize &icon_size); |
| 27 | 27 | }; |
| 28 | 28 | |
| 29 | | AeroButton::Private::Private() : hovered(false), |
| 30 | | pressed(false), |
| 29 | AeroButton::Private::Private(AeroButton *q) : hovered(false), |
| 31 | 30 | color(Qt::gray), |
| 32 | 31 | highlight(Qt::lightGray), |
| 33 | 32 | shadow(Qt::black), |
| 34 | 33 | opacity(1.0), |
| 35 | 34 | roundness(0) |
| 36 | 35 | { |
| 36 | QPalette pal = q->palette(); |
| 37 | pal.setBrush(QPalette::WindowText, Qt::white); |
| 38 | pal.setBrush(QPalette::Text, Qt::white); |
| 39 | pal.setBrush(QPalette::ButtonText, Qt::white); |
| 40 | q->setPalette(pal); |
| 41 | |
| 42 | q->setCheckable(1); |
| 37 | 43 | } |
| 38 | 44 | |
| 39 | 45 | QRect AeroButton::Private::calculateIconPosition(const QRect &button_rect, const QSize &icon_size) |
| … | … | |
| 49 | 49 | int width = icon_size.width(); |
| 50 | 50 | int height = icon_size.height(); |
| 51 | 51 | |
| 52 | | QRect icon_position; |
| 53 | | icon_position.setX(x); |
| 54 | | icon_position.setY(y); |
| 55 | | icon_position.setWidth(width); |
| 56 | | icon_position.setHeight(height); |
| 52 | QRect icon_position(x, y, width, height); |
| 57 | 53 | |
| 58 | 54 | return icon_position; |
| 59 | 55 | } |
| … | … | |
| 60 | 60 | |
| 61 | 61 | |
| 62 | 62 | AeroButton::AeroButton(QWidget * parent) |
| 63 | | : QPushButton(parent), d(new Private) |
| 63 | : QPushButton(parent), d(new Private(this)) |
| 64 | 64 | { |
| 65 | | QPalette pal = palette(); |
| 66 | | pal.setBrush(QPalette::WindowText, Qt::white); |
| 67 | | pal.setBrush(QPalette::Text, Qt::white); |
| 68 | | pal.setBrush(QPalette::ButtonText, Qt::white); |
| 69 | | setPalette(pal); |
| 70 | 65 | } |
| 71 | 66 | |
| 72 | 67 | AeroButton::AeroButton(const QString & text, QWidget * parent) |
| 73 | | : QPushButton(text, parent), d(new Private) |
| 68 | : QPushButton(text, parent), d(new Private(this)) |
| 74 | 69 | { |
| 75 | | QPalette pal = palette(); |
| 76 | | pal.setBrush(QPalette::WindowText, Qt::white); |
| 77 | | pal.setBrush(QPalette::Text, Qt::white); |
| 78 | | pal.setBrush(QPalette::ButtonText, Qt::white); |
| 79 | | setPalette(pal); |
| 80 | 70 | } |
| 81 | 71 | |
| 82 | 72 | AeroButton::AeroButton(const QIcon & icon, const QString & text, QWidget * parent) |
| 83 | | : QPushButton(icon, text, parent), d(new Private) |
| 73 | : QPushButton(icon, text, parent), d(new Private(this)) |
| 84 | 74 | { |
| 85 | | QPalette pal = palette(); |
| 86 | | pal.setBrush(QPalette::WindowText, Qt::white); |
| 87 | | pal.setBrush(QPalette::Text, Qt::white); |
| 88 | | pal.setBrush(QPalette::ButtonText, Qt::white); |
| 89 | | setPalette(pal); |
| 90 | 75 | } |
| 91 | 76 | |
| 92 | 77 | AeroButton::~AeroButton() |
| … | … | |
| 114 | 114 | void AeroButton::paintEvent(QPaintEvent * pe) |
| 115 | 115 | { |
| 116 | 116 | Q_UNUSED(pe); |
| 117 | |
| 118 | QRect rect = this->rect(); |
| 119 | QStyleOptionButton option; |
| 120 | initStyleOption(&option); |
| 117 | 121 | |
| 118 | 122 | QPainter painter(this); |
| 119 | 123 | painter.setRenderHint(QPainter::Antialiasing); |
| 120 | 124 | |
| 121 | 125 | painter.save(); |
| 122 | 126 | |
| 123 | | painter.fillRect(rect(), Qt::transparent); |
| 127 | painter.fillRect(rect, Qt::transparent); |
| 124 | 128 | |
| 125 | 129 | //test for state changes |
| 126 | 130 | QColor button_color; |
| … | … | |
| 132 | 132 | { |
| 133 | 133 | d->hovered ? button_color = d->highlight : button_color = d->color; |
| 134 | 134 | |
| 135 | | if(d->pressed) |
| 135 | if(option.state & QStyle::State_Sunken ) |
| 136 | 136 | { |
| 137 | 137 | button_color = d->highlight.darker(250); |
| 138 | 138 | } |
| … | … | |
| 179 | 179 | |
| 180 | 180 | painter.restore(); |
| 181 | 181 | |
| 182 | | //button text |
| 182 | QIcon icon = this->icon(); |
| 183 | 183 | QString text = this->text(); |
| 184 | QRect icon_position; |
| 185 | |
| 186 | //icon |
| 187 | if(!icon.isNull()) |
| 188 | { |
| 189 | QSize icon_size = this->iconSize(); |
| 190 | icon_position = d->calculateIconPosition(button_rect, icon_size); |
| 191 | |
| 192 | |
| 193 | if( !text.isEmpty() ) |
| 194 | { |
| 195 | icon_position.setX(20); |
| 196 | icon_position.setWidth(icon_size.width()); |
| 197 | } |
| 198 | |
| 199 | painter.setOpacity(1.0); |
| 200 | painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size))); |
| 201 | } |
| 202 | |
| 203 | //button text |
| 204 | |
| 184 | 205 | if(!text.isNull()) |
| 185 | 206 | { |
| 186 | 207 | QFont font = this->font(); |
| 187 | 208 | font.setBold(true); |
| 188 | 209 | painter.setFont(font); |
| 189 | 210 | |
| 190 | | // painter.setPen(Qt::white); |
| 191 | | |
| 192 | 211 | painter.setOpacity(1.0); |
| 193 | | painter.drawText(0, 0, button_rect.width(), button_rect.height(), Qt::AlignCenter, text); |
| 212 | painter.drawText(icon_position.width(), 0, button_rect.width() - icon_position.width(), button_rect.height(), Qt::AlignCenter, text); |
| 194 | 213 | } |
| 195 | | |
| 196 | | //icon |
| 197 | | QIcon icon = this->icon(); |
| 198 | | if(!icon.isNull()) |
| 214 | |
| 215 | if(option.state & QStyle::State_On) |
| 199 | 216 | { |
| 200 | | QSize icon_size = this->iconSize(); |
| 201 | | QRect icon_position = d->calculateIconPosition(button_rect, icon_size); |
| 202 | | painter.setOpacity(1.0); |
| 203 | | painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size))); |
| 217 | QPen old = painter.pen(); |
| 218 | |
| 219 | QColor h = d->highlight; |
| 220 | h.setAlpha(150); |
| 221 | QPen pen(h, 2); |
| 222 | painter.setPen(pen); |
| 223 | |
| 224 | painter.drawLine(rect.bottomLeft()-QPoint(-6, 3), rect.bottomRight()-QPoint(6, 3)); |
| 225 | |
| 226 | painter.setPen(old); |
| 204 | 227 | } |
| 205 | 228 | } |
| 206 | 229 | |
| … | … | |
| 247 | 247 | { |
| 248 | 248 | Q_UNUSED(e); |
| 249 | 249 | |
| 250 | | d->pressed = true; |
| 251 | 250 | this->update(); |
| 252 | 251 | |
| 253 | 252 | QPushButton::mousePressEvent(e); |
| … | … | |
| 256 | 256 | { |
| 257 | 257 | Q_UNUSED(e); |
| 258 | 258 | |
| 259 | | d->pressed = false; |
| 260 | 259 | this->update(); |
| 261 | 260 | |
| 262 | 261 | QPushButton::mouseReleaseEvent(e); |
| toggle raw diff |
--- a/src/sif/aerobutton.cpp
+++ b/src/sif/aerobutton.cpp
@@ -4,13 +4,13 @@
#include <QPainter>
#include <QApplication>
+#include <QStyleOptionButton>
namespace SIF {
struct AeroButton::Private {
bool hovered;
- bool pressed;
QColor color;
QColor highlight;
@@ -20,20 +20,26 @@ struct AeroButton::Private {
int roundness;
- Private();
+ Private(AeroButton *q);
~Private();
QRect calculateIconPosition(const QRect &button_rect, const QSize &icon_size);
};
-AeroButton::Private::Private() : hovered(false),
- pressed(false),
+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);
+
+ q->setCheckable(1);
}
QRect AeroButton::Private::calculateIconPosition(const QRect &button_rect, const QSize &icon_size)
@@ -43,11 +49,7 @@ QRect AeroButton::Private::calculateIconPosition(const QRect &button_rect, const
int width = icon_size.width();
int height = icon_size.height();
- QRect icon_position;
- icon_position.setX(x);
- icon_position.setY(y);
- icon_position.setWidth(width);
- icon_position.setHeight(height);
+ QRect icon_position(x, y, width, height);
return icon_position;
}
@@ -58,33 +60,18 @@ AeroButton::Private::~Private()
AeroButton::AeroButton(QWidget * parent)
- : QPushButton(parent), d(new Private)
+ : QPushButton(parent), d(new Private(this))
{
- QPalette pal = palette();
- pal.setBrush(QPalette::WindowText, Qt::white);
- pal.setBrush(QPalette::Text, Qt::white);
- pal.setBrush(QPalette::ButtonText, Qt::white);
- setPalette(pal);
}
AeroButton::AeroButton(const QString & text, QWidget * parent)
- : QPushButton(text, parent), d(new Private)
+ : QPushButton(text, parent), d(new Private(this))
{
- QPalette pal = palette();
- pal.setBrush(QPalette::WindowText, Qt::white);
- pal.setBrush(QPalette::Text, Qt::white);
- pal.setBrush(QPalette::ButtonText, Qt::white);
- setPalette(pal);
}
AeroButton::AeroButton(const QIcon & icon, const QString & text, QWidget * parent)
- : QPushButton(icon, text, parent), d(new Private)
+ : QPushButton(icon, text, parent), d(new Private(this))
{
- QPalette pal = palette();
- pal.setBrush(QPalette::WindowText, Qt::white);
- pal.setBrush(QPalette::Text, Qt::white);
- pal.setBrush(QPalette::ButtonText, Qt::white);
- setPalette(pal);
}
AeroButton::~AeroButton()
@@ -127,13 +114,17 @@ void AeroButton::setRoundness(int roundness)
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);
+ painter.fillRect(rect, Qt::transparent);
//test for state changes
QColor button_color;
@@ -141,7 +132,7 @@ void AeroButton::paintEvent(QPaintEvent * pe)
{
d->hovered ? button_color = d->highlight : button_color = d->color;
- if(d->pressed)
+ if(option.state & QStyle::State_Sunken )
{
button_color = d->highlight.darker(250);
}
@@ -188,28 +179,51 @@ void AeroButton::paintEvent(QPaintEvent * pe)
painter.restore();
- //button text
+ 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.setPen(Qt::white);
-
painter.setOpacity(1.0);
- painter.drawText(0, 0, button_rect.width(), button_rect.height(), Qt::AlignCenter, text);
+ painter.drawText(icon_position.width(), 0, button_rect.width() - icon_position.width(), button_rect.height(), Qt::AlignCenter, text);
}
-
- //icon
- QIcon icon = this->icon();
- if(!icon.isNull())
+
+ if(option.state & QStyle::State_On)
{
- QSize icon_size = this->iconSize();
- QRect icon_position = d->calculateIconPosition(button_rect, icon_size);
- painter.setOpacity(1.0);
- painter.drawPixmap(icon_position, QPixmap(icon.pixmap(icon_size)));
+ 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);
}
}
@@ -233,7 +247,6 @@ void AeroButton::mousePressEvent(QMouseEvent * e)
{
Q_UNUSED(e);
- d->pressed = true;
this->update();
QPushButton::mousePressEvent(e);
@@ -243,7 +256,6 @@ void AeroButton::mouseReleaseEvent(QMouseEvent * e)
{
Q_UNUSED(e);
- d->pressed = false;
this->update();
QPushButton::mouseReleaseEvent(e);
|