Permalink
Browse files

Finished making all graphics items use drawText and respect font

settings
  • Loading branch information...
aelliott committed Feb 16, 2012
1 parent 4288ed6 commit 76a356c1074e372ca9630ced86a1a16531e37060
@@ -49,6 +49,8 @@ QRectF AnchorGraphicsItem::boundingRect() const
for(int i = 0; i < words.size(); ++i)
if(_metrics.width(words.at(i)) > textWidth)
textWidth = _metrics.width(words.at(i));
+ // To prevent a rounding error causing a cut-off later, just add an extra pixel now
+ textWidth++;
return QRectF(0, 0, textWidth + 2*horizontalPadding, lines*_metrics.height() + 2*verticalPadding);
}
@@ -83,18 +85,11 @@ void AnchorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- 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()),
+ drawRect.width() - 2*horizontalPadding,
+ drawRect.height() - 2*verticalPadding),
Qt::AlignCenter | Qt::TextWordWrap,
_text);
}
@@ -135,7 +135,8 @@ QRectF BracketExpressionGraphicsItem::boundingRect() const
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)));
+ // To prevent a rounding error causing a cut-off later, just add an extra pixel now
+ textWidth = qMax(textWidth, static_cast<double>(_metrics.width(_heading))) + 1;
return QRectF(0, 0, textWidth + 2*horizontalPadding, lines*_metrics.height() + 3*verticalPadding);
}
@@ -171,25 +172,19 @@ void BracketExpressionGraphicsItem::paint(QPainter *painter, const QStyleOptionG
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
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,
+ drawRect.width() - 2*horizontalPadding,
_metrics.height()),
Qt::AlignCenter,
_heading);
painter->drawText(QRectF(
horizontalPadding,
_metrics.height() + 2*verticalPadding,
- textWidth,
+ drawRect.width() - 2*horizontalPadding,
lines*_metrics.height()),
Qt::AlignVCenter | Qt::AlignLeft | Qt::TextWordWrap,
_text);
@@ -24,31 +24,31 @@
CharacterClassItem::CharacterClassItem(Token *token, int tokenPos, QGraphicsItem *parent)
: RegexGraphicsItem(token, tokenPos, parent)
{
- _text = new QGraphicsTextItem(this);
+ _text = "";
switch(token->type())
{
case T_WORD:
- _text->setHtml("<center>Word<br>Character");
+ _text = tr("Word\nCharacter");
break;
case T_NOT_WORD:
- _text->setHtml("<center>Non-word<br>Character");
+ _text = tr("Non-word\nCharacter");
break;
case T_DIGIT:
- _text->setHtml("<center>Digit<br>Character");
+ _text = tr("Digit\nCharacter");
break;
case T_NOT_DIGIT:
- _text->setHtml("<center>Non-digit<br>Character");
+ _text = tr("Non-digit\nCharacter");
break;
case T_SPACE:
- _text->setHtml("<center>Space<br>Character");
+ _text = tr("Space\nCharacter");
break;
case T_NOT_SPACE:
- _text->setHtml("<center>Non-space<br>Character");
+ _text = tr("Non-space\nCharacter");
break;
+ default:
+ _text = tr("Unhandled\nToken");
}
-
- _text->setTextWidth(_text->boundingRect().width());
}
QRectF CharacterClassItem::boundingRect() const
@@ -57,9 +57,16 @@ QRectF CharacterClassItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/CharacterClass/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/CharacterClass/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));
+ // To prevent a rounding error causing a cut-off later, just add an extra pixel now
+ textWidth++;
- 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 CharacterClassItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -68,11 +75,13 @@ void CharacterClassItem::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/CharacterClass/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/CharacterClass/VerticalPadding", 5.0).toDouble();
double cornerRadius = settings.value("Visualisation/CharacterClass/CornerRadius", 5.0).toDouble();
QColor bgColor = settings.value("Visualisation/CharacterClass/Color", QColor(255,220,255)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -90,7 +99,13 @@ void CharacterClassItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- _text->setPos(horizontalPadding, verticalPadding);
+ painter->drawText(QRectF(
+ horizontalPadding,
+ verticalPadding,
+ drawRect.width() - 2*horizontalPadding,
+ drawRect.height() - 2*verticalPadding),
+ Qt::AlignCenter | Qt::TextWordWrap,
+ _text);
}
QSizeF CharacterClassItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
@@ -36,7 +36,7 @@ class CharacterClassItem : public RegexGraphicsItem
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
private:
- QGraphicsTextItem *_text;
+ QString _text;
};
#endif // CHARACTERCLASS_HPP
@@ -24,9 +24,6 @@
CommentGraphicsItem::CommentGraphicsItem(Token *token, int tokenPos, QGraphicsItem *parent)
: RegexGraphicsItem(token, tokenPos, parent)
{
- // (#...)
- QString text = token->value().mid(2, token->value().length()-3);
- _text = new QGraphicsTextItem(text, this);
}
QRectF CommentGraphicsItem::boundingRect() const
@@ -35,9 +32,7 @@ QRectF CommentGraphicsItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/Comment/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Comment/VerticalPadding", 5.0).toDouble();
- QRectF textRect = _text->boundingRect();
-
- return QRectF(0, 0, textRect.width() + 2*horizontalPadding, textRect.height() + 2*verticalPadding);
+ return QRectF(0, 0, _metrics.width(_text) + 2*horizontalPadding + 1, _metrics.height() + 2*verticalPadding);
}
void CommentGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -46,11 +41,13 @@ void CommentGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsIte
Q_UNUSED(widget)
QSettings settings;
+ _font = settings.value("Visualisation/Font", QFont("sans-serif", 10)).value<QFont>();
double horizontalPadding = settings.value("Visualisation/Comment/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Comment/VerticalPadding", 5.0).toDouble();
double cornerRadius = settings.value("Visualisation/Comment/CornerRadius", 5.0).toDouble();
QColor bgColor = settings.value("Visualisation/Comment/Color", QColor(225,225,225)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -68,7 +65,13 @@ void CommentGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsIte
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- _text->setPos(horizontalPadding, verticalPadding);
+ painter->drawText(QRectF(
+ drawRect.x() + horizontalPadding,
+ drawRect.y() + verticalPadding,
+ drawRect.width() - 2*horizontalPadding,
+ drawRect.height() - 2*verticalPadding),
+ Qt::AlignCenter,
+ _text);
}
QSizeF CommentGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
@@ -79,3 +82,8 @@ QSizeF CommentGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constrain
QRectF rect = boundingRect();
return QSizeF(rect.width(), rect.height());
}
+
+void CommentGraphicsItem::setText(QString text)
+{
+ _text = text;
+}
@@ -35,8 +35,10 @@ class CommentGraphicsItem : public RegexGraphicsItem
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
+ void setText(QString text);
+
private:
- QGraphicsTextItem *_text;
+ QString _text;
};
#endif // COMMENTGRAPHICSITEM_HPP
@@ -24,8 +24,7 @@
ErrorGraphicsItem::ErrorGraphicsItem(Token *token, int tokenPos, QGraphicsItem *parent)
: RegexGraphicsItem(token, tokenPos, parent)
{
- QString text = token->value();
- _text = new QGraphicsTextItem(text, this);
+ _text = token->value();
}
QRectF ErrorGraphicsItem::boundingRect() const
@@ -34,9 +33,7 @@ QRectF ErrorGraphicsItem::boundingRect() const
double horizontalPadding = settings.value("Visualisation/Error/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Error/VerticalPadding", 5.0).toDouble();
- QRectF textRect = _text->boundingRect();
-
- return QRectF(0, 0, textRect.width() + 2*horizontalPadding, textRect.height() + 2*verticalPadding);
+ return QRectF(0, 0, _metrics.width(_text) + 2*horizontalPadding + 1, _metrics.height() + 2*verticalPadding);
}
void ErrorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
@@ -45,11 +42,13 @@ void ErrorGraphicsItem::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/Error/HorizontalPadding", 6.0).toDouble();
double verticalPadding = settings.value("Visualisation/Error/VerticalPadding", 5.0).toDouble();
double cornerRadius = settings.value("Visualisation/Error/CornerRadius", 5.0).toDouble();
QColor bgColor = settings.value("Visualisation/Error/Color", QColor(255,220,220)).value<QColor>();
+ painter->setFont(_font);
painter->setBrush(bgColor);
painter->setPen(Qt::black);
@@ -67,7 +66,13 @@ void ErrorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem
}
painter->drawRoundedRect(drawRect, cornerRadius, cornerRadius);
- _text->setPos(horizontalPadding, verticalPadding);
+ painter->drawText(QRectF(
+ drawRect.x() + horizontalPadding,
+ drawRect.y() + verticalPadding,
+ drawRect.width() - 2*horizontalPadding,
+ drawRect.height() - 2*verticalPadding),
+ Qt::AlignCenter,
+ _text);
}
QSizeF ErrorGraphicsItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
@@ -36,7 +36,7 @@ class ErrorGraphicsItem : public RegexGraphicsItem
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint) const;
private:
- QGraphicsTextItem *_text;
+ QString _text;
};
#endif // ERRORGRAPHICSITEM_HPP
@@ -225,14 +225,16 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
_lastItem = repeat;
break;
case T_COMMENT_OPEN:
- newItem = new CommentGraphicsItem(*_iter, _pos);
+ {
+ CommentGraphicsItem *comment = new CommentGraphicsItem(*_iter, _pos);
++_iter;
++_pos;
_incremented = true;
if(_iter != _tokens.end() && (*_iter)->type() == T_LITERAL)
{
- currentLayout->addItem(newItem);
+ currentLayout->addItem(comment);
+ comment->setText((*_iter)->value());
++_iter;
++_pos;
if(_iter != _tokens.end() && (*_iter)->type() == T_COMMENT_CLOSE)
@@ -243,12 +245,13 @@ QGraphicsLinearLayout *ExpressionGraphicsItem::handleToken(Token token, QGraphic
}
else if(_iter != _tokens.end() && (*_iter)->type() == T_COMMENT_CLOSE)
{
- currentLayout->addItem(newItem);
+ currentLayout->addItem(comment);
++_iter;
++_pos;
}
- newItem->setEndPos(_pos-1);
- _lastItem = newItem;
+ comment->setEndPos(_pos-1);
+ _lastItem = comment;
+ }
break;
default:
newItem = new ErrorGraphicsItem(*_iter, _pos);
Oops, something went wrong.

0 comments on commit 76a356c

Please sign in to comment.