Marble

LabelGraphicsItem.cpp
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2009 Bastian Holst <[email protected]>
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 
16 using namespace Marble;
17 
18 LabelGraphicsItemPrivate::LabelGraphicsItemPrivate(LabelGraphicsItem *labelGraphicsItem,
19  MarbleGraphicsItem *parent)
20  : FrameGraphicsItemPrivate(labelGraphicsItem, parent)
21 {
22 }
23 
24 QFont LabelGraphicsItemPrivate::font()
25 {
26  return QApplication::font();
27 }
28 
29 void LabelGraphicsItem::setContentSize( const QSizeF &contentSize )
30 {
31  Q_D(LabelGraphicsItem);
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 
51 LabelGraphicsItem::LabelGraphicsItem( MarbleGraphicsItem *parent )
52  : FrameGraphicsItem(new LabelGraphicsItemPrivate(this, parent))
53 {
54 }
55 
56 LabelGraphicsItem::~LabelGraphicsItem()
57 {
58 }
59 
60 QString LabelGraphicsItem::text() const
61 {
62  Q_D(const LabelGraphicsItem);
63  return d->m_text;
64 }
65 
66 void LabelGraphicsItem::setText( const QString& text )
67 {
68  Q_D(LabelGraphicsItem);
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 
76 QImage LabelGraphicsItem::image() const
77 {
78  Q_D(const LabelGraphicsItem);
79  return d->m_image;
80 }
81 
82 void LabelGraphicsItem::setImage( const QImage& image, const QSize& size )
83 {
84  Q_D(LabelGraphicsItem);
85  clear();
86  d->m_image = image;
87  if ( size.isEmpty() ) {
88  setContentSize( image.size() );
89  }
90  else {
91  setContentSize( size );
92  }
93 }
94 
95 QIcon LabelGraphicsItem::icon() const
96 {
97  Q_D(const LabelGraphicsItem);
98  return d->m_icon;
99 }
100 
101 void LabelGraphicsItem::setIcon( const QIcon& icon, const QSize& size )
102 {
103  Q_D(LabelGraphicsItem);
104  clear();
105  d->m_icon = icon;
106  setContentSize( size );
107 }
108 
109 QSizeF LabelGraphicsItem::minimumSize() const
110 {
111  Q_D(const LabelGraphicsItem);
112  return d->m_minimumSize;
113 }
114 
115 void LabelGraphicsItem::setMinimumSize( const QSizeF& size )
116 {
117  Q_D(LabelGraphicsItem);
118  const QSizeF oldContentSize = contentSize();
119  d->m_minimumSize = size;
120  setContentSize( oldContentSize );
121 }
122 
123 void LabelGraphicsItem::clear()
124 {
125  Q_D(LabelGraphicsItem);
126  d->m_text.clear();
127  d->m_image = QImage();
128  d->m_icon = QIcon();
129  setContentSize( QSizeF( 0.0, 0.0 ) );
130 }
131 
132 void LabelGraphicsItem::paintContent( QPainter *painter )
133 {
134  Q_D(LabelGraphicsItem);
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() ),
151  Qt::AlignCenter );
152  }
153 
154  painter->restore();
155 }
bool isEmpty() const const
Binds a QML item to a specific geodetic location in screen coordinates.
void save()
AlignVCenter
void setFont(const QFont &font)
QFont font()
bool isEmpty() const const
void setPen(const QColor &color)
void setWidth(qreal width)
void drawText(const QPointF &position, const QString &text)
void restore()
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
A label item provides an Item that displays text or images/pixmaps.
QSize size() const const
qreal height() const const
void setHeight(qreal height)
qreal width() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 23:11:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.