Marble/Routing/MarblePythonBasicRouting: Difference between revisions

From KDE TechBase
(Created page with " {{TutorialBrowser| series=Marble Python Tutorial| name=GeoPainter: Painting onto the map| pre=[[Projects/Marble/Runners/MarblePythonParse|Tutorial 12 - Opening .kml, .gpx....")
 
(Blanked the page)
Line 1: Line 1:


{{TutorialBrowser|
series=Marble Python Tutorial|
name=GeoPainter: Painting onto the map|
pre=[[Projects/Marble/Runners/MarblePythonParse|Tutorial 12 - Opening .kml, .gpx... files]]|
next=[[Projects/Marble/MarblePythonLayerInterface|Tutorial 14 - Drawing in Custom Layers]]|
}}
{{note|
the documentation for the Marble Python bindings are currently not available online. They can instead be found in the source code at the location: <tt>src/bindings/python/html/marble/Marble.html</tt>.
}}
In the previous tutorial you've seen how easy it is to embed a <tt>MarbleWidget</tt> 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 <tt>MarbleWidget.customPaint()</tt>. 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 <tt>GeoPainter</tt>: 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.
<source lang="python">
#!/usr/bin/env python
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.marble import *
import sys
class MyMarbleWidget(Marble.MarbleWidget):
    def __init__(self):
        Marble.MarbleWidget.__init__(self)
    def customPaint(self, painter):
        home = Marble.GeoDataCoordinates(8.4, 49.0, 0.0, Marble.GeoDataCoordinates.Degree)
        painter.setPen(Qt.green)
        painter.drawEllipse(home, 7, 7)
        painter.setPen(Qt.black)
        painter.drawText(home, "Hello Marble!")
def main():
    app = QApplication(sys.argv)
    # create the marble widget
    marble = MyMarbleWidget()
    # resize the widget and add a window title
    marble.resize(500, 500)
    marble.setWindowTitle("my_marble")
    # Load the OpenStreetMap map
    marble.setMapThemeId("earth/openstreetmap/openstreetmap.dgml")
    marble.setZoom(1100)
    marble.centerOn(Marble.GeoDataCoordinates(8.4, 49.0, 0.0, Marble.GeoDataCoordinates.Degree))
    # show the marble widget
    marble.show()
    # run the app
    app.exec_()
main()
</source>
Save the code above as <tt>painting.py</tt> and execute <tt>python painting.py</tt> and you end up with a globe view similar to this:
[[Image:Marble-geopainter.png]]
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/MarblePythonLayerInterface|Drawing in Custom Layers]]

Revision as of 14:48, 29 December 2013