CHANGED: #1471 Little bit of refactoring, moved the parser code from GUIEditorWindow to a new class CWidgetPropParser.

--HG--
branch : gsoc2012-gui-editor
This commit is contained in:
dfighter1985 2012-07-05 08:18:33 +02:00
parent e6229bed94
commit 4d2932ccef
6 changed files with 319 additions and 207 deletions

View file

@ -28,6 +28,7 @@
#include <QtGui/QFileDialog>
#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();
}
}
}

View file

@ -21,6 +21,7 @@
#include <QtGui/QUndoStack>
#include <QXmlStreamReader>
#include <QFile>
#include "widget_info.h"
namespace GUIEditor
{

View file

@ -0,0 +1,54 @@
// Object Viewer Qt GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
// 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 <http://www.gnu.org/licenses/>.
#ifndef WIDGET_INFO_H
#define WIDGET_INFO_H
#include <string>
#include <vector>
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

View file

@ -19,40 +19,13 @@
#define WIDGETPROPS_H
#include "ui_widget_properties.h"
#include "widget_info.h"
#include <map>
#include <vector>
#include <string>
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

View file

@ -0,0 +1,208 @@
// Object Viewer Qt GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
// 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 <http://www.gnu.org/licenses/>.
#include "widget_properties_parser.h"
#include <QDir>
#include <QStringList>
#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();
}
}
}

View file

@ -0,0 +1,49 @@
// Object Viewer Qt GUI Editor plugin <http://dev.ryzom.com/projects/ryzom/>
// 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 <http://www.gnu.org/licenses/>.
#ifndef WIDGET_PROP_PARSER
#define WIDGET_PROP_PARSER
#include "widget_info.h"
#include <map>
#include <QString>
#include <QFile>
#include <QXmlStreamReader>
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