Skip to content
Browse files

Switch to using painter->drawText() in some more graphics items, define

drop zones in all directions of each element
  • Loading branch information...
1 parent 5b55b40 commit 4288ed68f72863b02882b8332bb73ea9dbc180aa @aelliott committed Feb 16, 2012
View
27 GraphicsItems/alternationgraphicsitem.cpp
@@ -29,11 +29,8 @@ AlternationGraphicsItem::AlternationGraphicsItem(QGraphicsLinearLayout *initialL
double horizontalPadding = settings.value("Visualisation/Alternation/HorizontalPadding", 8.0).toDouble();
double verticalPadding = settings.value("Visualisation/Alternation/VerticalPadding", 6.0).toDouble();
- _title = new QGraphicsTextItem("Alternation", this);
- _title->setPos(horizontalPadding, verticalPadding);
-
_mainLayout = new QGraphicsLinearLayout(Qt::Vertical);
- _mainLayout->setContentsMargins(horizontalPadding, 2*verticalPadding + _title->boundingRect().height(), horizontalPadding, verticalPadding);
+ _mainLayout->setContentsMargins(horizontalPadding, 2*verticalPadding + _metrics.height(), horizontalPadding, verticalPadding);
_mainLayout->setSpacing(itemSpacing);
setLayout(_mainLayout);
@@ -62,7 +59,7 @@ QRectF AlternationGraphicsItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/Alternation/HorizontalPadding", 8.0).toDouble();
QSizeF childrenRect = _mainLayout->sizeHint(Qt::PreferredSize);
- return QRectF(0, 0, qMax(childrenRect.width(), 2*horizontalPadding + _title->boundingRect().width()), childrenRect.height());
+ return QRectF(0, 0, qMax(childrenRect.width(), 2*horizontalPadding + _metrics.width(tr("Alternation"))), childrenRect.height());
}
void AlternationGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -71,12 +68,14 @@ void AlternationGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphic
Q_UNUSED(widget)
QSettings settings;
- double itemSpacing = settings.value("Visualisation/VerticalSpacing", 12.0).toDouble();
+ _font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
+ double itemSpacing = settings.value("Visualisation/VerticalSpacing", 12.0).toDouble();
double horizontalPadding = settings.value("Visualisation/Alternation/HorizontalPadding", 8.0).toDouble();
- double verticalPadding = settings.value("Visualisation/Alternation/VerticalPadding", 6.0).toDouble();
- double cornerRadius = settings.value("Visualisation/Alternation/CornerRadius", 5.0).toDouble();
- QColor bgColor = settings.value("Visualisation/Alternation/Color", QColor(255,255,225)).value<QColor>();
+ double verticalPadding = settings.value("Visualisation/Alternation/VerticalPadding", 6.0).toDouble();
+ double cornerRadius = settings.value("Visualisation/Alternation/CornerRadius", 5.0).toDouble();
+ QColor bgColor = settings.value("Visualisation/Alternation/Color", QColor(255,255,225)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -94,7 +93,15 @@ void AlternationGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphic
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- double offset = 2*verticalPadding + _title->boundingRect().height();
+ painter->drawText(QRectF(
+ horizontalPadding,
+ verticalPadding,
+ drawRect.width()-2*horizontalPadding,
+ _metrics.height()),
+ Qt::AlignCenter,
+ tr("Alternation"));
+
+ double offset = 2*verticalPadding + _metrics.height();
for(int i = 0; (i+1) < _mainLayout->count(); ++i)
{
if(i > 0)
View
36 GraphicsItems/anchorgraphicsitem.cpp
@@ -24,21 +24,17 @@
AnchorGraphicsItem::AnchorGraphicsItem(Token *token, int tokenPos, QGraphicsItem *parent)
: RegexGraphicsItem(token, tokenPos, parent)
{
- _text = new QGraphicsTextItem(this);
-
switch(token->type())
{
case T_STARTING_POSITION:
- _text->setHtml("<center>Starting<br>Position");
+ _text = tr("Starting\nPosition");
break;
case T_ENDING_POSITION:
- _text->setHtml("<center>Ending<br>Position");
+ _text = tr("Ending\nPosition");
break;
default:
- _text->setHtml("Unhandled token");
+ _text = tr("Unhandled\ntoken");
}
-
- _text->setTextWidth(_text->boundingRect().width());
}
QRectF AnchorGraphicsItem::boundingRect() const
@@ -47,9 +43,14 @@ QRectF AnchorGraphicsItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/Anchor/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Anchor/VerticalPadding", 5.0).toDouble();
- QRectF textRect = _text->boundingRect();
+ int lines = _text.count("\n")+1;
+ double textWidth = 0.0;
+ QStringList words = _text.split("\n");
+ for(int i = 0; i < words.size(); ++i)
+ if(_metrics.width(words.at(i)) > textWidth)
+ textWidth = _metrics.width(words.at(i));
- return QRectF(0, 0, textRect.width() + 2*horizontalPadding, textRect.height() + 2*verticalPadding);
+ return QRectF(0, 0, textWidth + 2*horizontalPadding, lines*_metrics.height() + 2*verticalPadding);
}
void AnchorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -58,11 +59,13 @@ void AnchorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
Q_UNUSED(widget)
QSettings settings;
+ _font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
double horizontalPadding = settings.value("Visualisation/Anchor/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Anchor/VerticalPadding", 5.0).toDouble();
double cornerRadius = settings.value("Visualisation/Anchor/CornerRadius", 5.0).toDouble();
QColor bgColor = settings.value("Visualisation/Anchor/Color", QColor(220,255,255)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -80,7 +83,20 @@ void AnchorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- _text->setPos(horizontalPadding, verticalPadding);
+ int lines = _text.count("\n")+1;
+ double textWidth = 0.0;
+ QStringList words = _text.split("\n");
+ for(int i = 0; i < words.size(); ++i)
+ if(_metrics.width(words.at(i)) > textWidth)
+ textWidth = _metrics.width(words.at(i));
+
+ painter->drawText(QRectF(
+ horizontalPadding,
+ verticalPadding,
+ textWidth,
+ lines*_metrics.height()),
+ Qt::AlignCenter | Qt::TextWordWrap,
+ _text);
}
QSizeF AnchorGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
View
2 GraphicsItems/anchorgraphicsitem.hpp
@@ -38,7 +38,7 @@ class AnchorGraphicsItem : public RegexGraphicsItem
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
private:
- QGraphicsTextItem *_text;
+ QString _text;
};
#endif // ANCHORITEM_HPP
View
60 GraphicsItems/bracketexpressiongraphicsitem.cpp
@@ -25,7 +25,6 @@ BracketExpressionGraphicsItem::BracketExpressionGraphicsItem(Token *token, int t
: RegexGraphicsItem(token, tokenPos, parent)
{
_negated = (token->type() == T_NEGATED_BRACKET_EXPRESSION_OPEN);
- _text = new QGraphicsTextItem(this);
}
void BracketExpressionGraphicsItem::addToken(Token *token)
@@ -88,7 +87,7 @@ void BracketExpressionGraphicsItem::readTokens()
if(characters.length() > 0)
{
- QString chars = "Any one of: ";
+ QString chars = "Characters: ";
bool first = true;
for(stringIter = characters.begin(); stringIter != characters.end(); ++stringIter)
{
@@ -103,18 +102,23 @@ void BracketExpressionGraphicsItem::readTokens()
items.push_back(chars);
}
- QString text;
if(_negated)
- text = "Any Character Except:";
+ _heading = "Any Character Except:";
else
- text = "Any One Of:";
+ _heading = "Any One Of:";
+ _text = "";
+ bool first = true;
for(stringIter = items.begin(); stringIter != items.end(); ++stringIter)
{
- text += QString("<br> - ") + (*stringIter);
+ if(first)
+ {
+ _text = QString(" - ") + (*stringIter);
+ first = false;
+ }
+ else
+ _text += QString("\n - ") + (*stringIter);
}
-
- _text->setHtml(text);
}
QRectF BracketExpressionGraphicsItem::boundingRect() const
@@ -123,9 +127,17 @@ QRectF BracketExpressionGraphicsItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/BracketExpression/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/BracketExpression/VerticalPadding", 5.0).toDouble();
- QRectF textRect = _text->boundingRect();
-
- return QRectF(0, 0, textRect.width() + 2*horizontalPadding, textRect.height() + 2*verticalPadding);
+ // +2 in this case because of the extra heading in paint()
+ // This also means 3 sets of vertical padding
+ int lines = _text.count("\n")+2;
+ double textWidth = 0.0;
+ QStringList words = _text.split("\n");
+ for(int i = 0; i < words.size(); ++i)
+ if(_metrics.width(words.at(i)) > textWidth)
+ textWidth = _metrics.width(words.at(i));
+ textWidth = qMax(textWidth, static_cast<double>(_metrics.width(_heading)));
+
+ return QRectF(0, 0, textWidth + 2*horizontalPadding, lines*_metrics.height() + 3*verticalPadding);
}
void BracketExpressionGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -134,11 +146,13 @@ void BracketExpressionGraphicsItem::paint(QPainter *painter, const QStyleOptionG
Q_UNUSED(widget)
QSettings settings;
+ _font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
double horizontalPadding = settings.value("Visualisation/BracketExpression/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/BracketExpression/VerticalPadding", 5.0).toDouble();
double cornerRadius = settings.value("Visualisation/BracketExpression/CornerRadius", 5.0).toDouble();
QColor bgColor = settings.value("Visualisation/BracketExpression/Color", QColor(225,225,255)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -156,7 +170,29 @@ void BracketExpressionGraphicsItem::paint(QPainter *painter, const QStyleOptionG
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- _text->setPos(horizontalPadding, verticalPadding);
+ int lines = _text.count("\n")+1;
+ double textWidth = 0.0;
+ QStringList words = _text.split("\n");
+ for(int i = 0; i < words.size(); ++i)
+ if(_metrics.width(words.at(i)) > textWidth)
+ textWidth = _metrics.width(words.at(i));
+ textWidth = qMax(textWidth, static_cast<double>(_metrics.width(_heading)));
+
+ painter->drawText(QRectF(
+ horizontalPadding,
+ verticalPadding,
+ textWidth,
+ _metrics.height()),
+ Qt::AlignCenter,
+ _heading);
+
+ painter->drawText(QRectF(
+ horizontalPadding,
+ _metrics.height() + 2*verticalPadding,
+ textWidth,
+ lines*_metrics.height()),
+ Qt::AlignVCenter | Qt::AlignLeft | Qt::TextWordWrap,
+ _text);
}
QSizeF BracketExpressionGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
View
3 GraphicsItems/bracketexpressiongraphicsitem.hpp
@@ -43,7 +43,8 @@ class BracketExpressionGraphicsItem : public RegexGraphicsItem
private:
std::vector<Token *> _tokens;
- QGraphicsTextItem *_text;
+ QString _text;
+ QString _heading;
bool _negated;
};
View
58 expressiongraphicsscene.cpp
@@ -23,6 +23,7 @@
#include <QGraphicsSceneDragDropEvent>
#include <QMimeData>
+#include <QSettings>
#include <QDebug>
ExpressionGraphicsScene::ExpressionGraphicsScene(QObject *parent)
@@ -32,6 +33,9 @@ ExpressionGraphicsScene::ExpressionGraphicsScene(QObject *parent)
void ExpressionGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
+ QSettings settings;
+ double itemSpacing = settings.value("Visualisation/HorizontalSpacing", 8.0).toDouble();
+
while(_dropZoneLabels.size() > 0)
{
removeItem(_dropZoneLabels.at(0));
@@ -45,21 +49,49 @@ void ExpressionGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
QPointF itemPos = item->scenePos();
QRectF bounds = item->boundingRect();
- qDebug() << itemPos;
- qDebug() << bounds;
+ QColor dropZoneColour(210, 210, 255, 80);
+ QBrush brush(dropZoneColour);
+ QPen pen(dropZoneColour);
QPointF topLeft = itemPos;
- topLeft.setY(itemPos.y()-8.0);
-
- QPointF bottomRight = itemPos;
- bottomRight.setX(itemPos.x()+bounds.width());
-
- QGraphicsRectItem *dropZoneLabel = new QGraphicsRectItem(0, 0, bounds.width(), 8.0);
- dropZoneLabel->setPos(topLeft);
- addItem(dropZoneLabel);
- _dropZoneLabels.append(dropZoneLabel);
-
- qDebug() << dropZoneLabel->boundingRect();
+ topLeft.setY(itemPos.y()-itemSpacing);
+
+ QGraphicsRectItem *topLabel = new QGraphicsRectItem(0, 0, bounds.width(), itemSpacing);
+ topLabel->setPen(pen);
+ topLabel->setBrush(brush);
+ topLabel->setPos(topLeft);
+ addItem(topLabel);
+ _dropZoneLabels.append(topLabel);
+
+ topLeft = itemPos;
+ topLeft.setX(itemPos.x()-itemSpacing);
+
+ QGraphicsRectItem *leftLabel = new QGraphicsRectItem(0, 0, itemSpacing, bounds.height());
+ leftLabel->setPen(pen);
+ leftLabel->setBrush(brush);
+ leftLabel->setPos(topLeft);
+ addItem(leftLabel);
+ _dropZoneLabels.append(leftLabel);
+
+ topLeft = itemPos;
+ topLeft.setY(itemPos.y()+bounds.height());
+
+ QGraphicsRectItem *bottomLabel = new QGraphicsRectItem(0, 0, bounds.width(), itemSpacing);
+ bottomLabel->setPen(pen);
+ bottomLabel->setBrush(brush);
+ bottomLabel->setPos(topLeft);
+ addItem(bottomLabel);
+ _dropZoneLabels.append(bottomLabel);
+
+ topLeft = itemPos;
+ topLeft.setX(itemPos.x()+bounds.width());
+
+ QGraphicsRectItem *rightLabel = new QGraphicsRectItem(0, 0, itemSpacing, bounds.height());
+ rightLabel->setPen(pen);
+ rightLabel->setBrush(brush);
+ rightLabel->setPos(topLeft);
+ addItem(rightLabel);
+ _dropZoneLabels.append(rightLabel);
}
event->accept();

0 comments on commit 4288ed6

Please sign in to comment.
Something went wrong with that request. Please try again.