Add screen utilities for screen and center widget on screen

This commit is contained in:
Jonas Kvinge 2023-04-17 21:12:12 +02:00
parent d22712a25b
commit 41d5792b27
10 changed files with 102 additions and 70 deletions

View File

@ -56,6 +56,7 @@ set(SOURCES
utilities/xmlutils.cpp
utilities/filemanagerutils.cpp
utilities/coverutils.cpp
utilities/screenutils.cpp
engine/enginetype.cpp
engine/enginebase.cpp

View File

@ -33,7 +33,6 @@
#include <QObject>
#include <QWidget>
#include <QScreen>
#include <QWindow>
#include <QMetaObject>
#include <QThread>
#include <QSortFilterProxyModel>
@ -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());

View File

@ -29,7 +29,6 @@
#include <QFuture>
#include <QFutureWatcher>
#include <QScreen>
#include <QWindow>
#include <QWidget>
#include <QDialog>
#include <QDir>
@ -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();

View File

@ -31,7 +31,6 @@
#include <QFuture>
#include <QFutureWatcher>
#include <QScreen>
#include <QWindow>
#include <QItemSelectionModel>
#include <QListWidgetItem>
#include <QFile>
@ -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);
}

View File

@ -34,6 +34,8 @@
#include <QListWidget>
#include <QLabel>
#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<int>(screen->geometry().size().width() / 0.5);
max_height = static_cast<int>(static_cast<float>(screen->geometry().size().height()) / static_cast<float>(1.5));

View File

@ -33,7 +33,6 @@
#include <QAbstractItemModel>
#include <QDialog>
#include <QScreen>
#include <QWindow>
#include <QHash>
#include <QMap>
#include <QDir>
@ -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) {

View File

@ -26,7 +26,6 @@
#include <QWidget>
#include <QMainWindow>
#include <QScreen>
#include <QWindow>
#include <QAbstractItemModel>
#include <QAbstractItemView>
#include <QTreeWidget>
@ -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);
}

View File

@ -27,7 +27,6 @@
#include <QWidget>
#include <QDialog>
#include <QScreen>
#include <QWindow>
#include <QMainWindow>
#include <QAbstractItemModel>
#include <QtAlgorithms>
@ -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);
}

View File

@ -0,0 +1,52 @@
/*
* Strawberry Music Player
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#include <QGuiApplication>
#include <QWidget>
#include <QScreen>
#include <QWindow>
#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

View File

@ -0,0 +1,33 @@
/*
* Strawberry Music Player
* Copyright 2023, Jonas Kvinge <jonas@jkvinge.net>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#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