1 #include "GroupScene.h"
3 #include "Dispatcher.h"
4 #include "Parameters.h"
5 #include "GroupWidget.h"
8 #include "ConnectionItem.h"
9 #include "InterfaceItem.h"
10 #include "AbstractBlock.h"
12 GroupScene::GroupScene(GroupScene *_parentScene, GroupWidget *_window, Dispatcher* _dispatcher, Parameters* _params, bool _topScene, QObject *parent) : QGraphicsScene(parent) {
13 dispatcher = _dispatcher;
15 parentScene = _parentScene;
16 if (parentScene != NULL) {
17 parentScene->addChildScene(this);
25 selectedInterfaces[0] = NULL;
26 selectedInterfaces[1] = NULL;
29 GroupScene::~GroupScene() {
31 connectionItems.clear();
35 void GroupScene::setSelectedInterface(int id, InterfaceItem* iface) {
36 if ((id < 1)|| (id > 2)) return;
37 selectedInterfaces[id-1] = iface;
40 InterfaceItem* GroupScene::getSelectedInterface(int id) {
41 if ((id < 1)|| (id > 2)) return NULL;
42 return selectedInterfaces[id-1];
45 QList<BoxItem *> GroupScene::getSelectedBlocks() {
47 foreach(BoxItem *item, blockItems){
48 if (item->isSelected()){
55 int GroupScene::setItemsId(int countInit) {
56 int counter = countInit;
57 groupItem->setId(counter++);
58 foreach(BoxItem *item, blockItems){
59 item->setId(counter++);
64 int GroupScene::setInterfacesId(int countInit) {
65 int counter = countInit;
66 foreach(InterfaceItem* inter, groupItem->getInterfaces()){
67 inter->setId(counter++);
69 foreach(BoxItem *item, blockItems){
70 foreach(InterfaceItem* inter, item->getInterfaces()){
71 inter->setId(counter++);
77 QList<AbstractBoxItem*> GroupScene::getGroupAndBlocks() {
79 QList<AbstractBoxItem*> lst;
80 lst.append(groupItem);
81 foreach(BoxItem *item, blockItems){
87 BoxItem *GroupScene::createBlockItem(AbstractBlock *block) {
89 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem);
91 // add item from the QList
92 blockItems.append(item);
93 // repainting the group
94 groupItem->updateShape();
95 // center the new block
96 QPointF newPos((groupItem->getWidth()-item->getTotalWidth())/2.0, (groupItem->getHeight()-item->getTotalHeight())/2.0);
97 newPos = newPos-item->getOriginPoint();
103 void GroupScene::addBlockItem(BoxItem* item) {
104 // add item from the QList
105 blockItems.append(item);
106 // repainting the group
107 groupItem->updateShape();
110 void GroupScene::removeBlockItem(BoxItem* item) {
111 // remove item from the viewport
113 // remove item from the QList
114 blockItems.removeAll(item);
115 // repainting the group
116 groupItem->updateShape();
119 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
120 ConnectionItem* conn = new ConnectionItem(iface1,iface2, dispatcher, params, groupItem);
121 addConnectionItem(conn);
124 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
125 foreach(ConnectionItem* conn , connectionItems) {
126 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
127 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
133 void GroupScene::addConnectionItem(ConnectionItem* item) {
134 // add item from the viewport
136 // add item from the QList
137 connectionItems.append(item);
140 void GroupScene::removeConnectionItem(ConnectionItem* item) {
141 // remove item from the viewport
143 // remove item from the QList
144 connectionItems.removeAll(item);
147 void GroupScene::setGroupItem(GroupItem *group) {
148 if ((groupItem == NULL) && (group != NULL)) {
154 void GroupScene::removeGroupItem() {
155 // remove item from the viewport
156 removeItem(groupItem);
160 QList<ConnectionItem *> GroupScene::getInterfaceConnections(InterfaceItem *item) {
161 QList<ConnectionItem*> list;
162 foreach(ConnectionItem *conn, connectionItems){
163 if(conn->getFromInterfaceItem() == item || conn->getToInterfaceItem() == item){
170 void GroupScene::unselecteInterfaces() {
172 if (selectedInterfaces[0] != NULL) {
173 selectedInterfaces[0]->selected = false;
174 selectedInterfaces[0] == NULL;
176 if (selectedInterfaces[1] != NULL) {
177 selectedInterfaces[1]->selected = false;
178 selectedInterfaces[1] == NULL;
182 void GroupScene::updateConnectionItemsShape() {
184 foreach(ConnectionItem* conn, connectionItems){
189 void GroupScene::save(QXmlStreamWriter &writer) {
190 writer.writeStartElement("scene");
191 writer.writeAttribute("id",QString::number(id));
192 if (parentScene == NULL) {
193 writer.writeAttribute("upper_scene",QString::number(-1));
196 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
198 groupItem->save(writer);
200 writer.writeStartElement("block_items");
202 QList<BoxItem *> functionalBlocks;
203 QList<BoxItem *> groupBlocks;
205 foreach(BoxItem *item, blockItems){
206 if(item->getRefBlock()->isFunctionalBlock()){
207 functionalBlocks.append(item);
208 } else if(item->getRefBlock()->isGroupBlock()){
209 groupBlocks.append(item);
212 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
213 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
215 foreach(BoxItem* item, functionalBlocks) {
218 foreach(BoxItem* item, groupBlocks) {
221 writer.writeEndElement(); // block_items
222 writer.writeEndElement(); // scene