< Marble
Revision as of 09:49, 3 August 2010 by Tackat (Talk | contribs) (Creating a window with controls)

Jump to: navigation, search

Editing Projects/Marble/MarbleCPlusPlus

Basic interaction with MarbleWidget
Tutorial Series   Marble C++ Tutorial
Previous   Tutorial 2 - Changing basic map properties
What's Next   Tutorial 4 - GeoPainter: Painting onto the map
Further Reading   n/a

Creating a window with controls

We'd like to add other widgets to our Marble window: A zoom slider and a label that shows the current mouse position in geodetic coordinates: longitude and latitude.

In order to achieve this we need to create a vertical layout. Once we are done we add the slider and the label that we created to the layout. Also we zoom the globe to the slider's default value using the MarbleWidget::zoomView(int) method.

We want to center our globe onto South America. So we create a new GeoDataCoordinates object that takes the longitude and the latitude as a parameter and we call MarbleWidget::centerOn.

As you might have realized already GeoDataCoordinates is the geodetic "sister" of QPoint. They share a very similar API. Additionally GeoDataCoordinates features a nice set of string conversion methods (GeoDataCoordinates::fromString(), GeoDataCoordinates::lonToString() and GeoDataCoordinates::latToString()). They are used in various places inside Marble such as the signal MarbleWidget::mouseMoveGeoPosition(const QString&) .

Finally we connect the signals and slots that MarbleWidget offers to the signals and slots of the slider and the label:

  1. include <QtGui/QApplication>
  1. include <QtGui/QLayout>
  2. include <QtGui/QSlider>
  3. include <QtGui/QLabel>
  1. include <marble/MarbleWidget.h>

using namespace Marble;

int main(int argc, char** argv) {

   QApplication app(argc,argv);
   QWidget *window = new QWidget;
   // Create a Marble QWidget without a parent
   MarbleWidget *mapWidget = new MarbleWidget();
   // Load the Plain map
   // Hide the FloatItems: OverviewMap, ScaleBar and Compass
   // Set the map quality to gain speed
   mapWidget->setMapQuality( NormalQuality, Still );
   mapWidget->setMapQuality( LowQuality, Animation );
   // Create a horizontal zoom slider and set the default zoom
   QSlider * zoomSlider = new QSlider(Qt::Horizontal);
   zoomSlider->setMinimum( 1000 );
   zoomSlider->setMaximum( 2400 );
   mapWidget->zoomView( zoomSlider->value() );
   // Create a label to show the geodetic position
   QLabel * positionLabel = new QLabel();
   positionLabel->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
   // Add all widgets to the vertical layout.
   QVBoxLayout *layout = new QVBoxLayout;
   // Center the map onto a given position
   GeoDataCoordinates home(-60.0, -10.0, 0.0, GeoDataCoordinates::Degree);
   // Connect the map widget to the position label.
   QObject::connect( mapWidget, SIGNAL( mouseMoveGeoPosition( QString ) ),
                     positionLabel, SLOT( setText( QString ) ) );
   // Connect the zoom slider to the map widget and vice versa.
   QObject::connect( zoomSlider, SIGNAL( valueChanged(int) ),
                     mapWidget, SLOT( zoomView(int) ) );
   QObject::connect( mapWidget, SIGNAL( zoomChanged(int) ),
                     zoomSlider, SLOT( setValue(int) ) );
   window->resize( 400, 300 );
   return app.exec();


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

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

If things go fine, execute ./marble_control and you end up with a map application that displays our globe with a zoom slider below:

Marble control.png