Parse expression files, and build the expression tree from the expressions parsed from these files.

This commit is contained in:
dfighter1985 2014-09-16 02:32:10 +02:00
parent 580ded1bd4
commit 48b46dcc8c
9 changed files with 492 additions and 50 deletions

View file

@ -26,14 +26,24 @@
#include "expression_node.h" #include "expression_node.h"
#include "expression_link.h" #include "expression_link.h"
#include "expr_link_dlg.h" #include "expr_link_dlg.h"
#include "expression_store.h"
#include "expression_info.h"
#include <QMessageBox> #include <QMessageBox>
#include <QInputDialog> #include <QInputDialog>
class ExpressionEditorPvt
{
public:
ExpressionStore store;
};
ExpressionEditor::ExpressionEditor( QWidget *parent ) : ExpressionEditor::ExpressionEditor( QWidget *parent ) :
QMainWindow( parent ) QMainWindow( parent )
{ {
m_ui.setupUi( this ); m_ui.setupUi( this );
m_pvt = new ExpressionEditorPvt();
m_selectionCount = 0; m_selectionCount = 0;
@ -48,9 +58,27 @@ QMainWindow( parent )
ExpressionEditor::~ExpressionEditor() ExpressionEditor::~ExpressionEditor()
{ {
delete m_pvt;
m_pvt = NULL;
m_scene = NULL; m_scene = NULL;
} }
void ExpressionEditor::load()
{
m_pvt->store.load();
QList< const ExpressionInfo* > l;
m_pvt->store.getExpressions( l );
QListIterator< const ExpressionInfo* > itr( l );
while( itr.hasNext() )
{
addExpression( itr.next() );
}
l.clear();
}
void ExpressionEditor::contextMenuEvent( QContextMenuEvent *e ) void ExpressionEditor::contextMenuEvent( QContextMenuEvent *e )
{ {
QMenu menu; QMenu menu;
@ -229,3 +257,31 @@ void ExpressionEditor::onChangeSlotCount()
node->changeSlotCount( c ); node->changeSlotCount( c );
} }
void ExpressionEditor::addExpression( const ExpressionInfo *info )
{
QTreeWidgetItem *item = findTopLevelItem( info->category );
if( item == NULL )
{
item = new QTreeWidgetItem();
item->setText( 0, info->category );
m_ui.tree->addTopLevelItem( item );
}
QTreeWidgetItem *citem = new QTreeWidgetItem();
citem->setText( 0, info->name );
item->addChild( citem );
}
QTreeWidgetItem* ExpressionEditor::findTopLevelItem( const QString &text )
{
int c = m_ui.tree->topLevelItemCount();
for( int i = 0; i < c; i++ )
{
QTreeWidgetItem *item = m_ui.tree->topLevelItem( i );
if( item->text( 0 ) == text )
return item;
}
return NULL;
}

View file

@ -23,6 +23,8 @@
#include "ui_expression_editor.h" #include "ui_expression_editor.h"
class QGraphicsScene; class QGraphicsScene;
class ExpressionEditorPvt;
class ExpressionInfo;
class ExpressionEditor : public QMainWindow class ExpressionEditor : public QMainWindow
{ {
@ -31,6 +33,8 @@ public:
ExpressionEditor( QWidget *parent = NULL ); ExpressionEditor( QWidget *parent = NULL );
~ExpressionEditor(); ~ExpressionEditor();
void load();
protected: protected:
void contextMenuEvent( QContextMenuEvent *e ); void contextMenuEvent( QContextMenuEvent *e );
@ -47,12 +51,16 @@ private Q_SLOTS:
void onChangeSlotCount(); void onChangeSlotCount();
private: private:
void addExpression( const ExpressionInfo *info );
QTreeWidgetItem* findTopLevelItem( const QString &text );
Ui::ExpressionEditor m_ui; Ui::ExpressionEditor m_ui;
QGraphicsScene *m_scene; QGraphicsScene *m_scene;
int m_selectionCount; int m_selectionCount;
int m_nodeCount; int m_nodeCount;
ExpressionEditorPvt *m_pvt;
}; };
#endif #endif

View file

@ -44,56 +44,6 @@
<string>Expressions</string> <string>Expressions</string>
</property> </property>
</column> </column>
<item>
<property name="text">
<string>Logical</string>
</property>
<item>
<property name="text">
<string>and</string>
</property>
</item>
<item>
<property name="text">
<string>or</string>
</property>
</item>
</item>
<item>
<property name="text">
<string>Mathematical</string>
</property>
<item>
<property name="text">
<string>add</string>
</property>
</item>
<item>
<property name="text">
<string>sub</string>
</property>
</item>
</item>
<item>
<property name="text">
<string>Value</string>
</property>
<item>
<property name="text">
<string>integer</string>
</property>
</item>
<item>
<property name="text">
<string>string</string>
</property>
</item>
<item>
<property name="text">
<string>boolean</string>
</property>
</item>
</item>
</widget> </widget>
</item> </item>
</layout> </layout>

View file

@ -0,0 +1,34 @@
// Ryzom Core Studio - GUI Editor Plugin
//
// Copyright (C) 2014 Laszlo Kis-Adam
// Copyright (C) 2010 Ryzom Core <http://ryzomcore.org/>
//
// 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 EXPRESSION_INFO
#define EXPRESSION_INFO
#include <QString>
#include <QStringList>
struct ExpressionInfo
{
QString name;
QString category;
bool variable;
QStringList slotNames;
};
#endif

View file

@ -0,0 +1,203 @@
// Ryzom Core Studio - GUI Editor Plugin
//
// Copyright (C) 2014 Laszlo Kis-Adam
// Copyright (C) 2010 Ryzom Core <http://ryzomcore.org/>
//
// 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 "expression_loader.h"
#include "expression_info.h"
#include <QFile>
#include <QXmlStreamReader>
class ExpressionLoaderPvt
{
public:
bool parseName()
{
QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
if( reader.hasError() )
return false;
m_info->name = text;
return true;
}
bool parseCategory()
{
QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
if( reader.hasError() )
return false;
m_info->category = text;
return true;
}
bool parseVariable()
{
QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
if( reader.hasError() )
return false;
if( text.toLower() == "true" )
m_info->variable = true;
else
m_info->variable = false;
return true;
}
bool parseSlot()
{
QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
if( reader.hasError() )
return false;
m_info->slotNames.push_back( text );
return true;
}
bool parseSlots()
{
bool error = false;
while( !reader.atEnd() )
{
reader.readNext();
if( reader.isStartElement() )
{
QString name = reader.name().toString();
if( name == "slot" )
error = !parseSlot();
}
else
if( reader.isEndElement() )
{
if( reader.name() == "slots" )
break;
}
}
if( reader.atEnd() )
return false;
return true;
}
bool load( QFile *f )
{
reader.clear();
reader.setDevice( f );
bool error = false;
// start of document
reader.readNext();
if( reader.atEnd() )
return false;
// root node
reader.readNext();
if( reader.atEnd() )
return false;
if( reader.isStartElement() )
{
// Not an expression file?
if( reader.name() != "expression" )
return false;
}
while( !reader.atEnd() )
{
reader.readNext();
if( reader.isStartElement() )
{
QString name = reader.name().toString();
if( name == "name" )
error = !parseName();
else
if( name == "category" )
error = !parseCategory();
else
if( name == "variable" )
error = !parseVariable();
else
if( name == "slots" )
error = !parseSlots();
}
if( error )
break;
}
if( error || reader.hasError() )
{
return false;
}
return true;
}
void setInfo( ExpressionInfo *info ){ m_info = info; }
private:
QXmlStreamReader reader;
ExpressionInfo *m_info;
};
ExpressionLoader::ExpressionLoader()
{
m_pvt = new ExpressionLoaderPvt;
}
ExpressionLoader::~ExpressionLoader()
{
delete m_pvt;
m_pvt = NULL;
}
ExpressionInfo* ExpressionLoader::load( const QString &filename )
{
ExpressionInfo *info = NULL;
bool ok = false;
QFile f( filename );
if( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
return NULL;
info = new ExpressionInfo();
m_pvt->setInfo( info );
ok = m_pvt->load( &f );
f.close();
if( !ok )
{
delete info;
info = NULL;
}
return info;
}

View file

@ -0,0 +1,39 @@
// Ryzom Core Studio - GUI Editor Plugin
//
// Copyright (C) 2014 Laszlo Kis-Adam
// Copyright (C) 2010 Ryzom Core <http://ryzomcore.org/>
//
// 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 EXPRESSION_LOADER
#define EXPRESSION_LOADER
struct ExpressionInfo;
class QString;
class ExpressionLoaderPvt;
class ExpressionLoader
{
public:
ExpressionLoader();
~ExpressionLoader();
ExpressionInfo* load( const QString &filename );
private:
ExpressionLoaderPvt *m_pvt;
};
#endif

View file

@ -0,0 +1,105 @@
// Ryzom Core Studio - GUI Editor Plugin
//
// Copyright (C) 2014 Laszlo Kis-Adam
// Copyright (C) 2010 Ryzom Core <http://ryzomcore.org/>
//
// 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 "expression_store.h"
#include "expression_info.h"
#include "expression_loader.h"
#include <QMap>
#include <QDir>
class ExpressionStorePvt
{
public:
~ExpressionStorePvt()
{
qDeleteAll( expressions );
expressions.clear();
}
QMap< QString, ExpressionInfo* > expressions;
};
ExpressionStore::ExpressionStore()
{
m_pvt = new ExpressionStorePvt();
}
ExpressionStore::~ExpressionStore()
{
delete m_pvt;
m_pvt = NULL;
}
bool ExpressionStore::load()
{
QDir d( "expressions" );
if( !d.exists() )
return false;
QFileInfoList l = d.entryInfoList();
QListIterator< QFileInfo > itr( l );
if( !itr.hasNext() )
return false;
ExpressionLoader loader;
while( itr.hasNext() )
{
const QFileInfo &info = itr.next();
if( !info.isFile() )
continue;
if( info.suffix() != "xml" )
continue;
ExpressionInfo *expr = loader.load( info.absoluteFilePath() );
if( expr == NULL )
continue;
m_pvt->expressions[ expr->name ] = expr;
}
return false;
}
void ExpressionStore::getExpressions( QList< const ExpressionInfo* > &l ) const
{
l.clear();
QMap< QString, ExpressionInfo* >::const_iterator itr = m_pvt->expressions.constBegin();
while( itr != m_pvt->expressions.constEnd() )
{
l.push_back( itr.value() );
++itr;
}
}
const ExpressionInfo* ExpressionStore::getInfo( const QString &name )
{
QMap< QString, ExpressionInfo* >::const_iterator itr = m_pvt->expressions.find( name );
if( itr == m_pvt->expressions.end() )
return NULL;
else
return itr.value();
}

View file

@ -0,0 +1,46 @@
// Ryzom Core Studio - GUI Editor Plugin
//
// Copyright (C) 2014 Laszlo Kis-Adam
// Copyright (C) 2010 Ryzom Core <http://ryzomcore.org/>
//
// 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 EXPRESSION_STORE
#define EXPRESSION_STORE
#include <QString>
#include <QList>
#include "expression_info.h"
//struct ExpressionInfo;
class ExpressionStorePvt;
class ExpressionStore
{
public:
ExpressionStore();
~ExpressionStore();
bool load();
void getExpressions( QList< const ExpressionInfo* > &l ) const;
const ExpressionInfo* getInfo( const QString &name );
private:
ExpressionStorePvt *m_pvt;
};
#endif

View file

@ -75,6 +75,7 @@ namespace GUIEditor
tc = new TextureChooser(); tc = new TextureChooser();
ee = new ExpressionEditor(); ee = new ExpressionEditor();
ee->load();
createMenus(); createMenus();
readSettings(); readSettings();