Skip to content

Commit

Permalink
Specify parent window for popups. (#3406)
Browse files Browse the repository at this point in the history
* Specify parrent window for popups.

Message boxes without parent are almost always a mistake. Such message boxes
* Aren't properly position on top of current window, instead they get placed in the corner of screen possibly away from main window.
* They get treated as separate window by window manager, but they still prevent proper interaction with main window. If you alt tab the message box can get lost, resulting in confusing behavior where you don't know why you can't interact with main window.
  • Loading branch information
karliss authored Jan 22, 2025
1 parent d5088a5 commit 3652cb7
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 24 deletions.
10 changes: 8 additions & 2 deletions src/common/IOModesController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <QObject>
#include <QMessageBox>
#include <QJsonObject>
#include <qwidget.h>

IOModesController::IOModesController(QWidget *parentWindow)
: QObject(parentWindow), parentWindow(parentWindow)
{
}

bool IOModesController::canWrite()
{
Expand Down Expand Up @@ -48,7 +54,7 @@ bool IOModesController::prepareForWriting()
return true;
}

QMessageBox msgBox;
QMessageBox msgBox(parentWindow);
msgBox.setIcon(QMessageBox::Icon::Critical);
msgBox.setWindowTitle(QObject::tr("Write error"));
msgBox.setText(QObject::tr(
Expand Down Expand Up @@ -91,7 +97,7 @@ bool IOModesController::askCommitUnsavedChanges()
// Check if there are uncommitted changes
if (!allChangesComitted()) {
QMessageBox::StandardButton ret = QMessageBox::question(
NULL, QObject::tr("Uncommitted changes"),
parentWindow, QObject::tr("Uncommitted changes"),
QObject::tr("It seems that you have changes or patches that are not committed to "
"the file.\n"
"Do you want to commit them now?"),
Expand Down
5 changes: 5 additions & 0 deletions src/common/IOModesController.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
#define IOMODESCONTROLLER_H

#include "core/Cutter.h"
#include <qwidget.h>

class IOModesController : public QObject

{
Q_OBJECT
public:
IOModesController(QWidget *parent);
enum class Mode { READ_ONLY, CACHE, WRITE };
bool prepareForWriting();
bool canWrite();
Expand All @@ -17,6 +19,9 @@ class IOModesController : public QObject

public slots:
bool askCommitUnsavedChanges();

private:
QWidget *parentWindow;
};

#endif // IOMODESCONTROLLER_H
5 changes: 3 additions & 2 deletions src/core/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ T *getNewInstance(MainWindow *m)

using namespace Cutter;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), core(Core()), ui(new Ui::MainWindow)
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), core(Core()), ui(new Ui::MainWindow), ioModesController(this)
{
tabsOnTop = false;
configuration = Config();
Expand Down Expand Up @@ -538,7 +539,7 @@ void MainWindow::openNewFile(InitialOptions &options, bool skipOptionsDialog)
if (options.script.isEmpty()) {
QString script = QString("%1.rz").arg(this->filename);
if (rz_file_exists(script.toStdString().data())) {
QMessageBox mb;
QMessageBox mb(this);
mb.setWindowTitle(tr("Script loading"));
mb.setText(tr("Do you want to load the '%1' script?").arg(script));
mb.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
Expand Down
10 changes: 6 additions & 4 deletions src/dialogs/AboutDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ void AboutDialog::on_checkForUpdatesButton_clicked()
#if CUTTER_UPDATE_WORKER_AVAILABLE
UpdateWorker updateWorker;

QProgressDialog waitDialog;
auto parentWindow = this;

QProgressDialog waitDialog(parentWindow);
QProgressBar *bar = new QProgressBar(&waitDialog);
bar->setMaximum(0);

Expand All @@ -104,12 +106,12 @@ void AboutDialog::on_checkForUpdatesButton_clicked()

connect(&updateWorker, &UpdateWorker::checkComplete, &waitDialog, &QProgressDialog::cancel);
connect(&updateWorker, &UpdateWorker::checkComplete,
[&updateWorker](const QVersionNumber &version, const QString &error) {
[&updateWorker, parentWindow](const QVersionNumber &version, const QString &error) {
if (!error.isEmpty()) {
QMessageBox::critical(nullptr, tr("Error!"), error);
QMessageBox::critical(parentWindow, tr("Error!"), error);
} else {
if (version <= UpdateWorker::currentVersionNumber()) {
QMessageBox::information(nullptr, tr("Version control"),
QMessageBox::information(parentWindow, tr("Version control"),
tr("Cutter is up to date!"));
} else {
updateWorker.showUpdateDialog(false);
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/GlibcHeapInfoDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void GlibcHeapInfoDialog::updateFields()

void GlibcHeapInfoDialog::saveChunkInfo()
{
QMessageBox msgBox;
QMessageBox msgBox(this);
msgBox.setText("Do you want to overwrite chunk metadata?");
msgBox.setInformativeText(
"Any field which cannot be converted to a valid integer will be saved as zero");
Expand Down
8 changes: 4 additions & 4 deletions src/dialogs/RemoteDebugDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ bool RemoteDebugDialog::validate()
} else if (debugger == WINDBG) {
return validatePath();
}
QMessageBox msgBox;
QMessageBox msgBox(this);
msgBox.setText(tr("Invalid debugger"));
msgBox.exec();
return false;
}

bool RemoteDebugDialog::validateIp()
{
QMessageBox msgBox;
QMessageBox msgBox(this);

QString ip = getIpOrPath();
if (QHostAddress(ip).isNull()) {
Expand All @@ -74,7 +74,7 @@ bool RemoteDebugDialog::validateIp()

bool RemoteDebugDialog::validatePath()
{
QMessageBox msgBox;
QMessageBox msgBox(this);

QString path = getIpOrPath();
if (!QFileInfo(path).exists()) {
Expand All @@ -87,7 +87,7 @@ bool RemoteDebugDialog::validatePath()

bool RemoteDebugDialog::validatePort()
{
QMessageBox msgBox;
QMessageBox msgBox(this);

int port = getPort();
if (port < 1 || port > 65535) {
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs/WelcomeDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void WelcomeDialog::onLanguageComboBox_currentIndexChanged(int index)
QString language = ui->languageComboBox->itemText(index);
Config()->setLocaleByName(language);

QMessageBox mb;
QMessageBox mb(this);
mb.setWindowTitle(tr("Language settings"));
mb.setText(tr("Language will be changed after next application start."));
mb.setIcon(QMessageBox::Information);
Expand Down
6 changes: 3 additions & 3 deletions src/dialogs/preferences/AppearanceOptionsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ void AppearanceOptionsWidget::on_deleteButton_clicked()
{
QString currTheme = ui->colorComboBox->currentText();
if (!ThemeWorker().isCustomTheme(currTheme)) {
QMessageBox::critical(nullptr, tr("Error"), ThemeWorker().deleteTheme(currTheme));
QMessageBox::critical(this, tr("Error"), ThemeWorker().deleteTheme(currTheme));
return;
}
int ret = QMessageBox::question(
nullptr, tr("Delete"), tr("Are you sure you want to delete <b>%1</b>?").arg(currTheme));
this, tr("Delete"), tr("Are you sure you want to delete <b>%1</b>?").arg(currTheme));
if (ret == QMessageBox::Yes) {
QString err = ThemeWorker().deleteTheme(currTheme);
updateThemeFromConfig(false);
if (!err.isEmpty()) {
QMessageBox::critical(nullptr, tr("Error"), err);
QMessageBox::critical(this, tr("Error"), err);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/menus/DisassemblyContextMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ DisassemblyContextMenu::DisassemblyContextMenu(QWidget *parent, MainWindow *main
offset(0),
canCopy(false),
mainWindow(mainWindow),
ioModesController(mainWindow),
actionEditInstruction(this),
actionNopInstruction(this),
actionJmpReverse(this),
Expand Down
12 changes: 6 additions & 6 deletions src/widgets/DebugActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ DebugActions::DebugActions(QToolBar *toolBar, MainWindow *main) : QObject(main),
reverseActions = { actionStepBack, actionContinueBack };

connect(Core(), &CutterCore::debugProcessFinished, this, [=](int pid) {
QMessageBox msgBox;
QMessageBox msgBox(main);
msgBox.setText(tr("Debugged process exited (") + QString::number(pid) + ")");
msgBox.exec();
});
Expand Down Expand Up @@ -262,7 +262,7 @@ void DebugActions::showDebugWarning()
{
if (!acceptedDebugWarning) {
acceptedDebugWarning = true;
QMessageBox msgBox;
QMessageBox msgBox(main);
msgBox.setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
msgBox.setText(tr("Debug is currently in beta.\n")
+ tr("If you encounter any problems or have suggestions, please submit an "
Expand Down Expand Up @@ -302,7 +302,7 @@ void DebugActions::onAttachedRemoteDebugger(bool successfully)
return;

if (!successfully) {
QMessageBox msgBox;
QMessageBox msgBox(main);
msgBox.setText(tr("Error connecting."));
msgBox.exec();
attachRemoteDialog();
Expand All @@ -326,7 +326,7 @@ void DebugActions::attachRemoteDialog()
if (!remoteDialog) {
remoteDialog = new RemoteDebugDialog(main);
}
QMessageBox msgBox;
QMessageBox msgBox(main);
bool success = false;
while (!success) {
success = true;
Expand Down Expand Up @@ -355,7 +355,7 @@ void DebugActions::attachProcessDialog()
attachProcess(pid);
} else {
success = false;
QMessageBox msgBox;
QMessageBox msgBox(main);
msgBox.setText(tr("Error attaching. No process selected!"));
msgBox.exec();
}
Expand Down Expand Up @@ -384,7 +384,7 @@ void DebugActions::startDebug()

QFileInfo info(filename);
if (!Core()->currentlyDebugging && !info.isExecutable()) {
QMessageBox msgBox;
QMessageBox msgBox(main);
msgBox.setText(tr("File '%1' does not have executable permissions.").arg(filename));
msgBox.exec();
return;
Expand Down
1 change: 1 addition & 0 deletions src/widgets/HexWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ HexWidget::HexWidget(QWidget *parent)
showAscii(true),
showExHex(true),
showExAddr(true),
ioModesController(parent),
warningTimer(this)
{
setMouseTracking(true);
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/ProcessesWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void ProcessesWidget::onActivated(const QModelIndex &index)
// attach to any given id. If it isn't found simply update the UI.
for (const auto &value : Core()->getAllProcesses()) {
if (pid == value.pid) {
QMessageBox msgBox;
QMessageBox msgBox(this);
switch (value.status) {
case RZ_DBG_PROC_ZOMBIE:
case RZ_DBG_PROC_DEAD:
Expand Down

0 comments on commit 3652cb7

Please sign in to comment.