widgethelpers.cpp 5.96 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/>.   *
 **************************************************************************/

#include "widgethelpers.h"

21
22
#include "mainwindow.h"

23
24
25
26
FadeOutable::FadeOutable(QWidget* parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_NoSystemBackground);
27
    fadeAnimation_ = new SizeAnimation(this, rect(), rect());
28
29
30
31
32
33
34
35
    fadeTimer_.setSingleShot(true);
    connect(&fadeTimer_, SIGNAL(timeout()), this, SLOT(slotTimeout()));
}

FadeOutable::~FadeOutable()
{}

void
36
FadeOutable::resetToFadeIn(bool resetTimer)
37
{
38
39
    fadeAnimation_->stop();
    fadeAnimation_->targetObject()->setProperty(fadeAnimation_->propertyName(), fadeAnimation_->startValue());
40
    Q_EMIT willReset();
41
42
    if(resetTimer)
        fadeTimer_.start(fadeDelay_);
43
44
45
46
47
48
}

void
FadeOutable::enterEvent(QEvent* event)
{
    Q_UNUSED(event);
49
    resetToFadeIn(false);
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    event->ignore();
}

void
FadeOutable::leaveEvent(QEvent* event)
{
    Q_UNUSED(event);
    fadeTimer_.start(fadeDelay_);
    event->ignore();
}

void
FadeOutable::mouseMoveEvent(QMouseEvent* event)
{
    // start/restart the timer after which the widget will fade
65
    resetToFadeIn(true);
66
67
68
69
70
71
72
73
74
75
76
77
78
    event->ignore();
}

void
FadeOutable::slotTimeout()
{
    if (!shouldFadeOut()) {
        return;
    }
    fadeAnimation_->start();
    Q_EMIT willFadeOut();
}

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Blinkable::Blinkable(QWidget* parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_NoSystemBackground);
    fadeAnimation_ = new FadeAnimation(this, blinkTime_);
}

Blinkable::~Blinkable()
{}

void
Blinkable::slotAnimationFinished()
{
    fadeAnimation_->setDirection(fadeAnimation_->direction() == QAbstractAnimation::Forward
                                 ? QAbstractAnimation::Backward
                                 : QAbstractAnimation::Forward);
    fadeAnimation_->start();
}

void
Blinkable::start()
{
    connection_ = connect(fadeAnimation_, &QPropertyAnimation::finished, this, &Blinkable::slotAnimationFinished);
    fadeAnimation_->setDirection(QAbstractAnimation::Forward);
    fadeAnimation_->start();
}

void
Blinkable::stop()
{
    disconnect(connection_);
    fadeAnimation_->stop();
}

void
Blinkable::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
    if(!pixmap_)
        return;
    QPainter painter(this);
    painter.drawPixmap(rect(), *pixmap_);
}

123
VignetteWidget::VignetteWidget(QWidget* parent)
124
125
    : QWidget(parent),
      geometryAnimation_(new SizeAnimation(this, rect(), rect()))
126
127
128
129
130
131
132
133
134
135
{
    setAttribute(Qt::WA_NoSystemBackground);
}

VignetteWidget::~VignetteWidget()
{}

void
VignetteWidget::slotWillFadeOut()
{
136
137
    geometryAnimation_->setDirection(QAbstractAnimation::Direction::Forward);
    geometryAnimation_->start();
138
139
140
141
142
}

void
VignetteWidget::slotWillResetOpacity()
{
143
    if (geometryAnimation_->direction() != QAbstractAnimation::Direction::Forward) {
144
145
        return;
    }
146
147
148
149
150
151
152
153
154
155
156
157
158
    geometryAnimation_->setDirection(QAbstractAnimation::Direction::Backward);
    geometryAnimation_->start();
}

void
VignetteWidget::updateGeometry(QSize size)
{
    resize(size);
    QRect endRect = rect();
    Utils::whileBlocking(geometryAnimation_)->setStartValue(endRect);
    endRect.setHeight(endRect.height() + height_);
    endRect.setY(endRect.y() - height_);
    Utils::whileBlocking(geometryAnimation_)->setEndValue(endRect);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
}

void
VignetteWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing, true);

    QLinearGradient gradient;

    // top
    gradient.setStart(0, rect().top());
    gradient.setFinalStop(0, rect().top() + height_);
    gradient.setColorAt(0, QColor(0, 0, 0, 96));
    gradient.setColorAt(0.5, QColor(0, 0, 0, 32));
    gradient.setColorAt(1, Qt::transparent);
176

177
178
    // draw upper rounded corner or just gradient itself
    fillRoundRectPath(painter, gradient);
179
180
181
182
183
184
185

    // bottom
    gradient.setStart(0, rect().bottom() - height_);
    gradient.setFinalStop(0, rect().bottom());
    gradient.setColorAt(0, Qt::transparent);
    gradient.setColorAt(0.5, QColor(0, 0, 0, 32));
    gradient.setColorAt(1, QColor(0, 0, 0, 96));
186

187
188
189
190
191
192
193
194
    // draw bottom rounded corner or just gradient itself
    fillRoundRectPath(painter, gradient);

}

void
VignetteWidget::fillRoundRectPath(QPainter& painter, const QLinearGradient & gradient)
{
195
    if (drawRoundedCorner_) {
196
        Utils::fillRoundRectPath(painter, gradient, rect(), cornerRadius_);
197
198
199
200
    } else {
        painter.fillRect(rect(), gradient);
    }
}
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

void
QmlPopupWidget::showEvent(QShowEvent* event)
{
    QWidget::showEvent(event);
    auto cp = getContainer()->parentWidget();
    if (auto mainWindow = qobject_cast<MainWindow*>(cp)) {
        mainWindow->darken();
    }
}

void
QmlPopupWidget::hideEvent(QHideEvent* event)
{
    QWidget::hideEvent(event);
    auto cp = getContainer()->parentWidget();
    if (auto mainWindow = qobject_cast<MainWindow*>(cp)) {
        mainWindow->lighten();
    }
}