Marble/MarbleGeoPainter: Difference between revisions

From KDE TechBase
No edit summary
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Editing Projects/Marble/MarbleGeoPainter}}
 
{{TutorialBrowser|
{{TutorialBrowser|


series=Marble C++ Tutorial|
series=Marble C++ Tutorial|


name=Hello Marble|
name=GeoPainter: Painting onto the map|


pre=[[Projects/Marble/MarbleCPlusPlus|Tutorial 1 - Hello World]]|
pre=[[Projects/Marble/Runners/Parse|Tutorial 12 - Opening .kml, .gpx... files]]|


next=[[Projects/Marble/LayerInterface|Tutorial 3 - Custom Layers: Drawing in Plugins]]|  
next=[[Projects/Marble/LayerInterface|Tutorial 14 - Drawing in Custom Layers]]|  
}}
}}


In the previous tutorial, you saw how easy it is to embed a MarbleWidget in any Qt application: Just create a Marble::MarbleWidget, set a map theme on it and... you're done already.
In the previous tutorial you've seen how easy it is to embed a [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1MarbleWidget.html MarbleWidget] into a Qt application: Just create a MarbleWidget, set a map theme on it and ... you're done already.


Next we'll extend that example a bit and write our own little paint method to add some extra content to the globe. To facilitate this, Marble provides a painting hook called customPaint. It is called in between of the normal paint operations: After the background and tiles are painted, but before the top layers like float items (info boxes).
Next we'll extend that example a bit and write our own little paint method to add some extra content to the globe. To facilitate this, Marble provides a painting hook called [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1MarbleWidget.html#47e87a8639f38e9da380923453a6f35f MarbleWidget::customPaint]. It is called in between of the normal paint operations: After the background and tiles are painted, but before the top layers like float items (info boxes).


The customPaint operation is called with a GeoPainter: An extended QPainter which not only is able to paint at certain screen (pixel) positions, but also at certain geo (lat,lon) positions. We'll make use of that feature now. To keep things simple again, we just add a little 'Hello World' message indicated by a green circle.
The customPaint operation is called with a [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1GeoPainter.html GeoPainter]: An extended [http://doc.trolltech.com/qpainter.html QPainter] which not only is able to paint at certain screen (pixel) positions, but also at certain geo (lat,lon) positions. We'll make use of that feature now. To keep things simple again, we just add a little 'Hello World' message indicated by a green circle.


<code cppqt>
<source lang="cpp">
#include <QtGui/QApplication>
#include <QtGui/QApplication>
#include <marble/MarbleWidget.h>
#include <marble/MarbleWidget.h>
Line 47: Line 47:
     return app.exec();
     return app.exec();
}
}
</code>
</source>


Save the code above as <tt>my_marble.cpp</tt> and compile it:
Save the code above as <tt>my_marble.cpp</tt> and compile it:


<code>
<source lang="bash">
  g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui
  g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui
</code>
</source>


If things go fine, execute <tt>./my_marble</tt> and you end up with a globe view similar to this:
If things go fine, execute <tt>./my_marble</tt> and you end up with a globe view similar to this:
[[Image:Marble-geopainter.png]]
[[Image:Marble-geopainter.png]]


There may be situations where customPaint() does not suit your needs. This can be the case when you don't want to paint at the very top position (above all other items), or when subclassing MarbleWidget is not possible for some reason. In that case, have a look at [[Projects/Marble/LayerInterface|Tutorial 2b - Creating Custom Layers]]
There may be situations where MarbleWidget::customPaint() does not suit your needs. This can be the case when you don't want to paint at the very top position (above all other items), or when subclassing MarbleWidget is not possible for some reason. In that case, have a look at the next chapter [[Projects/Marble/LayerInterface|Drawing in Custom Layers]]
 
The latest source code of this example can be found [https://quickgit.kde.org/?p=marble.git&a=blob&f=examples%2Fcpp%2Fgeopainter%2Fmain.cpp here].

Latest revision as of 21:01, 10 March 2016

GeoPainter: Painting onto the map
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 12 - Opening .kml, .gpx... files
What's Next   Tutorial 14 - Drawing in Custom Layers
Further Reading   n/a

In the previous tutorial you've seen how easy it is to embed a MarbleWidget into a Qt application: Just create a MarbleWidget, set a map theme on it and ... you're done already.

Next we'll extend that example a bit and write our own little paint method to add some extra content to the globe. To facilitate this, Marble provides a painting hook called MarbleWidget::customPaint. It is called in between of the normal paint operations: After the background and tiles are painted, but before the top layers like float items (info boxes).

The customPaint operation is called with a GeoPainter: An extended QPainter which not only is able to paint at certain screen (pixel) positions, but also at certain geo (lat,lon) positions. We'll make use of that feature now. To keep things simple again, we just add a little 'Hello World' message indicated by a green circle.

#include <QtGui/QApplication>
#include <marble/MarbleWidget.h>
#include <marble/GeoPainter.h>

using namespace Marble;
 
class MyMarbleWidget : public MarbleWidget
{
public:
    virtual void customPaint(GeoPainter* painter);
};

void MyMarbleWidget::customPaint(GeoPainter* painter)
{
    GeoDataCoordinates home(8.4, 49.0, 0.0, GeoDataCoordinates::Degree);
    painter->setPen(Qt::green);
    painter->drawEllipse(home, 7, 7);
    painter->setPen(Qt::black);
    painter->drawText(home, "Hello Marble!");
}

int main(int argc, char** argv)
{
    QApplication app(argc,argv);
    MyMarbleWidget *mapWidget = new MyMarbleWidget;
    mapWidget->setMapThemeId("earth/openstreetmap/openstreetmap.dgml");
    mapWidget->show();
    return app.exec();
}

Save the code above as my_marble.cpp and compile it:

 g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui

If things go fine, execute ./my_marble and you end up with a globe view similar to this:

There may be situations where MarbleWidget::customPaint() does not suit your needs. This can be the case when you don't want to paint at the very top position (above all other items), or when subclassing MarbleWidget is not possible for some reason. In that case, have a look at the next chapter Drawing in Custom Layers

The latest source code of this example can be found here.