KUserFeedback

feedbackconfigwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include "feedbackconfigwidget.h"
8#include "ui_feedbackconfigwidget.h"
9#include "auditlogbrowserdialog.h"
10
11#include <abstractdatasource.h>
12#include <auditloguicontroller.h>
13#include <feedbackconfiguicontroller.h>
14
15#include <QApplication>
16#include <QDebug>
17#include <QMessageBox>
18#include <QScrollBar>
19#include <QStyle>
20
21#include <algorithm>
22#include <vector>
23
24using namespace KUserFeedback;
25
26namespace KUserFeedback {
27class FeedbackConfigWidgetPrivate {
28public:
29 void telemetrySliderChanged();
30 void surveySliderChanged();
31 void applyPalette(QSlider *slider);
32
34 std::unique_ptr<Ui::FeedbackConfigWidget> ui;
35 AuditLogUiController *auditLogController;
36};
37
38}
39
40void FeedbackConfigWidgetPrivate::telemetrySliderChanged()
41{
42 if (ui->telemetrySlider->value() == 0) {
43 ui->telemetryStack->setCurrentWidget(ui->noTelemetryPage);
44 } else {
45 ui->telemetryStack->setCurrentWidget(ui->telemetryPage);
46 ui->telemetryLabel->setText(controller->telemetryModeDescription(ui->telemetrySlider->value()));
47 }
48
49 applyPalette(ui->telemetrySlider);
50
51 if (!controller->feedbackProvider())
52 return;
53
54 if (!ui->rawTelemetryButton->isChecked()) {
55 ui->telemetryDetails->setHtml(controller->telemetryModeDetails(ui->telemetrySlider->value()));
56 } else {
57 QByteArray jsonData;
58 QMetaObject::invokeMethod(controller->feedbackProvider(), "jsonData", Q_RETURN_ARG(QByteArray, jsonData), Q_ARG(KUserFeedback::Provider::TelemetryMode, controller->telemetryIndexToMode(ui->telemetrySlider->value())));
59 ui->telemetryDetails->setPlainText(QString::fromUtf8(jsonData.constData()));
60 }
61}
62
63void FeedbackConfigWidgetPrivate::surveySliderChanged()
64{
65 if (!controller->feedbackProvider())
66 return;
67
68 ui->surveyLabel->setText(controller->surveyModeDescription(ui->surveySlider->value()));
69 applyPalette(ui->surveySlider);
70}
71
72void FeedbackConfigWidgetPrivate::applyPalette(QSlider* slider)
73{
74 const auto ratio = (double)slider->value() / (double)slider->maximum();
75 const auto red = qBound<double>(0.0, 2.0 - ratio * 2.0, 1.0);
76 const auto green = qBound<double>(0.0, ratio * 2.0, 1.0);
77 auto color = QColor(255 * red, 255 * green, 0);
78
79 if (QApplication::palette().color(QPalette::Base).lightness() > 128)
80 color = color.lighter(150);
81 else
82 color = color.darker(150);
83
84 auto pal = slider->palette();
85 pal.setColor(QPalette::Highlight, color);
86 slider->setPalette(pal);
87}
88
89
91 : QWidget(parent)
92 , d(new FeedbackConfigWidgetPrivate)
93{
94 d->controller = new FeedbackConfigUiController(this);
95 d->ui.reset(new Ui::FeedbackConfigWidget);
96 d->ui->setupUi(this);
97 d->ui->noTelemetryLabel->setText(d->controller->telemetryModeDescription(Provider::NoTelemetry));
98
99 connect(d->ui->telemetrySlider, &QSlider::valueChanged, this, [this]() { d->telemetrySliderChanged(); });
101 connect(d->ui->surveySlider, &QSlider::valueChanged, this, [this]() { d->surveySliderChanged(); });
103
104 d->ui->rawTelemetryButton->setParent(d->ui->telemetryDetails);
105 d->ui->rawTelemetryButton->setIcon(style()->standardPixmap(QStyle::SP_DialogHelpButton));
106 d->ui->telemetryDetails->installEventFilter(this);
107 connect(d->ui->rawTelemetryButton, &QAbstractButton::toggled, this, [this]() { d->telemetrySliderChanged(); });
108
109 d->auditLogController = new AuditLogUiController(this);
110 connect(d->ui->auditLogLabel, &QLabel::linkActivated, this, [this](){
111 if (d->auditLogController->hasLogEntries()) {
112 AuditLogBrowserDialog dlg(this);
113 dlg.setUiController(d->auditLogController);
114 dlg.exec();
115 } else {
116 QMessageBox::information(this, QString(), tr("No data has been sent at this point."));
117 }
118 });
119
120 setEnabled(false); // see setFeedbackProvider
121}
122
123FeedbackConfigWidget::~FeedbackConfigWidget()
124{
125}
126
128{
129 return d->controller->feedbackProvider();
130}
131
133{
134 d->controller->setFeedbackProvider(provider);
135 if (!provider) {
136 setEnabled(false);
137 return;
138 }
139
140 const auto hasTelemetry = d->controller->telemetryModeCount() > 1;
141 d->ui->telemetrySlider->setEnabled(hasTelemetry);
142 d->ui->telemetryStack->setEnabled(hasTelemetry);
143 if (hasTelemetry)
144 d->ui->telemetrySlider->setMaximum(d->controller->telemetryModeCount() - 1);
145
146 d->ui->telemetrySlider->setValue(d->controller->telemetryModeToIndex(provider->telemetryMode()));
147 d->ui->surveySlider->setValue(d->controller->surveyIntervalToIndex(provider->surveyInterval()));
148 d->surveySliderChanged(); // update the description even if nothing changed initially
149
150 setEnabled(provider);
151}
152
154{
155 if (receiver == d->ui->telemetryDetails) {
156 d->ui->rawTelemetryButton->move(
157 d->ui->telemetryDetails->width()
158 - d->ui->rawTelemetryButton->width()
159 - style()->pixelMetric(QStyle::PM_LayoutRightMargin)
160 - (d->ui->telemetryDetails->verticalScrollBar()->isVisible() ? d->ui->telemetryDetails->verticalScrollBar()->width() : 0),
161 d->ui->telemetryDetails->height()
162 - d->ui->rawTelemetryButton->height()
163 - style()->pixelMetric(QStyle::PM_LayoutBottomMargin)
164 );
165 }
166 return QWidget::eventFilter(receiver, event);
167}
168
170{
171 return d->controller->telemetryIndexToMode(d->ui->telemetrySlider->value());
172}
173
175{
176 return d->controller->surveyIndexToInterval(d->ui->surveySlider->value());
177}
178
179#include "moc_feedbackconfigwidget.cpp"
Widget/QtQuick-independent logic and data for the audit log browser UI.
Logic/behavior of the feedback configuration UI.
KUserFeedback::Provider * feedbackProvider
The Provider instance we are configuring.
Q_INVOKABLE QString telemetryModeDescription(int telemetryIndex) const
Telemetry mode explanation text.
Q_INVOKABLE QString surveyModeDescription(int surveyIndex) const
Survey mode explanation text.
Q_INVOKABLE QString telemetryModeDetails(int telemetryIndex) const
Detailed information about the data sources of the given telemetry mode index.
Q_INVOKABLE KUserFeedback::Provider::TelemetryMode telemetryIndexToMode(int index) const
Convert slider index to telemetry mode.
void configurationChanged()
Emitted when any changes are made to the configuration represented in this widget.
Provider * feedbackProvider() const
Returns the feedback provider configured by this widget.
FeedbackConfigWidget(QWidget *parent=nullptr)
Create a new feedback provider configuration widget.
Provider::TelemetryMode telemetryMode() const
Returns the telemetry level currently selected in the widget.
int surveyInterval() const
Returns the survey interval currently selected in this widget.
void setFeedbackProvider(Provider *provider)
Set the feedback provider that should be configured with this widget.
The central object managing data sources and transmitting feedback to the server.
Definition provider.h:32
int surveyInterval
The interval in which the user accepts surveys.
Definition provider.h:44
TelemetryMode telemetryMode
The telemetry mode the user has configured.
Definition provider.h:50
TelemetryMode
Telemetry collection modes.
Definition provider.h:102
@ NoTelemetry
Transmit no data at all.
Definition provider.h:103
Classes for integrating telemetry collection, survey targeting, and contribution encouragenemt and co...
void toggled(bool checked)
void valueChanged(int value)
const char * constData() const const
QPalette palette()
void linkActivated(const QString &link)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool eventFilter(QObject *watched, QEvent *event)
QString fromUtf8(QByteArrayView str)
PM_LayoutRightMargin
SP_DialogHelpButton
void setEnabled(bool)
virtual bool event(QEvent *event) override
QStyle * style() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:57:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.