26 #include <QApplication>
27 #include <QGraphicsSceneMouseEvent>
28 #include <QGraphicsView>
35 #define RESIZE_RECT 30
36 #define ROUNDEDGESIZE 50
39 QGraphicsObject(parent), rimPen(Qt::black, 1), bounds(bounds)
42 scalingActive =
false;
45 setCacheMode(DeviceCoordinateCache);
47 setFlag(QGraphicsItem::ItemIsSelectable,
true);
48 setFlag(QGraphicsItem::ItemIsMovable,
true);
50 setFlag(QGraphicsItem::ItemSendsGeometryChanges,
true);
51 setAcceptHoverEvents(
true);
54 float curScale = scale();
57 QPointF curPos = pos();
66 fillGradient.setStart(
bounds.topLeft());
67 fillGradient.setFinalStop(
bounds.bottomRight());
68 fillGradient.setColorAt(0, newColor);
69 fillGradient.setColorAt(1, newColor.darker(100));
82 return bounds.adjusted(0, 0, 2, 2);
93 prepareGeometryChange();
102 if (newSize ==
bounds.size())
107 QRectF oldBounds =
bounds;
108 prepareGeometryChange();
119 painter->setPen(Qt::NoPen);
120 painter->setBrush(QColor(0, 0, 0, 64));
123 QLinearGradient tempGradient = fillGradient;
124 QPen tmpRimPen = rimPen;
127 tempGradient.setColorAt(1, color.lighter(130));
128 tmpRimPen.setStyle(Qt::DashLine);
132 painter->setBrush(tempGradient);
133 painter->setPen(tmpRimPen);
136 painter->setPen(QPen(Qt::black, 1));
145 if (event->modifiers().testFlag(Qt::ShiftModifier) && (!parent || parent->
isEditable()))
147 setFlag(QGraphicsItem::ItemIsMovable,
true);
151 setFlag(QGraphicsItem::ItemIsMovable,
false);
162 QPointF pos =
event->pos();
172 if (rect.contains(pos))
174 scalingActive =
true;
175 prepareGeometryChange();
184 resizingMode = eBottomResizing;
188 resizingMode = eTopResizing;
192 resizingMode = eLeftResizing;
196 resizingMode = eRightResizing;
200 QGraphicsObject::mousePressEvent(event);
210 QApplication::restoreOverrideCursor();
214 scalingActive =
false;
215 resizingMode = eNone;
216 QGraphicsItem::mouseReleaseEvent(event);
222 QPointF pos =
event->pos();
227 prepareGeometryChange();
228 QRectF newRect = oldRect;
229 newRect.setBottomRight(QPointF(pos.x() + 2, pos.y() + 2));
245 float xScale = newRect.width() / oldRect.width();
246 float yScale = newRect.height() / oldRect.height();
249 float resultScalefactor =
std::max(xScale, yScale);
250 resultScalefactor *= scale();
251 resultScalefactor =
std::max(resultScalefactor, 0.01f);
254 setScale(resultScalefactor);
256 else if (resizingMode != eNone)
259 prepareGeometryChange();
260 QRectF newBounds =
bounds;
262 switch (resizingMode)
268 case eBottomResizing:
269 newBounds.setBottom(pos.y() + 2);
276 newBounds.setRight(pos.x() + 2);
283 newBounds.setWidth(
std::max(50.0, newBounds.width()));
284 newBounds.setHeight(
std::max(50.0, newBounds.height()));
286 QRectF bbInP = mapRectToParent(newBounds);
287 QRectF obbInP = mapRectToParent(oldRect);
293 std::max(bbInP.width(), bbInP.height()));
299 QGraphicsItem::mouseMoveEvent(event);
307 (QApplication::overrideCursor() && QApplication::overrideCursor()->shape() != shape))
310 if (QApplication::overrideCursor() && QApplication::overrideCursor()->shape() != shape)
312 QApplication::changeOverrideCursor(QCursor(shape));
316 QApplication::setOverrideCursor(QCursor(shape));
326 QPointF pos =
event->pos();
337 if (rect.contains(pos))
357 else if (QApplication::overrideCursor())
359 QApplication::restoreOverrideCursor();
375 QApplication::restoreOverrideCursor();
385 result.setTop(result.bottom());
393 result.setBottom(result.top());
401 result.setRight(result.left());
409 result.setLeft(result.right());
421 QRectF rectP = (parentItem()->boundingRect());
423 float areaP = rectP.width() * rectP.height();
424 float area = rect.width() * rect.height() * resultScalefactor;
428 if (area / areaP > 0.4)
431 std::max(rect.width(), rect.height()) /
std::max(rectP.width(), rectP.height());
444 QRectF rect = parentItem()->boundingRect();
450 qMax(newPos.x(), rect.left() + rect.width() * 0.03)));
452 qMin(rect.bottom() -
boundingRect().height() * scale() -
453 rect.height() * 0.03,
454 qMax(newPos.y(), rect.top() + rect.height() * 0.20)));
464 QGraphicsView* view = qobject_cast<QGraphicsView*>(event->widget()->parent());
469 view->mapFromScene(parentItem()->mapToScene(parentItem()->
boundingRect()))
504 this->editable = editable;
510 if (change == ItemPositionChange)
513 QPointF newPos =
value.toPointF();
514 RoundRectItem* itemParent = qgraphicsitem_cast<RoundRectItem*>(parentItem());
526 else if (change == ItemScaleChange)
531 float resultScalefactor =
value.toFloat();
534 return resultScalefactor;
538 return QGraphicsItem::itemChange(change,
value);