From eeac53ebbab9142a4878bf9d4fbf9f17bc93aaea Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Wed, 20 Aug 2014 23:18:16 +0200 Subject: [PATCH] Added support for creating arrays. --HG-- branch : dfighter-tools --- .../plugins/georges_editor/browser_ctrl.cpp | 14 +-- .../georges_editor/browser_ctrl_pvt.cpp | 119 ++++++++++++++---- .../plugins/georges_editor/browser_ctrl_pvt.h | 33 ++++- .../src/plugins/georges_editor/formitem.cpp | 45 ++----- .../src/plugins/georges_editor/formitem.h | 10 +- .../georges_editor/georgesform_model.cpp | 8 +- 6 files changed, 144 insertions(+), 85 deletions(-) diff --git a/code/studio/src/plugins/georges_editor/browser_ctrl.cpp b/code/studio/src/plugins/georges_editor/browser_ctrl.cpp index aa431b45c..17eb170c4 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl.cpp +++ b/code/studio/src/plugins/georges_editor/browser_ctrl.cpp @@ -31,18 +31,8 @@ void BrowserCtrl::clicked( const QModelIndex &idx ) m_pvt->clear(); GeorgesQt::CFormItem *item = static_cast< GeorgesQt::CFormItem* >( idx.internalPointer() ); - NLGEORGES::UFormElm &root = m_form->getRootNode(); - NLGEORGES::CFormElm *rootNode = dynamic_cast< NLGEORGES::CFormElm* >( &root ); - m_pvt->setRootNode( rootNode ); - NLGEORGES::UFormElm *node = NULL; - bool b = false; - - b = m_form->getRootNode().getNodeByName( &node, item->formName().c_str() ); - - if( !b || ( node == NULL ) ) - return; - - m_pvt->setupNode( node ); + + m_pvt->setupNode( item ); enableMgrConnections(); diff --git a/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.cpp b/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.cpp index 0e22765da..297cf04d8 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.cpp +++ b/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.cpp @@ -2,6 +2,9 @@ #include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h" #include "3rdparty/qtpropertybrowser/qtvariantproperty.h" #include +#include "formitem.h" + +#include "nel/georges/form.h" namespace { @@ -26,6 +29,12 @@ namespace return t; } + NLGEORGES::UFormElm* getGeorgesNode( GeorgesQt::CFormItem *item ) + { + NLGEORGES::UFormElm *n = NULL; + item->form()->getRootNode().getNodeByName( &n, item->formName().c_str() ); + return n; + } } @@ -34,7 +43,6 @@ QObject( parent ) { mgr = new QtVariantPropertyManager(); factory = new QtVariantEditorFactory(); - m_currentNode = NULL; m_rootNode = NULL; } @@ -70,19 +78,6 @@ void BrowserCtrlPvt::setupAtom( NLGEORGES::CFormElmStruct::CFormElmStructElm &el m_browser->addProperty( p ); } -void BrowserCtrlPvt::setupArray( NLGEORGES::UFormElm *node ) -{ - NLGEORGES::CFormElmArray *arr = static_cast< NLGEORGES::CFormElmArray* >( node ); - uint size = 0; - arr->getArraySize( size ); - - QString key = QObject::tr( "Array size" ); - QtVariantProperty *p = mgr->addProperty( QVariant::Int, key ); - p->setValue( size ); - m_browser->addProperty( p ); - -} - void BrowserCtrlPvt::setupStruct( NLGEORGES::UFormElm *node ) { NLGEORGES::CFormElmStruct *st = static_cast< NLGEORGES::CFormElmStruct* >( node ); @@ -104,24 +99,54 @@ void BrowserCtrlPvt::setupStruct( NLGEORGES::UFormElm *node ) } } -void BrowserCtrlPvt::setupNode( NLGEORGES::UFormElm *node ) +void BrowserCtrlPvt::setupStruct( GeorgesQt::CFormItem *node ) { - if( node->isStruct() ) - setupStruct( node ); - else + NLGEORGES::UFormElm *n = getGeorgesNode( node ); + if( n == NULL ) + return; + + m_currentNode.p = n; + + setupStruct( n ); +} + +void BrowserCtrlPvt::setupArray( GeorgesQt::CFormItem *node ) +{ + NLGEORGES::UFormElm *n = getGeorgesNode( node ); + uint size = 0; + + if( n != NULL ) + { + NLGEORGES::CFormElmArray *arr = static_cast< NLGEORGES::CFormElmArray* >( n ); + arr->getArraySize( size ); + m_currentNode.p = n; + } + + QString key = QObject::tr( "Array size" ); + QtVariantProperty *p = mgr->addProperty( QVariant::Int, key ); + p->setValue( size ); + m_browser->addProperty( p ); +} + +void BrowserCtrlPvt::setupNode( GeorgesQt::CFormItem *node ) +{ + m_currentNode.clear(); + m_currentNode.name = node->formName().c_str(); + + m_rootNode = dynamic_cast< NLGEORGES::CFormElm* >( &(node->form()->getRootNode()) ); + if( node->isArray() ) setupArray( node ); else - return; + setupStruct( node ); - m_currentNode = node; m_browser->setFactoryForManager( mgr, factory ); } void BrowserCtrlPvt::clear() { m_browser->clear(); - m_currentNode = NULL; + m_currentNode.clear(); } @@ -131,14 +156,48 @@ void BrowserCtrlPvt::onStructValueChanged( QtProperty *p, const QVariant &value std::string v = value.toString().toUtf8().constData(); bool created = false; - m_currentNode->setValueByName( v.c_str(), k.c_str(), &created ); + m_currentNode.p->setValueByName( v.c_str(), k.c_str(), &created ); Q_EMIT modified(); } +void BrowserCtrlPvt::createArray() +{ + const NLGEORGES::CFormDfn *parentDfn; + const NLGEORGES::CFormDfn *nodeDfn; + uint indexDfn; + const NLGEORGES::CType *type; + NLGEORGES::UFormDfn::TEntryType entryType; + NLGEORGES::CFormElm *node; + bool created; + bool isArray; + + m_rootNode->createNodeByName( m_currentNode.name.toUtf8().constData(), &parentDfn, indexDfn, &nodeDfn, &type, &node, entryType, isArray, created ); + + if( !created ) + return; + + NLGEORGES::CFormElmArray *arr = dynamic_cast< NLGEORGES::CFormElmArray* >( node ); + QString idx = "[0]"; + arr->createNodeByName( idx.toUtf8().constData(), &parentDfn, indexDfn, &nodeDfn, &type, &node, entryType, isArray, created ); + + std::string formName; + arr->getFormName( formName, NULL ); + + Q_EMIT arrayResized( formName.c_str(), 1 ); + Q_EMIT modified(); + +} + void BrowserCtrlPvt::onArrayValueChanged( QtProperty *p, const QVariant &value ) { - NLGEORGES::CFormElmArray *arr = static_cast< NLGEORGES::CFormElmArray* >( m_currentNode ); + if( m_currentNode.p == NULL ) + { + createArray(); + return; + } + + NLGEORGES::CFormElmArray *arr = static_cast< NLGEORGES::CFormElmArray* >( m_currentNode.p ); std::string formName; arr->getFormName( formName, NULL ); @@ -192,13 +251,19 @@ void BrowserCtrlPvt::onArrayValueChanged( QtProperty *p, const QVariant &value ) void BrowserCtrlPvt::onValueChanged( QtProperty *p, const QVariant &value ) { - if( m_currentNode == NULL ) - return; + if( m_currentNode.p == NULL ) + { + if( m_currentNode.name.isEmpty() ) + return; - if( m_currentNode->isStruct() ) + onArrayValueChanged( p, value ); + return; + } + + if( m_currentNode.p->isStruct() ) onStructValueChanged( p, value ); else - if( m_currentNode->isArray() ) + if( m_currentNode.p->isArray() ) onArrayValueChanged( p, value ); } diff --git a/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.h b/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.h index 0d0363c1f..69924a308 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.h +++ b/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.h @@ -10,6 +10,11 @@ namespace NLGEORGES class CFormElmStruct; } +namespace GeorgesQt +{ + class CFormItem; +} + class QtVariantPropertyManager; class QtVariantEditorFactory; class QtTreePropertyBrowser; @@ -24,7 +29,7 @@ public: ~BrowserCtrlPvt(); void clear(); - void setupNode( NLGEORGES::UFormElm *node ); + void setupNode( GeorgesQt::CFormItem *node ); void onValueChanged( QtProperty *p, const QVariant &value ); QtVariantPropertyManager* manager() const{ return mgr; } @@ -37,18 +42,40 @@ Q_SIGNALS: private: void setupStruct( NLGEORGES::UFormElm *node ); - void setupArray( NLGEORGES::UFormElm *node ); void setupAtom( NLGEORGES::CFormElmStruct::CFormElmStructElm &elm ); + void setupStruct( GeorgesQt::CFormItem *node ); + void setupArray( GeorgesQt::CFormItem *node ); + void onStructValueChanged( QtProperty *p, const QVariant &value ); void onArrayValueChanged( QtProperty *p, const QVariant &value ); + void createArray(); QtVariantPropertyManager *mgr; QtVariantEditorFactory *factory; QtTreePropertyBrowser *m_browser; - NLGEORGES::UFormElm *m_currentNode; + QString m_currentNodeName; NLGEORGES::CFormElm *m_rootNode; + + struct CurrentNode + { + CurrentNode() + { + clear(); + } + + void clear() + { + p = NULL; + name = ""; + } + + QString name; + NLGEORGES::UFormElm *p; + }; + + CurrentNode m_currentNode; }; #endif diff --git a/code/studio/src/plugins/georges_editor/formitem.cpp b/code/studio/src/plugins/georges_editor/formitem.cpp index a18db48bb..8cd479961 100644 --- a/code/studio/src/plugins/georges_editor/formitem.cpp +++ b/code/studio/src/plugins/georges_editor/formitem.cpp @@ -40,6 +40,7 @@ namespace GeorgesQt _StructId = 0; _Slot = 0; _Type = Null; + _Array = false; } CFormItem::~CFormItem() @@ -112,48 +113,15 @@ namespace GeorgesQt bool CFormItem::isArray() { - // If it wasn't a root node then lets check the node type. - const NLGEORGES::CFormDfn *parentDfn; - uint indexDfn; - const NLGEORGES::CFormDfn *nodeDfn; - const NLGEORGES::CType *nodeType; - NLGEORGES::CFormElm *node; - NLGEORGES::UFormDfn::TEntryType type; - bool array; - bool parentVDfnArray; - NLGEORGES::CForm *form = static_cast(m_form); - NLGEORGES::CFormElm *elm = static_cast(&form->getRootNode()); - nlverify ( elm->getNodeByName (_FormName.c_str(), &parentDfn, indexDfn, - &nodeDfn, &nodeType, &node, type, array, parentVDfnArray, true, NLGEORGES_FIRST_ROUND) ); - - if(array && node) - return true; - - return false; + return _Array; } bool CFormItem::isArrayMember() { - CFormItem *parent = this->parent(); + if( parentItem == NULL ) + return false; - // If it wasn't a root node then lets check the node type. - const NLGEORGES::CFormDfn *parentDfn; - uint indexDfn; - const NLGEORGES::CFormDfn *nodeDfn; - const NLGEORGES::CType *nodeType; - NLGEORGES::CFormElm *parentNode; - NLGEORGES::UFormDfn::TEntryType type; - bool array; - bool parentVDfnArray; - NLGEORGES::CForm *form = static_cast(m_form); - NLGEORGES::CFormElm *elm = static_cast(&form->getRootNode()); - nlverify ( elm->getNodeByName (parent->formName ().c_str (), &parentDfn, indexDfn, - &nodeDfn, &nodeType, &parentNode, type, array, parentVDfnArray, true, NLGEORGES_FIRST_ROUND) ); - - if(array && parentNode) - return true; - - return false; + return parentItem->isArray(); } QIcon CFormItem::getItemImage(CFormItem *rootItem) @@ -243,7 +211,7 @@ namespace GeorgesQt childItems.clear(); } - CFormItem *CFormItem::add (TSub type, const char *name, uint structId, const char *formName, uint slot, NLGEORGES::UForm *formPtr) + CFormItem *CFormItem::add (TSub type, const char *name, uint structId, const char *formName, uint slot, NLGEORGES::UForm *formPtr, bool isArray) { CFormItem *newNode = new CFormItem(); newNode->_Type = type; @@ -253,6 +221,7 @@ namespace GeorgesQt newNode->_FormName = formName; newNode->_Slot = slot; newNode->m_form = formPtr; + newNode->_Array = isArray; appendChild(newNode); return newNode; diff --git a/code/studio/src/plugins/georges_editor/formitem.h b/code/studio/src/plugins/georges_editor/formitem.h index d29dfce0c..cd67d1b21 100644 --- a/code/studio/src/plugins/georges_editor/formitem.h +++ b/code/studio/src/plugins/georges_editor/formitem.h @@ -46,7 +46,7 @@ namespace GeorgesQt void appendChild(CFormItem *child); - CFormItem *add (TSub type, const char *name, uint structId, const char *formName, uint slot, NLGEORGES::UForm *formPtr); + CFormItem *add (TSub type, const char *name, uint structId, const char *formName, uint slot, NLGEORGES::UForm *formPtr, bool isArray ); CFormItem *child(int row); int childCount() const; @@ -76,6 +76,13 @@ namespace GeorgesQt void clearChildren(); + bool rootItem() const{ + if( parentItem == NULL ) + return true; + else + return false; + } + private: QList childItems; QList itemData; @@ -88,6 +95,7 @@ namespace GeorgesQt std::string _FormName; TSub _Type; uint _Slot; + bool _Array; }; // CFormItem diff --git a/code/studio/src/plugins/georges_editor/georgesform_model.cpp b/code/studio/src/plugins/georges_editor/georgesform_model.cpp index 3de9b7415..f533970fa 100644 --- a/code/studio/src/plugins/georges_editor/georgesform_model.cpp +++ b/code/studio/src/plugins/georges_editor/georgesform_model.cpp @@ -283,7 +283,7 @@ namespace GeorgesQt NLGEORGES::CForm *formPtr = static_cast(m_form); // Add the new node - CFormItem *newNode = parent->add(CFormItem::Form, name, structId, formName, slot, m_form); + CFormItem *newNode = parent->add(CFormItem::Form, name, structId, formName, slot, m_form, false); // Can be NULL in virtual DFN if (parentDfn) @@ -418,7 +418,7 @@ CFormItem *CGeorgesFormModel::addArray(CFormItem *parent, uint slot) { // Add the new node - CFormItem *newNode = parent->add (CFormItem::Form, name, structId, formName, slot, m_form); + CFormItem *newNode = parent->add (CFormItem::Form, name, structId, formName, slot, m_form, true); // The array exist if (array) @@ -451,7 +451,7 @@ CFormItem *CGeorgesFormModel::addArray(CFormItem *parent, else { NLGEORGES::CFormElmArray *elmPtr = array->Elements[elm].Element ? static_cast(array->Elements[elm].Element) : NULL; - newNode->add (CFormItem::Form, formArrayName, elm, formArrayElmName, slot, m_form); + newNode->add (CFormItem::Form, formArrayName, elm, formArrayElmName, slot, m_form, false); } } } @@ -494,7 +494,7 @@ void CGeorgesFormModel::arrayResized( const QString &name, int size ) else n = e.Name.c_str(); - item->add( CFormItem::Form, n.toUtf8().constData(), i, formName.toUtf8().constData(), 0, item->form() ); + item->add( CFormItem::Form, n.toUtf8().constData(), i, formName.toUtf8().constData(), 0, item->form(), false ); } if( celm->Elements.size() == 0 )