summaryrefslogtreecommitdiff
path: root/src/plugins/texteditor/textmark.cpp
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@qt.io>2017-07-05 11:09:07 +0200
committerDavid Schulz <david.schulz@qt.io>2017-07-18 06:53:30 +0000
commit862dd4c6bffc7b9b4b0465f5edb7e673331b54a5 (patch)
tree531de4b572f760264fbc85b3bbf56357617a0272 /src/plugins/texteditor/textmark.cpp
parenta196170df9e1decf9fb5e755b9ae411ae4ef7b9e (diff)
downloadqt-creator-862dd4c6bffc7b9b4b0465f5edb7e673331b54a5.tar.gz
TextEditor: Simplify annotation rectangle computation
Change-Id: Ie04f545d24696d66645c367b2c25d643c3691908 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
Diffstat (limited to 'src/plugins/texteditor/textmark.cpp')
-rw-r--r--src/plugins/texteditor/textmark.cpp50
1 files changed, 30 insertions, 20 deletions
diff --git a/src/plugins/texteditor/textmark.cpp b/src/plugins/texteditor/textmark.cpp
index 15939a3f44..254999f1b3 100644
--- a/src/plugins/texteditor/textmark.cpp
+++ b/src/plugins/texteditor/textmark.cpp
@@ -123,42 +123,52 @@ void TextMark::paintIcon(QPainter *painter, const QRect &rect) const
m_icon.paint(painter, rect, Qt::AlignCenter);
}
-void TextMark::paintAnnotation(QPainter *painter,
- QRectF *annotationRect,
- const QFontMetrics &fm) const
+void TextMark::paintAnnotation(QPainter *painter, QRectF *annotationRect) const
{
QString text = lineAnnotation();
if (text.isEmpty())
return;
- const bool drawIcon = !m_icon.isNull();
- int textWidth = fm.width(text);
- constexpr qreal margin = 1;
- const qreal iconHeight = annotationRect->height() - 2 * margin;
- const qreal iconWidth = iconHeight * m_widthFactor + 2 * margin;
- qreal annotationWidth = (drawIcon ? textWidth + iconWidth : textWidth) + margin;
- if (annotationRect->left() + annotationWidth > annotationRect->right()) {
- textWidth = int(annotationRect->width() - (drawIcon ? iconWidth + margin : margin));
- text = fm.elidedText(text, Qt::ElideRight, textWidth);
- annotationWidth = annotationRect->width();
- }
+ const AnnotationRects &rects = annotationRects(*annotationRect, painter->fontMetrics());
+
const QColor markColor = m_hasColor ? Utils::creatorTheme()->color(m_color).toHsl()
: painter->pen().color();
const AnnotationColors &colors =
AnnotationColors::getAnnotationColors(markColor, painter->background().color());
painter->save();
- annotationRect->setWidth(annotationWidth);
painter->setPen(colors.rectColor);
painter->setBrush(colors.rectColor);
- painter->drawRect(*annotationRect);
+ painter->drawRect(rects.annotationRect);
painter->setPen(colors.textColor);
+ paintIcon(painter, rects.iconRect.toAlignedRect());
+ painter->drawText(rects.textRect, Qt::AlignLeft, rects.text);
+ painter->restore();
+ *annotationRect = rects.annotationRect;
+}
+
+TextMark::AnnotationRects TextMark::annotationRects(const QRectF &boundingRect,
+ const QFontMetrics &fm) const
+{
+ AnnotationRects rects;
+ rects.annotationRect = boundingRect;
+ rects.text = lineAnnotation();
+ const bool drawIcon = !m_icon.isNull();
+ constexpr qreal margin = 1;
+ rects.iconRect = QRectF(boundingRect.left() + margin, boundingRect.top() + margin, 0, 0);
if (drawIcon) {
- paintIcon(painter, annotationRect->adjusted(
- margin, margin, -(textWidth + 2 * margin), -margin).toAlignedRect());
+ rects.iconRect.setHeight(boundingRect.height() - 2 * margin);
+ rects.iconRect.setWidth(rects.iconRect.height() * m_widthFactor);
}
- painter->drawText(annotationRect->adjusted(iconWidth, 0, 0, 0), Qt::AlignLeft, text);
- painter->restore();
+ rects.textRect = QRectF(rects.iconRect.right() + margin, boundingRect.top(),
+ qreal(fm.width(rects.text)), boundingRect.height());
+ rects.annotationRect.setRight(rects.textRect.right() + margin);
+ if (rects.annotationRect.right() > boundingRect.right()) {
+ rects.textRect.setRight(boundingRect.right() - margin);
+ rects.text = fm.elidedText(rects.text, Qt::ElideRight, int(rects.textRect.width()));
+ rects.annotationRect.setRight(boundingRect.right());
+ }
+ return rects;
}
void TextMark::updateLineNumber(int lineNumber)