File: notificationpopup.cpp

package info (click to toggle)
gammaray 3.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,612 kB
  • sloc: cpp: 94,643; ansic: 2,227; sh: 336; python: 164; yacc: 90; lex: 82; xml: 61; makefile: 26
file content (200 lines) | stat: -rw-r--r-- 5,410 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include "notificationpopup.h"
#include "ui_notificationpopup.h"
#include "feedbackconfigdialog.h"

#include <provider.h>
#include <surveyinfo.h>

#include <QApplication>
#include <QDebug>
#include <QDesktopServices>
#include <QKeyEvent>
#include <QPropertyAnimation>
#include <QStyle>

using namespace KUserFeedback;

namespace KUserFeedback {
namespace Ui
{
class NotificationPopup;
}

class NotificationPopupPrivate {
public:
    NotificationPopupPrivate(QWidget *qq);
    void showEncouragement();
    void surveyAvailable(const SurveyInfo &info);

    void showPopup();
    void hidePopup();
    void action();
    void reposition();
    int xPosition() const;

    static QString appName();

    Provider *provider;
    SurveyInfo survey;
    QPropertyAnimation *animation;
    std::unique_ptr<Ui::NotificationPopup> ui;
    QWidget *q;
};

}

NotificationPopupPrivate::NotificationPopupPrivate(QWidget *qq) :
    provider(nullptr),
    animation(nullptr),
    q(qq)
{
}

void NotificationPopupPrivate::showEncouragement()
{
    if (q->isVisible())
        return;

    survey = SurveyInfo();
    const auto name = appName();
    if (name.isEmpty()) {
        ui->title->setText(NotificationPopup::tr("Help us make this application better!"));
        ui->message->setText(NotificationPopup::tr("You can help us improving this application by sharing statistics and participate in surveys."));
    } else {
        ui->title->setText(NotificationPopup::tr("Help us make %1 better!").arg(name));
        ui->message->setText(NotificationPopup::tr("You can help us improving %1 by sharing statistics and participate in surveys.").arg(name));
    }
    ui->actionButton->setText(NotificationPopup::tr("Contribute..."));
    showPopup();
}

void NotificationPopupPrivate::surveyAvailable(const SurveyInfo &info)
{
    if (q->isVisible())
        return;

    survey = info;
    const auto name = appName();
    ui->title->setText(NotificationPopup::tr("We are looking for your feedback!"));
    if (name.isEmpty())
        ui->message->setText(NotificationPopup::tr("We would like a few minutes of your time to provide feedback about this application in a survey."));
    else
        ui->message->setText(NotificationPopup::tr("We would like a few minutes of your time to provide feedback about %1 in a survey.").arg(name));
    ui->actionButton->setText(NotificationPopup::tr("Participate"));
    showPopup();
}

void NotificationPopupPrivate::showPopup()
{
    q->show();

    q->resize(q->sizeHint());
    const auto startPos = QPoint(xPosition(), q->parentWidget()->height());
    q->move(startPos);

    if (!animation)
        animation = new QPropertyAnimation(q, "pos", q);
    animation->setStartValue(startPos);
    animation->setEndValue(QPoint(xPosition(), q->parentWidget()->height() - q->height()));
    animation->setDuration(100);
    animation->setEasingCurve(QEasingCurve::InQuad);
    animation->start();

    ui->actionButton->setFocus();
}

void NotificationPopupPrivate::hidePopup()
{
    if (animation)
        animation->stop();
    q->hide();
}

void NotificationPopupPrivate::action()
{
    if (survey.isValid()) {
        QDesktopServices::openUrl(survey.url());
        provider->surveyCompleted(survey);
    } else {
        FeedbackConfigDialog dlg(q);
        dlg.setFeedbackProvider(provider);
        dlg.exec();
    }

    hidePopup();
}

void NotificationPopupPrivate::reposition()
{
    const auto pos = QPoint(xPosition(), q->parentWidget()->height() - q->height());
    if (animation->state() == QAbstractAnimation::Running)
        animation->setEndValue(pos);
    else
        q->move(pos);
}

int NotificationPopupPrivate::xPosition() const
{
    if (QApplication::layoutDirection() == Qt::LeftToRight) {
        return q->parentWidget()->width() - q->width();
    }
    return 0;
}

QString NotificationPopupPrivate::appName()
{
    return QGuiApplication::applicationDisplayName();
}


NotificationPopup::NotificationPopup(QWidget* parent)
    : QWidget(parent)
    , d(new NotificationPopupPrivate(this))
{
    Q_ASSERT(parent);

    d->ui.reset(new Ui::NotificationPopup);
    d->ui->setupUi(this);

    d->ui->frame->setAutoFillBackground(true);
    d->ui->closeButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
    connect(d->ui->actionButton, &QPushButton::clicked, this, [this]() { d->action(); });
    connect(d->ui->closeButton, &QPushButton::clicked, this, [this]() { d->hidePopup(); });

    parent->installEventFilter(this);
    setVisible(false);
}

NotificationPopup::~NotificationPopup()
{
}

void NotificationPopup::setFeedbackProvider(Provider* provider)
{
    Q_ASSERT(provider);
    d->provider = provider;
    connect(provider, &Provider::showEncouragementMessage, this, [this]() { d->showEncouragement(); });
    connect(provider, &Provider::surveyAvailable, this, [this](const SurveyInfo &info) { d->surveyAvailable(info); });
}

void NotificationPopup::keyReleaseEvent(QKeyEvent* event)
{
    if (isVisible() && event->key() == Qt::Key_Escape)
        d->hidePopup();
}

bool NotificationPopup::eventFilter(QObject* receiver, QEvent* event)
{
    if (receiver == parentWidget() && isVisible()) {
        d->reposition();
    }
    return QWidget::eventFilter(receiver, event);
}

#include "moc_notificationpopup.cpp"