NCL Composer  0.1.5
 All Classes Functions Variables Pages
qnstgraphicsinterface.cpp
1 #include "qnstgraphicsinterface.h"
2 
3 QnstGraphicsInterface::QnstGraphicsInterface(QnstGraphicsEntity* parent)
5 {
6  setncgType(Qncg::Interface);
7  setnstType(Qnst::Interface);
8 
9  setResizable(false);
10 
11  setnstId("");
12 
13  setRefer(false);
14 
15  /* Default size for interface */
16  setTop(0);
17  setLeft(0);
18  setWidth(DEFAULT_INTERFACE_WIDTH);
19  setHeight(DEFAULT_INTERFACE_HEIGHT);
20 }
21 
22 QnstGraphicsInterface::~QnstGraphicsInterface()
23 {
24 
25 }
26 
27 bool QnstGraphicsInterface::isRefer()
28 {
29  return _isRefer;
30 }
31 
32 void QnstGraphicsInterface::setRefer(bool isRefer)
33 {
34  _isRefer = isRefer;
35 }
36 
37 void QnstGraphicsInterface::updateToolTip()
38 {
39  QString tip = "";
40  QString name = (getnstId() != "" ? getnstId() : "?");
41 
42  if (getnstType() == Qnst::Port)
43  {
44  tip += "Port ("+name+")";
45  }
46  else if (getnstType() == Qnst::Area)
47  {
48  tip += "Area ("+name+")";
49  }
50  else if (getnstType() == Qnst::Property)
51  {
52  tip += "Property ("+name+")";
53  }
54  else
55  {
56  tip += "Interface ("+name+")";
57  }
58 
59  setToolTip(tip);
60 }
61 
62 void QnstGraphicsInterface::setProperties(const QMap<QString, QString> &props)
63 {
64  QnstGraphicsEntityWithEdges::setProperties(props);
65 
66  if(props.contains("isRefer"))
67  {
68  if(props["isRefer"] == "true")
69  setRefer(true);
70  else
71  setRefer(false);
72  }
73 }
74 
75 void QnstGraphicsInterface::adjust(bool avoidCollision)
76 {
77  QnstGraphicsEntity* parent = getnstGraphicsParent();
78 
79  if (parent != NULL)
80  {
81  adjustToBorder();
82 
83  if (avoidCollision)
84  {
85  int colliding;
86  int maxInter = 10, inter = 0;
87  do
88  {
89  if(inter > maxInter) break;
90  inter++;
91 
92  colliding = false;
93  foreach(QnstGraphicsEntity *entity,
94  getnstGraphicsParent()->getnstGraphicsEntities())
95  {
96  if(this != entity && entity->getncgType() == Qncg::Interface)
97  {
98  qreal n = 0;
99  qreal i = 0.0;
100 
101  entity->setSelectable(false); update();
102  // check collision
103  while(collidesWithItem(entity, Qt::IntersectsItemBoundingRect))
104  {
105 
106  QPointF pa(getLeft()+getWidth()/2, getTop()+getHeight()/2);
107  QPointF pb(entity->getWidth()/2, entity->getHeight()/2);
108 
109  QLineF line(pa, pb);
110 
111  line.setAngle(qrand()%360);
112 
113  i += (double)(qrand()%100)/10000.0;
114 
115  setTop(getTop()+line.pointAt(i/2).y()-pa.y());
116  setLeft(getLeft()+line.pointAt(i/2).x()-pa.x());
117 
118  if (++n > 1000) // avoid infinity loop
119  {
120  n = -1;
121  break;
122  }
123  }
124 
125  adjustToBorder();
126 
127  entity->setSelectable(true); update();
128  }
129  }
130 
131  foreach(QnstGraphicsEntity *entity,
132  getnstGraphicsParent()->getnstGraphicsEntities())
133  {
134  if(collidesWithItem(entity, Qt::IntersectsItemBoundingRect))
135  {
136  colliding = true;
137  }
138  }
139  }
140  while(colliding);
141  }
142  }
143 
144  foreach(QnstGraphicsEntity* edges, getnstGraphicsEdges())
145  {
146  if (edges->getnstType() == Qnst::Reference ||
147  edges->getnstType() == Qnst::Link ||
148  edges->getnstType() == Qnst::Mapping ||
149  edges->getnstType() == Qnst::Action ||
150  edges->getnstType() == Qnst::Condition)
151  {
152  edges->adjust();
153  }
154  }
155 
156  // redrawing
157  if (scene() != NULL)
158  scene()->update();
159 }
160 
161 void QnstGraphicsInterface::adjustToBorder()
162 {
163  QnstGraphicsEntity* parent = getnstGraphicsParent();
164 
165  if (parent != NULL)
166  {
167  // setting
168  QPointF pointa(parent->getWidth()/2, parent->getHeight()/2);
169  QPointF pointb(getLeft() + getWidth()/2, getTop() + getHeight()/2);
170 
171  if (pointa == pointb)
172  {
173  pointb.setX(pointa.x());
174  pointb.setY(pointa.y() - 10);
175  }
176 
177  QLineF line(pointa,pointb);
178 
179  // adjusting
180  if (parent->contains(pointb))
181  {
182  QPointF pointn = pointb;
183 
184  qreal index = 1.0;
185 
186  if (parent->contains(line.pointAt(index+0.01)))
187  {
188  while(parent->contains(pointn))
189  {
190  index += 0.01;
191 
192  pointn = line.pointAt(index);
193  }
194  }
195 
196  setTop(pointn.y() - getHeight()/2);
197  setLeft(pointn.x() - getWidth()/2);
198 
199  }
200  else
201  {
202  QPointF pointn = pointb;
203 
204  qreal index = 1.0;
205 
206  if (!parent->contains(line.pointAt(index-0.01)))
207  {
208  while(!parent->contains(pointn))
209  {
210  index -= 0.01;
211 
212  pointn = line.pointAt(index);
213  }
214  }
215 
216  setTop(pointn.y() - getHeight()/2);
217  setLeft(pointn.x() - getWidth()/2);
218  }
219  }
220 }
221 
222 void QnstGraphicsInterface::move(QGraphicsSceneMouseEvent* event)
223 {
224  // setting
225  qreal x = getLeft();
226  qreal y = getTop();
227 
228  QnstGraphicsEntity* parent = getnstGraphicsParent();
229 
230  qreal minx;
231  qreal miny;
232 
233  if (parent != NULL)
234  {
235  minx = 4;
236  miny = 4;
237  }
238  else
239  {
240  minx = 0;
241  miny = 0;
242  }
243 
244  qreal maxx;
245  qreal maxy;
246 
247  if (parent != NULL)
248  {
249  maxx = parent->getWidth() - getWidth() - 4;
250  maxy = parent->getHeight() - getHeight() - 4;
251  }
252  else
253  {
254  maxx = scene()->width() - getWidth();
255  maxy = scene()->height() - getHeight();
256  }
257 
258  qreal dx = event->pos().x() - getPressLeft(); // (x1 - x0)
259  qreal dy = event->pos().y() - getPressTop(); // (y1 - y0)
260 
261  qreal nextx = x + dx;
262  qreal nexty = y + dy;
263 
264  // moving
265  setMoveTop(nexty);
266  setMoveLeft(nextx);
267 
268  // redrawing
269  if (scene() != NULL)
270  scene()->update();
271 }
272 
273 void QnstGraphicsInterface::resize(QGraphicsSceneMouseEvent* event)
274 {
275  // setting
276  qreal x = getLeft();
277  qreal y = getTop();
278  qreal w = getWidth();
279  qreal h = getHeight();
280 
281  QnstGraphicsEntity* parent = getnstGraphicsParent();
282 
283  qreal minx;
284  qreal miny;
285  qreal minw;
286  qreal minh;
287 
288  if (parentItem() != NULL)
289  {
290  minx = 4;
291  miny = 4;
292  minw = -1; // not used
293  minh = -1; // not used
294  }
295  else
296  {
297  minx = 0;
298  miny = 0;
299  minw = -1; // not used
300  minh = -1; // not used
301  }
302 
303  qreal maxx;
304  qreal maxy;
305  qreal maxw;
306  qreal maxh;
307 
308  if (parentItem() != NULL)
309  {
310  maxx = parent->getWidth() - getWidth() - 4;
311  maxy = parent->getHeight() - getHeight() - 4;
312  maxw = parent->getWidth() - 4;
313  maxh = parent->getHeight() - 4;
314  }
315  else
316  {
317  maxx = scene()->width() - getWidth();
318  maxy = scene()->height() - getHeight();
319  maxw = scene()->width();
320  maxh = scene()->height();
321  }
322 
323  qreal dx = event->pos().x() - getPressLeft(); // (x1 - x0)
324  qreal dy = event->pos().y() - getPressTop(); // (y1 - y0)
325  qreal dw = -dx;
326  qreal dh = -dy;
327 
328  qreal nextx = x + dx;
329  qreal nexty = y + dy;
330  qreal nextw = w + dw;
331  qreal nexth = h + dh;
332 
333  // adjusting
334  switch(getncgResize())
335  {
336  case Qncg::TopLeft:
337  {
338  break;
339  }
340 
341  case Qncg::Top:
342  {
343  nextx = x; // fixed x
344  nextw = w; // fixed width
345 
346  break;
347  }
348 
349  case Qncg::TopRight:
350  {
351  nextx = x; // fixed x
352  nextw = w - dw;
353 
354  break;
355  }
356 
357  case Qncg::Right:
358  {
359  nextx = x; // fixed x
360  nextw = w - dw;
361 
362  nexty = y; // fixed y
363  nexth = h; // fixed height
364 
365  break;
366  }
367 
368  case Qncg::BottomRight:
369  {
370  nextx = x; // fixed x
371  nextw = w - dw;
372 
373  nexty = y; // fixed y
374  nexth = h - dh;
375 
376  break;
377  }
378 
379  case Qncg::Bottom:
380  {
381  nextx = x; // fixed x
382  nextw = w; // fixed width
383 
384  nexty = y; // fixed y
385  nexth = h - dh;
386 
387  break;
388  }
389 
390  case Qncg::BottomLeft:
391  {
392  nexty = y; // fixed y
393  nexth = h - dh;
394  break;
395  }
396 
397  case Qncg::Left:
398  {
399  nexty = y; // fixed y
400  nexth = h; // fixed height
401 
402  break;
403  }
404  }
405 
406  // resizing
407  setResizeTop(nexty);
408  setResizeLeft(nextx);
409  setResizeWidth(nextw);
410  setResizeHeight(nexth);
411 
412  // redrawing
413  if (scene() != NULL)
414  scene()->update();
415 }