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 "AbstractInterface.h"
12 #include "ConnectedInterface.h"
15 AbstractBoxItem:: AbstractBoxItem(AbstractBlock *_refBlock, Dispatcher *_dispatcher, Parameters *_params, QGraphicsItem *parent) : QGraphicsItem(parent) {
16 dispatcher = _dispatcher;
19 QFont fontId("Arial",10);
20 QFontMetrics fmId(fontId);
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;
40 setAcceptHoverEvents(true);
42 // NOTE : initInterfaces() is only called in subclasses
45 AbstractBoxItem::~AbstractBoxItem() {
46 foreach(InterfaceItem* inter, interfaces) {
52 bool AbstractBoxItem::isBoxItem() {
56 bool AbstractBoxItem::isGroupItem() {
60 void AbstractBoxItem::initInterfaces()
62 /* TO DO : creating all needed InterfaceItem, with by default, input at west and output at east */
63 int orientation = Parameters::West;
65 foreach(AbstractInterface *inter, refBlock->getInterfaces()){
66 if(inter->getPurpose() != AbstractInterface::Wishbone){
68 if(inter->getDirection() == AbstractInterface::Input){
69 orientation = Parameters::West;
70 } else if(inter->getDirection() == AbstractInterface::Output){
71 orientation = Parameters::East;
72 } else if(inter->getDirection() == AbstractInterface::InOut){
73 orientation = Parameters::North;
75 item = new InterfaceItem(0.0 , orientation, (ConnectedInterface *)inter, this, params);
76 interfaces.append(item);
81 InterfaceItem* AbstractBoxItem::searchInterfaceByName(QString name) {
82 foreach(InterfaceItem *inter, interfaces){
83 if(inter->getName() == name)
89 InterfaceItem* AbstractBoxItem::searchInterfaceByRef(ConnectedInterface *ref) {
90 foreach(InterfaceItem *inter, interfaces){
91 if(inter->refInter == ref) {
98 void AbstractBoxItem::addInterface(InterfaceItem *i, bool resetPosition) {
100 if (resetPosition) resetInterfacesPosition();
105 void AbstractBoxItem::removeInterface(InterfaceItem *i) {
106 // NB : removing from model is done in dispatcher
107 interfaces.removeOne(i);
110 //resetInterfacesPosition();
116 void AbstractBoxItem::resetInterfacesPosition() {
118 int nbNorth=0, nbSouth=0, nbEast=0, nbWest=0;
119 double cntNorth=1.0,cntSouth=1.0,cntEast=1.0,cntWest=1.0;
120 double positionRatio = 1.0;
123 foreach(InterfaceItem* inter, interfaces) {
124 // only data interfaces and if needed time and reset
125 if(inter->refInter->getPurpose() == AbstractInterface::Data || inter->getOwner()->isRstClkVisible()){
126 if(inter->getOrientation() == Parameters::North){
128 } else if(inter->getOrientation() == Parameters::South){
130 } else if(inter->getOrientation() == Parameters::East){
132 } else if(inter->getOrientation() == Parameters::West){
138 foreach(InterfaceItem* inter, interfaces) {
140 if(inter->refInter->getPurpose() == AbstractInterface::Data || inter->getOwner()->isRstClkVisible()){
142 if(inter->getOrientation() == Parameters::North){
143 positionRatio = cntNorth/(double)(nbNorth+1);
145 } else if(inter->getOrientation() == Parameters::South){
146 positionRatio = cntSouth/(double)(nbSouth+1);
148 } else if(inter->getOrientation() == Parameters::East){
149 positionRatio = cntEast/(double)(nbEast+1);
151 } else if(inter->getOrientation() == Parameters::West){
152 positionRatio = cntWest/(double)(nbWest+1);
155 inter->setPositionRatio(positionRatio);
156 inter->updatePosition();
161 void AbstractBoxItem::deplaceInterface(QPointF pos) {
162 double positionRatio;
163 if(currentInterface->getOrientation() == Parameters::North || currentInterface->getOrientation() == Parameters::South){
166 if(pos.y() > 0 && pos.y() < boxHeight){
167 currentInterface->setOrientation(Parameters::West);
169 } else if(pos.x() > boxWidth){
171 if(pos.y() > 0 && pos.y() < boxHeight){
172 currentInterface->setOrientation(Parameters::East);
175 positionRatio = ((double) pos.x())/boxWidth;
181 if(pos.x() > 0 && pos.x() < boxWidth){
182 currentInterface->setOrientation(Parameters::North);
184 } else if(pos.y() > boxHeight){
186 if(pos.x() > 0 && pos.x() < boxWidth){
187 currentInterface->setOrientation(Parameters::South);
190 positionRatio = ((double) pos.y())/boxHeight;
193 currentInterface->setPositionRatio(positionRatio);
194 currentInterface->updatePosition();
197 QRectF AbstractBoxItem::boundingRect() const {
198 // returns a QRectF that contains the block (i.e the main rectangle, interfaces, title, ...)
199 QPointF p = originPoint - QPointF(nameHeight,nameHeight);
200 QSizeF s(totalWidth+2*nameHeight,totalHeight+2*nameHeight);
205 /* isInterface() : return true if there are some interfaces
206 with the given orientation (N,S,E,O)
208 bool AbstractBoxItem::isInterfaces(int orientation) const {
209 foreach(InterfaceItem* inter, interfaces) {
210 if (inter->getOrientation() == orientation) return true;
215 int AbstractBoxItem::nbInterfacesByOrientation(int orientation) {
217 foreach(InterfaceItem* inter, interfaces) {
218 if ((inter->visible) && (inter->getOrientation() == orientation)) nb++;
223 void AbstractBoxItem::updateInterfacesAndConnections() {
225 // update all interfaces positions
226 foreach(InterfaceItem *item, interfaces){
227 item->updatePosition();
229 if (getScene() != NULL) {
230 // update all connections from/to this block
231 foreach(ConnectionItem *item, getScene()->getConnectionItems()){
232 if ((item->getFromInterfaceItem()->getOwner() == this) || (item->getToInterfaceItem()->getOwner() == this)) {
239 void AbstractBoxItem::setDimension(int x, int y) {
244 InterfaceItem* AbstractBoxItem::getInterfaceFromCursor(qreal x, qreal y) {
246 foreach(InterfaceItem* inter, interfaces) {
247 if(x > inter->boundingRect().x() && x < (inter->boundingRect().x() + inter->boundingRect().width())){
248 if(y > inter->boundingRect().y() && y < (inter->boundingRect().y() + inter->boundingRect().height())){
253 /* TO DO : check each interfaces if it contains x,y. If yes, return that interface */