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++);
65 foreach(SourceItem *item, sourceItems){
66 item->setId(counter++);
69 foreach(BoxItem *item, boxItems){
70 item->setId(counter++);
75 int GroupScene::setInterfacesId(int countInit) {
76 int counter = countInit;
77 foreach(InterfaceItem* inter, groupItem->getInterfaces()){
78 inter->setId(counter++);
81 foreach(SourceItem *item, sourceItems){
82 foreach(InterfaceItem* inter, item->getInterfaces()){
83 inter->setId(counter++);
87 foreach(BoxItem *item, boxItems){
88 foreach(InterfaceItem* inter, item->getInterfaces()){
89 inter->setId(counter++);
95 BoxItem *GroupScene::createBoxItem(AbstractBlock *block) {
97 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem);
99 // add item from the QList
100 boxItems.append(item);
101 // repainting the group
102 groupItem->updateShape();
103 // center the new block
104 QPointF newPos((groupItem->getWidth()-item->getTotalWidth())/2.0, (groupItem->getHeight()-item->getTotalHeight())/2.0);
105 newPos = newPos-item->getOriginPoint();
106 item->moveTo(newPos);
111 void GroupScene::addBoxItem(BoxItem* item) {
112 // add item from the QList
113 boxItems.append(item);
114 // repainting the group
115 groupItem->updateShape();
118 void GroupScene::removeBoxItem(BoxItem* item) {
119 // remove item from the viewport
121 // remove item from the QList
122 boxItems.removeAll(item);
123 // repainting the group
124 groupItem->updateShape();
127 SourceItem *GroupScene::createSourceItem(AbstractBlock *block) {
129 SourceItem* item = new SourceItem(block,dispatcher,params);
130 // adding item to the scene
133 // add item from the QList
134 sourceItems.append(item);
135 // center the new block
136 QPointF groupPos = groupItem->pos();
137 QPointF newPos(groupPos.x()-item->getTotalWidth()-50, groupPos.y());
138 newPos = newPos-item->getOriginPoint();
139 item->moveTo(newPos);
143 void GroupScene::addSourceItem(SourceItem* item) {
144 // adding item to the scene
147 // add item from the QList
148 sourceItems.append(item);
151 void GroupScene::removeSourceItem(SourceItem* item) {
152 // remove item from the viewport
154 // remove item from the QList
155 sourceItems.removeAll(item);
158 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
159 ConnectionItem* conn = NULL;
161 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
163 addConnectionItem(conn);
166 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
167 foreach(ConnectionItem* conn , connectionItems) {
168 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
169 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
176 void GroupScene::addConnectionItem(ConnectionItem* item) {
177 // add item from the viewport
179 // add item from the QList
180 connectionItems.append(item);
183 void GroupScene::removeConnectionItem(ConnectionItem* item) {
185 // remove connection from/to InterfaceItem
186 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
187 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
188 fromIfaceItem->removeConnectionItem(item);
189 toIfaceItem->removeConnectionItem(item);
191 // remove item from the viewport
193 // remove item from the QList
194 connectionItems.removeAll(item);
197 void GroupScene::setGroupItem(GroupItem *group) {
198 if ((groupItem == NULL) && (group != NULL)) {
204 void GroupScene::removeGroupItem() {
205 // remove item from the viewport
206 removeItem(groupItem);
210 void GroupScene::unselecteInterfaces() {
212 if (selectedInterfaces[0] != NULL) {
213 selectedInterfaces[0]->selected = false;
214 selectedInterfaces[0] == NULL;
216 if (selectedInterfaces[1] != NULL) {
217 selectedInterfaces[1]->selected = false;
218 selectedInterfaces[1] == NULL;
222 void GroupScene::updateConnectionItemsShape() {
224 foreach(ConnectionItem* conn, connectionItems){
229 void GroupScene::save(QXmlStreamWriter &writer) {
230 writer.writeStartElement("scene");
231 writer.writeAttribute("id",QString::number(id));
232 if (parentScene == NULL) {
233 writer.writeAttribute("upper_scene",QString::number(-1));
236 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
238 groupItem->save(writer);
241 writer.writeStartElement("source_items");
242 writer.writeAttribute("count",QString::number(sourceItems.length()));
243 foreach(SourceItem* item, sourceItems) {
246 writer.writeEndElement(); // source_items
248 writer.writeStartElement("block_items");
250 QList<BoxItem *> functionalBlocks;
251 QList<BoxItem *> groupBlocks;
253 foreach(BoxItem *item, boxItems){
254 if(item->getRefBlock()->isFunctionalBlock()){
255 functionalBlocks.append(item);
256 } else if(item->getRefBlock()->isGroupBlock()){
257 groupBlocks.append(item);
260 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
261 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
263 foreach(BoxItem* item, functionalBlocks) {
266 foreach(BoxItem* item, groupBlocks) {
269 writer.writeEndElement(); // block_items
270 writer.writeEndElement(); // scene