From 954bb6a533c840e0dc506d5792b75f065df0dd72 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Thu, 9 May 2013 05:53:14 +0200 Subject: [PATCH] Some more work for widget adding support. Basically the system works, just need to make sure the proper widget is instantiated, and the defaults are loaded ( so it shows up ). --- code/nel/include/nel/gui/interface_parser.h | 1 + code/nel/include/nel/gui/parser.h | 2 + .../include/nel/gui/widget_addition_watcher.h | 16 ++++ code/nel/include/nel/gui/widget_manager.h | 8 ++ code/nel/src/gui/interface_parser.cpp | 5 ++ code/nel/src/gui/widget_manager.cpp | 73 +++++++++++++++++++ .../gui_editor/editor_message_processor.cpp | 29 ++++---- .../plugins/gui_editor/widget_hierarchy.cpp | 44 +++++++++++ .../src/plugins/gui_editor/widget_hierarchy.h | 1 + 9 files changed, 165 insertions(+), 14 deletions(-) create mode 100644 code/nel/include/nel/gui/widget_addition_watcher.h diff --git a/code/nel/include/nel/gui/interface_parser.h b/code/nel/include/nel/gui/interface_parser.h index eb602b8ca..2bf1df9a8 100644 --- a/code/nel/include/nel/gui/interface_parser.h +++ b/code/nel/include/nel/gui/interface_parser.h @@ -382,6 +382,7 @@ namespace NLGUI bool serializeProcs( xmlNodePtr parentNode ) const; bool serializePointerSettings( xmlNodePtr parentNode ) const; bool serializeKeySettings( xmlNodePtr parentNode ) const; + CViewBase* createClass( const std::string &name ); }; } diff --git a/code/nel/include/nel/gui/parser.h b/code/nel/include/nel/gui/parser.h index dc6d00767..db868f70d 100644 --- a/code/nel/include/nel/gui/parser.h +++ b/code/nel/include/nel/gui/parser.h @@ -27,6 +27,7 @@ namespace NLGUI { class CInterfaceElement; + class CViewBase; class CInterfaceGroup; class CInterfaceAnim; class CCtrlSheetSelection; @@ -86,6 +87,7 @@ namespace NLGUI virtual bool serializeProcs( xmlNodePtr parentNode ) const = 0; virtual bool serializePointerSettings( xmlNodePtr parentNode ) const = 0; virtual bool serializeKeySettings( xmlNodePtr parentNode ) const = 0; + virtual CViewBase* createClass( const std::string &name ) = 0; }; } diff --git a/code/nel/include/nel/gui/widget_addition_watcher.h b/code/nel/include/nel/gui/widget_addition_watcher.h new file mode 100644 index 000000000..555c1ff3c --- /dev/null +++ b/code/nel/include/nel/gui/widget_addition_watcher.h @@ -0,0 +1,16 @@ +#ifndef WIDGET_ADD_WATCHER +#define WIDGET_ADD_WATCHER + +#include + +namespace NLGUI +{ + class IWidgetAdditionWatcher + { + public: + virtual void widgetAdded( const std::string &name ) = 0; + }; +} + +#endif + diff --git a/code/nel/include/nel/gui/widget_manager.h b/code/nel/include/nel/gui/widget_manager.h index 498139ecf..7ec4908a0 100644 --- a/code/nel/include/nel/gui/widget_manager.h +++ b/code/nel/include/nel/gui/widget_manager.h @@ -48,6 +48,7 @@ namespace NLGUI class CInterfaceAnim; class CProcedure; class IEditorSelectionWatcher; + class IWidgetAdditionWatcher; /** GUI Widget Manager @@ -490,6 +491,12 @@ namespace NLGUI void notifySelectionWatchers(); void registerSelectionWatcher( IEditorSelectionWatcher *watcher ); void unregisterSelectionWatcher( IEditorSelectionWatcher *watcher ); + + void notifyAdditionWatchers( const std::string &widgetName ); + void registerAdditionWatcher( IWidgetAdditionWatcher *watcher ); + void unregisterAdditionWatcher( IWidgetAdditionWatcher *watcher ); + + CInterfaceElement* addWidgetToGroup( std::string &group, std::string &widgetClass, std::string &widgetName ); private: CWidgetManager(); @@ -570,6 +577,7 @@ namespace NLGUI std::vector< INewScreenSizeHandler* > newScreenSizeHandlers; std::vector< IOnWidgetsDrawnHandler* > onWidgetsDrawnHandlers; std::vector< IEditorSelectionWatcher* > selectionWatchers; + std::vector< IWidgetAdditionWatcher* > additionWatchers; std::string currentEditorSelection; diff --git a/code/nel/src/gui/interface_parser.cpp b/code/nel/src/gui/interface_parser.cpp index 76f5df1ed..03cce1449 100644 --- a/code/nel/src/gui/interface_parser.cpp +++ b/code/nel/src/gui/interface_parser.cpp @@ -3148,5 +3148,10 @@ namespace NLGUI return true; } + + CViewBase* CInterfaceParser::createClass( const std::string &name ) + { + return NLMISC_GET_FACTORY( CViewBase, std::string ).createObject( std::string( name ) , CViewBase::TCtorParam() ); + } } diff --git a/code/nel/src/gui/widget_manager.cpp b/code/nel/src/gui/widget_manager.cpp index 0612d54b1..ecac9335c 100644 --- a/code/nel/src/gui/widget_manager.cpp +++ b/code/nel/src/gui/widget_manager.cpp @@ -34,6 +34,7 @@ #include "nel/gui/interface_expr.h" #include "nel/gui/reflect_register.h" #include "nel/gui/editor_selection_watcher.h" +#include "nel/gui/widget_addition_watcher.h" #include "nel/misc/events.h" namespace NLGUI @@ -3236,6 +3237,78 @@ namespace NLGUI selectionWatchers.erase( itr ); } + void CWidgetManager::notifyAdditionWatchers( const std::string &widgetName ) + { + std::vector< IWidgetAdditionWatcher* >::const_iterator itr = additionWatchers.begin(); + while( itr != additionWatchers.end() ) + { + (*itr)->widgetAdded( widgetName ); + ++itr; + } + } + + void CWidgetManager::registerAdditionWatcher( IWidgetAdditionWatcher *watcher ) + { + std::vector< IWidgetAdditionWatcher* >::const_iterator itr + = std::find( additionWatchers.begin(), additionWatchers.end(), watcher ); + // already exists + if( itr != additionWatchers.end() ) + return; + + additionWatchers.push_back( watcher ); + } + + void CWidgetManager::unregisterAdditionWatcher( IWidgetAdditionWatcher *watcher ) + { + std::vector< IWidgetAdditionWatcher* >::iterator itr + = std::find( additionWatchers.begin(), additionWatchers.end(), watcher ); + // doesn't exist + if( itr == additionWatchers.end() ) + return; + + additionWatchers.erase( itr ); + } + + CInterfaceElement* CWidgetManager::addWidgetToGroup( std::string &group, std::string &widgetClass, std::string &widgetName ) + { + // Check if this group exists + CInterfaceElement *e = getElementFromId( group ); + if( e == NULL ) + return NULL; + CInterfaceGroup *g = dynamic_cast< CInterfaceGroup* >( e ); + if( g == NULL ) + return NULL; + + // Check if an element already exists with that name + if( g->getElement( widgetName ) != NULL ) + return NULL; + + // Create and add the new widget + CViewBase *v = getParser()->createClass( "button" ); + if( v == NULL ) + return NULL; + + v->setId( std::string( g->getId() + ":" + widgetName ) ); + + v->setParentPosRef( Hotspot_TL ); + v->setPosRef( Hotspot_TL ); + + if( v->isGroup() ) + g->addGroup( dynamic_cast< CInterfaceGroup* >( v ) ); + else + if( v->isCtrl() ) + g->addCtrl( dynamic_cast< CCtrlBase* >( v ) ); + else + g->addView( v ); + + // Invalidate so it shows up! + v->invalidateCoords(); + + notifyAdditionWatchers( v->getId() ); + + return v; + } + CWidgetManager::CWidgetManager() { diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/editor_message_processor.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/editor_message_processor.cpp index 25924a804..c91381fb7 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/editor_message_processor.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/editor_message_processor.cpp @@ -57,21 +57,22 @@ namespace GUIEditor void CEditorMessageProcessor::onAdd( const QString &parentGroup, const QString &widgetType, const QString &name ) { - // Check if this group exists - CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( std::string( parentGroup.toAscii() ) ); + CInterfaceElement *e = + CWidgetManager::getInstance()->addWidgetToGroup( + std::string( parentGroup.toUtf8() ), + std::string( widgetType.toUtf8() ), + std::string( name.toUtf8() ) + ); + if( e == NULL ) - return; - CInterfaceGroup *g = dynamic_cast< CInterfaceGroup* >( e ); - if( g == NULL ) - return; - - // Check if an element already exists with that name - if( g->getElement( std::string( name.toAscii() ) ) != NULL ) - return; - - // Create and add the new widget - //CViewBase *v = NULL; - //g->addView( v ); + { + QMessageBox::critical( + NULL, + tr( "Error" ), + tr( "Error adding the new widget!" ), + QMessageBox::Ok + ); + } } } diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.cpp index f48c35e93..24208f4a3 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.cpp @@ -18,6 +18,7 @@ #include "widget_hierarchy.h" #include "nel/gui/interface_group.h" #include "nel/gui/widget_manager.h" +#include "nel/gui/widget_addition_watcher.h" namespace { @@ -75,7 +76,26 @@ namespace GUIEditor::WidgetHierarchy *h; }; + class CWidgetAdditionWatcher : public IWidgetAdditionWatcher + { + public: + CWidgetAdditionWatcher(){ h = NULL; } + ~CWidgetAdditionWatcher(){} + + void widgetAdded( const std::string &name ) + { + if( h != NULL ) + h->onWidgetAdded( name ); + } + + void setWidgetHierarchy( GUIEditor::WidgetHierarchy *h ){ this->h = h; } + + private: + GUIEditor::WidgetHierarchy *h; + }; + CWidgetDeletionWatcher deletionWatcher; + CWidgetAdditionWatcher additionWatcher; } namespace GUIEditor @@ -87,6 +107,7 @@ namespace GUIEditor connect( widgetHT, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ), this, SLOT( onItemDblClicked( QTreeWidgetItem* ) ) ); deletionWatcher.setWidgetHierarchy( this ); + additionWatcher.setWidgetHierarchy( this ); } WidgetHierarchy::~WidgetHierarchy() @@ -96,6 +117,7 @@ namespace GUIEditor void WidgetHierarchy::clearHierarchy() { CInterfaceElement::unregisterDeletionWatcher( &deletionWatcher ); + CWidgetManager::getInstance()->unregisterAdditionWatcher( &additionWatcher ); widgetHT->clear(); widgetHierarchyMap.clear(); } @@ -104,6 +126,7 @@ namespace GUIEditor { clearHierarchy(); CInterfaceElement::registerDeletionWatcher( &deletionWatcher ); + CWidgetManager::getInstance()->registerAdditionWatcher( &additionWatcher ); CInterfaceGroup *mg = CWidgetManager::getInstance()->getMasterGroupFromId( masterGroup ); if( mg != NULL ) @@ -187,6 +210,27 @@ namespace GUIEditor widgetHierarchyMap.erase( itr ); } + void WidgetHierarchy::onWidgetAdded( const std::string &id ) + { + // Get the parent's name + std::string::size_type p = id.find_last_of( ':' ); + if( p == std::string::npos ) + return; + std::string parentId = id.substr( 0, p ); + + // Do we have the parent in the hierarchy? + std::map< std::string, QTreeWidgetItem* >::iterator itr + = widgetHierarchyMap.find( parentId ); + if( itr == widgetHierarchyMap.end() ) + return; + + // Add the new widget to the hierarchy + QTreeWidgetItem *parent = itr->second; + QTreeWidgetItem *item = new QTreeWidgetItem( parent ); + item->setText( 0, makeNodeName( id ).c_str() ); + widgetHierarchyMap[ id ] = item; + } + void WidgetHierarchy::getCurrentGroup( QString &g ) { std::string s = CWidgetManager::getInstance()->getCurrentEditorSelection(); diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.h index bfc68ea4f..4641c8ce8 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_hierarchy.h @@ -43,6 +43,7 @@ namespace GUIEditor void buildHierarchy( std::string &masterGroup ); void onWidgetDeleted( const std::string &id ); + void onWidgetAdded( const std::string &id ); void getCurrentGroup( QString &g );