From af63f94da001bfa74c365ca6d3dac24be1e985a5 Mon Sep 17 00:00:00 2001 From: James Cotton Date: Wed, 9 May 2012 17:25:23 -0500 Subject: [PATCH] OP-107 Make the waypoint editor have the ability to add new waypoints and edit their positions. --- .../waypointeditorgadgetwidget.cpp | 18 ++++-- .../waypointeditorgadgetwidget.h | 2 +- .../plugins/waypointeditor/waypointtable.cpp | 64 ++++++++++++++----- .../plugins/waypointeditor/waypointtable.h | 7 ++ 4 files changed, 69 insertions(+), 22 deletions(-) diff --git a/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.cpp b/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.cpp index 50866885a..2d1a9419d 100644 --- a/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.cpp +++ b/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.cpp @@ -44,17 +44,14 @@ WaypointEditorGadgetWidget::WaypointEditorGadgetWidget(QWidget *parent) : QLabel waypointTable = new WaypointTable(this); m_waypointeditor->waypoints->setModel(waypointTable); - // Get the objects used here ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); Q_ASSERT(pm != NULL); UAVObjectManager *objManager = pm->getObject(); Q_ASSERT(objManager != NULL); - waypointActiveObj = WaypointActive::GetInstance(objManager); - Q_ASSERT(waypointActiveObj != NULL); + waypointObj = Waypoint::GetInstance(objManager); + Q_ASSERT(waypointObj != NULL); // Connect the signals - connect(waypointActiveObj, SIGNAL(objectUpdated(UAVObject*)), - this, SLOT(waypointActiveChanged(UAVObject*))); connect(m_waypointeditor->buttonNewWaypoint, SIGNAL(clicked()), this, SLOT(addInstance())); } @@ -74,6 +71,17 @@ void WaypointEditorGadgetWidget::waypointActiveChanged(UAVObject *) void WaypointEditorGadgetWidget::addInstance() { + ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); + Q_ASSERT(pm != NULL); + UAVObjectManager *objManager = pm->getObject(); + Q_ASSERT(objManager != NULL); + + qDebug() << "Instances before: " << objManager->getNumInstances(waypointObj->getObjID()); + Waypoint *obj = new Waypoint(); + quint32 newInstId = objManager->getNumInstances(waypointObj->getObjID()); + obj->initialize(newInstId,obj->getMetaObject()); + objManager->registerObject(obj); + qDebug() << "Instances after: " << objManager->getNumInstances(waypointObj->getObjID()); } /** diff --git a/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.h b/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.h index c829dbceb..24bf7f8a1 100644 --- a/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.h +++ b/ground/openpilotgcs/src/plugins/waypointeditor/waypointeditorgadgetwidget.h @@ -51,7 +51,7 @@ protected slots: private: Ui_WaypointEditor * m_waypointeditor; WaypointTable *waypointTable; - WaypointActive *waypointActiveObj; + Waypoint *waypointObj; }; #endif /* WaypointEditorGADGETWIDGET_H_ */ diff --git a/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.cpp b/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.cpp index 60a895c75..46621952b 100644 --- a/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.cpp +++ b/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.cpp @@ -34,15 +34,18 @@ WaypointTable::WaypointTable(QObject *parent) : { ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); Q_ASSERT(pm != NULL); - UAVObjectManager *objManager = pm->getObject(); + objManager = pm->getObject(); Q_ASSERT(objManager != NULL); waypointObj = Waypoint::GetInstance(objManager); Q_ASSERT(waypointObj != NULL); waypointActiveObj = WaypointActive::GetInstance(objManager); - Q_ASSERT(waypointObj != NULL); + Q_ASSERT(waypointActiveObj != NULL); elements = 0; + // Unfortunately there is no per object new instance signal yet + connect(objManager, SIGNAL(newInstance(UAVObject*)), + this, SLOT(waypointsUpdated(UAVObject*))); connect(waypointActiveObj, SIGNAL(objectUpdated(UAVObject*)), this, SLOT(waypointsUpdated(UAVObject*))); connect(waypointObj, SIGNAL(), @@ -54,7 +57,7 @@ WaypointTable::WaypointTable(QObject *parent) : headers.append(QString("Down")); } -int WaypointTable::rowCount(const QModelIndex &parent) const +int WaypointTable::rowCount(const QModelIndex & /*parent*/) const { return elements; } @@ -70,11 +73,6 @@ int WaypointTable::columnCount(const QModelIndex &parent) const QVariant WaypointTable::data(const QModelIndex &index, int role) const { if(role == Qt::DisplayRole) { - ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); - Q_ASSERT(pm != NULL); - UAVObjectManager *objManager = pm->getObject(); - Q_ASSERT(objManager != NULL); - Waypoint *obj = Waypoint::GetInstance(objManager, index.row()); Q_ASSERT(obj); Waypoint::DataFields waypoint = obj->getData(); @@ -120,22 +118,56 @@ QVariant WaypointTable::headerData(int section, Qt::Orientation orientation, int */ void WaypointTable::waypointsUpdated(UAVObject *) { - ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); - Q_ASSERT(pm != NULL); - UAVObjectManager *objManager = pm->getObject(); - Q_ASSERT(objManager != NULL); - int elementsNow = objManager->getNumInstances(waypointObj->getObjID()); + int elementsNow = objManager->getNumInstances(waypointObj->getObjID()); // Currently only support adding instances which is all the UAVO manager // supports + qDebug() << "Elements before " << elementsNow << " and cached " << elements; if (elementsNow > elements) { - beginInsertRows(QModelIndex(), elements, elementsNow); + beginInsertRows(QModelIndex(), elements, elementsNow-1); elements = elementsNow; endInsertRows(); } QModelIndex i1 = index(0,0); - QModelIndex i2 = index(elements, columnCount(QModelIndex())); - qDebug() << "Waypoints updated. Found " << rowCount(QModelIndex()); + QModelIndex i2 = index(elements-1, columnCount(QModelIndex())); emit dataChanged(i1, i2); } + +Qt::ItemFlags WaypointTable::flags(const QModelIndex &index) const +{ + return QAbstractTableModel::flags(index) | Qt::ItemIsEditable; +} + +bool WaypointTable::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) +{ +// if(role != Qt::EditRole) +// return false; + + double val = value.toDouble(); + qDebug() << "New value " << val << " for column " << index.column(); + + Waypoint *obj = Waypoint::GetInstance(objManager, index.row()); + Q_ASSERT(obj); + Waypoint::DataFields waypoint = obj->getData(); + + switch(index.column()) { + case 0: + waypoint.Position[Waypoint::POSITION_NORTH] = val; + break; + case 1: + waypoint.Position[Waypoint::POSITION_EAST] = val; + break; + case 2: + waypoint.Position[Waypoint::POSITION_DOWN] = val; + break; + default: + return false; + } + + obj->setData(waypoint); + obj->updated(); + qDebug() << "Set data for instance " << obj->getInstID(); + + return true; +} diff --git a/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.h b/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.h index e213c1cf8..a9821e99e 100644 --- a/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.h +++ b/ground/openpilotgcs/src/plugins/waypointeditor/waypointtable.h @@ -39,11 +39,17 @@ class WaypointTable : public QAbstractTableModel public: explicit WaypointTable(QObject *parent = 0); + // Get dimensionality of the data int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; + // Access data QVariant data(const QModelIndex &index, int role) const; QVariant headerData(int section, Qt::Orientation orientation, int role) const; + + // Functions to make the data editable + Qt::ItemFlags flags(const QModelIndex &index) const; + bool setData ( const QModelIndex & index, const QVariant & value, int role); signals: protected slots: @@ -52,6 +58,7 @@ protected slots: public slots: private: + UAVObjectManager *objManager; Waypoint *waypointObj; WaypointActive *waypointActiveObj; QList headers;