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"
14 GroupScene::GroupScene(GroupScene *_parentScene, GroupWidget *_window, Dispatcher* _dispatcher, Parameters* _params, bool _topScene, QObject *parent) : QGraphicsScene(parent) {
15 dispatcher = _dispatcher;
17 parentScene = _parentScene;
18 if (parentScene != NULL) {
19 parentScene->addChildScene(this);
27 selectedInterfaces[0] = NULL;
28 selectedInterfaces[1] = NULL;
31 GroupScene::~GroupScene() {
32 static QString fctName = "GroupScene::~GroupScene()";
34 cout << "call to " << qPrintable(fctName) << endl;
38 connectionItems.clear();
43 void GroupScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
45 //QGraphicsScene::mouseMoveEvent(event);
47 QPointF p = event->scenePos();
48 cout << p.x() << "," << p.y() << endl;
53 void GroupScene::setSelectedInterface(int id, InterfaceItem* iface) {
54 if ((id < 1)|| (id > 2)) return;
55 selectedInterfaces[id-1] = iface;
58 InterfaceItem* GroupScene::getSelectedInterface(int id) {
59 if ((id < 1)|| (id > 2)) return NULL;
60 return selectedInterfaces[id-1];
63 QList<BoxItem *> GroupScene::getSelectedBlocks() {
65 foreach(BoxItem *item, boxItems){
66 if (item->isSelected()){
73 int GroupScene::setItemsId(int countInit) {
74 int counter = countInit;
75 groupItem->setId(counter++);
77 foreach(StimuliItem *item, stimuliItems){
78 item->setId(counter++);
81 foreach(BoxItem *item, boxItems){
82 item->setId(counter++);
87 int GroupScene::setInterfacesId(int countInit) {
88 int counter = countInit;
89 foreach(InterfaceItem* inter, groupItem->getInterfaces()){
90 inter->setId(counter++);
93 foreach(StimuliItem *item, stimuliItems){
94 foreach(InterfaceItem* inter, item->getInterfaces()){
95 inter->setId(counter++);
99 foreach(BoxItem *item, boxItems){
100 foreach(InterfaceItem* inter, item->getInterfaces()){
101 inter->setId(counter++);
107 BoxItem *GroupScene::createBoxItem(AbstractBlock *block, BoxItem::Position position, int lock, BoxItem::SpanType span) {
109 BoxItem* item = new BoxItem(block,dispatcher,params,groupItem, lock, span, position);
111 // add item from the QList
112 boxItems.append(item);
113 // repainting the group
114 groupItem->updateShape();
117 // position the new block
119 if (hPos == BoxItem::Left) {
122 else if (hPos == BoxItem::Center) {
123 x = (groupItem->getWidth()-item->getTotalWidth())/2.0;
125 else if (hPos == BoxItem::Right) {
126 x = groupItem->getWidth()-item->getTotalWidth();
128 if (vPos == BoxItem::Top) {
131 else if (vPos == BoxItem::Center) {
132 y = (groupItem->getHeight()-item->getTotalHeight())/2.0;
134 else if (vPos == BoxItem::Bottom) {
135 y = groupItem->getHeight()-item->getTotalHeight();
138 newPos = newPos-item->getOriginPoint();
139 item->moveTo(newPos);
144 void GroupScene::addBoxItem(BoxItem* item) {
145 // add item from the QList
146 boxItems.append(item);
147 // repainting the group
148 groupItem->updateShape();
151 void GroupScene::removeBoxItem(BoxItem* item) {
152 // remove item from the viewport
154 // remove item from the QList
155 boxItems.removeAll(item);
156 // repainting the group
157 groupItem->updateShape();
160 BoxItem* GroupScene::searchBoxItemByName(QString name) {
161 foreach(BoxItem* item, boxItems) {
162 if (item->getRefBlock()->getName() == name) {
169 StimuliItem *GroupScene::createStimuliItem(AbstractBlock *block) {
171 StimuliItem* item = new StimuliItem(block,dispatcher,params);
172 // adding item to the scene
175 // add item from the QList
176 stimuliItems.append(item);
177 // center the new block
178 QPointF groupPos = groupItem->pos();
179 QPointF newPos(groupPos.x()-item->getTotalWidth()-100, groupPos.y());
180 newPos = newPos-item->getOriginPoint();
181 item->moveTo(newPos);
185 void GroupScene::addStimuliItem(StimuliItem* item) {
186 // adding item to the scene
189 // add item from the QList
190 stimuliItems.append(item);
193 void GroupScene::removeStimuliItem(StimuliItem* item) {
194 // remove item from the viewport
196 // remove item from the QList
197 stimuliItems.removeAll(item);
200 void GroupScene::createConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2, bool visible) {
201 ConnectionItem* conn = NULL;
203 conn = new ConnectionItem(iface1,iface2, dispatcher, params);
204 conn->setVisible(visible);
206 addConnectionItem(conn);
209 ConnectionItem* GroupScene::searchConnectionItem(InterfaceItem *iface1, InterfaceItem *iface2) {
210 foreach(ConnectionItem* conn , connectionItems) {
211 if ( ((conn->getFromInterfaceItem() == iface1) && (conn->getToInterfaceItem() == iface2)) ||
212 ((conn->getFromInterfaceItem() == iface2) && (conn->getToInterfaceItem() == iface1))) {
219 void GroupScene::addConnectionItem(ConnectionItem* item) {
220 // add item from the viewport
222 // add item from the QList
223 connectionItems.append(item);
226 void GroupScene::removeConnectionItem(ConnectionItem* item) {
228 // remove connection from/to InterfaceItem
229 InterfaceItem* fromIfaceItem = item->getFromInterfaceItem();
230 InterfaceItem* toIfaceItem = item->getToInterfaceItem();
231 fromIfaceItem->removeConnectionItem(item);
232 toIfaceItem->removeConnectionItem(item);
234 // remove item from the viewport
236 // remove item from the QList
237 connectionItems.removeAll(item);
240 void GroupScene::setGroupItem(GroupItem *group) {
241 if ((groupItem == NULL) && (group != NULL)) {
247 void GroupScene::removeGroupItem() {
248 // remove item from the viewport
249 removeItem(groupItem);
253 void GroupScene::unselecteInterfaces() {
255 if (selectedInterfaces[0] != NULL) {
256 selectedInterfaces[0]->selected = false;
257 selectedInterfaces[0] = NULL;
259 if (selectedInterfaces[1] != NULL) {
260 selectedInterfaces[1]->selected = false;
261 selectedInterfaces[1] = NULL;
265 void GroupScene::updateConnectionItemsShape() {
267 foreach(ConnectionItem* conn, connectionItems){
272 void GroupScene::save(QXmlStreamWriter &writer) {
273 writer.writeStartElement("scene");
274 writer.writeAttribute("id",QString::number(id));
275 if (parentScene == NULL) {
276 writer.writeAttribute("upper_scene",QString::number(-1));
279 writer.writeAttribute("upper_scene",QString::number(parentScene->getId()));
282 QString clkList = "";
283 QList<double> lst = params->getGraph()->getClocks();
284 foreach(double d, lst) {
285 clkList += QString::number(d)+",";
288 writer.writeAttribute("clkList",clkList);
291 groupItem->save(writer);
294 writer.writeStartElement("source_items");
295 writer.writeAttribute("count",QString::number(stimuliItems.length()));
296 foreach(StimuliItem* item, stimuliItems) {
299 writer.writeEndElement(); // source_items
301 writer.writeStartElement("block_items");
303 QList<BoxItem *> functionalBlocks;
304 QList<BoxItem *> groupBlocks;
306 foreach(BoxItem *item, boxItems){
307 if(item->getRefBlock()->isFunctionalBlock()){
308 functionalBlocks.append(item);
309 } else if(item->getRefBlock()->isGroupBlock()){
310 groupBlocks.append(item);
313 writer.writeAttribute("functional_count",QString::number(functionalBlocks.length()));
314 writer.writeAttribute("group_count",QString::number(groupBlocks.length()));
316 foreach(BoxItem* item, functionalBlocks) {
319 foreach(BoxItem* item, groupBlocks) {
322 writer.writeEndElement(); // block_items
323 writer.writeEndElement(); // scene