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) {
169 // remove item from the viewport
171 // remove item from the QList
172 connectionItems.removeAll(item);
175 void GroupScene::setGroupItem(GroupItem *group) {
176 if ((groupItem == NULL) && (group != NULL)) {
182 void GroupScene::removeGroupItem() {
183 // remove item from the viewport
184 removeItem(groupItem);
188 void GroupScene::unselecteInterfaces() {
190 if (selectedInterfaces[0] != NULL) {
191 selectedInterfaces[0]->selected = false;
192 selectedInterfaces[0] == NULL;
194 if (selectedInterfaces[1] != NULL) {
195 selectedInterfaces[1]->selected = false;
196 selectedInterfaces[1] == NULL;
200 void GroupScene::updateConnectionItemsShape() {
202 foreach(ConnectionItem* conn, connectionItems){
207 void GroupScene::save(QXmlStreamWriter &writer) {
208 writer.writeStartElement("scene");
209 writer.writeAttribute("id",QString::number(id));
210 if (parentScene == NULL) {
211 writer.writeAttribute("upper_scene",QString::number(-1));
214 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
216 groupItem->save(writer);
219 writer.writeStartElement("source_items");
220 writer.writeAttribute("count",QString::number(sourceItems.length()));
221 foreach(SourceItem* item, sourceItems) {
224 writer.writeEndElement(); // source_items
226 writer.writeStartElement("block_items");
228 QList<BoxItem *> functionalBlocks;
229 QList<BoxItem *> groupBlocks;
231 foreach(BoxItem *item, boxItems){
232 if(item->getRefBlock()->isFunctionalBlock()){
233 functionalBlocks.append(item);
234 } else if(item->getRefBlock()->isGroupBlock()){
235 groupBlocks.append(item);
238 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
239 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
241 foreach(BoxItem* item, functionalBlocks) {
244 foreach(BoxItem* item, groupBlocks) {
247 writer.writeEndElement(); // block_items
248 writer.writeEndElement(); // scene