From 827faaf3e73788f54cd2b466ac921de0b8dd7ced Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Mon, 18 Aug 2014 18:03:48 +0200 Subject: [PATCH] Array resize. --HG-- branch : gsoc2014-dfighter --- .../plugins/georges_editor/browser_ctrl.cpp | 7 +++ .../src/plugins/georges_editor/browser_ctrl.h | 4 ++ .../georges_editor/browser_ctrl_pvt.cpp | 46 +++++++++++++++++++ .../plugins/georges_editor/browser_ctrl_pvt.h | 3 ++ .../src/plugins/georges_editor/formitem.cpp | 25 +++++++++- .../src/plugins/georges_editor/formitem.h | 4 ++ .../georges_treeview_dialog.cpp | 22 +++++++++ .../georges_editor/georges_treeview_dialog.h | 4 ++ .../georges_editor/georgesform_model.cpp | 40 ++++++++++++++++ .../georges_editor/georgesform_model.h | 3 ++ 10 files changed, 157 insertions(+), 1 deletion(-) diff --git a/code/studio/src/plugins/georges_editor/browser_ctrl.cpp b/code/studio/src/plugins/georges_editor/browser_ctrl.cpp index 8801894e2..d881f42a2 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl.cpp +++ b/code/studio/src/plugins/georges_editor/browser_ctrl.cpp @@ -14,6 +14,8 @@ QObject( browser ) { m_pvt = new BrowserCtrlPvt(); m_pvt->setBrowser( browser ); + + connect( m_pvt, SIGNAL( arrayResized( const QString&, int ) ), this, SLOT( onArrayResized( const QString&, int ) ) ); } BrowserCtrl::~BrowserCtrl() @@ -50,6 +52,11 @@ void BrowserCtrl::onValueChanged( QtProperty *p, const QVariant &value ) m_pvt->onValueChanged( p, value ); } +void BrowserCtrl::onArrayResized( const QString &name, int size ) +{ + Q_EMIT arrayResized( name, size ); +} + void BrowserCtrl::enableMgrConnections() { QtVariantPropertyManager *mgr = m_pvt->manager(); diff --git a/code/studio/src/plugins/georges_editor/browser_ctrl.h b/code/studio/src/plugins/georges_editor/browser_ctrl.h index da14e14c9..6014b0419 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl.h +++ b/code/studio/src/plugins/georges_editor/browser_ctrl.h @@ -26,8 +26,12 @@ public: public Q_SLOTS: void clicked( const QModelIndex &idx ); +Q_SIGNALS: + void arrayResized( const QString &name, int size ); + private Q_SLOTS: void onValueChanged( QtProperty *p, const QVariant &value ); + void onArrayResized( const QString &name, int size ); private: void 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 f49ebc1ba..52eaabfa6 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.cpp +++ b/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.cpp @@ -139,6 +139,52 @@ void BrowserCtrlPvt::onArrayValueChanged( QtProperty *p, const QVariant &value ) NLGEORGES::CFormElmArray *arr = static_cast< NLGEORGES::CFormElmArray* >( m_currentNode ); std::string formName; arr->getFormName( formName, NULL ); + + int newSize = value.toInt(); + int oldSize = arr->Elements.size(); + + if( newSize == oldSize ) + return; + + if( newSize < oldSize ) + { + for( int i = newSize; i < oldSize; i++ ) + { + delete arr->Elements[ i ].Element; + } + + arr->Elements.resize( newSize ); + } + else + { + arr->Elements.resize( newSize ); + + + 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; + + QString idx; + + for( int i = oldSize; i < newSize; i++ ) + { + idx.clear(); + idx += "["; + idx += QString::number( i ); + idx += "]"; + + bool b; + b = arr->createNodeByName( idx.toUtf8().constData(), &parentDfn, indexDfn, &nodeDfn, &type, &node, entryType, isArray, created ); + } + } + + QString name = formName.c_str(); + Q_EMIT arrayResized( name, newSize ); } void BrowserCtrlPvt::onValueChanged( QtProperty *p, const QVariant &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 51d756cfa..5bf93da4a 100644 --- a/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.h +++ b/code/studio/src/plugins/georges_editor/browser_ctrl_pvt.h @@ -31,6 +31,9 @@ public: void setRootNode( NLGEORGES::CFormElm *root ){ m_rootNode = root; } void setBrowser( QtTreePropertyBrowser *browser ){ m_browser = browser; } +Q_SIGNALS: + void arrayResized( const QString &name, int size ); + private: void setupStruct( NLGEORGES::UFormElm *node ); void setupArray( NLGEORGES::UFormElm *node ); diff --git a/code/studio/src/plugins/georges_editor/formitem.cpp b/code/studio/src/plugins/georges_editor/formitem.cpp index 426009ca3..1ca2ecbe1 100644 --- a/code/studio/src/plugins/georges_editor/formitem.cpp +++ b/code/studio/src/plugins/georges_editor/formitem.cpp @@ -38,7 +38,7 @@ namespace GeorgesQt CFormItem::~CFormItem() { - qDeleteAll(childItems); + clearChildren(); } void CFormItem::appendChild(CFormItem *item) @@ -214,6 +214,29 @@ namespace GeorgesQt return QIcon(); } + CFormItem* CFormItem::findItem( const QString &formName ) + { + CFormItem *item = NULL; + + if( _FormName.c_str() == formName ) + return this; + + for( int i = 0; i < childItems.count(); i++ ) + { + item = childItems[ i ]->findItem( formName ); + if( item != NULL ) + return item; + } + + return item; + } + + void CFormItem::clearChildren() + { + qDeleteAll( childItems ); + childItems.clear(); + } + CFormItem *CFormItem::add (TSub type, const char *name, uint structId, const char *formName, uint slot, NLGEORGES::UForm *formPtr) { CFormItem *newNode = new CFormItem(); diff --git a/code/studio/src/plugins/georges_editor/formitem.h b/code/studio/src/plugins/georges_editor/formitem.h index cc2dafece..d29dfce0c 100644 --- a/code/studio/src/plugins/georges_editor/formitem.h +++ b/code/studio/src/plugins/georges_editor/formitem.h @@ -72,6 +72,10 @@ namespace GeorgesQt QIcon getItemImage(CFormItem *rootItem); + CFormItem* findItem( const QString &formName ); + + void clearChildren(); + private: QList childItems; QList itemData; diff --git a/code/studio/src/plugins/georges_editor/georges_treeview_dialog.cpp b/code/studio/src/plugins/georges_editor/georges_treeview_dialog.cpp index a8e7e7451..838fa8cc2 100644 --- a/code/studio/src/plugins/georges_editor/georges_treeview_dialog.cpp +++ b/code/studio/src/plugins/georges_editor/georges_treeview_dialog.cpp @@ -74,6 +74,7 @@ namespace GeorgesQt m_ui.treeViewTabWidget->setTabEnabled (2,false); m_form = 0; + m_model = NULL; m_ui.treeView->setContextMenuPolicy(Qt::CustomContextMenu); @@ -87,11 +88,13 @@ namespace GeorgesQt m_browserCtrl, SLOT(clicked(const QModelIndex&))); connect(m_header, SIGNAL(headerClicked(int)), this, SLOT(headerClicked(int))); + connect(m_browserCtrl, SIGNAL(arrayResized(const QString&,int)), this, SLOT(onArrayResized(const QString&,int))); } CGeorgesTreeViewDialog::~CGeorgesTreeViewDialog() { m_browserCtrl = NULL; + m_model = NULL; delete m_form; qDebug() << "DTOR"; @@ -265,6 +268,8 @@ namespace GeorgesQt setWindowTitle(loadedForm); // //Modules::mainWin().getTabBar(); + + m_model = model; } } @@ -446,6 +451,23 @@ namespace GeorgesQt //} } + void CGeorgesTreeViewDialog::onArrayResized( const QString &name, int size ) + { + QModelIndex current = m_ui.treeView->currentIndex(); + QModelIndex parent = current.parent(); + int r = current.row(); + int c = current.column(); + + m_model->arrayResized( name, size ); + m_ui.treeView->reset(); + m_ui.treeView->expandAll(); + + QModelIndex idx = m_model->index( r, c, parent ); + if( !idx.isValid() ) + return; + m_ui.treeView->setCurrentIndex( idx ); + } + void CGeorgesTreeViewDialog::closeEvent(QCloseEvent *event) { Q_EMIT closing(); diff --git a/code/studio/src/plugins/georges_editor/georges_treeview_dialog.h b/code/studio/src/plugins/georges_editor/georges_treeview_dialog.h index b2171cf03..7b40fc3ef 100644 --- a/code/studio/src/plugins/georges_editor/georges_treeview_dialog.h +++ b/code/studio/src/plugins/georges_editor/georges_treeview_dialog.h @@ -47,6 +47,7 @@ namespace GeorgesQt { class CGeorges; + class CGeorgesFormModel; class CGeorgesTreeViewDialog: public QDockWidget { @@ -102,6 +103,8 @@ namespace GeorgesQt void filterRows(); void headerClicked(int); + void onArrayResized( const QString &name, int size ); + private: Ui::CGeorgesTreeViewDialog m_ui; ExpandableHeaderView *m_header; @@ -116,6 +119,7 @@ namespace GeorgesQt bool m_modified; BrowserCtrl *m_browserCtrl; + CGeorgesFormModel *m_model; }; /* CGeorgesTreeViewDialog */ diff --git a/code/studio/src/plugins/georges_editor/georgesform_model.cpp b/code/studio/src/plugins/georges_editor/georgesform_model.cpp index 2cc5c1280..90eaaaacd 100644 --- a/code/studio/src/plugins/georges_editor/georgesform_model.cpp +++ b/code/studio/src/plugins/georges_editor/georgesform_model.cpp @@ -460,6 +460,46 @@ CFormItem *CGeorgesFormModel::addArray(CFormItem *parent, } +void CGeorgesFormModel::arrayResized( const QString &name, int size ) +{ + CFormItem *item = m_rootItem->findItem( name ); + if( item == NULL ) + return; + + NLGEORGES::UFormElm *elm = NULL; + + item->form()->getRootNode().getNodeByName( &elm, name.toUtf8().constData() ); + + if( elm == NULL ) + return; + + NLGEORGES::CFormElmArray *celm = dynamic_cast< NLGEORGES::CFormElmArray* >( elm ); + if( celm == NULL ) + return; + + item->clearChildren(); + + for( int i = 0; i < celm->Elements.size(); i++ ) + { + NLGEORGES::CFormElmArray::CElement &e = celm->Elements[ i ]; + + QString formName = name; + formName += '['; + formName += QString::number( i ); + formName += ']'; + + QString n; + if( e.Name.empty() ) + n = "#" + QString::number( i ); + else + n = e.Name.c_str(); + + item->add( CFormItem::Form, n.toUtf8().constData(), i, formName.toUtf8().constData(), 0, item->form() ); + } + +} + + /******************************************************************************/ diff --git a/code/studio/src/plugins/georges_editor/georgesform_model.h b/code/studio/src/plugins/georges_editor/georgesform_model.h index 07589b63c..4ca91398b 100644 --- a/code/studio/src/plugins/georges_editor/georgesform_model.h +++ b/code/studio/src/plugins/georges_editor/georgesform_model.h @@ -74,6 +74,9 @@ namespace GeorgesQt { Q_EMIT dataChanged(index, index); } + + void arrayResized( const QString &name, int size ); + private: void setupModelData(); void loadFormData(NLGEORGES::UFormElm *rootElm, CFormItem *parent);