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, bool visible) {
170 ConnectionItem* conn = NULL;
172 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
173 conn->setVisible(visible);
175 addConnectionItem(conn);
178 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
179 foreach(ConnectionItem* conn , connectionItems) {
180 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
181 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
188 void GroupScene::addConnectionItem(ConnectionItem* item) {
189 // add item from the viewport
191 // add item from the QList
192 connectionItems.append(item);
195 void GroupScene::removeConnectionItem(ConnectionItem* item) {
197 // remove connection from/to InterfaceItem
198 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
199 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
200 fromIfaceItem->removeConnectionItem(item);
201 toIfaceItem->removeConnectionItem(item);
203 // remove item from the viewport
205 // remove item from the QList
206 connectionItems.removeAll(item);
209 void GroupScene::setGroupItem(GroupItem *group) {
210 if ((groupItem == NULL) && (group != NULL)) {
216 void GroupScene::removeGroupItem() {
217 // remove item from the viewport
218 removeItem(groupItem);
222 void GroupScene::unselecteInterfaces() {
224 if (selectedInterfaces[0] != NULL) {
225 selectedInterfaces[0]->selected = false;
226 selectedInterfaces[0] = NULL;
228 if (selectedInterfaces[1] != NULL) {
229 selectedInterfaces[1]->selected = false;
230 selectedInterfaces[1] = NULL;
234 void GroupScene::updateConnectionItemsShape() {
236 foreach(ConnectionItem* conn, connectionItems){
241 void GroupScene::save(QXmlStreamWriter &writer) {
242 writer.writeStartElement("scene");
243 writer.writeAttribute("id",QString::number(id));
244 if (parentScene == NULL) {
245 writer.writeAttribute("upper_scene",QString::number(-1));
248 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
250 groupItem->save(writer);
253 writer.writeStartElement("source_items");
254 writer.writeAttribute("count",QString::number(sourceItems.length()));
255 foreach(SourceItem* item, sourceItems) {
258 writer.writeEndElement(); // source_items
260 writer.writeStartElement("block_items");
262 QList<BoxItem *> functionalBlocks;
263 QList<BoxItem *> groupBlocks;
265 foreach(BoxItem *item, boxItems){
266 if(item->getRefBlock()->isFunctionalBlock()){
267 functionalBlocks.append(item);
268 } else if(item->getRefBlock()->isGroupBlock()){
269 groupBlocks.append(item);
272 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
273 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
275 foreach(BoxItem* item, functionalBlocks) {
278 foreach(BoxItem* item, groupBlocks) {
281 writer.writeEndElement(); // block_items
282 writer.writeEndElement(); // scene