• 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
  • examples
  • cpp
  • custom-layers
examples/cpp/custom-layers/main.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 2012 Dennis Nienhüser <earthwings@gentoo.org>
9 //
10 
11 #include <marble/MarbleWidget.h>
12 #include <marble/MarbleMap.h>
13 #include <marble/MarbleModel.h>
14 #include <marble/GeoPainter.h>
15 #include <GeoDataLineString.h>
16 #include <marble/LayerInterface.h>
17 
18 #include <QTime>
19 #include <QTimer>
20 #include <QApplication>
21 #include <QKeyEvent>
22 
23 using namespace Marble;
24 
25 class MyPaintLayer : public QObject, public LayerInterface
26 {
27 public:
28  // Constructor
29  MyPaintLayer(MarbleWidget* widget);
30 
31  // Implemented from LayerInterface
32  virtual QStringList renderPosition() const;
33 
34  // Implemented from LayerInterface
35  virtual bool render( GeoPainter *painter, ViewportParams *viewport,
36  const QString& renderPos = "NONE", GeoSceneLayer * layer = 0 );
37 
38  // Overriding QObject
39  virtual bool eventFilter(QObject *obj, QEvent *event);
40 
41  static GeoDataCoordinates approximate(const GeoDataCoordinates &base, qreal angle, qreal dist);
42 
43 private:
44  MarbleWidget* m_widget;
45 
46  int m_index;
47 };
48 
49 MyPaintLayer::MyPaintLayer(MarbleWidget* widget) : m_widget(widget), m_index(0)
50 {
51  // nothing to do
52 }
53 
54 QStringList MyPaintLayer::renderPosition() const
55 {
56  // We will paint in exactly one of the following layers.
57  // The current one can be changed by pressing the '+' key
58  QStringList layers = QStringList() << "SURFACE" << "HOVERS_ABOVE_SURFACE";
59  layers << "ORBIT" << "USER_TOOLS" << "STARS";
60 
61  int index = m_index % layers.size();
62  return QStringList() << layers.at(index);
63 }
64 
65 bool MyPaintLayer::eventFilter(QObject *obj, QEvent *event)
66 {
67  Q_UNUSED(obj)
68 
69  // Adjust the current layer when '+' is pressed
70  if (event->type() == QEvent::KeyPress)
71  {
72  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
73  if (keyEvent->key() == Qt::Key_Plus) {
74  ++m_index;
75  return true;
76  }
77  }
78 
79  return false;
80 }
81 
82 GeoDataCoordinates MyPaintLayer::approximate(const GeoDataCoordinates &base, qreal angle, qreal dist)
83 {
84  // This is just a rough estimation that ignores projections.
85  // It only works for short distances. Don't use in real code.
86  GeoDataCoordinates::Unit deg = GeoDataCoordinates::Degree;
87  return GeoDataCoordinates ( base.longitude(deg) + 1.5 * dist * sin(angle),
88  base.latitude(deg) + dist * cos(angle), 0.0, deg);
89 }
90 
91 bool MyPaintLayer::render( GeoPainter *painter, ViewportParams *viewport,
92  const QString& renderPos, GeoSceneLayer * layer )
93 {
94  Q_UNUSED(viewport)
95  Q_UNUSED(renderPos)
96  Q_UNUSED(layer)
97 
98  // Have window title reflect the current paint layer
99  m_widget->setWindowTitle(renderPosition().first());
100  GeoDataCoordinates home(8.4, 48.0, 0.0, GeoDataCoordinates::Degree);
101  QTime now = QTime::currentTime();
102 
103  painter->setRenderHint(QPainter::Antialiasing, true);
104 
105  // Large circle built by 60 small circles
106  painter->setPen( QPen(QBrush(QColor::fromRgb(255,255,255,200)), 3.0, Qt::SolidLine, Qt::RoundCap ) );
107  for (int i=0; i<60; ++i)
108  painter->drawEllipse(approximate(home, M_PI * i / 30.0, 1.0), 5, 5);
109 
110  // hour, minute, second hand
111  painter->drawPolyline(GeoDataLineString() << home << approximate(home, M_PI * now.minute() / 30.0, 0.75));
112  painter->drawPolyline(GeoDataLineString() << home << approximate(home, M_PI * now.hour() / 6.0, 0.5));
113  painter->setPen(QPen(QBrush(Qt::red), 4.0, Qt::SolidLine, Qt::RoundCap ));
114  painter->drawPolyline(GeoDataLineString() << home << approximate(home, M_PI * now.second() / 30.0, 1.0));
115 
116  return true;
117 }
118 
119 int main(int argc, char** argv)
120 {
121  QApplication app(argc,argv);
122  MarbleWidget *mapWidget = new MarbleWidget;
123 
124  // Create and register our paint layer
125  MyPaintLayer* layer = new MyPaintLayer(mapWidget);
126  // Uncomment for older versions of Marble:
127  // mapWidget->map()->model()->addLayer(layer);
128  mapWidget->addLayer(layer);
129 
130  // Install an event handler: Pressing + will change the layer we paint at
131  mapWidget->installEventFilter(layer);
132 
133  // Finish widget creation.
134  mapWidget->setMapThemeId("earth/bluemarble/bluemarble.dgml");
135  mapWidget->show();
136 
137  // Update each second to give the clock second resolution
138  QTimer seconds;
139  seconds.setInterval(1000);
140  QObject::connect(&seconds, SIGNAL(timeout()), mapWidget, SLOT(update()));
141  seconds.start();
142 
143  return app.exec();
144 }
Marble::GeoDataCoordinates::Unit
Unit
enum used constructor to specify the units used
Definition: GeoDataCoordinates.h:64
QTimer::setInterval
void setInterval(int msec)
QTime::minute
int minute() const
QEvent
Marble::GeoDataCoordinates
A 3d point representation.
Definition: GeoDataCoordinates.h:52
angle
double angle(double vec1[3], double vec2[3])
Definition: sgp4ext.cpp:164
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
Marble::GeoPainter
A painter that allows to draw geometric primitives on the map.
Definition: GeoPainter.h:98
MarbleModel.h
This file contains the headers for MarbleModel.
QList::at
const T & at(int i) const
QApplication
LayerInterface.h
Marble::LayerInterface
Definition: LayerInterface.h:26
Marble::GeoPainter::drawPolyline
void drawPolyline(const GeoDataLineString &lineString, const QString &labelText=QString(), LabelPositionFlags labelPositionFlags=LineCenter)
Draws a given line string (a "polyline").
Definition: GeoPainter.cpp:474
QBrush
Marble::GeoDataCoordinates::latitude
qreal latitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the latitude of the GeoDataCoordinates object use the unit parameter to switch between Radi...
Definition: GeoDataCoordinates.cpp:751
Marble::GeoPainter::drawEllipse
void drawEllipse(const GeoDataCoordinates &centerPosition, qreal width, qreal height, bool isGeoProjected=false)
Draws an ellipse at the given position. The ellipse is placed with its center located at the given ce...
Definition: GeoPainter.cpp:289
QTime
Marble::GeoDataCoordinates::Degree
Definition: GeoDataCoordinates.h:66
QTime::second
int second() const
QList::size
int size() const
Marble::MyPaintLayer::MyPaintLayer
MyPaintLayer(MarbleWidget *widget)
Definition: examples/cpp/custom-layers/main.cpp:49
Marble::MarbleWidget
A widget class that displays a view of the earth.
Definition: MarbleWidget.h:104
QObject::event
virtual bool event(QEvent *e)
Marble::MarbleWidget::setMapThemeId
void setMapThemeId(const QString &maptheme)
Set a new map theme.
Definition: MarbleWidget.cpp:759
main
int main(int argc, char **argv)
Definition: examples/cpp/custom-layers/main.cpp:119
QColor::fromRgb
QColor fromRgb(QRgb rgb)
MarbleMap.h
This file contains the headers for MarbleMap.
Marble::GeoSceneLayer
Layer of a GeoScene document.
Definition: GeoSceneLayer.h:43
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
QTimer
Marble::MyPaintLayer::renderPosition
QStringList renderPosition() const
Preferred level in the layer stack for the rendering.
Definition: examples/cpp/custom-layers/main.cpp:54
QObject
QPainter::setPen
void setPen(const QColor &color)
GeoDataLineString.h
QObject::eventFilter
virtual bool eventFilter(QObject *watched, QEvent *event)
QString
GeoPainter.h
Marble::MarbleWidget::addLayer
void addLayer(LayerInterface *layer)
Add a layer to be included in rendering.
Definition: MarbleWidget.cpp:370
Marble::GeoDataLineString
A LineString that allows to store a contiguous set of line segments.
Definition: GeoDataLineString.h:75
QStringList
Marble::ViewportParams
A public class that controls what is visible in the viewport of a Marble map.
Definition: ViewportParams.h:44
Marble::MyPaintLayer
Definition: squad-interpolation.h:28
QTime::hour
int hour() const
QKeyEvent::key
int key() const
Marble::GeoDataCoordinates::longitude
qreal longitude(GeoDataCoordinates::Unit unit=GeoDataCoordinates::Radian) const
retrieves the longitude of the GeoDataCoordinates object use the unit parameter to switch between Rad...
Definition: GeoDataCoordinates.cpp:739
Marble::MyPaintLayer::render
bool render(GeoPainter *painter, ViewportParams *viewport, const QString &renderPos="NONE", GeoSceneLayer *layer=0)
Renders the content provided by the layer on the viewport.
Definition: examples/cpp/custom-layers/main.cpp:91
QTime::currentTime
QTime currentTime()
QKeyEvent
QApplication::exec
int exec()
QWidget::setWindowTitle
void setWindowTitle(const QString &)
M_PI
#define M_PI
Definition: GeoDataCoordinates.h:26
QPen
MarbleWidget.h
This file contains the headers for MarbleWidget.
QTimer::start
void start(int msec)
QWidget::show
void show()
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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