1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2007-27-08
7  * Description : a tool bar action object to display animated logo
8  *
9  * Copyright (C) 2007-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "dlogoaction.h"
25 
26 // C++ includes
27 
28 #include <cmath>
29 
30 // Qt includes
31 
32 #include <QPalette>
33 #include <QPixmap>
34 #include <QBoxLayout>
35 #include <QTimer>
36 #include <QPainter>
37 #include <QApplication>
38 #include <QStandardPaths>
39 
40 // KDE includes
41 
42 #include <klocalizedstring.h>
43 
44 // Local includes
45 
46 #include "daboutdata.h"
47 #include "dactivelabel.h"
48 
49 namespace Digikam
50 {
51 
52 class Q_DECL_HIDDEN DLogoAction::Private
53 {
54 public:
55 
Private()56     explicit Private()
57       : alignOnright(true),
58         progressCount(0),
59         progressTimer(nullptr),
60         urlLabel(nullptr)
61     {
62     }
63 
64     bool          alignOnright;
65 
66     int           progressCount;         ///< Position of animation.
67 
68     QTimer*       progressTimer;
69 
70     QPixmap       progressPixmap;
71 
72     DActiveLabel* urlLabel;
73 };
74 
DLogoAction(QObject * const parent,bool alignOnright)75 DLogoAction::DLogoAction(QObject* const parent, bool alignOnright)
76     : QWidgetAction(parent),
77       d(new Private)
78 {
79     setText(QLatin1String("digikam.org"));
80 
81     if (QApplication::applicationName() == QLatin1String("digikam"))
82     {
83         setIcon(QIcon::fromTheme(QLatin1String("digikam")));
84         d->progressPixmap = QPixmap(QStandardPaths::locate(QStandardPaths::GenericDataLocation,
85                                     QLatin1String("digikam/data/banner-digikam.png")));
86     }
87     else
88     {
89         setIcon(QIcon::fromTheme(QLatin1String("showfoto")));
90         d->progressPixmap = QPixmap(QStandardPaths::locate(QStandardPaths::GenericDataLocation,
91                                     QLatin1String("showfoto/data/banner-showfoto.png")));
92     }
93 
94     d->alignOnright  = alignOnright;
95     d->progressTimer = new QTimer(this);
96     d->progressTimer->setSingleShot(true);
97 
98     connect(d->progressTimer, SIGNAL(timeout()),
99             this, SLOT(slotProgressTimerDone()));
100 }
101 
~DLogoAction()102 DLogoAction::~DLogoAction()
103 {
104     delete d;
105 }
106 
start()107 void DLogoAction::start()
108 {
109     d->progressCount = 0;
110     d->progressTimer->start(100);
111 }
112 
stop()113 void DLogoAction::stop()
114 {
115     d->progressCount = 0;
116     d->progressTimer->stop();
117 
118     if (d->urlLabel)
119     {
120         d->urlLabel->updateData(DAboutData::webProjectUrl(),
121                                 d->progressPixmap.copy(0, 0, 144, 32).toImage());
122     }
123 }
124 
running() const125 bool DLogoAction::running() const
126 {
127     return d->progressTimer->isActive();
128 }
129 
slotProgressTimerDone()130 void DLogoAction::slotProgressTimerDone()
131 {
132     QPixmap anim(d->progressPixmap.copy(0, d->progressCount*32, 144, 32));
133     d->progressCount++;
134 
135     if (d->progressCount == 36)
136     {
137         d->progressCount = 0;
138     }
139 
140     if (d->urlLabel)
141     {
142         d->urlLabel->updateData(DAboutData::webProjectUrl(), anim.toImage());
143     }
144 
145     d->progressTimer->start(100);
146 }
147 
createWidget(QWidget * parent)148 QWidget* DLogoAction::createWidget(QWidget* parent)
149 {
150     QWidget* const container  = new QWidget(parent);
151     QHBoxLayout* const layout = new QHBoxLayout(container);
152     d->urlLabel               = new DActiveLabel(DAboutData::webProjectUrl(), QString(), container);
153     d->urlLabel->setToolTip(i18n("Visit digiKam project website"));
154     d->urlLabel->updateData(DAboutData::webProjectUrl(), d->progressPixmap.copy(0, 0, 144, 32).toImage());
155 
156     layout->setContentsMargins(QMargins());
157     layout->setSpacing(0);
158 
159     if (d->alignOnright)
160     {
161         layout->addStretch();
162     }
163 
164     layout->addWidget(d->urlLabel);
165 
166     return container;
167 }
168 
deleteWidget(QWidget * widget)169 void DLogoAction::deleteWidget(QWidget* widget)
170 {
171     stop();
172     d->urlLabel = nullptr;
173     QWidgetAction::deleteWidget(widget);
174 }
175 
176 } // namespace Digikam
177