Added support for creating arrays.

This commit is contained in:
dfighter1985 2014-08-20 23:18:16 +02:00
parent f32ded34fa
commit 56b335ca9e
6 changed files with 144 additions and 85 deletions

View file

@ -31,18 +31,8 @@ void BrowserCtrl::clicked( const QModelIndex &idx )
m_pvt->clear(); m_pvt->clear();
GeorgesQt::CFormItem *item = static_cast< GeorgesQt::CFormItem* >( idx.internalPointer() ); 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->setupNode( item );
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 );
enableMgrConnections(); enableMgrConnections();

View file

@ -2,6 +2,9 @@
#include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h" #include "3rdparty/qtpropertybrowser/qttreepropertybrowser.h"
#include "3rdparty/qtpropertybrowser/qtvariantproperty.h" #include "3rdparty/qtpropertybrowser/qtvariantproperty.h"
#include <QVariant> #include <QVariant>
#include "formitem.h"
#include "nel/georges/form.h"
namespace namespace
{ {
@ -26,6 +29,12 @@ namespace
return t; 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(); mgr = new QtVariantPropertyManager();
factory = new QtVariantEditorFactory(); factory = new QtVariantEditorFactory();
m_currentNode = NULL;
m_rootNode = NULL; m_rootNode = NULL;
} }
@ -70,19 +78,6 @@ void BrowserCtrlPvt::setupAtom( NLGEORGES::CFormElmStruct::CFormElmStructElm &el
m_browser->addProperty( p ); 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 ) void BrowserCtrlPvt::setupStruct( NLGEORGES::UFormElm *node )
{ {
NLGEORGES::CFormElmStruct *st = static_cast< NLGEORGES::CFormElmStruct* >( 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() ) NLGEORGES::UFormElm *n = getGeorgesNode( node );
setupStruct( node ); if( n == NULL )
else 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() ) if( node->isArray() )
setupArray( node ); setupArray( node );
else else
return; setupStruct( node );
m_currentNode = node;
m_browser->setFactoryForManager( mgr, factory ); m_browser->setFactoryForManager( mgr, factory );
} }
void BrowserCtrlPvt::clear() void BrowserCtrlPvt::clear()
{ {
m_browser->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(); std::string v = value.toString().toUtf8().constData();
bool created = false; 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(); 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 ) 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; std::string formName;
arr->getFormName( formName, NULL ); arr->getFormName( formName, NULL );
@ -192,13 +251,19 @@ void BrowserCtrlPvt::onArrayValueChanged( QtProperty *p, const QVariant &value )
void BrowserCtrlPvt::onValueChanged( QtProperty *p, const QVariant &value ) void BrowserCtrlPvt::onValueChanged( QtProperty *p, const QVariant &value )
{ {
if( m_currentNode == NULL ) if( m_currentNode.p == NULL )
return; {
if( m_currentNode.name.isEmpty() )
return;
if( m_currentNode->isStruct() ) onArrayValueChanged( p, value );
return;
}
if( m_currentNode.p->isStruct() )
onStructValueChanged( p, value ); onStructValueChanged( p, value );
else else
if( m_currentNode->isArray() ) if( m_currentNode.p->isArray() )
onArrayValueChanged( p, value ); onArrayValueChanged( p, value );
} }

View file

@ -10,6 +10,11 @@ namespace NLGEORGES
class CFormElmStruct; class CFormElmStruct;
} }
namespace GeorgesQt
{
class CFormItem;
}
class QtVariantPropertyManager; class QtVariantPropertyManager;
class QtVariantEditorFactory; class QtVariantEditorFactory;
class QtTreePropertyBrowser; class QtTreePropertyBrowser;
@ -24,7 +29,7 @@ public:
~BrowserCtrlPvt(); ~BrowserCtrlPvt();
void clear(); void clear();
void setupNode( NLGEORGES::UFormElm *node ); void setupNode( GeorgesQt::CFormItem *node );
void onValueChanged( QtProperty *p, const QVariant &value ); void onValueChanged( QtProperty *p, const QVariant &value );
QtVariantPropertyManager* manager() const{ return mgr; } QtVariantPropertyManager* manager() const{ return mgr; }
@ -37,18 +42,40 @@ Q_SIGNALS:
private: private:
void setupStruct( NLGEORGES::UFormElm *node ); void setupStruct( NLGEORGES::UFormElm *node );
void setupArray( NLGEORGES::UFormElm *node );
void setupAtom( NLGEORGES::CFormElmStruct::CFormElmStructElm &elm ); void setupAtom( NLGEORGES::CFormElmStruct::CFormElmStructElm &elm );
void setupStruct( GeorgesQt::CFormItem *node );
void setupArray( GeorgesQt::CFormItem *node );
void onStructValueChanged( QtProperty *p, const QVariant &value ); void onStructValueChanged( QtProperty *p, const QVariant &value );
void onArrayValueChanged( QtProperty *p, const QVariant &value ); void onArrayValueChanged( QtProperty *p, const QVariant &value );
void createArray();
QtVariantPropertyManager *mgr; QtVariantPropertyManager *mgr;
QtVariantEditorFactory *factory; QtVariantEditorFactory *factory;
QtTreePropertyBrowser *m_browser; QtTreePropertyBrowser *m_browser;
NLGEORGES::UFormElm *m_currentNode; QString m_currentNodeName;
NLGEORGES::CFormElm *m_rootNode; NLGEORGES::CFormElm *m_rootNode;
struct CurrentNode
{
CurrentNode()
{
clear();
}
void clear()
{
p = NULL;
name = "";
}
QString name;
NLGEORGES::UFormElm *p;
};
CurrentNode m_currentNode;
}; };
#endif #endif

View file

@ -40,6 +40,7 @@ namespace GeorgesQt
_StructId = 0; _StructId = 0;
_Slot = 0; _Slot = 0;
_Type = Null; _Type = Null;
_Array = false;
} }
CFormItem::~CFormItem() CFormItem::~CFormItem()
@ -112,48 +113,15 @@ namespace GeorgesQt
bool CFormItem::isArray() bool CFormItem::isArray()
{ {
// If it wasn't a root node then lets check the node type. return _Array;
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<CForm*>(m_form);
NLGEORGES::CFormElm *elm = static_cast<CFormElm*>(&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;
} }
bool CFormItem::isArrayMember() 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. return parentItem->isArray();
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<CForm*>(m_form);
NLGEORGES::CFormElm *elm = static_cast<CFormElm*>(&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;
} }
QIcon CFormItem::getItemImage(CFormItem *rootItem) QIcon CFormItem::getItemImage(CFormItem *rootItem)
@ -243,7 +211,7 @@ namespace GeorgesQt
childItems.clear(); 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(); CFormItem *newNode = new CFormItem();
newNode->_Type = type; newNode->_Type = type;
@ -253,6 +221,7 @@ namespace GeorgesQt
newNode->_FormName = formName; newNode->_FormName = formName;
newNode->_Slot = slot; newNode->_Slot = slot;
newNode->m_form = formPtr; newNode->m_form = formPtr;
newNode->_Array = isArray;
appendChild(newNode); appendChild(newNode);
return newNode; return newNode;

View file

@ -46,7 +46,7 @@ namespace GeorgesQt
void appendChild(CFormItem *child); 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); CFormItem *child(int row);
int childCount() const; int childCount() const;
@ -76,6 +76,13 @@ namespace GeorgesQt
void clearChildren(); void clearChildren();
bool rootItem() const{
if( parentItem == NULL )
return true;
else
return false;
}
private: private:
QList<CFormItem*> childItems; QList<CFormItem*> childItems;
QList<QVariant> itemData; QList<QVariant> itemData;
@ -88,6 +95,7 @@ namespace GeorgesQt
std::string _FormName; std::string _FormName;
TSub _Type; TSub _Type;
uint _Slot; uint _Slot;
bool _Array;
}; // CFormItem }; // CFormItem

View file

@ -283,7 +283,7 @@ namespace GeorgesQt
NLGEORGES::CForm *formPtr = static_cast<NLGEORGES::CForm*>(m_form); NLGEORGES::CForm *formPtr = static_cast<NLGEORGES::CForm*>(m_form);
// Add the new node // 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 // Can be NULL in virtual DFN
if (parentDfn) if (parentDfn)
@ -418,7 +418,7 @@ CFormItem *CGeorgesFormModel::addArray(CFormItem *parent,
uint slot) uint slot)
{ {
// Add the new node // 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 // The array exist
if (array) if (array)
@ -451,7 +451,7 @@ CFormItem *CGeorgesFormModel::addArray(CFormItem *parent,
else else
{ {
NLGEORGES::CFormElmArray *elmPtr = array->Elements[elm].Element ? static_cast<NLGEORGES::CFormElmArray*>(array->Elements[elm].Element) : NULL; NLGEORGES::CFormElmArray *elmPtr = array->Elements[elm].Element ? static_cast<NLGEORGES::CFormElmArray*>(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 else
n = e.Name.c_str(); 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 ) if( celm->Elements.size() == 0 )