• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

marble

  • sources
  • kde-4.14
  • kdeedu
  • marble
  • src
  • lib
  • marble
  • graphicsview
LabelGraphicsItem.cpp
Go to the documentation of this file.
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 <bastianholst@gmx.de>
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 *parent )
24  : m_text(),
25  m_parent( parent )
26 {
27 }
28 
29 QFont LabelGraphicsItemPrivate::font()
30 {
31  return QApplication::font();
32 }
33 
34 void LabelGraphicsItem::setContentSize( const QSizeF &contentSize )
35 {
36  QSizeF updatedSize = contentSize;
37  if ( updatedSize.isEmpty() ) {
38  updatedSize.setHeight( 0 );
39  updatedSize.setWidth( 0 );
40  }
41  else {
42  if ( d->m_minimumSize.width() > updatedSize.width() ) {
43  updatedSize.setWidth( d->m_minimumSize.width() );
44  }
45  if ( d->m_minimumSize.height() > updatedSize.height() ) {
46  updatedSize.setHeight( d->m_minimumSize.height() );
47  }
48  }
49 
50  FrameGraphicsItem::setContentSize( updatedSize );
51 }
52 
53 // ----------------------------------------------------------------
54 
55 LabelGraphicsItem::LabelGraphicsItem( MarbleGraphicsItem *parent )
56  : FrameGraphicsItem( parent ),
57  d( new LabelGraphicsItemPrivate( this ) )
58 {
59 }
60 
61 LabelGraphicsItem::~LabelGraphicsItem()
62 {
63  delete d;
64 }
65 
66 QString LabelGraphicsItem::text() const
67 {
68  return d->m_text;
69 }
70 
71 void LabelGraphicsItem::setText( const QString& text )
72 {
73  clear();
74  d->m_text = text;
75  QFontMetrics metrics( d->font() );
76  QSizeF size = metrics.boundingRect( text ).size() + QSizeF( 14, 2 );
77  setContentSize( size );
78 }
79 
80 QImage LabelGraphicsItem::image() const
81 {
82  return d->m_image;
83 }
84 
85 void LabelGraphicsItem::setImage( const QImage& image, const QSize& size )
86 {
87  clear();
88  d->m_image = image;
89  if ( size.isEmpty() ) {
90  setContentSize( image.size() );
91  }
92  else {
93  setContentSize( size );
94  }
95 }
96 
97 QIcon LabelGraphicsItem::icon() const
98 {
99  return d->m_icon;
100 }
101 
102 void LabelGraphicsItem::setIcon( const QIcon& icon, const QSize& size )
103 {
104  clear();
105  d->m_icon = icon;
106  setContentSize( size );
107 }
108 
109 QSizeF LabelGraphicsItem::minimumSize() const
110 {
111  return d->m_minimumSize;
112 }
113 
114 void LabelGraphicsItem::setMinimumSize( const QSizeF& size )
115 {
116  const QSizeF oldContentSize = contentSize();
117  d->m_minimumSize = size;
118  setContentSize( oldContentSize );
119 }
120 
121 void LabelGraphicsItem::clear()
122 {
123  d->m_text.clear();
124  d->m_image = QImage();
125  d->m_icon = QIcon();
126  setContentSize( QSizeF( 0.0, 0.0 ) );
127 }
128 
129 void LabelGraphicsItem::paintContent( QPainter *painter )
130 {
131  painter->save();
132 
133  if ( !d->m_text.isNull() ) {
134  painter->setFont( d->font() );
135  painter->setPen( QColor( Qt::black ) );
136  painter->drawText( QRect( QPoint( 0, 0 ), contentSize().toSize() ),
137  Qt::AlignVCenter | Qt::AlignLeft,
138  d->m_text );
139  }
140  else if ( !d->m_image.isNull() ) {
141  painter->drawImage( QRectF( QPointF( 0, 0 ), contentSize() ),
142  d->m_image );
143  }
144  else if ( !d->m_icon.isNull() ) {
145  d->m_icon.paint( painter,
146  QRect( QPoint( 0, 0 ), contentSize().toSize() ),
147  Qt::AlignCenter );
148  }
149 
150  painter->restore();
151 }
Marble::FrameGraphicsItem
Definition: FrameGraphicsItem.h:25
LabelGraphicsItem.h
Marble::LabelGraphicsItem::setIcon
void setIcon(const QIcon &icon, const QSize &size)
Definition: LabelGraphicsItem.cpp:102
Marble::LabelGraphicsItem::text
QString text() const
Definition: LabelGraphicsItem.cpp:66
QSize::isEmpty
bool isEmpty() const
QFont
Marble::LabelGraphicsItem::minimumSize
QSizeF minimumSize() const
Definition: LabelGraphicsItem.cpp:109
Marble::LabelGraphicsItem::clear
void clear()
Definition: LabelGraphicsItem.cpp:121
Marble::FrameGraphicsItem::contentSize
QSizeF contentSize() const
Returns the size of the content of the MarbleGraphicsItem.
Definition: FrameGraphicsItem.cpp:180
QPainter::save
void save()
Marble::LabelGraphicsItem::setImage
void setImage(const QImage &image, const QSize &size=QSize())
Definition: LabelGraphicsItem.cpp:85
Marble::LabelGraphicsItemPrivate::m_image
QImage m_image
Definition: LabelGraphicsItem_p.h:35
Marble::LabelGraphicsItemPrivate::m_minimumSize
QSizeF m_minimumSize
Definition: LabelGraphicsItem_p.h:38
Marble::MarbleGraphicsItem::size
QSizeF size() const
Returns the size of the item.
Definition: MarbleGraphicsItem.cpp:136
QPoint
QFontMetrics
QImage::isNull
bool isNull() const
Marble::LabelGraphicsItem::~LabelGraphicsItem
~LabelGraphicsItem()
Definition: LabelGraphicsItem.cpp:61
Marble::LabelGraphicsItemPrivate
Definition: LabelGraphicsItem_p.h:26
Marble::LabelGraphicsItemPrivate::m_icon
QIcon m_icon
Definition: LabelGraphicsItem_p.h:36
QString::isNull
bool isNull() const
QPointF
Marble::LabelGraphicsItem::setMinimumSize
void setMinimumSize(const QSizeF &size)
Definition: LabelGraphicsItem.cpp:114
QString::clear
void clear()
QRect
QPainter::setFont
void setFont(const QFont &font)
QApplication::font
QFont font()
QSizeF::isEmpty
bool isEmpty() const
Marble::LabelGraphicsItem::setText
void setText(const QString &text)
Definition: LabelGraphicsItem.cpp:71
QPainter::setPen
void setPen(const QColor &color)
QSizeF::setWidth
void setWidth(qreal width)
QPainter
Marble::MarbleGraphicsItem
Definition: MarbleGraphicsItem.h:34
QPainter::drawText
void drawText(const QPointF &position, const QString &text)
QString
QColor
QIcon::paint
void paint(QPainter *painter, const QRect &rect, QFlags< Qt::AlignmentFlag > alignment, Mode mode, State state) const
QSize
QImage
QPainter::restore
void restore()
Marble::LabelGraphicsItem::paintContent
void paintContent(QPainter *painter)
Here the items paint their content.
Definition: LabelGraphicsItem.cpp:129
QPainter::drawImage
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
QSizeF
QRectF
QIcon::isNull
bool isNull() const
Marble::LabelGraphicsItem::icon
QIcon icon() const
Definition: LabelGraphicsItem.cpp:97
Marble::LabelGraphicsItem
A label item provides an Item that displays text or images/pixmaps.
Definition: LabelGraphicsItem.h:31
QImage::size
QSize size() const
Marble::LabelGraphicsItem::image
QImage image() const
Definition: LabelGraphicsItem.cpp:80
Marble::LabelGraphicsItemPrivate::font
static QFont font()
Definition: LabelGraphicsItem.cpp:29
Marble::LabelGraphicsItem::LabelGraphicsItem
LabelGraphicsItem(MarbleGraphicsItem *parent=0)
Definition: LabelGraphicsItem.cpp:55
Marble::LabelGraphicsItemPrivate::m_text
QString m_text
Definition: LabelGraphicsItem_p.h:34
QSizeF::height
qreal height() const
Marble::FrameGraphicsItem::setContentSize
void setContentSize(const QSizeF &size)
Sets the size of the content of the item.
Definition: FrameGraphicsItem.cpp:200
LabelGraphicsItem_p.h
QSizeF::setHeight
void setHeight(qreal height)
QSizeF::width
qreal width() const
QIcon
Marble::LabelGraphicsItem::setContentSize
void setContentSize(const QSizeF &contentSize)
Sets the size of the content of the item.
Definition: LabelGraphicsItem.cpp:34
Marble::LabelGraphicsItemPrivate::LabelGraphicsItemPrivate
LabelGraphicsItemPrivate(LabelGraphicsItem *parent)
Definition: LabelGraphicsItem.cpp:23
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:13:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

marble

Skip menu "marble"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal