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

marble

  • sources
  • kde-4.12
  • kdeedu
  • marble
  • src
  • lib
  • marble
VisiblePlacemark.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 2006-2007 Torsten Rahn <tackat@kde.org>
9 // Copyright 2007 Inge Wallin <ingwa@kde.org>
10 //
11 
12 #include "VisiblePlacemark.h"
13 
14 #include "MarbleDebug.h"
15 
16 #include "GeoDataStyle.h"
17 #include "PlacemarkLayer.h"
18 
19 #include <QApplication>
20 #include <QPainter>
21 #include <QPalette>
22 #include <QPixmap>
23 
24 using namespace Marble;
25 
26 VisiblePlacemark::VisiblePlacemark( const GeoDataPlacemark *placemark )
27  : m_placemark( placemark ),
28  m_selected( false )
29 {
30  drawLabelPixmap();
31 }
32 
33 const GeoDataPlacemark* VisiblePlacemark::placemark() const
34 {
35  return m_placemark;
36 }
37 
38 const QPixmap& VisiblePlacemark::symbolPixmap() const
39 {
40  const GeoDataStyle* style = m_placemark->style();
41  if ( style ) {
42  m_symbolPixmap = QPixmap::fromImage( style->iconStyle().icon() );
43  } else {
44  mDebug() << "Style pointer null";
45  }
46  return m_symbolPixmap;
47 }
48 
49 bool VisiblePlacemark::selected() const
50 {
51  return m_selected;
52 }
53 
54 void VisiblePlacemark::setSelected( bool selected )
55 {
56  m_selected = selected;
57  drawLabelPixmap();
58 }
59 
60 const QPoint& VisiblePlacemark::symbolPosition() const
61 {
62  return m_symbolPosition;
63 }
64 
65 const QPointF VisiblePlacemark::hotSpot() const
66 {
67  const QSize iconSize = m_placemark->style()->iconStyle().icon().size();
68 
69  GeoDataHotSpot::Units xunits;
70  GeoDataHotSpot::Units yunits;
71  QPointF pixelHotSpot = m_placemark->style()->iconStyle().hotSpot( xunits, yunits );
72 
73  switch ( xunits ) {
74  case GeoDataHotSpot::Fraction:
75  pixelHotSpot.setX( iconSize.width() * pixelHotSpot.x() );
76  break;
77  case GeoDataHotSpot::Pixels:
78  /* nothing to do */
79  break;
80  case GeoDataHotSpot::InsetPixels:
81  pixelHotSpot.setX( iconSize.width() - pixelHotSpot.x() );
82  break;
83  }
84 
85  switch ( yunits ) {
86  case GeoDataHotSpot::Fraction:
87  pixelHotSpot.setY( iconSize.height() * ( 1.0 - pixelHotSpot.y() ) );
88  break;
89  case GeoDataHotSpot::Pixels:
90  /* nothing to do */
91  break;
92  case GeoDataHotSpot::InsetPixels:
93  pixelHotSpot.setY( iconSize.height() - pixelHotSpot.y() );
94  break;
95  }
96 
97  return pixelHotSpot;
98 }
99 
100 void VisiblePlacemark::setSymbolPosition( const QPoint& position )
101 {
102  m_symbolPosition = position;
103 }
104 
105 const QPixmap& VisiblePlacemark::labelPixmap() const
106 {
107  return m_labelPixmap;
108 }
109 
110 const QRectF& VisiblePlacemark::labelRect() const
111 {
112  return m_labelRect;
113 }
114 
115 void VisiblePlacemark::setLabelRect( const QRectF& labelRect )
116 {
117  m_labelRect = labelRect;
118 }
119 
120 void VisiblePlacemark::drawLabelPixmap()
121 {
122  const GeoDataStyle* style = m_placemark->style();
123 
124  QString labelName = m_placemark->name();
125  if ( labelName.isEmpty() ) {
126  m_labelPixmap = QPixmap();
127  return;
128  }
129 
130  QFont labelFont = style->labelStyle().font();
131  QColor labelColor = style->labelStyle().color();
132 
133  LabelStyle labelStyle = Normal;
134  if ( m_selected ) {
135  labelStyle = Selected;
136  } else if ( style->labelStyle().glow() ) {
137  labelStyle = Glow;
138  }
139 
140  int textHeight = QFontMetrics( labelFont ).height();
141 
142  int textWidth;
143  if ( style->labelStyle().glow() ) {
144  labelFont.setWeight( 75 ); // Needed to calculate the correct pixmap size;
145  textWidth = ( QFontMetrics( labelFont ).width( labelName )
146  + qRound( 2 * s_labelOutlineWidth ) );
147  } else {
148  textWidth = ( QFontMetrics( labelFont ).width( labelName ) );
149  }
150 
151 
152  // Due to some XOrg bug this requires a workaround via
153  // QImage in some cases (at least with Qt 4.2).
154  if ( !PlacemarkLayer::m_useXWorkaround ) {
155  m_labelPixmap = QPixmap( QSize( textWidth, textHeight ) );
156  m_labelPixmap.fill( Qt::transparent );
157 
158  QPainter labelPainter( &m_labelPixmap );
159 
160  drawLabelText( labelPainter, labelName, labelFont, labelStyle, labelColor );
161  } else {
162  QImage image( QSize( textWidth, textHeight ),
163  QImage::Format_ARGB32_Premultiplied );
164  image.fill( 0 );
165 
166  QPainter labelPainter( &image );
167 
168  drawLabelText( labelPainter, labelName, labelFont, labelStyle, labelColor );
169 
170  labelPainter.end();
171 
172  m_labelPixmap = QPixmap::fromImage( image );
173  }
174 }
175 
176 void VisiblePlacemark::drawLabelText(QPainter &labelPainter, const QString &text,
177  const QFont &labelFont, LabelStyle labelStyle, const QColor &color )
178 {
179  QFont font = labelFont;
180  QFontMetrics metrics = QFontMetrics( font );
181  int fontAscent = metrics.ascent();
182 
183  switch ( labelStyle ) {
184  case Selected: {
185  labelPainter.setPen( color );
186  labelPainter.setFont( font );
187  QRect textRect( 0, 0, metrics.width( text ), metrics.height() );
188  labelPainter.fillRect( textRect, QApplication::palette().highlight() );
189  labelPainter.setPen( QPen( QApplication::palette().highlightedText(), 1 ) );
190  labelPainter.drawText( 0, fontAscent, text );
191  break;
192  }
193  case Glow: {
194  font.setWeight( 75 );
195  fontAscent = QFontMetrics( font ).ascent();
196 
197  QPen outlinepen( color == QColor( Qt::white ) ? Qt::black : Qt::white );
198  outlinepen.setWidthF( s_labelOutlineWidth );
199  QBrush outlinebrush( color );
200 
201  QPainterPath outlinepath;
202 
203  const QPointF baseline( s_labelOutlineWidth / 2.0, fontAscent );
204  outlinepath.addText( baseline, font, text );
205  labelPainter.setRenderHint( QPainter::Antialiasing, true );
206  labelPainter.setPen( outlinepen );
207  labelPainter.setBrush( outlinebrush );
208  labelPainter.drawPath( outlinepath );
209  labelPainter.setPen( Qt::NoPen );
210  labelPainter.drawPath( outlinepath );
211  labelPainter.setRenderHint( QPainter::Antialiasing, false );
212  break;
213  }
214  default: {
215  labelPainter.setPen( color );
216  labelPainter.setFont( font );
217  labelPainter.drawText( 0, fontAscent, text );
218  }
219  }
220 }
221 
QPainter
Marble::VisiblePlacemark::setSelected
void setSelected(bool selected)
Sets the state of the place mark.
Definition: VisiblePlacemark.cpp:54
Marble::GeoDataHotSpot::Pixels
Definition: GeoDataHotSpot.h:30
Marble::VisiblePlacemark::symbolPixmap
const QPixmap & symbolPixmap() const
Returns the pixmap of the place mark symbol.
Definition: VisiblePlacemark.cpp:38
Marble::VisiblePlacemark::labelPixmap
const QPixmap & labelPixmap() const
Returns the pixmap of the place mark name label.
Definition: VisiblePlacemark.cpp:105
Marble::VisiblePlacemark::Glow
Definition: VisiblePlacemark.h:96
GeoDataStyle.h
Marble::VisiblePlacemark::LabelStyle
LabelStyle
Definition: VisiblePlacemark.h:94
PlacemarkLayer.h
Marble::VisiblePlacemark::symbolPosition
const QPoint & symbolPosition() const
Returns the position of the place mark symbol on the map.
Definition: VisiblePlacemark.cpp:60
Marble::GeoDataFeature::style
const GeoDataStyle * style() const
Return the style assigned to the placemark.
Definition: GeoDataFeature.cpp:624
Marble::GeoDataHotSpot::InsetPixels
Definition: GeoDataHotSpot.h:30
Marble::VisiblePlacemark::labelRect
const QRectF & labelRect() const
Returns the area covered by the place mark name label on the map.
Definition: VisiblePlacemark.cpp:110
Marble::GeoDataHotSpot::Units
Units
Definition: GeoDataHotSpot.h:30
MarbleDebug.h
Marble::GeoDataIconStyle::hotSpot
QPointF hotSpot(GeoDataHotSpot::Units &xunits, GeoDataHotSpot::Units &yunits) const
Definition: GeoDataIconStyle.cpp:118
Marble::s_labelOutlineWidth
static const qreal s_labelOutlineWidth
Definition: VisiblePlacemark.h:29
Marble::GeoDataStyle::labelStyle
GeoDataLabelStyle & labelStyle() const
Return the label style of this style.
Definition: GeoDataStyle.cpp:128
Marble::GeoDataStyle
an addressable style group
Definition: GeoDataStyle.h:55
Marble::GeoDataHotSpot::Fraction
Definition: GeoDataHotSpot.h:30
Marble::GeoDataStyle::iconStyle
GeoDataIconStyle & iconStyle() const
Return the icon style of this style.
Definition: GeoDataStyle.cpp:113
Marble::VisiblePlacemark::VisiblePlacemark
VisiblePlacemark(const GeoDataPlacemark *placemark)
Definition: VisiblePlacemark.cpp:26
Marble::GeoDataLabelStyle::glow
bool glow() const
Return true if the text of the label should glow, false otherwise.
Definition: GeoDataLabelStyle.cpp:125
VisiblePlacemark.h
Marble::VisiblePlacemark::selected
bool selected() const
Returns the state of the place mark.
Definition: VisiblePlacemark.cpp:49
Marble::GeoDataIconStyle::icon
QImage icon() const
Definition: GeoDataIconStyle.cpp:99
Marble::VisiblePlacemark::setLabelRect
void setLabelRect(const QRectF &area)
Sets the area covered by the place mark name label on the map.
Definition: VisiblePlacemark.cpp:115
Marble::VisiblePlacemark::Normal
Definition: VisiblePlacemark.h:95
Marble::GeoDataFeature::name
QString name() const
The name of the feature.
Definition: GeoDataFeature.cpp:480
Marble::GeoDataColorStyle::color
QColor color() const
Return the color component.
Definition: GeoDataColorStyle.cpp:87
Marble::VisiblePlacemark::placemark
const GeoDataPlacemark * placemark() const
Returns the index of the place mark model which is associated with this visible place mark...
Definition: VisiblePlacemark.cpp:33
Marble::VisiblePlacemark::setSymbolPosition
void setSymbolPosition(const QPoint &position)
Sets the position of the place mark symbol on the map.
Definition: VisiblePlacemark.cpp:100
Marble::PlacemarkLayer::m_useXWorkaround
static bool m_useXWorkaround
Definition: PlacemarkLayer.h:76
Marble::GeoDataLabelStyle::font
QFont font() const
Return the current font of the label.
Definition: GeoDataLabelStyle.cpp:120
iconSize
const QSize iconSize(16, 16)
Marble::GeoDataPlacemark
a class representing a point of interest on the map
Definition: GeoDataPlacemark.h:54
Marble::mDebug
QDebug mDebug()
a function to replace qDebug() in Marble library code
Definition: MarbleDebug.cpp:31
Marble::VisiblePlacemark::hotSpot
const QPointF hotSpot() const
Returns the top left corner of the place mark symbol's hot spot.
Definition: VisiblePlacemark.cpp:65
Marble::VisiblePlacemark::Selected
Definition: VisiblePlacemark.h:97
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:53 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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