29 #include "graphicscomponentitem.h"
30 #include "sheetview.h"
31 #include "editcomponentwidget.h"
34 #include <QGraphicsSceneContextMenuEvent>
35 #include <QMessageBox>
36 #include <QFontMetrics>
37 #include <QStringList>
39 #include <QStyleOptionGraphicsItem>
42 #include "portdialog.h"
48 markedForDeletion =
false;
50 setAcceptHoverEvents(
true);
52 setFlag(QGraphicsItem::ItemIsMovable);
53 setFlag(QGraphicsItem::ItemIsSelectable);
54 setFlag(QGraphicsItem::ItemSendsScenePositionChanges);
58 componentLabel =
new QGraphicsSimpleTextItem(aac->
getProperty(
"id").toString()+
":"+aac->
getType(),
this);
59 setRect(componentLabel->boundingRect().adjusted(-5,-5,5,5));
62 GraphicsComponentItem::~GraphicsComponentItem()
65 if (markedForDeletion)
69 QMapIterator<QString,GraphicsPortItem*> i(signalPorts);
73 i.value()->markForDeletion();
75 QMapIterator<QString,GraphicsPortItem*> i2(slotPorts);
79 i2.value()->markForDeletion();
86 sheetView->getSheet().removeProperty(component->
getProperty(
"id").toString() +
"-slots");
87 sheetView->getSheet().removeProperty(component->
getProperty(
"id").toString() +
"-signals");
88 sheetView->getSheet().removeProperty(component->
getProperty(
"id").toString() +
"-pos");
94 void GraphicsComponentItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * )
96 setPen(QPen(Qt::darkBlue));
101 void GraphicsComponentItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * )
109 void GraphicsComponentItem::removePort(QString name)
112 slotPorts.remove(name);
113 signalPorts.remove(name);
119 QStringList(slotPorts.keys()).join(
";"));
121 QStringList(signalPorts.keys()).join(
";"));
126 void GraphicsComponentItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* event)
130 QAction* addSlot = menu.addAction(
"Add slot");
131 menu.addAction(
"Add signal");
134 QAction* selectedAction = menu.exec(event->screenPos());
139 bool type = (selectedAction==addSlot);
145 if (pd.result() == QDialog::Accepted)
150 if (slotPorts.contains(pd.getPort()))
152 QMessageBox::information(0,
"Port already added",
153 "The port "+ pd.getPort() +
" has already been added to this component.",
157 this->addSlot(pd.getPort());
161 if (signalPorts.contains(pd.getPort()))
163 QMessageBox::information(0,
"Port already added",
164 "The port "+ pd.getPort() +
" has already been added to this component.",
168 this->addSignal(pd.getPort());
175 void GraphicsComponentItem::addSignal(QString s,
bool updateSheet)
178 item->setType(GraphicsPortItem::Signal);
181 signalPorts[s] = item;
182 item->setPos(rect().width()-5,QFontMetrics(componentLabel->font()).height()+15*(signalPorts.count()-1)+5);
184 QRectF rectangle(rect());
185 rectangle.setHeight(QFontMetrics(componentLabel->font()).height()
186 + 15*( (signalPorts.count()>slotPorts.count())?signalPorts.count():slotPorts.count() )+5);
194 QStringList(signalPorts.keys()).join(
";"));
200 void GraphicsComponentItem::addSlot(QString s,
bool updateSheet)
203 item->setType(GraphicsPortItem::Slot);
207 item->setPos(-5,QFontMetrics(componentLabel->font()).height()+15*(slotPorts.count()-1)+5);
209 QRectF rectangle(rect());
210 rectangle.setHeight(QFontMetrics(componentLabel->font()).height()
211 + 15*( (signalPorts.count()>slotPorts.count())?signalPorts.count():slotPorts.count())+5);
219 QStringList(slotPorts.keys()).join(
";"));
227 if ( change == ItemScenePositionHasChanged && scene() )
229 QMap<QString,GraphicsPortItem*>::iterator i;
230 for (i = slotPorts.begin(); i != slotPorts.end(); ++i)
231 (i.value())->fireUpdate();
232 for (i = signalPorts.begin(); i != signalPorts.end(); ++i)
233 (i.value())->fireUpdate();
238 QString::number(x())+
","+QString::number(y()));
242 if (change == ItemChildRemovedChange)
247 QMap<QString,GraphicsPortItem*>::iterator i;
248 for (i = slotPorts.begin(); i != slotPorts.end(); ++i)
250 i.value()->setPos(-5,QFontMetrics(componentLabel->font()).height()+15*j+5);
251 (i.value())->fireUpdate();
255 for (i = signalPorts.begin(); i != signalPorts.end(); ++i)
257 i.value()->setPos(rect().width()-5,QFontMetrics(componentLabel->font()).height()+15*j+5);
258 (i.value())->fireUpdate();
263 QRectF rectangle(rect());
265 rectangle.setHeight(QFontMetrics(componentLabel->font()).height()
266 + 15*( (signalPorts.count()>slotPorts.count())?signalPorts.count():slotPorts.count())+5);
272 QStringList(slotPorts.keys()).join(
";"));
277 return QGraphicsRectItem::itemChange(change,value);
281 void GraphicsComponentItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
283 QStyleOptionGraphicsItem myOptions(*option);
284 if (myOptions.state & (QStyle::State_Selected))
286 QPen myPen(Qt::darkRed);
289 componentLabel->setPen(QPen(Qt::darkRed));
290 myOptions.state = myOptions.state ^ QStyle::State_Selected ;
295 componentLabel->setPen(Qt::NoPen);
297 QGraphicsRectItem::paint(painter,& myOptions, widget );
302 void GraphicsComponentItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *)
void setProperty(QString name, QString value)
QVariant getProperty(QString name)
Gets a meta-property from this component.
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value)
Class handling the generic description of a component.