1 #include "AbstractBoxItem.h"
3 #include "Parameters.h"
5 #include "Dispatcher.h"
6 #include "InterfaceItem.h"
7 #include "ConnectionItem.h"
9 #include "AbstractBlock.h"
10 #include "GroupScene.h"
11 #include "GroupItem.h"
12 #include "AbstractInterface.h"
13 #include "ConnectedInterface.h"
16 AbstractBoxItem:: AbstractBoxItem(AbstractBlock *_refBlock, Dispatcher *_dispatcher, Parameters *_params, QGraphicsItem *parent) : QGraphicsItem(parent) {
17 dispatcher = _dispatcher;
20 QFontMetrics fmId(params->defaultBlockFont);
21 nameWidth = fmId.width(refBlock->getName());
22 nameHeight = fmId.height();
26 // the six following values will be override in subclass constructors
34 originPoint = QPointF(0.0,0.0);
37 currentInterface = NULL;
38 rstClkVisible = false;
39 wishboneVisible = false;
41 setAcceptHoverEvents(true);
43 // NOTE : initInterfaces() is only called in subclasses
46 AbstractBoxItem::AbstractBoxItem(Dispatcher *_dispatcher, Parameters *_params, QGraphicsItem* parent) : QGraphicsItem(parent) {
47 dispatcher = _dispatcher;
55 // the six following values will be override in subclass constructors
63 originPoint = QPointF(0.0,0.0);
66 currentInterface = NULL;
67 rstClkVisible = false;
68 wishboneVisible = false;
70 setAcceptHoverEvents(true);
72 // NOTE : initInterfaces() is only called in subclasses
75 AbstractBoxItem::~AbstractBoxItem() {
76 foreach(InterfaceItem* inter, interfaces) {
82 bool AbstractBoxItem::isBoxItem() {
86 bool AbstractBoxItem::isGroupItem() {
89 void AbstractBoxItem::setRstClkVisible(bool b) {
91 foreach(InterfaceItem* ifaceItem, interfaces) {
92 if ((ifaceItem->refInter->getPurpose() == AbstractInterface::Clock) ||
93 (ifaceItem->refInter->getPurpose() == AbstractInterface::Reset) ) {
94 ifaceItem->visible = b;
97 resetInterfacesPosition();
98 updateGeometry(InterfaceMove);
100 getScene()->updateConnectionItemsShape();
101 (getScene()->getGroupItem())->updateShape();
105 void AbstractBoxItem::setWishboneVisible(bool b) {
107 foreach(InterfaceItem* ifaceItem, interfaces) {
108 if (ifaceItem->refInter->getPurpose() == AbstractInterface::Wishbone) {
109 ifaceItem->visible = b;
112 resetInterfacesPosition();
113 updateGeometry(InterfaceMove);
115 getScene()->updateConnectionItemsShape();
116 (getScene()->getGroupItem())->updateShape();
119 void AbstractBoxItem::setRefBlock(AbstractBlock* _refBlock) {
120 refBlock = _refBlock;
121 QFontMetrics fmId(params->defaultBlockFont);
122 nameWidth = fmId.width(refBlock->getName());
123 nameHeight = fmId.height();
126 void AbstractBoxItem::initInterfaces() {
127 /* TO DO : creating all needed InterfaceItem, with by default, input at west and output at east */
128 int orientation = Parameters::West;
130 foreach(AbstractInterface *inter, refBlock->getInterfaces()){
133 if(inter->getDirection() == AbstractInterface::Input){
134 orientation = Parameters::West;
135 } else if(inter->getDirection() == AbstractInterface::Output){
136 orientation = Parameters::East;
137 } else if(inter->getDirection() == AbstractInterface::InOut){
138 orientation = Parameters::North;
140 item = new InterfaceItem(0.0 , orientation, (ConnectedInterface *)inter, this, params);
141 interfaces.append(item);
145 InterfaceItem* AbstractBoxItem::searchInterfaceByName(QString name) {
146 foreach(InterfaceItem *inter, interfaces){
147 if(inter->getName() == name)
153 InterfaceItem* AbstractBoxItem::searchInterfaceByRef(ConnectedInterface *ref) {
154 foreach(InterfaceItem *inter, interfaces){
155 if(inter->refInter == ref) {
162 void AbstractBoxItem::addInterface(InterfaceItem *i, bool resetPosition) {
163 interfaces.append(i);
164 if (resetPosition) resetInterfacesPosition();
165 updateGeometry(InterfaceMove);
169 void AbstractBoxItem::removeInterface(InterfaceItem *i) {
170 // NB : removing from model is done in dispatcher
171 interfaces.removeOne(i);
174 //resetInterfacesPosition();
175 updateGeometry(InterfaceMove);
180 void AbstractBoxItem::resetInterfacesPosition() {
182 int nbNorth=0, nbSouth=0, nbEast=0, nbWest=0;
183 double cntNorth=1.0,cntSouth=1.0,cntEast=1.0,cntWest=1.0;
184 double positionRatio = 1.0;
187 foreach(InterfaceItem* inter, interfaces) {
188 // only data interfaces and if needed time and reset
190 if(inter->getOrientation() == Parameters::North){
192 } else if(inter->getOrientation() == Parameters::South){
194 } else if(inter->getOrientation() == Parameters::East){
196 } else if(inter->getOrientation() == Parameters::West){
202 foreach(InterfaceItem* inter, interfaces) {
206 if(inter->getOrientation() == Parameters::North){
207 positionRatio = cntNorth/(double)(nbNorth+1);
209 } else if(inter->getOrientation() == Parameters::South){
210 positionRatio = cntSouth/(double)(nbSouth+1);
212 } else if(inter->getOrientation() == Parameters::East){
213 positionRatio = cntEast/(double)(nbEast+1);
215 } else if(inter->getOrientation() == Parameters::West){
216 positionRatio = cntWest/(double)(nbWest+1);
219 inter->setPositionRatio(positionRatio);
220 inter->updatePosition();
225 void AbstractBoxItem::moveInterfaceTo(QPointF pos) {
226 double positionRatio;
227 if(currentInterface->getOrientation() == Parameters::North || currentInterface->getOrientation() == Parameters::South){
230 if(pos.y() > 0 && pos.y() < boxHeight){
231 currentInterface->setOrientation(Parameters::West);
233 } else if(pos.x() > boxWidth){
235 if(pos.y() > 0 && pos.y() < boxHeight){
236 currentInterface->setOrientation(Parameters::East);
239 positionRatio = ((double) pos.x())/boxWidth;
245 if(pos.x() > 0 && pos.x() < boxWidth){
246 currentInterface->setOrientation(Parameters::North);
248 } else if(pos.y() > boxHeight){
250 if(pos.x() > 0 && pos.x() < boxWidth){
251 currentInterface->setOrientation(Parameters::South);
254 positionRatio = ((double) pos.y())/boxHeight;
257 currentInterface->setPositionRatio(positionRatio);
258 currentInterface->updatePosition();
261 QRectF AbstractBoxItem::boundingRect() const {
262 // returns a QRectF that contains the block (i.e the main rectangle, interfaces, title, ...)
263 QPointF p = originPoint - QPointF(nameHeight,nameHeight);
264 QSizeF s(totalWidth+2*nameHeight,totalHeight+2*nameHeight);
269 /* isInterface() : return true if there are some interfaces
270 with the given orientation (N,S,E,O)
272 bool AbstractBoxItem::isInterfaces(int orientation) const {
273 foreach(InterfaceItem* inter, interfaces) {
274 if (inter->getOrientation() == orientation) return true;
279 int AbstractBoxItem::nbInterfacesByOrientation(int orientation) {
281 foreach(InterfaceItem* inter, interfaces) {
282 if ((inter->visible) && (inter->getOrientation() == orientation)) nb++;
287 void AbstractBoxItem::updateInterfacesAndConnections() {
289 // update all interfaces positions
290 foreach(InterfaceItem *item, interfaces){
291 item->updatePosition();
293 // NB: dunno the utility of this test !!
294 if (getScene() != NULL) {
295 // update all connections from/to this block
296 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
297 if ((item->getFromInterfaceItem()->getOwner() == this) || (item->getToInterfaceItem()->getOwner() == this)) {
304 void AbstractBoxItem::setDimension(int x, int y) {
309 InterfaceItem* AbstractBoxItem::getInterfaceFromCursor(qreal x, qreal y) {
311 foreach(InterfaceItem* inter, interfaces) {
312 if(x > inter->boundingRect().x() && x < (inter->boundingRect().x() + inter->boundingRect().width())){
313 if(y > inter->boundingRect().y() && y < (inter->boundingRect().y() + inter->boundingRect().height())){
318 /* TO DO : check each interfaces if it contains x,y. If yes, return that interface */