NCL Composer  0.1.5
 All Classes Functions Variables Pages
qncggraphicsentity.cpp
1 #include "qncggraphicsentity.h"
2 
3 
4 QncgGraphicsEntity::QncgGraphicsEntity(QncgGraphicsEntity* parent)
5  : QObject(parent), QGraphicsItem(parent)
6 {
7  setncgType(Qncg::NoType);
8 
9  setncgGraphicsParent(parent);
10 
11  setFlag(QGraphicsItem::ItemIsMovable, true);
12  setFlag(QGraphicsItem::ItemIsFocusable, true);
13 
14  setAcceptHoverEvents(true);
15 
16  setMoveable(true);
17  setResizable(true);
18  setSelectable(true);
19 
20  setMoving(false);
21  setResizing(false);
22  setSelected(false);
23 
24  setTop(0);
25  setLeft(0);
26  setWidth(0);
27  setHeight(0);
28 
29  setMoveTop(0);
30  setMoveLeft(0);
31 
32  setResizeTop(0);
33  setResizeLeft(0);
34  setResizeWidth(0);
35  setResizeHeight(0);
36 
37  setPressTop(0);
38  setPressLeft(0);
39  setPressWidth(0);
40  setPressHeight(0);
41 
42  setzIndex(0);
43 }
44 
45 QncgGraphicsEntity::~QncgGraphicsEntity()
46 {
47 
48 }
49 
50 void QncgGraphicsEntity::setMoveable(bool moveable)
51 {
52  this->moveable = moveable;
53 }
54 
55 bool QncgGraphicsEntity::isMoveable() const
56 {
57  return moveable;
58 }
59 
60 void QncgGraphicsEntity::setSelectable(bool selected)
61 {
62  this->selectable = selected;
63 }
64 
65 bool QncgGraphicsEntity::isSelectable() const
66 {
67  return selectable;
68 }
69 
70 void QncgGraphicsEntity::setResizable(bool resizable)
71 {
72  this->resizable = resizable;
73 }
74 
75 bool QncgGraphicsEntity::isResizable() const
76 {
77  return resizable;
78 }
79 
80 void QncgGraphicsEntity::setMoving(bool moving)
81 {
82  this->moving = moving;
83 }
84 
85 bool QncgGraphicsEntity::isMoving() const
86 {
87  return moving;
88 }
89 
90 void QncgGraphicsEntity::setResizing(bool resizing)
91 {
92  this->resizing = resizing;
93 }
94 
95 bool QncgGraphicsEntity::isResizing() const
96 {
97  return resizing;
98 }
99 
100 void QncgGraphicsEntity::setSelected(bool selected)
101 {
102  this->selected = selected;
103 }
104 
105 bool QncgGraphicsEntity::isSelected() const
106 {
107  return selected;
108 }
109 
110 qreal QncgGraphicsEntity::getTop() const
111 {
112  return top;
113 }
114 
115 void QncgGraphicsEntity::setTop(qreal top)
116 {
117  this->top = top;
118 
119  setY(top-4);
120 }
121 
122 qreal QncgGraphicsEntity::getMoveTop() const
123 {
124  return moveTop;
125 }
126 
127 void QncgGraphicsEntity::setMoveTop(qreal moveTop)
128 {
129  this->moveTop = moveTop;
130 }
131 
132 qreal QncgGraphicsEntity::getPressTop() const
133 {
134  return pressTop;
135 }
136 
137 void QncgGraphicsEntity::setPressTop(qreal pressTop)
138 {
139  this->pressTop = pressTop;
140 }
141 
142 qreal QncgGraphicsEntity::getResizeTop() const
143 {
144  return resizeTop;
145 }
146 
147 void QncgGraphicsEntity::setResizeTop(qreal resizeTop)
148 {
149  this->resizeTop = resizeTop;
150 }
151 
152 qreal QncgGraphicsEntity::getLeft() const
153 {
154  return left;
155 }
156 
157 void QncgGraphicsEntity::setLeft(qreal left)
158 {
159  this->left = left;
160 
161  setX(left-4);
162 }
163 
164 qreal QncgGraphicsEntity::getMoveLeft() const
165 {
166  return moveLeft;
167 }
168 
169 void QncgGraphicsEntity::setMoveLeft(qreal moveLeft)
170 {
171  this->moveLeft = moveLeft;
172 }
173 
174 qreal QncgGraphicsEntity::getPressLeft() const
175 {
176  return pressLeft;
177 }
178 
179 void QncgGraphicsEntity::setPressLeft(qreal pressLeft)
180 {
181  this->pressLeft = pressLeft;
182 }
183 
184 qreal QncgGraphicsEntity::getResizeLeft() const
185 {
186  return resizeLeft;
187 }
188 
189 void QncgGraphicsEntity::setResizeLeft(qreal resizeLeft)
190 {
191  this->resizeLeft = resizeLeft;
192 }
193 
194 qreal QncgGraphicsEntity::getWidth() const
195 {
196  return width;
197 }
198 
199 void QncgGraphicsEntity::setWidth(qreal width)
200 {
201  this->width = width;
202 }
203 
204 qreal QncgGraphicsEntity::getPressWidth() const
205 {
206  return pressWidth;
207 }
208 
209 void QncgGraphicsEntity::setPressWidth(qreal pressWidth)
210 {
211  this->pressWidth = pressWidth;
212 }
213 
214 qreal QncgGraphicsEntity::getResizeWidth() const
215 {
216  return resizeWidth;
217 }
218 
219 void QncgGraphicsEntity::setResizeWidth(qreal resizeWidth)
220 {
221  this->resizeWidth = resizeWidth;
222 }
223 
224 qreal QncgGraphicsEntity::getHeight() const
225 {
226  return height;
227 }
228 
229 void QncgGraphicsEntity::setHeight(qreal height)
230 {
231  this->height = height;
232 }
233 
234 qreal QncgGraphicsEntity::getPressHeight() const
235 {
236  return pressHeight;
237 }
238 
239 void QncgGraphicsEntity::setPressHeight(qreal pressHeight)
240 {
241  this->pressHeight = pressHeight;
242 }
243 
244 qreal QncgGraphicsEntity::getResizeHeight() const
245 {
246  return resizeHeight;
247 }
248 
249 void QncgGraphicsEntity::setResizeHeight(qreal resizeHeight)
250 {
251  this->resizeHeight = resizeHeight;
252 }
253 
254 int QncgGraphicsEntity::getzIndex() const
255 {
256  return zindex;
257 }
258 
259 void QncgGraphicsEntity::setzIndex(int zindex)
260 {
261  this->zindex = zindex;
262 
263  setZValue(zindex);
264 }
265 
266 QncgResizeType QncgGraphicsEntity::getncgResize() const
267 {
268  return resizeType;
269 }
270 
271 void QncgGraphicsEntity::setncgResize(QncgResizeType resizeType)
272 {
273  this->resizeType = resizeType;
274 }
275 
276 QncgEntityType QncgGraphicsEntity::getncgType() const
277 {
278  return entityType;
279 }
280 
281 void QncgGraphicsEntity::setncgType(QncgEntityType entityType)
282 {
283  this->entityType = entityType;
284 }
285 
286 QncgGraphicsEntity* QncgGraphicsEntity::getncgGraphicsParent() const
287 {
288  return parent;
289 }
290 
291 void QncgGraphicsEntity::setncgGraphicsParent(QncgGraphicsEntity* parent)
292 {
293  this->parent = parent;
294 
295  setParent(parent);
296  setParentItem(parent);
297 }
298 
299 QVector<QncgGraphicsEntity*> QncgGraphicsEntity::getncgGraphicsEntities()
300 {
301  return entities;
302 }
303 
304 void QncgGraphicsEntity::addncgGraphicsEntity(QncgGraphicsEntity* entity)
305 {
306  if (entity != NULL){
307  entities.append(entity);
308  }
309 }
310 
311 void QncgGraphicsEntity::removencgGraphicsEntity(QncgGraphicsEntity* entity)
312 {
313  if (entity != NULL){
314  int index = entities.indexOf(entity);
315 
316  if (index >= 0){
317  entities.remove(index);
318 
319  if (entity->isVisible())
320  scene()->removeItem(entity);
321 
322  }
323  }
324 }
325 
326 QPainterPath QncgGraphicsEntity::shape() const
327 {
328  QPainterPath painter;
329 
330  delineate(&painter);
331 
332  if (selectable && selected && resizable){
333  painter.setFillRule(Qt::WindingFill);
334 
335  painter.addRect(0,0,8,8); // topleft
336  painter.addRect((width+8)/2 - 4,0,8,8); // top
337  painter.addRect((width+8) - 8,0,8,8); // topright
338  painter.addRect((width+8) - 8,(height+8)/2 - 4,8,8); // right
339  painter.addRect((width+8) - 8,(height+8) - 8,8,8); // bottomright
340  painter.addRect((width+8)/2 - 4,(height+8) - 8,8,8); // bottom
341  painter.addRect(0,(height+8) - 8,8,8); // bottomleft
342  painter.addRect(0,(height+8)/2 - 4,8,8); // left
343  }
344 
345  return painter;
346 }
347 
348 QRectF QncgGraphicsEntity::boundingRect() const
349 {
350  QRectF bounds;
351 
352  bounds.setX(0);
353  bounds.setY(0);
354  bounds.setWidth(width+8);
355  bounds.setHeight(height+8);
356 
357  return bounds;
358 }
359 
360 void QncgGraphicsEntity::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
361 {
362  draw(painter);
363 
364  if (selectable && selected){
365  painter->setRenderHint(QPainter::Antialiasing,false);
366 
367  painter->setBrush(Qt::NoBrush);
368  painter->setPen(QPen(QBrush(Qt::black), 0, Qt::DashLine));
369 
370  painter->drawRect(4, 4, getWidth()-1, getHeight()-1);
371 
372  if (resizable){
373  painter->setBrush(QBrush(Qt::white));
374  painter->setPen(QPen(QBrush(Qt::black), 0));
375 
376  painter->drawRect(0,0,8,8); // topleft
377  painter->drawRect((width+8)/2-4-1,0,8,8); // top
378  painter->drawRect((width+8)-8-1,0,8,8); // topright
379  painter->drawRect((width+8)-8-1,(height+8)/2-4-1,8,8); // right
380  painter->drawRect((width+8)-8-1,(height+8)-8-1,8,8); // bottomright
381  painter->drawRect((width+8)/2-4-1,(height+8)-8-1,8,8); // bottom
382  painter->drawRect(0,(height+8)-8-1,8,8); // bottomleft
383  painter->drawRect(0,(height+8)/2-4-1,8,8); // left
384  }
385  }
386 }
387 
388 void QncgGraphicsEntity::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
389 {
390  if (moveable && moving){
391  move(event);
392 
393  } else if (resizable && resizing){
394  resize(event);
395  }
396 }
397 
398 void QncgGraphicsEntity::mousePressEvent(QGraphicsSceneMouseEvent* event)
399 {
400  if (event->button() == Qt::RightButton){
401  event->ignore();
402 
403  }else if (event->button() == Qt::LeftButton){
404  if (selectable && !selected){
405  setSelected(true); emit entitySelected();
406  }
407 
408  setPressTop(event->pos().y());
409  setPressLeft(event->pos().x());
410  setPressWidth(width);
411  setPressHeight(height);
412 
413  // avoid flickering on first move
414  if (moveable){
415  setMoveTop(top);
416  setMoveLeft(left);
417  }
418 
419  // avoid flickering on first resize
420  if (resizable){
421  setResizeTop(top);
422  setResizeLeft(left);
423  setResizeWidth(width);
424  setResizeHeight(height);
425  }
426 
427  if (resizable){
428  // if over TOPLEFT resize region
429  if (QRectF(0,0,8,8).contains(event->pos())){
430  setncgResize(Qncg::TopLeft);
431  setResizing(true);
432 
433  // if over TOP resize region
434  }else if (QRectF((width+8)/2 - 4,0,8,8).contains(event->pos())){
435  setncgResize(Qncg::Top);
436  setResizing(true);
437 
438  // if over TOPRIGHT resize region
439  }else if (QRectF((width+8) - 8,0,8,8).contains(event->pos())){
440  setncgResize(Qncg::TopRight);
441  setResizing(true);
442 
443  // if over RIGHT resize region
444  }else if (QRectF((width+8) - 8,(height+8)/2 - 4,8,8).contains(event->pos())){
445  setncgResize(Qncg::Right);
446  setResizing(true);
447 
448  // if over BOTTOMRIGHT resize region
449  }else if (QRectF((width+8) - 8,(height+8) - 8,8,8).contains(event->pos())){
450  setncgResize(Qncg::BottomRight);
451  setResizing(true);
452 
453  // if over BOTTOM resize region
454  }else if (QRectF((width+8)/2 - 4,(height+8) - 8,8,8).contains(event->pos())){
455  setncgResize(Qncg::Bottom);
456  setResizing(true);
457 
458  // if over BOTTOMLEFT resize region
459  }else if (QRectF(0,(height+8) - 8,8,8).contains(event->pos())){
460  setncgResize(Qncg::BottomLeft);
461  setResizing(true);
462 
463  // if over LEFT resize region
464  }else if (QRectF(0,(height+8)/2 - 4,8,8).contains(event->pos())){
465  setncgResize(Qncg::Left);
466  setResizing(true);
467 
468  // if not over any resize region
469  }else if (moveable){
470  setMoving(true);
471  }
472 
473  }else if (moveable){
474  setMoving(true);
475  }
476  }
477 
478  QGraphicsItem::mousePressEvent(event);
479 }
480 
481 void QncgGraphicsEntity::mouseReleaseEvent(QGraphicsSceneMouseEvent*event)
482 {
483  if (moving){
484  setMoving(false);
485 
486  if ((top != moveTop || left != moveLeft)){
487 
488  QMap<QString, QString> properties;
489 
490  properties["top"] = QString::number(moveTop);
491  properties["left"] = QString::number(moveLeft);
492 
493  emit entityAboutToChange(properties);
494 
495  setTop(moveTop);
496  setLeft(moveLeft);
497 
498  adjust();
499 
500  emit entityChanged();
501 
502  }else{
503  adjust();
504  }
505 
506  }else if (resizing){
507  setResizing(false);
508 
509  if ((top != resizeTop || left != resizeLeft || width != resizeWidth || height != resizeHeight)){
510  if (resizeTop > top + height){
511  setResizeHeight(resizeTop - (top + height));
512  setResizeTop(top + height);
513  }
514 
515  if (resizeLeft > left + width){
516  setResizeWidth(resizeLeft - (left + width));
517  setResizeLeft(left + width);
518  }
519 
520  if (resizeWidth < 0){
521  setResizeLeft(resizeLeft + resizeWidth);
522  setResizeWidth(-resizeWidth);
523  }
524 
525  if (resizeHeight < 0){
526  setResizeTop(resizeTop + resizeHeight);
527  setResizeHeight(-resizeHeight);
528  }
529 
530  foreach(QncgGraphicsEntity* entity, entities){
531  entity->setTop(entity->getTop() - (resizeTop - top));
532  entity->setLeft(entity->getLeft() - (resizeLeft - left));
533  }
534 
535  QMap<QString, QString> properties;
536 
537  properties["top"] = QString::number(moveTop);
538  properties["left"] = QString::number(moveLeft);
539  properties["width"] = QString::number(resizeWidth);
540  properties["height"] = QString::number(resizeHeight);
541 
542  emit entityAboutToChange(properties);
543 
544  setTop(resizeTop);
545  setLeft(resizeLeft);
546  setWidth(resizeWidth);
547  setHeight(resizeHeight);
548 
549  adjust();
550 
551  emit entityChanged();
552 
553  }else{
554  adjust();
555  }
556  }
557 
558  QGraphicsItem::mouseReleaseEvent(event);
559 }
560 
561 void QncgGraphicsEntity::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
562 {
563  if (selected){
564  if (resizable){
565  // if over TOPLEFT resize region
566  if (QRectF(0,0,8,8).contains(event->pos())){
567  setCursor(Qt::SizeFDiagCursor);
568 
569  // if over TOP resize region
570  }else if (QRectF((width+8)/2 - 4,0,8,8).contains(event->pos())){
571  setCursor(Qt::SizeVerCursor);
572 
573  // if over TOPRIGHT resize region
574  }else if (QRectF((width+8) - 8,0,8,8).contains(event->pos())){
575  setCursor(Qt::SizeBDiagCursor);
576 
577  // if over RIGHT resize region
578  }else if (QRectF((width+8) - 8,(height+8)/2 - 4,8,8).contains(event->pos())){
579  setCursor(Qt::SizeHorCursor);
580 
581  // if over BOTTOMRIGHT resize region
582  }else if (QRectF((width+8) - 8,(height+8) - 8,8,8).contains(event->pos())){
583  setCursor(Qt::SizeFDiagCursor);
584 
585  // if over BOTTOM resize region
586  }else if (QRectF((width+8)/2 - 4,(height+8) - 8,8,8).contains(event->pos())){
587  setCursor(Qt::SizeVerCursor);
588 
589  // if over BOTTOMLEFT resize region
590  }else if (QRectF(0,(height+8) - 8,8,8).contains(event->pos())){
591  setCursor(Qt::SizeBDiagCursor);
592 
593  // if over LEFT resize region
594  }else if (QRectF(0,(height+8)/2 - 4,8,8).contains(event->pos())){
595  setCursor(Qt::SizeHorCursor);
596 
597  // if not over any resize region
598  }else if (moveable){
599  setCursor(Qt::SizeAllCursor);
600  }
601 
602  }else if (moveable){
603  setCursor(Qt::SizeAllCursor);
604 
605  }else{
606  setCursor(Qt::ArrowCursor);
607  }
608  }else{
609  setCursor(Qt::ArrowCursor);
610  }
611 
612  QGraphicsItem::hoverEnterEvent(event);
613 }