Allow multiselection.

This commit is contained in:
dfighter1985 2014-10-10 20:25:25 +02:00
parent 490dcc951c
commit 9bc654f136
13 changed files with 132 additions and 61 deletions

View file

@ -15,6 +15,7 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <string> #include <string>
#include <vector>
namespace NLGUI namespace NLGUI
{ {
@ -24,7 +25,7 @@ namespace NLGUI
public: public:
/// Notifies the watcher about the change /// Notifies the watcher about the change
virtual void selectionChanged( std::string &newSelection ) = 0; virtual void selectionChanged() = 0;
}; };
} }

View file

@ -503,8 +503,15 @@ namespace NLGUI
IParser* getParser() const{ return parser; } IParser* getParser() const{ return parser; }
std::string& getCurrentEditorSelection(){ return currentEditorSelection; } /// Retrieves the Id of the currently selected widgets
void setCurrentEditorSelection( const std::string &name ); void getEditorSelection( std::vector< std::string > &selection );
/// Adds the widget with the specified Id to the selected widgets
void selectWidget( const std::string &name );
/// Clears the selection
void clearEditorSelection();
void notifySelectionWatchers(); void notifySelectionWatchers();
void registerSelectionWatcher( IEditorSelectionWatcher *watcher ); void registerSelectionWatcher( IEditorSelectionWatcher *watcher );
void unregisterSelectionWatcher( IEditorSelectionWatcher *watcher ); void unregisterSelectionWatcher( IEditorSelectionWatcher *watcher );
@ -519,6 +526,7 @@ namespace NLGUI
void setGroupSelection( bool b ){ groupSelection = b; } void setGroupSelection( bool b ){ groupSelection = b; }
bool unGroupSelection(); bool unGroupSelection();
void setMultiSelection( bool b ){ multiSelection = b; }
private: private:
CWidgetManager(); CWidgetManager();
@ -611,9 +619,9 @@ namespace NLGUI
std::vector< IEditorSelectionWatcher* > selectionWatchers; std::vector< IEditorSelectionWatcher* > selectionWatchers;
std::vector< IWidgetWatcher* > widgetWatchers; std::vector< IWidgetWatcher* > widgetWatchers;
std::vector< std::string > editorSelection;
std::string currentEditorSelection;
bool groupSelection; bool groupSelection;
bool multiSelection;
}; };
} }

View file

@ -57,7 +57,7 @@ namespace NLGUI
{ {
if( editorMode ) if( editorMode )
{ {
CWidgetManager::getInstance()->setCurrentEditorSelection( getId() ); CWidgetManager::getInstance()->selectWidget( getId() );
return true; return true;
} }
} }

View file

@ -2077,9 +2077,9 @@ namespace NLGUI
if( CInterfaceElement::getEditorMode() ) if( CInterfaceElement::getEditorMode() )
{ {
if( !currentEditorSelection.empty() ) for( int i = 0; i < editorSelection.size(); i++ )
{ {
CInterfaceElement *e = getElementFromId( currentEditorSelection ); CInterfaceElement *e = getElementFromId( editorSelection[ i ] );
if( e != NULL ) if( e != NULL )
e->drawHighlight(); e->drawHighlight();
} }
@ -2478,7 +2478,7 @@ namespace NLGUI
else else
{ {
if( CInterfaceElement::getEditorMode() ) if( CInterfaceElement::getEditorMode() )
setCurrentEditorSelection( "" ); clearEditorSelection();
} }
} }
@ -3349,25 +3349,53 @@ namespace NLGUI
} }
} }
void CWidgetManager::getEditorSelection( std::vector< std::string > &selection )
void CWidgetManager::setCurrentEditorSelection( const std::string &name )
{ {
selection.clear();
for( int i = 0; i < editorSelection.size(); i++ )
selection.push_back( editorSelection[ i ] );
}
void CWidgetManager::selectWidget( const std::string &name )
{
std::vector< std::string >::iterator itr
= std::find( editorSelection.begin(), editorSelection.end(), name );
CInterfaceElement *e = getElementFromId( name ); CInterfaceElement *e = getElementFromId( name );
if( e != NULL )
if( itr != editorSelection.end() )
{ {
if( !currentEditorSelection.empty() ) // If multiselection is on unselect if already selected
if( multiSelection )
{ {
CInterfaceElement *prev = getElementFromId( currentEditorSelection ); editorSelection.erase( itr );
if( prev != NULL ) if( e != NULL )
prev->setEditorSelected( false ); e->setEditorSelected( false );
} }
e->setEditorSelected( true );
} }
else else
if( !name.empty() ) {
return; // Select if not yet selected
if( e != NULL )
currentEditorSelection = name; {
// If multiselection is off, we can only have 1 widget selected
if( !multiSelection )
{
editorSelection.clear();
}
e->setEditorSelected( true );
editorSelection.push_back( name );
}
}
notifySelectionWatchers();
}
void CWidgetManager::clearEditorSelection()
{
editorSelection.clear();
notifySelectionWatchers(); notifySelectionWatchers();
} }
@ -3376,7 +3404,7 @@ namespace NLGUI
std::vector< IEditorSelectionWatcher* >::iterator itr = selectionWatchers.begin(); std::vector< IEditorSelectionWatcher* >::iterator itr = selectionWatchers.begin();
while( itr != selectionWatchers.end() ) while( itr != selectionWatchers.end() )
{ {
(*itr)->selectionChanged( currentEditorSelection ); (*itr)->selectionChanged();
++itr; ++itr;
} }
} }
@ -3484,11 +3512,11 @@ namespace NLGUI
bool CWidgetManager::unGroupSelection() bool CWidgetManager::unGroupSelection()
{ {
if( currentEditorSelection.empty() ) if( editorSelection.size() != 1 )
return false; return false;
// Does the element exist? // Does the element exist?
CInterfaceElement *e = getElementFromId( currentEditorSelection ); CInterfaceElement *e = getElementFromId( editorSelection[ 0 ] );
if( e == NULL ) if( e == NULL )
return false; return false;
@ -3509,7 +3537,7 @@ namespace NLGUI
p->delElement( g ); p->delElement( g );
setCurrentEditorSelection( "" ); clearEditorSelection();
p->updateCoords(); p->updateCoords();
@ -3554,8 +3582,8 @@ namespace NLGUI
setScreenWH( 0, 0 ); setScreenWH( 0, 0 );
currentEditorSelection = "";
groupSelection = false; groupSelection = false;
multiSelection = false;
} }
CWidgetManager::~CWidgetManager() CWidgetManager::~CWidgetManager()
@ -3569,6 +3597,8 @@ namespace NLGUI
curContextHelp = NULL; curContextHelp = NULL;
CStringShared::deleteStringMapper(); CStringShared::deleteStringMapper();
editorSelection.clear();
} }
} }

View file

@ -25,35 +25,40 @@ namespace GUIEditor
{ {
void CEditorMessageProcessor::onDelete() void CEditorMessageProcessor::onDelete()
{ {
std::string selection = CWidgetManager::getInstance()->getCurrentEditorSelection(); std::vector< std::string > selection;
CWidgetManager::getInstance()->getEditorSelection( selection );
if( selection.empty() ) if( selection.empty() )
return; return;
QMessageBox::StandardButton r = QMessageBox::StandardButton r =
QMessageBox::question( NULL, QMessageBox::question( NULL,
tr( "Deleting widget" ), tr( "Deleting widget" ),
tr( "Are you sure you want to delete %1?" ).arg( selection.c_str() ), tr( "Are you sure you want to delete these?" ),
QMessageBox::Yes | QMessageBox::No ); QMessageBox::Yes | QMessageBox::No );
if( r != QMessageBox::Yes ) if( r != QMessageBox::Yes )
return; return;
CInterfaceElement *e = for( int i = 0; i < selection.size(); i++ )
CWidgetManager::getInstance()->getElementFromId( selection );
if( e == NULL )
return;
CInterfaceElement *p = e->getParent();
if( p == NULL )
return;
CInterfaceGroup *g = dynamic_cast< CInterfaceGroup* >( p );
if( g == NULL )
return;
if( g->delElement( e ) )
{ {
CWidgetManager::getInstance()->setCurrentEditorSelection( "" ); CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( selection[ i ] );
if( e == NULL )
continue;
CInterfaceElement *p = e->getParent();
if( p == NULL )
continue;
CInterfaceGroup *g = dynamic_cast< CInterfaceGroup* >( p );
if( g == NULL )
continue;
g->delElement( e );
} }
CWidgetManager::getInstance()->clearEditorSelection();
} }
void CEditorMessageProcessor::onAdd( const QString &parentGroup, const QString &widgetType, const QString &name ) void CEditorMessageProcessor::onAdd( const QString &parentGroup, const QString &widgetType, const QString &name )
@ -147,5 +152,10 @@ namespace GUIEditor
tr( "Couldn't ungroup widgets." ) ); tr( "Couldn't ungroup widgets." ) );
} }
} }
void CEditorMessageProcessor::onSetMultiSelection( bool b )
{
CWidgetManager::getInstance()->setMultiSelection( b );
}
} }

View file

@ -40,6 +40,7 @@ namespace GUIEditor
void onAdd( const QString &parentGroup, const QString &widgetType, const QString &name ); void onAdd( const QString &parentGroup, const QString &widgetType, const QString &name );
void onSetGroupSelection( bool b ); void onSetGroupSelection( bool b );
void onUngroup(); void onUngroup();
void onSetMultiSelection( bool b );
private: private:
CWidgetInfoTree *tree; CWidgetInfoTree *tree;

View file

@ -18,9 +18,9 @@
namespace GUIEditor namespace GUIEditor
{ {
void CEditorSelectionWatcher::selectionChanged( std::string &newSelection ) void CEditorSelectionWatcher::selectionChanged()
{ {
Q_EMIT sgnSelectionChanged( newSelection ); Q_EMIT sgnSelectionChanged();
} }
} }

View file

@ -27,10 +27,10 @@ namespace GUIEditor
public: public:
CEditorSelectionWatcher() : QObject( NULL ){} CEditorSelectionWatcher() : QObject( NULL ){}
void selectionChanged( std::string &newSelection ); void selectionChanged();
Q_SIGNALS: Q_SIGNALS:
void sgnSelectionChanged( std::string &id ); void sgnSelectionChanged();
}; };
} }

View file

@ -283,8 +283,8 @@ namespace GUIEditor
CEditorSelectionWatcher *w = GUICtrl->getWatcher(); CEditorSelectionWatcher *w = GUICtrl->getWatcher();
disconnect( w, SIGNAL( sgnSelectionChanged( std::string& ) ), hierarchyView, SLOT( onSelectionChanged( std::string& ) ) ); disconnect( w, SIGNAL( sgnSelectionChanged() ), hierarchyView, SLOT( onSelectionChanged() ) );
disconnect( w, SIGNAL( sgnSelectionChanged( std::string& ) ), &browserCtrl, SLOT( onSelectionChanged( std::string& ) ) ); disconnect( w, SIGNAL( sgnSelectionChanged() ), &browserCtrl, SLOT( onSelectionChanged() ) );
projectFiles.clearAll(); projectFiles.clearAll();
projectWindow->clear(); projectWindow->clear();
@ -322,8 +322,8 @@ namespace GUIEditor
linkList->onGUILoaded(); linkList->onGUILoaded();
CEditorSelectionWatcher *w = GUICtrl->getWatcher(); CEditorSelectionWatcher *w = GUICtrl->getWatcher();
connect( w, SIGNAL( sgnSelectionChanged( std::string& ) ), hierarchyView, SLOT( onSelectionChanged( std::string& ) ) ); connect( w, SIGNAL( sgnSelectionChanged() ), hierarchyView, SLOT( onSelectionChanged() ) );
connect( w, SIGNAL( sgnSelectionChanged( std::string& ) ), &browserCtrl, SLOT( onSelectionChanged( std::string& ) ) ); connect( w, SIGNAL( sgnSelectionChanged() ), &browserCtrl, SLOT( onSelectionChanged() ) );
} }
void GUIEditorWindow::onAddWidgetClicked() void GUIEditorWindow::onAddWidgetClicked()
@ -413,6 +413,12 @@ namespace GUIEditor
connect( a, SIGNAL( triggered( bool ) ), messageProcessor, SLOT( onSetGroupSelection( bool ) ) ); connect( a, SIGNAL( triggered( bool ) ), messageProcessor, SLOT( onSetGroupSelection( bool ) ) );
m->addAction( a ); m->addAction( a );
a = new QAction( "Multiselect", this );
a->setCheckable( true );
a->setChecked( false );
connect( a, SIGNAL( triggered( bool ) ), messageProcessor, SLOT( onSetMultiSelection( bool ) ) );
m->addAction( a );
menu = m; menu = m;
} }
} }

View file

@ -436,7 +436,7 @@ namespace GUIEditor
browser->clear(); browser->clear();
} }
void CPropBrowserCtrl::onSelectionChanged( std::string &id ) void CPropBrowserCtrl::onSelectionChanged()
{ {
if( browser == NULL ) if( browser == NULL )
return; return;
@ -444,14 +444,20 @@ namespace GUIEditor
disablePropertyWatchers(); disablePropertyWatchers();
browser->clear(); browser->clear();
CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( id ); std::vector< std::string > selection;
CWidgetManager::getInstance()->getEditorSelection( selection );
if( selection.size() != 1 )
return;
CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( selection[ 0 ] );
if( e == NULL ) if( e == NULL )
{ {
enablePropertyWatchers(); enablePropertyWatchers();
return; return;
} }
currentElement = id; currentElement = selection[ 0 ];
std::string n = e->getClassName(); std::string n = e->getClassName();

View file

@ -59,7 +59,7 @@ namespace GUIEditor
void clear(); void clear();
public Q_SLOTS: public Q_SLOTS:
void onSelectionChanged( std::string &id ); void onSelectionChanged();
private Q_SLOTS: private Q_SLOTS:
void onPropertyChanged( QtProperty *prop, const QVariant &v ); void onPropertyChanged( QtProperty *prop, const QVariant &v );

View file

@ -304,14 +304,15 @@ namespace GUIEditor
void WidgetHierarchy::getCurrentGroup( QString &g ) void WidgetHierarchy::getCurrentGroup( QString &g )
{ {
std::string s = CWidgetManager::getInstance()->getCurrentEditorSelection(); std::vector< std::string > selection;
if( s.empty() ) CWidgetManager::getInstance()->getEditorSelection( selection );
if( selection.size() != 1 )
{ {
g = ""; g = "";
return; return;
} }
NLGUI::CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( s ); NLGUI::CInterfaceElement *e = CWidgetManager::getInstance()->getElementFromId( selection[ 0 ] );
if( e == NULL ) if( e == NULL )
{ {
g = ""; g = "";
@ -342,8 +343,16 @@ namespace GUIEditor
currentSelection.clear(); currentSelection.clear();
} }
void WidgetHierarchy::onSelectionChanged( std::string &newSelection ) void WidgetHierarchy::onSelectionChanged()
{ {
std::vector< std::string > selection;
CWidgetManager::getInstance()->getEditorSelection( selection );
if( selection.size() != 1 )
return;
std::string newSelection = selection[ 0 ];
if( newSelection == currentSelection ) if( newSelection == currentSelection )
return; return;
@ -376,6 +385,6 @@ namespace GUIEditor
std::string n = item->text( 0 ).toUtf8().constData(); std::string n = item->text( 0 ).toUtf8().constData();
currentSelection = makeFullName( item, n ); currentSelection = makeFullName( item, n );
CWidgetManager::getInstance()->setCurrentEditorSelection( currentSelection ); CWidgetManager::getInstance()->selectWidget( currentSelection );
} }
} }

View file

@ -56,7 +56,7 @@ namespace GUIEditor
public Q_SLOTS: public Q_SLOTS:
void onGUILoaded(); void onGUILoaded();
void onSelectionChanged( std::string &newSelection ); void onSelectionChanged();
private Q_SLOTS: private Q_SLOTS:
void onItemDblClicked( QTreeWidgetItem *item ); void onItemDblClicked( QTreeWidgetItem *item );