From c1b63372f16cff509004b95ac2dac3df230db6c3 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/addnewtorrentdialog.cpp | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 147b90f04..e9331a654 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -135,6 +136,36 @@ namespace settings()->storeValue(settingsKey, QStringList(pathList.mid(0, maxLength))); } + + 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); + } } class AddNewTorrentDialog::TorrentContentAdaptor final @@ -539,9 +570,14 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre : dlg->loadTorrentFile(source); if (isLoaded) + { + adjustDialogGeometry(dlg, parent); dlg->QDialog::show(); + } else + { delete dlg; + } } void AddNewTorrentDialog::show(const QString &source, QWidget *parent)