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 <QString>
12#include <QApplication>
13#include <QFont>
14#include <QPainter>
15
16using namespace Marble;
17
18LabelGraphicsItemPrivate::LabelGraphicsItemPrivate(LabelGraphicsItem *labelGraphicsItem,
19 MarbleGraphicsItem *parent)
20 : FrameGraphicsItemPrivate(labelGraphicsItem, parent)
21{
22}
23
24QFont LabelGraphicsItemPrivate::font()
25{
26 return QApplication::font();
27}
28
29void LabelGraphicsItem::setContentSize( const QSizeF &contentSize )
30{
32 QSizeF updatedSize = contentSize;
33 if ( updatedSize.isEmpty() ) {
34 updatedSize.setHeight( 0 );
35 updatedSize.setWidth( 0 );
36 }
37 else {
38 if ( d->m_minimumSize.width() > updatedSize.width() ) {
39 updatedSize.setWidth( d->m_minimumSize.width() );
40 }
41 if ( d->m_minimumSize.height() > updatedSize.height() ) {
42 updatedSize.setHeight( d->m_minimumSize.height() );
43 }
44 }
45
46 FrameGraphicsItem::setContentSize( updatedSize );
47}
48
49// ----------------------------------------------------------------
50
51LabelGraphicsItem::LabelGraphicsItem( MarbleGraphicsItem *parent )
52 : FrameGraphicsItem(new LabelGraphicsItemPrivate(this, parent))
53{
54}
55
56LabelGraphicsItem::~LabelGraphicsItem()
57{
58}
59
60QString LabelGraphicsItem::text() const
61{
63 return d->m_text;
64}
65
66void LabelGraphicsItem::setText( const QString& text )
67{
69 clear();
70 d->m_text = text;
71 QFontMetrics metrics( d->font() );
72 QSizeF size = metrics.boundingRect( text ).size() + QSizeF( 14, 2 );
73 setContentSize( size );
74}
75
76QImage LabelGraphicsItem::image() const
77{
79 return d->m_image;
80}
81
82void LabelGraphicsItem::setImage( const QImage& image, const QSize& size )
83{
85 clear();
86 d->m_image = image;
87 if ( size.isEmpty() ) {
88 setContentSize( image.size() );
89 }
90 else {
91 setContentSize( size );
92 }
93}
94
95QIcon LabelGraphicsItem::icon() const
96{
98 return d->m_icon;
99}
100
101void LabelGraphicsItem::setIcon( const QIcon& icon, const QSize& size )
102{
104 clear();
105 d->m_icon = icon;
106 setContentSize( size );
107}
108
109QSizeF LabelGraphicsItem::minimumSize() const
110{
111 Q_D(const LabelGraphicsItem);
112 return d->m_minimumSize;
113}
114
115void LabelGraphicsItem::setMinimumSize( const QSizeF& size )
116{
118 const QSizeF oldContentSize = contentSize();
119 d->m_minimumSize = size;
120 setContentSize( oldContentSize );
121}
122
123void LabelGraphicsItem::clear()
124{
126 d->m_text.clear();
127 d->m_image = QImage();
128 d->m_icon = QIcon();
129 setContentSize( QSizeF( 0.0, 0.0 ) );
130}
131
132void LabelGraphicsItem::paintContent( QPainter *painter )
133{
135 painter->save();
136
137 if ( !d->m_text.isNull() ) {
138 painter->setFont( d->font() );
139 painter->setPen( QColor( Qt::black ) );
140 painter->drawText( QRect( QPoint( 0, 0 ), contentSize().toSize() ),
142 d->m_text );
143 }
144 else if ( !d->m_image.isNull() ) {
145 painter->drawImage( QRectF( QPointF( 0, 0 ), contentSize() ),
146 d->m_image );
147 }
148 else if ( !d->m_icon.isNull() ) {
149 d->m_icon.paint( painter,
150 QRect( QPoint( 0, 0 ), contentSize().toSize() ),
152 }
153
154 painter->restore();
155}
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 Tue Mar 26 2024 11:18:17 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.