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

Barth Netterfield netterfield at astro.utoronto.ca
Tue Sep 13 15:37:39 UTC 2011


SVN commit 1253177 by netterfield:

Allow labels to be resized with the mouse like other view objects.



 M  +47 -4     labelitem.cpp  
 M  +2 -0      labelitem.h  
 M  +1 -1      viewitem.cpp  
 M  +1 -1      viewitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/labelitem.cpp #1253176:1253177
@@ -30,10 +30,11 @@
 namespace Kst {
 
 LabelItem::LabelItem(View *parent, const QString& txt)
-  : ViewItem(parent), _labelRc(0), _dirty(true), _text(txt), _height(0) {
+  : ViewItem(parent), _labelRc(0), _dirty(true), _text(txt), _height(0), _resized(false) {
   setTypeName("Label");
-  setFixedSize(true);
-  setAllowedGripModes(Move /*| Resize*/ | Rotate /*| Scale*/);
+  setFixedSize(false);
+  setLockAspectRatio(true);
+  setAllowedGripModes(Move | Resize | Rotate /*| Scale*/);
 
   applyDefaults();
 }
@@ -63,7 +64,9 @@
 }
 
 void LabelItem::generateLabel() {
+  double lines = 1.0;
   if (_labelRc) {
+    lines = _labelRc->lines;
     delete _labelRc;
   }
 
@@ -73,8 +76,14 @@
     _dirty = false;
     QRectF box = rect();
     QFont font(_font);
+    if (_resized) {
     font.setPointSizeF(view()->viewScaledFontSize(_scale));
     QFontMetrics fm(font);
+      double fs_adjust = rect().height()/(fm.height()*(lines+1));
+      _scale *= fs_adjust;
+    }
+    font.setPointSizeF(view()->viewScaledFontSize(_scale));
+    QFontMetrics fm(font);
     _paintTransform.reset();
     _paintTransform.translate(box.x(), box.y() + fm.ascent());
     _labelRc = new Label::RenderContext(font, 0);
@@ -83,8 +92,33 @@
     _height = fm.height();
 
     // Make sure we have a rect for selection, movement, etc
+    if (_resized) {
+      _resized = false;
+      double x0 = rect().x();
+      double y0 = rect().y();
+      double x1 = x0 + rect().width();
+      double y1 = y0 + rect().height();
+      double w = _labelRc->xMax;
+      double h = (_labelRc->lines+1) * _height;
+      switch(_activeGrip) {
+      case TopLeftGrip:
+        setViewRect(QRectF(x1-w,y1-h,w,h));
+        break;
+      case TopRightGrip:
+        setViewRect(QRectF(x0,y1-h,w,h));
+        break;
+      case BottomLeftGrip:
+        setViewRect(QRectF(x1-w,y0,w,h));
+        break;
+      case BottomRightGrip:
+      case NoGrip:
+      default:
+        setViewRect(QRectF(x0,y0,w,h));
+        break;
+      }
+    } else {
     setViewRect(QRectF(rect().x(), rect().y(), _labelRc->xMax, (_labelRc->lines+1) * _height));
-
+    }
     connect(_labelRc, SIGNAL(labelDirty()), this, SLOT(setDirty()));
     connect(_labelRc, SIGNAL(labelDirty()), this, SLOT(triggerUpdate()));
 
@@ -177,6 +211,15 @@
 }
 
 
+void LabelItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+  ViewItem::mouseMoveEvent(event);
+
+  if ((gripMode() == ViewItem::Resize) && (activeGrip() != NoGrip)) {
+    _resized = true;
+  }
+
+}
+
 void LabelItem::creationPolygonChanged(View::CreationEvent event) {
 
   if (event == View::MouseMove) {
--- branches/work/kst/portto4/kst/src/libkstapp/labelitem.h #1253176:1253177
@@ -65,6 +65,7 @@
 
   protected Q_SLOTS:
     virtual void creationPolygonChanged(View::CreationEvent event);
+    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
 
   private:
     void generateLabel();
@@ -77,6 +78,7 @@
     QColor _color;
     QFont _font;
     qreal _height;
+    bool _resized;
 };
 
 
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #1253176:1253177
@@ -60,6 +60,7 @@
     _isXTiedZoom(false),
     _isYTiedZoom(false),
     _plotMaximized(false),
+    _activeGrip(NoGrip),
     _gripMode(Move),
     _allowedGripModes(Move | Resize | Rotate /*| Scale*/),
     _creationState(None),
@@ -78,7 +79,6 @@
     _acceptsContextMenuEvents(true),
     _updatingLayout(false),
     _highlighted(false),
-    _activeGrip(NoGrip),
     _allowedGrips(TopLeftGrip | TopRightGrip | BottomRightGrip | BottomLeftGrip |
                   TopMidGrip | RightMidGrip | BottomMidGrip | LeftMidGrip),
     _parentRelativeHeight(0),
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #1253176:1253177
@@ -325,6 +325,7 @@
     virtual void _initializeShortName();
     QPointF dragStartPosition;
     void startDragging(QWidget *widget, const QPointF& hotspot);
+    ActiveGrip _activeGrip;
 
   private:
     GripMode _gripMode;
@@ -352,7 +353,6 @@
     QTransform _originalTransform;
     QLineF _normalLine;
     QLineF _rotationLine;
-    ActiveGrip _activeGrip;
     ActiveGrips _allowedGrips;
     QTransform _rotationTransform;
     QHash<QString, QAction*> _shortcutMap;


More information about the Kst mailing list