From 110306f6b07ef8a9336b274d3d69c9c0c66bea0f Mon Sep 17 00:00:00 2001
From: Stanislav Bas <stanislav.m.bas@gmail.com>
Date: Sat, 4 Jul 2015 18:49:32 +0300
Subject: [PATCH] Create a separate class for Edit 'ID' action to use in
 tables' context menus

---
 apps/opencs/CMakeLists.txt                   |  2 +-
 apps/opencs/view/world/tableeditidaction.cpp | 42 ++++++++++++++++++++
 apps/opencs/view/world/tableeditidaction.hpp | 31 +++++++++++++++
 3 files changed, 74 insertions(+), 1 deletion(-)
 create mode 100644 apps/opencs/view/world/tableeditidaction.cpp
 create mode 100644 apps/opencs/view/world/tableeditidaction.hpp

diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt
index 02e26c0100..ee1d5253d4 100644
--- a/apps/opencs/CMakeLists.txt
+++ b/apps/opencs/CMakeLists.txt
@@ -64,7 +64,7 @@ opencs_units (view/world
     table tablesubview scriptsubview util regionmapsubview tablebottombox creator genericcreator
     cellcreator referenceablecreator referencecreator scenesubview
     infocreator scriptedit dialoguesubview previewsubview regionmap dragrecordtable nestedtable
-    dialoguespinbox recordbuttonbar
+    dialoguespinbox recordbuttonbar tableeditidaction
     )
 
 opencs_units_noqt (view/world
diff --git a/apps/opencs/view/world/tableeditidaction.cpp b/apps/opencs/view/world/tableeditidaction.cpp
new file mode 100644
index 0000000000..7ce726e412
--- /dev/null
+++ b/apps/opencs/view/world/tableeditidaction.cpp
@@ -0,0 +1,42 @@
+#include "tableeditidaction.hpp"
+
+#include <QTableView>
+
+#include "../../model/world/tablemimedata.hpp"
+
+CSVWorld::TableEditIdAction::CellData CSVWorld::TableEditIdAction::getCellData(int row, int column) const
+{
+    QModelIndex index = mTable.model()->index(row, column);
+    if (index.isValid())
+    {
+        QVariant display = mTable.model()->data(index, CSMWorld::ColumnBase::Role_Display);
+        QString value = mTable.model()->data(index).toString();
+        return std::make_pair(static_cast<CSMWorld::ColumnBase::Display>(display.toInt()), value);
+    }
+    return std::make_pair(CSMWorld::ColumnBase::Display_None, "");
+}
+
+CSVWorld::TableEditIdAction::TableEditIdAction(const QTableView &table, QWidget *parent)
+    : QAction(parent),
+      mTable(table),
+      mCurrentId(CSMWorld::UniversalId::Type_None)
+{}
+
+void CSVWorld::TableEditIdAction::setCell(int row, int column)
+{
+    CellData data = getCellData(row, column);
+    mCurrentId = CSMWorld::UniversalId(CSMWorld::TableMimeData::convertEnums(data.first), 
+                                       data.second.toUtf8().constData());
+    setText("Edit '" + data.second + "'");
+}
+
+CSMWorld::UniversalId CSVWorld::TableEditIdAction::getCurrentId() const
+{
+    return mCurrentId;
+}
+
+bool CSVWorld::TableEditIdAction::isValidIdCell(int row, int column) const
+{
+    CellData data = getCellData(row, column);
+    return CSMWorld::ColumnBase::isId(data.first) && !data.second.isEmpty();
+}
diff --git a/apps/opencs/view/world/tableeditidaction.hpp b/apps/opencs/view/world/tableeditidaction.hpp
new file mode 100644
index 0000000000..f2cf0b7bd0
--- /dev/null
+++ b/apps/opencs/view/world/tableeditidaction.hpp
@@ -0,0 +1,31 @@
+#ifndef CSVWORLD_TABLEEDITIDACTION_HPP
+#define CSVWORLD_TABLEEDITIDACTION_HPP
+
+#include <QAction>
+
+#include "../../model/world/columnbase.hpp"
+#include "../../model/world/universalid.hpp"
+
+class QTableView;
+
+namespace CSVWorld
+{
+    class TableEditIdAction : public QAction
+    {
+            const QTableView &mTable;
+            CSMWorld::UniversalId mCurrentId;
+
+            typedef std::pair<CSMWorld::ColumnBase::Display, QString> CellData;
+            CellData getCellData(int row, int column) const;
+
+        public:
+            TableEditIdAction(const QTableView &table, QWidget *parent = 0);
+
+            void setCell(int row, int column);
+
+            CSMWorld::UniversalId getCurrentId() const;
+            bool isValidIdCell(int row, int column) const;
+    };
+}
+
+#endif