landmarkadddialog.cpp Example File
landmarkbrowser/landmarkadddialog.cpp
#include <QListWidgetItem>
#include <qlandmarkcategory.h>
#include <QMessageBox>
#include <qlandmarkmanager.h>
#include "landmarkadddialog.h"
LandmarkAddDialog::LandmarkAddDialog(QWidget *parent, Qt::WindowFlags flags, const QLandmark &landmark)
: QDialog(parent, flags)
{
setupUi(this);
if (landmark != QLandmark()) {
setWindowTitle("Edit Landmark");
lm = landmark;
nameLineEdit->setText(landmark.name());
latitudeLineEdit->setText(QString::number(landmark.coordinate().latitude()));
longitudeLineEdit->setText(QString::number(landmark.coordinate().longitude()));
streetLineEdit->setText(landmark.address().street());
districtLineEdit->setText(landmark.address().district());
cityLineEdit->setText(landmark.address().city());
countyLineEdit->setText(landmark.address().county());
stateLineEdit->setText(landmark.address().state());
countryLineEdit->setText(landmark.address().country());
descriptionLineEdit->setText(landmark.description());
iconUrlLineEdit->setText(landmark.iconUrl().toString());
urlLineEdit->setText(landmark.url().toString());
phoneLineEdit->setText(landmark.phoneNumber());
radiusLineEdit->setText(QString::number(landmark.radius()));
} else {
setWindowTitle("Add Landmark");
}
QLandmarkManager manager;
QList<QLandmarkCategory> categories = manager.categories();
foreach( QLandmarkCategory category, categories) {
QListWidgetItem *categoryItem = new QListWidgetItem(categoryList,QListWidgetItem::UserType + 1);
categoryItem->setData(Qt::DisplayRole,category.name());
QVariant var;
var.setValue(category.categoryId());
categoryItem->setData(Qt::UserRole, var);
categoryItem->setFlags(Qt::ItemIsEnabled);
if (landmark.categoryIds().contains(category.categoryId()))
categoryItem->setCheckState(Qt::Checked);
else
categoryItem->setCheckState(Qt::Unchecked);
}
categoryList->setMinimumHeight(categories.count()
* (categoryList->sizeHintForRow(0) + categoryList->spacing())
+ categoryList->frameWidth() *2);
QObject::connect(categoryList, SIGNAL(itemPressed(QListWidgetItem *)),
this, SLOT(categoryPressed(QListWidgetItem *)));
}
LandmarkAddDialog::~LandmarkAddDialog()
{
delete categoryList;
categoryList =0;
}
QLandmark LandmarkAddDialog::landmark()
{
return lm;
}
void LandmarkAddDialog::accept()
{
bool ok;
double latitude = this->latitudeLineEdit->text().toDouble(&ok);
if (!ok || latitude < -90.0 || latitude > 90.0 || !(latitude == latitude)) {
QMessageBox::warning(this, "Invalid latitude", "Latitude must be between -90 and 90 degrees",
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
double longitude = this->longitudeLineEdit->text().toDouble(&ok);
if (!ok || longitude < -180.0 || longitude > 180.0 || !(longitude == longitude)) {
QMessageBox::warning(this, "Invalid longitude", "Longitude must be between -180 and 180 degrees",
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
QGeoCoordinate coordinate;
coordinate.setLatitude(latitude);
coordinate.setLongitude(longitude);
lm.setCoordinate(coordinate);
lm.setName(nameLineEdit->text());
QGeoAddress address = lm.address();
address.setStreet(streetLineEdit->text());
address.setDistrict(districtLineEdit->text());
address.setCity(cityLineEdit->text());
address.setCounty(countyLineEdit->text());
address.setState(stateLineEdit->text());
address.setCountry(countryLineEdit->text());
lm.setAddress(address);
lm.setDescription(descriptionLineEdit->text());
lm.setIconUrl(QUrl(iconUrlLineEdit->text()));
lm.setUrl(QUrl(urlLineEdit->text()));
lm.setPhoneNumber(phoneLineEdit->text());
lm.setRadius(radiusLineEdit->text().toDouble());
lm.setCategoryIds(QList<QLandmarkCategoryId>());
for (int i=0; i < categoryList->count(); ++i) {
if (categoryList->item(i)->checkState() == Qt::Checked) {
lm.addCategoryId(categoryList->item(i)->data(Qt::UserRole).value<QLandmarkCategoryId>());
}
}
QDialog::accept();
}
void LandmarkAddDialog::reject()
{
lm.clear();
QDialog::reject();
}
void LandmarkAddDialog::categoryPressed(QListWidgetItem *item)
{
if (item->checkState() == Qt::Checked)
item->setCheckState(Qt::Unchecked);
else
item->setCheckState(Qt::Checked);
}
[+] Documentation Feedback