[Kst] branches/work/kst/portto4/kst/src/libkstapp

Adam Treat treat at kde.org
Thu Sep 13 15:37:01 CEST 2007


SVN commit 712066 by treat:

* SELECT_BOUND is disabled.  Now default to selection
rect and grip orientation as defined in the layout spec.
* Turn off scale mousemode altogether.  Now controlled through
a define.
* Redesign lineitem completely.  Still not done, but it works
much more like it is supposed to.
* Rotation improvements and bugfixes.
* Don't display selection rect when creating the item.
* Provide a 'centerOfRotation' method that controls the origin
about which the item rotates.
* Simplify the debug rotation mode.
* Style fixups.



 M  +79 -38    lineitem.cpp  
 M  +6 -7      lineitem.h  
 M  +109 -51   viewitem.cpp  
 M  +12 -11    viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.cpp #712065:712066
@@ -15,6 +15,7 @@
 
 #include <QDebug>
 #include <QGraphicsScene>
+#include <QGraphicsSceneContextMenuEvent>
 
 namespace Kst {
 
@@ -27,51 +28,62 @@
 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;
+void LineItem::paint(QPainter *painter) {
+  painter->drawLine(line());
 }
 
-QPainterPath LineItem::itemShape() const {
-  QPainterPath path(_line.p1());
-  path.lineTo(_line.p2());
-  return path;
+
+QLineF LineItem::line() const {
+  return QLineF(rect().left(), rect().center().y(), rect().right(), rect().center().y());
 }
 
 
-void LineItem::paint(QPainter *painter) {
-  painter->drawLine(_line);
-}
+void LineItem::setLine(const QLineF &line_) {
+  setPos(line_.p1());
+  setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height()));
 
-QLineF LineItem::line() const {
-  return _line;
-}
+  if (!rect().isEmpty()) {
+    rotateTowards(line().p2(), line_.p2());
+  }
 
-void LineItem::setLine(const QLineF &line) {
-  _line = line;
-  setViewRect(QRectF(_line.p1(), _line.p2()));
+  QRectF r = rect();
+  r.setSize(QSizeF(QLineF(line().p1(), line_.p2()).length(), r.height()));
+  setViewRect(r);
 }
 
 
-QPainterPath LineItem::p1Grip() const {
-    return topLeftGrip();
+QPainterPath LineItem::leftMidGrip() const {
+  if (mouseMode() == Default || mouseMode() == Move)
+    return QPainterPath();
+
+  QRectF bound = gripBoundingRect();
+  QRectF grip = QRectF(bound.topLeft(), sizeOfGrip());
+  grip.moveCenter(QPointF(grip.center().x(), bound.center().y()));
+  QPainterPath path;
+  if (mouseMode() == Resize || mouseMode() == Scale)
+    path.addRect(grip);
+  else
+    path.addEllipse(grip);
+
+  return path;
 }
 
 
-QPainterPath LineItem::p2Grip() const {
-    return bottomRightGrip();
+QPainterPath LineItem::rightMidGrip() const {
+  if (mouseMode() == Default || mouseMode() == Move)
+    return QPainterPath();
+
+  QRectF bound = gripBoundingRect();
+  QRectF grip = QRectF(bound.topRight() - QPointF(sizeOfGrip().width(), 0), sizeOfGrip());
+  grip.moveCenter(QPointF(grip.center().x(), bound.center().y()));
+  QPainterPath path;
+  if (mouseMode() == Resize || mouseMode() == Scale)
+    path.addRect(grip);
+  else
+    path.addEllipse(grip);
+
+  return path;
 }
 
 
@@ -80,16 +92,27 @@
     return QPainterPath();
 
   QPainterPath grips;
-  grips.addPath(p1Grip());
-  grips.addPath(p2Grip());
+  grips.addPath(leftMidGrip());
+  grips.addPath(rightMidGrip());
   return grips;
 }
 
 
+QPointF LineItem::centerOfRotation() const {
+  if (activeGrip() == RightMidGrip)
+    return line().p1();
+  else if (activeGrip() == LeftMidGrip)
+    return line().p2();
+
+  return line().p1();
+}
+
+
 void LineItem::creationPolygonChanged(View::CreationEvent event) {
   if (event == View::MousePress) {
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MousePress));
-    setLine(QLineF(poly[0], poly[0])); //start and end
+    setPos(poly.first().x(), poly.first().y());
+    setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height()));
     parentView()->scene()->addItem(this);
     setZValue(1);
     return;
@@ -97,14 +120,17 @@
 
   if (event == View::MouseMove) {
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseMove));
-    setLine(QLineF(line().p1(), poly.last())); //start and end
+    if (!rect().isEmpty()) {
+      rotateTowards(line().p2(), poly.last());
+    }
+    QRectF r = rect();
+    r.setSize(QSizeF(QLineF(line().p1(), poly.last()).length(), r.height()));
+    setViewRect(r);
     return;
   }
 
   if (event == View::MouseRelease) {
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseRelease));
-    setLine(QLineF(line().p1(), poly.last())); //start and end
-
     parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself
     parentView()->disconnect(this, SLOT(creationPolygonChanged(View::CreationEvent)));
     parentView()->setMouseMode(View::Default);
@@ -120,7 +146,22 @@
 
 
 void LineItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  ViewItem::mousePressEvent(event);
+
+  if (parentView()->viewMode() == View::Data) {
+    event->ignore();
+    return;
+  }
+
+  QPointF p = event->pos();
+  if (leftMidGrip().contains(p)) {
+    setActiveGrip(LeftMidGrip);
+  } else if (rightMidGrip().contains(p)) {
+    setActiveGrip(RightMidGrip);
+  } else {
+    setActiveGrip(NoGrip);
+  }
+
+  QGraphicsRectItem::mousePressEvent(event);
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.h #712065:712066
@@ -24,15 +24,15 @@
   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 void paint(QPainter *painter);
+
   virtual QPainterPath grips() const;
 
+  virtual QPointF centerOfRotation() const;
+
 protected Q_SLOTS:
   virtual void creationPolygonChanged(View::CreationEvent event);
 
@@ -42,9 +42,8 @@
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
   virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
 
-private:
-  QPainterPath p1Grip() const;
-  QPainterPath p2Grip() const;
+  virtual QPainterPath leftMidGrip() const;
+  virtual QPainterPath rightMidGrip() const;
 
 private:
   QLineF _line;
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #712065:712066
@@ -15,12 +15,21 @@
 #include "viewitemdialog.h"
 #include "viewgridlayout.h"
 
+#include <math.h>
+
 #include <QMenu>
 #include <QDebug>
 #include <QGraphicsScene>
 #include <QKeyEvent>
 #include <QGraphicsSceneContextMenuEvent>
 
+static const double ONE_PI = 3.14159265358979323846264338327950288419717;
+static double TWO_PI = 2.0 * ONE_PI;
+static double RAD2DEG = 180.0 / ONE_PI;
+
+#define SELECT_BOUND 0
+#define SUPPRESS_SCALE 1
+
 namespace Kst {
 
 ViewItem::ViewItem(View *parent)
@@ -54,6 +63,10 @@
 
 
 void ViewItem::setMouseMode(MouseMode mode) {
+#if SUPPRESS_SCALE
+  if (mode == Scale)
+    return;
+#endif
   _mouseMode = mode;
   update();
 }
@@ -97,9 +110,12 @@
 
 
 QSizeF ViewItem::sizeOfGrip() const {
-
   int base = 15;
+#if SELECT_BOUND
   return mapFromScene(parentView()->mapToScene(QRect(0, 0, base, base)).boundingRect()).boundingRect().size();
+#else
+  return parentView()->mapToScene(QRect(0, 0, base, base)).boundingRect().size();
+#endif
 }
 
 
@@ -115,10 +131,11 @@
   else
     path.addEllipse(grip);
 
-//   if (_mouseMode != Resize)
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -134,10 +151,11 @@
   else
     path.addEllipse(grip);
 
-//   if (_mouseMode != Resize)
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -153,10 +171,11 @@
   else
     path.addEllipse(grip);
 
-//   if (_mouseMode != Resize)
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -172,10 +191,11 @@
   else
     path.addEllipse(grip);
 
-//   if (_mouseMode != Resize)
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -189,10 +209,12 @@
 
   QPainterPath path;
   path.addRect(grip);
-//   if (_mouseMode != Resize)
+
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -206,10 +228,12 @@
 
   QPainterPath path;
   path.addRect(grip);
-//   if (_mouseMode != Resize)
+
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -223,10 +247,12 @@
 
   QPainterPath path;
   path.addRect(grip);
-//   if (_mouseMode != Resize)
+
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -240,10 +266,12 @@
 
   QPainterPath path;
   path.addRect(grip);
-//   if (_mouseMode != Resize)
+
+#if SELECT_BOUND
     return mapFromScene(path);
-//   else
-//     return path;
+#else
+    return path;
+#endif
 }
 
 
@@ -276,12 +304,16 @@
 
 
 QRectF ViewItem::selectBoundingRect() const {
+#if SELECT_BOUND
   return mapToScene(itemShape()).boundingRect();
+#else
+  return rect();
+#endif
 }
 
 
 QRectF ViewItem::gripBoundingRect() const {
-  QRectF bound = /*_mouseMode != Resize ?*/ selectBoundingRect() /*: rect()*/;
+  QRectF bound = selectBoundingRect();
   bound.setTopLeft(bound.topLeft() - QPointF(sizeOfGrip().width(), sizeOfGrip().height()));
   bound.setWidth(bound.width() + sizeOfGrip().width());
   bound.setHeight(bound.height() + sizeOfGrip().height());
@@ -290,42 +322,40 @@
 
 
 QRectF ViewItem::boundingRect() const {
-  if (!isSelected() && !_hovering)
+  if (!isSelected() && !isHovering() || parentView()->mouseMode() == View::Create)
     return QGraphicsRectItem::boundingRect();
 
+#if SELECT_BOUND
   QPolygonF gripBound = mapFromScene(gripBoundingRect());
+#else
+  QPolygonF gripBound = gripBoundingRect();
+#endif
   return QRectF(gripBound[0], gripBound[2]);
 }
 
 
 QPainterPath ViewItem::shape() const {
-  if (!isSelected() && !_hovering)
+  if (!isSelected() && !isHovering() || parentView()->mouseMode() == View::Create)
     return itemShape();
 
   QPainterPath selectPath;
 
-//   if (_mouseMode != Resize)
+#if SELECT_BOUND
     selectPath.addPolygon(mapFromScene(selectBoundingRect()));
-//   else
-//     selectPath.addPolygon(rect());
+#else
+    selectPath.addPolygon(rect());
+#endif
 
   selectPath.addPath(grips());
   return selectPath;
 }
 
-QLineF ViewItem::originLine() const {
-  QRectF r = selectBoundingRect();
-  r.setBottom(r.bottom() - (r.height() / 2));
-  QPolygonF polygon = mapFromScene(r);
-  QPointF right = polygon[2]; //bottomRight
-  return QLineF(rect().center(), right);
-}
 
 void ViewItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
 
   painter->save();
   painter->setPen(Qt::DotLine);
-  if (isSelected() || _hovering) {
+  if (isSelected() || isHovering() && parentView()->mouseMode() != View::Create) {
     painter->drawPath(shape());
     if (_mouseMode == Resize)
       painter->fillPath(grips(), Qt::blue);
@@ -341,8 +371,6 @@
   painter->fillPath(shape(), semiRed);
 
   QPen p = painter->pen();
-  painter->setPen(Qt::black);
-  painter->drawLine(originLine());
 
   painter->setPen(Qt::white);
   painter->drawLine(_normalLine);
@@ -414,7 +442,7 @@
   if (event == View::MousePress) {
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MousePress));
     setPos(poly.first().x(), poly.first().y());
-    setViewRect(0, 0, 0, 0);
+    setViewRect(0.0, 0.0, 0.0, 0.0);
     parentView()->scene()->addItem(this);
     setZValue(1);
     return;
@@ -500,7 +528,30 @@
 
   if (mouseMode() == ViewItem::Rotate) {
 
+#if SELECT_BOUND
     rotateTowards(l, p);
+#else
+    switch(_activeGrip) {
+    case TopLeftGrip:
+        rotateTowards(topLeftGrip().boundingRect().center(), p); break;
+    case TopRightGrip:
+        rotateTowards(topRightGrip().boundingRect().center(), p); break;
+    case BottomRightGrip:
+        rotateTowards(bottomRightGrip().boundingRect().center(), p); break;
+    case BottomLeftGrip:
+        rotateTowards(bottomLeftGrip().boundingRect().center(), p); break;
+    case TopMidGrip:
+        rotateTowards(topMidGrip().boundingRect().center(), p); break;
+    case RightMidGrip:
+        rotateTowards(rightMidGrip().boundingRect().center(), p); break;
+    case BottomMidGrip:
+        rotateTowards(bottomMidGrip().boundingRect().center(), p); break;
+    case LeftMidGrip:
+        rotateTowards(leftMidGrip().boundingRect().center(), p); break;
+    case NoGrip:
+      break;
+    }
+#endif
 
   } else if (mouseMode() == ViewItem::Resize) {
 
@@ -813,20 +864,23 @@
 
 void ViewItem::rotateTowards(const QPointF &corner, const QPointF &point) {
 
-  QPointF origin = rect().center();
+  QPointF origin = centerOfRotation();
+  if (origin == corner || origin == point)
+    return;
+
   _normalLine = QLineF(origin, corner);
   _rotationLine = QLineF(origin, point);
 
-  qreal angle;
+  qreal angle1 = ::acos(_normalLine.dx() / _normalLine.length());
+  if (_normalLine.dy() >= 0)
+      angle1 = TWO_PI - angle1;
 
-  if (mapToScene(point).y() >= mapToScene(origin).y()) {
-/*    qDebug() << "positive" << endl;*/
-    angle = originLine().angle(_rotationLine) - originLine().angle(_normalLine);
-  } else {
-/*    qDebug() << "negative" << endl;*/
-    angle =  originLine().angle(_normalLine) - originLine().angle(_rotationLine);
-  }
+  qreal angle2 = ::acos(_rotationLine.dx() / _rotationLine.length());
+  if (_rotationLine.dy() >= 0)
+      angle2 = TWO_PI - angle2;
 
+  qreal angle = RAD2DEG * (angle1 - angle2);
+
   QTransform t;
   t.translate(origin.x(), origin.y());
   t.rotate(angle);
@@ -932,7 +986,11 @@
       setMouseMode(Resize);
       break;
     case Resize:
+#if SUPPRESS_SCALE
+      setMouseMode(Rotate);
+#else
       setMouseMode(Scale);
+#endif
       break;
     case Scale:
       setMouseMode(Rotate);
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #712065:712066
@@ -82,6 +82,10 @@
 
   virtual QList<DialogPage*> dialogPages() const { return QList<DialogPage*>(); }
 
+  virtual QPointF centerOfRotation() const { return rect().center(); }
+
+  bool isHovering() const { return _hovering; }
+
 Q_SIGNALS:
   void geometryChanged();
   void creationComplete();
@@ -112,16 +116,14 @@
   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:
+  virtual QPainterPath topLeftGrip() const;
+  virtual QPainterPath topRightGrip() const;
+  virtual QPainterPath bottomRightGrip() const;
+  virtual QPainterPath bottomLeftGrip() const;
+  virtual QPainterPath topMidGrip() const;
+  virtual QPainterPath rightMidGrip() const;
+  virtual QPainterPath bottomMidGrip() const;
+  virtual QPainterPath leftMidGrip() const;
   QTransform selectTransform() const;
   bool transformToRect(const QRectF &from, const QRectF &to);
   bool transformToRect(const QPolygonF &from, const QPolygonF &to);
@@ -147,7 +149,6 @@
 
 private:
   bool maybeReparent();
-  QLineF originLine() const;
 
 private:
   MouseMode _mouseMode;


More information about the Kst mailing list