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

Adam Treat treat at kde.org
Thu Oct 4 00:40:29 CEST 2007


SVN commit 720872 by treat:

* And we have mouse based generic zoom :)
* Provide proper coordinate transformation
from the projection to the plotitem's coordinates.
* Update the mainwindow statusbar whenever we hover
the projection to report the current mouse projection
coordinates.
* Draw a crossCursor when we are in data mode and hovering
over a projection.


 M  +150 -15   plotrenderitem.cpp  
 M  +20 -7     plotrenderitem.h  
 M  +10 -68    vectorcurverenderitem.cpp  
 M  +0 -11     vectorcurverenderitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #720871:720872
@@ -12,15 +12,19 @@
 #include "plotrenderitem.h"
 
 #include <QTime>
+#include <QStatusBar>
+#include <QMainWindow>
+#include <QGraphicsSceneHoverEvent>
 
 #include "plotitem.h"
+#include "application.h"
 
 // #define CURVE_DRAWING_TIME
 
 namespace Kst {
 
 PlotRenderItem::PlotRenderItem(const QString &name, PlotItem *parentItem)
-  : ViewItem(parentItem->parentView()) {
+  : ViewItem(parentItem->parentView()), _zoomRect(QRectF()), _selectionRect(QRectF()) {
 
   setName(name);
   setParentItem(parentItem);
@@ -28,8 +32,13 @@
   setAllowedGripModes(0);
   setAllowedGrips(0);
 
-  connect(parentItem, SIGNAL(geometryChanged()), this, SLOT(updateGeometry()));
+  connect(parentItem, SIGNAL(geometryChanged()),
+          this, SLOT(updateGeometry()));
+  connect(parentItem->parentView(), SIGNAL(viewModeChanged(View::ViewMode)),
+          this, SLOT(updateViewMode()));
+
   updateGeometry(); //the initial rect
+  updateViewMode(); //the initial view
 }
 
 
@@ -42,15 +51,6 @@
 }
 
 
-void PlotRenderItem::updateGeometry() {
-  QRectF rect = plotItem()->rect().normalized();
-  QPointF margin(plotItem()->marginWidth(), plotItem()->marginHeight());
-  QPointF topLeft(rect.topLeft() + margin);
-  QPointF bottomRight(rect.bottomRight() - margin);
-  setViewRect(QRectF(topLeft, bottomRight));
-}
-
-
 void PlotRenderItem::setType(RenderType type) {
   _type = type;
 }
@@ -69,6 +69,27 @@
 }
 
 
+QRectF PlotRenderItem::zoomRect() const {
+  if (_zoomRect.isEmpty() || !_zoomRect.isValid())
+    return projectionRect();
+  else
+    return _zoomRect;
+}
+
+
+QRectF PlotRenderItem::projectionRect() const {
+  qreal minX, maxX, minY, maxY = 0.0;
+  foreach (KstRelationPtr relation, relationList()) {
+      minX = qMin(relation->minX(), minX);
+      minY = qMin(relation->minY(), minY);
+      maxX = qMax(relation->maxX(), maxX);
+      maxY = qMax(relation->maxY(), maxY);
+  }
+  return QRectF(QPointF(minX, minY),
+                QPointF(maxX, maxY));
+}
+
+
 void PlotRenderItem::setRelationList(const KstRelationList &relationList) {
   _relationList = relationList;
 }
@@ -91,6 +112,13 @@
 
   paintRelations(painter);
 
+  if (_selectionRect.isValid() && !_selectionRect.isEmpty()) {
+    painter->save();
+    painter->setPen(Qt::black);
+    painter->drawRect(_selectionRect);
+    painter->restore();
+  }
+
 #ifdef CURVE_DRAWING_TIME
   int elapsed = time.elapsed();
   qDebug()<<"curve drawing took" << elapsed << "to render.";
@@ -138,15 +166,122 @@
 }
 
 
-QRectF PlotRenderItem::mapToProjection(const QRectF &rect) {
-  return QRectF(mapToProjection(rect.topLeft()), mapToProjection(rect.bottomRight()));
+void PlotRenderItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+  if (parentView()->viewMode() != View::Data) {
+    event->ignore();
+    return;
+  }
+
+  _selectionRect.setBottomRight(event->pos());
+  update(); //FIXME should optimize instead of redrawing entire curve?
 }
 
 
-QRectF PlotRenderItem::mapFromProjection(const QRectF &rect) {
-  return QRectF(mapFromProjection(rect.topLeft()), mapFromProjection(rect.bottomRight()));
+void PlotRenderItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+  if (parentView()->viewMode() != View::Data) {
+    event->ignore();
+    return;
+  }
+
+  _selectionRect = QRectF(event->pos(), QSizeF(0,0));
 }
 
+
+void PlotRenderItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+  if (parentView()->viewMode() != View::Data) {
+    event->ignore();
+    return;
+  }
+
+  _zoomRect = mapToProjection(_selectionRect);
+  _selectionRect = QRectF();
+  update();
 }
 
+
+void PlotRenderItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
+  ViewItem::hoverMoveEvent(event);
+
+  const QPointF p = mapToProjection(event->pos());
+  QString message = QString("(%1, %2)").arg(QString::number(p.x())).arg(QString::number(p.y()));
+  kstApp->mainWindow()->statusBar()->showMessage(message);
+}
+
+
+void PlotRenderItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
+  ViewItem::hoverEnterEvent(event);
+
+  const QPointF p = mapToProjection(event->pos());
+  QString message = QString("(%1, %2)").arg(QString::number(p.x())).arg(QString::number(p.y()));
+  kstApp->mainWindow()->statusBar()->showMessage(message);
+}
+
+
+void PlotRenderItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
+  ViewItem::hoverLeaveEvent(event);
+
+  kstApp->mainWindow()->statusBar()->showMessage(QString());
+}
+
+
+QTransform PlotRenderItem::projectionTransform() const {
+  QTransform t;
+
+  QRectF v = QRectF(rect().bottomLeft(), viewRect().topRight());
+
+  QPolygonF from_ = QPolygonF(v);
+  from_.pop_back(); //get rid of last closed point
+
+  QPolygonF to_ = QPolygonF(zoomRect());
+  to_.pop_back(); //get rid of last closed point
+
+  QTransform::quadToQuad(from_, to_, t);
+  return t;
+}
+
+
+QPointF PlotRenderItem::mapToProjection(const QPointF &point) const {
+  return projectionTransform().map(point);
+}
+
+
+QPointF PlotRenderItem::mapFromProjection(const QPointF &point) const {
+  return projectionTransform().inverted().map(point);
+}
+
+
+QRectF PlotRenderItem::mapToProjection(const QRectF &rect) const {
+  return projectionTransform().mapRect(rect);
+}
+
+
+QRectF PlotRenderItem::mapFromProjection(const QRectF &rect) const {
+  return projectionTransform().inverted().mapRect(rect);
+}
+
+
+void PlotRenderItem::updateGeometry() {
+  QRectF rect = plotItem()->rect().normalized();
+  QPointF margin(plotItem()->marginWidth(), plotItem()->marginHeight());
+  QPointF topLeft(rect.topLeft() + margin);
+  QPointF bottomRight(rect.bottomRight() - margin);
+  setViewRect(QRectF(topLeft, bottomRight));
+}
+
+
+void PlotRenderItem::updateViewMode() {
+  switch (parentView()->viewMode()) {
+  case View::Data:
+    setCursor(Qt::CrossCursor);
+    break;
+  case View::Layout:
+    setCursor(Qt::ArrowCursor);
+    break;
+  default:
+    break;
+  }
+}
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.h #720871:720872
@@ -38,6 +38,8 @@
     RenderType type();
 
     QRectF plotRect() const;
+    QRectF zoomRect() const;
+    QRectF projectionRect() const;
 
     void setRelationList(const KstRelationList &relationList);
     KstRelationList relationList() const;
@@ -50,20 +52,31 @@
     QString rightLabel() const;
     QString topLabel() const;
 
-  public Q_SLOTS:
-    void updateGeometry();
+    QPointF mapToProjection(const QPointF &point) const;
+    QPointF mapFromProjection(const QPointF &point) const;
+    QRectF mapToProjection(const QRectF &rect) const;
+    QRectF mapFromProjection(const QRectF &rect) const;
 
   protected:
-    virtual QPointF mapToProjection(const QPointF &point) = 0;
-    virtual QPointF mapFromProjection(const QPointF &point) = 0;
+    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
+    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
+    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
 
-    QRectF mapToProjection(const QRectF &rect);
-    QRectF mapFromProjection(const QRectF &rect);
+    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
+    virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
+    virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
 
+    virtual QTransform projectionTransform() const;
+
+  private Q_SLOTS:
+    void updateGeometry();
+    void updateViewMode();
+
   private:
     RenderType _type;
-
     KstRelationList _relationList;
+    QRectF _zoomRect;
+    QRectF _selectionRect;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/vectorcurverenderitem.cpp #720871:720872
@@ -11,11 +11,11 @@
 
 #include "vectorcurverenderitem.h"
 
-#include "plotitem.h"
-
 #include <QDebug>
 #include <QGraphicsSceneMouseEvent>
 
+#include "plotitem.h"
+
 namespace Kst {
 
 VectorCurveRenderItem::VectorCurveRenderItem(const QString &name, PlotItem *parentItem)
@@ -45,28 +45,19 @@
     context.penWidth = 1.0;
 
     //FIXME rename these methods in kstvcurve
-    QRectF vectorRect(QPointF(relation->minX(), relation->minY()),
-                      QPointF(relation->maxX(), relation->maxY()));
 
-    QTransform t;
-    qreal scaleFactor = 1.0;
-    t.scale(scaleFactor, scaleFactor);
+    qDebug() << "============================================================>\n"
+             << "projectionRect" << projectionRect() << "\n"
+             << "zoomRect" << zoomRect() << "\n"
+             << "plotRect" << plotRect() << endl;
 
-    QRectF zoomRect = t.mapRect(vectorRect);
-    zoomRect.moveTopLeft(vectorRect.topLeft());
-
-//     qDebug() << "============================================================>\n"
-//              << "vectorRect" << vectorRect << "\n"
-//              << "zoomRect" << zoomRect << "\n"
-//              << "plotRect" << plotRect() << endl;
-
     //FIXME Completely refactor KstCurveRenderContext now that we know what these are
 
     //Set what amounts to the zoombox...
-    context.XMin = zoomRect.left();
-    context.XMax = zoomRect.right();
-    context.YMin = zoomRect.top();
-    context.YMax = zoomRect.bottom();
+    context.XMin = zoomRect().left();
+    context.XMax = zoomRect().right();
+    context.YMin = zoomRect().top();
+    context.YMax = zoomRect().bottom();
 
     //These are the bounding box in regular QGV coord
     context.Lx = plotRect().left();
@@ -89,57 +80,8 @@
   }
 
   painter->restore();
-
-  if (_selectionRect.isValid() && !_selectionRect.isEmpty()) {
-    painter->save();
-    painter->setPen(Qt::black);
-    painter->drawRect(_selectionRect);
-    painter->restore();
-  }
 }
 
-
-void VectorCurveRenderItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
-  if (plotItem()->parentView()->viewMode() != View::Data) {
-    event->ignore();
-    return;
-  }
-
-  _selectionRect.setBottomRight(event->pos());
-  update(); //FIXME should optimize instead of redrawing entire curve?
 }
 
-
-void VectorCurveRenderItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  if (plotItem()->parentView()->viewMode() != View::Data) {
-    event->ignore();
-    return;
-  }
-
-  _selectionRect = QRectF(event->pos(), QSizeF(0,0));
-}
-
-
-void VectorCurveRenderItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  if (plotItem()->parentView()->viewMode() != View::Data) {
-    event->ignore();
-    return;
-  }
-
-  _selectionRect = QRectF();
-  update();
-}
-
-
-QPointF VectorCurveRenderItem::mapToProjection(const QPointF &point) {
-  return point;
-}
-
-
-QPointF VectorCurveRenderItem::mapFromProjection(const QPointF &point) {
-  return point;
-}
-
-}
-
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/vectorcurverenderitem.h #720871:720872
@@ -24,17 +24,6 @@
     virtual ~VectorCurveRenderItem();
 
     virtual void paintRelations(QPainter *painter);
-
-  protected:
-    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
-    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
-    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-
-    virtual QPointF mapToProjection(const QPointF &point);
-    virtual QPointF mapFromProjection(const QPointF &point);
-
-  private:
-    QRectF _selectionRect;
 };
 
 }


More information about the Kst mailing list