1 #include "GroupScene.h"
3 #include "Dispatcher.h"
4 #include "Parameters.h"
5 #include "GroupWidget.h"
8 #include "StimuliItem.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(StimuliItem *item, stimuliItems){
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(StimuliItem *item, stimuliItems){
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 position, int lock, BoxItem::SpanType span) {
108 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem, lock, span, position);
110 // add item from the QList
111 boxItems.append(item);
112 // repainting the group
113 groupItem->updateShape();
116 // position the new block
118 if (hPos == BoxItem::Left) {
121 else if (hPos == BoxItem::Center) {
122 x = (groupItem->getWidth()-item->getTotalWidth())/2.0;
124 else if (hPos == BoxItem::Right) {
125 x = groupItem->getWidth()-item->getTotalWidth();
127 if (vPos == BoxItem::Top) {
130 else if (vPos == BoxItem::Center) {
131 y = (groupItem->getHeight()-item->getTotalHeight())/2.0;
133 else if (vPos == BoxItem::Bottom) {
134 y = groupItem->getHeight()-item->getTotalHeight();
137 newPos = newPos-item->getOriginPoint();
138 item->moveTo(newPos);
143 void GroupScene::addBoxItem(BoxItem* item) {
144 // add item from the QList
145 boxItems.append(item);
146 // repainting the group
147 groupItem->updateShape();
150 void GroupScene::removeBoxItem(BoxItem* item) {
151 // remove item from the viewport
153 // remove item from the QList
154 boxItems.removeAll(item);
155 // repainting the group
156 groupItem->updateShape();
159 StimuliItem *GroupScene::createStimuliItem(AbstractBlock *block) {
161 StimuliItem* item = new StimuliItem(block,dispatcher,params);
162 // adding item to the scene
165 // add item from the QList
166 stimuliItems.append(item);
167 // center the new block
168 QPointF groupPos = groupItem->pos();
169 QPointF newPos(groupPos.x()-item->getTotalWidth()-100, groupPos.y());
170 newPos = newPos-item->getOriginPoint();
171 item->moveTo(newPos);
175 void GroupScene::addStimuliItem(StimuliItem* item) {
176 // adding item to the scene
179 // add item from the QList
180 stimuliItems.append(item);
183 void GroupScene::removeStimuliItem(StimuliItem* item) {
184 // remove item from the viewport
186 // remove item from the QList
187 stimuliItems.removeAll(item);
190 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2, bool visible) {
191 ConnectionItem* conn = NULL;
193 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
194 conn->setVisible(visible);
196 addConnectionItem(conn);
199 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
200 foreach(ConnectionItem* conn , connectionItems) {
201 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
202 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
209 void GroupScene::addConnectionItem(ConnectionItem* item) {
210 // add item from the viewport
212 // add item from the QList
213 connectionItems.append(item);
216 void GroupScene::removeConnectionItem(ConnectionItem* item) {
218 // remove connection from/to InterfaceItem
219 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
220 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
221 fromIfaceItem->removeConnectionItem(item);
222 toIfaceItem->removeConnectionItem(item);
224 // remove item from the viewport
226 // remove item from the QList
227 connectionItems.removeAll(item);
230 void GroupScene::setGroupItem(GroupItem *group) {
231 if ((groupItem == NULL) && (group != NULL)) {
237 void GroupScene::removeGroupItem() {
238 // remove item from the viewport
239 removeItem(groupItem);
243 void GroupScene::unselecteInterfaces() {
245 if (selectedInterfaces[0] != NULL) {
246 selectedInterfaces[0]->selected = false;
247 selectedInterfaces[0] = NULL;
249 if (selectedInterfaces[1] != NULL) {
250 selectedInterfaces[1]->selected = false;
251 selectedInterfaces[1] = NULL;
255 void GroupScene::updateConnectionItemsShape() {
257 foreach(ConnectionItem* conn, connectionItems){
262 void GroupScene::save(QXmlStreamWriter &writer) {
263 writer.writeStartElement("scene");
264 writer.writeAttribute("id",QString::number(id));
265 if (parentScene == NULL) {
266 writer.writeAttribute("upper_scene",QString::number(-1));
269 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
271 groupItem->save(writer);
274 writer.writeStartElement("source_items");
275 writer.writeAttribute("count",QString::number(stimuliItems.length()));
276 foreach(StimuliItem* item, stimuliItems) {
279 writer.writeEndElement(); // source_items
281 writer.writeStartElement("block_items");
283 QList<BoxItem *> functionalBlocks;
284 QList<BoxItem *> groupBlocks;
286 foreach(BoxItem *item, boxItems){
287 if(item->getRefBlock()->isFunctionalBlock()){
288 functionalBlocks.append(item);
289 } else if(item->getRefBlock()->isGroupBlock()){
290 groupBlocks.append(item);
293 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
294 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
296 foreach(BoxItem* item, functionalBlocks) {
299 foreach(BoxItem* item, groupBlocks) {
302 writer.writeEndElement(); // block_items
303 writer.writeEndElement(); // scene