[Kst] branches/work/kst/portto4/kst/src/libkstapp
Adam Treat
treat at kde.org
Wed Sep 12 23:50:30 CEST 2007
SVN commit 711824 by treat:
* Use floating point in viewitem
* Start implementing customizations for handling lineitem
* Don't resize item handles when window resizes
* Show selection rect when the item is under mouse
M +58 -0 lineitem.cpp
M +13 -0 lineitem.h
M +28 -11 viewitem.cpp
M +18 -12 viewitem.h
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.cpp #711823:711824
@@ -27,7 +27,23 @@
LineItem::~LineItem() {
}
+QPainterPath LineItem::shape() const {
+ QPolygonF polygon;
+ QLineF left = _line;
+ left.translate(-5.0, 5.0);
+ QLineF right = _line;
+ right.translate(5.0, -5.0);
+ polygon << left.p1();
+ polygon << right.p1();
+ polygon << right.p2();
+ polygon << left.p2();
+ polygon << left.p1();
+ QPainterPath p;
+ p.addPolygon(polygon);
+ return p;
+}
+
QPainterPath LineItem::itemShape() const {
QPainterPath path(_line.p1());
path.lineTo(_line.p2());
@@ -49,6 +65,27 @@
}
+QPainterPath LineItem::p1Grip() const {
+ return topLeftGrip();
+}
+
+
+QPainterPath LineItem::p2Grip() const {
+ return bottomRightGrip();
+}
+
+
+QPainterPath LineItem::grips() const {
+ if (mouseMode() == Default || mouseMode() == Move)
+ return QPainterPath();
+
+ QPainterPath grips;
+ grips.addPath(p1Grip());
+ grips.addPath(p2Grip());
+ return grips;
+}
+
+
void LineItem::creationPolygonChanged(View::CreationEvent event) {
if (event == View::MousePress) {
const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MousePress));
@@ -76,6 +113,27 @@
}
}
+
+void LineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+ ViewItem::mouseMoveEvent(event);
+}
+
+
+void LineItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+ ViewItem::mousePressEvent(event);
+}
+
+
+void LineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+ ViewItem::mouseReleaseEvent(event);
+}
+
+
+void LineItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+ ViewItem::mouseDoubleClickEvent(event);
+}
+
+
void CreateLineCommand::createItem() {
_item = new LineItem(_view);
_view->setCursor(Qt::CrossCursor);
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.h #711823:711824
@@ -24,16 +24,29 @@
LineItem(View *parent);
virtual ~LineItem();
+ virtual QPainterPath shape() const;
virtual QPainterPath itemShape() const;
virtual void paint(QPainter *painter);
QLineF line() const;
void setLine(const QLineF &line);
+ virtual QPainterPath grips() const;
+
protected Q_SLOTS:
virtual void creationPolygonChanged(View::CreationEvent event);
+protected:
+ virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
+ virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
+ virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
+ virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
+
private:
+ QPainterPath p1Grip() const;
+ QPainterPath p2Grip() const;
+
+private:
QLineF _line;
};
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #711823:711824
@@ -26,6 +26,7 @@
ViewItem::ViewItem(View *parent)
: QObject(parent),
_mouseMode(Default),
+ _hovering(false),
_lockAspectRatio(false),
_layout(0),
_activeGrip(NoGrip) {
@@ -95,8 +96,10 @@
}
-QSize ViewItem::sizeOfGrip() const {
- return QSize(10,10);
+QSizeF ViewItem::sizeOfGrip() const {
+
+ int base = 15;
+ return mapFromScene(parentView()->mapToScene(QRect(0, 0, base, base)).boundingRect()).boundingRect().size();
}
@@ -124,7 +127,7 @@
return QPainterPath();
QRectF bound = gripBoundingRect();
- QRectF grip = QRectF(bound.topRight() - QPoint(sizeOfGrip().width(), 0), sizeOfGrip());
+ QRectF grip = QRectF(bound.topRight() - QPointF(sizeOfGrip().width(), 0), sizeOfGrip());
QPainterPath path;
if (_mouseMode == Resize || _mouseMode == Scale)
path.addRect(grip);
@@ -143,7 +146,7 @@
return QPainterPath();
QRectF bound = gripBoundingRect();
- QRectF grip = QRectF(bound.bottomRight() - QPoint(sizeOfGrip().width(), sizeOfGrip().height()), sizeOfGrip());
+ QRectF grip = QRectF(bound.bottomRight() - QPointF(sizeOfGrip().width(), sizeOfGrip().height()), sizeOfGrip());
QPainterPath path;
if (_mouseMode == Resize || _mouseMode == Scale)
path.addRect(grip);
@@ -162,7 +165,7 @@
return QPainterPath();
QRectF bound = gripBoundingRect();
- QRectF grip = QRectF(bound.bottomLeft() - QPoint(0, sizeOfGrip().height()), sizeOfGrip());
+ QRectF grip = QRectF(bound.bottomLeft() - QPointF(0, sizeOfGrip().height()), sizeOfGrip());
QPainterPath path;
if (_mouseMode == Resize || _mouseMode == Scale)
path.addRect(grip);
@@ -198,7 +201,7 @@
return QPainterPath();
QRectF bound = gripBoundingRect();
- QRectF grip = QRectF(bound.topRight() - QPoint(sizeOfGrip().width(), 0), sizeOfGrip());
+ QRectF grip = QRectF(bound.topRight() - QPointF(sizeOfGrip().width(), 0), sizeOfGrip());
grip.moveCenter(QPointF(grip.center().x(), bound.center().y()));
QPainterPath path;
@@ -215,7 +218,7 @@
return QPainterPath();
QRectF bound = gripBoundingRect();
- QRectF grip = QRectF(bound.bottomLeft() - QPoint(0, sizeOfGrip().height()), sizeOfGrip());
+ QRectF grip = QRectF(bound.bottomLeft() - QPointF(0, sizeOfGrip().height()), sizeOfGrip());
grip.moveCenter(QPointF(bound.center().x(), grip.center().y()));
QPainterPath path;
@@ -279,7 +282,7 @@
QRectF ViewItem::gripBoundingRect() const {
QRectF bound = /*_mouseMode != Resize ?*/ selectBoundingRect() /*: rect()*/;
- bound.setTopLeft(bound.topLeft() - QPoint(sizeOfGrip().width(), sizeOfGrip().height()));
+ bound.setTopLeft(bound.topLeft() - QPointF(sizeOfGrip().width(), sizeOfGrip().height()));
bound.setWidth(bound.width() + sizeOfGrip().width());
bound.setHeight(bound.height() + sizeOfGrip().height());
return bound;
@@ -287,7 +290,7 @@
QRectF ViewItem::boundingRect() const {
- if (!isSelected())
+ if (!isSelected() && !_hovering)
return QGraphicsRectItem::boundingRect();
QPolygonF gripBound = mapFromScene(gripBoundingRect());
@@ -296,7 +299,7 @@
QPainterPath ViewItem::shape() const {
- if (!isSelected())
+ if (!isSelected() && !_hovering)
return itemShape();
QPainterPath selectPath;
@@ -322,7 +325,7 @@
painter->save();
painter->setPen(Qt::DotLine);
- if (isSelected()) {
+ if (isSelected() || _hovering) {
painter->drawPath(shape());
if (_mouseMode == Resize)
painter->fillPath(grips(), Qt::blue);
@@ -948,6 +951,20 @@
}
+void ViewItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
+ QGraphicsRectItem::hoverMoveEvent(event);
+ _hovering = true;
+ update();
+}
+
+
+void ViewItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
+ QGraphicsRectItem::hoverMoveEvent(event);
+ _hovering = false;
+ update();
+}
+
+
QVariant ViewItem::itemChange(GraphicsItemChange change, const QVariant &value) {
if (change == ItemSelectedChange) {
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #711823:711824
@@ -65,22 +65,15 @@
qreal width() const { return viewRect().normalized().width(); }
qreal height() const { return viewRect().normalized().height(); }
- QSize sizeOfGrip() const;
- QPainterPath topLeftGrip() const;
- QPainterPath topRightGrip() const;
- QPainterPath bottomRightGrip() const;
- QPainterPath bottomLeftGrip() const;
- QPainterPath topMidGrip() const;
- QPainterPath rightMidGrip() const;
- QPainterPath bottomMidGrip() const;
- QPainterPath leftMidGrip() const;
- QPainterPath grips() const;
ActiveGrip activeGrip() const;
void setActiveGrip(ActiveGrip grip);
- QRectF selectBoundingRect() const;
- QRectF gripBoundingRect() const;
+ virtual QSizeF sizeOfGrip() const;
+ virtual QPainterPath grips() const;
+ virtual QRectF selectBoundingRect() const;
+ virtual QRectF gripBoundingRect() const;
+
virtual QRectF boundingRect() const;
virtual QPainterPath shape() const;
virtual QPainterPath itemShape() const { return QGraphicsRectItem::shape(); }
@@ -118,6 +111,16 @@
void setLeft(qreal x);
void setRight(qreal x);
+protected:
+ QPainterPath topLeftGrip() const;
+ QPainterPath topRightGrip() const;
+ QPainterPath bottomRightGrip() const;
+ QPainterPath bottomLeftGrip() const;
+ QPainterPath topMidGrip() const;
+ QPainterPath rightMidGrip() const;
+ QPainterPath bottomMidGrip() const;
+ QPainterPath leftMidGrip() const;
+
private:
QTransform selectTransform() const;
bool transformToRect(const QRectF &from, const QRectF &to);
@@ -135,6 +138,8 @@
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
+ virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
+ virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private Q_SLOTS:
@@ -146,6 +151,7 @@
private:
MouseMode _mouseMode;
+ bool _hovering;
bool _lockAspectRatio;
ViewGridLayout *_layout;
QPointF _originalPosition;
More information about the Kst
mailing list