Marble

LabelGraphicsItem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
4//
5
6// Self
7#include "LabelGraphicsItem.h"
8#include "LabelGraphicsItem_p.h"
9
10// Qt
11#include <QApplication>
12#include <QFont>
13#include <QPainter>
14#include <QString>
15
16using namespace Marble;
17
18LabelGraphicsItemPrivate::LabelGraphicsItemPrivate(LabelGraphicsItem *labelGraphicsItem, MarbleGraphicsItem *parent)
19 : FrameGraphicsItemPrivate(labelGraphicsItem, parent)
20{
21}
22
23QFont LabelGraphicsItemPrivate::font()
24{
25 return QApplication::font();
26}
27
28void LabelGraphicsItem::setContentSize(const QSizeF &contentSize)
29{
31 QSizeF updatedSize = contentSize;
32 if (updatedSize.isEmpty()) {
33 updatedSize.setHeight(0);
34 updatedSize.setWidth(0);
35 } else {
36 if (d->m_minimumSize.width() > updatedSize.width()) {
37 updatedSize.setWidth(d->m_minimumSize.width());
38 }
39 if (d->m_minimumSize.height() > updatedSize.height()) {
40 updatedSize.setHeight(d->m_minimumSize.height());
41 }
42 }
43
44 FrameGraphicsItem::setContentSize(updatedSize);
45}
46
47// ----------------------------------------------------------------
48
49LabelGraphicsItem::LabelGraphicsItem(MarbleGraphicsItem *parent)
50 : FrameGraphicsItem(new LabelGraphicsItemPrivate(this, parent))
51{
52}
53
54LabelGraphicsItem::~LabelGraphicsItem() = default;
55
56QString LabelGraphicsItem::text() const
57{
59 return d->m_text;
60}
61
62void LabelGraphicsItem::setText(const QString &text)
63{
65 clear();
66 d->m_text = text;
67 QFontMetrics metrics(d->font());
68 QSizeF size = metrics.boundingRect(text).size() + QSizeF(14, 2);
69 setContentSize(size);
70}
71
72QImage LabelGraphicsItem::image() const
73{
75 return d->m_image;
76}
77
78void LabelGraphicsItem::setImage(const QImage &image, const QSize &size)
79{
81 clear();
82 d->m_image = image;
83 if (size.isEmpty()) {
84 setContentSize(image.size());
85 } else {
86 setContentSize(size);
87 }
88}
89
90QIcon LabelGraphicsItem::icon() const
91{
93 return d->m_icon;
94}
95
96void LabelGraphicsItem::setIcon(const QIcon &icon, const QSize &size)
97{
99 clear();
100 d->m_icon = icon;
101 setContentSize(size);
102}
103
104QSizeF LabelGraphicsItem::minimumSize() const
105{
106 Q_D(const LabelGraphicsItem);
107 return d->m_minimumSize;
108}
109
110void LabelGraphicsItem::setMinimumSize(const QSizeF &size)
111{
113 const QSizeF oldContentSize = contentSize();
114 d->m_minimumSize = size;
115 setContentSize(oldContentSize);
116}
117
118void LabelGraphicsItem::clear()
119{
121 d->m_text.clear();
122 d->m_image = QImage();
123 d->m_icon = QIcon();
124 setContentSize(QSizeF(0.0, 0.0));
125}
126
127void LabelGraphicsItem::paintContent(QPainter *painter)
128{
130 painter->save();
131
132 if (!d->m_text.isNull()) {
133 painter->setFont(d->font());
134 painter->setPen(QColor(Qt::black));
135 painter->drawText(QRect(QPoint(0, 0), contentSize().toSize()), Qt::AlignVCenter | Qt::AlignLeft, d->m_text);
136 } else if (!d->m_image.isNull()) {
137 painter->drawImage(QRectF(QPointF(0, 0), contentSize()), d->m_image);
138 } else if (!d->m_icon.isNull()) {
139 d->m_icon.paint(painter, QRect(QPoint(0, 0), contentSize().toSize()), Qt::AlignCenter);
140 }
141
142 painter->restore();
143}
A label item provides an Item that displays text or images/pixmaps.
Binds a QML item to a specific geodetic location in screen coordinates.
QFont font()
QSize size() const const
void drawImage(const QPoint &point, const QImage &image)
void drawText(const QPoint &position, const QString &text)
void restore()
void save()
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
bool isEmpty() const const
qreal height() const const
bool isEmpty() const const
void setHeight(qreal height)
void setWidth(qreal width)
qreal width() const const
AlignVCenter
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.