From 545bb65f06e74b891daa90301b43706ecc5675ab Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Thu, 5 Jul 2012 07:22:28 +0200 Subject: [PATCH] CHANGED: #1471 The GUI widget properties are now read from the GUI XML files ( widget inheritance is not yet supported ). --- .../plugins/gui_editor/gui_editor_window.cpp | 182 +++++++++++++++++- .../plugins/gui_editor/gui_editor_window.h | 8 + .../plugins/gui_editor/widget_properties.cpp | 67 ++----- .../plugins/gui_editor/widget_properties.h | 33 ++++ 4 files changed, 237 insertions(+), 53 deletions(-) diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp index 6faca8363..b2b53328d 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.cpp @@ -32,6 +32,7 @@ namespace GUIEditor { QString _lastDir; + std::map< std::string, SWidgetInfo > widgetInfo; GUIEditorWindow::GUIEditorWindow(QWidget *parent) : QMainWindow(parent) @@ -41,6 +42,8 @@ namespace GUIEditor widgetProps = new CWidgetProperties; createMenus(); readSettings(); + parseGUIWidgets(); + widgetProps->setupWidgetInfo( &widgetInfo ); } GUIEditorWindow::~GUIEditorWindow() @@ -97,5 +100,182 @@ namespace GUIEditor settings->endGroup(); settings->sync(); } - + + void GUIEditorWindow::parseGUIWidgets() + { + QDir d( "widgets" ); + if( !d.exists() ) + { + nlwarning( "GUI widgets directory doesn't exist!" ); + return; + } + + QStringList nameFilters; + nameFilters.push_back( "*.xml" ); + + QStringList files = d.entryList( nameFilters, QDir::Files ); + if( files.empty() ) + { + nlwarning( "GUI widgets directory has no files!" ); + return; + } + + QStringListIterator itr( files ); + while( itr.hasNext() ) + parseGUIWidget( "widgets/" + itr.next() ); + } + + void GUIEditorWindow::parseGUIWidget( const QString &file ) + { + QFile f( file ); + if( f.open( QIODevice::ReadOnly ) ) + { + parseGUIWidgetXML( f ); + f.close(); + } + else + nlwarning( QString( "File %1 cannot be opened!" ).arg( file ).toStdString().c_str() ); + } + + void GUIEditorWindow::parseGUIWidgetXML( QFile &file ) + { + QXmlStreamReader reader; + reader.setDevice( &file ); + + reader.readNext(); + if( reader.atEnd() ) + return; + + while( !reader.atEnd() && !( reader.isStartElement() && ( reader.name() == "widget" ) ) ) + reader.readNext(); + if( reader.atEnd() ) + return; + + while( !reader.atEnd() && !( reader.isStartElement() && ( reader.name() == "header" ) ) ) + reader.readNext(); + if( reader.atEnd() ) + return; + + QString name = parseGUIWidgetHeader( reader ); + if( name.isEmpty() ) + { + nlwarning( "malformed XML." ); + return; + } + + while( !reader.atEnd() && !( reader.isStartElement() && ( reader.name() == "properties" ) ) ) + reader.readNext(); + if( reader.atEnd() ) + return; + + parseGUIWidgetProperties( reader, name ); + } + + QString GUIEditorWindow::parseGUIWidgetHeader( QXmlStreamReader &reader ) + { + reader.readNext(); + if( reader.atEnd() ) + return QString( "" ); + + SWidgetInfo info; + + while( !reader.atEnd() && !( reader.isEndElement() && ( reader.name() == "header" ) ) ) + { + if( reader.isStartElement() ) + { + QString key = reader.name().toString(); + QString value = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement ); + + if( !reader.hasError() ) + { + if( key == "name" ) + info.name = value.toStdString(); + else + if( key == "guiname" ) + info.GUIName = value.toStdString(); + else + if( key == "description" ) + info.description = value.toStdString(); + else + if( key == "icon" ) + info.icon == value.toStdString(); + else + if( key == "abstract" ) + { + info.isAbstract = false; + if( value == "true" ) + info.isAbstract = true; + } + else + nlwarning( "Malformed XML." ); + } + } + + reader.readNext(); + } + if( reader.atEnd() ) + return QString( "" ); + if( info.name.empty() ) + return QString( "" ); + + widgetInfo[ info.name.c_str() ] = info; + return QString( info.name.c_str() ); + } + + void GUIEditorWindow::parseGUIWidgetProperties( QXmlStreamReader &reader, const QString &widgetName ) + { + reader.readNext(); + if( reader.atEnd() ) + return; + + std::map< std::string, SWidgetInfo >::iterator itr = + widgetInfo.find( widgetName.toStdString() ); + if( itr == widgetInfo.end() ) + return; + + std::vector< SPropEntry > &v = itr->second.props; + + while( !reader.atEnd() && !( reader.isEndElement() && ( reader.name() == "properties" ) ) ) + { + if( reader.isStartElement() && reader.name() == "property" ) + { + SPropEntry prop; + reader.readNext(); + + while( !reader.atEnd() && !( reader.isEndElement() && ( reader.name() == "property" ) ) ) + { + if( reader.isStartElement() ) + { + QString key = reader.name().toString(); + QString value = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement ); + + if( !reader.hasError() ) + { + if( key == "name" ) + prop.propName = value.toStdString(); + else + if( key == "type" ) + prop.propType = value.toStdString(); + else + if( key == "default" ) + prop.propDefault = value.toStdString(); + else + nlwarning( QString( "Unknown tag %1 within a property" ).arg( key ).toStdString().c_str() ); + + } + else + nlwarning( "Malformed XML." ); + } + + reader.readNext(); + } + if( reader.atEnd() ) + return; + + v.push_back( prop ); + } + + reader.readNext(); + } + } } diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.h index a6883aa2f..8faa6b790 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/gui_editor_window.h @@ -19,6 +19,8 @@ #include "ui_gui_editor_window.h" #include +#include +#include namespace GUIEditor { @@ -49,6 +51,12 @@ private: void writeSettings(); + void parseGUIWidgets(); + void parseGUIWidget( const QString &file ); + void parseGUIWidgetXML( QFile &file ); + QString parseGUIWidgetHeader( QXmlStreamReader &reader ); + void parseGUIWidgetProperties( QXmlStreamReader &reader, const QString &widgetName ); + QUndoStack *m_undoStack; Ui::GUIEditorWindow m_ui; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.cpp index 053f77d20..802dcd591 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.cpp +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.cpp @@ -15,67 +15,30 @@ // along with this program. If not, see . #include "widget_properties.h" -#include -#include - -namespace -{ - struct SPropEntry - { - std::string propName; - std::string propType; - std::string propDefault; - - static SPropEntry create( const char *propname, const char *proptype, const char *propdefault ) - { - SPropEntry entry; - entry.propName = propname; - entry.propType = proptype; - entry.propDefault = propdefault; - return entry; - } - }; - - std::map< std::string, std::vector< SPropEntry > > props; -} namespace GUIEditor{ - CWidgetProperties::CWidgetProperties( QWidget *parent ) : QWidget( parent ) { setupUi( this ); - - widgetList->addItem( QString( "InterfaceElement" ) ); - widgetList->addItem( QString( "CtrlBase" ) ); - - props[ "InterfaceElement" ] = std::vector< SPropEntry >(); - props[ "CtrlBase" ] = std::vector< SPropEntry >(); - - std::map< std::string, std::vector< SPropEntry > >::iterator itr = - props.find( "InterfaceElement" ); - if( itr != props.end() ) - { - itr->second.push_back( SPropEntry::create( "id", "string", "ie" ) ); - itr->second.push_back( SPropEntry::create( "active", "bool", "false" ) ); - } - - itr = props.find( "CtrlBase" ); - if( itr != props.end() ) - { - itr->second.push_back( SPropEntry::create( "on_tooltip", "string", "tooltip" ) ); - itr->second.push_back( SPropEntry::create( "on_tooltip_params", "string", "params" ) ); - } - connect( closeButton, SIGNAL( clicked(bool) ), this, SLOT( hide() ) ); - connect( widgetList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onListSelectionChanged( int ) ) ); - } CWidgetProperties::~CWidgetProperties() { } + void CWidgetProperties::setupWidgetInfo( std::map< std::string, SWidgetInfo > *info ) + { + widgetInfo = info; + for( std::map< std::string, SWidgetInfo >::iterator itr = info->begin(); itr != info->end(); ++itr ){ + widgetList->addItem( itr->first.c_str() ); + } + + onListSelectionChanged( 0 ); + connect( widgetList, SIGNAL( currentRowChanged( int ) ), this, SLOT( onListSelectionChanged( int ) ) ); + } + void CWidgetProperties::onListSelectionChanged( int i ) { if( i >= widgetList->count() ) @@ -87,15 +50,15 @@ namespace GUIEditor{ void CWidgetProperties::setPropsOf( const char *name ) { - std::map< std::string, std::vector< SPropEntry > >::iterator itr = - props.find( name ); + std::map< std::string, SWidgetInfo >::iterator itr = + widgetInfo->find( name ); - if( itr == props.end() ) + if( itr == widgetInfo->end() ) return; widgetPropTree->clear(); - std::vector< SPropEntry > &v = itr->second; + std::vector< SPropEntry > &v = itr->second.props; for( std::vector< SPropEntry >::iterator itr2 = v.begin(); itr2 != v.end(); ++itr2 ) { SPropEntry e = *itr2; diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.h index c691806a2..9346a6df2 100644 --- a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.h +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties.h @@ -19,9 +19,40 @@ #define WIDGETPROPS_H #include "ui_widget_properties.h" +#include +#include +#include namespace GUIEditor { + struct SPropEntry + { + std::string propName; + std::string propType; + std::string propDefault; + + static SPropEntry create( const char *propname, const char *proptype, const char *propdefault ) + { + SPropEntry entry; + entry.propName = propname; + entry.propType = proptype; + entry.propDefault = propdefault; + return entry; + } + }; + + struct SWidgetInfo + { + std::string name; + std::string GUIName; + std::string description; + bool isAbstract; + std::string icon; + + std::vector< SPropEntry > props; + }; + + class CWidgetProperties : public QWidget, public Ui::WidgetProperties { Q_OBJECT @@ -29,12 +60,14 @@ namespace GUIEditor public: CWidgetProperties( QWidget *parent = NULL ); ~CWidgetProperties(); + void setupWidgetInfo( std::map< std::string, SWidgetInfo > *info ); private Q_SLOTS: void onListSelectionChanged( int i ); private: void setPropsOf( const char *name ); + std::map< std::string, SWidgetInfo > *widgetInfo; }; }