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

George Staikos staikos at kde.org
Wed May 30 18:45:04 CEST 2007


SVN commit 669862 by staikos:

make labelitem a rectitem so that it can use the label renderer.  Hook in the
label renderer in a very primitive manner - it works! - but it looks ugly.
This is also a bit slow, but it shows that compositing works.



 M  +1 -0      CMakeLists.txt  
 A             dataref.h   branches/work/kst/portto4/kst/src/old_libkstapp/dataref.h#668577 [License: GPL (v2+)]
 M  +51 -18    labelitem.cpp  
 M  +27 -21    labelitem.h  
 A             labelrenderer.cpp   branches/work/kst/portto4/kst/src/old_libkstapp/labelrenderer.cpp#668577 [License: GPL (v2+)]
 A             labelrenderer.h   branches/work/kst/portto4/kst/src/old_libkstapp/labelrenderer.h#668577 [License: GPL (v2+)]


--- branches/work/kst/portto4/kst/src/libkstapp/CMakeLists.txt #669861:669862
@@ -8,6 +8,7 @@
    ellipseitem.cpp
    kstapplication.cpp
    labelitem.cpp
+   labelrenderer.cpp
    lineitem.cpp
    mainwindow.cpp
    matrixmodel.cpp
--- branches/work/kst/portto4/kst/src/libkstapp/labelitem.cpp #669861:669862
@@ -10,6 +10,8 @@
  ***************************************************************************/
 
 #include "labelitem.h"
+#include <labelparser.h>
+#include "labelrenderer.h"
 
 #include <QDebug>
 #include <QInputDialog>
@@ -18,8 +20,8 @@
 
 namespace Kst {
 
-LabelItem::LabelItem(View *parent)
-    : ViewItem(parent) {
+LabelItem::LabelItem(View *parent, const QString& txt)
+    : ViewItem(parent), _parsed(0), _text(txt) {
   setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
   parent->setMouseMode(View::Create);
   parent->setCursor(Qt::IBeamCursor);
@@ -34,17 +36,41 @@
 
 
 LabelItem::~LabelItem() {
+  delete _parsed;
+  _parsed = 0;
 }
 
 
+void LabelItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+  if (!_parsed) {
+    _parsed = Label::parse(_text);
+  }
+
+  // We can do better here. - caching
+  if (_parsed) {
+    const qreal w = pen().widthF();
+    painter->save();
+    QRect box = rect().adjusted(w, w, -w, -w).toRect();
+    painter->translate(rect().topLeft());
+    Label::RenderContext rc(QFont().family(), 16, painter);
+    Label::renderLabel(rc, _parsed->chunk);
+    painter->restore();
+  }
+  QBrush b = brush();
+  setBrush(Qt::NoBrush);
+  QGraphicsRectItem::paint(painter, option, widget);
+  setBrush(b);
+}
+
+
 void LabelItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-  QGraphicsSimpleTextItem::mousePressEvent(event);
+  QGraphicsRectItem::mousePressEvent(event);
   _originalPos = pos();
 }
 
 
 void LabelItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  QGraphicsSimpleTextItem::mouseReleaseEvent(event);
+  QGraphicsRectItem::mouseReleaseEvent(event);
 
   QPointF newPos = pos();
   if (_originalPos != newPos)
@@ -54,23 +80,23 @@
 
 void LabelItem::creationPolygonChanged(View::CreationEvent event) {
   if (event == View::MousePress) {
-
-    bool ok;
-    QString text = QInputDialog::getText(parentView(), QObject::tr("label"),
-                                         QObject::tr("label:"), QLineEdit::Normal,
-                                         QString::null, &ok);
-    if (!ok || text.isEmpty()) {
-      //This will delete...
-      parentView()->setMouseMode(View::Default);
-      return;
-    }
-
     const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MousePress));
-    setText(text);
-    setPos(poly[0]);
+    setRect(poly.first().x(), poly.first().y(), poly.last().x() - poly.first().x(), poly.last().y() - poly.first().y());
     parentView()->scene()->addItem(this);
     setZValue(1);
+    return;
+  }
 
+  if (event == View::MouseMove) {
+    const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseMove));
+    setRect(rect().x(), rect().y(), poly.last().x() - rect().x(), poly.last().y() - rect().y());
+    return;
+  }
+
+  if (event == View::MouseRelease) {
+    const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseRelease));
+    setRect(rect().x(), rect().y(), poly.last().x() - rect().x(), poly.last().y() - rect().y());
+
 #ifdef DEBUG_GEOMETRY
     debugGeometry();
 #endif
@@ -83,8 +109,15 @@
   }
 }
 
+
 void CreateLabelCommand::createItem() {
-  _item = new LabelItem(_view);
+  bool ok;
+  QString text = QInputDialog::getText(_view, tr("Kst: Create Label"), tr("Label:"), QLineEdit::Normal, QString::null, &ok);
+  if (!ok || text.isEmpty()) {
+    return;
+  }
+
+  _item = new LabelItem(_view, text);
   connect(_item, SIGNAL(creationComplete()), this, SLOT(creationComplete()));
 
   //If the item is interrupted while creating itself it will destroy itself
--- branches/work/kst/portto4/kst/src/libkstapp/labelitem.h #669861:669862
@@ -12,39 +12,45 @@
 #ifndef LABELITEM_H
 #define LABELITEM_H
 
-#include <QGraphicsSimpleTextItem>
+#include <QGraphicsRectItem>
 #include "viewitem.h"
 
+namespace Label {
+  class Parsed;
+}
+
 namespace Kst {
 
-class LabelItem : public ViewItem, public QGraphicsSimpleTextItem
-{
+class LabelItem : public ViewItem, public QGraphicsRectItem {
   Q_OBJECT
-public:
-  LabelItem(View *parent);
-  virtual ~LabelItem();
+  public:
+    LabelItem(View *parent, const QString& labelText);
+    virtual ~LabelItem();
 
-  virtual QGraphicsItem *graphicsItem() { return this; }
+    virtual QGraphicsItem *graphicsItem() { return this; }
 
-protected:
-  void mousePressEvent(QGraphicsSceneMouseEvent *event);
-  void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
+    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
 
-private Q_SLOTS:
-  void creationPolygonChanged(View::CreationEvent event);
+  protected:
+    void mousePressEvent(QGraphicsSceneMouseEvent *event);
+    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
 
-private:
-  QPointF _originalPos;
+  private Q_SLOTS:
+    void creationPolygonChanged(View::CreationEvent event);
+
+  private:
+    QPointF _originalPos;
+    Label::Parsed *_parsed;
+    QString _text;
 };
 
 
-class KST_EXPORT CreateLabelCommand : public CreateCommand
-{
-public:
-  CreateLabelCommand() : CreateCommand(QObject::tr("Create Label")) {}
-  CreateLabelCommand(View *view): CreateCommand(view, QObject::tr("Create Label")) {}
-  virtual ~CreateLabelCommand() {}
-  virtual void createItem();
+class KST_EXPORT CreateLabelCommand : public CreateCommand {
+  public:
+    CreateLabelCommand() : CreateCommand(QObject::tr("Create Label")) {}
+    CreateLabelCommand(View *view): CreateCommand(view, QObject::tr("Create Label")) {}
+    virtual ~CreateLabelCommand() {}
+    virtual void createItem();
 };
 
 }


More information about the Kst mailing list