Whereabouts plugins can easily be added to retrieve location data from custom sources.
An example
The Whereabouts Sample Plugin demonstrates the integration of a custom Whereabouts plugin. The examples/sampleplugin/locationplugin directory contains the plugin, and examples/sampleplugin/mylocationapp contains an application that uses the plugin to retrieve location updates.
First, we create a QWhereabouts subclass called LocationProvider:
class LocationProvider : public QWhereabouts
{
Q_OBJECT
public:
LocationProvider(QObject *parent = 0);
void requestUpdate();
void startUpdates();
void stopUpdates();
private:
QTimer *m_timer;
};
This LocationProvider class simply produces updates with the current date/time and latitude-longitude coordinates of (0, 0). It uses QTimer to implement timed updates for QWhereabouts::startUpdates() and QWhereabouts::stopUpdates(). Here is the implementation:
LocationProvider::LocationProvider(QObject *parent)
: QWhereabouts(QWhereabouts::TerminalBasedUpdate, parent),
m_timer(new QTimer(this))
{
connect(m_timer, SIGNAL(timeout()), SLOT(requestUpdate()));
}
void LocationProvider::requestUpdate()
{
QWhereaboutsUpdate update;
update.setCoordinate(QWhereaboutsCoordinate(0.0, 0.0));
update.setUpdateDateTime(QDateTime::currentDateTime());
emitUpdated(update);
}
void LocationProvider::startUpdates()
{
if (updateInterval() > 0)
m_timer->start(updateInterval());
else
m_timer->start(1000);
}
void LocationProvider::stopUpdates()
{
m_timer->stop();
}
Then we create a Whereaboutsplugin called LocationPlugin that is able to create and return instances of LocationProvider. Here is the class definition:
class QTOPIA_PLUGIN_EXPORT LocationPlugin : public QWhereaboutsPlugin
{
Q_OBJECT
public:
LocationPlugin(QObject *parent = 0);
QWhereabouts *create(const QString &source);
};
In the locationplugin.cpp file, we implement create() to return an instance of LocationProvider, and export the plugin using the QTOPIA_EXPORT_PLUGIN macro:
LocationPlugin::LocationPlugin(QObject *parent)
: QWhereaboutsPlugin(parent)
{
}
QWhereabouts *LocationPlugin::create(const QString &source)
{
Q_UNUSED(source);
return new LocationProvider;
}
QTOPIA_EXPORT_PLUGIN(LocationPlugin)
Then, once the plugin has been installed with qbuild image, you can use it by passing the class name to QWhereaboutsFactory::create(). (The class name argument is not case-sensitive.) So in the examples/sampleplugin/mylocationapp project, we create a MyLocationApp class that uses our custom plugin, like this:
MyLocationApp::MyLocationApp(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
QWhereabouts *whereabouts = QWhereaboutsFactory::create("locationplugin");
connect(whereabouts, SIGNAL(updated(QWhereaboutsUpdate)),
SLOT(updated(QWhereaboutsUpdate)));
whereabouts->startUpdates();
}
void MyLocationApp::updated(const QWhereaboutsUpdate &update)
{
}
So MyLocationApp now receives updates through the LocationProvider we created.
To make this the default plugin to be returned if QWhereaboutsFactory::create() is called without any arguments, set the "Plugins/Default" value in $QPEDIR/etc/Settings/Trolltech/Whereabouts.conf to "locationplugin".
Other examples
The Whereabouts Mapping Demo uses the Whereabouts API together with Google Maps to either track the user's current location, or provide a simulation of a previous journey using a NMEA data log.