Approach to handle the restoreGeometry/State crash

* use CLogSubscriber to "catch" the issue (log handler "catching" the ASSERT)
* restart message box
This commit is contained in:
Klaus Basan
2019-04-30 00:22:59 +02:00
parent ce534d2536
commit 9a3ef1e82e
2 changed files with 43 additions and 2 deletions

View File

@@ -27,6 +27,7 @@
#include "blackmisc/logcategory.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/logmessage.h"
#include "blackmisc/loghandler.h"
#include "blackmisc/metadatautils.h"
#include "blackmisc/registermetadata.h"
#include "blackmisc/settingscache.h"
@@ -62,6 +63,7 @@
#include <QWidget>
#include <QWindow>
#include <QMainWindow>
#include <QMessageBox>
#include <QtGlobal>
#include <QWhatsThis>
@@ -374,6 +376,14 @@ namespace BlackGui
return true;
}
void CGuiApplication::resetWindowGeometryAndState()
{
QByteArray ba;
QSettings settings("swift-project.org", this->getApplicationName());
settings.setValue("geometry", ba);
settings.setValue("windowState", ba);
}
bool CGuiApplication::restoreWindowGeometryAndState(QMainWindow *window)
{
if (!window) { return false; }
@@ -384,8 +394,36 @@ namespace BlackGui
const QByteArray g = settings.value("geometry").toByteArray();
const QByteArray s = settings.value("windowState").toByteArray();
if (g.isEmpty() || s.isEmpty()) { return false; }
window->restoreGeometry(g);
window->restoreState(s);
// block for subscriber
{
const auto pattern = CLogPattern().withSeverity(CStatusMessage::SeverityError);
const QString parameter = m_cmdWindowSizeReset.names().first();
CLogSubscriber logSub(this, [&](const CStatusMessage & message)
{
// handles an error in restoreGeometry/State
const int ret = QMessageBox::critical(sGui->mainApplicationWidget(), sGui->getApplicationNameAndVersion(),
QStringLiteral(
"Restoring the window state/geometry failed!\n"
"You need to reset the window size (command -%1).\n\n"
"Original msg: %2\n\n"
"We can try to reset the values and restart\n"
"Do you want to try?"
).arg(parameter, message.getMessage()),
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{
this->resetWindowGeometryAndState();
this->restartApplication();
}
// most likely crashing if we do nothing
});
logSub.changeSubscription(pattern);
window->restoreGeometry(g);
window->restoreState(s);
}
return true;
}

View File

@@ -227,6 +227,9 @@ namespace BlackGui
//! Save widget's geometry and state
bool saveWindowGeometryAndState(const QMainWindow *window = CGuiApplication::mainApplicationWindow()) const;
//! Reset the saved values
void resetWindowGeometryAndState();
//! Restore widget's geometry and state
bool restoreWindowGeometryAndState(QMainWindow *window = CGuiApplication::mainApplicationWindow());