summaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/qcp_string_legend_item.cpp
blob: 62596895614d6b59dc5663690ca22e53246dde6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/** @file
 *
 * QCustomPlot QCPAbstractLegendItem subclass containing a string.
 * This is used to add a title to a QCPLegend.
 *
 * This file is from https://www.qcustomplot.com/index.php/support/forum/443
 * where the author David said "I thought I would share in case any one else
 * is needing the same functionality." Accordingly, this file is in the
 * public domain.
 */

#include <ui/qt/widgets/qcp_string_legend_item.h>

QCPStringLegendItem::QCPStringLegendItem(QCPLegend *pParent, const QString& strText)
    : QCPAbstractLegendItem(pParent)
    , m_strText(strText)
{
}

QString QCPStringLegendItem::text() const
{
    return m_strText;
}

void QCPStringLegendItem::setText(const QString& strText)
{
    m_strText = strText;
}

void QCPStringLegendItem::draw(QCPPainter *pPainter)
{
    pPainter->setFont(mFont);
    pPainter->setPen(QPen(mTextColor));
    QRectF textRect = pPainter->fontMetrics().boundingRect(0, 0, 0, 0, Qt::TextDontClip, m_strText);
    pPainter->drawText(mRect.x() + mMargins.left(), mRect.y(), textRect.width(), textRect.height(), Qt::TextDontClip | Qt::AlignHCenter, m_strText);
}

QSize QCPStringLegendItem::minimumOuterSizeHint() const
{
    QSize cSize(0, 0);
    QFontMetrics fontMetrics(mFont);
    QRect textRect = fontMetrics.boundingRect(0, 0, 0, 0, Qt::TextDontClip, m_strText);
    cSize.setWidth(textRect.width() + mMargins.left() + mMargins.right());
    cSize.setHeight(textRect.height() + mMargins.top() + mMargins.bottom());
    return cSize;
}