Replace all uses of QDesktopWidget with QScreen

This commit is contained in:
Jonas Kvinge 2020-01-04 21:18:12 +01:00
parent 0308a3f9a5
commit 6a8f70285f
7 changed files with 180 additions and 73 deletions

View File

@ -29,8 +29,10 @@
#include "ui/coverfromurldialog.h"
#include "ui/iconloader.h"
#include <QGuiApplication>
#include <QScreen>
#include <QWindow>
#include <QAction>
#include <QDesktopWidget>
#include <QDialog>
#include <QDragEnterEvent>
#include <QFileDialog>
@ -244,10 +246,15 @@ QDialog* AlbumCoverChoiceController::ShowCoverPrivate(const Song& song) {
// if the cover is larger than the screen, resize the window
// 85% seems to be enough to account for title bar and taskbar etc.
QDesktopWidget desktop;
int current_screen = desktop.screenNumber(this);
int desktop_height = desktop.screenGeometry(current_screen).height();
int desktop_width = desktop.screenGeometry(current_screen).width();
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = screen();
#else
QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen());
#endif
QRect screenGeometry = screen->availableGeometry();
int desktop_height = screenGeometry.height();
int desktop_width = screenGeometry.width();
// resize differently if monitor is in portrait mode
if (desktop_width < desktop_height) {

View File

@ -220,7 +220,7 @@ void NotificationsSettingsPage::Save() {
s.setValue("foreground_color", pretty_popup_->foreground_color());
s.setValue("background_color", pretty_popup_->background_color());
s.setValue("background_opacity", pretty_popup_->background_opacity());
s.setValue("popup_display", pretty_popup_->popup_display());
s.setValue("popup_screen", pretty_popup_->popup_screen());
s.setValue("popup_pos", pretty_popup_->popup_pos());
s.setValue("font", pretty_popup_->font().toString());
s.setValue("disable_duration",

View File

@ -81,8 +81,9 @@
#include "internet/spotify/spotifysettingspage.h"
#endif
#include <QScreen>
#include <QWindow>
#include <QAbstractButton>
#include <QDesktopWidget>
#include <QPainter>
#include <QPushButton>
#include <QScrollArea>
@ -304,10 +305,16 @@ void SettingsDialog::showEvent(QShowEvent* e) {
loading_settings_ = false;
// Resize the dialog if it's too big
const QSize available =
QApplication::desktop()->availableGeometry(this).size();
if (available.height() < height()) {
resize(width(), sizeHint().height());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = screen();
#else
QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen());
#endif
if (screen) {
const QRect available = screen->availableGeometry();
if (available.height() < height()) {
resize(width(), sizeHint().height());
}
}
QDialog::showEvent(e);

View File

@ -17,7 +17,9 @@
#include "nowplayingwidget.h"
#include <QDesktopWidget>
#include <QGuiApplication>
#include <QScreen>
#include <QWindow>
#include <QMenu>
#include <QMovie>
#include <QPainter>
@ -647,10 +649,13 @@ void NowPlayingWidget::SearchCoverAutomatically() {
}
void NowPlayingWidget::Bask() {
QDesktopWidget desktop;
int current_screen = desktop.screenNumber(this);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = screen();
#else
QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen());
#endif
big_hypnotoad_.reset(new FullscreenHypnotoad);
big_hypnotoad_->setGeometry(desktop.screenGeometry(current_screen));
if (screen) big_hypnotoad_->setGeometry(screen->availableGeometry());
big_hypnotoad_->showFullScreen();
}

View File

@ -20,9 +20,11 @@
#include "ui_osdpretty.h"
#include <QApplication>
#include <QGuiApplication>
#include <QScreen>
#include <QWindow>
#include <QBitmap>
#include <QColor>
#include <QDesktopWidget>
#include <QLayout>
#include <QMouseEvent>
#include <QPainter>
@ -30,8 +32,6 @@
#include <QTimer>
#include <QTimeLine>
#include <QtDebug>
#ifdef HAVE_X11
#include <QX11Info>
#endif
@ -60,7 +60,7 @@ OSDPretty::OSDPretty(Mode mode, QWidget* parent)
mode_(mode),
background_color_(kPresetBlue),
background_opacity_(0.85),
popup_display_(0),
popup_screen_(nullptr),
font_(QFont()),
disable_duration_(false),
timeout_(new QTimer(this)),
@ -125,20 +125,27 @@ OSDPretty::OSDPretty(Mode mode, QWidget* parent)
int margin = l->margin() + kDropShadowSize;
l->setMargin(margin);
// Get current screen resolution
QRect screenResolution = QApplication::desktop()->screenGeometry();
// Leave 200 px for icon
ui_->summary->setMaximumWidth(screenResolution.width() - 200);
ui_->message->setMaximumWidth(screenResolution.width() - 200);
// Set maximum size for the OSD, a little margin here too
setMaximumSize(screenResolution.width() - 100,
screenResolution.height() - 100);
connect(qApp, SIGNAL(screenAdded(QScreen*)), this, SLOT(ScreenAdded(QScreen*)));
connect(qApp, SIGNAL(screenRemoved(QScreen*)), this, SLOT(ScreenRemoved(QScreen*)));
// Don't load settings here, they will be reloaded anyway on creation
}
OSDPretty::~OSDPretty() { delete ui_; }
void OSDPretty::ScreenAdded(QScreen *screen) {
screens_.insert(screen->name(), screen);
}
void OSDPretty::ScreenRemoved(QScreen *screen) {
if (screens_.contains(screen->name())) screens_.remove(screen->name());
if (screen == popup_screen_) popup_screen_ = current_screen();
}
bool OSDPretty::IsTransparencyAvailable() {
#if defined(HAVE_X11) && (QT_VERSION >= QT_VERSION_CHECK(5, 7, 0))
return QX11Info::isCompositingManagerRunning();
@ -147,19 +154,51 @@ bool OSDPretty::IsTransparencyAvailable() {
}
void OSDPretty::Load() {
QSettings s;
s.beginGroup(kSettingsGroup);
foreground_color_ = QColor(s.value("foreground_color", 0).toInt());
background_color_ = QColor(s.value("background_color", kPresetBlue).toInt());
background_opacity_ = s.value("background_opacity", 0.85).toDouble();
popup_display_ = s.value("popup_display", -1).toInt();
popup_pos_ = s.value("popup_pos", QPoint(0, 0)).toPoint();
font_.fromString(s.value("font", "Verdana,9,-1,5,50,0,0,0,0,0").toString());
disable_duration_ = s.value("disable_duration", false).toBool();
if (s.contains("popup_screen")) {
popup_screen_name_ = s.value("popup_screen").toString();
if (screens_.contains(popup_screen_name_)) {
popup_screen_ = screens_[popup_screen_name_];
}
else {
popup_screen_ = current_screen();
if (current_screen()) popup_screen_name_ = current_screen()->name();
else popup_screen_name_.clear();
}
}
else {
popup_screen_ = current_screen();
if (current_screen()) popup_screen_name_ = current_screen()->name();
}
if (s.contains("popup_pos")) {
popup_pos_ = s.value("popup_pos").toPoint();
}
else {
if (popup_screen_) {
QRect geometry = popup_screen_->availableGeometry();
popup_pos_.setX(geometry.width() - width());
popup_pos_.setY(0);
}
else {
popup_pos_.setX(0);
popup_pos_.setY(0);
}
}
set_font(font());
set_foreground_color(foreground_color());
s.endGroup();
}
void OSDPretty::ReloadSettings() {
@ -268,11 +307,27 @@ void OSDPretty::ShowMessage(const QString& summary, const QString& message,
}
}
void OSDPretty::showEvent(QShowEvent* e) {
void OSDPretty::showEvent(QShowEvent *e) {
screens_.clear();
for(QScreen *screen : qApp->screens()) {
screens_.insert(screen->name(), screen);
}
// Get current screen resolution
QRect screenResolution = current_screen()->availableGeometry();
// Leave 200 px for icon
ui_->summary->setMaximumWidth(screenResolution.width() - 200);
ui_->message->setMaximumWidth(screenResolution.width() - 200);
// Set maximum size for the OSD, a little margin here too
setMaximumSize(screenResolution.width() - 100,
screenResolution.height() - 100);
setWindowOpacity(fading_enabled_ ? 0.0 : 1.0);
QWidget::showEvent(e);
Load();
Reposition();
if (fading_enabled_) {
@ -305,7 +360,6 @@ void OSDPretty::FaderFinished() {
void OSDPretty::FaderValueChanged(qreal value) { setWindowOpacity(value); }
void OSDPretty::Reposition() {
QDesktopWidget* desktop = QApplication::desktop();
// Make the OSD the proper size
layout()->activate();
@ -313,21 +367,23 @@ void OSDPretty::Reposition() {
// Work out where to place the OSD. -1 for x or y means "on the right or
// bottom edge".
QRect geometry(desktop->availableGeometry(popup_display_));
if (popup_screen_) {
int x = popup_pos_.x() < 0 ? geometry.right() - width()
QRect geometry = popup_screen_->availableGeometry();
int x = popup_pos_.x() < 0 ? geometry.right() - width()
: geometry.left() + popup_pos_.x();
int y = popup_pos_.y() < 0 ? geometry.bottom() - height()
: geometry.top() + popup_pos_.y();
int y = popup_pos_.y() < 0 ? geometry.bottom() - height()
: geometry.top() + popup_pos_.y();
#ifndef Q_OS_WIN32
// windows needs negative coordinates for monitors
// to the left or above the primary
x = qBound(0, x, geometry.right() - width());
y = qBound(0, y, geometry.bottom() - height());
// windows needs negative coordinates for monitors
// to the left or above the primary
x = qBound(0, x, geometry.right() - width());
y = qBound(0, y, geometry.bottom() - height());
#endif
move(x, y);
move(x, y);
}
// Create a mask for the actual area of the OSD
QBitmap mask(size());
@ -374,8 +430,13 @@ void OSDPretty::mouseMoveEvent(QMouseEvent* e) {
QPoint new_pos = original_window_pos_ + delta;
// Keep it to the bounds of the desktop
QDesktopWidget* desktop = QApplication::desktop();
QRect geometry(desktop->availableGeometry(e->globalPos()));
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
QScreen *screen = QGuiApplication::screenAt(e->globalPos());
#else
QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen());
#endif
if (!screen) return;
QRect geometry = screen->availableGeometry();
new_pos.setX(
qBound(geometry.left(), new_pos.x(), geometry.right() - width()));
@ -391,26 +452,35 @@ void OSDPretty::mouseMoveEvent(QMouseEvent* e) {
move(new_pos);
popup_display_ = current_display();
popup_pos_ = current_pos();
popup_screen_ = screen;
popup_screen_name_ = screen->name();
}
}
QScreen *OSDPretty::current_screen() const {
#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
return QGuiApplication::screenAt(pos());
#else
return (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen());
#endif
}
QPoint OSDPretty::current_pos() const {
QDesktopWidget* desktop = QApplication::desktop();
QRect geometry(desktop->availableGeometry(current_display()));
int x = pos().x() >= geometry.right() - width() ? -1
if (current_screen()) {
QRect geometry = current_screen()->availableGeometry();
int x = pos().x() >= geometry.right() - width() ? -1
: pos().x() - geometry.left();
int y = pos().y() >= geometry.bottom() - height() ? -1 : pos().y() -
int y = pos().y() >= geometry.bottom() - height() ? -1 : pos().y() -
geometry.top();
return QPoint(x, y);
}
return QPoint(x, y);
}
return QPoint(0, 0);
int OSDPretty::current_display() const {
QDesktopWidget* desktop = QApplication::desktop();
return desktop->screenNumber(pos());
}
void OSDPretty::set_background_color(QRgb color) {
@ -436,8 +506,9 @@ void OSDPretty::set_foreground_color(QRgb color) {
void OSDPretty::set_popup_duration(int msec) { timeout_->setInterval(msec); }
void OSDPretty::mouseReleaseEvent(QMouseEvent*) {
if (mode_ == Mode_Draggable) {
popup_display_ = current_display();
if (current_screen() && mode_ == Mode_Draggable) {
popup_screen_ = current_screen();
popup_screen_name_ = current_screen()->name();
popup_pos_ = current_pos();
}
}

View File

@ -19,9 +19,11 @@
#define OSDPRETTY_H
#include <QWidget>
#include <QMap>
class Ui_OSDPretty;
class QScreen;
class QTimeLine;
class OSDPretty : public QWidget {
@ -66,7 +68,7 @@ class OSDPretty : public QWidget {
QRgb foreground_color() const { return foreground_color_.rgb(); }
QRgb background_color() const { return background_color_.rgb(); }
qreal background_opacity() const { return background_opacity_; }
int popup_display() const { return popup_display_; }
QString popup_screen() const { return popup_screen_name_; }
QPoint popup_pos() const { return popup_pos_; }
QFont font() const { return font_; }
bool disable_duration() const { return disable_duration_; }
@ -74,7 +76,7 @@ class OSDPretty : public QWidget {
// When the user has been moving the popup, use these to get its current
// position and screen. Note that these return invalid values if the popup
// is hidden.
int current_display() const;
QScreen *current_screen() const;
QPoint current_pos() const;
// QWidget
@ -104,6 +106,8 @@ class OSDPretty : public QWidget {
private slots:
void FaderValueChanged(qreal value);
void FaderFinished();
void ScreenAdded(QScreen* screen);
void ScreenRemoved(QScreen* screen);
private:
Ui_OSDPretty* ui_;
@ -114,8 +118,9 @@ class OSDPretty : public QWidget {
QColor foreground_color_;
QColor background_color_;
float background_opacity_;
int popup_display_; // -1 for default
QString popup_screen_name_;
QPoint popup_pos_;
QScreen* popup_screen_;
QFont font_;
// The OSD is kept always on top until you click (no timer)
bool disable_duration_;
@ -138,6 +143,9 @@ class OSDPretty : public QWidget {
// Toggling requested, we have to show or hide the OSD
bool toggle_mode_;
QMap<QString, QScreen*> screens_;
};
#endif // OSDPRETTY_H

View File

@ -18,8 +18,9 @@
#include "prettyimage.h"
#include <QApplication>
#include <QScreen>
#include <QWindow>
#include <QContextMenuEvent>
#include <QDesktopWidget>
#include <QDir>
#include <QFileDialog>
#include <QFuture>
@ -189,25 +190,33 @@ void PrettyImage::contextMenuEvent(QContextMenuEvent* e) {
}
void PrettyImage::ShowFullsize() {
// Work out how large to make the window, based on the size of the screen
QRect desktop_rect(QApplication::desktop()->availableGeometry(this));
QSize window_size(qMin(desktop_rect.width() - 20, image_.width()),
qMin(desktop_rect.height() - 20, image_.height()));
// Create the window
QScrollArea* window = new QScrollArea;
window->setAttribute(Qt::WA_DeleteOnClose, true);
window->setWindowTitle(tr("Clementine image viewer"));
window->resize(window_size);
QScrollArea* pwindow = new QScrollArea;
pwindow->setAttribute(Qt::WA_DeleteOnClose, true);
pwindow->setWindowTitle(tr("Clementine image viewer"));
// Work out how large to make the window, based on the size of the screen
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QScreen *screen = screen();
#else
QScreen *screen = (window() && window()->windowHandle() ? window()->windowHandle()->screen() : QGuiApplication::primaryScreen());
#endif
if (screen) {
QRect desktop_rect(screen->availableGeometry());
QSize window_size(qMin(desktop_rect.width() - 20, image_.width()),
qMin(desktop_rect.height() - 20, image_.height()));
pwindow->resize(window_size);
}
// Create the label that displays the image
QLabel* label = new QLabel(window);
QLabel* label = new QLabel(pwindow);
label->setPixmap(QPixmap::fromImage(image_));
// Show the label in the window
window->setWidget(label);
window->setFrameShape(QFrame::NoFrame);
window->show();
pwindow->setWidget(label);
pwindow->setFrameShape(QFrame::NoFrame);
pwindow->show();
}
void PrettyImage::SaveAs() {