From f4307ee1378aaa3839a6ce2a4697bd19107d74bb Mon Sep 17 00:00:00 2001
From: tobiglaser <76131623+tobiglaser@users.noreply.github.com>
Date: Mon, 8 Aug 2022 16:53:56 +0200
Subject: [PATCH] moved decision about selection from model to panel

---
 src/PanelCommandTable.cpp | 10 ++++++++--
 src/TableCommandModel.cpp | 11 +----------
 src/TableCommandModel.h   |  3 ++-
 3 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/src/PanelCommandTable.cpp b/src/PanelCommandTable.cpp
index 441cd44..7b84dbc 100644
--- a/src/PanelCommandTable.cpp
+++ b/src/PanelCommandTable.cpp
@@ -1,7 +1,6 @@
 #include "PanelCommandTable.h"
 #include <QBoxLayout>
 #include <QHeaderView>
-#include <iostream>
 
 PanelCommandTable::PanelCommandTable(ControlModel *_cM)
 {
@@ -109,7 +108,14 @@ void PanelCommandTable::onStopButton()
 
 void PanelCommandTable::updateTable(std::shared_ptr<ICommand> _icom)
 {
-    QModelIndex index = tM->onChange(_icom);
+    tM->onChange(_icom);
+
+    int row = cM->getCommandList().getPos(_icom) - 1;
+    QModelIndex index;
+    if (_icom)
+        index = tM->index(row, 0);
+    else
+        index = tM->index(- 1, 0);
     lSM->setCurrentIndex(index, QItemSelectionModel::SelectionFlag::ClearAndSelect | QItemSelectionModel::SelectionFlag::Rows);
 }
 
diff --git a/src/TableCommandModel.cpp b/src/TableCommandModel.cpp
index 890b0bd..7b97977 100644
--- a/src/TableCommandModel.cpp
+++ b/src/TableCommandModel.cpp
@@ -74,9 +74,8 @@ QVariant TableCommandModel::headerData(int section, Qt::Orientation orientation,
     return QVariant();
 }
 
-QModelIndex TableCommandModel::onChange(std::shared_ptr<ICommand> _icom)
+void TableCommandModel::onChange(std::shared_ptr<ICommand> _icom)
 {
-    QModelIndex ret;
     if (_icom != nullptr)
     {
         //update only one command (and its neighbours)
@@ -89,19 +88,11 @@ QModelIndex TableCommandModel::onChange(std::shared_ptr<ICommand> _icom)
                 emit(dataChanged(index, index));
             }
         }
-        ret  = createIndex(row, 0, nullptr);
     }
-    else
-    {
-        ret = createIndex(- 1, 0, nullptr);
-    }
-    static int rowsBefore = 0;
     int rowsNow = rowCount();
     if (rowsNow != rowsBefore)
     {
-        emit(layoutAboutToBeChanged());
         emit(layoutChanged());
         rowsBefore = rowsNow;
     }
-    return ret;
 }
diff --git a/src/TableCommandModel.h b/src/TableCommandModel.h
index 7dbb8b5..3b415bb 100644
--- a/src/TableCommandModel.h
+++ b/src/TableCommandModel.h
@@ -8,6 +8,7 @@ class TableCommandModel : public QAbstractTableModel
 private:
     std::array<QString, 3> headerNames = {"No.", "Command", "Configuration"};
     CommandList *cL;
+    int rowsBefore = 0;
 
 public:
     TableCommandModel(CommandList *_list) { cL = _list; }
@@ -20,5 +21,5 @@ public:
     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) { return createIndex(row, column, nullptr); }
     QModelIndex parent(const QModelIndex &index) { return QModelIndex(); }
 
-    QModelIndex onChange(std::shared_ptr<ICommand> _icom);
+    void onChange(std::shared_ptr<ICommand> _icom);
 };
-- 
GitLab