Skip to content

Commit

Permalink
Align items to the center by adding in stretch factors at loose ends
Browse files Browse the repository at this point in the history
  • Loading branch information
aelliott committed Feb 16, 2012
1 parent 76a356c commit ab1a88b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions GraphicsItems/alternationgraphicsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down
5 changes: 4 additions & 1 deletion GraphicsItems/expressiongraphicsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -110,6 +112,7 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
_incremented = true;
}
}
alternation->linearLayout()->addStretch();
alternation->setEndPos(_pos-1);
_lastItem = alternation;
}
Expand Down Expand Up @@ -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()));

Expand Down
8 changes: 7 additions & 1 deletion GraphicsItems/repeatgraphicsitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions expressiongraphicsscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ab1a88b

Please sign in to comment.