1 // Ryzom Core Studio - GUI Editor Plugin
3 // Copyright (C) 2010-2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Affero General Public License for more details.
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "expression_node.h"
20 #include "expression_link.h"
22 #include <QStyleOption>
41 // width-height of the box
48 NodeSlot( const NodeSlotInfo
&info
)
59 p
.setX( m_info
.tl
.x() + m_info
.wh
/ 2.0 );
60 p
.setY( m_info
.tl
.y() + m_info
.wh
/ 2.0 );
65 void paint( QPainter
*painter
)
70 boxBrush
.setColor( Qt::black
);
71 boxBrush
.setStyle( Qt::SolidPattern
);
72 p
.setColor( Qt::black
);
78 box
.setTopLeft( m_info
.tl
);
79 box
.setHeight( m_info
.wh
);
80 box
.setWidth( m_info
.wh
);
82 painter
->fillRect( box
, boxBrush
);
84 tbox
.setTopLeft( m_info
.ttl
);
85 tbox
.setHeight( m_info
.th
);
86 tbox
.setWidth( m_info
.tw
);
88 painter
->drawText( tbox
, Qt::AlignRight
, m_info
.text
);
91 QString
text() const{ return m_info
.text
; }
92 void setText( const QString
&text
){ m_info
.text
= text
; }
100 ExpressionNode::ExpressionNode( const QString
&name
, int slotCount
, QGraphicsItem
*parent
) :
101 QGraphicsItem( parent
)
103 setFlags( QGraphicsItem::ItemIsSelectable
| QGraphicsItem::ItemIsMovable
| QGraphicsItem::ItemSendsScenePositionChanges
);
116 m_h
= m_h
+ 20.0 * ( slotCount
- 3 );
118 createSlots( slotCount
);
121 ExpressionNode::~ExpressionNode()
127 QRectF
ExpressionNode::boundingRect() const
129 return QRectF( 0, 0, m_w
, m_h
);
132 void ExpressionNode::paint( QPainter
*painter
, const QStyleOptionGraphicsItem
*option
, QWidget
*widget
)
139 QRectF rect
= boundingRect();
140 QRectF header
= rect
;
141 header
.setHeight( m_hh
);
143 // Draw filled rectangle, header
157 br
.setStyle( Qt::SolidPattern
);
159 painter
->setPen( p
);
160 painter
->fillRect( header
, br
);
163 p
.setColor( Qt::black
);
164 painter
->setPen( p
);
165 painter
->drawText( header
, Qt::AlignCenter
, m_name
);
167 // Draw value if applicable
171 vbox
.setTopLeft( QPoint( 0.0, 20.0 ) );
172 vbox
.setWidth( header
.width() );
173 vbox
.setHeight( header
.height() );
175 vpen
.setColor( Qt::red
);
176 painter
->setPen( vpen
);
177 painter
->drawText( vbox
, Qt::AlignCenter
, m_value
);
178 painter
->setPen( p
);
181 if( option
->state
& QStyle::State_Selected
)
183 p
.setStyle( Qt::DotLine
);
184 p
.setColor( Qt::red
);
187 // Draw outline of the entire thing + header
188 painter
->setPen( p
);
189 painter
->drawRect( rect
);
190 painter
->drawRect( header
);
192 paintSlots( painter
);
196 QPointF
ExpressionNode::slotPos( int slot
) const
198 const NodeSlot
*s
= m_slots
[ slot
];
199 QPointF sp
= s
->pos();
206 void ExpressionNode::changeSlotCount( int count
)
215 m_h
= 100.0 + 20.0 * ( count
- 3 );
217 createSlots( count
);
222 void ExpressionNode::clearSlots()
224 qDeleteAll( m_slots
);
228 bool ExpressionNode::slotEmpty( int slot
) const
230 if( m_links
[ 0 ] == NULL
)
236 void ExpressionNode::getSlots( QList
< SlotInfo
> &l
)
240 for( int i
= 0; i
< m_slots
.count(); i
++ )
242 if( m_links
[ i
] != NULL
)
245 info
.name
= m_slots
[ i
]->text();
251 void ExpressionNode::setLink( ExpressionLink
*link
, int slot
)
253 m_links
[ slot
] = link
;
256 ExpressionLink
* ExpressionNode::link( int slot
) const
258 return m_links
[ slot
];
261 void ExpressionNode::setSlotNames( const QList
< QString
> &l
)
264 for( int i
= 0; i
< c
; i
++ )
266 // "Out" slot is at position 0, so set the names with an offset of 1
267 m_slots
[ i
+ 1 ]->setText( l
[ i
] );
271 void ExpressionNode::setValue( const QString
&value
)
275 int c
= m_value
.count();
279 m_w
= m_w
+ ( 100.0 / 15.0 ) * ( c
- 15.0 );
284 void ExpressionNode::setRoot( bool b
)
290 QString
ExpressionNode::build() const
297 QStringList l
= m_name
.split( ' ' );
300 int c
= m_links
.count();
309 for( int i
= 1; i
< c
; i
++ )
311 ExpressionLink
*link
= m_links
[ i
];
315 ExpressionNode
*node
= NULL
;
317 if( link
->from() == this )
322 result
+= node
->build();
332 QVariant
ExpressionNode::itemChange( GraphicsItemChange change
, const QVariant
&value
)
334 if( change
== ItemScenePositionHasChanged
)
339 return QGraphicsItem::itemChange( change
, value
);
342 void ExpressionNode::onNodeMove()
344 for( int i
= 0; i
< m_links
.count(); i
++ )
346 ExpressionLink
*link
= m_links
[ i
];
354 void ExpressionNode::createSlots( int count
)
357 m_links
.push_back( NULL
);
359 for( int i
= 0; i
< count
; i
++ )
360 m_links
.push_back( NULL
);
362 // First create the "Out" slot
371 qreal ty
= m_h
* 0.5 - 2;
373 info
.tl
= QPoint( x
, y
);
374 info
.ttl
= QPoint( tx
, ty
);
377 m_slots
.push_back( new NodeSlot( info
) );
379 // Then the rest of them
380 for( int i
= 0; i
< count
; i
++ )
384 tx
= x
- 5 - info
.tw
;
387 info
.tl
= QPoint( x
, y
);
388 info
.ttl
= QPoint( tx
, ty
);
389 info
.text
= QString( 'A' + i
);
391 m_slots
.push_back( new NodeSlot( info
) );
395 void ExpressionNode::paintSlots( QPainter
*painter
)
397 for( int i
= 0; i
< m_slots
.count(); i
++ )
399 NodeSlot
*slot
= m_slots
[ i
];
400 slot
->paint( painter
);
405 void ExpressionNode::clearLinks()
407 for( int i
= 0; i
< m_links
.count(); i
++ )
409 ExpressionLink
*link
= m_links
[ i
];