Show Add new torrent dialog on main window screen

PR #19963.
Closes #19774.
This commit is contained in:
Vladimir Golovnev 2023-11-21 22:13:22 +03:00 committed by Vladimir Golovnev (Glassez)
parent 081eace057
commit c1b63372f1
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7

View file

@ -39,6 +39,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMenu> #include <QMenu>
#include <QPushButton> #include <QPushButton>
#include <QScreen>
#include <QShortcut> #include <QShortcut>
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
@ -135,6 +136,36 @@ namespace
settings()->storeValue(settingsKey, QStringList(pathList.mid(0, maxLength))); 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 class AddNewTorrentDialog::TorrentContentAdaptor final
@ -539,10 +570,15 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
: dlg->loadTorrentFile(source); : dlg->loadTorrentFile(source);
if (isLoaded) if (isLoaded)
{
adjustDialogGeometry(dlg, parent);
dlg->QDialog::show(); dlg->QDialog::show();
}
else else
{
delete dlg; delete dlg;
} }
}
void AddNewTorrentDialog::show(const QString &source, QWidget *parent) void AddNewTorrentDialog::show(const QString &source, QWidget *parent)
{ {