File indexing completed on 2024-05-12 04:19:41

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2014 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018 */
0019 // Self
0020 #include "hudcountdown.h"
0021 
0022 // Local
0023 #include <lib/hud/hudtheme.h>
0024 
0025 // KF
0026 
0027 // Qt
0028 #include <QPainter>
0029 #include <QTimeLine>
0030 
0031 namespace Gwenview
0032 {
0033 struct HudCountDownPrivate {
0034     QTimeLine *mTimeLine = nullptr;
0035 };
0036 
0037 HudCountDown::HudCountDown(QGraphicsWidget *parent)
0038     : QGraphicsWidget(parent)
0039     , d(new HudCountDownPrivate)
0040 {
0041     d->mTimeLine = new QTimeLine(0, this);
0042     d->mTimeLine->setDirection(QTimeLine::Backward);
0043     connect(d->mTimeLine, &QTimeLine::valueChanged, this, &HudCountDown::doUpdate);
0044     connect(d->mTimeLine, &QTimeLine::finished, this, &HudCountDown::timeout);
0045 
0046     // Use an odd value so that the vertical line is aligned to pixel
0047     // boundaries
0048     setMinimumSize(17, 17);
0049 }
0050 
0051 HudCountDown::~HudCountDown()
0052 {
0053     delete d;
0054 }
0055 
0056 void HudCountDown::start(qreal ms)
0057 {
0058     d->mTimeLine->setDuration(ms);
0059     d->mTimeLine->start();
0060     update();
0061 }
0062 
0063 void HudCountDown::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
0064 {
0065     HudTheme::RenderInfo info = HudTheme::renderInfo(HudTheme::CountDown);
0066     painter->setRenderHint(QPainter::Antialiasing);
0067     const int circle = 5760;
0068     const int start = circle / 4; // Start at 12h, not 3h
0069     const int end = int(circle * d->mTimeLine->currentValue());
0070     painter->setBrush(info.bgBrush);
0071     painter->setPen(info.borderPen);
0072 
0073     QRectF square = boundingRect().adjusted(.5, .5, -.5, -.5);
0074     const qreal width = square.width();
0075     const qreal height = square.height();
0076     if (width < height) {
0077         square.setHeight(width);
0078         square.moveTop((height - width) / 2);
0079     } else {
0080         square.setWidth(height);
0081         square.moveLeft((width - height) / 2);
0082     }
0083     painter->drawPie(square, start, end);
0084 }
0085 
0086 void HudCountDown::doUpdate()
0087 {
0088     update();
0089 }
0090 
0091 } // namespace
0092 
0093 #include "moc_hudcountdown.cpp"