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();
42 void GroupScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
44 //QGraphicsScene::mouseMoveEvent(event);
46 QPointF p = event->scenePos();
47 cout << p.x() << "," << p.y() << endl;
52 void GroupScene::setSelectedInterface(int id, InterfaceItem* iface) {
53 if ((id < 1)|| (id > 2)) return;
54 selectedInterfaces[id-1] = iface;
57 InterfaceItem* GroupScene::getSelectedInterface(int id) {
58 if ((id < 1)|| (id > 2)) return NULL;
59 return selectedInterfaces[id-1];
62 QList<BoxItem *> GroupScene::getSelectedBlocks() {
64 foreach(BoxItem *item, boxItems){
65 if (item->isSelected()){
72 int GroupScene::setItemsId(int countInit) {
73 int counter = countInit;
74 groupItem->setId(counter++);
76 foreach(SourceItem *item, sourceItems){
77 item->setId(counter++);
80 foreach(BoxItem *item, boxItems){
81 item->setId(counter++);
86 int GroupScene::setInterfacesId(int countInit) {
87 int counter = countInit;
88 foreach(InterfaceItem* inter, groupItem->getInterfaces()){
89 inter->setId(counter++);
92 foreach(SourceItem *item, sourceItems){
93 foreach(InterfaceItem* inter, item->getInterfaces()){
94 inter->setId(counter++);
98 foreach(BoxItem *item, boxItems){
99 foreach(InterfaceItem* inter, item->getInterfaces()){
100 inter->setId(counter++);
106 BoxItem *GroupScene::createBoxItem(AbstractBlock *block) {
108 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem);
110 // add item from the QList
111 boxItems.append(item);
112 // repainting the group
113 groupItem->updateShape();
114 // center the new block
115 QPointF newPos((groupItem->getWidth()-item->getTotalWidth())/2.0, (groupItem->getHeight()-item->getTotalHeight())/2.0);
116 newPos = newPos-item->getOriginPoint();
117 item->moveTo(newPos);
122 void GroupScene::addBoxItem(BoxItem* item) {
123 // add item from the QList
124 boxItems.append(item);
125 // repainting the group
126 groupItem->updateShape();
129 void GroupScene::removeBoxItem(BoxItem* item) {
130 // remove item from the viewport
132 // remove item from the QList
133 boxItems.removeAll(item);
134 // repainting the group
135 groupItem->updateShape();
138 SourceItem *GroupScene::createSourceItem(AbstractBlock *block) {
140 SourceItem* item = new SourceItem(block,dispatcher,params);
141 // adding item to the scene
144 // add item from the QList
145 sourceItems.append(item);
146 // center the new block
147 QPointF groupPos = groupItem->pos();
148 QPointF newPos(groupPos.x()-item->getTotalWidth()-50, groupPos.y());
149 newPos = newPos-item->getOriginPoint();
150 item->moveTo(newPos);
154 void GroupScene::addSourceItem(SourceItem* item) {
155 // adding item to the scene
158 // add item from the QList
159 sourceItems.append(item);
162 void GroupScene::removeSourceItem(SourceItem* item) {
163 // remove item from the viewport
165 // remove item from the QList
166 sourceItems.removeAll(item);
169 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
170 ConnectionItem* conn = NULL;
172 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
174 addConnectionItem(conn);
177 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
178 foreach(ConnectionItem* conn , connectionItems) {
179 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
180 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
187 void GroupScene::addConnectionItem(ConnectionItem* item) {
188 // add item from the viewport
190 // add item from the QList
191 connectionItems.append(item);
194 void GroupScene::removeConnectionItem(ConnectionItem* item) {
196 // remove connection from/to InterfaceItem
197 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
198 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
199 fromIfaceItem->removeConnectionItem(item);
200 toIfaceItem->removeConnectionItem(item);
202 // remove item from the viewport
204 // remove item from the QList
205 connectionItems.removeAll(item);
208 void GroupScene::setGroupItem(GroupItem *group) {
209 if ((groupItem == NULL) && (group != NULL)) {
215 void GroupScene::removeGroupItem() {
216 // remove item from the viewport
217 removeItem(groupItem);
221 void GroupScene::unselecteInterfaces() {
223 if (selectedInterfaces[0] != NULL) {
224 selectedInterfaces[0]->selected = false;
225 selectedInterfaces[0] = NULL;
227 if (selectedInterfaces[1] != NULL) {
228 selectedInterfaces[1]->selected = false;
229 selectedInterfaces[1] = NULL;
233 void GroupScene::updateConnectionItemsShape() {
235 foreach(ConnectionItem* conn, connectionItems){
240 void GroupScene::save(QXmlStreamWriter &writer) {
241 writer.writeStartElement("scene");
242 writer.writeAttribute("id",QString::number(id));
243 if (parentScene == NULL) {
244 writer.writeAttribute("upper_scene",QString::number(-1));
247 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
249 groupItem->save(writer);
252 writer.writeStartElement("source_items");
253 writer.writeAttribute("count",QString::number(sourceItems.length()));
254 foreach(SourceItem* item, sourceItems) {
257 writer.writeEndElement(); // source_items
259 writer.writeStartElement("block_items");
261 QList<BoxItem *> functionalBlocks;
262 QList<BoxItem *> groupBlocks;
264 foreach(BoxItem *item, boxItems){
265 if(item->getRefBlock()->isFunctionalBlock()){
266 functionalBlocks.append(item);
267 } else if(item->getRefBlock()->isGroupBlock()){
268 groupBlocks.append(item);
271 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
272 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
274 foreach(BoxItem* item, functionalBlocks) {
277 foreach(BoxItem* item, groupBlocks) {
280 writer.writeEndElement(); // block_items
281 writer.writeEndElement(); // scene