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

Mike Fenton mike at staikos.net
Wed Mar 5 18:10:04 CET 2008


SVN commit 782662 by fenton:

Redo of Line/Arrow updates.
Add ArrowItemDialog and configuration of Arrow settings.


 M  +43 -130   arrowitem.cpp  
 M  +16 -17    arrowitem.h  
 A             arrowitemdialog.cpp   [License: GPL (v2+)]
 A             arrowitemdialog.h   [License: GPL (v2+)]
 A             arrowpropertiestab.cpp   [License: GPL (v2+)]
 A             arrowpropertiestab.h   [License: GPL (v2+)]
 A             arrowpropertiestab.ui  
 M  +9 -0      dialog.cpp  
 M  +1 -0      dialog.h  
 M  +5 -0      libkstapp.pro  
 M  +55 -26    lineitem.cpp  
 M  +1 -2      lineitem.h  


--- branches/work/kst/portto4/kst/src/libkstapp/arrowitem.cpp #782661:782662
@@ -13,6 +13,7 @@
 
 #include "view.h"
 #include "viewitemzorder.h"
+#include "arrowitemdialog.h"
 
 #include <debug.h>
 
@@ -23,10 +24,13 @@
 namespace Kst {
 
 ArrowItem::ArrowItem(View *parent)
-  : ViewItem(parent) {
+  : LineItem(parent),
+  _startArrowHead(false),
+  _endArrowHead(true),
+  _startArrowScale(1.0),
+  _endArrowScale(1.0) {
   setName("Arrow");
   setZValue(ARROW_ZVALUE);
-  setAllowedGrips(RightMidGrip | LeftMidGrip);
   QBrush b = brush();
   b.setStyle(Qt::SolidPattern);
   setBrush(b);
@@ -39,151 +43,60 @@
 
 void ArrowItem::paint(QPainter *painter) {
   painter->drawLine(line());
-  double deltax = 2.0 * painter->pen().widthF();
-  double theta = atan2(double(line().y1() - line().y2()), double(line().x1() - line().x2())) - M_PI / 2.0;
-  double sina = sin(theta);
-  double cosa = cos(theta);
-  double yin = sqrt(3.0) * deltax;
-  double x1, y1, x2, y2;
-  QMatrix m(cosa, sina, -sina, cosa, 0.0, 0.0);
 
-  m.map( deltax, yin, &x1, &y1);
-  m.map(-deltax, yin, &x2, &y2);
+  if (_startArrowHead) {
+    double deltax = 2.0 * painter->pen().widthF() * _startArrowScale;
+    double theta = atan2(double(line().y2() - line().y1()), double(line().x2() - line().x1())) - M_PI / 2.0;
+    double sina = sin(theta);
+    double cosa = cos(theta);
+    double yin = sqrt(3.0) * deltax;
+    double x1, y1, x2, y2;
+    QMatrix m(cosa, sina, -sina, cosa, 0.0, 0.0);
 
-  QPolygonF pts;
-  pts.append(line().p2());
-  pts.append(line().p2() + QPointF(x1, y1));
-  pts.append(line().p2() + QPointF(x2, y2));
+    m.map( deltax, yin, &x1, &y1);
+    m.map(-deltax, yin, &x2, &y2);
 
-  painter->drawPolygon(pts);
-}
-
-
-void ArrowItem::save(QXmlStreamWriter &xml) {
-  xml.writeStartElement("arrow");
-  ViewItem::save(xml);
-  xml.writeEndElement();
-}
-
-
-QLineF ArrowItem::line() const {
-  return QLineF(rect().left(), rect().center().y(), rect().right(), rect().center().y());
-}
-
-
-void ArrowItem::setLine(const QLineF &line_) {
-  setPos(line_.p1());
-  setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height()));
-
-  if (!rect().isEmpty()) {
-    rotateTowards(line().p2(), line_.p2());
+    QPolygonF pts;
+    pts.append(line().p1());
+    pts.append(line().p1() + QPointF(x1, y1));
+    pts.append(line().p1() + QPointF(x2, y2));
+    painter->drawPolygon(pts);
   }
 
-  QRectF r = rect();
-  r.setSize(QSizeF(QLineF(line().p1(), line_.p2()).length(), r.height()));
-  setViewRect(r);
-}
+  if (_endArrowHead) {
+    double deltax = 2.0 * painter->pen().widthF() * _endArrowScale;
+    double theta = atan2(double(line().y1() - line().y2()), double(line().x1() - line().x2())) - M_PI / 2.0;
+    double sina = sin(theta);
+    double cosa = cos(theta);
+    double yin = sqrt(3.0) * deltax;
+    double x1, y1, x2, y2;
+    QMatrix m(cosa, sina, -sina, cosa, 0.0, 0.0);
 
+    m.map( deltax, yin, &x1, &y1);
+    m.map(-deltax, yin, &x2, &y2);
 
-QPainterPath ArrowItem::leftMidGrip() const {
-  QRectF bound = gripBoundingRect();
-  QRectF grip = QRectF(bound.topLeft(), sizeOfGrip());
-  grip.moveCenter(QPointF(grip.center().x(), bound.center().y()));
-  QPainterPath path;
-  if (gripMode() == Resize || gripMode() == Scale || gripMode() == Move)
-    path.addRect(grip);
-  else
-    path.addEllipse(grip);
-
-  return path;
-}
-
-
-QPainterPath ArrowItem::rightMidGrip() const {
-  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 (gripMode() == Resize || gripMode() == Scale || gripMode() == Move)
-    path.addRect(grip);
-  else
-    path.addEllipse(grip);
-
-  return path;
-}
-
-
-QPainterPath ArrowItem::grips() const {
-  QPainterPath grips;
-  grips.addPath(leftMidGrip());
-  grips.addPath(rightMidGrip());
-  return grips;
-}
-
-
-QPointF ArrowItem::centerOfRotation() const {
-  if (activeGrip() == RightMidGrip)
-    return line().p1();
-  else if (activeGrip() == LeftMidGrip)
-    return line().p2();
-
-  return line().p1();
-}
-
-
-void ArrowItem::creationPolygonChanged(View::CreationEvent event) {
-  if (event == View::MousePress) {
-    const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MousePress));
-    setPos(poly.first().x(), poly.first().y());
-    setViewRect(QRectF(0.0, 0.0, 0.0, sizeOfGrip().height()));
-    parentView()->scene()->addItem(this);
-    //setZValue(1);
-    return;
+    QPolygonF pts;
+    pts.append(line().p2());
+    pts.append(line().p2() + QPointF(x1, y1));
+    pts.append(line().p2() + QPointF(x2, y2));
+    painter->drawPolygon(pts);
   }
-
-  if (event == View::MouseMove) {
-    const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseMove));
-    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));
-    parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself
-    parentView()->disconnect(this, SLOT(creationPolygonChanged(View::CreationEvent)));
-    parentView()->setMouseMode(View::Default);
-    maybeReparent();
-    emit creationComplete();
-    return;
-  }
 }
 
 
-void ArrowItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
-  ViewItem::mouseMoveEvent(event);
+void ArrowItem::save(QXmlStreamWriter &xml) {
+  xml.writeStartElement("arrow");
+  ViewItem::save(xml);
+  xml.writeEndElement();
 }
 
 
-void ArrowItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  ViewItem::mousePressEvent(event);
+void ArrowItem::edit() {
+  ArrowItemDialog editDialog(this);
+  editDialog.exec();
 }
 
 
-void ArrowItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  ViewItem::mouseReleaseEvent(event);
-}
-
-
-void ArrowItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
-  ViewItem::mouseDoubleClickEvent(event);
-}
-
-
 void CreateArrowCommand::createItem() {
   _item = new ArrowItem(_view);
   _view->setCursor(Qt::CrossCursor);
--- branches/work/kst/portto4/kst/src/libkstapp/arrowitem.h #782661:782662
@@ -12,42 +12,41 @@
 #ifndef ARROWITEM_H
 #define ARROWITEM_H
 
-#include "viewitem.h"
+#include "lineitem.h"
 #include "graphicsfactory.h"
 
 namespace Kst {
 
-class ArrowItem : public ViewItem
+class ArrowItem : public LineItem
 {
   Q_OBJECT
   public:
     ArrowItem(View *parent);
     virtual ~ArrowItem();
 
-    QLineF line() const;
-    void setLine(const QLineF &line);
-
     virtual void save(QXmlStreamWriter &xml);
     virtual void paint(QPainter *painter);
 
-    virtual QPainterPath grips() const;
+    bool startArrowHead() { return _startArrowHead; }
+    void setStartArrowHead(const bool enabled) { _startArrowHead = enabled; }
 
-    virtual QPointF centerOfRotation() const;
+    bool endArrowHead() { return _endArrowHead; }
+    void setEndArrowHead(const bool enabled) { _endArrowHead = enabled; }
 
-  protected Q_SLOTS:
-    virtual void creationPolygonChanged(View::CreationEvent event);
+    qreal startArrowScale() { return _startArrowScale; }
+    void setStartArrowScale(const qreal scale) { _startArrowScale = scale; }
 
-  protected:
-    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
-    virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
-    virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-    virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
+    qreal endArrowScale() { return _endArrowScale; }
+    void setEndArrowScale(const qreal scale) { _endArrowScale = scale; }
 
-    virtual QPainterPath leftMidGrip() const;
-    virtual QPainterPath rightMidGrip() const;
+  public Q_SLOTS:
+    virtual void edit();
 
   private:
-    QLineF _line;
+    bool _startArrowHead;
+    bool _endArrowHead;
+    qreal _startArrowScale;
+    qreal _endArrowScale;
 };
 
 class KST_EXPORT CreateArrowCommand : public CreateCommand
--- branches/work/kst/portto4/kst/src/libkstapp/dialog.cpp #782661:782662
@@ -71,6 +71,15 @@
 }
 
 
+void Dialog::selectDialogPage(DialogPage *page) {
+  QList<QListWidgetItem*> items = _listWidget->findItems(page->pageTitle(), Qt::MatchExactly);
+  foreach (QListWidgetItem* item, items) {
+    _listWidget->setCurrentItem(item);
+    selectPageForItem(item);
+  }
+}
+
+
 void Dialog::setVisible(bool visible) {
 
   _listWidget->setVisible(_itemHash.count() > 1);
--- branches/work/kst/portto4/kst/src/libkstapp/dialog.h #782661:782662
@@ -35,6 +35,7 @@
     void addDialogPage(DialogPage *page);
     void removeDialogPage(DialogPage *page);
     DialogPage* getDialogPage(const QString &pageName);
+    void selectDialogPage(DialogPage *page);
 
   Q_SIGNALS:
     void ok();
--- branches/work/kst/portto4/kst/src/libkstapp/libkstapp.pro #782661:782662
@@ -24,6 +24,8 @@
     applicationsettings.cpp \
     applicationsettingsdialog.cpp \
     arrowitem.cpp \
+    arrowitemdialog.cpp \
+    arrowpropertiestab.cpp \
     axis.cpp \
     axistab.cpp \
     basicplugindialog.cpp \
@@ -111,6 +113,8 @@
     applicationsettings.h \
     applicationsettingsdialog.h \
     arrowitem.h \
+    arrowitemdialog.h \
+    arrowpropertiestab.h \
     axis.h \
     axistab.h \
     basicplugindialog.h \
@@ -197,6 +201,7 @@
 
 FORMS += \
     aboutdialog.ui \
+    arrowpropertiestab.ui \
     axistab.ui \
     basicplugintab.ui \
     changedatasampledialog.ui \
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.cpp #782661:782662
@@ -27,7 +27,10 @@
   setName("Line");
   setZValue(LINE_ZVALUE);
   setAllowedGrips(RightMidGrip | LeftMidGrip);
-}
+  setAllowedGripModes(Resize);
+  QPen p = pen();
+  p.setWidthF(1);
+  setPen(p);}
 
 
 LineItem::~LineItem() {
@@ -70,10 +73,7 @@
   QRectF grip = QRectF(bound.topLeft(), sizeOfGrip());
   grip.moveCenter(QPointF(grip.center().x(), bound.center().y()));
   QPainterPath path;
-  if (gripMode() == Resize || gripMode() == Scale || gripMode() == Move)
-    path.addRect(grip);
-  else
-    path.addEllipse(grip);
+  path.addEllipse(grip);
 
   return path;
 }
@@ -84,10 +84,7 @@
   QRectF grip = QRectF(bound.topRight() - QPointF(sizeOfGrip().width(), 0), sizeOfGrip());
   grip.moveCenter(QPointF(grip.center().x(), bound.center().y()));
   QPainterPath path;
-  if (gripMode() == Resize || gripMode() == Scale || gripMode() == Move)
-    path.addRect(grip);
-  else
-    path.addEllipse(grip);
+  path.addEllipse(grip);
 
   return path;
 }
@@ -106,7 +103,6 @@
     return line().p1();
   else if (activeGrip() == LeftMidGrip)
     return line().p2();
-
   return line().p1();
 }
 
@@ -145,26 +141,44 @@
 
 
 void LineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
-  ViewItem::mouseMoveEvent(event);
+
+  if (parentView()->viewMode() == View::Data || itemInLayout()) {
+    event->ignore();
+    return;
+  }
+
+  if (parentView()->mouseMode() == View::Default) {
+    if (gripMode() == ViewItem::Resize) {
+      parentView()->setMouseMode(View::Resize);
+      parentView()->undoStack()->beginMacro(tr("Resize"));
+    }
+  }
+
+  if (activeGrip() == NoGrip)
+    return QGraphicsRectItem::mouseMoveEvent(event);
+
+  QPointF p = event->pos();
+  QPointF l = event->lastPos();
+  QPointF s = event->scenePos();
+
+  if (gripMode() == ViewItem::Resize) {
+    switch(activeGrip()) {
+    case RightMidGrip:
+      resizeRight(p.x() - l.x());
+      rotateTowards(rightMidGrip().controlPointRect().center(), p);
+      break;
+    case LeftMidGrip:
+      resizeLeft(p.x() - l.x());
+      rotateTowards(leftMidGrip().controlPointRect().center(), p);
+      break;
+    default:
+      break;
+    }
+  }
 }
 
 
 void LineItem::mousePressEvent(QGraphicsSceneMouseEvent *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);
-//   }
-
   ViewItem::mousePressEvent(event);
 }
 
@@ -179,6 +193,21 @@
 }
 
 
+void LineItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
+  QGraphicsRectItem::hoverMoveEvent(event);
+  if (isSelected()) {
+    QPointF p = event->pos();
+    if (isAllowed(RightMidGrip) && rightMidGrip().contains(p) || isAllowed(LeftMidGrip) && leftMidGrip().contains(p)) {
+      parentView()->setCursor(Qt::CrossCursor);
+    } else {
+      parentView()->setCursor(Qt::SizeAllCursor);
+    }
+  } else {
+    parentView()->setCursor(Qt::SizeAllCursor);
+  }
+}
+
+
 void CreateLineCommand::createItem() {
   _item = new LineItem(_view);
   _view->setCursor(Qt::CrossCursor);
--- branches/work/kst/portto4/kst/src/libkstapp/lineitem.h #782661:782662
@@ -42,12 +42,11 @@
     virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
     virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
+    virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
 
     virtual QPainterPath leftMidGrip() const;
     virtual QPainterPath rightMidGrip() const;
 
-  private:
-    QLineF _line;
 };
 
 class KST_EXPORT CreateLineCommand : public CreateCommand


More information about the Kst mailing list