From ec657d4207dff6678520268a224537af51524c73 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Wed, 17 Sep 2014 15:31:54 +0200 Subject: [PATCH] Expression can now be built. --- .../plugins/gui_editor/expression_editor.cpp | 22 +++++++++++- .../plugins/gui_editor/expression_editor.h | 1 + .../src/plugins/gui_editor/expression_link.h | 3 ++ .../plugins/gui_editor/expression_node.cpp | 35 +++++++++++++++++++ .../src/plugins/gui_editor/expression_node.h | 2 ++ 5 files changed, 62 insertions(+), 1 deletion(-) diff --git a/code/studio/src/plugins/gui_editor/expression_editor.cpp b/code/studio/src/plugins/gui_editor/expression_editor.cpp index 0967b33e1..a1c35e892 100644 --- a/code/studio/src/plugins/gui_editor/expression_editor.cpp +++ b/code/studio/src/plugins/gui_editor/expression_editor.cpp @@ -89,7 +89,6 @@ void ExpressionEditor::load() void ExpressionEditor::contextMenuEvent( QContextMenuEvent *e ) { QMenu menu; - QAction *a = NULL; if( m_selectionCount > 0 ) @@ -129,6 +128,11 @@ void ExpressionEditor::contextMenuEvent( QContextMenuEvent *e ) a = menu.addAction( "Unlink" ); connect( a, SIGNAL( triggered() ), this, SLOT( onUnLinkItems() ) ); } + else + { + a = menu.addAction( "Build expression" ); + connect( a, SIGNAL( triggered() ), this, SLOT( onBuildExpression() ) ); + } menu.exec( e->globalPos() ); } @@ -295,6 +299,22 @@ void ExpressionEditor::onSetRoot() node->setRoot( true ); } +void ExpressionEditor::onBuildExpression() +{ + if( m_pvt->m_root == NULL ) + { + QMessageBox::information( this, + tr( "Building expression" ), + tr( "Failed to build expression: You must set a root node." ) ); + return; + } + + QString result = m_pvt->m_root->build(); + QMessageBox::information( this, + tr( "Building expression" ), + tr( "The result is\n" ) + result ); +} + void ExpressionEditor::addExpression( const ExpressionInfo *info ) { diff --git a/code/studio/src/plugins/gui_editor/expression_editor.h b/code/studio/src/plugins/gui_editor/expression_editor.h index 448ac536c..700813618 100644 --- a/code/studio/src/plugins/gui_editor/expression_editor.h +++ b/code/studio/src/plugins/gui_editor/expression_editor.h @@ -48,6 +48,7 @@ private Q_SLOTS: void onChangeSlotCount(); void onChangeValue(); void onSetRoot(); + void onBuildExpression(); private: void addExpression( const ExpressionInfo *info ); diff --git a/code/studio/src/plugins/gui_editor/expression_link.h b/code/studio/src/plugins/gui_editor/expression_link.h index a5235737c..4e6df90dd 100644 --- a/code/studio/src/plugins/gui_editor/expression_link.h +++ b/code/studio/src/plugins/gui_editor/expression_link.h @@ -36,6 +36,9 @@ public: void paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget ); + ExpressionNode* from() const{ return m_from; } + ExpressionNode* to() const{ return m_to; } + private: ExpressionNode *m_from; ExpressionNode *m_to; diff --git a/code/studio/src/plugins/gui_editor/expression_node.cpp b/code/studio/src/plugins/gui_editor/expression_node.cpp index 17343c11c..8316d8901 100644 --- a/code/studio/src/plugins/gui_editor/expression_node.cpp +++ b/code/studio/src/plugins/gui_editor/expression_node.cpp @@ -286,6 +286,41 @@ void ExpressionNode::setRoot( bool b ) update(); } +QString ExpressionNode::build() const +{ + QString result; + + if( isValue() ) + return m_value; + + QStringList l = m_name.split( ' ' ); + result = l[ 0 ]; + result += "( "; + + int c = m_links.count(); + for( int i = 1; i < c; i++ ) + { + ExpressionLink *link = m_links[ i ]; + if( link == NULL ) + continue; + + ExpressionNode *node = NULL; + + if( link->from() == this ) + node = link->to(); + else + node = link->from(); + + result += node->build(); + + if( i != ( c - 1 ) ) + result += ", "; + } + + result += " )"; + return result; +} + void ExpressionNode::mouseMoveEvent( QGraphicsSceneMouseEvent *e ) { for( int i = 0; i < m_links.count(); i++ ) diff --git a/code/studio/src/plugins/gui_editor/expression_node.h b/code/studio/src/plugins/gui_editor/expression_node.h index ec1fb9c67..101a6ebf8 100644 --- a/code/studio/src/plugins/gui_editor/expression_node.h +++ b/code/studio/src/plugins/gui_editor/expression_node.h @@ -65,6 +65,8 @@ public: void setIsValue( bool b ){ m_isValue = b; } void setRoot( bool b ); + QString build() const; + protected: void mouseMoveEvent( QGraphicsSceneMouseEvent *e );