widgethelpers.h 7.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/***************************************************************************
 * Copyright (C) 2019 by Savoir-faire Linux                                *
 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>          *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/
#pragma once

#include "animationhelpers.h"
21
#include "popupdialog.h"
22
#include "utils.h"
23
24

#include <QWidget>
25
#include <QLabel>
26
27
28
#include <QTimer>
#include <QMouseEvent>
#include <QPainter>
29
#include <QPixmap>
30

31
32
33
34
35
36
37
38
39
#include <QtQML>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQmlComponent>

#include <QtQuick/QtQuick>
#include <QQuickItem>
#include <QQuickWidget>

40
41
42
43
44
45
46
class FadeOutable : public QWidget
{
    Q_OBJECT
public:
    explicit FadeOutable(QWidget* parent = nullptr);
    ~FadeOutable();

47
    void resetToFadeIn(bool resetTimer);
48
49

    void setFadeDelay(const quint64 ms) { fadeDelay_ = ms; }
50
51
    void setFadeOutStartLocation(const QRect& rect) { Utils::whileBlocking(fadeAnimation_)->setStartValue(rect); }
    void setFadeOutEndLocation(const QRect& rect) { Utils::whileBlocking(fadeAnimation_)->setEndValue(rect); }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

    virtual bool shouldFadeOut() { return false; };

signals:
    void willFadeOut();
    void willReset();

protected:
    void enterEvent(QEvent* event) override;
    void leaveEvent(QEvent* event) override;
    void mouseMoveEvent(QMouseEvent* event) override;

private slots:
    void slotTimeout();

private:
68
    SizeAnimation* fadeAnimation_;
69
70
71
72
73
74
    QTimer fadeTimer_;

    quint64 fadeDelay_{ 2000 };

};

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class Blinkable : public QWidget
{
    Q_OBJECT
public:
    explicit Blinkable(QWidget* parent = nullptr);
    ~Blinkable();

    void start();
    void stop();

    void setBlinkTime(const quint64 ms) { blinkTime_ = ms; }
    void setPixmap(QPixmap* pixmap) { pixmap_ = pixmap; }
    void unsetPixmap() { pixmap_ = nullptr; }

protected:
    void paintEvent(QPaintEvent *event);

private slots:
    void slotAnimationFinished();

private:
    FadeAnimation* fadeAnimation_;

    quint64 blinkTime_{ 1000 };
    QPixmap* pixmap_{ nullptr };
    QMetaObject::Connection connection_;
};

103
104
105
106
107
108
109
class VignetteWidget : public QWidget
{
    Q_OBJECT
public:
    explicit VignetteWidget(QWidget* parent = nullptr);
    ~VignetteWidget();

110
111
    void setDrawRoundedCorner(bool state) { drawRoundedCorner_ = state; }

112
113
114
public slots:
    void slotWillFadeOut();
    void slotWillResetOpacity();
115
    void updateGeometry(QSize size);
116
117
118
119
120

protected:
    void paintEvent(QPaintEvent *event) override;

private:
121
122
    void fillRoundRectPath(QPainter& painter, const QLinearGradient& gradient);

123
    SizeAnimation* geometryAnimation_;
124
    constexpr static qreal cornerRadius_ = 10.0f;
125
126
    quint64 height_{ 128 };
    quint64 fadeTime_{ 1000 };
127
    bool drawRoundedCorner_{ false };
128

129
130
131
132
133
134
135
136
137
};

class PopupWidget : public QWidget
{
    Q_OBJECT
public:
    // insider content of PopupDialog
    explicit PopupWidget(QWidget* parent = nullptr,
                         QColor spikeColor = Qt::white,
138
139
                         PopupDialog::SpikeLabelAlignment spikeAlignment = PopupDialog::SpikeLabelAlignment::AlignLeft)
            : QWidget(parent) {
140
141
142
143
144
145
146
147
148
149
        widgetContainer_ = new PopupDialog(parent, spikeColor, spikeAlignment);
        setParent(widgetContainer_);
        widgetContainer_->insertWidget(this);
    }
    virtual ~PopupWidget() {}

    PopupDialog *getContainer() { return widgetContainer_; }

protected:
    PopupDialog *widgetContainer_;
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

};

class QmlPopupWidget : public QQuickWidget
{
    Q_OBJECT
public:
    // insider content of PopupDialog
    explicit QmlPopupWidget(const QUrl& source,
                            QWidget* parent = nullptr,
                            QColor spikeColor = Qt::white,
                            PopupDialog::SpikeLabelAlignment spikeAlignment = PopupDialog::SpikeLabelAlignment::None)
        : QQuickWidget(source, parent) {

        widgetContainer_ = new PopupDialog(parent, spikeColor, spikeAlignment);
        setMouseTracking(true);
        setParent(widgetContainer_);
        widgetContainer_->insertWidget(this);
    }
    virtual ~QmlPopupWidget() {}

    PopupDialog* getContainer() { return widgetContainer_; }

protected:
    void showEvent(QShowEvent* event) override;
    void hideEvent(QHideEvent* event) override;

    PopupDialog* widgetContainer_;
178
179

};
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

class Darkenable : public QWidget
{
    Q_OBJECT
public:
    explicit Darkenable(QWidget* parent = nullptr)
        : QWidget(parent)
    {
        setAttribute(Qt::WA_NoSystemBackground);
        animation_ = new FadeAnimation(this, 400, 0, .33);
        this->hide();
    };
    ~Darkenable() {};

    void darken() {
        disconnect(connection_);
        this->show();
        animation_->setDirection(QAbstractAnimation::Backward);
        animation_->start();
    }
    void lighten() {
        animation_->setDirection(QAbstractAnimation::Forward);
        disconnect(connection_);
        connection_ = connect(animation_, &QPropertyAnimation::finished,
            [this] {
                this->hide();
            });
        animation_->start();
    }

    void setAnimationTime(const quint64 ms) { animation_->setDuration(ms); }

protected:
    void paintEvent(QPaintEvent *event) override {
        Q_UNUSED(event);
        QPainter painter(this);
        painter.setBrush(Qt::black);
        auto rect = qobject_cast<QWidget*>(parent())->rect();
        setGeometry(rect);
        painter.drawRect(rect);
    };

private:
    FadeAnimation* animation_;
    QMetaObject::Connection connection_;

226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
};

class FullScreenNotification : public QLabel
{
    Q_OBJECT
public:
    FullScreenNotification(const QString& text, QWidget *parent = nullptr)
        : QLabel(parent)
    {
        setText(text);
        setAttribute(Qt::WA_TransparentForMouseEvents);
        fadeAnimation_ = new FadeAnimation(this, 1000, 0, 0.6f);
        fadeTimer_.setSingleShot(true);
        connect(&fadeTimer_, SIGNAL(timeout()), this, SLOT(slotTimeout()));
        connect(fadeAnimation_, &QAbstractAnimation::finished,
            [this]() { this->hide(); });
    }

protected:
    void showEvent(QShowEvent* event) override {
        fadeTimer_.start(2000);
        QLabel::showEvent(event);
    };

private slots:
    void slotTimeout() { fadeAnimation_->start(); };

private:
    FadeAnimation* fadeAnimation_;
    QTimer fadeTimer_;
256
};