From 41d5792b27b0e9aaf95fbf4077d3dcbce9d0e158 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Mon, 17 Apr 2023 21:12:12 +0200 Subject: [PATCH] Add screen utilities for screen and center widget on screen --- src/CMakeLists.txt | 1 + src/core/mainwindow.cpp | 8 +-- .../albumcoverchoicecontroller.cpp | 8 +-- src/covermanager/albumcovermanager.cpp | 14 +---- src/dialogs/deleteconfirmationdialog.cpp | 8 ++- src/organize/organizedialog.cpp | 20 ++----- src/settings/settingsdialog.cpp | 14 +---- src/transcoder/transcodedialog.cpp | 14 +---- src/utilities/screenutils.cpp | 52 +++++++++++++++++++ src/utilities/screenutils.h | 33 ++++++++++++ 10 files changed, 102 insertions(+), 70 deletions(-) create mode 100644 src/utilities/screenutils.cpp create mode 100644 src/utilities/screenutils.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 918d9face..90f7c7be9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -56,6 +56,7 @@ set(SOURCES utilities/xmlutils.cpp utilities/filemanagerutils.cpp utilities/coverutils.cpp + utilities/screenutils.cpp engine/enginetype.cpp engine/enginebase.cpp diff --git a/src/core/mainwindow.cpp b/src/core/mainwindow.cpp index 568481759..9d1b11d50 100644 --- a/src/core/mainwindow.cpp +++ b/src/core/mainwindow.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -102,6 +101,7 @@ #include "utilities/envutils.h" #include "utilities/filemanagerutils.h" #include "utilities/timeconstants.h" +#include "utilities/screenutils.h" #include "engine/enginetype.h" #include "engine/enginebase.h" #include "engine/engine_fwd.h" @@ -2389,11 +2389,7 @@ void MainWindow::CommandlineOptionsReceived(const CommandlineOptions &options) { if (w_ok && h_ok) { QSize window_size(w, h); if (window_size.isValid()) { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = QWidget::screen(); -#else - QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : nullptr); -#endif + QScreen *screen = Utilities::GetScreen(this); if (screen) { const QRect sr = screen->availableGeometry(); window_size = window_size.boundedTo(sr.size()); diff --git a/src/covermanager/albumcoverchoicecontroller.cpp b/src/covermanager/albumcoverchoicecontroller.cpp index 66b243e0b..eb21d5d01 100644 --- a/src/covermanager/albumcoverchoicecontroller.cpp +++ b/src/covermanager/albumcoverchoicecontroller.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include #include @@ -61,6 +60,7 @@ #include "utilities/imageutils.h" #include "utilities/coveroptions.h" #include "utilities/coverutils.h" +#include "utilities/screenutils.h" #include "core/application.h" #include "core/song.h" #include "core/iconloader.h" @@ -478,11 +478,7 @@ void AlbumCoverChoiceController::ShowCover(const Song &song, const QPixmap &pixm title_text += " (" + QString::number(pixmap.width()) + "x" + QString::number(pixmap.height()) + "px)"; // If the cover is larger than the screen, resize the window 85% seems to be enough to account for title bar and taskbar etc. -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = QWidget::screen(); -#else - QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen()); -#endif + QScreen *screen = Utilities::GetScreen(this); QRect screenGeometry = screen->availableGeometry(); int desktop_height = screenGeometry.height(); int desktop_width = screenGeometry.width(); diff --git a/src/covermanager/albumcovermanager.cpp b/src/covermanager/albumcovermanager.cpp index 431d5f0fb..cafe75d4c 100644 --- a/src/covermanager/albumcovermanager.cpp +++ b/src/covermanager/albumcovermanager.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -68,6 +67,7 @@ #include "utilities/fileutils.h" #include "utilities/imageutils.h" #include "utilities/mimeutils.h" +#include "utilities/screenutils.h" #include "core/application.h" #include "core/iconloader.h" #include "core/tagreaderclient.h" @@ -297,17 +297,7 @@ void AlbumCoverManager::LoadGeometry() { s.endGroup(); // Center the window on the same screen as the mainwindow. -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = mainwindow_->screen(); -#else - QScreen *screen = (mainwindow_->window() && mainwindow_->window()->windowHandle() ? mainwindow_->window()->windowHandle()->screen() : nullptr); -#endif - if (screen) { - const QRect sr = screen->availableGeometry(); - const QRect wr({}, size().boundedTo(sr.size())); - resize(wr.size()); - move(sr.center() - wr.center()); - } + Utilities::CenterWidgetOnScreen(Utilities::GetScreen(mainwindow_), this); } diff --git a/src/dialogs/deleteconfirmationdialog.cpp b/src/dialogs/deleteconfirmationdialog.cpp index 15fd02d7f..fe641de9b 100644 --- a/src/dialogs/deleteconfirmationdialog.cpp +++ b/src/dialogs/deleteconfirmationdialog.cpp @@ -34,6 +34,8 @@ #include #include +#include "utilities/screenutils.h" + #include "deleteconfirmationdialog.h" DeleteConfirmationDialog::DeleteConfirmationDialog(const QStringList &files, QWidget *parent) @@ -86,11 +88,7 @@ DeleteConfirmationDialog::DeleteConfirmationDialog(const QStringList &files, QWi // Set size of dialog int max_width = 0; int max_height = 0; -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = QWidget::screen(); -#else - QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen()); -#endif + QScreen *screen = Utilities::GetScreen(this); if (screen) { max_width = static_cast(screen->geometry().size().width() / 0.5); max_height = static_cast(static_cast(screen->geometry().size().height()) / static_cast(1.5)); diff --git a/src/organize/organizedialog.cpp b/src/organize/organizedialog.cpp index 3008ca114..bb590d06a 100644 --- a/src/organize/organizedialog.cpp +++ b/src/organize/organizedialog.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -60,6 +59,7 @@ #include "core/musicstorage.h" #include "core/tagreaderclient.h" #include "utilities/strutils.h" +#include "utilities/screenutils.h" #include "widgets/freespacebar.h" #include "widgets/linetextedit.h" #include "collection/collectionbackend.h" @@ -221,17 +221,7 @@ void OrganizeDialog::LoadGeometry() { if (parentwindow_) { // Center the window on the same screen as the parentwindow. -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = parentwindow_->screen(); -#else - QScreen *screen = (parentwindow_->window() && parentwindow_->window()->windowHandle() ? parentwindow_->window()->windowHandle()->screen() : nullptr); -#endif - if (screen) { - const QRect sr = screen->availableGeometry(); - const QRect wr({}, size().boundedTo(sr.size())); - resize(wr.size()); - move(sr.center() - wr.center()); - } + Utilities::CenterWidgetOnScreen(Utilities::GetScreen(parentwindow_), this); } } @@ -249,11 +239,7 @@ void OrganizeDialog::SaveGeometry() { void OrganizeDialog::AdjustSize() { -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = QWidget::screen(); -#else - QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen()); -#endif + QScreen *screen = Utilities::GetScreen(this); int max_width = 0; int max_height = 0; if (screen) { diff --git a/src/settings/settingsdialog.cpp b/src/settings/settingsdialog.cpp index 085cd812b..b87a88fb3 100644 --- a/src/settings/settingsdialog.cpp +++ b/src/settings/settingsdialog.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -50,6 +49,7 @@ #include "core/application.h" #include "core/player.h" +#include "utilities/screenutils.h" #include "widgets/groupediconview.h" #include "collection/collectionmodel.h" @@ -245,17 +245,7 @@ void SettingsDialog::LoadGeometry() { s.endGroup(); // Center the dialog on the same screen as mainwindow. -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = mainwindow_->screen(); -#else - QScreen *screen = (mainwindow_->window() && mainwindow_->window()->windowHandle() ? mainwindow_->window()->windowHandle()->screen() : nullptr); -#endif - if (screen) { - const QRect sr = screen->availableGeometry(); - const QRect wr({}, size().boundedTo(sr.size())); - resize(wr.size()); - move(sr.center() - wr.center()); - } + Utilities::CenterWidgetOnScreen(Utilities::GetScreen(mainwindow_), this); } diff --git a/src/transcoder/transcodedialog.cpp b/src/transcoder/transcodedialog.cpp index 78d4034c8..ccb0feebb 100644 --- a/src/transcoder/transcodedialog.cpp +++ b/src/transcoder/transcodedialog.cpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -58,6 +57,7 @@ #include "core/iconloader.h" #include "core/mainwindow.h" +#include "utilities/screenutils.h" #include "widgets/fileview.h" #include "transcodedialog.h" #include "transcoder.h" @@ -194,17 +194,7 @@ void TranscodeDialog::LoadGeometry() { s.endGroup(); // Center the window on the same screen as the mainwindow. -#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) - QScreen *screen = mainwindow_->screen(); -#else - QScreen *screen = (mainwindow_->window() && mainwindow_->window()->windowHandle() ? mainwindow_->window()->windowHandle()->screen() : nullptr); -#endif - if (screen) { - const QRect sr = screen->availableGeometry(); - const QRect wr({}, size().boundedTo(sr.size())); - resize(wr.size()); - move(sr.center() - wr.center()); - } + Utilities::CenterWidgetOnScreen(Utilities::GetScreen(mainwindow_), this); } diff --git a/src/utilities/screenutils.cpp b/src/utilities/screenutils.cpp new file mode 100644 index 000000000..0afd839c9 --- /dev/null +++ b/src/utilities/screenutils.cpp @@ -0,0 +1,52 @@ +/* + * Strawberry Music Player + * Copyright 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 +#include +#include +#include + +#include "screenutils.h" + +namespace Utilities { + +QScreen *GetScreen(QWidget *widget) { + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)) + QScreen *screen = widget->screen(); +#else + QScreen *screen = (widget->window() && widget->window()->windowHandle() ? widget->window()->windowHandle()->screen() : nullptr); +#endif + + return screen; + +} + +void CenterWidgetOnScreen(QScreen *screen, QWidget *widget) { + + if (screen) { + const QRect sr = screen->availableGeometry(); + const QRect wr({}, widget->size().boundedTo(sr.size())); + widget->resize(wr.size()); + widget->move(sr.center() - wr.center()); + } + +} + +} // namespace Utilities diff --git a/src/utilities/screenutils.h b/src/utilities/screenutils.h new file mode 100644 index 000000000..10770cf82 --- /dev/null +++ b/src/utilities/screenutils.h @@ -0,0 +1,33 @@ +/* + * Strawberry Music Player + * Copyright 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 SCREENUTILS_H +#define SCREENUTILS_H + +class QScreen; +class QWidget; + +namespace Utilities { + +QScreen *GetScreen(QWidget *widget); +void CenterWidgetOnScreen(QScreen *screen, QWidget *widget); + +} // namespace Utilities + +#endif // SCREENUTILS_H