From c3adc90f7ef67a11036ae43e96d0feca880d12de Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Tue, 21 Nov 2023 22:13:22 +0300 Subject: [PATCH] Show Add new torrent dialog on main window screen PR #19963. Closes #19774. --- src/gui/guiaddtorrentmanager.cpp | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/gui/guiaddtorrentmanager.cpp b/src/gui/guiaddtorrentmanager.cpp index 57ecd713b..19cc5ce1b 100644 --- a/src/gui/guiaddtorrentmanager.cpp +++ b/src/gui/guiaddtorrentmanager.cpp @@ -29,6 +29,8 @@ #include "guiaddtorrentmanager.h" +#include + #include "base/bittorrent/session.h" #include "base/bittorrent/torrentdescriptor.h" #include "base/logger.h" @@ -40,6 +42,39 @@ #include "mainwindow.h" #include "raisedmessagebox.h" +namespace +{ + void adjustDialogGeometry(QWidget *dialog, const QWidget *parentWindow) + { + // It is preferable to place the dialog in the center of the parent window. + // However, if it goes beyond the current screen, then move it so that it fits there + // (or, if the dialog is larger than the current screen, at least make sure that + // the upper/left coordinates of the dialog are inside it). + + QRect dialogGeometry = dialog->geometry(); + + dialogGeometry.moveCenter(parentWindow->geometry().center()); + + const QRect screenGeometry = parentWindow->screen()->availableGeometry(); + + QPoint delta = screenGeometry.bottomRight() - dialogGeometry.bottomRight(); + if (delta.x() > 0) + delta.setX(0); + if (delta.y() > 0) + delta.setY(0); + dialogGeometry.translate(delta); + + delta = screenGeometry.topLeft() - dialogGeometry.topLeft(); + if (delta.x() < 0) + delta.setX(0); + if (delta.y() < 0) + delta.setY(0); + dialogGeometry.translate(delta); + + dialog->setGeometry(dialogGeometry); + } +} + GUIAddTorrentManager::GUIAddTorrentManager(IGUIApplication *app, BitTorrent::Session *session, QObject *parent) : GUIApplicationComponent(app, session, parent) { @@ -200,6 +235,8 @@ bool GUIAddTorrentManager::processTorrent(const QString &source, const BitTorren m_dialogs.remove(infoHash); }); + + adjustDialogGeometry(dlg, app()->mainWindow()); dlg->show(); return true;