FIXED: Widgets will no longer get stuck in the widget hierarchy tree, when deleting their parent.

--HG--
branch : gsoc2012-gui-editor
This commit is contained in:
dfighter1985 2013-03-09 22:02:31 +01:00
parent 20820697a3
commit df834e0584
4 changed files with 109 additions and 28 deletions

View file

@ -70,6 +70,14 @@ namespace NLGUI
{ {
public: public:
/// Watches CInterfaceElement deletions
class IDeletionWatcher
{
public:
IDeletionWatcher(){}
virtual ~IDeletionWatcher(){}
virtual void onDeleted( const std::string &name ){}
};
enum EStrech enum EStrech
{ {
@ -489,6 +497,12 @@ namespace NLGUI
/// Called when the widget is removed from it's parent group /// Called when the widget is removed from it's parent group
virtual void onRemoved(){} virtual void onRemoved(){}
/// Registers a deletion watcher
static void registerDeletionWatcher( IDeletionWatcher *watcher );
/// Unregisters a deletion watcher
static void unregisterDeletionWatcher( IDeletionWatcher *watcher );
protected: protected:
bool editorSelected; bool editorSelected;
@ -549,6 +563,11 @@ namespace NLGUI
void parseSizeRef(const char *sizeRefStr, sint32 &sizeref, sint32 &sizeDivW, sint32 &sizeDivH); void parseSizeRef(const char *sizeRefStr, sint32 &sizeref, sint32 &sizeDivW, sint32 &sizeDivH);
private: private:
/// Notifies the deletion watchers that this interface element is being deleted
void notifyDeletionWatchers();
static std::vector< IDeletionWatcher* > deletionWatchers;
//void snapSize(); //void snapSize();
bool serializable; bool serializable;

View file

@ -32,6 +32,7 @@ using namespace NLMISC;
namespace NLGUI namespace NLGUI
{ {
bool CInterfaceElement::editorMode = false; bool CInterfaceElement::editorMode = false;
std::vector< CInterfaceElement::IDeletionWatcher* > CInterfaceElement::deletionWatchers;
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
CInterfaceElement::~CInterfaceElement() CInterfaceElement::~CInterfaceElement()
@ -44,6 +45,7 @@ namespace NLGUI
} }
delete _Links; delete _Links;
} }
notifyDeletionWatchers();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -1551,6 +1553,36 @@ namespace NLGUI
} }
} }
void CInterfaceElement::registerDeletionWatcher( IDeletionWatcher *watcher )
{
std::vector< IDeletionWatcher* >::iterator itr
= std::find( deletionWatchers.begin(), deletionWatchers.end(), watcher );
// Already registered
if( itr != deletionWatchers.end() )
return;
deletionWatchers.push_back( watcher );
}
void CInterfaceElement::unregisterDeletionWatcher( IDeletionWatcher *watcher )
{
std::vector< IDeletionWatcher* >::iterator itr
= std::find( deletionWatchers.begin(), deletionWatchers.end(), watcher );
// Not registered
if( itr == deletionWatchers.end() )
return;
deletionWatchers.erase( itr );
}
void CInterfaceElement::notifyDeletionWatchers()
{
std::vector< IDeletionWatcher* >::iterator itr = deletionWatchers.begin();
while( itr != deletionWatchers.end() )
{
(*itr)->onDeleted( _Id );
++itr;
}
}
CStringMapper* CStringShared::_UIStringMapper = NULL; CStringMapper* CStringShared::_UIStringMapper = NULL;

View file

@ -56,6 +56,26 @@ namespace
name = s.toUtf8().constData(); name = s.toUtf8().constData();
return name; return name;
} }
class CWidgetDeletionWatcher : public CInterfaceElement::IDeletionWatcher
{
public:
CWidgetDeletionWatcher(){ h = NULL; }
~CWidgetDeletionWatcher(){}
void onDeleted( const std::string &id ){
if( h != NULL )
h->onWidgetDeleted( id );
}
void setWidgetHierarchy( GUIEditor::WidgetHierarchy *h ){ this->h = h; }
private:
GUIEditor::WidgetHierarchy *h;
};
CWidgetDeletionWatcher deletionWatcher;
} }
namespace GUIEditor namespace GUIEditor
@ -66,6 +86,7 @@ namespace GUIEditor
setupUi( this ); setupUi( this );
connect( widgetHT, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ), connect( widgetHT, SIGNAL( itemDoubleClicked( QTreeWidgetItem*, int ) ),
this, SLOT( onItemDblClicked( QTreeWidgetItem* ) ) ); this, SLOT( onItemDblClicked( QTreeWidgetItem* ) ) );
deletionWatcher.setWidgetHierarchy( this );
} }
WidgetHierarchy::~WidgetHierarchy() WidgetHierarchy::~WidgetHierarchy()
@ -74,6 +95,7 @@ namespace GUIEditor
void WidgetHierarchy::clearHierarchy() void WidgetHierarchy::clearHierarchy()
{ {
CInterfaceElement::unregisterDeletionWatcher( &deletionWatcher );
widgetHT->clear(); widgetHT->clear();
widgetHierarchyMap.clear(); widgetHierarchyMap.clear();
} }
@ -81,6 +103,7 @@ namespace GUIEditor
void WidgetHierarchy::buildHierarchy( std::string &masterGroup ) void WidgetHierarchy::buildHierarchy( std::string &masterGroup )
{ {
clearHierarchy(); clearHierarchy();
CInterfaceElement::registerDeletionWatcher( &deletionWatcher );
CInterfaceGroup *mg = CWidgetManager::getInstance()->getMasterGroupFromId( masterGroup ); CInterfaceGroup *mg = CWidgetManager::getInstance()->getMasterGroupFromId( masterGroup );
if( mg != NULL ) if( mg != NULL )
@ -131,6 +154,39 @@ namespace GUIEditor
} }
} }
void WidgetHierarchy::onWidgetDeleted( const std::string &id )
{
std::map< std::string, QTreeWidgetItem* >::iterator itr
= widgetHierarchyMap.find( id );
if( itr == widgetHierarchyMap.end() )
return;
if( widgetHT->currentItem() == itr->second )
{
QTreeWidgetItem *item = itr->second;
QTreeWidgetItem *p = item;
// Deselect item
item->setSelected( false );
widgetHT->setCurrentItem( NULL );
// Collapse the tree
while( p != NULL )
{
p->setExpanded( false );
p = p->parent();
}
currentSelection = "";
}
itr->second->setSelected( false );
delete itr->second;
itr->second = NULL;
widgetHierarchyMap.erase( itr );
}
void WidgetHierarchy::onGUILoaded() void WidgetHierarchy::onGUILoaded()
{ {
if( masterGroup.empty() ) if( masterGroup.empty() )
@ -145,35 +201,7 @@ namespace GUIEditor
return; return;
if( newSelection.empty() ) if( newSelection.empty() )
{
if( widgetHT->currentItem() != NULL )
{
QTreeWidgetItem *item = widgetHT->currentItem();
QTreeWidgetItem *p = item;
// Deselect item
item->setSelected( false );
widgetHT->setCurrentItem( NULL );
// Collapse the tree
while( p != NULL )
{
p->setExpanded( false );
p = p->parent();
}
// Finally remove the item!
delete item;
item = NULL;
std::map< std::string, QTreeWidgetItem* >::iterator itr =
widgetHierarchyMap.find( currentSelection );
if( itr != widgetHierarchyMap.end() )
widgetHierarchyMap.erase( itr );
currentSelection = "";
}
return; return;
}
std::map< std::string, QTreeWidgetItem* >::iterator itr = std::map< std::string, QTreeWidgetItem* >::iterator itr =
widgetHierarchyMap.find( newSelection ); widgetHierarchyMap.find( newSelection );

View file

@ -42,6 +42,8 @@ namespace GUIEditor
void clearHierarchy(); void clearHierarchy();
void buildHierarchy( std::string &masterGroup ); void buildHierarchy( std::string &masterGroup );
void onWidgetDeleted( const std::string &id );
private: private:
void buildHierarchy( QTreeWidgetItem *parent, NLGUI::CInterfaceGroup *group ); void buildHierarchy( QTreeWidgetItem *parent, NLGUI::CInterfaceGroup *group );