From dc8e858b64e31fc96900ce81247b5407dd63e61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Wed, 19 Jun 2013 22:47:18 +0200 Subject: [PATCH] miss --- custom_widgets/yacreader_busy_widget.cpp | 186 +++++++++++++++++++++++ custom_widgets/yacreader_busy_widget.h | 50 ++++++ 2 files changed, 236 insertions(+) create mode 100644 custom_widgets/yacreader_busy_widget.cpp create mode 100644 custom_widgets/yacreader_busy_widget.h diff --git a/custom_widgets/yacreader_busy_widget.cpp b/custom_widgets/yacreader_busy_widget.cpp new file mode 100644 index 00000000..0db5e8c2 --- /dev/null +++ b/custom_widgets/yacreader_busy_widget.cpp @@ -0,0 +1,186 @@ +#include "yacreader_busy_widget.h" + +#include +#include +#include +#include + +YACReaderBusyWidget::YACReaderBusyWidget(QWidget *parent) + :QWidget(parent) +{ + setFixedSize(70,70); + BusyIndicator * busy = new BusyIndicator(this); + busy->setIndicatorStyle(BusyIndicator::StyleArc); + busy->setColor(Qt::white); + busy->move(20,20); +} + +void YACReaderBusyWidget::paintEvent(QPaintEvent * event) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.drawPixmap(0,0,width(),height(),QPixmap(":/images/busy_background.png")); +} + +BusyIndicator::BusyIndicator(QWidget *parent) : + QWidget(parent), + startAngle(0), + m_style(StyleArc) +{ + QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Fixed); + policy.setHeightForWidth(true); + setSizePolicy(policy); + + fillColor = palette().color(QPalette::WindowText); + + timer.setInterval(16); + connect(&timer, SIGNAL(timeout()), this, SLOT(rotate())); + timer.start(); +} + +void BusyIndicator::rotate() +{ + startAngle += 9; + startAngle %= 360; + update(); +} + +void BusyIndicator::setIndicatorStyle(IndicatorStyle style) +{ + m_style = style; + update(); +} + +void BusyIndicator::setColor(QColor color) +{ + fillColor = color; +} + +const BusyIndicator::IndicatorStyle BusyIndicator::indicatorStyle() const +{ + return m_style; +} + + +QPixmap BusyIndicator::generatePixmap(int side) +{ + QPixmap pixmap(QSize(side, side)); + pixmap.fill(QColor(255, 255, 255, 0)); + + QPainter painter(&pixmap); + painter.setRenderHint(QPainter::Antialiasing); + + painter.translate(side / 2, side / 2); + painter.scale(side / 200.0, side / 200.0); + + switch (m_style) { + case StyleRect: + drawRectStyle(&painter); + break; + case StyleEllipse: + drawEllipseStyle(&painter); + break; + case StyleArc: + drawArcStyle(&painter); + break; + } + return pixmap; +} + +void BusyIndicator::drawRectStyle(QPainter *painter) +{ + // QColor color = palette().color(QPalette::WindowText); + QColor color = fillColor; + QBrush brush(color); + painter->setPen(Qt::NoPen); + + painter->rotate(startAngle); + + float angle = 0; + while (angle < 360) { + painter->setBrush(brush); + painter->drawRect(-8, -100, 16, 35); + + painter->rotate(30); + angle += 30; + + color.setAlphaF(angle / 360); + brush.setColor(color); + } +} + +void BusyIndicator::drawEllipseStyle(QPainter *painter) +{ + // QColor color = palette().color(QPalette::WindowText); + QColor color = fillColor; + QBrush brush(color); + painter->setPen(Qt::NoPen); + + painter->rotate(startAngle); + + float angle = 0; + while (angle < 360) { + painter->setBrush(brush); + painter->drawEllipse(-10, -100, 30, 30); + + painter->rotate(30); + angle += 30; + + color.setAlphaF(angle / 360); + brush.setColor(color); + } +} + +void BusyIndicator::drawArcStyle(QPainter *painter) +{ + // QColor color = palette().color(QPalette::WindowText); + QColor color = fillColor; + QConicalGradient gradient(0, 0, -startAngle); + gradient.setColorAt(0, color); + color.setAlpha(0); + gradient.setColorAt(0.8, color); + color.setAlpha(255); + gradient.setColorAt(1, color); + + QPen pen; + pen.setWidth(30); + pen.setBrush(QBrush(gradient)); + painter->setPen(pen); + + painter->drawArc(-85, -85, 170, 170, 0 * 16, 360 * 16); +} + +void BusyIndicator::paintEvent(QPaintEvent *) +{ + QString key = QString("%1:%2:%3:%4:%5") + .arg(metaObject()->className()) + .arg(width()) + .arg(height()) + .arg(startAngle) + .arg(m_style); + + QPixmap pixmap; + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + int side = qMin(width(), height()); + + if(!QPixmapCache::find(key, &pixmap)) { + pixmap = generatePixmap(side); + QPixmapCache::insert(key, pixmap); + } + + painter.translate(width() / 2 - side / 2, height() / 2 - side / 2); + + painter.drawPixmap(0, 0, side, side, pixmap); +} + +QSize BusyIndicator::minimumSizeHint() const +{ + return QSize(30, 30); +} + +QSize BusyIndicator::sizeHint() const +{ + return QSize(30, 30); +} diff --git a/custom_widgets/yacreader_busy_widget.h b/custom_widgets/yacreader_busy_widget.h new file mode 100644 index 00000000..e88f7196 --- /dev/null +++ b/custom_widgets/yacreader_busy_widget.h @@ -0,0 +1,50 @@ +#ifndef YACREADER_BUSYINDICATOR_H +#define YACREADER_BUSYINDICATOR_H + +#include +#include + +class YACReaderBusyWidget : public QWidget +{ + Q_OBJECT +public: + explicit YACReaderBusyWidget(QWidget *parent = 0); + void paintEvent(QPaintEvent *); +}; + +class BusyIndicator : public QWidget +{ + Q_OBJECT +public: + enum IndicatorStyle{StyleRect, StyleEllipse, StyleArc}; + + explicit BusyIndicator(QWidget *parent = 0); + + void paintEvent(QPaintEvent *); + QSize minimumSizeHint() const; + QSize sizeHint() const; + + void setIndicatorStyle(IndicatorStyle); + void setColor(QColor color); + const IndicatorStyle indicatorStyle() const; + +signals: + +private slots: + void rotate(); + +private: + QPixmap generatePixmap(int sideLength); + void drawRectStyle(QPainter *painter); + void drawEllipseStyle(QPainter *painter); + void drawArcStyle(QPainter *painter); + + QTimer timer; + int startAngle; + + IndicatorStyle m_style; + + QColor fillColor; +}; + +#endif // BUSYINDICATOR_H