2015-05-17 19:13:41 +00:00
|
|
|
#include "locationinformation.h"
|
|
|
|
#include "dive.h"
|
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "divelistview.h"
|
|
|
|
#include "qthelper.h"
|
2015-05-22 19:15:53 +00:00
|
|
|
#include "globe.h"
|
2015-05-26 20:42:45 +00:00
|
|
|
#include "filtermodels.h"
|
2015-05-30 00:19:44 +00:00
|
|
|
#include "divelocationmodel.h"
|
2015-05-17 19:13:41 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QShowEvent>
|
|
|
|
|
|
|
|
LocationInformationWidget::LocationInformationWidget(QWidget *parent) : QGroupBox(parent), modified(false)
|
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
|
|
|
ui.diveSiteMessage->setCloseButtonVisible(false);
|
|
|
|
ui.diveSiteMessage->show();
|
|
|
|
|
|
|
|
// create the three buttons and only show the close button for now
|
|
|
|
closeAction = new QAction(tr("Close"), this);
|
|
|
|
connect(closeAction, SIGNAL(triggered(bool)), this, SLOT(rejectChanges()));
|
|
|
|
|
|
|
|
acceptAction = new QAction(tr("Apply changes"), this);
|
|
|
|
connect(acceptAction, SIGNAL(triggered(bool)), this, SLOT(acceptChanges()));
|
|
|
|
|
|
|
|
rejectAction = new QAction(tr("Discard changes"), this);
|
|
|
|
connect(rejectAction, SIGNAL(triggered(bool)), this, SLOT(rejectChanges()));
|
|
|
|
|
|
|
|
ui.diveSiteMessage->setText(tr("Dive site management"));
|
|
|
|
ui.diveSiteMessage->addAction(closeAction);
|
|
|
|
|
2015-05-26 20:42:45 +00:00
|
|
|
connect(this, SIGNAL(startFilterDiveSite(uint32_t)), MultiFilterSortModel::instance(), SLOT(startFilterDiveSite(uint32_t)));
|
|
|
|
connect(this, SIGNAL(stopFilterDiveSite()), MultiFilterSortModel::instance(), SLOT(stopFilterDiveSite()));
|
2015-05-17 20:14:23 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 19:58:23 +00:00
|
|
|
void LocationInformationWidget::setCurrentDiveSiteByUuid(uint32_t uuid)
|
2015-05-17 19:13:41 +00:00
|
|
|
{
|
|
|
|
currentDs = get_dive_site_by_uuid(uuid);
|
2015-05-26 20:02:50 +00:00
|
|
|
if(!currentDs)
|
|
|
|
return;
|
2015-05-17 19:13:41 +00:00
|
|
|
|
2015-05-26 20:42:45 +00:00
|
|
|
if (displayed_dive_site.uuid == currentDs->uuid)
|
|
|
|
return;
|
|
|
|
|
2015-05-17 19:13:41 +00:00
|
|
|
displayed_dive_site = *currentDs;
|
2015-05-26 20:42:45 +00:00
|
|
|
|
2015-05-17 19:13:41 +00:00
|
|
|
if (displayed_dive_site.name)
|
|
|
|
ui.diveSiteName->setText(displayed_dive_site.name);
|
|
|
|
else
|
|
|
|
ui.diveSiteName->clear();
|
|
|
|
if (displayed_dive_site.description)
|
|
|
|
ui.diveSiteDescription->setText(displayed_dive_site.description);
|
|
|
|
else
|
|
|
|
ui.diveSiteDescription->clear();
|
|
|
|
if (displayed_dive_site.notes)
|
|
|
|
ui.diveSiteNotes->setPlainText(displayed_dive_site.notes);
|
|
|
|
else
|
|
|
|
ui.diveSiteNotes->clear();
|
|
|
|
if (displayed_dive_site.latitude.udeg || displayed_dive_site.longitude.udeg)
|
|
|
|
ui.diveSiteCoordinates->setText(printGPSCoords(displayed_dive_site.latitude.udeg, displayed_dive_site.longitude.udeg));
|
|
|
|
else
|
|
|
|
ui.diveSiteCoordinates->clear();
|
2015-05-26 20:42:45 +00:00
|
|
|
|
|
|
|
emit startFilterDiveSite(displayed_dive_site.uuid);
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::updateGpsCoordinates()
|
|
|
|
{
|
|
|
|
ui.diveSiteCoordinates->setText(printGPSCoords(displayed_dive_site.latitude.udeg, displayed_dive_site.longitude.udeg));
|
|
|
|
MainWindow::instance()->setApplicationState("EditDiveSite");
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::acceptChanges()
|
|
|
|
{
|
2015-05-26 20:42:45 +00:00
|
|
|
emit stopFilterDiveSite();
|
2015-05-17 19:13:41 +00:00
|
|
|
char *uiString;
|
|
|
|
currentDs->latitude = displayed_dive_site.latitude;
|
|
|
|
currentDs->longitude = displayed_dive_site.longitude;
|
|
|
|
uiString = ui.diveSiteName->text().toUtf8().data();
|
|
|
|
if (!same_string(uiString, currentDs->name)) {
|
|
|
|
free(currentDs->name);
|
|
|
|
currentDs->name = copy_string(uiString);
|
|
|
|
}
|
|
|
|
uiString = ui.diveSiteDescription->text().toUtf8().data();
|
|
|
|
if (!same_string(uiString, currentDs->description)) {
|
|
|
|
free(currentDs->description);
|
|
|
|
currentDs->description = copy_string(uiString);
|
|
|
|
}
|
|
|
|
uiString = ui.diveSiteNotes->document()->toPlainText().toUtf8().data();
|
|
|
|
if (!same_string(uiString, currentDs->notes)) {
|
|
|
|
free(currentDs->notes);
|
|
|
|
currentDs->notes = copy_string(uiString);
|
|
|
|
}
|
|
|
|
if (dive_site_is_empty(currentDs)) {
|
2015-06-02 02:13:51 +00:00
|
|
|
LocationInformationModel::instance()->removeRow(get_divesite_idx(currentDs));
|
2015-05-17 19:13:41 +00:00
|
|
|
displayed_dive.dive_site_uuid = 0;
|
|
|
|
}
|
2015-05-26 19:59:06 +00:00
|
|
|
|
2015-05-17 19:13:41 +00:00
|
|
|
mark_divelist_changed(true);
|
|
|
|
resetState();
|
|
|
|
emit informationManagementEnded();
|
2015-05-22 19:15:53 +00:00
|
|
|
emit coordinatesChanged();
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::rejectChanges()
|
|
|
|
{
|
2015-05-31 20:10:26 +00:00
|
|
|
if (currentDs && dive_site_is_empty(currentDs)) {
|
2015-06-02 02:13:51 +00:00
|
|
|
LocationInformationModel::instance()->removeRow(get_divesite_idx(currentDs));
|
2015-05-17 19:13:41 +00:00
|
|
|
displayed_dive.dive_site_uuid = 0;
|
|
|
|
}
|
|
|
|
resetState();
|
2015-05-27 13:54:00 +00:00
|
|
|
emit stopFilterDiveSite();
|
2015-05-17 19:13:41 +00:00
|
|
|
emit informationManagementEnded();
|
2015-05-22 19:15:53 +00:00
|
|
|
emit coordinatesChanged();
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2015-05-17 20:14:23 +00:00
|
|
|
void LocationInformationWidget::showEvent(QShowEvent *ev)
|
|
|
|
{
|
2015-05-31 20:56:53 +00:00
|
|
|
if (displayed_dive_site.uuid)
|
|
|
|
emit startFilterDiveSite(displayed_dive_site.uuid);
|
2015-05-17 19:13:41 +00:00
|
|
|
ui.diveSiteMessage->setCloseButtonVisible(false);
|
2015-05-17 20:14:23 +00:00
|
|
|
QGroupBox::showEvent(ev);
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::markChangedWidget(QWidget *w)
|
|
|
|
{
|
|
|
|
QPalette p;
|
|
|
|
qreal h, s, l, a;
|
|
|
|
if (!modified)
|
|
|
|
enableEdition();
|
|
|
|
qApp->palette().color(QPalette::Text).getHslF(&h, &s, &l, &a);
|
|
|
|
p.setBrush(QPalette::Base, (l <= 0.3) ? QColor(Qt::yellow).lighter() : (l <= 0.6) ? QColor(Qt::yellow).light() : /* else */ QColor(Qt::yellow).darker(300));
|
|
|
|
w->setPalette(p);
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::resetState()
|
|
|
|
{
|
|
|
|
modified = false;
|
|
|
|
resetPallete();
|
|
|
|
MainWindow::instance()->dive_list()->setEnabled(true);
|
|
|
|
MainWindow::instance()->setEnabledToolbar(true);
|
|
|
|
ui.diveSiteMessage->setText(tr("Dive site management"));
|
|
|
|
ui.diveSiteMessage->addAction(closeAction);
|
|
|
|
ui.diveSiteMessage->removeAction(acceptAction);
|
|
|
|
ui.diveSiteMessage->removeAction(rejectAction);
|
|
|
|
ui.diveSiteMessage->setCloseButtonVisible(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::enableEdition()
|
|
|
|
{
|
|
|
|
MainWindow::instance()->dive_list()->setEnabled(false);
|
|
|
|
MainWindow::instance()->setEnabledToolbar(false);
|
|
|
|
ui.diveSiteMessage->setText(tr("You are editing a dive site"));
|
|
|
|
ui.diveSiteMessage->removeAction(closeAction);
|
|
|
|
ui.diveSiteMessage->addAction(acceptAction);
|
|
|
|
ui.diveSiteMessage->addAction(rejectAction);
|
|
|
|
ui.diveSiteMessage->setCloseButtonVisible(false);
|
2015-05-27 13:31:34 +00:00
|
|
|
if (!currentDs) {
|
|
|
|
displayed_dive.dive_site_uuid = create_dive_site(NULL);
|
|
|
|
currentDs = get_dive_site_by_uuid(displayed_dive.dive_site_uuid);
|
|
|
|
}
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 18:56:03 +00:00
|
|
|
extern bool parseGpsText(const QString &gps_text, double *latitude, double *longitude);
|
|
|
|
|
2015-05-17 19:13:41 +00:00
|
|
|
void LocationInformationWidget::on_diveSiteCoordinates_textChanged(const QString& text)
|
|
|
|
{
|
2015-05-27 13:31:34 +00:00
|
|
|
if (!currentDs || !same_string(qPrintable(text), printGPSCoords(currentDs->latitude.udeg, currentDs->longitude.udeg))) {
|
2015-05-22 18:56:03 +00:00
|
|
|
double latitude, longitude;
|
|
|
|
if (parseGpsText(text, &latitude, &longitude)) {
|
|
|
|
displayed_dive_site.latitude.udeg = latitude * 1000000;
|
|
|
|
displayed_dive_site.longitude.udeg = longitude * 1000000;
|
|
|
|
markChangedWidget(ui.diveSiteCoordinates);
|
2015-05-22 19:15:53 +00:00
|
|
|
emit coordinatesChanged();
|
2015-05-22 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::on_diveSiteDescription_textChanged(const QString& text)
|
|
|
|
{
|
2015-05-27 13:36:19 +00:00
|
|
|
if (!currentDs || !same_string(qPrintable(text), currentDs->description))
|
2015-05-17 19:13:41 +00:00
|
|
|
markChangedWidget(ui.diveSiteDescription);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::on_diveSiteName_textChanged(const QString& text)
|
|
|
|
{
|
2015-06-02 01:16:07 +00:00
|
|
|
if (currentDs && text != currentDs->name) {
|
|
|
|
// This needs to be changed directly into the model so that
|
|
|
|
// the changes are replyed on the ComboBox with the current selection.
|
|
|
|
|
2015-06-02 01:30:33 +00:00
|
|
|
int i;
|
|
|
|
struct dive_site *ds;
|
|
|
|
for_each_dive_site(i,ds)
|
|
|
|
if (ds->uuid == currentDs->uuid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
QModelIndex idx = LocationInformationModel::instance()->index(i,0);
|
2015-06-02 01:16:07 +00:00
|
|
|
LocationInformationModel::instance()->setData(idx, text, Qt::EditRole);
|
2015-05-17 19:13:41 +00:00
|
|
|
markChangedWidget(ui.diveSiteName);
|
2015-05-22 19:28:20 +00:00
|
|
|
emit coordinatesChanged();
|
|
|
|
}
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::on_diveSiteNotes_textChanged()
|
|
|
|
{
|
2015-05-27 13:36:19 +00:00
|
|
|
if (! currentDs || !same_string(qPrintable(ui.diveSiteNotes->toPlainText()), currentDs->notes))
|
2015-05-17 19:13:41 +00:00
|
|
|
markChangedWidget(ui.diveSiteNotes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::resetPallete()
|
|
|
|
{
|
|
|
|
QPalette p;
|
|
|
|
ui.diveSiteCoordinates->setPalette(p);
|
|
|
|
ui.diveSiteDescription->setPalette(p);
|
|
|
|
ui.diveSiteName->setPalette(p);
|
|
|
|
ui.diveSiteNotes->setPalette(p);
|
|
|
|
}
|