From 790363c5422f7794be1be2d7f016af8de96d5c1b Mon Sep 17 00:00:00 2001
From: Walter Van Herck <w.van.herck@fz-juelich.de>
Date: Mon, 19 Mar 2018 17:06:49 +0100
Subject: [PATCH] Clear focus of current editor before running simulation with
 ctrl-R shortcut

---
 GUI/coregui/Models/SessionModelDelegate.cpp       |  8 +-------
 .../Views/PropertyEditor/ComponentFlatView.cpp    | 15 ---------------
 .../PropertyEditor/PropertyEditorFactory.cpp      |  6 ------
 .../Views/PropertyEditor/PropertyWidgetItem.cpp   |  9 +--------
 GUI/coregui/mainwindow/mainwindow.cpp             |  7 ++++---
 5 files changed, 6 insertions(+), 39 deletions(-)

diff --git a/GUI/coregui/Models/SessionModelDelegate.cpp b/GUI/coregui/Models/SessionModelDelegate.cpp
index 7ee4f660762..45e1b83ca22 100644
--- a/GUI/coregui/Models/SessionModelDelegate.cpp
+++ b/GUI/coregui/Models/SessionModelDelegate.cpp
@@ -39,7 +39,6 @@ QString doubleToString(const SessionItem& item)
         auto locale = QLocale::system();
         result = locale.toString(item.value().toDouble(), 'f', item.decimals());
     }
-
     return result;
 }
 
@@ -50,8 +49,7 @@ QWidget* createEditorFromIndex(const QModelIndex& index, QWidget* parent) {
     }
     return nullptr;
 }
-
-}
+}  // unnamed namespace
 
 SessionModelDelegate::SessionModelDelegate(QObject* parent)
     : QStyledItemDelegate(parent)
@@ -64,11 +62,9 @@ void SessionModelDelegate::paint(QPainter* painter, const QStyleOptionViewItem&
     if (PropertyEditorFactory::IsCustomVariant(index.data())) {
         QString text = PropertyEditorFactory::ToString(index.data());
         paintCustomLabel(painter, option, index, text);
-
     } else if (isDoubleProperty(index)) {
         auto item = static_cast<SessionItem*>(index.internalPointer());
         paintCustomLabel(painter, option, index, doubleToString(*item));
-
     } else {
         QStyledItemDelegate::paint(painter, option, index);
     }
@@ -88,11 +84,9 @@ QWidget* SessionModelDelegate::createEditor(QWidget* parent, const QStyleOptionV
             // Int and Double will be handled by standard spin boxes
             // QStyledItemDelegate already knows how to handle it, no special connections are required
         }
-
     } else {
         result = QStyledItemDelegate::createEditor(parent, option, index);
     }
-
     return result;
 }
 
diff --git a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
index bdc617f89d6..4075fc461c7 100644
--- a/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
+++ b/GUI/coregui/Views/PropertyEditor/ComponentFlatView.cpp
@@ -60,7 +60,6 @@ void ComponentFlatView::addItem(SessionItem* item)
         setItem(item);
         return;
     }
-
     m_topItems.push_back(item);
     updateItemProperties();
 }
@@ -69,34 +68,26 @@ void ComponentFlatView::setModel(SessionModel* model)
 {
     if (m_model) {
         disconnect(m_model, &SessionModel::dataChanged, this, &ComponentFlatView::onDataChanged);
-
     }
-
     m_model = model;
-
     if (m_model) {
         connect(m_model, &SessionModel::dataChanged, this, &ComponentFlatView::onDataChanged);
     }
-
 }
 
 void ComponentFlatView::clearLayout()
 {
     Q_ASSERT(m_gridLayout);
-
     LayoutUtils::clearGridLayout(m_gridLayout, false);
 
     for(auto widget: m_widgetItems)
         widget->deleteLater();
-
     m_widgetItems.clear();
-
 }
 
 void ComponentFlatView::setShowChildren(bool show)
 {
     m_show_children = show;
-
 }
 
 void ComponentFlatView::onDataChanged(const QModelIndex& topLeft, const QModelIndex&bottomRight,
@@ -120,28 +111,22 @@ void ComponentFlatView::clearEditor()
 
 void ComponentFlatView::updateItemProperties()
 {
-
     clearLayout();
 
     QList<const SessionItem*> allitems;
-
     for (auto item : m_topItems)
         allitems += ComponentUtils::componentItems(*item);
 
     int nrow(0);
     for (auto child : allitems) {
-
         auto widget = createWidget(child);
         if (!widget)
             continue;
-
         widget->addToGrid(m_gridLayout, ++nrow);
         m_widgetItems.push_back(widget);
-
         if (!m_show_children)
             break;
     }
-
 }
 
 void ComponentFlatView::updateItemRoles(SessionItem* item)
diff --git a/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp b/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
index 3d8bcfa02e3..4cc2d5a97dc 100644
--- a/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
+++ b/GUI/coregui/Views/PropertyEditor/PropertyEditorFactory.cpp
@@ -111,21 +111,17 @@ QWidget* PropertyEditorFactory::CreateEditor(const SessionItem& item, QWidget* p
             result = createCustomDoubleEditor(item);
         }
     }
-
     else if(isIntProperty(item.value())) {
         result = createCustomIntEditor(item);
     }
-
     else if(isBoolProperty(item.value())) {
         auto editor = new BoolEditor;
         editor->setData(item.value());
         result = editor;
     }
-
     else if(isStringProperty(item.value())) {
         result = createCustomStringEditor(item);
     }
-
     else if(isExternalProperty(item.value())) {
         auto editor = new ExternalPropertyEditor;
         editor->setData(item.value());
@@ -133,13 +129,11 @@ QWidget* PropertyEditorFactory::CreateEditor(const SessionItem& item, QWidget* p
             editor->setExternalDialogType(item.editorType());
         result = editor;
     }
-
     else if(isComboProperty(item.value())) {
         auto editor = new ComboPropertyEditor;
         editor->setData(item.value());
         result = editor;
     }
-
     if (parent && result)
         result->setParent(parent);
 
diff --git a/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp b/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
index dde815be23c..b73ad8bd1c7 100644
--- a/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
+++ b/GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
@@ -98,16 +98,9 @@ void PropertyWidgetItem::connectEditor(QWidget* editor)
     if (auto customEditor = dynamic_cast<ComboPropertyEditor*>(editor)) {
         connect(customEditor, &ComboPropertyEditor::dataChanged,
                 [=] { m_delegate->commitData(customEditor); });
-
     } else if (auto customEditor = dynamic_cast<QSpinBox*>(editor)) {
-        connect(customEditor, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
-                [=] { m_delegate->commitData(customEditor); });
-
-    } else if (auto customEditor = dynamic_cast<QDoubleSpinBox*>(editor)) {
-        connect(customEditor,
-                static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
+        connect(customEditor, qOverload<int>(&QSpinBox::valueChanged),
                 [=] { m_delegate->commitData(customEditor); });
-
     } else if (auto customEditor = dynamic_cast<ScientificDoublePropertyEditor*>(editor)) {
         connect(customEditor, &ScientificDoublePropertyEditor::dataChanged,
                 [=] { m_delegate->commitData(customEditor); });
diff --git a/GUI/coregui/mainwindow/mainwindow.cpp b/GUI/coregui/mainwindow/mainwindow.cpp
index 2e4345b8d97..fdcdbf642c3 100644
--- a/GUI/coregui/mainwindow/mainwindow.cpp
+++ b/GUI/coregui/mainwindow/mainwindow.cpp
@@ -122,6 +122,10 @@ void MainWindow::openRecentProject()
 
 void MainWindow::onRunSimulationShortcut()
 {
+    // This clearFocus is needed for the propagation of the current editor value,
+    // since the runSimulation method will only change focus after finishing the simulation
+    if (auto widget = QApplication::focusWidget())
+        widget->clearFocus();
     m_simulationView->onRunSimulationShortcut();
 }
 
@@ -132,11 +136,9 @@ void MainWindow::onSessionModelViewActive(bool isActive)
     if (isActive) {
         if (m_sessionModelView)
             return;
-
         m_sessionModelView = new SessionModelView(this);
         m_tabWidget->insertTab(MAXVIEWCOUNT, m_sessionModelView,
                                QIcon(":/images/main_sessionmodel.svg"), "Models");
-
     } else {
         if (!m_sessionModelView)
             return;
@@ -160,7 +162,6 @@ void MainWindow::closeEvent(QCloseEvent* event)
         event->ignore();
         return;
     }
-
     if (m_projectManager->closeCurrentProject()) {
         writeSettings();
         event->accept();
-- 
GitLab