CHANGED: #1471 The GUI widget properties are now read from the GUI XML files ( widget inheritance is not yet supported ).

This commit is contained in:
dfighter1985 2012-07-05 07:22:28 +02:00
parent fafbebf10b
commit 545bb65f06
4 changed files with 237 additions and 53 deletions

View file

@ -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();
}
}
}

View file

@ -19,6 +19,8 @@
#include "ui_gui_editor_window.h"
#include <QtGui/QUndoStack>
#include <QXmlStreamReader>
#include <QFile>
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;

View file

@ -15,67 +15,30 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "widget_properties.h"
#include <map>
#include <vector>
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;

View file

@ -19,9 +19,40 @@
#define WIDGETPROPS_H
#include "ui_widget_properties.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
@ -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;
};
}