[Kst] branches/work/kst/portto4/kst/src/libkstapp
Adam Treat
treat at kde.org
Fri Oct 5 23:16:52 CEST 2007
SVN commit 721683 by treat:
* Begin implementation of shared axis for plots in layout.
* Don't reparent the plotrenderitems *ever*
* Bugfix for ViewItem.
* Provide suppression of labels.
etc,...
M +117 -39 plotitem.cpp
M +24 -0 plotitem.h
M +8 -5 plotrenderitem.cpp
M +1 -0 plotrenderitem.h
M +127 -0 viewgridlayout.cpp
M +10 -0 viewgridlayout.h
M +3 -1 viewitem.cpp
M +1 -1 viewitem.h
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.cpp #721682:721683
@@ -32,7 +32,13 @@
namespace Kst {
PlotItem::PlotItem(View *parent)
- : ViewItem(parent), _calculatedMarginWidth(0), _calculatedMarginHeight(0) {
+ : ViewItem(parent),
+ _isLeftLabelVisible(true),
+ _isBottomLabelVisible(true),
+ _isRightLabelVisible(true),
+ _isTopLabelVisible(true),
+ _calculatedMarginWidth(0.0),
+ _calculatedMarginHeight(0.0) {
setName("Plot");
setBrush(Qt::white);
@@ -116,41 +122,46 @@
}
-qreal PlotItem::calculatedMarginWidth() const {
- qreal m = qMax(MARGIN_WIDTH, _calculatedMarginWidth);
+QRectF PlotItem::plotRegion() const {
+ qreal left = isLeftLabelVisible() ? marginWidth() : 0.5;
+ qreal bottom = isBottomLabelVisible() ? marginHeight() : 0.5;
+ qreal right = isRightLabelVisible() ? marginWidth() : 0.5;
+ qreal top = isTopLabelVisible() ? marginHeight() : 0.5;
- //No more than 1/4 the width of the plot
- if (width() < m * 4)
- return width() / 4;
+ QPointF topLeft(rect().topLeft() + QPointF(left, top));
+ QPointF bottomRight(rect().bottomRight() - QPointF(right, bottom));
- return m;
+ return QRectF(topLeft, bottomRight);
}
-void PlotItem::setCalculatedMarginWidth(qreal marginWidth) {
- qreal before = this->calculatedMarginWidth();
- _calculatedMarginWidth = marginWidth;
- if (before != this->calculatedMarginWidth())
- emit geometryChanged();
+QRectF PlotItem::projectionRect() const {
+ QRectF rect(QPointF(-0.1, -0.1), QPointF(0.1, 0.1)); //default
+ foreach (PlotRenderItem *renderer, _renderers) {
+ if (!renderer->projectionRect().isEmpty())
+ rect = rect.united(renderer->projectionRect());
+ }
+ return rect;
}
-qreal PlotItem::calculatedMarginHeight() const {
- qreal m = qMax(MARGIN_HEIGHT, _calculatedMarginHeight);
-
- //No more than 1/4 the height of the plot
- if (height() < m * 4)
- return height() / 4;
-
- return m;
+qreal PlotItem::marginWidth() const {
+ ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(parentItem());
+ if (viewItem && viewItem->layout()) {
+ return viewItem->layout()->plotMarginWidth(this);
+ } else {
+ return calculatedMarginWidth();
+ }
}
-void PlotItem::setCalculatedMarginHeight(qreal marginHeight) {
- qreal before = this->calculatedMarginHeight();
- _calculatedMarginHeight = marginHeight;
- if (before != this->calculatedMarginHeight())
- emit geometryChanged();
+qreal PlotItem::marginHeight() const {
+ ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(parentItem());
+ if (viewItem && viewItem->layout()) {
+ return viewItem->layout()->plotMarginHeight(this);
+ } else {
+ return calculatedMarginHeight();
+ }
}
@@ -190,26 +201,93 @@
}
-qreal PlotItem::marginWidth() const {
- ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(parentItem());
- if (viewItem && viewItem->layout()) {
- return viewItem->layout()->plotMarginWidth(this);
- } else {
- return calculatedMarginWidth();
- }
+bool PlotItem::PlotItem::isLeftLabelVisible() const {
+ return _isLeftLabelVisible;
}
-qreal PlotItem::marginHeight() const {
- ViewItem *viewItem = qgraphicsitem_cast<ViewItem*>(parentItem());
- if (viewItem && viewItem->layout()) {
- return viewItem->layout()->plotMarginHeight(this);
- } else {
- return calculatedMarginHeight();
- }
+void PlotItem::PlotItem::setLeftLabelVisible(bool visible) {
+ _isLeftLabelVisible = visible;
}
+bool PlotItem::PlotItem::isBottomLabelVisible() const {
+ return _isBottomLabelVisible;
+}
+
+
+void PlotItem::PlotItem::setBottomLabelVisible(bool visible) {
+ _isBottomLabelVisible = visible;
+}
+
+
+bool PlotItem::PlotItem::isRightLabelVisible() const {
+ return _isRightLabelVisible;
+}
+
+
+void PlotItem::PlotItem::setRightLabelVisible(bool visible) {
+ _isRightLabelVisible = visible;
+}
+
+
+bool PlotItem::PlotItem::isTopLabelVisible() const {
+ return _isTopLabelVisible;
+}
+
+
+void PlotItem::PlotItem::setTopLabelVisible(bool visible) {
+ _isTopLabelVisible = visible;
+}
+
+
+void PlotItem::setLabelsVisible(bool visible) {
+ setLeftLabelVisible(visible);
+ setRightLabelVisible(visible);
+ setBottomLabelVisible(visible);
+ setTopLabelVisible(visible);
+ emit labelsVisibleChanged();
+}
+
+
+qreal PlotItem::calculatedMarginWidth() const {
+ qreal m = qMax(MARGIN_WIDTH, _calculatedMarginWidth);
+
+ //No more than 1/4 the width of the plot
+ if (width() < m * 4)
+ return width() / 4;
+
+ return m;
+}
+
+
+void PlotItem::setCalculatedMarginWidth(qreal marginWidth) {
+ qreal before = this->calculatedMarginWidth();
+ _calculatedMarginWidth = marginWidth;
+ if (before != this->calculatedMarginWidth())
+ emit geometryChanged();
+}
+
+
+qreal PlotItem::calculatedMarginHeight() const {
+ qreal m = qMax(MARGIN_HEIGHT, _calculatedMarginHeight);
+
+ //No more than 1/4 the height of the plot
+ if (height() < m * 4)
+ return height() / 4;
+
+ return m;
+}
+
+
+void PlotItem::setCalculatedMarginHeight(qreal marginHeight) {
+ qreal before = this->calculatedMarginHeight();
+ _calculatedMarginHeight = marginHeight;
+ if (before != this->calculatedMarginHeight())
+ emit geometryChanged();
+}
+
+
QRectF PlotItem::horizontalLabelRect(bool calc) const {
if (calc)
return QRectF(0.0, 0.0, width() - 2.0 * calculatedMarginWidth(), calculatedMarginHeight());
--- branches/work/kst/portto4/kst/src/libkstapp/plotitem.h #721682:721683
@@ -43,6 +43,9 @@
virtual void paint(QPainter *painter);
+ QRectF plotRegion() const;
+ QRectF projectionRect() const;
+
qreal marginWidth() const;
qreal marginHeight() const;
@@ -51,6 +54,23 @@
QString rightLabel() const;
QString topLabel() const;
+ bool isLeftLabelVisible() const;
+ void setLeftLabelVisible(bool visible);
+
+ bool isBottomLabelVisible() const;
+ void setBottomLabelVisible(bool visible);
+
+ bool isRightLabelVisible() const;
+ void setRightLabelVisible(bool visible);
+
+ bool isTopLabelVisible() const;
+ void setTopLabelVisible(bool visible);
+
+ void setLabelsVisible(bool visible);
+
+ Q_SIGNALS:
+ void labelsVisibleChanged(); //only emitted when 'setLabelsVisible' is called
+
private:
qreal calculatedMarginWidth() const;
void setCalculatedMarginWidth(qreal marginWidth);
@@ -72,6 +92,10 @@
private:
QList<PlotRenderItem*> _renderers;
+ bool _isLeftLabelVisible;
+ bool _isBottomLabelVisible;
+ bool _isRightLabelVisible;
+ bool _isTopLabelVisible;
qreal _calculatedMarginWidth;
qreal _calculatedMarginHeight;
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.cpp #721682:721683
@@ -44,6 +44,8 @@
connect(parentItem, SIGNAL(geometryChanged()),
this, SLOT(updateGeometry()));
+ connect(parentItem, SIGNAL(labelsVisibleChanged()),
+ this, SLOT(updateGeometry()));
connect(parentItem->parentView(), SIGNAL(viewModeChanged(View::ViewMode)),
this, SLOT(updateViewMode()));
@@ -805,6 +807,11 @@
}
+bool PlotRenderItem::maybeReparent() {
+ return false; //never reparent a plot renderer
+}
+
+
QRectF PlotRenderItem::checkBoxBoundingRect() const {
QRectF bound = selectBoundingRect();
bound.setTopLeft(bound.topLeft() - QPointF(sizeOfGrip().width(), sizeOfGrip().height()));
@@ -824,11 +831,7 @@
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));
+ setViewRect(plotItem()->plotRegion());
}
--- branches/work/kst/portto4/kst/src/libkstapp/plotrenderitem.h #721682:721683
@@ -115,6 +115,7 @@
virtual QPainterPath shape() const;
virtual QRectF boundingRect() const;
virtual QSizeF sizeOfGrip() const;
+ virtual bool maybeReparent();
QRectF checkBoxBoundingRect() const;
QPainterPath checkBox() const;
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.cpp #721682:721683
@@ -66,8 +66,10 @@
_rowCount = maxRow > _rowCount ? maxRow : _rowCount;
_columnCount = maxColumn > _columnCount ? maxColumn : _columnCount;
+ //FIXME these could be consolidated
_items.append(item);
_itemInfos.insert(viewItem, item);
+ _itemLayouts.insert(qMakePair(item.row, item.column), item);
}
@@ -119,13 +121,24 @@
item.viewItem->setTransform(item.transform);
item.viewItem->setPos(item.position);
item.viewItem->setViewRect(item.rect);
+ if (PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem))
+ plotItem->setLabelsVisible(true);
}
}
+void ViewGridLayout::resetSharedAxis() {
+ foreach (LayoutItem item, _items) {
+ if (PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem))
+ plotItem->setLabelsVisible(true);
+ }
+}
+
+
void ViewGridLayout::update() {
updatePlotMargins();
+ updateSharedAxis();
//For now we divide up equally... can do stretch factors and such later...
@@ -202,6 +215,120 @@
}
+void ViewGridLayout::updateSharedAxis() {
+
+ foreach (LayoutItem item, _items) {
+ PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem);
+
+ if (!plotItem)
+ continue;
+
+ //same horizontal range and same row/rowspan
+ //same vertical range and same col/colspan
+ shareAxisWithPlotToLeft(item);
+ shareAxisWithPlotToRight(item);
+ shareAxisWithPlotAbove(item);
+ shareAxisWithPlotBelow(item);
+ }
+}
+
+
+void ViewGridLayout::shareAxisWithPlotToLeft(LayoutItem item) const {
+ QPair<int, int> key = qMakePair(item.row, item.column - 1);
+ if (!_itemLayouts.contains(key))
+ return;
+
+ LayoutItem left = _itemLayouts.value(key);
+ PlotItem *leftItem = qgraphicsitem_cast<PlotItem*>(left.viewItem);
+ if (!leftItem)
+ return;
+
+ PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem);
+
+ //horizontal range check...
+ if (plotItem->projectionRect().left() != leftItem->projectionRect().left() ||
+ plotItem->projectionRect().right() != leftItem->projectionRect().right())
+ return;
+
+ if (item.rowSpan == left.rowSpan && item.columnSpan == left.columnSpan) {
+ plotItem->setLeftLabelVisible(false);
+ leftItem->setRightLabelVisible(false);
+ }
+}
+
+
+void ViewGridLayout::shareAxisWithPlotToRight(LayoutItem item) const {
+ QPair<int, int> key = qMakePair(item.row, item.column + 1);
+ if (!_itemLayouts.contains(key))
+ return;
+
+ LayoutItem right = _itemLayouts.value(key);
+ PlotItem *rightItem = qgraphicsitem_cast<PlotItem*>(right.viewItem);
+ if (!rightItem)
+ return;
+
+ PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem);
+
+ //horizontal range check...
+ if (plotItem->projectionRect().left() != rightItem->projectionRect().left() ||
+ plotItem->projectionRect().right() != rightItem->projectionRect().right())
+ return;
+
+ if (item.rowSpan == right.rowSpan && item.columnSpan == right.columnSpan) {
+ plotItem->setRightLabelVisible(false);
+ rightItem->setLeftLabelVisible(false);
+ }
+}
+
+
+void ViewGridLayout::shareAxisWithPlotAbove(LayoutItem item) const {
+ QPair<int, int> key = qMakePair(item.row - 1, item.column);
+ if (!_itemLayouts.contains(key))
+ return;
+
+ LayoutItem top = _itemLayouts.value(key);
+ PlotItem *topItem = qgraphicsitem_cast<PlotItem*>(top.viewItem);
+ if (!topItem)
+ return;
+
+ PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem);
+
+ //vertical range check...
+ if (plotItem->projectionRect().top() != topItem->projectionRect().top() ||
+ plotItem->projectionRect().bottom() != topItem->projectionRect().bottom())
+ return;
+
+ if (item.rowSpan == top.rowSpan && item.columnSpan == top.columnSpan) {
+ plotItem->setTopLabelVisible(false);
+ topItem->setBottomLabelVisible(false);
+ }
+}
+
+
+void ViewGridLayout::shareAxisWithPlotBelow(LayoutItem item) const {
+ QPair<int, int> key = qMakePair(item.row + 1, item.column);
+ if (!_itemLayouts.contains(key))
+ return;
+
+ LayoutItem bottom = _itemLayouts.value(key);
+ PlotItem *bottomItem = qgraphicsitem_cast<PlotItem*>(bottom.viewItem);
+ if (!bottomItem)
+ return;
+
+ PlotItem *plotItem = qgraphicsitem_cast<PlotItem*>(item.viewItem);
+
+ //vertical range check...
+ if (plotItem->projectionRect().top() != bottomItem->projectionRect().top() ||
+ plotItem->projectionRect().bottom() != bottomItem->projectionRect().bottom())
+ return;
+
+ if (item.rowSpan == bottom.rowSpan && item.columnSpan == bottom.columnSpan) {
+ plotItem->setBottomLabelVisible(false);
+ bottomItem->setTopLabelVisible(false);
+ }
+}
+
+
#if 0
void LayoutMarginCommand::undo() {
Q_ASSERT(_layout);
--- branches/work/kst/portto4/kst/src/libkstapp/viewgridlayout.h #721682:721683
@@ -53,6 +53,7 @@
public Q_SLOTS:
void reset();
+ void resetSharedAxis();
void update();
Q_SIGNALS:
@@ -73,6 +74,14 @@
QRectF rect;
};
+ private:
+ void updateSharedAxis();
+ void shareAxisWithPlotToLeft(LayoutItem item) const;
+ void shareAxisWithPlotToRight(LayoutItem item) const;
+ void shareAxisWithPlotAbove(LayoutItem item) const;
+ void shareAxisWithPlotBelow(LayoutItem item) const;
+
+ private:
bool _enabled;
int _rowCount;
int _columnCount;
@@ -84,6 +93,7 @@
QHash<int, qreal> _plotMarginWidth;
QHash<int, qreal> _plotMarginHeight;
QHash<const ViewItem*, LayoutItem> _itemInfos;
+ QHash< QPair<int, int>, LayoutItem> _itemLayouts;
};
//FIXME How far should we go with the command pattern?
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.cpp #721682:721683
@@ -445,7 +445,7 @@
painter->setBrush(Qt::NoBrush);
if (isSelected() || isHovering()
&& parentView()->mouseMode() != View::Create
- && parentView()->mouseMode() != View::Data) {
+ && parentView()->viewMode() != View::Data) {
painter->drawPath(shape());
if (_gripMode == Resize)
painter->fillPath(grips(), Qt::blue);
@@ -1611,7 +1611,9 @@
void BreakLayoutCommand::redo() {
_layout = _item->layout();
Q_ASSERT(_layout);
+ _layout->resetSharedAxis();
_item->setLayout(0);
+
}
--- branches/work/kst/portto4/kst/src/libkstapp/viewitem.h #721682:721683
@@ -156,7 +156,7 @@
bool transformToRect(const QPolygonF &from, const QPolygonF &to);
void rotateTowards(const QPointF &corner, const QPointF &point);
QPointF lockOffset(const QPointF &offset, qreal ratio, bool oddCorner) const;
- bool maybeReparent();
+ virtual bool maybeReparent();
GripMode nextGripMode(GripMode currentMode) const;
void addTitle(QMenu *menu) const;
void registerShortcut(QAction *action);
More information about the Kst
mailing list