[Kst] branches/work/kst/portto4/kst/src/libkstapp
Adam Treat
treat at kde.org
Wed Oct 3 22:27:19 CEST 2007
SVN commit 720832 by treat:
* PlotRenderItem's are now real ViewItem's
which means they now can reparent along with
everything else.
* Modify the paint routines of PlotRenderItem
to take this into account.
* Don't allow PlotRenderItem's to end up in a
layout.
* Item's that have a layout no longer completely
handle the children's events. This allows data
mode to work for PlotRenderItem's even when the
plot itself is in a layout.
* Clip the selectionRect of the PlotRenderItem.
* The View now default's to Data mode.
* Rects are always normalized. We no longer can
have items with negative width/height which leads
to artifacts and bad things.
M +13 -9 plotrenderitem.cpp
M +5 -6 plotrenderitem.h
M +11 -11 vectorcurverenderitem.cpp
M +1 -1 vectorcurverenderitem.h
M +1 -1 view.cpp
M +36 -12 viewitem.cpp
M +6 -0 viewitem.h
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #720831:720832
@@ -20,9 +20,14 @@
namespace Kst {
PlotRenderItem::PlotRenderItem(const QString &name, PlotItem *parentItem)
- : QObject(parentItem), QGraphicsRectItem(parentItem) {
- _name = name;
+ : ViewItem(parentItem->parentView()) {
+ setName(name);
+ setParentItem(parentItem);
+ setHasStaticGeometry(true);
+ setAllowedGripModes(0);
+ setAllowedGrips(0);
+
connect(parentItem, SIGNAL(geometryChanged()), this, SLOT(updateGeometry()));
updateGeometry(); //the initial rect
}
@@ -42,7 +47,7 @@
QPointF margin(plotItem()->marginWidth(), plotItem()->marginHeight());
QPointF topLeft(rect.topLeft() + margin);
QPointF bottomRight(rect.bottomRight() - margin);
- setRect(QRectF(topLeft, bottomRight));
+ setViewRect(QRectF(topLeft, bottomRight));
}
@@ -59,7 +64,7 @@
QRectF PlotRenderItem::plotRect() const {
QRectF plotRect = rect();
plotRect = plotRect.normalized();
- plotRect.moveTopLeft(QPoint(0,0));
+ plotRect.moveTopLeft(QPointF(0.0, 0.0));
return plotRect;
}
@@ -74,18 +79,17 @@
}
-void PlotRenderItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
- Q_UNUSED(option);
- Q_UNUSED(widget);
+void PlotRenderItem::paint(QPainter *painter) {
+ painter->setRenderHint(QPainter::Antialiasing, false);
+ painter->setClipRect(rect());
painter->drawRect(rect());
- painter->fillRect(rect(), Qt::white);
#ifdef CURVE_DRAWING_TIME
QTime time;
time.start();
#endif
- paint(painter);
+ paintRelations(painter);
#ifdef CURVE_DRAWING_TIME
int elapsed = time.elapsed();
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.h #720831:720832
@@ -12,10 +12,10 @@
#ifndef PLOTRENDERITEM_H
#define PLOTRENDERITEM_H
+#include "viewitem.h"
+
#include <QList>
-#include <QObject>
#include <QPainterPath>
-#include <QGraphicsRectItem>
#include "kstrelation.h"
@@ -25,7 +25,7 @@
enum RenderType { Cartesian, Polar, Sinusoidal };
-class PlotRenderItem : public QObject, public QGraphicsRectItem
+class PlotRenderItem : public ViewItem
{
Q_OBJECT
public:
@@ -42,8 +42,8 @@
void setRelationList(const KstRelationList &relationList);
KstRelationList relationList() const;
- virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
- virtual void paint(QPainter *painter) = 0;
+ virtual void paint(QPainter *painter);
+ virtual void paintRelations(QPainter *painter) = 0;
QString leftLabel() const;
QString bottomLabel() const;
@@ -61,7 +61,6 @@
QRectF mapFromProjection(const QRectF &rect);
private:
- QString _name;
RenderType _type;
KstRelationList _relationList;
--- branches/work/kst/portto4/kst/src/libkstapp/vectorcurverenderitem.cpp #720831:720832
@@ -28,18 +28,12 @@
}
-void VectorCurveRenderItem::paint(QPainter *painter) {
+void VectorCurveRenderItem::paintRelations(QPainter *painter) {
QRectF normalRect = rect();
normalRect = normalRect.normalized();
- if (_selectionRect.isValid() && !_selectionRect.isEmpty()) {
- painter->save();
- painter->setPen(Qt::black);
- painter->drawRect(_selectionRect);
- painter->restore();
- }
-
+ painter->save();
painter->translate(normalRect.x(), normalRect.y());
foreach (KstRelationPtr relation, relationList()) {
@@ -91,9 +85,15 @@
context.b_X = b_X;
context.b_Y = b_Y;
+ relation->paint(context);
+ }
+
+ painter->restore();
+
+ if (_selectionRect.isValid() && !_selectionRect.isEmpty()) {
painter->save();
- painter->setRenderHint(QPainter::Antialiasing, false);
- relation->paint(context);
+ painter->setPen(Qt::black);
+ painter->drawRect(_selectionRect);
painter->restore();
}
}
@@ -106,7 +106,7 @@
}
_selectionRect.setBottomRight(event->pos());
- update(_selectionRect);
+ update(); //FIXME should optimize instead of redrawing entire curve?
}
--- branches/work/kst/portto4/kst/src/libkstapp/vectorcurverenderitem.h #720831:720832
@@ -23,7 +23,7 @@
VectorCurveRenderItem(const QString &name, PlotItem *parentItem);
virtual ~VectorCurveRenderItem();
- virtual void paint(QPainter *painter);
+ virtual void paintRelations(QPainter *painter);
protected:
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #720831:720832
@@ -31,7 +31,7 @@
View::View()
: QGraphicsView(kstApp->mainWindow()),
- _viewMode(Layout),
+ _viewMode(Data),
_mouseMode(Default),
_layoutBoxItem(0),
_useOpenGL(false),
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #720831:720832
@@ -43,6 +43,7 @@
_allowedGripModes(Move | Resize | Rotate /*| Scale*/),
_hovering(false),
_lockAspectRatio(false),
+ _hasStaticGeometry(false),
_layout(0),
_activeGrip(NoGrip),
_allowedGrips(TopLeftGrip | TopRightGrip | BottomRightGrip | BottomLeftGrip |
@@ -65,6 +66,16 @@
}
+ViewItem *ViewItem::parentViewItem() const {
+ return qgraphicsitem_cast<ViewItem*>(parentItem());
+}
+
+
+bool ViewItem::itemInLayout() const {
+ return parentViewItem() && parentViewItem()->layout();
+}
+
+
ViewItem::GripMode ViewItem::gripMode() const {
return _gripMode;
}
@@ -112,8 +123,6 @@
_layout->setEnabled(true);
connect(this, SIGNAL(geometryChanged()), _layout, SLOT(update()));
}
-
- setHandlesChildEvents(_layout);
}
@@ -143,6 +152,9 @@
if (!viewItem)
continue;
+ if (viewItem->hasStaticGeometry())
+ continue;
+
ViewItem::updateChildGeometry(viewItem, oldViewRect, viewRect);
}
}
@@ -520,15 +532,19 @@
if (event == View::MouseMove) {
const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseMove));
- setViewRect(rect().x(), rect().y(),
- poly.last().x() - rect().x(), poly.last().y() - rect().y());
+ QRectF newRect(rect().x(), rect().y(),
+ poly.last().x() - rect().x(),
+ poly.last().y() - rect().y());
+ setViewRect(newRect);
return;
}
if (event == View::MouseRelease) {
const QPolygonF poly = mapFromScene(parentView()->creationPolygon(View::MouseRelease));
- setViewRect(rect().x(), rect().y(),
- poly.last().x() - rect().x(), poly.last().y() - rect().y());
+ QRectF newRect(rect().x(), rect().y(),
+ poly.last().x() - rect().x(),
+ poly.last().y() - rect().y());
+ setViewRect(newRect.normalized());
parentView()->disconnect(this, SLOT(deleteLater())); //Don't delete ourself
parentView()->disconnect(this, SLOT(creationPolygonChanged(View::CreationEvent)));
@@ -569,7 +585,7 @@
void ViewItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
- if (parentView()->viewMode() == View::Data) {
+ if (parentView()->viewMode() == View::Data || itemInLayout()) {
event->ignore();
return;
}
@@ -680,6 +696,7 @@
QRectF r = rect();
QPointF o = _lockAspectRatio ? lockOffset(offset, oldAspect, false) : offset;
r.setTopLeft(r.topLeft() + o);
+ if (!r.isValid()) return;
const qreal newAspect = r.width() / r.height();
Q_ASSERT_X(_lockAspectRatio ? qFuzzyCompare(newAspect, oldAspect) : true,
@@ -694,6 +711,7 @@
QRectF r = rect();
QPointF o = _lockAspectRatio ? lockOffset(offset, oldAspect, true) : offset;
r.setTopRight(r.topRight() + o);
+ if (!r.isValid()) return;
const qreal newAspect = r.width() / r.height();
Q_ASSERT_X(_lockAspectRatio ? qFuzzyCompare(newAspect, oldAspect) : true,
@@ -708,6 +726,7 @@
QRectF r = rect();
QPointF o = _lockAspectRatio ? lockOffset(offset, oldAspect, true) : offset;
r.setBottomLeft(r.bottomLeft() + o);
+ if (!r.isValid()) return;
const qreal newAspect = r.width() / r.height();
Q_ASSERT_X(_lockAspectRatio ? qFuzzyCompare(newAspect, oldAspect) : true,
@@ -722,6 +741,7 @@
QRectF r = rect();
QPointF o = _lockAspectRatio ? lockOffset(offset, oldAspect, false) : offset;
r.setBottomRight(r.bottomRight() + o);
+ if (!r.isValid()) return;
const qreal newAspect = r.width() / r.height();
Q_ASSERT_X(_lockAspectRatio ? qFuzzyCompare(newAspect, oldAspect) : true,
@@ -733,6 +753,7 @@
void ViewItem::resizeTop(qreal offset) {
QRectF r = rect();
r.setTop(r.top() + offset);
+ if (!r.isValid()) return;
setViewRect(r);
}
@@ -740,6 +761,7 @@
void ViewItem::resizeBottom(qreal offset) {
QRectF r = rect();
r.setBottom(r.bottom() + offset);
+ if (!r.isValid()) return;
setViewRect(r);
}
@@ -747,6 +769,7 @@
void ViewItem::resizeLeft(qreal offset) {
QRectF r = rect();
r.setLeft(r.left() + offset);
+ if (!r.isValid()) return;
setViewRect(r);
}
@@ -754,6 +777,7 @@
void ViewItem::resizeRight(qreal offset) {
QRectF r = rect();
r.setRight(r.right() + offset);
+ if (!r.isValid()) return;
setViewRect(r);
}
@@ -1245,7 +1269,7 @@
void ViewItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
- if (parentView()->viewMode() == View::Data) {
+ if (parentView()->viewMode() == View::Data || itemInLayout()) {
event->ignore();
return;
}
@@ -1254,7 +1278,7 @@
void ViewItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
- if (parentView()->viewMode() == View::Data) {
+ if (parentView()->viewMode() == View::Data || itemInLayout()) {
event->ignore();
return;
}
@@ -1327,7 +1351,7 @@
void ViewItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
- if (parentView()->viewMode() == View::Data) {
+ if (parentView()->viewMode() == View::Data || itemInLayout()) {
event->ignore();
return;
}
@@ -1362,7 +1386,7 @@
QVariant ViewItem::itemChange(GraphicsItemChange change, const QVariant &value) {
- if (change == ItemSelectedChange) {
+if (change == ItemSelectedChange) {
bool selected = value.toBool();
if (!selected) {
setGripMode(ViewItem::Move);
@@ -1496,7 +1520,7 @@
foreach (QGraphicsItem *item, list) {
ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(item);
- if (!viewItem || viewItem->parentItem() != _item)
+ if (!viewItem || viewItem->hasStaticGeometry() || viewItem->parentItem() != _item)
continue;
viewItems.append(viewItem);
}
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #720831:720832
@@ -56,6 +56,8 @@
int type() const { return Type; }
View *parentView() const;
+ ViewItem *parentViewItem() const;
+ bool itemInLayout() const;
GripMode gripMode() const;
void setGripMode(GripMode mode);
@@ -67,6 +69,9 @@
bool lockAspectRatio() const { return _lockAspectRatio; }
void setLockAspectRatio(bool lockAspectRatio) { _lockAspectRatio = lockAspectRatio; }
+ bool hasStaticGeometry() { return _hasStaticGeometry; }
+ void setHasStaticGeometry(bool hasStaticGeometry ) { _hasStaticGeometry = hasStaticGeometry; }
+
//NOTE We can change this to a generic abstract class once we have
//more layouts besides grid layout...
ViewGridLayout *layout() const;
@@ -177,6 +182,7 @@
GripModes _allowedGripModes;
bool _hovering;
bool _lockAspectRatio;
+ bool _hasStaticGeometry;
ViewGridLayout *_layout;
QPointF _originalPosition;
QRectF _originalRect;
More information about the Kst
mailing list