1 #include "qncggraphicsentity.h"
5 : QObject(parent), QGraphicsItem(parent)
7 setncgType(Qncg::NoType);
9 setncgGraphicsParent(parent);
11 setFlag(QGraphicsItem::ItemIsMovable,
true);
12 setFlag(QGraphicsItem::ItemIsFocusable,
true);
14 setAcceptHoverEvents(
true);
45 QncgGraphicsEntity::~QncgGraphicsEntity()
50 void QncgGraphicsEntity::setMoveable(
bool moveable)
52 this->moveable = moveable;
55 bool QncgGraphicsEntity::isMoveable()
const
60 void QncgGraphicsEntity::setSelectable(
bool selected)
62 this->selectable = selected;
65 bool QncgGraphicsEntity::isSelectable()
const
70 void QncgGraphicsEntity::setResizable(
bool resizable)
72 this->resizable = resizable;
75 bool QncgGraphicsEntity::isResizable()
const
80 void QncgGraphicsEntity::setMoving(
bool moving)
82 this->moving = moving;
85 bool QncgGraphicsEntity::isMoving()
const
90 void QncgGraphicsEntity::setResizing(
bool resizing)
92 this->resizing = resizing;
95 bool QncgGraphicsEntity::isResizing()
const
100 void QncgGraphicsEntity::setSelected(
bool selected)
102 this->selected = selected;
105 bool QncgGraphicsEntity::isSelected()
const
110 qreal QncgGraphicsEntity::getTop()
const
115 void QncgGraphicsEntity::setTop(qreal top)
122 qreal QncgGraphicsEntity::getMoveTop()
const
127 void QncgGraphicsEntity::setMoveTop(qreal moveTop)
129 this->moveTop = moveTop;
132 qreal QncgGraphicsEntity::getPressTop()
const
137 void QncgGraphicsEntity::setPressTop(qreal pressTop)
139 this->pressTop = pressTop;
142 qreal QncgGraphicsEntity::getResizeTop()
const
147 void QncgGraphicsEntity::setResizeTop(qreal resizeTop)
149 this->resizeTop = resizeTop;
152 qreal QncgGraphicsEntity::getLeft()
const
157 void QncgGraphicsEntity::setLeft(qreal left)
164 qreal QncgGraphicsEntity::getMoveLeft()
const
169 void QncgGraphicsEntity::setMoveLeft(qreal moveLeft)
171 this->moveLeft = moveLeft;
174 qreal QncgGraphicsEntity::getPressLeft()
const
179 void QncgGraphicsEntity::setPressLeft(qreal pressLeft)
181 this->pressLeft = pressLeft;
184 qreal QncgGraphicsEntity::getResizeLeft()
const
189 void QncgGraphicsEntity::setResizeLeft(qreal resizeLeft)
191 this->resizeLeft = resizeLeft;
194 qreal QncgGraphicsEntity::getWidth()
const
199 void QncgGraphicsEntity::setWidth(qreal width)
204 qreal QncgGraphicsEntity::getPressWidth()
const
209 void QncgGraphicsEntity::setPressWidth(qreal pressWidth)
211 this->pressWidth = pressWidth;
214 qreal QncgGraphicsEntity::getResizeWidth()
const
219 void QncgGraphicsEntity::setResizeWidth(qreal resizeWidth)
221 this->resizeWidth = resizeWidth;
224 qreal QncgGraphicsEntity::getHeight()
const
229 void QncgGraphicsEntity::setHeight(qreal height)
231 this->height = height;
234 qreal QncgGraphicsEntity::getPressHeight()
const
239 void QncgGraphicsEntity::setPressHeight(qreal pressHeight)
241 this->pressHeight = pressHeight;
244 qreal QncgGraphicsEntity::getResizeHeight()
const
249 void QncgGraphicsEntity::setResizeHeight(qreal resizeHeight)
251 this->resizeHeight = resizeHeight;
254 int QncgGraphicsEntity::getzIndex()
const
259 void QncgGraphicsEntity::setzIndex(
int zindex)
261 this->zindex = zindex;
266 QncgResizeType QncgGraphicsEntity::getncgResize()
const
271 void QncgGraphicsEntity::setncgResize(QncgResizeType resizeType)
273 this->resizeType = resizeType;
276 QncgEntityType QncgGraphicsEntity::getncgType()
const
281 void QncgGraphicsEntity::setncgType(QncgEntityType entityType)
283 this->entityType = entityType;
293 this->parent = parent;
296 setParentItem(parent);
299 QVector<QncgGraphicsEntity*> QncgGraphicsEntity::getncgGraphicsEntities()
307 entities.append(entity);
314 int index = entities.indexOf(entity);
317 entities.remove(index);
319 if (entity->isVisible())
320 scene()->removeItem(entity);
326 QPainterPath QncgGraphicsEntity::shape()
const
328 QPainterPath painter;
332 if (selectable && selected && resizable){
333 painter.setFillRule(Qt::WindingFill);
335 painter.addRect(0,0,8,8);
336 painter.addRect((width+8)/2 - 4,0,8,8);
337 painter.addRect((width+8) - 8,0,8,8);
338 painter.addRect((width+8) - 8,(height+8)/2 - 4,8,8);
339 painter.addRect((width+8) - 8,(height+8) - 8,8,8);
340 painter.addRect((width+8)/2 - 4,(height+8) - 8,8,8);
341 painter.addRect(0,(height+8) - 8,8,8);
342 painter.addRect(0,(height+8)/2 - 4,8,8);
348 QRectF QncgGraphicsEntity::boundingRect()
const
354 bounds.setWidth(width+8);
355 bounds.setHeight(height+8);
360 void QncgGraphicsEntity::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
364 if (selectable && selected){
365 painter->setRenderHint(QPainter::Antialiasing,
false);
367 painter->setBrush(Qt::NoBrush);
368 painter->setPen(QPen(QBrush(Qt::black), 0, Qt::DashLine));
370 painter->drawRect(4, 4, getWidth()-1, getHeight()-1);
373 painter->setBrush(QBrush(Qt::white));
374 painter->setPen(QPen(QBrush(Qt::black), 0));
376 painter->drawRect(0,0,8,8);
377 painter->drawRect((width+8)/2-4-1,0,8,8);
378 painter->drawRect((width+8)-8-1,0,8,8);
379 painter->drawRect((width+8)-8-1,(height+8)/2-4-1,8,8);
380 painter->drawRect((width+8)-8-1,(height+8)-8-1,8,8);
381 painter->drawRect((width+8)/2-4-1,(height+8)-8-1,8,8);
382 painter->drawRect(0,(height+8)-8-1,8,8);
383 painter->drawRect(0,(height+8)/2-4-1,8,8);
388 void QncgGraphicsEntity::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
390 if (moveable && moving){
393 }
else if (resizable && resizing){
398 void QncgGraphicsEntity::mousePressEvent(QGraphicsSceneMouseEvent* event)
400 if (event->button() == Qt::RightButton){
403 }
else if (event->button() == Qt::LeftButton){
404 if (selectable && !selected){
405 setSelected(
true); emit entitySelected();
408 setPressTop(event->pos().y());
409 setPressLeft(event->pos().x());
410 setPressWidth(width);
411 setPressHeight(height);
423 setResizeWidth(width);
424 setResizeHeight(height);
429 if (QRectF(0,0,8,8).contains(event->pos())){
430 setncgResize(Qncg::TopLeft);
434 }
else if (QRectF((width+8)/2 - 4,0,8,8).contains(event->pos())){
435 setncgResize(Qncg::Top);
439 }
else if (QRectF((width+8) - 8,0,8,8).contains(event->pos())){
440 setncgResize(Qncg::TopRight);
444 }
else if (QRectF((width+8) - 8,(height+8)/2 - 4,8,8).contains(event->pos())){
445 setncgResize(Qncg::Right);
449 }
else if (QRectF((width+8) - 8,(height+8) - 8,8,8).contains(event->pos())){
450 setncgResize(Qncg::BottomRight);
454 }
else if (QRectF((width+8)/2 - 4,(height+8) - 8,8,8).contains(event->pos())){
455 setncgResize(Qncg::Bottom);
459 }
else if (QRectF(0,(height+8) - 8,8,8).contains(event->pos())){
460 setncgResize(Qncg::BottomLeft);
464 }
else if (QRectF(0,(height+8)/2 - 4,8,8).contains(event->pos())){
465 setncgResize(Qncg::Left);
478 QGraphicsItem::mousePressEvent(event);
481 void QncgGraphicsEntity::mouseReleaseEvent(QGraphicsSceneMouseEvent*event)
486 if ((top != moveTop || left != moveLeft)){
488 QMap<QString, QString> properties;
490 properties[
"top"] = QString::number(moveTop);
491 properties[
"left"] = QString::number(moveLeft);
493 emit entityAboutToChange(properties);
500 emit entityChanged();
509 if ((top != resizeTop || left != resizeLeft || width != resizeWidth || height != resizeHeight)){
510 if (resizeTop > top + height){
511 setResizeHeight(resizeTop - (top + height));
512 setResizeTop(top + height);
515 if (resizeLeft > left + width){
516 setResizeWidth(resizeLeft - (left + width));
517 setResizeLeft(left + width);
520 if (resizeWidth < 0){
521 setResizeLeft(resizeLeft + resizeWidth);
522 setResizeWidth(-resizeWidth);
525 if (resizeHeight < 0){
526 setResizeTop(resizeTop + resizeHeight);
527 setResizeHeight(-resizeHeight);
531 entity->setTop(entity->getTop() - (resizeTop - top));
532 entity->setLeft(entity->getLeft() - (resizeLeft - left));
535 QMap<QString, QString> properties;
537 properties[
"top"] = QString::number(moveTop);
538 properties[
"left"] = QString::number(moveLeft);
539 properties[
"width"] = QString::number(resizeWidth);
540 properties[
"height"] = QString::number(resizeHeight);
542 emit entityAboutToChange(properties);
546 setWidth(resizeWidth);
547 setHeight(resizeHeight);
551 emit entityChanged();
558 QGraphicsItem::mouseReleaseEvent(event);
561 void QncgGraphicsEntity::hoverMoveEvent(QGraphicsSceneHoverEvent* event)
566 if (QRectF(0,0,8,8).contains(event->pos())){
567 setCursor(Qt::SizeFDiagCursor);
570 }
else if (QRectF((width+8)/2 - 4,0,8,8).contains(event->pos())){
571 setCursor(Qt::SizeVerCursor);
574 }
else if (QRectF((width+8) - 8,0,8,8).contains(event->pos())){
575 setCursor(Qt::SizeBDiagCursor);
578 }
else if (QRectF((width+8) - 8,(height+8)/2 - 4,8,8).contains(event->pos())){
579 setCursor(Qt::SizeHorCursor);
582 }
else if (QRectF((width+8) - 8,(height+8) - 8,8,8).contains(event->pos())){
583 setCursor(Qt::SizeFDiagCursor);
586 }
else if (QRectF((width+8)/2 - 4,(height+8) - 8,8,8).contains(event->pos())){
587 setCursor(Qt::SizeVerCursor);
590 }
else if (QRectF(0,(height+8) - 8,8,8).contains(event->pos())){
591 setCursor(Qt::SizeBDiagCursor);
594 }
else if (QRectF(0,(height+8)/2 - 4,8,8).contains(event->pos())){
595 setCursor(Qt::SizeHorCursor);
599 setCursor(Qt::SizeAllCursor);
603 setCursor(Qt::SizeAllCursor);
606 setCursor(Qt::ArrowCursor);
609 setCursor(Qt::ArrowCursor);
612 QGraphicsItem::hoverEnterEvent(event);