Marble/MarbleMarbleWidget: Difference between revisions

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


Line 6: Line 6:
name=MarbleWidget: Changing basic map properties|
name=MarbleWidget: Changing basic map properties|


pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt]|
pre=[[Projects/Marble/MarbleCPlusPlus|Tutorial 1 - Hello World]]|


next=[[Projects/Marble/MarbleGeoPainter|Tutorial 3 - Marble's GeoPainter]]|  
next=[[Projects/Marble/MarbleSignalsSlots|Tutorial 3 - Basic interaction with MarbleWidget]]|  
}}
}}




== Creating a weather map ==
== Creating a weather map ==
Now we want to modify the map:
We'd like to display a small weather map. So we need to modify the map. And we need to turn on the satellite view, enable the clouds and enable the country border lines.


Again [http://api.kde.org/4.x-api/kdeedu/marble/classMarble_1_1MarbleWidget.html MarbleWidget] provides a convenient way to make changes to the overall look and feel of the map:
Again [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1MarbleWidget.html MarbleWidget] provides a convenient way to make these changes to the overall look and feel of the map.


We'd like to display a small weather map. So we turn on the satellite view, enable the clouds and enable the country border lines.
By default Marble shows a few info boxes: '''Overview Map''', '''Compass''' and '''ScaleBar'''. But the size for the widget is very limited. Therefore we want to shrink the compass. And we want to get rid of all the clutter, so we turn off the Overview Map and the ScaleBar. In the source code the class [http://api.kde.org/4.x-api/kdeedu-apidocs/marble/html/classMarble_1_1AbstractFloatItem.html AbstractFloatItem] is used to display all kinds of '''Info Boxes'''. All the Info Boxes are derived from the AbstractFloatItem class. Now we get a list of all the float items that are known to MarbleWidget and we go through it. Once we reach the float item which has got the name id "compass" we make all the changes we want to it (this has been simplified in Marble 0.11.0 where you can access AbstractFloatItems directly via their nameId):


By default Marble shows a few info boxes: Overview Map, Compass and ScaleBar.
<source lang="cpp-qt">
Due to the limited size we want to shrink the compass. We turn the Overview Map and the ScaleBar off. In the source code the class [http://api.kde.org/4.x-api/kdeedu/marble/classMarble_1_1AbstractFloatItem.html AbstractFloatItem] is used to display Info Boxes:
 
<code cppqt>
#include <QtGui/QApplication>
#include <QtGui/QApplication>


Line 65: Line 62:
     return app.exec();
     return app.exec();
}
}
</code>
</source>
 
Save the code above as <tt>marble_weather.cpp</tt> and compile it:
 
<source lang="bash">
g++ -I /usr/include/qt4/ -o marble_weather marble_weather.cpp -lmarblewidget -lQtGui
</source>
 
Instead of calling the compiler directly you can also create a [http://doc.trolltech.com/qmake-tutorial.html qmake project file]:


Save the code above as <tt>my_marble.cpp</tt> and compile it:
<syntaxhighlight lang="text">
TEMPLATE = app
TARGET = marble_weather
DEPENDPATH += .
INCLUDEPATH += .
SOURCES += marble_weather.cpp
LIBS += -lmarblewidget
</syntaxhighlight>


<code>
Store it as <tt>marble_weather.pro</tt> in the same directory and call
g++ -I /usr/include/qt4/ -o my_marble my_marble.cpp -lmarblewidget -lQtGui
</code>


If things go fine, execute <tt>./my_marble</tt> and you end up with a fully usable OpenStreetMap application: [[Image:My_marble.png]]
<syntaxhighlight lang="text">
qmake marble_weather.pro
make
</syntaxhighlight>


Here's a little checklist to tackle some problems that might arise when compiling the code above:
If things go fine, execute <tt>./marble_weather</tt> and you end up with a map application that displays clouds on top of a flat map:  


* You need Qt and Marble development packages (or comparable SVN installations)
[[Image:Marble_weather.png]]
* If Qt headers are not installed in /usr/include/qt4 on your system, change the path in the g++ call above accordingly.
* Likewise, add -I /path/to/marble/headers if they're not to be found in /usr/include

Revision as of 16:04, 19 July 2012

MarbleWidget: Changing basic map properties
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 1 - Hello World
What's Next   Tutorial 3 - Basic interaction with MarbleWidget
Further Reading   n/a


Creating a weather map

We'd like to display a small weather map. So we need to modify the map. And we need to turn on the satellite view, enable the clouds and enable the country border lines.

Again MarbleWidget provides a convenient way to make these changes to the overall look and feel of the map.

By default Marble shows a few info boxes: Overview Map, Compass and ScaleBar. But the size for the widget is very limited. Therefore we want to shrink the compass. And we want to get rid of all the clutter, so we turn off the Overview Map and the ScaleBar. In the source code the class AbstractFloatItem is used to display all kinds of Info Boxes. All the Info Boxes are derived from the AbstractFloatItem class. Now we get a list of all the float items that are known to MarbleWidget and we go through it. Once we reach the float item which has got the name id "compass" we make all the changes we want to it (this has been simplified in Marble 0.11.0 where you can access AbstractFloatItems directly via their nameId):

#include <QtGui/QApplication>

#include <marble/global.h>
#include <marble/MarbleWidget.h>
#include <marble/AbstractFloatItem.h>

using namespace Marble;

int main(int argc, char** argv)
{
    QApplication app(argc,argv);

    // Create a Marble QWidget without a parent
    MarbleWidget *mapWidget = new MarbleWidget();

    // Load the OpenStreetMap map
    mapWidget->setMapThemeId("earth/bluemarble/bluemarble.dgml");

    mapWidget->setProjection( Mercator ); 
    
    // Enable the cloud cover and enable the country borders
    mapWidget->setShowClouds( true );
    mapWidget->setShowBorders( true );
    
    // Hide the FloatItems: Compass and StatusBar
    mapWidget->setShowOverviewMap(false);
    mapWidget->setShowScaleBar(false);
    
    foreach ( AbstractFloatItem * floatItem, mapWidget->floatItems() )
        if ( floatItem && floatItem->nameId() == "compass" ) {
            
            // Put the compass onto the left hand side
            floatItem->setPosition( QPoint( 10, 10 ) );
            // Make the content size of the compass smaller
            floatItem->setContentSize( QSize( 50, 50 ) );
        }
    
    mapWidget->resize( 400, 300 );
    mapWidget->show();

    return app.exec();
}

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

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

Instead of calling the compiler directly you can also create a qmake project file:

TEMPLATE = app
TARGET = marble_weather
DEPENDPATH += .
INCLUDEPATH += .
SOURCES += marble_weather.cpp
LIBS += -lmarblewidget

Store it as marble_weather.pro in the same directory and call

qmake marble_weather.pro
make

If things go fine, execute ./marble_weather and you end up with a map application that displays clouds on top of a flat map: