From 30eef5ddef4d39b915d41514c019e54214c79dac Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Thu, 5 Jul 2012 08:18:33 +0200 Subject: [PATCH] CHANGED: #1471 Little bit of refactoring, moved the parser code from GUIEditorWindow to a new class CWidgetPropParser. --- .../plugins/gui_editor/gui_editor_window.cpp | 185 +--------------- .../plugins/gui_editor/gui_editor_window.h | 1 + .../src/plugins/gui_editor/widget_info.h | 54 +++++ .../plugins/gui_editor/widget_properties.h | 29 +-- .../gui_editor/widget_properties_parser.cpp | 208 ++++++++++++++++++ .../gui_editor/widget_properties_parser.h | 49 +++++ 6 files changed, 319 insertions(+), 207 deletions(-) create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_info.h create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.cpp create mode 100644 code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.h 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 b2b53328d..832c39fe9 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 @@ -28,6 +28,7 @@ #include #include "widget_properties.h" +#include "widget_properties_parser.h" namespace GUIEditor { @@ -42,7 +43,11 @@ namespace GUIEditor widgetProps = new CWidgetProperties; createMenus(); readSettings(); - parseGUIWidgets(); + + CWidgetPropParser parser; + + parser.setWidgetPropMap( &widgetInfo ); + parser.parseGUIWidgets(); widgetProps->setupWidgetInfo( &widgetInfo ); } @@ -100,182 +105,4 @@ 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 8faa6b790..ee0db991b 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 @@ -21,6 +21,7 @@ #include #include #include +#include "widget_info.h" namespace GUIEditor { diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_info.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_info.h new file mode 100644 index 000000000..3fed78733 --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_info.h @@ -0,0 +1,54 @@ +// Object Viewer Qt GUI Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef WIDGET_INFO_H +#define WIDGET_INFO_H + +#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; + }; +} + +#endif 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 9346a6df2..4ab026401 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,40 +19,13 @@ #define WIDGETPROPS_H #include "ui_widget_properties.h" +#include "widget_info.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 diff --git a/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.cpp b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.cpp new file mode 100644 index 000000000..8f5b64191 --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.cpp @@ -0,0 +1,208 @@ +// Object Viewer Qt GUI Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#include "widget_properties_parser.h" +#include +#include +#include "nel/misc/debug.h" + +using namespace NLMISC; + +namespace GUIEditor +{ + void CWidgetPropParser::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() ); + + widgetInfo = NULL; + } + + void CWidgetPropParser::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 CWidgetPropParser::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 CWidgetPropParser::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 CWidgetPropParser::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/widget_properties_parser.h b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.h new file mode 100644 index 000000000..b3dc403ca --- /dev/null +++ b/code/nel/tools/3d/object_viewer_qt/src/plugins/gui_editor/widget_properties_parser.h @@ -0,0 +1,49 @@ +// Object Viewer Qt GUI Editor plugin +// Copyright (C) 2010 Winch Gate Property Limited +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +#ifndef WIDGET_PROP_PARSER +#define WIDGET_PROP_PARSER + +#include "widget_info.h" +#include +#include +#include +#include + +namespace GUIEditor +{ + /// Parser for the widget properties XML files + class CWidgetPropParser + { + public: + CWidgetPropParser(){} + ~CWidgetPropParser(){} + + void setWidgetPropMap( std::map< std::string, SWidgetInfo > *info ){ widgetInfo = info; } + void parseGUIWidgets(); + + private: + void parseGUIWidget( const QString &file ); + void parseGUIWidgetXML( QFile &file ); + QString parseGUIWidgetHeader( QXmlStreamReader &reader ); + void parseGUIWidgetProperties( QXmlStreamReader &reader, const QString &widgetName ); + + std::map< std::string, SWidgetInfo > *widgetInfo; + }; +} + +#endif