Move cancel button back into progressbar

I made it hide the cancel button this time and
added a nice animation when hovered.
This commit is contained in:
Jens Bache-Wiig
2010-03-17 16:27:33 +01:00
parent 9663067357
commit 5d993bbccf
2 changed files with 72 additions and 22 deletions

View File

@@ -31,6 +31,7 @@
#include <utils/stylehelper.h> #include <utils/stylehelper.h>
#include <QtCore/QPropertyAnimation>
#include <QtGui/QPainter> #include <QtGui/QPainter>
#include <QtGui/QFont> #include <QtGui/QFont>
#include <QtGui/QBrush> #include <QtGui/QBrush>
@@ -38,10 +39,10 @@
#include <QtDebug> #include <QtDebug>
#define PROGRESSBAR_HEIGHT 13 #define PROGRESSBAR_HEIGHT 13
#define CANCEL_WIDTH 12 #define CANCELBUTTON_SIZE 15
ProgressBar::ProgressBar(QWidget *parent) ProgressBar::ProgressBar(QWidget *parent)
: QWidget(parent), m_error(false), m_minimum(1), m_maximum(100), m_value(1) : QWidget(parent), m_error(false), m_minimum(1), m_maximum(100), m_value(1), m_fader(0)
{ {
setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
setMouseTracking(true); setMouseTracking(true);
@@ -51,6 +52,32 @@ ProgressBar::~ProgressBar()
{ {
} }
bool ProgressBar::event(QEvent *e)
{
switch(e->type()) {
case QEvent::Enter:
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
animation->setDuration(125);
animation->setEndValue(1.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
break;
case QEvent::Leave:
{
QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
animation->setDuration(225);
animation->setEndValue(0.0);
animation->start(QAbstractAnimation::DeleteWhenStopped);
}
break;
default:
return QWidget::event(e);
}
return false;
}
void ProgressBar::reset() void ProgressBar::reset()
{ {
m_value = m_minimum; m_value = m_minimum;
@@ -111,8 +138,16 @@ namespace { const int INDENT = 6; }
void ProgressBar::mousePressEvent(QMouseEvent *event) void ProgressBar::mousePressEvent(QMouseEvent *event)
{ {
QFont boldFont(font());
boldFont.setPointSizeF(Utils::StyleHelper::sidebarFontSize());
boldFont.setBold(true);
QFontMetrics fm(boldFont);
int h = fm.height();
QRect rect(INDENT - 1, h+8, size().width()-2*INDENT + 1, m_progressHeight-1);
QRect cancelRect(rect.adjusted(rect.width() - CANCELBUTTON_SIZE, 1, -1, 0));
if (event->modifiers() == Qt::NoModifier if (event->modifiers() == Qt::NoModifier
&& event->x() >= size().width()-INDENT-m_progressHeight) { && cancelRect.contains(event->pos())) {
event->accept(); event->accept();
emit clicked(); emit clicked();
return; return;
@@ -159,17 +194,12 @@ void ProgressBar::paintEvent(QPaintEvent *)
QRect textBounds = fm.boundingRect(m_title); QRect textBounds = fm.boundingRect(m_title);
textBounds.moveCenter(rect().center()); textBounds.moveCenter(rect().center());
int buttonWidth = value() < maximum() ? CANCEL_WIDTH : 0;
int alignment = Qt::AlignHCenter; int alignment = Qt::AlignHCenter;
int textSpace = rect().width() - buttonWidth - 8; int textSpace = rect().width() - 8;
// If there is not enough room when centered, we left align and // If there is not enough room when centered, we left align and
// elide the text // elide the text
QString elidedtitle = m_title; QString elidedtitle = fm.elidedText(m_title, Qt::ElideRight, textSpace);
if (!m_error && textBounds.right() > textSpace) {
alignment = Qt::AlignLeft;
elidedtitle = fm.elidedText(m_title, Qt::ElideRight, textSpace);
}
QRect textRect = rect().adjusted(INDENT + 1, 1, -INDENT - 1, 0); QRect textRect = rect().adjusted(INDENT + 1, 1, -INDENT - 1, 0);
textRect.setHeight(h+5); textRect.setHeight(h+5);
@@ -193,6 +223,7 @@ void ProgressBar::paintEvent(QPaintEvent *)
c.setAlpha(180); c.setAlpha(180);
p.setPen(Qt::NoPen); p.setPen(Qt::NoPen);
QRect inner = rect.adjusted(3, 2, -2, -2); QRect inner = rect.adjusted(3, 2, -2, -2);
inner.adjust(0, 0, qRound((percent - 1) * inner.width()), 0); inner.adjust(0, 0, qRound((percent - 1) * inner.width()), 0);
if (m_error) { if (m_error) {
@@ -228,20 +259,32 @@ void ProgressBar::paintEvent(QPaintEvent *)
p.drawPoint(inner.bottomRight()); p.drawPoint(inner.bottomRight());
// Draw cancel button // Draw cancel button
p.setOpacity(m_fader);
if (value() < maximum() && !m_error) { if (value() < maximum() && !m_error) {
QRect parentRect = parentWidget()->rect(); // ### Move to parent QRect cancelRect(rect.adjusted(rect.width() - CANCELBUTTON_SIZE, 1, -1, 0));
QRect cancelRect(parentRect.right() - buttonWidth - 2,
parentRect.top() + 4, buttonWidth, CANCEL_WIDTH);
bool hover = cancelRect.contains(mapFromGlobal(QCursor::pos())); bool hover = cancelRect.contains(mapFromGlobal(QCursor::pos()));
p.setPen(QPen(QColor(0, 0, 0, 20), 4)); QLinearGradient grad(cancelRect.topLeft(), cancelRect.bottomLeft());
p.drawLine(cancelRect.center()+QPoint(-2,-2), cancelRect.center()+QPoint(+4,+4)); int intensity = hover ? 90 : 70;
p.drawLine(cancelRect.center()+QPoint(+4,-2), cancelRect.center()+QPoint(-2,+4)); QColor buttonColor(intensity, intensity, intensity, 255);
grad.setColorAt(0, buttonColor.lighter(130));
grad.setColorAt(1, buttonColor.darker(130));
p.setPen(Qt::NoPen);
p.setBrush(grad);
p.drawRect(cancelRect.adjusted(1, 1, -1, -1));
p.setPen(QPen(hover ? Utils::StyleHelper::panelTextColor() : QColor(200, 200, 200), 1)); p.setPen(QPen(QColor(0, 0, 0, 30)));
p.drawLine(cancelRect.topLeft() + QPoint(0,1), cancelRect.bottomLeft() + QPoint(0,-1));
p.setPen(QPen(QColor(0, 0, 0, 120)));
p.drawLine(cancelRect.topLeft() + QPoint(1,1), cancelRect.bottomLeft() + QPoint(1,-1));
p.setPen(QPen(QColor(255, 255, 255, 30)));
p.drawLine(cancelRect.topLeft() + QPoint(2,1), cancelRect.bottomLeft() + QPoint(2,-1));
p.setPen(QPen(hover ? Utils::StyleHelper::panelTextColor() : QColor(180, 180, 180), 1));
p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::Antialiasing);
p.translate(0.5, 0.5); p.translate(0.5, 0.5);
p.drawLine(cancelRect.center()+QPoint(-2,-2), cancelRect.center()+QPoint(+4,+4)); p.drawLine(cancelRect.center()+QPoint(-1,-2), cancelRect.center()+QPoint(+3,+2));
p.drawLine(cancelRect.center()+QPoint(+4,-2), cancelRect.center()+QPoint(-2,+4)); p.drawLine(cancelRect.center()+QPoint(+3,-2), cancelRect.center()+QPoint(-1,+2));
p.setPen(QPen(QColor(0, 0, 0, 80)));
p.drawLine(cancelRect.bottomLeft() + QPoint(2,-1), cancelRect.bottomRight() + QPoint(0,-1));
} }
} }

View File

@@ -38,6 +38,9 @@
class ProgressBar : public QWidget class ProgressBar : public QWidget
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(float fader READ fader WRITE setFader)
public: public:
ProgressBar(QWidget *parent = 0); ProgressBar(QWidget *parent = 0);
~ProgressBar(); ~ProgressBar();
@@ -50,13 +53,16 @@ public:
QSize sizeHint() const; QSize sizeHint() const;
void paintEvent(QPaintEvent *); void paintEvent(QPaintEvent *);
void mouseMoveEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *);
int minimum() const { return m_minimum; } int minimum() const { return m_minimum; }
int maximum() const { return m_maximum; } int maximum() const { return m_maximum; }
int value() const { return m_value; } int value() const { return m_value; }
void reset(); void reset();
void setRange(int minimum, int maximum); void setRange(int minimum, int maximum);
void setValue(int value); void setValue(int value);
float fader() { return m_fader; }
void setFader(float value) { m_fader = value; update(); }
bool event(QEvent *);
signals: signals:
void clicked(); void clicked();
@@ -69,10 +75,11 @@ private:
QString m_title; QString m_title;
bool m_error; bool m_error;
int m_progressHeight; int m_progressHeight;
int m_minimum; int m_minimum;
int m_maximum; int m_maximum;
int m_value; int m_value;
float m_fader;
}; };
#endif // PROGRESSPIE_H #endif // PROGRESSPIE_H