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, BoxItem::Position hPos, BoxItem::Position vPos, AbstractBoxItem::LockType lock, BoxItem::SpanType span) {
108 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem, lock, span);
110 // add item from the QList
111 boxItems.append(item);
112 // repainting the group
113 groupItem->updateShape();
114 // center the new block
116 if (hPos == BoxItem::Left) {
119 else if (hPos == BoxItem::Center) {
120 x = (groupItem->getWidth()-item->getTotalWidth())/2.0;
122 else if (hPos == BoxItem::Right) {
123 x = groupItem->getWidth()-item->getTotalWidth();
125 if (vPos == BoxItem::Top) {
128 else if (vPos == BoxItem::Center) {
129 y = (groupItem->getHeight()-item->getTotalHeight())/2.0;
131 else if (vPos == BoxItem::Bottom) {
132 y = groupItem->getHeight()-item->getTotalHeight();
135 newPos = newPos-item->getOriginPoint();
136 item->moveTo(newPos);
141 void GroupScene::addBoxItem(BoxItem* item) {
142 // add item from the QList
143 boxItems.append(item);
144 // repainting the group
145 groupItem->updateShape();
148 void GroupScene::removeBoxItem(BoxItem* item) {
149 // remove item from the viewport
151 // remove item from the QList
152 boxItems.removeAll(item);
153 // repainting the group
154 groupItem->updateShape();
157 SourceItem *GroupScene::createSourceItem(AbstractBlock *block) {
159 SourceItem* item = new SourceItem(block,dispatcher,params);
160 // adding item to the scene
163 // add item from the QList
164 sourceItems.append(item);
165 // center the new block
166 QPointF groupPos = groupItem->pos();
167 QPointF newPos(groupPos.x()-item->getTotalWidth()-50, groupPos.y());
168 newPos = newPos-item->getOriginPoint();
169 item->moveTo(newPos);
173 void GroupScene::addSourceItem(SourceItem* item) {
174 // adding item to the scene
177 // add item from the QList
178 sourceItems.append(item);
181 void GroupScene::removeSourceItem(SourceItem* item) {
182 // remove item from the viewport
184 // remove item from the QList
185 sourceItems.removeAll(item);
188 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2, bool visible) {
189 ConnectionItem* conn = NULL;
191 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
192 conn->setVisible(visible);
194 addConnectionItem(conn);
197 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
198 foreach(ConnectionItem* conn , connectionItems) {
199 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
200 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
207 void GroupScene::addConnectionItem(ConnectionItem* item) {
208 // add item from the viewport
210 // add item from the QList
211 connectionItems.append(item);
214 void GroupScene::removeConnectionItem(ConnectionItem* item) {
216 // remove connection from/to InterfaceItem
217 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
218 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
219 fromIfaceItem->removeConnectionItem(item);
220 toIfaceItem->removeConnectionItem(item);
222 // remove item from the viewport
224 // remove item from the QList
225 connectionItems.removeAll(item);
228 void GroupScene::setGroupItem(GroupItem *group) {
229 if ((groupItem == NULL) && (group != NULL)) {
235 void GroupScene::removeGroupItem() {
236 // remove item from the viewport
237 removeItem(groupItem);
241 void GroupScene::unselecteInterfaces() {
243 if (selectedInterfaces[0] != NULL) {
244 selectedInterfaces[0]->selected = false;
245 selectedInterfaces[0] = NULL;
247 if (selectedInterfaces[1] != NULL) {
248 selectedInterfaces[1]->selected = false;
249 selectedInterfaces[1] = NULL;
253 void GroupScene::updateConnectionItemsShape() {
255 foreach(ConnectionItem* conn, connectionItems){
260 void GroupScene::save(QXmlStreamWriter &writer) {
261 writer.writeStartElement("scene");
262 writer.writeAttribute("id",QString::number(id));
263 if (parentScene == NULL) {
264 writer.writeAttribute("upper_scene",QString::number(-1));
267 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
269 groupItem->save(writer);
272 writer.writeStartElement("source_items");
273 writer.writeAttribute("count",QString::number(sourceItems.length()));
274 foreach(SourceItem* item, sourceItems) {
277 writer.writeEndElement(); // source_items
279 writer.writeStartElement("block_items");
281 QList<BoxItem *> functionalBlocks;
282 QList<BoxItem *> groupBlocks;
284 foreach(BoxItem *item, boxItems){
285 if(item->getRefBlock()->isFunctionalBlock()){
286 functionalBlocks.append(item);
287 } else if(item->getRefBlock()->isGroupBlock()){
288 groupBlocks.append(item);
291 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
292 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
294 foreach(BoxItem* item, functionalBlocks) {
297 foreach(BoxItem* item, groupBlocks) {
300 writer.writeEndElement(); // block_items
301 writer.writeEndElement(); // scene