1 #include "Dispatcher.h"
2 #include "Parameters.h"
3 #include "MainWindow.h"
6 #include "ReferenceBlock.h"
7 #include "GroupBlock.h"
8 #include "FunctionalBlock.h"
10 #include "ConnectedInterface.h"
11 #include "ReferenceInterface.h"
12 #include "GroupInterface.h"
13 #include "FunctionalInterface.h"
15 #include "GroupWidget.h"
16 #include "GroupScene.h"
17 #include "GroupItem.h"
19 #include "InterfaceItem.h"
20 #include "ConnectionItem.h"
22 #include "BlockLibraryWidget.h"
23 #include "BlockLibraryTree.h"
25 #include "InterfacePropertiesWindow.h"
27 int Dispatcher::sceneCounter = 0;
29 Dispatcher::Dispatcher(Parameters* _params, MainWindow* _window) {
32 params->setDispatcher(this);
37 GroupWidget *Dispatcher::loadProject(const QString& filename) {
41 root = params->openProjectFile(filename);
43 catch(Exception err) {
48 // creating the top widget/scene
49 topGroup = new GroupWidget(NULL,this,params);
50 currentGroup = topGroup;
51 // getting the newly created scene
52 GroupScene *scene = topGroup->getScene();
54 params->setTopScene(scene);
55 params->setCurrentScene(scene);
58 topGroup = params->loadProject(root);
61 cerr << qPrintable(e.getDefaultMessage()) << endl;
62 cerr << "Aborting ..." << endl;
63 // TO DO : deleteting topGroup and all
67 groupList.append(topGroup);
71 void Dispatcher::closeCurrentProject() {
73 foreach(GroupWidget* win, groupList) {
77 params->destroyGraph();
83 bool Dispatcher::connect(InterfaceItem *iface1, InterfaceItem *iface2) {
85 ConnectedInterface* ref1 = iface1->refInter;
86 ConnectedInterface* ref2 = iface2->refInter;
87 // connect both interface
92 if (ref1->canConnectTo(ref2)) {
93 ok1 = ref1->connectTo(ref2);
94 ok1 = ok1 & ref2->connectFrom(ref1);
96 if (ref2->canConnectTo(ref1)) {
97 ok2 = ref2->connectTo(ref1);
98 ok2 = ok2 & ref1->connectFrom(ref2);
100 if ((ok1 == true) || (ok2 == true)) {
102 iface1->getOwner()->getScene()->createConnectionItem(iface1,iface2);
105 params->unsaveModif = true;
111 void Dispatcher::checkSelection(){
112 InterfaceItem *iface1 = NULL;
113 InterfaceItem *iface2 = NULL;
115 GroupScene *scene = params->getCurrentScene();
116 QList<AbstractBoxItem*> list = scene->getGroupAndBlocks();
117 foreach(AbstractBoxItem *block, list){
118 InterfaceItem *tmp = block->getCurrentInterface();
120 if (iface1 == NULL) {
128 if(iface1 != NULL && iface2 != NULL){
129 connect(iface1,iface2);
133 void Dispatcher::unselectAllItems(int direction){
135 GroupScene *scene = params->getCurrentScene();
137 foreach(BoxItem* block, scene->getBlockItems()) {
138 block->setSelected(false);
139 block->setCurrentInterface(NULL);
141 scene->unselecteInterfaces();
145 void Dispatcher::setCurrentGroupWidget(GroupWidget *win){
147 win->changeConnectionMode(-1);
149 params->setCurrentScene(win->getScene());
152 void Dispatcher::changeConnectionMode(int mode){
155 foreach(GroupWidget* win, groupList){
157 QToolButton* buttonNewConnection = win->getButtonNewConnection();
159 QPalette pal = buttonNewConnection->palette();
162 if(params->sceneMode != Parameters::EditOnConnection){
163 params->sceneMode = Parameters::EditOnConnection;
164 pal.setColor(QPalette::Button, QColor(Qt::lightGray));
166 params->sceneMode = Parameters::EditNoOperation;
167 pal.setColor(QPalette::Button, QColor("#edeceb"));
170 else if(mode == Parameters::EditOnConnection){
171 params->sceneMode = Parameters::EditOnConnection;
172 pal.setColor(QPalette::Button, QColor(Qt::lightGray));
175 params->sceneMode = Parameters::EditNoOperation;
176 pal.setColor(QPalette::Button, QColor("#edeceb"));
178 unselectAllInterfaces();
180 buttonNewConnection->setAutoFillBackground(true);
181 buttonNewConnection->setPalette(pal);
182 buttonNewConnection->update();
187 void Dispatcher::renameBlockOrGroup(AbstractBoxItem *item){
188 static QString fctName = "Dispatcher::renameBlockOrGroup()";
190 cout << "call to " << qPrintable(fctName) << endl;
194 QString text = QInputDialog::getText(NULL, "Rename an block/group",
195 "New name:", QLineEdit::Normal,
196 item->getRefBlock()->getName(), &ok);
199 if(!text.isEmpty() && text.length() < 30){
200 item->getRefBlock()->setName(text);
201 if(item->isGroupItem()){
202 if (currentGroup->isTopGroup()) {
203 mainWindow->setWindowTitle("blast - "+text);
206 currentGroup->setWindowTitle("blast - "+text);
210 mainWindow->getLibrary()->updateComboScene();
213 QMessageBox::warning(NULL,"Error in given name",
214 "the element name must be shorter than 30 characters and can't be empty!",
216 renameBlockOrGroup(item);
221 void Dispatcher::renameInterface(InterfaceItem *item) {
222 static QString fctName = "Dispatcher::renameInterface()";
224 cout << "call to " << qPrintable(fctName) << endl;
230 text = QInputDialog::getText(NULL, "Rename an interface",
231 "New name:", QLineEdit::Normal,
232 item->refInter->getName(), &ok);
236 if (text == item->refInter->getName()) return;
238 if( (text.isEmpty()) || (text.length() > 30)) {
239 QMessageBox::warning(NULL,"Error in given name",
240 "the interface name must be shorter than 30 characters, cannot be empty",
245 AbstractInterface* iface = item->refInter->getOwner()->getIfaceFromName(text);
247 QMessageBox::warning(NULL,"Error in given name",
248 "the name provided is similar to that of another interface",
254 item->refInter->setName(text);
255 AbstractInterface* assoIface = item->refInter->getAssociatedIface();
256 if (assoIface != NULL) {
257 assoIface->setName(text+"_ctl");
259 item->updateName(text);
260 item->getOwner()->interfaceRenamed();
263 void Dispatcher::duplicateBlock(BoxItem *item){
264 static QString fctName = "Dispatcher::duplicateBlock()";
266 cout << "call to " << qPrintable(fctName) << endl;
269 GroupScene *scene = item->getScene();
270 AbstractBlock* block = item->getRefBlock();
271 AbstractBlock *newBlock;
273 // only duplicate functional blocks
274 if(block->isFunctionalBlock()) {
276 // adding to the model
277 FunctionalBlock* funBlock = (FunctionalBlock*)block;
278 newBlock = params->duplicateFunctionalBlock(funBlock);
279 // adding to the view
280 scene->createBlockItem(newBlock);
282 params->unsaveModif = true;
286 void Dispatcher::duplicateInterface(InterfaceItem *item) {
287 static QString fctName = "Dispatcher::duplicateInterface()";
289 cout << "call to " << qPrintable(fctName) << endl;
292 AbstractInterface *refI = item->refInter;
293 if (! refI->isFunctionalInterface()) return;
295 AbstractBlock *refB = refI->getOwner();
296 if(! refB->isFunctionalBlock()) return;
298 FunctionalInterface* iface = (FunctionalInterface*)refI;
299 AbstractInterface *cloneIface = iface->clone();
300 if (cloneIface == NULL) {
301 QMessageBox::warning(NULL,"Error while cloning an interface","the interface cannot be cloned because its maximum multiplicity is reached", QMessageBox::Ok);
305 refB->addInterface(cloneIface);
307 InterfaceItem *cloneIfaceItem = new InterfaceItem(item->getPosition(),item->getOrientation(),(ConnectedInterface*)cloneIface,item->getOwner(),params);
308 item->getOwner()->addInterface(cloneIfaceItem,true);
310 // creating control interface if needed
311 if (refI->getAssociatedIface() != NULL) {
312 QString ctlName = cloneIface->getName()+"_ctl";
313 ReferenceInterface* ctlIface = new ReferenceInterface(refB,ctlName,"boolean","1",cloneIface->getDirection(), AbstractInterface::Control, 1);
314 refB->addInterface(ctlIface);
315 if (! ctlIface->setAssociatedIface(cloneIface)) {
316 cerr << "Abnormal case while cloning an interface and creating its associated control interface" << endl;
322 void Dispatcher::addBlock(int idCategory, int idBlock, int idScene) {
323 static QString fctName = "Dispatcher::addBlock()";
325 cout << "call to " << qPrintable(fctName) << endl;
328 GroupScene *scene = searchSceneById(idScene);
329 ReferenceBlock* ref = params->getReferenceBlock(idCategory,idBlock);
330 GroupBlock* group = AB_TO_GRP(scene->getGroupItem()->getRefBlock());
331 FunctionalBlock* newOne = params->getGraph()->addFunctionalBlock(group, ref);
332 scene->createBlockItem(newOne);
333 params->unsaveModif = true;
337 GroupWidget *Dispatcher::createTopScene(){
338 static QString fctName = "Dispatcher::createTopScene()";
340 cout << "call to " << qPrintable(fctName) << endl;
343 // creating the model part of the group
344 Graph* graph = params->createGraph();
345 GroupBlock *refBlock = graph->getTopGroup();
347 // creating a fake and not connected interface
348 //AbstractInterface* iface = new GroupInterface(refBlock,"grp_iface",AbstractInterface::Input,AbstractInterface::Top);
350 // creating the group widget
351 topGroup = new GroupWidget(NULL,this,params);
352 currentGroup = topGroup;
353 // getting the newly created scene
354 GroupScene *scene = topGroup->getScene();
355 scene->setId(sceneCounter++);
356 params->setTopScene(scene);
357 params->setCurrentScene(scene);
358 // creating the view part of the group
359 GroupItem *group = new GroupItem(NULL,refBlock,this,params);
361 // adding the fake interface to the top group item
362 //InterfaceItem* item = new InterfaceItem(0.0 , Parameters::West, (ConnectedInterface*)iface, group, params);
363 //group->addInterface(item,true);
365 scene->setGroupItem(group);
367 groupList.append(topGroup);
371 GroupWidget* Dispatcher::addNewEmptyGroup(GroupScene* scene, bool show) {
372 static QString fctName = "Dispatcher::addNewEmptyGroup();";
374 cout << "call to " << qPrintable(fctName) << endl;
377 // getting the parent block in the graph
378 GroupBlock* parent = AB_TO_GRP(scene->getGroupItem()->getRefBlock());
379 cout << "new group : parent = "<< qPrintable(parent->getName()) << endl;
380 GroupBlock* groupBlock = params->getGraph()->createChildBlock(parent);
381 cout << "new group : child = "<< qPrintable(groupBlock->getName()) << ", child of " << qPrintable(groupBlock->getParent()->getName()) << endl;
382 // creating the BlockItem in the scene
383 BoxItem* newItem = scene->createBlockItem(groupBlock);
385 params->unsaveModif = true;
387 GroupWidget* child = createChildScene(scene->getGroupWidget(),newItem);
388 if (show) child->show();
393 GroupWidget *Dispatcher::createChildScene(GroupWidget* parentWidget, BoxItem *upperItemOfGroupItem) {
394 static QString fctName = "Dispatcher::createChildScene()";
396 cout << "call to " << qPrintable(fctName) << endl;
399 GroupWidget* group = NULL;
400 /* NB: this method may be called during design process or when loading
401 a project. In this case, upperItemOfGroupItem is NULL, thus a lot of things
402 cannot be initialized yet. This is why there are 2 cases below
405 if (upperItemOfGroupItem != NULL) {
406 // getting back the goup block already created
407 GroupBlock* groupBlock = AB_TO_GRP(upperItemOfGroupItem->getRefBlock());
408 // creating the view part of the group
409 GroupItem *groupItem = new GroupItem(upperItemOfGroupItem,groupBlock,this,params);
410 // creating the group widget
411 group = new GroupWidget(parentWidget, this, params);
412 // getting the newly created scene
413 GroupScene *scene = group->getScene();
414 scene->setId(sceneCounter++);
415 // affecting group item to the scene
416 scene->setGroupItem(groupItem);
417 groupList.append(group);
419 mainWindow->getLibrary()->updateComboScene();
422 GroupItem *groupItem = new GroupItem(this,params);
423 // creating the group widget
424 group = new GroupWidget(parentWidget, this, params);
425 // getting the newly created scene
426 GroupScene *scene = group->getScene();
427 // affecting group item to the scene
428 scene->setGroupItem(groupItem);
429 groupList.append(group);
434 void Dispatcher::destroyScene(GroupScene *scene) {
435 foreach(GroupScene* s, scene->getChildrenScene()) {
439 if (scene->getNbChildScene() == 0) {
440 // remove scene from the parent list
441 scene->getParentScene()->removeChildScene(scene);
442 // destroy the GroupWidget
443 groupList.removeAll(scene->getGroupWidget());
444 scene->getGroupWidget()->deleteLater();
447 cerr << "Abnormal case when destroying a scene" << endl;
451 void Dispatcher::showRaiseWindow(BoxItem *item) {
452 static QString fctName = "Dispatcher::showRaiseWindow()";
454 cout << "call to " << qPrintable(fctName) << endl;
457 cout << "raising child scene of " << qPrintable(item->getRefBlock()->getName()) << endl;
458 GroupItem* child = item->getChildGroupItem();
460 cerr << "abnormal case: child group item is null " << endl;
464 GroupWidget* win = child->getScene()->getGroupWidget();
468 win->activateWindow();
471 params->setCurrentScene(currentGroup->getScene());
474 void Dispatcher::showRstClkIface(AbstractBoxItem *item) {
475 static QString fctName = "Dispatcher::showRstClkIface()";
477 cout << "call to " << qPrintable(fctName) << endl;
480 item->setRstClkVisible(!item->isRstClkVisible());
484 void Dispatcher::showWishboneIface(AbstractBoxItem *item) {
485 static QString fctName = "Dispatcher::showWishboneIface()";
487 cout << "call to " << qPrintable(fctName) << endl;
490 item->setWishboneVisible(!item->isWishboneVisible());
493 void Dispatcher::addNewFullGroup() {
494 static QString fctName = "Dispatcher::addNewFullGroup()";
496 cout << "call to " << qPrintable(fctName) << endl;
502 QList<BlockItem*> listBlocks = params->getCurrentScene()->getSelectedBlocks(); //selected blocks in the current scene
503 QList<AbstractBlock*> listAbstractBlocks; //abstract blocks in the group
504 QList<ConnectionItem *> connections = params->getCurrentScene()->getConnectionItems();
506 /* What must be done:
507 1 - creating a new GroupBlock
508 2 - moving the selected blocks from the current GroupBlock to the new GroupBlock
509 3 - creating a BlockItem that references the new GroupBlock
510 4 - creating a new GroupWidget
511 5 - creating a new GroupItem added to the scene of the GroupWidget
515 /* step 1 : creating new GroupBlock that will have as a parent the GroupBlock
516 associated to the GroupItem of the current scene
518 GroupBlock* parentBlock = params->getCurrentScene()->getGroupItem()->getRefBlock();
519 GroupBlock* newGroupBlock = new GroupBlock(parentBlock);
520 /* step 2: moving selected blocks */
521 foreach(BlockItem* blockItem, listBlocks) {
522 parentBlock->removeBlock(blockItem->getRefBlock());
523 newGroupBlock->addBlock(blockItem->getRefBlock());
526 GroupItem *parent = currentGroup->getScene()->getGroupItem();
527 GroupBlock *groupBlock = new GroupBlock(((GroupBlock*)parent->getRefBlock()),params->currentWindow);
528 BlockItem *blockItem = new BlockItem(params->getCurrentScene()->getGroupItem(),groupBlock,this,params);
529 GroupItem *groupItem = new GroupItem(blockItem,groupBlock,this,params);
531 //create the new window
532 GroupWidget* win = new GroupWidget(this,params);
533 win->getScene()->setGroupItem(groupItem);
534 win->getScene()->addItem(groupItem);
535 ((GroupBlock*)groupItem->getRefBlock())->setWindow(win);
536 params->addWindow(win);
540 params->getCurrentScene()->addBlockItem(blockItem);
541 params->getCurrentScene()->addItem(blockItem);
542 ((GroupItem*)params->getCurrentScene()->getGroupItem())->addBlockItem(blockItem);
544 //replace selected blocks in the group
545 foreach(AbstractBoxItem *block, listBlocks){
546 ((GroupItem*)block->getParentItem())->removeBlockItem(block);
547 ((GroupBlock*)block->getParentItem()->getRefBlock())->removeBlock(block->getRefBlock());
548 params->getCurrentScene()->removeItem(block);
549 params->getCurrentScene()->removeBlockItem(block);
551 groupBlock->addBlock(block->getRefBlock());
552 listAbstractBlocks.append(block->getRefBlock());
554 block->setUpperItem(groupItem);
555 groupItem->addBlockItem(block);
556 win->getScene()->addItem(block);
557 win->getScene()->addBlockItem(block);
560 //replace connection between selected blocks in the group
561 foreach(ConnectionItem *conn, connections){
562 if(listBlocks.contains(conn->getFromInterfaceItem()->getOwner())){
563 if(listBlocks.contains(conn->getToInterfaceItem()->getOwner())){
564 parent->removeConnection(conn);
565 params->getCurrentScene()->removeItem(conn);
567 groupItem->addConnection(conn);
568 win->getScene()->addItem(conn);
573 //create new interfaces and connections for the new group
574 foreach(AbstractBoxItem *block, listBlocks){
575 foreach(InterfaceItem *inter, block->getInterfaces()){
576 cout << "inter : " << inter->getName().toStdString() << endl;
577 if(inter->refInter->getConnectedFrom() != NULL && inter->refInter->getDirection() == AbstractInterface::Input){
578 cout << "connected from non null" << endl;
579 if(!listAbstractBlocks.contains(inter->refInter->getConnectedFrom()->getOwner())){
581 AbstractInterface *iface = inter->refInter->clone(0);
582 iface->setName(iface->getName()+"_group");
583 groupBlock->addInterface(iface);
585 InterfaceItem *ifaceItem = new InterfaceItem(0,Parameters::East,iface,blockItem,params);
586 blockItem->addInterface(ifaceItem);
587 blockItem->resetInterfacesPosition();
589 InterfaceItem *ifaceGroupItem = new InterfaceItem(0,Parameters::West,iface,groupItem,params);
590 groupItem->addInterface(ifaceGroupItem);
591 groupItem->resetInterfacesPosition();
592 foreach(ConnectionItem* conn, currentGroup->getScene()->getInterfaceConnections(inter)){
593 if(conn->getToInterfaceItem() == inter){
594 conn->setToInterfaceItem(ifaceItem);
595 ifaceItem->refInter->setConnectedFrom(NULL);
596 conn->getFromInterfaceItem()->refInter->clearConnectedTo();
597 connect(ifaceItem,conn->getFromInterfaceItem());
600 params->setCurrentWindow(win);
602 inter->refInter->setConnectedFrom(NULL);
603 ifaceGroupItem->refInter->clearConnectedTo();
604 connect(inter,ifaceGroupItem);
605 params->setCurrentWindow(mainWindow);
609 if(!inter->refInter->getConnectedTo().isEmpty() && inter->refInter->getDirection() == AbstractInterface::Output){
610 cout << "connected to non null" << endl;
611 foreach(AbstractInterface *iface, inter->refInter->getConnectedTo()){
612 if(!listAbstractBlocks.contains(iface->getOwner())){
614 AbstractInterface *iface = inter->refInter->clone(0);
615 iface->setName(iface->getName()+"_group");
616 groupBlock->addInterface(iface);
618 InterfaceItem *ifaceItem = new InterfaceItem(0,Parameters::East,iface,blockItem,params);
619 blockItem->addInterface(ifaceItem);
620 blockItem->resetInterfacesPosition();
622 foreach(ConnectionItem* conn, currentGroup->getScene()->getInterfaceConnections(inter)){
623 if(conn->getFromInterfaceItem() == inter){
624 conn->setFromInterfaceItem(ifaceItem);
625 iface->addConnectedTo(conn->getToInterfaceItem()->refInter);
626 conn->getToInterfaceItem()->refInter->setConnectedFrom(iface);
630 InterfaceItem *ifaceGroupItem = new InterfaceItem(0,Parameters::East,iface,groupItem,params);
631 groupItem->addInterface(ifaceGroupItem);
632 groupItem->resetInterfacesPosition();
633 inter->refInter->clearConnectedTo();
634 ifaceGroupItem->refInter->setConnectedFrom(NULL);
635 connect(ifaceGroupItem,inter);
644 parent->updateShape();
645 currentGroup->getScene()->updateConnectionItemsShape();
647 groupItem->updateShape();
648 win->getScene()->updateConnectionItemsShape();
649 groupItem->update(groupItem->boundingRect());
654 void Dispatcher::removeBlock(BoxItem *item) {
655 static QString fctName = "Dispatcher::removeBlock()";
657 cout << "call to " << qPrintable(fctName) << endl;
660 /* a BoxItem (group of func) can be removed only if none of its
661 interfaces is connected to a group interface that is itself
662 connected to another one.
664 bool canRemove = true;
666 foreach(InterfaceItem* ifaceItem, item->getInterfaces()) {
667 foreach(ConnectionItem* conn, ifaceItem->connections) {
668 InterfaceItem* other = NULL;
669 if (conn->getFromInterfaceItem() == ifaceItem) {
670 other = conn->getToInterfaceItem();
673 other = conn->getFromInterfaceItem();
676 if (other->getOwner()->isGroupItem()) {
677 ConnectedInterface* ref = other->refInter;
678 if ((ref->isConnectedFrom()) && (ref->isConnectedTo())) {
685 QMessageBox::warning(NULL,"Forbidden operation",
686 "The block has at least one connection to a group interface that is totally connected.",
692 if (item->getRefBlock()->isFunctionalBlock()) {
693 msg = "Removing block ";
696 msg = "Removing group ";
698 msg += item->getRefBlock()->getName();
699 msg += " and all its connections.\n\nAre you sure ?";
701 int ret = QMessageBox::question(NULL,"Removing functional block",msg, QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Ok);
702 if (ret == QMessageBox::Cancel) {
705 removeAllBlockConnections(item);
707 if (item->getRefBlock()->isFunctionalBlock()) {
708 FunctionalBlock* block = AB_TO_FUN(item->getRefBlock());
709 GroupBlock* group = AB_TO_GRP(block->getParent());
710 item->getScene()->removeBlockItem(item);
711 params->getGraph()->removeFunctionalBlock(block,group);
713 else if (item->getRefBlock()->isGroupBlock()) {
715 GroupBlock* group = AB_TO_GRP(item->getRefBlock());
717 // remove all child scenes recursively
718 GroupItem* subgroup = item->getChildGroupItem();
719 destroyScene(subgroup->getScene());
720 // remove the BoxItem
721 item->getScene()->removeBlockItem(item);
722 // remove the group from the graph
723 params->getGraph()->removeGroupBlock(group);
727 void Dispatcher::removeAllBlockConnections(BoxItem *item) {
728 static QString fctName = "Dispatcher::removeAllBlockConnection()";
730 cout << "call to " << qPrintable(fctName) << endl;
733 foreach(InterfaceItem* ifaceItem, item->getInterfaces()) {
734 foreach(ConnectionItem* conn, ifaceItem->connections) {
735 removeConnection(conn);
740 void Dispatcher::removeConnection(ConnectionItem *conn) {
741 static QString fctName = "Dispatcher::removeConnection()";
743 cout << "call to " << qPrintable(fctName) << endl;
745 InterfaceItem* fromIfaceItem = conn->getFromInterfaceItem();
746 InterfaceItem* toIfaceItem = conn->getToInterfaceItem();
749 cout << "remove connection from " << qPrintable(fromIfaceItem->refInter->getName()) << " to " << qPrintable(toIfaceItem->refInter->getName()) << endl;
752 InterfaceItem* groupIfaceItem = NULL; // in case of one of the two interface belongs to the GroupItem
753 GroupItem* groupItem = NULL;
755 if (fromIfaceItem->getOwner()->isGroupItem()) {
756 groupIfaceItem = fromIfaceItem;
757 groupItem = toIfaceItem->getOwner()->getScene()->getGroupItem();
759 else if (toIfaceItem->getOwner()->isGroupItem()) {
760 groupIfaceItem = toIfaceItem;
761 groupItem = fromIfaceItem->getOwner()->getScene()->getGroupItem();
764 groupItem = fromIfaceItem->getOwner()->getScene()->getGroupItem();
767 // removing the connection from graph
769 cout << "removing connections from graph ..." ;
771 ConnectedInterface *fromInter = fromIfaceItem->refInter;
772 ConnectedInterface *toInter = toIfaceItem->refInter;
773 if (fromInter->getDirection() == AbstractInterface::InOut) {
774 fromInter->clearConnectedTo();
775 fromInter->clearConnectedFrom();
776 toInter->clearConnectedTo();
777 toInter->clearConnectedFrom();
780 fromInter->removeConnectedTo(toInter);
781 toInter->clearConnectedFrom();
784 cout << "done." << endl ;
787 // removing the connection from scene
789 cout << "removing connections from scene ..." ;
791 fromIfaceItem->removeConnectionItem(conn);
792 toIfaceItem->removeConnectionItem(conn);
793 groupItem->getScene()->removeConnectionItem(conn);
796 cout << "done." << endl ;
799 if (groupIfaceItem != NULL) {
800 ConnectedInterface* groupInter = groupIfaceItem->refInter;
801 groupItem->removeInterface(groupIfaceItem);
803 BoxItem* parent2Item = groupItem->getParentItem();
804 if (parent2Item != NULL) {
805 InterfaceItem* group2IfaceItem = parent2Item->searchInterfaceByRef(groupInter);
806 parent2Item->removeInterface(group2IfaceItem);
808 groupInter->getOwner()->removeInterface(groupInter);
812 void Dispatcher::showBlocksLibrary(){
813 cout << "showing block library" << endl;
814 mainWindow->getLibrary()->show();
815 mainWindow->getLibrary()->raise();
818 void Dispatcher::showProperties(InterfaceItem *inter)
820 new InterfacePropertiesWindow(inter);
823 /* connectInterToGroup() :
824 The only way for a block (functional of group) within a GroupItem to be connected
825 to the latter is to right-click on one of its interfaces and to choose "connect to group".
826 That action will create a new InterfaceItem on the GroupItem and a connectionItem between the
829 void Dispatcher::connectInterToGroup(InterfaceItem *item){
831 // getting the GroupBlock and GroupItem that are parent of the block that owns item
832 ConnectedInterface *refInter = item->refInter;
833 cout << "owner of iface = " << qPrintable(refInter->getOwner()->getName()) << endl;
834 GroupBlock* parentBlock = AB_TO_GRP(refInter->getOwner()->getParent());
835 cout << "create iface for parent group = " << qPrintable(parentBlock->getName()) << endl;
836 GroupItem *parentItem = item->getOwner()->getScene()->getGroupItem();
838 // creating/adding the group interface in the graph model
839 GroupInterface *groupInter = new GroupInterface(parentBlock,refInter->getName()+"_group",refInter->getDirection(),refInter->getPurpose());
840 groupInter->setType(refInter->getType());
841 groupInter->setWidth(refInter->getWidth());
842 groupInter->setPurpose(refInter->getPurpose());
843 parentItem->getRefBlock()->addInterface(groupInter);
845 // connect both interface
847 if (refInter->getDirection() == AbstractInterface::Output) {
848 ok = refInter->connectTo(groupInter);
849 ok = ok & groupInter->connectFrom(refInter);
851 else if (refInter->getDirection() == AbstractInterface::Input) {
852 ok = groupInter->connectTo(refInter);
853 ok = ok & refInter->connectFrom(groupInter);
855 else if (refInter->getDirection() == AbstractInterface::InOut) {
856 ok = refInter->connectTo(groupInter);
857 ok = ok & groupInter->connectFrom(refInter);
858 ok = ok & groupInter->connectTo(refInter);
859 ok = ok & refInter->connectFrom(groupInter);
862 cerr << "abnormal case while connecting a block iface to its parent group" << endl;
864 // creating/adding the group interface in the current scene model, and connection item
865 InterfaceItem *groupIfaceItem = new InterfaceItem(0,item->getOrientation(),groupInter,parentItem,params);
866 parentItem->addInterface(groupIfaceItem,true);
868 parentItem->getScene()->createConnectionItem(item, groupIfaceItem);
870 // if groupItem is not topGroup, must also add a new interface to the parent BlockItem
871 BoxItem* parent2Item = parentItem->getParentItem();
872 if(parent2Item != NULL){
873 InterfaceItem *blockIfaceItem = new InterfaceItem(0,item->getOrientation(),groupInter,parent2Item,params);
874 parent2Item->addInterface(blockIfaceItem,true);
878 parentItem->getScene()->updateConnectionItemsShape();
880 params->unsaveModif = true;
885 void Dispatcher::disconnectInterFromGroup(InterfaceItem *item) {
886 static QString fctName = "Dispatcher::disconnectInterFromGroup()";
888 cout << "call to " << qPrintable(fctName) << endl;
891 // getting the GroupBlock and GroupItem that are parent of the block that owns item
892 ConnectedInterface *refInter = item->refInter;
893 ConnectedInterface *groupInter = NULL;
894 GroupBlock* parentGroup = AB_TO_GRP(refInter->getOwner()->getParent());
895 GroupItem *parentItem = item->getOwner()->getScene()->getGroupItem();
897 // removing the connection from graph
899 cout << "removing connections from graph ..." ;
902 if (refInter->getDirection() == AbstractInterface::Output) {
903 groupInter = refInter->getConnectionToParentGroup(); // must be a single connection to
904 refInter->removeConnectedTo(groupInter);
905 groupInter->clearConnectedFrom();
907 else if (refInter->getDirection() == AbstractInterface::Input) {
908 groupInter = refInter->getConnectedFrom();
909 refInter->clearConnectedFrom();
910 groupInter->clearConnectedTo();
912 else if (refInter->getDirection() == AbstractInterface::InOut) {
913 groupInter = refInter->getConnectionToParentGroup(); // must be a single connection to
914 refInter->clearConnectedTo();
915 refInter->clearConnectedFrom();
916 groupInter->clearConnectedTo();
917 groupInter->clearConnectedFrom();
920 cout << "done." << endl ;
923 if (groupInter == NULL) {
924 cerr << "abnormal case 1 while removing an interface item of a block, linked to a parent group" << endl;
928 cout << "getting group interface item, and connection item ..." ;
932 InterfaceItem* groupIfaceItem = parentItem->searchInterfaceByRef(groupInter);
933 if (groupIfaceItem == NULL) {
934 cerr << "abnormal case 2 while removing an interface item of a block, linked to a parent group" << endl;
936 ConnectionItem* conn = parentItem->getScene()->searchConnectionItem(item,groupIfaceItem);
938 cerr << "abnormal case 3 while removing an interface item of a block, linked to a parent group" << endl;
941 cout << "done." << endl ;
944 // removing the interface group item from the group item, and the connection item
946 cout << "removing group interface item, and connection item ..." ;
949 item->removeConnectionItem(conn);
950 groupIfaceItem->removeConnectionItem(conn);
951 parentItem->removeInterface(groupIfaceItem); // CAUTION : this deletes the interface item.
952 parentItem->getScene()->removeConnectionItem(conn);
954 cout << "done." << endl ;
957 // removing the interface box item in the parent scene
959 cout << "removing the inteeface item of box item in parent scene if needed ..." ;
962 BoxItem* parent2Item = parentItem->getParentItem();
963 if (parent2Item != NULL) {
964 InterfaceItem* group2IfaceItem = parent2Item->searchInterfaceByRef(groupInter);
965 parent2Item->removeInterface(group2IfaceItem);
968 cout << "done." << endl ;
971 // removing the interface group from the group
973 cout << "removing group interface ..." ;
975 parentGroup->removeInterface(groupInter);
977 cout << "done." << endl ;
981 void Dispatcher::removeFunctionalInterface(InterfaceItem *item) {
982 static QString fctName = "Dispatcher::removeBlockInterface()";
984 cout << "call to " << qPrintable(fctName) << endl;
987 /* first, remove all connections from item
988 NB: if there is a connection to a group interface, then this
989 method should not be called if the group interface is also
990 connected to another interface. Normally, this is not possible
991 because such a check is done when creating the contextual menu
992 that allows to remove an interface.
994 foreach(ConnectionItem* conn, item->connections) {
995 removeConnection(conn);
998 ConnectedInterface* ref = item->refInter;
999 item->getOwner()->removeInterface(item);
1000 FunctionalBlock* fun = AB_TO_FUN(ref->getOwner());
1001 fun->removeInterface(ref);
1004 void Dispatcher::removeGroupInterface(InterfaceItem *item) {
1005 static QString fctName = "Dispatcher::removeGroupInterface()";
1006 #ifdef DEBUG_FCTNAME
1007 cout << "call to " << qPrintable(fctName) << endl;
1010 /* NB: there is a single connection between item and another one that is owned
1011 by a BoxItem. Thus, we just have to find it and to call disconnectInterFromGroup();
1013 ConnectionItem* conn = item->connections.at(0);
1014 if (conn->getFromInterfaceItem() == item) {
1015 disconnectInterFromGroup(conn->getToInterfaceItem());
1018 disconnectInterFromGroup(conn->getFromInterfaceItem());
1022 QMap<int, QString> Dispatcher::getAllGroupNames() {
1024 QMap<int, QString> list;
1025 foreach(GroupWidget *group, groupList) {
1026 list.insert(group->getScene()->getId(), group->getScene()->getGroupItem()->getRefBlock()->getName());
1031 GroupScene* Dispatcher::searchSceneById(int id) {
1032 foreach(GroupWidget *group, groupList){
1033 if(group->getScene()->getId() == id)
1034 return group->getScene();
1036 cout << "search scene by id :" << id << " :: not found..." << endl;
1040 GroupItem *Dispatcher::searchGroupItemById(int id) {
1041 foreach(GroupWidget *group, groupList) {
1042 GroupScene* scene = group->getScene();
1043 if (scene->getGroupItem()->getId() == id) return scene->getGroupItem();
1045 cout << "search GroupItem by id :" << id << " :: not found..." << endl;
1049 BoxItem *Dispatcher::searchBlockItemById(int id) {
1050 foreach(GroupWidget *group, groupList) {
1052 GroupScene* scene = group->getScene();
1053 foreach(BoxItem *item, scene->getBlockItems()){
1054 if(item->getId() == id){
1059 cout << "search BlockItem by id :" << id << " :: not found..." << endl;
1063 InterfaceItem* Dispatcher::searchInterfaceItemById(int id) {
1065 foreach(GroupWidget *group, groupList) {
1067 GroupScene* scene = group->getScene();
1069 foreach(InterfaceItem *item, scene->getGroupItem()->getInterfaces()){
1070 if(item->getId() == id){
1074 foreach(BoxItem *block, scene->getBlockItems()){
1075 foreach(InterfaceItem *item, block->getInterfaces()){
1076 if(item->getId() == id){
1082 cout << "search interface by id :" << id << " :: not found..." << endl;