graphicsinvocationitem.cpp
1 /*
2  name: tools/editor/graphicsinvocationitem.cpp
3 
4  This file is part of ARCS - Augmented Reality Component System
5  (version 2-current), written by Jean-Yves Didier
6  for IBISC Laboratory (http://www.ibisc.univ-evry.fr)
7 
8  Copyright (C) 2013 Université d'Evry-Val d'Essonne
9 
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 2 of the License, or
13  (at your option) any later version.
14 
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with this program. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Please send bugreports with examples or suggestions to
25  jean-yves.didier__at__ibisc.univ-evry.fr
26 */
27 
28 
29 #include "graphicsinvocationitem.h"
30 #include <QBrush>
31 #include <QStyleOptionGraphicsItem>
32 #include <QGraphicsRectItem>
33 #include <QGraphicsSimpleTextItem>
34 #include <iostream>
35 #include "sheetview.h"
36 
37 GraphicsInvocationItem::GraphicsInvocationItem(QGraphicsItem *parent,GraphicsInvocationItem::InvocationType itype, ARCSInit &i) :
38  QGraphicsPathItem(parent), init(i)
39 {
40 #if QT_VERSION >= 0x050000
41  setAcceptHoverEvents(true);
42 #else
43  setAcceptsHoverEvents(true);
44 #endif
45  setFlag(ItemIsSelectable,true);
46  invocationType = itype;
47  defaultPen = pen();
48  drawPath();
49  markedForDeletion = false;
50  rectInvocation = new QGraphicsRectItem(this);
51  textInvocation = new QGraphicsSimpleTextItem(rectInvocation);
52  textNumber = new QGraphicsSimpleTextItem(this);
53  rectInvocation->setVisible(false);
54  QString invocationTypeName;
55 
56  switch(invocationType)
57  {
58  case Pre:
59  invocationTypeName = "Pre-connection" ;
60  break;
61  case Post:
62  invocationTypeName = "Post-connection" ;
63  break;
64  case Cleanup:
65  invocationTypeName = "Clean-up invocation";
66  break;
67  }
68 
69 
70  textInvocation->setText(invocationTypeName +
71  "\nType:\"" + init.getValueType() +
72  "\";\nValue:\"" + init.getValueString() + "\"");
73  rectInvocation->setBrush(QBrush(Qt::lightGray));
74  rectInvocation->setRect(textInvocation->boundingRect().adjusted(-2,-2,2,2));
75  QSizeF s = rectInvocation->rect().size();
76  rectInvocation->setPos(-s.width()-15,10);
77 }
78 
79 
80 GraphicsInvocationItem::~GraphicsInvocationItem()
81 {
82  setSelected(false);
83  if (markedForDeletion)
84  {
85  SheetView* view = dynamic_cast<SheetView*>(scene()->views()[0]);
86  if (!view)
87  return ;
88 
89  std::cout << "We shall remove this initialisation from sheet" << std::endl;
90 
91  switch(invocationType)
92  {
93  case Pre:
94  view->getSheet().removePreconnect(init);
95  break;
96  case Post:
97  view->getSheet().removePostconnect(init);
98  break;
99  case Cleanup:
100  view->getSheet().removeCleanup(init);
101  break;
102  }
103  }
104 }
105 
106 
107 
108 void GraphicsInvocationItem::drawPath()
109 {
110  QPainterPath pp;
111 
112  switch(invocationType)
113  {
114  case Pre: // then draw an event
115  {
116  QBrush br(Qt::white);
117  setBrush(br);
118  pp.moveTo(-5,0);
119  pp.lineTo(-10,0);
120  pp.lineTo(-12.5,-5);
121  pp.lineTo(-20,-5);
122  pp.lineTo(-17.5,0);
123  pp.lineTo(-20,5);
124  pp.lineTo(-12.5,5);
125  pp.lineTo(-10,0);
126  setPath(pp);
127  break;
128  }
129  case Post: // then draw a filled circle
130  {
131  QBrush br(Qt::black);
132  setBrush(br);
133  pp.moveTo(-5,0);
134  pp.lineTo(-10,0);
135  //pp.moveTo(-30,0);
136  pp.addEllipse(QPointF(-15,0),5,5);
137  setPath(pp);
138  break;
139  }
140  case Cleanup: // then draw a cross circled
141  {
142  QBrush br(Qt::white);
143  setBrush(br);
144  pp.moveTo(-5,0);
145  pp.lineTo(-10,0);
146  pp.addEllipse(QPointF(-15,0),5,5);
147  pp.moveTo(-11.47,3.03);
148  pp.lineTo(-18.53,-3.03);
149  pp.moveTo(-18.53,3.03);
150  pp.lineTo(-11.47,-3.03);
151  setPath(pp);
152  break;
153  }
154  }
155 }
156 
157 
158 void GraphicsInvocationItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
159 {
160  QStyleOptionGraphicsItem myOptions(*option);
161  if (myOptions.state & (QStyle::State_Selected))
162  {
163  QPen myPen;
164  myPen.setWidth(2);
165  myPen.setColor(Qt::darkRed);
166  setPen(myPen);
167  myOptions.state = myOptions.state ^ QStyle::State_Selected ;
168  }
169  else
170  {
171  setPen(defaultPen);
172  }
173  QGraphicsPathItem::paint(painter,& myOptions, widget );
174 
175 }
176 
177 void GraphicsInvocationItem::hoverEnterEvent(QGraphicsSceneHoverEvent */*event*/)
178 {
179  SheetView* view = dynamic_cast<SheetView*>(scene()->views()[0]);
180  if (!view)
181  return ;
182 
183  int idx = 0;
184  switch(invocationType)
185  {
186  case Pre:
187  idx = view->getSheet().getPreconnectIndex(init);
188  break;
189  case Post:
190  idx = view->getSheet().getPostconnectIndex(init);
191  break;
192  case Cleanup:
193  idx = view->getSheet().getCleanupIndex(init);
194  break;
195  }
196 
197  textNumber->setText(QString::number(idx));
198  QFont font = textNumber->font();
199  font.setBold(true);
200  textNumber->setFont(font);
201  textNumber->setBrush(QBrush(Qt::darkMagenta));
202  QSizeF s = textNumber->boundingRect().size();
203  textNumber->setPos(-s.width()-25,-s.height()/2);
204 
205  textNumber->setVisible(true);
206  rectInvocation->setVisible(true);
207 }
208 
209 void GraphicsInvocationItem::hoverLeaveEvent(QGraphicsSceneHoverEvent */*event*/)
210 {
211  textNumber->setVisible(false);
212  rectInvocation->setVisible(false);
213 }
214 
Desribes an initialization over a component.
Definition: arcsinit.h:46