xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/allwinner/browser/chasewidget.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33 
34 #include "chasewidget.h"
35 
36 #include <QtCore/QPoint>
37 
38 #include <QtWidgets/QApplication>
39 #include <QtGui/QHideEvent>
40 #include <QtGui/QPainter>
41 #include <QtGui/QPaintEvent>
42 #include <QtGui/QShowEvent>
43 
ChaseWidget(QWidget * parent,QPixmap pixmap,bool pixmapEnabled)44 ChaseWidget::ChaseWidget(QWidget *parent, QPixmap pixmap, bool pixmapEnabled)
45     : QWidget(parent)
46     , m_segment(0)
47     , m_delay(100)
48     , m_step(40)
49     , m_timerId(-1)
50     , m_animated(false)
51     , m_pixmap(pixmap)
52     , m_pixmapEnabled(pixmapEnabled)
53 {
54 }
55 
setAnimated(bool value)56 void ChaseWidget::setAnimated(bool value)
57 {
58     if (m_animated == value)
59         return;
60     m_animated = value;
61     if (m_timerId != -1) {
62         killTimer(m_timerId);
63         m_timerId = -1;
64     }
65     if (m_animated) {
66         m_segment = 0;
67         m_timerId = startTimer(m_delay);
68     }
69     update();
70 }
71 
paintEvent(QPaintEvent * event)72 void ChaseWidget::paintEvent(QPaintEvent *event)
73 {
74     Q_UNUSED(event);
75     QPainter p(this);
76     if (m_pixmapEnabled && !m_pixmap.isNull()) {
77         p.drawPixmap(0, 0, m_pixmap);
78         return;
79     }
80 
81     const int extent = qMin(width() - 8, height() - 8);
82     const int displ = extent / 4;
83     const int ext = extent / 4 - 1;
84 
85     p.setRenderHint(QPainter::Antialiasing, true);
86 
87     if(m_animated)
88         p.setPen(Qt::gray);
89     else
90         p.setPen(QPen(palette().dark().color()));
91 
92     p.translate(width() / 2, height() / 2); // center
93 
94     for (int segment = 0; segment < segmentCount(); ++segment) {
95         p.rotate(QApplication::isRightToLeft() ? m_step : -m_step);
96         if(m_animated)
97             p.setBrush(colorForSegment(segment));
98         else
99             p.setBrush(palette().background());
100         p.drawEllipse(QRect(displ, -ext / 2, ext, ext));
101     }
102 }
103 
sizeHint() const104 QSize ChaseWidget::sizeHint() const
105 {
106     return QSize(32, 32);
107 }
108 
timerEvent(QTimerEvent * event)109 void ChaseWidget::timerEvent(QTimerEvent *event)
110 {
111     if (event->timerId() == m_timerId) {
112         ++m_segment;
113         update();
114     }
115     QWidget::timerEvent(event);
116 }
117 
colorForSegment(int seg) const118 QColor ChaseWidget::colorForSegment(int seg) const
119 {
120     int index = ((seg + m_segment) % segmentCount());
121     int comp = qMax(0, 255 - (index * (255 / segmentCount())));
122     return QColor(comp, comp, comp, 255);
123 }
124 
segmentCount() const125 int ChaseWidget::segmentCount() const
126 {
127     return 360 / m_step;
128 }
129 
setPixmapEnabled(bool enable)130 void ChaseWidget::setPixmapEnabled(bool enable)
131 {
132     m_pixmapEnabled = enable;
133 }
134 
135