1 #include "GroupScene.h"
3 #include "Dispatcher.h"
4 #include "Parameters.h"
5 #include "GroupWidget.h"
8 #include "SourceItem.h"
9 #include "ConnectionItem.h"
10 #include "InterfaceItem.h"
11 #include "AbstractBlock.h"
13 GroupScene::GroupScene(GroupScene *_parentScene, GroupWidget *_window, Dispatcher* _dispatcher, Parameters* _params, bool _topScene, QObject *parent) : QGraphicsScene(parent) {
14 dispatcher = _dispatcher;
16 parentScene = _parentScene;
17 if (parentScene != NULL) {
18 parentScene->addChildScene(this);
26 selectedInterfaces[0] = NULL;
27 selectedInterfaces[1] = NULL;
30 GroupScene::~GroupScene() {
31 static QString fctName = "GroupScene::~GroupScene()";
33 cout << "call to " << qPrintable(fctName) << endl;
37 connectionItems.clear();
41 void GroupScene::setSelectedInterface(int id, InterfaceItem* iface) {
42 if ((id < 1)|| (id > 2)) return;
43 selectedInterfaces[id-1] = iface;
46 InterfaceItem* GroupScene::getSelectedInterface(int id) {
47 if ((id < 1)|| (id > 2)) return NULL;
48 return selectedInterfaces[id-1];
51 QList<BoxItem *> GroupScene::getSelectedBlocks() {
53 foreach(BoxItem *item, boxItems){
54 if (item->isSelected()){
61 int GroupScene::setItemsId(int countInit) {
62 int counter = countInit;
63 groupItem->setId(counter++);
64 foreach(BoxItem *item, boxItems){
65 item->setId(counter++);
70 int GroupScene::setInterfacesId(int countInit) {
71 int counter = countInit;
72 foreach(InterfaceItem* inter, groupItem->getInterfaces()){
73 inter->setId(counter++);
75 foreach(BoxItem *item, boxItems){
76 foreach(InterfaceItem* inter, item->getInterfaces()){
77 inter->setId(counter++);
83 BoxItem *GroupScene::createBoxItem(AbstractBlock *block) {
85 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem);
87 // add item from the QList
88 boxItems.append(item);
89 // repainting the group
90 groupItem->updateShape();
91 // center the new block
92 QPointF newPos((groupItem->getWidth()-item->getTotalWidth())/2.0, (groupItem->getHeight()-item->getTotalHeight())/2.0);
93 newPos = newPos-item->getOriginPoint();
99 void GroupScene::addBoxItem(BoxItem* item) {
100 // add item from the QList
101 boxItems.append(item);
102 // repainting the group
103 groupItem->updateShape();
106 void GroupScene::removeBoxItem(BoxItem* item) {
107 // remove item from the viewport
109 // remove item from the QList
110 boxItems.removeAll(item);
111 // repainting the group
112 groupItem->updateShape();
115 SourceItem *GroupScene::createSourceItem(AbstractBlock *block) {
117 SourceItem* item = new SourceItem(block,dispatcher,params);
118 // adding item to the scene
121 // add item from the QList
122 sourceItems.append(item);
123 // center the new block
124 QPointF groupPos = groupItem->pos();
125 QPointF newPos(groupPos.x()-item->getTotalWidth()-50, groupPos.y());
126 newPos = newPos-item->getOriginPoint();
127 item->moveTo(newPos);
131 void GroupScene::addSourceItem(SourceItem* item) {
132 // add item from the QList
133 sourceItems.append(item);
136 void GroupScene::removeSourceItem(SourceItem* item) {
137 // remove item from the viewport
139 // remove item from the QList
140 sourceItems.removeAll(item);
143 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
144 ConnectionItem* conn = NULL;
146 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
148 addConnectionItem(conn);
151 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
152 foreach(ConnectionItem* conn , connectionItems) {
153 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
154 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
161 void GroupScene::addConnectionItem(ConnectionItem* item) {
162 // add item from the viewport
164 // add item from the QList
165 connectionItems.append(item);
168 void GroupScene::removeConnectionItem(ConnectionItem* item) {
170 // remove connection from/to InterfaceItem
171 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
172 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
173 fromIfaceItem->removeConnectionItem(item);
174 toIfaceItem->removeConnectionItem(item);
176 // remove item from the viewport
178 // remove item from the QList
179 connectionItems.removeAll(item);
182 void GroupScene::setGroupItem(GroupItem *group) {
183 if ((groupItem == NULL) && (group != NULL)) {
189 void GroupScene::removeGroupItem() {
190 // remove item from the viewport
191 removeItem(groupItem);
195 void GroupScene::unselecteInterfaces() {
197 if (selectedInterfaces[0] != NULL) {
198 selectedInterfaces[0]->selected = false;
199 selectedInterfaces[0] == NULL;
201 if (selectedInterfaces[1] != NULL) {
202 selectedInterfaces[1]->selected = false;
203 selectedInterfaces[1] == NULL;
207 void GroupScene::updateConnectionItemsShape() {
209 foreach(ConnectionItem* conn, connectionItems){
214 void GroupScene::save(QXmlStreamWriter &writer) {
215 writer.writeStartElement("scene");
216 writer.writeAttribute("id",QString::number(id));
217 if (parentScene == NULL) {
218 writer.writeAttribute("upper_scene",QString::number(-1));
221 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
223 groupItem->save(writer);
226 writer.writeStartElement("source_items");
227 writer.writeAttribute("count",QString::number(sourceItems.length()));
228 foreach(SourceItem* item, sourceItems) {
231 writer.writeEndElement(); // source_items
233 writer.writeStartElement("block_items");
235 QList<BoxItem *> functionalBlocks;
236 QList<BoxItem *> groupBlocks;
238 foreach(BoxItem *item, boxItems){
239 if(item->getRefBlock()->isFunctionalBlock()){
240 functionalBlocks.append(item);
241 } else if(item->getRefBlock()->isGroupBlock()){
242 groupBlocks.append(item);
245 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
246 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
248 foreach(BoxItem* item, functionalBlocks) {
251 foreach(BoxItem* item, groupBlocks) {
254 writer.writeEndElement(); // block_items
255 writer.writeEndElement(); // scene