From b273a449e3b056b8b68793ae820d6b3fad3e1820 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 18 Apr 2023 17:44:42 +0200 Subject: [PATCH] Add common message dialog --- src/CMakeLists.txt | 4 +- src/core/mainwindow.cpp | 7 ++- src/dialogs/messagedialog.cpp | 57 +++++++++++++++++++ src/dialogs/messagedialog.h | 46 +++++++++++++++ .../{snapdialog.ui => messagedialog.ui} | 16 ++++-- src/dialogs/snapdialog.cpp | 50 +++++++--------- src/dialogs/snapdialog.h | 14 +---- 7 files changed, 144 insertions(+), 50 deletions(-) create mode 100644 src/dialogs/messagedialog.cpp create mode 100644 src/dialogs/messagedialog.h rename src/dialogs/{snapdialog.ui => messagedialog.ui} (91%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 90f7c7be9..a1e5650df 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -208,6 +208,7 @@ set(SOURCES dialogs/userpassdialog.cpp dialogs/deleteconfirmationdialog.cpp dialogs/lastfmimportdialog.cpp + dialogs/messagedialog.cpp dialogs/snapdialog.cpp dialogs/saveplaylistsdialog.cpp @@ -444,6 +445,7 @@ set(HEADERS dialogs/userpassdialog.h dialogs/deleteconfirmationdialog.h dialogs/lastfmimportdialog.h + dialogs/messagedialog.h dialogs/snapdialog.h dialogs/saveplaylistsdialog.h @@ -570,7 +572,7 @@ set(UI dialogs/addstreamdialog.ui dialogs/userpassdialog.ui dialogs/lastfmimportdialog.ui - dialogs/snapdialog.ui + dialogs/messagedialog.ui dialogs/saveplaylistsdialog.ui widgets/trackslider.ui diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index 1eb522cd5..225828b5e 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -1030,12 +1030,13 @@ MainWindow::MainWindow(Application *app, std::shared_ptr tray_ic #ifdef Q_OS_LINUX if (!Utilities::GetEnv("SNAP").isEmpty() && !Utilities::GetEnv("SNAP_NAME").isEmpty()) { s.beginGroup(kSettingsGroup); - if (!s.value("ignore_snap", false).toBool()) { - SnapDialog *snap_dialog = new SnapDialog(); + const bool ignore_snap = s.value("ignore_snap", false).toBool(); + s.endGroup(); + if (!ignore_snap) { + SnapDialog *snap_dialog = new SnapDialog(this); snap_dialog->setAttribute(Qt::WA_DeleteOnClose); snap_dialog->show(); } - s.endGroup(); } #endif diff --git a/src/dialogs/messagedialog.cpp b/src/dialogs/messagedialog.cpp new file mode 100644 index 000000000..64412351b --- /dev/null +++ b/src/dialogs/messagedialog.cpp @@ -0,0 +1,57 @@ +/* + * Strawberry Music Player + * Copyright 2020-2023, Jonas Kvinge + * + * Strawberry 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. + * + * Strawberry 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 Strawberry. If not, see . + * + */ + +#include "config.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "messagedialog.h" +#include "ui_messagedialog.h" + +MessageDialog::MessageDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_MessageDialog) { + + ui_->setupUi(this); + + setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); + + ui_->buttonBox->button(QDialogButtonBox::Ok)->setShortcut(QKeySequence::Close); + + QObject::connect(ui_->checkbox_do_not_show_message_again, &QCheckBox::toggled, this, &MessageDialog::DoNotShowMessageAgain); + +} + +MessageDialog::~MessageDialog() { delete ui_; } + +void MessageDialog::DoNotShowMessageAgain() { + + if (!settings_group_.isEmpty() && !do_not_show_message_again_.isEmpty()) { + QSettings s; + s.beginGroup(settings_group_); + s.setValue(do_not_show_message_again_, ui_->checkbox_do_not_show_message_again->isChecked()); + s.endGroup(); + } + +} diff --git a/src/dialogs/messagedialog.h b/src/dialogs/messagedialog.h new file mode 100644 index 000000000..fdba3fe92 --- /dev/null +++ b/src/dialogs/messagedialog.h @@ -0,0 +1,46 @@ +/* + * Strawberry Music Player + * Copyright 2020-2023, Jonas Kvinge + * + * Strawberry 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. + * + * Strawberry 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 Strawberry. If not, see . + * + */ + +#ifndef MESSAGEDIALOG_H +#define MESSAGEDIALOG_H + +#include + +class Ui_MessageDialog; + +class MessageDialog : public QDialog { + Q_OBJECT + + public: + explicit MessageDialog(QWidget *parent = nullptr); + ~MessageDialog() override; + + void set_settings_group(const QString &settings_group) { settings_group_ = settings_group; } + void set_do_not_show_message_again(const QString &do_not_show_message_again) { do_not_show_message_again_ = do_not_show_message_again; } + + private slots: + void DoNotShowMessageAgain(); + + protected: + Ui_MessageDialog *ui_; + QString settings_group_; + QString do_not_show_message_again_; +}; + +#endif // MESSAGEDIALOG_H diff --git a/src/dialogs/snapdialog.ui b/src/dialogs/messagedialog.ui similarity index 91% rename from src/dialogs/snapdialog.ui rename to src/dialogs/messagedialog.ui index 949450aca..5f6bfae85 100644 --- a/src/dialogs/snapdialog.ui +++ b/src/dialogs/messagedialog.ui @@ -1,7 +1,7 @@ - SnapDialog - + MessageDialog + 0 @@ -14,7 +14,7 @@ Qt::StrongFocus - Strawberry is running as a snap + Message Dialog @@ -46,6 +46,12 @@ Qt::Vertical + + + 0 + 0 + + @@ -121,7 +127,7 @@ buttonBox accepted() - SnapDialog + MessageDialog accept() @@ -137,7 +143,7 @@ buttonBox rejected() - SnapDialog + MessageDialog reject() diff --git a/src/dialogs/snapdialog.cpp b/src/dialogs/snapdialog.cpp index 9e7fc153c..e7cc492ef 100644 --- a/src/dialogs/snapdialog.cpp +++ b/src/dialogs/snapdialog.cpp @@ -1,6 +1,6 @@ /* * Strawberry Music Player - * Copyright 2020-2021, Jonas Kvinge + * Copyright 2020-2023, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,28 +17,26 @@ * */ -#include "config.h" - -#include -#include -#include -#include -#include -#include -#include -#include +#include "core/logging.h" +#include "core/iconloader.h" +#include "core/mainwindow.h" +#include "utilities/screenutils.h" #include "snapdialog.h" -#include "ui_snapdialog.h" +#include "ui_messagedialog.h" -#include "core/mainwindow.h" +SnapDialog::SnapDialog(QWidget *parent) : MessageDialog(parent) { -SnapDialog::SnapDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_SnapDialog) { - - ui_->setupUi(this); - setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint); setWindowTitle(tr("Strawberry is running as a Snap")); + const QIcon icon = IconLoader::Load("dialog-warning"); +#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) + const QPixmap pixmap = icon.pixmap(QSize(64, 64), devicePixelRatioF()); +#else + const QPixmap pixmap = icon.pixmap(QSize(64, 64)); +#endif + ui_->label_logo->setPixmap(pixmap); + QString text; text += QString("

"); text += tr("It is detected that Strawberry is running as a Snap"); @@ -88,19 +86,11 @@ SnapDialog::SnapDialog(QWidget *parent) : QDialog(parent), ui_(new Ui_SnapDialog ui_->label_text->adjustSize(); adjustSize(); - ui_->buttonBox->button(QDialogButtonBox::Ok)->setShortcut(QKeySequence::Close); + settings_group_ = MainWindow::kSettingsGroup; + do_not_show_message_again_ = "ignore_snap"; - QObject::connect(ui_->checkbox_do_not_show_message_again, &QCheckBox::toggled, this, &SnapDialog::DoNotShowMessageAgain); - -} - -SnapDialog::~SnapDialog() { delete ui_; } - -void SnapDialog::DoNotShowMessageAgain() { - - QSettings s; - s.beginGroup(MainWindow::kSettingsGroup); - s.setValue("ignore_snap", ui_->checkbox_do_not_show_message_again->isChecked()); - s.endGroup(); + if (parent) { + Utilities::CenterWidgetOnScreen(Utilities::GetScreen(parent), this); + } } diff --git a/src/dialogs/snapdialog.h b/src/dialogs/snapdialog.h index 085ee7747..44bc9acb4 100644 --- a/src/dialogs/snapdialog.h +++ b/src/dialogs/snapdialog.h @@ -1,6 +1,6 @@ /* * Strawberry Music Player - * Copyright 2020-2021, Jonas Kvinge + * Copyright 2020-2023, Jonas Kvinge * * Strawberry is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,22 +20,14 @@ #ifndef SNAPDIALOG_H #define SNAPDIALOG_H -#include +#include "messagedialog.h" -class Ui_SnapDialog; - -class SnapDialog : public QDialog { +class SnapDialog : public MessageDialog { Q_OBJECT public: explicit SnapDialog(QWidget *parent = nullptr); - ~SnapDialog() override; - private slots: - void DoNotShowMessageAgain(); - - private: - Ui_SnapDialog *ui_; }; #endif // SNAPDIALOG_H