Marble

LabelGraphicsItem.cpp
1 //
2 // This file is part of the Marble Virtual Globe.
3 //
4 // This program is free software licensed under the GNU LGPL. You can
5 // find a copy of this license in LICENSE.txt in the top directory of
6 // the source code.
7 //
8 // Copyright 2009 Bastian Holst <[email protected]>
9 //
10 
11 // Self
12 #include "LabelGraphicsItem.h"
13 #include "LabelGraphicsItem_p.h"
14 
15 // Qt
16 #include <QString>
17 #include <QApplication>
18 #include <QFont>
19 #include <QPainter>
20 
21 using namespace Marble;
22 
23 LabelGraphicsItemPrivate::LabelGraphicsItemPrivate(LabelGraphicsItem *labelGraphicsItem,
24  MarbleGraphicsItem *parent)
25  : FrameGraphicsItemPrivate(labelGraphicsItem, parent)
26 {
27 }
28 
29 QFont LabelGraphicsItemPrivate::font()
30 {
31  return QApplication::font();
32 }
33 
34 void LabelGraphicsItem::setContentSize( const QSizeF &contentSize )
35 {
36  Q_D(LabelGraphicsItem);
37  QSizeF updatedSize = contentSize;
38  if ( updatedSize.isEmpty() ) {
39  updatedSize.setHeight( 0 );
40  updatedSize.setWidth( 0 );
41  }
42  else {
43  if ( d->m_minimumSize.width() > updatedSize.width() ) {
44  updatedSize.setWidth( d->m_minimumSize.width() );
45  }
46  if ( d->m_minimumSize.height() > updatedSize.height() ) {
47  updatedSize.setHeight( d->m_minimumSize.height() );
48  }
49  }
50 
51  FrameGraphicsItem::setContentSize( updatedSize );
52 }
53 
54 // ----------------------------------------------------------------
55 
56 LabelGraphicsItem::LabelGraphicsItem( MarbleGraphicsItem *parent )
57  : FrameGraphicsItem(new LabelGraphicsItemPrivate(this, parent))
58 {
59 }
60 
61 LabelGraphicsItem::~LabelGraphicsItem()
62 {
63 }
64 
65 QString LabelGraphicsItem::text() const
66 {
67  Q_D(const LabelGraphicsItem);
68  return d->m_text;
69 }
70 
71 void LabelGraphicsItem::setText( const QString& text )
72 {
73  Q_D(LabelGraphicsItem);
74  clear();
75  d->m_text = text;
76  QFontMetrics metrics( d->font() );
77  QSizeF size = metrics.boundingRect( text ).size() + QSizeF( 14, 2 );
78  setContentSize( size );
79 }
80 
81 QImage LabelGraphicsItem::image() const
82 {
83  Q_D(const LabelGraphicsItem);
84  return d->m_image;
85 }
86 
87 void LabelGraphicsItem::setImage( const QImage& image, const QSize& size )
88 {
89  Q_D(LabelGraphicsItem);
90  clear();
91  d->m_image = image;
92  if ( size.isEmpty() ) {
93  setContentSize( image.size() );
94  }
95  else {
96  setContentSize( size );
97  }
98 }
99 
100 QIcon LabelGraphicsItem::icon() const
101 {
102  Q_D(const LabelGraphicsItem);
103  return d->m_icon;
104 }
105 
106 void LabelGraphicsItem::setIcon( const QIcon& icon, const QSize& size )
107 {
108  Q_D(LabelGraphicsItem);
109  clear();
110  d->m_icon = icon;
111  setContentSize( size );
112 }
113 
114 QSizeF LabelGraphicsItem::minimumSize() const
115 {
116  Q_D(const LabelGraphicsItem);
117  return d->m_minimumSize;
118 }
119 
120 void LabelGraphicsItem::setMinimumSize( const QSizeF& size )
121 {
122  Q_D(LabelGraphicsItem);
123  const QSizeF oldContentSize = contentSize();
124  d->m_minimumSize = size;
125  setContentSize( oldContentSize );
126 }
127 
128 void LabelGraphicsItem::clear()
129 {
130  Q_D(LabelGraphicsItem);
131  d->m_text.clear();
132  d->m_image = QImage();
133  d->m_icon = QIcon();
134  setContentSize( QSizeF( 0.0, 0.0 ) );
135 }
136 
137 void LabelGraphicsItem::paintContent( QPainter *painter )
138 {
139  Q_D(LabelGraphicsItem);
140  painter->save();
141 
142  if ( !d->m_text.isNull() ) {
143  painter->setFont( d->font() );
144  painter->setPen( QColor( Qt::black ) );
145  painter->drawText( QRect( QPoint( 0, 0 ), contentSize().toSize() ),
147  d->m_text );
148  }
149  else if ( !d->m_image.isNull() ) {
150  painter->drawImage( QRectF( QPointF( 0, 0 ), contentSize() ),
151  d->m_image );
152  }
153  else if ( !d->m_icon.isNull() ) {
154  d->m_icon.paint( painter,
155  QRect( QPoint( 0, 0 ), contentSize().toSize() ),
156  Qt::AlignCenter );
157  }
158 
159  painter->restore();
160 }
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-2020 The KDE developers.
Generated on Sun Oct 25 2020 23:24:19 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.