Permalink
Browse files

Align items to the center by adding in stretch factors at loose ends

  • Loading branch information...
aelliott committed Feb 16, 2012
1 parent 76a356c commit ab1a88bc630308020b4523dec2619c7103238315
@@ -23,6 +23,7 @@
AlternationGraphicsItem::AlternationGraphicsItem(QGraphicsLinearLayout *initialLayout, Token *token, int tokenPos, QGraphicsItem *parent)
: RegexGraphicsItem(token, tokenPos, parent)
+ , _currentLayout(0)
{
QSettings settings;
double itemSpacing = settings.value("Visualisation/VerticalSpacing", 12.0).toDouble();
@@ -43,7 +44,10 @@ AlternationGraphicsItem::AlternationGraphicsItem(QGraphicsLinearLayout *initialL
void AlternationGraphicsItem::newBranch()
{
+ if(_currentLayout != 0)
+ _currentLayout->addStretch();
_currentLayout = new QGraphicsLinearLayout(Qt::Horizontal);
+ _currentLayout->insertStretch(0);
_mainLayout->addItem(_currentLayout);
_mainLayout->setAlignment(_currentLayout, Qt::AlignTop | Qt::AlignHCenter);
}
@@ -85,6 +85,8 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
// This is a bit nasty, but it works
int start = static_cast<RegexGraphicsItem *>(currentLayout->itemAt(0))->tokenPos();
AlternationGraphicsItem *alternation = new AlternationGraphicsItem(currentLayout, *_iter, start);
+ currentLayout->insertStretch(0);
+ currentLayout->addStretch();
currentLayout = new QGraphicsLinearLayout(Qt::Horizontal);
currentLayout->setSpacing(_itemSpacing);
currentLayout->addItem(alternation);
@@ -110,6 +112,7 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
_incremented = true;
}
}
+ alternation->linearLayout()->addStretch();
alternation->setEndPos(_pos-1);
_lastItem = alternation;
}
@@ -261,7 +264,7 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
}
_lastItem->setGeometry(_lastItem->boundingRect());
- currentLayout->setAlignment(_lastItem, Qt::AlignVCenter | Qt::AlignLeft);
+ currentLayout->setAlignment(_lastItem, Qt::AlignCenter);
currentLayout->setGeometry(QRectF(0, 0, currentLayout->sizeHint(Qt::PreferredSize).width(),
currentLayout->sizeHint(Qt::PreferredSize).height()));
@@ -30,7 +30,7 @@ RepeatGraphicsItem::RepeatGraphicsItem(Token *token, int tokenPos, QGraphicsWidg
parseToken(*token);
- _layout = new QGraphicsLinearLayout(Qt::Vertical);
+ _layout = new QGraphicsLinearLayout(Qt::Horizontal);
_layout->setContentsMargins(horizontalPadding, 2*verticalPadding + _metrics.height(), horizontalPadding, verticalPadding);
setLayout(_layout);
_layout->setGeometry(boundingRect());
@@ -39,7 +39,9 @@ RepeatGraphicsItem::RepeatGraphicsItem(Token *token, int tokenPos, QGraphicsWidg
_child = new QGraphicsWidget(this);
else
_child = childWidget;
+ _layout->addStretch();
_layout->addItem(_child);
+ _layout->addStretch();
_layout->setAlignment(_child, Qt::AlignTop | Qt::AlignHCenter);
}
@@ -48,7 +50,11 @@ void RepeatGraphicsItem::setWidget(QGraphicsWidget *widget)
_layout->removeItem(_child);
delete _child;
_child = widget;
+ for(int i = 0; i < _layout->count(); ++i)
+ _layout->removeAt(0);
+ _layout->addStretch();
_layout->addItem(_child);
+ _layout->addStretch();
_layout->setAlignment(_child, Qt::AlignBottom | Qt::AlignHCenter);
}
@@ -48,6 +48,7 @@ void ExpressionGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
QGraphicsItem *item = itemList.at(i);
QPointF itemPos = item->scenePos();
QRectF bounds = item->boundingRect();
+ qDebug() << bounds;
QColor dropZoneColour(210, 210, 255, 80);
QBrush brush(dropZoneColour);

0 comments on commit ab1a88b

Please sign in to comment.