revgeocodingtab.cpp Example File
qgeoapiui/revgeocodingtab.cpp
#include "revgeocodingtab.h"
#include "placepresenter.h"
#include <QTreeWidget>
#include <QLineEdit>
#include <QString>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QMessageBox>
ReverseGeocodingTab::ReverseGeocodingTab(QWidget *parent) :
QWidget(parent),
m_searchManager(NULL)
{
QLabel *locationlbl = new QLabel(tr("Location (lat,long):"));
m_locLong = new QLineEdit("13.377");
m_locLong->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_locLat = new QLineEdit("52.51");
m_locLat->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
requestBtn = new QPushButton(tr("Reverse Geocoding"));
requestBtn->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
requestBtn->setDisabled(true);
QObject::connect(requestBtn, SIGNAL(clicked(bool)), this, SLOT(on_btnRequest_clicked()));
m_resultTree = new QTreeWidget();
QStringList labels;
labels << tr("Elements") << tr("Value");
m_resultTree->setHeaderLabels(labels);
m_resultTree->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_resultTree->setColumnWidth(0, 150);
QHBoxLayout *firstrow = new QHBoxLayout;
firstrow->setSpacing(0);
firstrow->setContentsMargins(0, 0, 0, 0);
firstrow->addWidget(locationlbl);
firstrow->addWidget(m_locLat);
firstrow->addWidget(m_locLong);
firstrow->addWidget(requestBtn);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setSpacing(2);
mainLayout->setContentsMargins(2, 1, 2, 1);
mainLayout->addLayout(firstrow);
mainLayout->addWidget(m_resultTree);
setLayout(mainLayout);
}
ReverseGeocodingTab::~ReverseGeocodingTab()
{
}
void ReverseGeocodingTab::initialize(QGeoSearchManager *searchManager)
{
m_resultTree->clear();
m_searchManager = searchManager;
if (m_searchManager) {
QObject::connect(m_searchManager, SIGNAL(finished(QGeoSearchReply*)), this,
SLOT(replyFinished(QGeoSearchReply*)));
QObject::connect(m_searchManager,
SIGNAL(error(QGeoSearchReply*, QGeoSearchReply::Error, QString)), this,
SLOT(resultsError(QGeoSearchReply*, QGeoSearchReply::Error, QString)));
if(m_searchManager->supportsGeocoding())
requestBtn->setDisabled(false);
}
else
requestBtn->setDisabled(true);
}
void ReverseGeocodingTab::on_btnRequest_clicked()
{
if (m_searchManager) {
QGeoCoordinate coord(m_locLat->text().toDouble(), m_locLong->text().toDouble());
m_resultTree->clear();
m_searchManager->geocode(coord);
} else {
QMessageBox::warning(this, tr("Reverse Geocoding"), tr("No search manager available."));
}
}
void ReverseGeocodingTab::replyFinished(QGeoSearchReply* reply)
{
if (!isHidden() && reply->error() == QGeoSearchReply::NoError) {
PlacePresenter presenter(m_resultTree, reply);
presenter.show();
reply->deleteLater();
}
}
void ReverseGeocodingTab::resultsError(QGeoSearchReply* reply, QGeoSearchReply::Error errorCode, QString errorString)
{
Q_UNUSED(errorCode)
if (!isHidden()) {
QTreeWidgetItem* top = new QTreeWidgetItem(m_resultTree);
top->setText(0, tr("Error"));
top->setText(1, errorString);
reply->deleteLater();
}
}