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"
15 int AbstractBoxItem::NoLock = 0;
16 int AbstractBoxItem::Position = 1;
17 int AbstractBoxItem::Dimension = 2;
18 int AbstractBoxItem::Interfaces = 4;
19 int AbstractBoxItem::Names = 8;
21 AbstractBoxItem::AbstractBoxItem(AbstractBlock *_refBlock, Dispatcher *_dispatcher, Parameters *_params, int _lock, QGraphicsItem *parent) : QGraphicsItem(parent) {
22 dispatcher = _dispatcher;
26 QFontMetrics fmId(params->defaultBlockFont);
27 nameWidth = fmId.width(refBlock->getName());
28 nameHeight = fmId.height();
32 // the six following values will be override in subclass constructors
40 originPoint = QPointF(0.0,0.0);
43 currentInterface = NULL;
44 rstClkVisible = false;
45 wishboneVisible = false;
47 setAcceptHoverEvents(true);
49 // NOTE : initInterfaces() is only called in subclasses
52 AbstractBoxItem::AbstractBoxItem(Dispatcher *_dispatcher, Parameters *_params, int _lock, QGraphicsItem* parent) : QGraphicsItem(parent) {
53 dispatcher = _dispatcher;
62 // the six following values will be override in subclass constructors
70 originPoint = QPointF(0.0,0.0);
73 currentInterface = NULL;
74 rstClkVisible = false;
75 wishboneVisible = false;
77 setAcceptHoverEvents(true);
79 // NOTE : initInterfaces() is only called in subclasses
82 AbstractBoxItem::~AbstractBoxItem() {
83 foreach(InterfaceItem* inter, interfaces) {
89 bool AbstractBoxItem::isBoxItem() {
93 bool AbstractBoxItem::isGroupItem() {
97 bool AbstractBoxItem::isSourceItem() {
101 void AbstractBoxItem::setRstClkVisible(bool b) {
103 foreach(InterfaceItem* ifaceItem, interfaces) {
104 if ((ifaceItem->refInter->getPurpose() == AbstractInterface::Clock) ||
105 (ifaceItem->refInter->getPurpose() == AbstractInterface::Reset) ) {
106 ifaceItem->visible = b;
109 resetInterfaceItemsPosition();
110 updateGeometry(InterfaceMove);
112 getScene()->updateConnectionItemsShape();
113 (getScene()->getGroupItem())->updateShape();
117 void AbstractBoxItem::setWishboneVisible(bool b) {
119 foreach(InterfaceItem* ifaceItem, interfaces) {
120 if (ifaceItem->refInter->getPurpose() == AbstractInterface::Wishbone) {
121 ifaceItem->visible = b;
124 resetInterfaceItemsPosition();
125 updateGeometry(InterfaceMove);
127 getScene()->updateConnectionItemsShape();
128 (getScene()->getGroupItem())->updateShape();
131 void AbstractBoxItem::setRefBlock(AbstractBlock* _refBlock) {
132 refBlock = _refBlock;
133 QFontMetrics fmId(params->defaultBlockFont);
134 nameWidth = fmId.width(refBlock->getName());
135 nameHeight = fmId.height();
138 void AbstractBoxItem::createInterfaceItems(Parameters::Direction posInputs, Parameters::Direction posOutputs, Parameters::Direction posBidirs) {
139 /* TO DO : creating all needed InterfaceItem, with by default, input at west and output at east */
140 int orientation = Parameters::West;
142 foreach(AbstractInterface *inter, refBlock->getInterfaces()){
144 /* NB: does not create InterfaceItem for control interfaces.
146 if (inter->getPurpose() != AbstractInterface::Control) {
148 if(inter->getDirection() == AbstractInterface::Input){
149 orientation = posInputs;
150 } else if(inter->getDirection() == AbstractInterface::Output){
151 orientation = posOutputs;
152 } else if(inter->getDirection() == AbstractInterface::InOut){
153 orientation = posBidirs;
155 item = new InterfaceItem(0.0 , orientation, (ConnectedInterface *)inter, this, params);
156 interfaces.append(item);
161 InterfaceItem* AbstractBoxItem::searchInterfaceItemByName(QString name) {
162 foreach(InterfaceItem *inter, interfaces){
163 if(inter->getName() == name)
169 InterfaceItem* AbstractBoxItem::searchInterfaceItemByRef(ConnectedInterface *ref) {
170 foreach(InterfaceItem *inter, interfaces){
171 if(inter->refInter == ref) {
178 void AbstractBoxItem::addInterfaceItem(InterfaceItem *i, bool resetPosition) {
179 interfaces.append(i);
180 if (resetPosition) resetInterfaceItemsPosition();
181 updateGeometry(InterfaceMove);
185 void AbstractBoxItem::removeInterfaceItem(InterfaceItem *i) {
186 // NB : removing from model is done in dispatcher
187 interfaces.removeAll(i);
190 //resetInterfacesPosition();
191 updateGeometry(InterfaceMove);
196 void AbstractBoxItem::resetInterfaceItemsPosition() {
198 int nbNorth=0, nbSouth=0, nbEast=0, nbWest=0;
199 double cntNorth=1.0,cntSouth=1.0,cntEast=1.0,cntWest=1.0;
200 double positionRatio = 1.0;
203 foreach(InterfaceItem* inter, interfaces) {
204 // only data interfaces and if needed time and reset
206 if(inter->getOrientation() == Parameters::North){
208 } else if(inter->getOrientation() == Parameters::South){
210 } else if(inter->getOrientation() == Parameters::East){
212 } else if(inter->getOrientation() == Parameters::West){
218 foreach(InterfaceItem* inter, interfaces) {
222 if(inter->getOrientation() == Parameters::North){
223 positionRatio = cntNorth/(double)(nbNorth+1);
225 } else if(inter->getOrientation() == Parameters::South){
226 positionRatio = cntSouth/(double)(nbSouth+1);
228 } else if(inter->getOrientation() == Parameters::East){
229 positionRatio = cntEast/(double)(nbEast+1);
231 } else if(inter->getOrientation() == Parameters::West){
232 positionRatio = cntWest/(double)(nbWest+1);
235 inter->setPositionRatio(positionRatio);
236 inter->updatePosition();
241 void AbstractBoxItem::moveInterfaceItemTo(QPointF pos) {
242 double positionRatio;
243 if(currentInterface->getOrientation() == Parameters::North || currentInterface->getOrientation() == Parameters::South){
246 if(pos.y() > 0 && pos.y() < boxHeight){
247 currentInterface->setOrientation(Parameters::West);
249 } else if(pos.x() > boxWidth){
251 if(pos.y() > 0 && pos.y() < boxHeight){
252 currentInterface->setOrientation(Parameters::East);
255 positionRatio = ((double) pos.x())/boxWidth;
261 if(pos.x() > 0 && pos.x() < boxWidth){
262 currentInterface->setOrientation(Parameters::North);
264 } else if(pos.y() > boxHeight){
266 if(pos.x() > 0 && pos.x() < boxWidth){
267 currentInterface->setOrientation(Parameters::South);
270 positionRatio = ((double) pos.y())/boxHeight;
273 currentInterface->setPositionRatio(positionRatio);
274 currentInterface->updatePosition();
277 QRectF AbstractBoxItem::boundingRect() const {
278 // returns a QRectF that contains the block (i.e the main rectangle, interfaces, title, ...)
279 QPointF p = originPoint;
280 QSizeF s(totalWidth,totalHeight);
284 QRectF AbstractBoxItem::boundingRectInScene() {
285 /* returns a QRectF in scene coordinates, that contains the block plus
286 a margin of size arrowWidth+arrowLineLength
288 int marginConn = params->arrowLineLength+params->arrowWidth;
290 QPointF posBox = scenePos();
291 posBox.setX(posBox.x()+originPoint.x()-marginConn);
292 posBox.setY(posBox.y()+originPoint.y()-marginConn);
294 QSizeF sizeBox(totalWidth+2*marginConn,totalHeight+2*marginConn);
296 return QRectF(posBox,sizeBox);
300 /* isInterface() : return true if there are some interfaces
301 with the given orientation (N,S,E,O)
303 bool AbstractBoxItem::isInterfaces(int orientation) const {
304 foreach(InterfaceItem* inter, interfaces) {
305 if ((inter->visible) && (inter->getOrientation() == orientation)) return true;
310 int AbstractBoxItem::nbInterfacesByOrientation(int orientation) {
312 foreach(InterfaceItem* inter, interfaces) {
313 if ((inter->visible) && (inter->getOrientation() == orientation)) nb++;
318 void AbstractBoxItem::updateInterfaceAndConnectionItems() {
320 // update all interfaces positions
321 foreach(InterfaceItem *item, interfaces){
322 item->updatePosition();
324 // NB: dunno the utility of this test !!
325 if (getScene() != NULL) {
326 // update all connections from/to this block
327 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
328 if ((item->getFromInterfaceItem()->getOwner() == this) || (item->getToInterfaceItem()->getOwner() == this)) {
335 void AbstractBoxItem::setDimension(int x, int y) {
340 InterfaceItem* AbstractBoxItem::getInterfaceItemFromCursor(qreal x, qreal y) {
342 foreach(InterfaceItem* inter, interfaces) {
343 if(x > inter->boundingRect().x() && x < (inter->boundingRect().x() + inter->boundingRect().width())){
344 if(y > inter->boundingRect().y() && y < (inter->boundingRect().y() + inter->boundingRect().height())){
349 /* TO DO : check each interfaces if it contains x,y. If yes, return that interface */