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, bool withinGroup) {
144 ConnectionItem* conn = NULL;
146 conn = new ConnectionItem(iface1,iface2, dispatcher, params, groupItem);
149 conn = new ConnectionItem(iface1,iface2, dispatcher, params, NULL);
152 addConnectionItem(conn);
155 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
156 foreach(ConnectionItem* conn , connectionItems) {
157 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
158 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
165 void GroupScene::addConnectionItem(ConnectionItem* item) {
166 // add item from the viewport
168 // add item from the QList
169 connectionItems.append(item);
172 void GroupScene::removeConnectionItem(ConnectionItem* item) {
173 // remove item from the viewport
175 // remove item from the QList
176 connectionItems.removeAll(item);
179 void GroupScene::setGroupItem(GroupItem *group) {
180 if ((groupItem == NULL) && (group != NULL)) {
186 void GroupScene::removeGroupItem() {
187 // remove item from the viewport
188 removeItem(groupItem);
192 void GroupScene::unselecteInterfaces() {
194 if (selectedInterfaces[0] != NULL) {
195 selectedInterfaces[0]->selected = false;
196 selectedInterfaces[0] == NULL;
198 if (selectedInterfaces[1] != NULL) {
199 selectedInterfaces[1]->selected = false;
200 selectedInterfaces[1] == NULL;
204 void GroupScene::updateConnectionItemsShape() {
206 foreach(ConnectionItem* conn, connectionItems){
211 void GroupScene::save(QXmlStreamWriter &writer) {
212 writer.writeStartElement("scene");
213 writer.writeAttribute("id",QString::number(id));
214 if (parentScene == NULL) {
215 writer.writeAttribute("upper_scene",QString::number(-1));
218 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
220 groupItem->save(writer);
223 writer.writeStartElement("source_items");
224 writer.writeAttribute("count",QString::number(sourceItems.length()));
225 foreach(SourceItem* item, sourceItems) {
228 writer.writeEndElement(); // source_items
230 writer.writeStartElement("block_items");
232 QList<BoxItem *> functionalBlocks;
233 QList<BoxItem *> groupBlocks;
235 foreach(BoxItem *item, boxItems){
236 if(item->getRefBlock()->isFunctionalBlock()){
237 functionalBlocks.append(item);
238 } else if(item->getRefBlock()->isGroupBlock()){
239 groupBlocks.append(item);
242 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
243 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
245 foreach(BoxItem* item, functionalBlocks) {
248 foreach(BoxItem* item, groupBlocks) {
251 writer.writeEndElement(); // block_items
252 writer.writeEndElement(); // scene