• 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
  • plugins
  • render
  • compass
CompassFloatItem.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 2008 Torsten Rahn <tackat@kde.org>
9 // Copyright 2010 Dennis Nienhüser <earthwings@gentoo.org>
10 //
11 
12 #include "CompassFloatItem.h"
13 #include "ui_CompassConfigWidget.h"
14 
15 #include "MarbleDebug.h"
16 #include "MarbleDirs.h"
17 #include "ViewportParams.h"
18 
19 #include <QRect>
20 #include <QColor>
21 #include <QPainter>
22 #include <QPixmap>
23 #include <QPushButton>
24 #include <QSvgRenderer>
25 
26 namespace Marble
27 {
28 
29 CompassFloatItem::CompassFloatItem()
30  : AbstractFloatItem( 0 ),
31  m_svgobj( 0 )
32 {
33 }
34 
35 CompassFloatItem::CompassFloatItem( const MarbleModel *marbleModel )
36  : AbstractFloatItem( marbleModel, QPointF( -1.0, 10.0 ), QSizeF( 75.0, 75.0 ) ),
37  m_isInitialized( false ),
38  m_svgobj( 0 ),
39  m_compass(),
40  m_polarity( 0 ),
41  m_themeIndex( 0 ),
42  m_configDialog( 0 ),
43  m_uiConfigWidget( 0 )
44 {
45 }
46 
47 CompassFloatItem::~CompassFloatItem ()
48 {
49  delete m_svgobj;
50 }
51 
52 QStringList CompassFloatItem::backendTypes() const
53 {
54  return QStringList( "compass" );
55 }
56 
57 QString CompassFloatItem::name() const
58 {
59  return tr( "Compass" );
60 }
61 
62 QString CompassFloatItem::guiString() const
63 {
64  return tr( "&Compass" );
65 }
66 
67 QString CompassFloatItem::nameId() const
68 {
69  return QString( "compass" );
70 }
71 
72 QString CompassFloatItem::version() const
73 {
74  return "1.0";
75 }
76 
77 QString CompassFloatItem::description() const
78 {
79  return tr( "This is a float item that provides a compass." );
80 }
81 
82 QString CompassFloatItem::copyrightYears() const
83 {
84  return "2009, 2010";
85 }
86 
87 QList<PluginAuthor> CompassFloatItem::pluginAuthors() const
88 {
89  return QList<PluginAuthor>()
90  << PluginAuthor( QString::fromUtf8( "Dennis Nienhüser" ), "earthwings@gentoo.org" )
91  << PluginAuthor( "Torsten Rahn", "tackat@kde.org" );
92 }
93 
94 QIcon CompassFloatItem::icon() const
95 {
96  return QIcon(":/icons/compass.png");
97 }
98 
99 void CompassFloatItem::initialize()
100 {
101  readSettings();
102  m_isInitialized = true;
103 }
104 
105 bool CompassFloatItem::isInitialized() const
106 {
107  return m_isInitialized;
108 }
109 
110 QPainterPath CompassFloatItem::backgroundShape() const
111 {
112  QRectF contentRect = this->contentRect();
113  QPainterPath path;
114  int fontheight = QFontMetrics( font() ).ascent();
115  int compassLength = static_cast<int>( contentRect.height() ) - 5 - fontheight;
116 
117  path.addEllipse( QRectF( QPointF( marginLeft() + padding() + ( contentRect.width() - compassLength ) / 2,
118  marginTop() + padding() + 5 + fontheight ),
119  QSize( compassLength, compassLength ) ).toRect() );
120  return path;
121 }
122 
123 void CompassFloatItem::changeViewport( ViewportParams *viewport )
124 {
125  // figure out the polarity ...
126  if ( m_polarity != viewport->polarity() ) {
127  m_polarity = viewport->polarity();
128  update();
129  }
130 }
131 
132 void CompassFloatItem::paintContent( QPainter *painter )
133 {
134  painter->save();
135 
136  QRectF compassRect( contentRect() );
137 
138  QString dirstr = tr( "N" );
139  if ( m_polarity == -1 )
140  dirstr = tr( "S" );
141  if ( m_polarity == 0 )
142  dirstr = "";
143 
144  int fontheight = QFontMetrics( font() ).ascent();
145  int fontwidth = QFontMetrics( font() ).boundingRect( dirstr ).width();
146 
147  QPen outlinepen( background().color() );
148  outlinepen.setWidth( 2 );
149  QBrush outlinebrush( pen().color() );
150 
151  QPainterPath outlinepath;
152  const QPointF baseline( 0.5 * (qreal)( compassRect.width() - fontwidth ),
153  (qreal)(fontheight) + 2.0 );
154 
155  outlinepath.addText( baseline, font(), dirstr );
156 
157  painter->setPen( outlinepen );
158  painter->setBrush( outlinebrush );
159  painter->drawPath( outlinepath );
160 
161  painter->setPen( Qt::NoPen );
162  painter->drawPath( outlinepath );
163 
164  int compassLength = static_cast<int>( compassRect.height() ) - 5 - fontheight;
165 
166  QSize compassSize( compassLength, compassLength );
167 
168  // Rerender compass pixmap if the size has changed
169  if ( m_compass.isNull() || m_compass.size() != compassSize ) {
170  m_compass = QPixmap( compassSize );
171  m_compass.fill( Qt::transparent );
172  QPainter mapPainter( &m_compass );
173  mapPainter.setViewport( m_compass.rect() );
174  m_svgobj->render( &mapPainter );
175  }
176  painter->drawPixmap( QPoint( static_cast<int>( compassRect.width() - compassLength ) / 2, fontheight + 5 ), m_compass );
177 
178  painter->restore();
179 }
180 
181 QDialog *CompassFloatItem::configDialog()
182 {
183  if ( !m_configDialog ) {
184  m_configDialog = new QDialog();
185  m_uiConfigWidget = new Ui::CompassConfigWidget;
186  m_uiConfigWidget->setupUi( m_configDialog );
187  readSettings();
188  connect( m_uiConfigWidget->m_buttonBox, SIGNAL(accepted()),
189  SLOT(writeSettings()) );
190  connect( m_uiConfigWidget->m_buttonBox, SIGNAL(rejected()),
191  SLOT(readSettings()) );
192  QPushButton *applyButton = m_uiConfigWidget->m_buttonBox->button( QDialogButtonBox::Apply );
193  connect( applyButton, SIGNAL(clicked()),
194  this, SLOT(writeSettings()) );
195  }
196 
197  return m_configDialog;
198 }
199 
200 QHash<QString,QVariant> CompassFloatItem::settings() const
201 {
202  QHash<QString, QVariant> result = AbstractFloatItem::settings();
203 
204  result.insert( "theme", m_themeIndex );
205 
206  return result;
207 }
208 
209 void CompassFloatItem::setSettings( const QHash<QString,QVariant> &settings )
210 {
211  AbstractFloatItem::setSettings( settings );
212 
213  m_themeIndex = settings.value( "theme", 0 ).toInt();
214 
215  readSettings();
216 }
217 
218 void CompassFloatItem::readSettings()
219 {
220  if ( m_uiConfigWidget && m_themeIndex >= 0 && m_themeIndex < m_uiConfigWidget->m_themeList->count() ) {
221  m_uiConfigWidget->m_themeList->setCurrentRow( m_themeIndex );
222  }
223 
224  QString theme = ":/compass.svg";
225  switch( m_themeIndex ) {
226  case 1:
227  theme = ":/compass-arrows.svg";
228  break;
229  case 2:
230  theme = ":/compass-atom.svg";
231  break;
232  case 3:
233  theme = ":/compass-magnet.svg";
234  break;
235  }
236 
237  delete m_svgobj;
238  m_svgobj = new QSvgRenderer( theme, this );
239  m_compass = QPixmap();
240 }
241 
242 void CompassFloatItem::writeSettings()
243 {
244  if ( m_uiConfigWidget ) {
245  m_themeIndex = m_uiConfigWidget->m_themeList->currentRow();
246  }
247  readSettings();
248  update();
249  emit settingsChanged( nameId() );
250 }
251 
252 }
253 
254 Q_EXPORT_PLUGIN2( CompassFloatItem, Marble::CompassFloatItem )
255 
256 #include "CompassFloatItem.moc"
QPainter
Marble::AbstractFloatItem::settings
virtual QHash< QString, QVariant > settings() const
Settings of the plugin.
Definition: AbstractFloatItem.cpp:70
Marble::CompassFloatItem::version
QString version() const
Definition: CompassFloatItem.cpp:72
QDialog
Marble::CompassFloatItem::nameId
QString nameId() const
Returns the unique name of the plugin.
Definition: CompassFloatItem.cpp:67
Marble::CompassFloatItem::configDialog
QDialog * configDialog()
Returns a pointer to the configuration dialog of the plugin.
Definition: CompassFloatItem.cpp:181
Marble::FrameGraphicsItem::marginLeft
qreal marginLeft() const
Returns the left margin of the item.
Definition: FrameGraphicsItem.cpp:89
Marble::PluginAuthor
Definition: PluginInterface.h:28
Marble::AbstractFloatItem::pen
QPen pen() const
current pen for rendering
Definition: AbstractFloatItem.cpp:96
Marble::AbstractFloatItem::setSettings
virtual void setSettings(const QHash< QString, QVariant > &settings)
Set the settings of the plugin.
Definition: AbstractFloatItem.cpp:77
Marble::CompassFloatItem::backgroundShape
QPainterPath backgroundShape() const
Returns the shape of the background.
Definition: CompassFloatItem.cpp:110
Marble::CompassFloatItem
The class that creates a compass.
Definition: CompassFloatItem.h:33
Marble::CompassFloatItem::setSettings
void setSettings(const QHash< QString, QVariant > &settings)
Set the settings of the plugin.
Definition: CompassFloatItem.cpp:209
MarbleDebug.h
Marble::CompassFloatItem::changeViewport
void changeViewport(ViewportParams *viewport)
Definition: CompassFloatItem.cpp:123
Marble::FrameGraphicsItem::background
QBrush background() const
Returns the background brush of the item.
Definition: FrameGraphicsItem.cpp:160
Marble::RenderPlugin::settingsChanged
void settingsChanged(QString nameId)
This signal is emitted if the settings of the RenderPlugin changed.
Marble::AbstractFloatItem
The abstract class for float item plugins.
Definition: AbstractFloatItem.h:48
Marble::CompassFloatItem::guiString
QString guiString() const
String that should be displayed in GUI.
Definition: CompassFloatItem.cpp:62
Marble::CompassFloatItem::name
QString name() const
Returns the user-visible name of the plugin.
Definition: CompassFloatItem.cpp:57
Marble::FrameGraphicsItem::contentRect
QRectF contentRect() const
Returns the rect of the content in item coordinates.
Definition: FrameGraphicsItem.cpp:171
MarbleDirs.h
Marble::CompassFloatItem::settings
QHash< QString, QVariant > settings() const
Settings of the plugin.
Definition: CompassFloatItem.cpp:200
Marble::CompassFloatItem::copyrightYears
QString copyrightYears() const
Definition: CompassFloatItem.cpp:82
Marble::CompassFloatItem::isInitialized
bool isInitialized() const
Definition: CompassFloatItem.cpp:105
Marble::ViewportParams::polarity
int polarity() const
Definition: ViewportParams.cpp:150
Marble::FrameGraphicsItem::padding
qreal padding() const
Returns the padding of the item.
Definition: FrameGraphicsItem.cpp:125
Marble::ViewportParams
A public class that controls what is visible in the viewport of a Marble map.
Definition: ViewportParams.h:44
Q_EXPORT_PLUGIN2
#define Q_EXPORT_PLUGIN2(a, b)
Definition: marble_export.h:34
ViewportParams.h
This file contains the headers for ViewportParams.
Marble::CompassFloatItem::description
QString description() const
Returns a user description of the plugin.
Definition: CompassFloatItem.cpp:77
Marble::MarbleModel
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition: MarbleModel.h:96
Marble::FrameGraphicsItem::marginTop
qreal marginTop() const
Returns the top margin of the item.
Definition: FrameGraphicsItem.cpp:65
Marble::CompassFloatItem::icon
QIcon icon() const
Returns an icon for the plugin.
Definition: CompassFloatItem.cpp:94
Marble::CompassFloatItem::pluginAuthors
QList< PluginAuthor > pluginAuthors() const
Definition: CompassFloatItem.cpp:87
Marble::MarbleGraphicsItem::update
void update()
Marks the item and all parent items as invalid.
Definition: MarbleGraphicsItem.cpp:167
Marble::AbstractFloatItem::font
QFont font() const
current font for rendering
Definition: AbstractFloatItem.cpp:107
Marble::CompassFloatItem::paintContent
void paintContent(QPainter *painter)
Here the items paint their content.
Definition: CompassFloatItem.cpp:132
Marble::CompassFloatItem::initialize
void initialize()
Definition: CompassFloatItem.cpp:99
Marble::CompassFloatItem::CompassFloatItem
CompassFloatItem()
Definition: CompassFloatItem.cpp:29
Marble::CompassFloatItem::~CompassFloatItem
~CompassFloatItem()
Definition: CompassFloatItem.cpp:47
CompassFloatItem.h
Marble::CompassFloatItem::backendTypes
QStringList backendTypes() const
Returns the name(s) of the backend that the plugin can render This method should return the name of t...
Definition: CompassFloatItem.cpp:52
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:38:49 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