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* blockItem = new BoxItem(block,dispatcher,params,groupItem);
90 blockItem->setZValue(1);
91 addBlockItem(blockItem);
95 void GroupScene::addBlockItem(BoxItem* item) {
96 // add item from the viewport
98 // add item from the QList
99 blockItems.append(item);
100 // repainting the group
101 groupItem->updateShape();
102 // center the new block
103 QPointF newPos((groupItem->getWidth()-item->getTotalWidth())/2.0, (groupItem->getHeight()-item->getTotalHeight())/2.0);
104 newPos = newPos-item->getOriginPoint();
105 item->moveTo(newPos);
108 void GroupScene::removeBlockItem(BoxItem* item) {
109 // remove item from the viewport
111 // remove item from the QList
112 blockItems.removeAll(item);
113 // repainting the group
114 groupItem->updateShape();
117 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
118 ConnectionItem* conn = new ConnectionItem(iface1,iface2, dispatcher, params, groupItem);
119 addConnectionItem(conn);
122 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
123 foreach(ConnectionItem* conn , connectionItems) {
124 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
125 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
131 void GroupScene::addConnectionItem(ConnectionItem* item) {
132 // add item from the viewport
134 // add item from the QList
135 connectionItems.append(item);
138 void GroupScene::removeConnectionItem(ConnectionItem* item) {
139 // remove item from the viewport
141 // remove item from the QList
142 connectionItems.removeAll(item);
145 void GroupScene::setGroupItem(GroupItem *group) {
146 if ((groupItem == NULL) && (group != NULL)) {
152 void GroupScene::removeGroupItem() {
153 // remove item from the viewport
154 removeItem(groupItem);
158 QList<ConnectionItem *> GroupScene::getInterfaceConnections(InterfaceItem *item) {
159 QList<ConnectionItem*> list;
160 foreach(ConnectionItem *conn, connectionItems){
161 if(conn->getFromInterfaceItem() == item || conn->getToInterfaceItem() == item){
168 void GroupScene::unselecteInterfaces() {
170 if (selectedInterfaces[0] != NULL) {
171 selectedInterfaces[0]->selected = false;
172 selectedInterfaces[0] == NULL;
174 if (selectedInterfaces[1] != NULL) {
175 selectedInterfaces[1]->selected = false;
176 selectedInterfaces[1] == NULL;
180 void GroupScene::updateConnectionItemsShape() {
182 foreach(ConnectionItem* conn, connectionItems){
187 void GroupScene::save(QXmlStreamWriter &writer) {
188 writer.writeStartElement("scene");
189 writer.writeAttribute("id",QString::number(id));
190 groupItem->save(writer);
192 writer.writeStartElement("block_items");
194 QList<BoxItem *> functionalBlocks;
195 QList<BoxItem *> groupBlocks;
197 foreach(BoxItem *item, blockItems){
198 if(item->getRefBlock()->isFunctionalBlock()){
199 functionalBlocks.append(item);
200 } else if(item->getRefBlock()->isGroupBlock()){
201 groupBlocks.append(item);
204 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
205 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
207 foreach(BoxItem* item, functionalBlocks) {
210 foreach(BoxItem* item, groupBlocks) {
213 writer.writeEndElement(); // block_items
214 writer.writeEndElement(); // scene