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 void GroupScene::createBlockItem(AbstractBlock *block) {
89 BoxItem* blockItem = new BoxItem(block,dispatcher,params,groupItem);
90 blockItem->setZValue(1);
91 addBlockItem(blockItem);
94 void GroupScene::addBlockItem(BoxItem* item) {
95 // add item from the viewport
97 // add item from the QList
98 blockItems.append(item);
99 // repainting the group
100 groupItem->updateShape();
101 // center the new block
102 QPointF newPos((groupItem->getWidth()-item->getTotalWidth())/2.0, (groupItem->getHeight()-item->getTotalHeight())/2.0);
103 newPos = newPos-item->getOriginPoint();
104 item->moveTo(newPos);
107 void GroupScene::removeBlockItem(BoxItem* item) {
108 // remove item from the viewport
110 // remove item from the QList
111 blockItems.removeAll(item);
112 // repainting the group
113 groupItem->updateShape();
116 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
117 ConnectionItem* conn = new ConnectionItem(iface1,iface2, dispatcher, params, groupItem);
118 addConnectionItem(conn);
121 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
122 foreach(ConnectionItem* conn , connectionItems) {
123 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
124 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
130 void GroupScene::addConnectionItem(ConnectionItem* item) {
131 // add item from the viewport
133 // add item from the QList
134 connectionItems.append(item);
137 void GroupScene::removeConnectionItem(ConnectionItem* item) {
138 // remove item from the viewport
140 // remove item from the QList
141 connectionItems.removeAll(item);
144 void GroupScene::setGroupItem(GroupItem *group) {
145 if ((groupItem == NULL) && (group != NULL)) {
151 void GroupScene::removeGroupItem() {
152 // remove item from the viewport
153 removeItem(groupItem);
157 QList<ConnectionItem *> GroupScene::getInterfaceConnections(InterfaceItem *item) {
158 QList<ConnectionItem*> list;
159 foreach(ConnectionItem *conn, connectionItems){
160 if(conn->getFromInterfaceItem() == item || conn->getToInterfaceItem() == item){
167 void GroupScene::unselecteInterfaces() {
169 if (selectedInterfaces[0] != NULL) {
170 selectedInterfaces[0]->selected = false;
171 selectedInterfaces[0] == NULL;
173 if (selectedInterfaces[1] != NULL) {
174 selectedInterfaces[1]->selected = false;
175 selectedInterfaces[1] == NULL;
179 void GroupScene::updateConnectionItemsShape() {
181 foreach(ConnectionItem* conn, connectionItems){
186 void GroupScene::save(QXmlStreamWriter &writer) {
187 writer.writeStartElement("scene");
188 writer.writeAttribute("id",QString::number(id));
189 groupItem->save(writer);
191 writer.writeStartElement("block_items");
193 QList<BoxItem *> functionalBlocks;
194 QList<BoxItem *> groupBlocks;
196 foreach(BoxItem *item, blockItems){
197 if(item->getRefBlock()->isFunctionalBlock()){
198 functionalBlocks.append(item);
199 } else if(item->getRefBlock()->isGroupBlock()){
200 groupBlocks.append(item);
203 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
204 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
206 foreach(BoxItem* item, functionalBlocks) {
209 foreach(BoxItem* item, groupBlocks) {
212 writer.writeEndElement(); // block_items
213 writer.writeEndElement(); // scene