Expression can now be built.

--HG--
branch : dfighter-tools
This commit is contained in:
dfighter1985 2014-09-17 15:31:54 +02:00
parent cbca49fcc0
commit 5f47f74431
5 changed files with 62 additions and 1 deletions

View file

@ -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 )
{

View file

@ -48,6 +48,7 @@ private Q_SLOTS:
void onChangeSlotCount();
void onChangeValue();
void onSetRoot();
void onBuildExpression();
private:
void addExpression( const ExpressionInfo *info );

View file

@ -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;

View file

@ -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++ )

View file

@ -65,6 +65,8 @@ public:
void setIsValue( bool b ){ m_isValue = b; }
void setRoot( bool b );
QString build() const;
protected:
void mouseMoveEvent( QGraphicsSceneMouseEvent *e );