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

Adam Treat treat at kde.org
Wed Sep 19 23:39:51 CEST 2007


SVN commit 714552 by treat:

* Provide bit flags to set/unset the various
grip modes.  Labelitems can no longer resize for
instance.


 M  +2 -0      labelitem.cpp  
 M  +20 -19    lineitem.cpp  
 M  +115 -65   viewitem.cpp  
 M  +34 -6     viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/labelitem.cpp #714551:714552
@@ -23,6 +23,8 @@
 LabelItem::LabelItem(View *parent, const QString& txt)
   : ViewItem(parent), _parsed(0), _text(txt) {
   setName("LabelItem");
+
+  setAllowedGripModes(Move /*| Resize*/ | Rotate /*| Scale*/);
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.cpp #714551:714552
@@ -22,6 +22,7 @@
 LineItem::LineItem(View *parent)
   : ViewItem(parent) {
   setName("LineItem");
+  setAllowedGrips(RightMidGrip | LeftMidGrip);
 }
 
 
@@ -54,14 +55,14 @@
 
 
 QPainterPath LineItem::leftMidGrip() const {
-  if (mouseMode() == Default || mouseMode() == Move)
+  if (gripMode() == 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)
+  if (gripMode() == Resize || gripMode() == Scale)
     path.addRect(grip);
   else
     path.addEllipse(grip);
@@ -71,14 +72,14 @@
 
 
 QPainterPath LineItem::rightMidGrip() const {
-  if (mouseMode() == Default || mouseMode() == Move)
+  if (gripMode() == 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)
+  if (gripMode() == Resize || gripMode() == Scale)
     path.addRect(grip);
   else
     path.addEllipse(grip);
@@ -88,7 +89,7 @@
 
 
 QPainterPath LineItem::grips() const {
-  if (mouseMode() == Default || mouseMode() == Move)
+  if (gripMode() == Move)
     return QPainterPath();
 
   QPainterPath grips;
@@ -148,21 +149,21 @@
 
 void LineItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 
-  if (parentView()->viewMode() == View::Data) {
-    event->ignore();
-    return;
-  }
+//   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);
+//   }
 
-  QPointF p = event->pos();
-  if (leftMidGrip().contains(p)) {
-    setActiveGrip(LeftMidGrip);
-  } else if (rightMidGrip().contains(p)) {
-    setActiveGrip(RightMidGrip);
-  } else {
-    setActiveGrip(NoGrip);
-  }
-
-  QGraphicsRectItem::mousePressEvent(event);
+  ViewItem::mousePressEvent(event);
 }
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #714551:714552
@@ -32,17 +32,19 @@
 // #define DEBUG_GEOMETRY
 // #define DEBUG_REPARENT
 #define INKSCAPE_MODE 0
-#define SUPPRESS_SCALE 1
 
 namespace Kst {
 
 ViewItem::ViewItem(View *parent)
   : QObject(parent),
-    _mouseMode(Default),
+    _gripMode(Move),
+    _allowedGripModes(Move | Resize | Rotate /*| Scale*/),
     _hovering(false),
     _lockAspectRatio(false),
     _layout(0),
-    _activeGrip(NoGrip) {
+    _activeGrip(NoGrip),
+    _allowedGrips(TopLeftGrip | TopRightGrip | BottomRightGrip | BottomLeftGrip |
+                  TopMidGrip | RightMidGrip | BottomMidGrip | LeftMidGrip) {
 
   setName("ViewItem");
   setAcceptsHoverEvents(true);
@@ -61,21 +63,32 @@
 }
 
 
-ViewItem::MouseMode ViewItem::mouseMode() const {
-  return _mouseMode;
+ViewItem::GripMode ViewItem::gripMode() const {
+  return _gripMode;
 }
 
 
-void ViewItem::setMouseMode(MouseMode mode) {
-#if SUPPRESS_SCALE
-  if (mode == Scale)
-    return;
-#endif
-  _mouseMode = mode;
+void ViewItem::setGripMode(GripMode mode) {
+  _gripMode = mode;
   update();
 }
 
 
+ViewItem::GripModes ViewItem::allowedGripModes() const {
+  return _allowedGripModes;
+}
+
+
+void ViewItem::setAllowedGripModes(GripModes modes) {
+  _allowedGripModes = modes;
+}
+
+
+bool ViewItem::isAllowed(GripMode mode) const {
+  return _allowedGripModes & mode;
+}
+
+
 ViewGridLayout *ViewItem::layout() const {
   return _layout;
 }
@@ -134,6 +147,9 @@
 
 
 QSizeF ViewItem::sizeOfGrip() const {
+  if (!parentView())
+    return QSizeF();
+
   int base = 15;
 #if INKSCAPE_MODE
   return mapFromScene(parentView()->mapToScene(QRect(0, 0, base, base)).boundingRect()).boundingRect().size();
@@ -144,13 +160,13 @@
 
 
 QPainterPath ViewItem::topLeftGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move)
+  if (_gripMode == Move)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
   QRectF grip = QRectF(bound.topLeft(), sizeOfGrip());
   QPainterPath path;
-  if (_mouseMode == Resize || _mouseMode == Scale)
+  if (_gripMode == Resize || _gripMode == Scale)
     path.addRect(grip);
   else
     path.addEllipse(grip);
@@ -164,13 +180,13 @@
 
 
 QPainterPath ViewItem::topRightGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move)
+  if (_gripMode == Move)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
   QRectF grip = QRectF(bound.topRight() - QPointF(sizeOfGrip().width(), 0), sizeOfGrip());
   QPainterPath path;
-  if (_mouseMode == Resize || _mouseMode == Scale)
+  if (_gripMode == Resize || _gripMode == Scale)
     path.addRect(grip);
   else
     path.addEllipse(grip);
@@ -184,13 +200,13 @@
 
 
 QPainterPath ViewItem::bottomRightGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move)
+  if (_gripMode == Move)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
   QRectF grip = QRectF(bound.bottomRight() - QPointF(sizeOfGrip().width(), sizeOfGrip().height()), sizeOfGrip());
   QPainterPath path;
-  if (_mouseMode == Resize || _mouseMode == Scale)
+  if (_gripMode == Resize || _gripMode == Scale)
     path.addRect(grip);
   else
     path.addEllipse(grip);
@@ -204,13 +220,13 @@
 
 
 QPainterPath ViewItem::bottomLeftGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move)
+  if (_gripMode == Move)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
   QRectF grip = QRectF(bound.bottomLeft() - QPointF(0, sizeOfGrip().height()), sizeOfGrip());
   QPainterPath path;
-  if (_mouseMode == Resize || _mouseMode == Scale)
+  if (_gripMode == Resize || _gripMode == Scale)
     path.addRect(grip);
   else
     path.addEllipse(grip);
@@ -224,7 +240,7 @@
 
 
 QPainterPath ViewItem::topMidGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move || _mouseMode == Rotate)
+  if (_gripMode == Move || _gripMode == Rotate)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
@@ -243,7 +259,7 @@
 
 
 QPainterPath ViewItem::rightMidGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move || _mouseMode == Rotate)
+  if (_gripMode == Move || _gripMode == Rotate)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
@@ -262,7 +278,7 @@
 
 
 QPainterPath ViewItem::bottomMidGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move || _mouseMode == Rotate)
+  if (_gripMode == Move || _gripMode == Rotate)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
@@ -281,7 +297,7 @@
 
 
 QPainterPath ViewItem::leftMidGrip() const {
-  if (_mouseMode == Default || _mouseMode == Move || _mouseMode == Rotate)
+  if (_gripMode == Move || _gripMode == Rotate)
     return QPainterPath();
 
   QRectF bound = gripBoundingRect();
@@ -301,7 +317,7 @@
 
 QPainterPath ViewItem::grips() const {
 
-  if (_mouseMode == Default || _mouseMode == Move)
+  if (_gripMode == Move)
     return QPainterPath();
 
   QPainterPath grips;
@@ -327,6 +343,21 @@
 }
 
 
+ViewItem::ActiveGrips ViewItem::allowedGrips() const {
+  return _allowedGrips;
+}
+
+
+void ViewItem::setAllowedGrips(ActiveGrips grips) {
+  _allowedGrips = grips;
+}
+
+
+bool ViewItem::isAllowed(ActiveGrip grip) const {
+  return _allowedGrips & grip;
+}
+
+
 QRectF ViewItem::selectBoundingRect() const {
 #if INKSCAPE_MODE
   return mapToScene(itemShape()).boundingRect();
@@ -393,11 +424,11 @@
   painter->setBrush(Qt::NoBrush);
   if (isSelected() || isHovering() && parentView()->mouseMode() != View::Create) {
     painter->drawPath(shape());
-    if (_mouseMode == Resize)
+    if (_gripMode == Resize)
       painter->fillPath(grips(), Qt::blue);
-    else if (_mouseMode == Scale)
+    else if (_gripMode == Scale)
       painter->fillPath(grips(), Qt::black);
-    else if (_mouseMode == Rotate)
+    else if (_gripMode == Rotate)
       painter->fillPath(grips(), Qt::red);
   }
 
@@ -532,18 +563,16 @@
   }
 
   if (parentView()->mouseMode() == View::Default) {
-    if (mouseMode() == ViewItem::Default ||
-        mouseMode() == ViewItem::Move ||
-        activeGrip() == NoGrip) {
+    if (gripMode() == ViewItem::Move || activeGrip() == NoGrip) {
       parentView()->setMouseMode(View::Move);
       parentView()->undoStack()->beginMacro(tr("Move"));
-    } else if (mouseMode() == ViewItem::Resize) {
+    } else if (gripMode() == ViewItem::Resize) {
       parentView()->setMouseMode(View::Resize);
       parentView()->undoStack()->beginMacro(tr("Resize"));
-    } else if (mouseMode() == ViewItem::Scale) {
+    } else if (gripMode() == ViewItem::Scale) {
       parentView()->setMouseMode(View::Scale);
       parentView()->undoStack()->beginMacro(tr("Scale"));
-    } else if (mouseMode() == ViewItem::Rotate) {
+    } else if (gripMode() == ViewItem::Rotate) {
       parentView()->setMouseMode(View::Rotate);
       parentView()->undoStack()->beginMacro(tr("Rotate"));
     }
@@ -556,7 +585,7 @@
   QPointF l = event->lastPos();
   QPointF s = event->scenePos();
 
-  if (mouseMode() == ViewItem::Rotate) {
+  if (gripMode() == ViewItem::Rotate) {
 
 #if INKSCAPE_MODE
     rotateTowards(l, p);
@@ -583,7 +612,7 @@
     }
 #endif
 
-  } else if (mouseMode() == ViewItem::Resize) {
+  } else if (gripMode() == ViewItem::Resize) {
 
     switch(_activeGrip) {
     case TopLeftGrip:
@@ -606,7 +635,7 @@
       break;
     }
 
-  } else if (mouseMode() == ViewItem::Scale) {
+  } else if (gripMode() == ViewItem::Scale) {
 
     switch(_activeGrip) {
     case TopLeftGrip:
@@ -1219,30 +1248,71 @@
   }
 
   QPointF p = event->pos();
-  if (topLeftGrip().contains(p)) {
+  if (isAllowed(TopLeftGrip) && topLeftGrip().contains(p)) {
     setActiveGrip(TopLeftGrip);
-  } else if (topRightGrip().contains(p)) {
+  } else if (isAllowed(TopRightGrip) && topRightGrip().contains(p)) {
     setActiveGrip(TopRightGrip);
-  } else if (bottomRightGrip().contains(p)) {
+  } else if (isAllowed(BottomRightGrip) && bottomRightGrip().contains(p)) {
     setActiveGrip(BottomRightGrip);
-  } else if (bottomLeftGrip().contains(p)) {
+  } else if (isAllowed(BottomLeftGrip) && bottomLeftGrip().contains(p)) {
     setActiveGrip(BottomLeftGrip);
-  } else if (topMidGrip().contains(p)) {
+  } else if (isAllowed(TopMidGrip) && topMidGrip().contains(p)) {
     setActiveGrip(TopMidGrip);
-  } else if (rightMidGrip().contains(p)) {
+  } else if (isAllowed(RightMidGrip) && rightMidGrip().contains(p)) {
     setActiveGrip(RightMidGrip);
-  } else if (bottomMidGrip().contains(p)) {
+  } else if (isAllowed(BottomMidGrip) && bottomMidGrip().contains(p)) {
     setActiveGrip(BottomMidGrip);
-  } else if (leftMidGrip().contains(p)) {
+  } else if (isAllowed(LeftMidGrip) && leftMidGrip().contains(p)) {
     setActiveGrip(LeftMidGrip);
   } else {
     setActiveGrip(NoGrip);
   }
 
+  if (!grips().contains(event->pos()) && event->button() & Qt::LeftButton) {
+    setGripMode(nextGripMode(_gripMode));
+  }
+
   QGraphicsRectItem::mousePressEvent(event);
 }
 
 
+ViewItem::GripMode ViewItem::nextGripMode(GripMode currentMode) const {
+  if (!(_allowedGripModes & (Resize | Rotate | Scale)))
+    return currentMode;
+
+  switch (currentMode) {
+  case Move:
+    if (isAllowed(Resize))
+      return Resize;
+    else
+      return nextGripMode(Resize);
+    break;
+  case Resize:
+    if (isAllowed(Scale))
+      return Scale;
+    else
+      return nextGripMode(Scale);
+    break;
+  case Scale:
+    if (isAllowed(Rotate))
+      return Rotate;
+    else
+      return nextGripMode(Rotate);
+    break;
+  case Rotate:
+    if (isAllowed(Resize))
+      return Resize;
+    else
+      return nextGripMode(Resize);
+    break;
+  default:
+    break;
+  }
+
+  return currentMode;
+}
+
+
 void ViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
 
   if (parentView()->viewMode() == View::Data) {
@@ -1253,26 +1323,6 @@
   if (parentView()->mouseMode() != View::Default) {
     parentView()->setMouseMode(View::Default);
     parentView()->undoStack()->endMacro();
-  } else if (!grips().contains(event->pos()) && event->button() & Qt::LeftButton) {
-    switch (_mouseMode) {
-    case Default:
-    case Move:
-    case Rotate:
-      setMouseMode(Resize);
-      break;
-    case Resize:
-#if SUPPRESS_SCALE
-      setMouseMode(Rotate);
-#else
-      setMouseMode(Scale);
-#endif
-      break;
-    case Scale:
-      setMouseMode(Rotate);
-      break;
-    default:
-      break;
-    }
   }
 
   QGraphicsRectItem::mouseReleaseEvent(event);
@@ -1303,7 +1353,7 @@
   if (change == ItemSelectedChange) {
     bool selected = value.toBool();
     if (!selected) {
-      setMouseMode(ViewItem::Default);
+      setGripMode(ViewItem::Move);
       update();
     }
   }
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #714551:714552
@@ -28,12 +28,26 @@
 {
   Q_OBJECT
 public:
-  enum MouseMode { Default, Move, Resize, Scale, Rotate };
+  enum GripMode {
+    Move    = 1,
+    Resize  = 2,
+    Scale   = 4,
+    Rotate  = 8
+  };
+  Q_DECLARE_FLAGS(GripModes, GripMode)
 
   enum ActiveGrip {
-    NoGrip, TopLeftGrip, TopRightGrip, BottomRightGrip, BottomLeftGrip,
-    TopMidGrip, RightMidGrip, BottomMidGrip, LeftMidGrip
+    NoGrip          = 1,
+    TopLeftGrip     = 2,
+    TopRightGrip    = 4,
+    BottomRightGrip = 8,
+    BottomLeftGrip  = 16,
+    TopMidGrip      = 32,
+    RightMidGrip    = 64,
+    BottomMidGrip   = 128,
+    LeftMidGrip     = 256
   };
+  Q_DECLARE_FLAGS(ActiveGrips, ActiveGrip)
 
   ViewItem(View *parent);
   virtual ~ViewItem();
@@ -43,9 +57,13 @@
 
   View *parentView() const;
 
-  MouseMode mouseMode() const;
-  void setMouseMode(MouseMode mode);
+  GripMode gripMode() const;
+  void setGripMode(GripMode mode);
 
+  GripModes allowedGripModes() const;
+  void setAllowedGripModes(GripModes modes);
+  bool isAllowed(GripMode mode) const;
+
   bool lockAspectRatio() const { return _lockAspectRatio; }
   void setLockAspectRatio(bool lockAspectRatio) { _lockAspectRatio = lockAspectRatio; }
 
@@ -65,6 +83,10 @@
   ActiveGrip activeGrip() const;
   void setActiveGrip(ActiveGrip grip);
 
+  ActiveGrips allowedGrips() const;
+  void setAllowedGrips(ActiveGrips grips);
+  bool isAllowed(ActiveGrip grip) const;
+
   virtual QSizeF sizeOfGrip() const;
   virtual QPainterPath grips() const;
 
@@ -127,6 +149,7 @@
   void rotateTowards(const QPointF &corner, const QPointF &point);
   QPointF lockOffset(const QPointF &offset, qreal ratio, bool oddCorner) const;
   bool maybeReparent();
+  GripMode nextGripMode(GripMode currentMode) const;
 
 protected Q_SLOTS:
   virtual void creationPolygonChanged(View::CreationEvent event);
@@ -150,7 +173,8 @@
                                                    const QRectF &newParentRect);
 
 private:
-  MouseMode _mouseMode;
+  GripMode _gripMode;
+  GripModes _allowedGripModes;
   bool _hovering;
   bool _lockAspectRatio;
   ViewGridLayout *_layout;
@@ -160,11 +184,15 @@
   QLineF _normalLine;
   QLineF _rotationLine;
   ActiveGrip _activeGrip;
+  ActiveGrips _allowedGrips;
   QTransform _rotationTransform;
 
   friend class View;
 };
 
+Q_DECLARE_OPERATORS_FOR_FLAGS(ViewItem::GripModes)
+Q_DECLARE_OPERATORS_FOR_FLAGS(ViewItem::ActiveGrips)
+
 #ifndef QT_NO_DEBUG_STREAM
 Q_CORE_EXPORT QDebug operator<<(QDebug, ViewItem*);
 #endif


More information about the Kst mailing list