Use queued connection to thread-safe MainWindow error handling

Up to now, errors produced by threads were not directly shown in
the MainWindow. Code running in the GUI thread had to manually
show the errors.

This can be simplified by using Qt's queued connection as message
passing facility.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2018-01-28 22:08:30 +01:00 committed by Jan Mulder
parent 1704e08012
commit a25f54e3c2
5 changed files with 11 additions and 20 deletions

View file

@ -222,9 +222,6 @@ void DownloadFromDCWidget::updateState(states state)
else if (state == ERROR) { else if (state == ERROR) {
timer->stop(); timer->stop();
// Show messages that worker thread produced.
MainWindow::instance()->showErrors();
QMessageBox::critical(this, TITLE_OR_TEXT(tr("Error"), thread.error), QMessageBox::Ok); QMessageBox::critical(this, TITLE_OR_TEXT(tr("Error"), thread.error), QMessageBox::Ok);
markChildrenAsEnabled(); markChildrenAsEnabled();
progress_bar_text = ""; progress_bar_text = "";

View file

@ -91,21 +91,9 @@ MainWindow *MainWindow::m_Instance = NULL;
extern "C" void showErrorFromC() extern "C" void showErrorFromC()
{ {
// Show errors only if we are running in the GUI thread.
// If we're not in the GUI thread, let errors accumulate.
if (QThread::currentThread() != QCoreApplication::instance()->thread())
return;
MainWindow *mainwindow = MainWindow::instance(); MainWindow *mainwindow = MainWindow::instance();
if (mainwindow) if (mainwindow)
mainwindow->showErrors(); emit mainwindow->showError(get_error_string());
}
void MainWindow::showErrors()
{
const char *error = get_error_string();
if (!empty_string(error))
getNotificationWidget()->showNotification(error, KMessageWidget::Error);
} }
MainWindow::MainWindow() : QMainWindow(), MainWindow::MainWindow() : QMainWindow(),
@ -219,6 +207,7 @@ MainWindow::MainWindow() : QMainWindow(),
connect(plannerDetails->printPlan(), SIGNAL(pressed()), divePlannerWidget(), SLOT(printDecoPlan())); connect(plannerDetails->printPlan(), SIGNAL(pressed()), divePlannerWidget(), SLOT(printDecoPlan()));
connect(this, SIGNAL(startDiveSiteEdit()), this, SLOT(on_actionDiveSiteEdit_triggered())); connect(this, SIGNAL(startDiveSiteEdit()), this, SLOT(on_actionDiveSiteEdit_triggered()));
connect(information(), SIGNAL(diveSiteChanged(struct dive_site *)), mapWidget, SLOT(centerOnDiveSite(struct dive_site *))); connect(information(), SIGNAL(diveSiteChanged(struct dive_site *)), mapWidget, SLOT(centerOnDiveSite(struct dive_site *)));
connect(this, &MainWindow::showError, ui.mainErrorMessage, &NotificationWidget::showError, Qt::AutoConnection);
wtu = new WindowTitleUpdate(); wtu = new WindowTitleUpdate();
connect(WindowTitleUpdate::instance(), SIGNAL(updateTitle()), this, SLOT(setAutomaticTitle())); connect(WindowTitleUpdate::instance(), SIGNAL(updateTitle()), this, SLOT(setAutomaticTitle()));

View file

@ -88,10 +88,6 @@ public:
void enableDisableCloudActions(); void enableDisableCloudActions();
void setCheckedActionFilterTags(bool checked); void setCheckedActionFilterTags(bool checked);
// Shows errors that have accumulated.
// Must be called from GUI thread.
void showErrors();
private private
slots: slots:
/* file menu action */ /* file menu action */
@ -159,6 +155,7 @@ protected:
signals: signals:
void startDiveSiteEdit(); void startDiveSiteEdit();
void showError(QString message);
public public
slots: slots:

View file

@ -16,6 +16,11 @@ void NotificationWidget::showNotification(QString message, KMessageWidget::Messa
animatedShow(); animatedShow();
} }
void NotificationWidget::showError(QString message)
{
showNotification(message, KMessageWidget::Error);
}
void NotificationWidget::hideNotification() void NotificationWidget::hideNotification()
{ {
animatedHide(); animatedHide();

View file

@ -21,6 +21,9 @@ public:
void hideNotification(); void hideNotification();
QString getNotificationText(); QString getNotificationText();
public
slots:
void showError(QString message);
private: private:
QFutureWatcher<void> future_watcher; QFutureWatcher<void> future_watcher;