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-08-25 21:45:29 +00:00
|
|
|
#include "divesitehelpers.h"
|
|
|
|
|
2015-05-17 19:13:41 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QShowEvent>
|
2015-09-01 00:11:28 +00:00
|
|
|
#include <QItemSelectionModel>
|
2015-09-01 00:35:17 +00:00
|
|
|
#include <cstdlib>
|
2015-05-17 19:13:41 +00:00
|
|
|
|
|
|
|
LocationInformationWidget::LocationInformationWidget(QWidget *parent) : QGroupBox(parent), modified(false)
|
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
|
|
|
ui.diveSiteMessage->setCloseButtonVisible(false);
|
|
|
|
|
|
|
|
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"));
|
2015-06-04 03:18:25 +00:00
|
|
|
ui.diveSiteMessage->addAction(acceptAction);
|
|
|
|
ui.diveSiteMessage->addAction(rejectAction);
|
2015-05-17 19:13:41 +00:00
|
|
|
|
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-08-25 21:45:29 +00:00
|
|
|
connect(ui.geoCodeButton, SIGNAL(clicked()), this, SLOT(reverseGeocode()));
|
2015-07-31 01:10:01 +00:00
|
|
|
|
2015-08-31 23:59:13 +00:00
|
|
|
SsrfSortFilterProxyModel *filter_model = new SsrfSortFilterProxyModel(this);
|
|
|
|
filter_model->setSourceModel(LocationInformationModel::instance());
|
|
|
|
filter_model->setFilterRow(filter_same_gps_cb);
|
|
|
|
ui.diveSiteListView->setModel(filter_model);
|
2015-09-01 00:03:31 +00:00
|
|
|
ui.diveSiteListView->setModelColumn(LocationInformationModel::NAME);
|
2015-09-01 00:11:28 +00:00
|
|
|
ui.diveSiteListView->installEventFilter(this);
|
2015-07-31 18:15:35 +00:00
|
|
|
#ifndef NO_MARBLE
|
2015-07-31 01:10:01 +00:00
|
|
|
// Globe Management Code.
|
|
|
|
connect(this, &LocationInformationWidget::requestCoordinates,
|
|
|
|
GlobeGPS::instance(), &GlobeGPS::prepareForGetDiveCoordinates);
|
|
|
|
connect(this, &LocationInformationWidget::endRequestCoordinates,
|
|
|
|
GlobeGPS::instance(), &GlobeGPS::endGetDiveCoordinates);
|
|
|
|
connect(GlobeGPS::instance(), &GlobeGPS::coordinatesChanged,
|
|
|
|
this, &LocationInformationWidget::updateGpsCoordinates);
|
2015-07-31 01:47:17 +00:00
|
|
|
connect(this, &LocationInformationWidget::endEditDiveSite,
|
|
|
|
GlobeGPS::instance(), &GlobeGPS::repopulateLabels);
|
2015-07-31 18:15:35 +00:00
|
|
|
#endif
|
2015-05-17 20:14:23 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 00:11:28 +00:00
|
|
|
bool LocationInformationWidget::eventFilter(QObject*, QEvent *ev)
|
|
|
|
{
|
|
|
|
if( ev->type() == QEvent::ContextMenu ) {
|
|
|
|
if (ui.diveSiteListView->selectionModel()->selectedIndexes().count() >= 2) {
|
|
|
|
QContextMenuEvent *ctx = (QContextMenuEvent*) ev;
|
|
|
|
QMenu contextMenu;
|
2015-09-01 00:35:17 +00:00
|
|
|
contextMenu.addAction(tr("Merge dive Sites"), this, SLOT(mergeSelectedDiveSites()));
|
2015-09-01 00:11:28 +00:00
|
|
|
contextMenu.exec(ctx->globalPos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:35:17 +00:00
|
|
|
void LocationInformationWidget::mergeSelectedDiveSites() {
|
|
|
|
QModelIndexList selection = ui.diveSiteListView->selectionModel()->selectedIndexes();
|
|
|
|
uint32_t *selected_dive_sites = (uint32_t*) malloc(sizeof(u_int32_t) * selection.count());
|
|
|
|
int i = 0;
|
|
|
|
Q_FOREACH(const QModelIndex& idx, selection) {
|
|
|
|
selected_dive_sites[i] = (uint32_t) idx.data(LocationInformationModel::UUID_ROLE).toInt();
|
|
|
|
}
|
2015-09-01 00:45:31 +00:00
|
|
|
merge_dive_sites(displayed_dive_site.uuid, selected_dive_sites, i);
|
2015-09-01 00:35:17 +00:00
|
|
|
free(selected_dive_sites);
|
|
|
|
}
|
|
|
|
|
2015-07-25 16:03:14 +00:00
|
|
|
void LocationInformationWidget::updateLabels()
|
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
|
|
|
|
2015-07-25 15:56:48 +00:00
|
|
|
emit startFilterDiveSite(displayed_dive_site.uuid);
|
2015-07-25 16:03:14 +00:00
|
|
|
emit startEditDiveSite(displayed_dive_site.uuid);
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::updateGpsCoordinates()
|
|
|
|
{
|
2015-07-31 01:41:02 +00:00
|
|
|
QString oldText = ui.diveSiteCoordinates->text();
|
2015-05-17 19:13:41 +00:00
|
|
|
ui.diveSiteCoordinates->setText(printGPSCoords(displayed_dive_site.latitude.udeg, displayed_dive_site.longitude.udeg));
|
2015-07-31 01:41:02 +00:00
|
|
|
if (oldText != ui.diveSiteCoordinates->text())
|
|
|
|
markChangedWidget(ui.diveSiteCoordinates);
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::acceptChanges()
|
|
|
|
{
|
2015-05-26 20:42:45 +00:00
|
|
|
emit stopFilterDiveSite();
|
2015-05-17 19:13:41 +00:00
|
|
|
char *uiString;
|
2015-08-29 23:00:22 +00:00
|
|
|
struct dive_site *currentDs;
|
|
|
|
uiString = ui.diveSiteName->text().toUtf8().data();
|
|
|
|
|
|
|
|
if (get_dive_site_by_uuid(displayed_dive_site.uuid) != NULL)
|
|
|
|
currentDs = get_dive_site_by_uuid(displayed_dive_site.uuid);
|
|
|
|
else
|
|
|
|
currentDs = get_dive_site_by_uuid(create_dive_site_from_current_dive(uiString));
|
|
|
|
|
2015-05-17 19:13:41 +00:00
|
|
|
currentDs->latitude = displayed_dive_site.latitude;
|
|
|
|
currentDs->longitude = displayed_dive_site.longitude;
|
|
|
|
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);
|
|
|
|
}
|
2015-07-31 01:38:28 +00:00
|
|
|
if (!ui.diveSiteCoordinates->text().isEmpty()) {
|
|
|
|
double lat, lon;
|
|
|
|
parseGpsText(ui.diveSiteCoordinates->text(), &lat, &lon);
|
|
|
|
currentDs->latitude.udeg = lat * 1000000.0;
|
|
|
|
currentDs->longitude.udeg = lon * 1000000.0;
|
|
|
|
}
|
2015-05-17 19:13:41 +00:00
|
|
|
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-08-29 23:00:22 +00:00
|
|
|
copy_dive_site(currentDs, &displayed_dive_site);
|
2015-05-17 19:13:41 +00:00
|
|
|
mark_divelist_changed(true);
|
|
|
|
resetState();
|
2015-07-31 01:10:01 +00:00
|
|
|
emit endRequestCoordinates();
|
2015-07-31 00:18:57 +00:00
|
|
|
emit endEditDiveSite();
|
2015-05-22 19:15:53 +00:00
|
|
|
emit coordinatesChanged();
|
2015-05-17 19:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::rejectChanges()
|
|
|
|
{
|
|
|
|
resetState();
|
2015-07-31 01:10:01 +00:00
|
|
|
emit endRequestCoordinates();
|
2015-05-27 13:54:00 +00:00
|
|
|
emit stopFilterDiveSite();
|
2015-07-31 00:18:57 +00:00
|
|
|
emit endEditDiveSite();
|
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-07-31 01:10:01 +00:00
|
|
|
if (displayed_dive_site.uuid) {
|
2015-07-25 16:03:14 +00:00
|
|
|
updateLabels();
|
2015-08-31 23:59:13 +00:00
|
|
|
QSortFilterProxyModel *m = qobject_cast<QSortFilterProxyModel*>(ui.diveSiteListView->model());
|
2015-05-31 20:56:53 +00:00
|
|
|
emit startFilterDiveSite(displayed_dive_site.uuid);
|
2015-08-31 23:59:13 +00:00
|
|
|
if (m)
|
|
|
|
m->invalidate();
|
2015-07-31 01:10:01 +00:00
|
|
|
}
|
|
|
|
emit requestCoordinates();
|
|
|
|
|
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);
|
2015-07-25 20:12:57 +00:00
|
|
|
p.setBrush(QPalette::Base, (l <= 0.3) ? QColor(Qt::yellow).lighter()
|
|
|
|
: (l <= 0.6) ? QColor(Qt::yellow).light()
|
|
|
|
: /* else */ QColor(Qt::yellow).darker(300));
|
2015-05-17 19:13:41 +00:00
|
|
|
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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::enableEdition()
|
|
|
|
{
|
|
|
|
MainWindow::instance()->dive_list()->setEnabled(false);
|
|
|
|
MainWindow::instance()->setEnabledToolbar(false);
|
|
|
|
ui.diveSiteMessage->setText(tr("You are editing a dive site"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::on_diveSiteCoordinates_textChanged(const QString& text)
|
|
|
|
{
|
2015-07-25 15:56:48 +00:00
|
|
|
uint lat = displayed_dive_site.latitude.udeg;
|
|
|
|
uint lon = displayed_dive_site.longitude.udeg;
|
|
|
|
if (!same_string(qPrintable(text), printGPSCoords(lat, lon))) {
|
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-07-25 15:56:48 +00:00
|
|
|
if (!same_string(qPrintable(text), displayed_dive_site.description))
|
2015-05-17 19:13:41 +00:00
|
|
|
markChangedWidget(ui.diveSiteDescription);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::on_diveSiteName_textChanged(const QString& text)
|
|
|
|
{
|
2015-07-25 15:56:48 +00:00
|
|
|
if (!same_string(qPrintable(text), displayed_dive_site.name))
|
2015-05-17 19:13:41 +00:00
|
|
|
markChangedWidget(ui.diveSiteName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationInformationWidget::on_diveSiteNotes_textChanged()
|
|
|
|
{
|
2015-07-25 15:56:48 +00:00
|
|
|
if (!same_string(qPrintable(ui.diveSiteNotes->toPlainText()), displayed_dive_site.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);
|
|
|
|
}
|
2015-06-26 16:23:53 +00:00
|
|
|
|
2015-07-08 17:27:17 +00:00
|
|
|
bool LocationManagementEditHelper::eventFilter(QObject *obj, QEvent *ev)
|
|
|
|
{
|
|
|
|
QListView *view = qobject_cast<QListView*>(obj);
|
|
|
|
if(!view)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(ev->type() == QEvent::Show) {
|
2015-07-13 18:14:46 +00:00
|
|
|
last_uuid = 0;
|
|
|
|
qDebug() << "EventFilter: " << last_uuid;
|
2015-07-08 17:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(ev->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent *keyEv = (QKeyEvent*) ev;
|
2015-07-18 20:39:19 +00:00
|
|
|
if(keyEv->key() == Qt::Key_Return) {
|
2015-07-08 17:27:17 +00:00
|
|
|
handleActivation(view->currentIndex());
|
2015-07-17 17:08:01 +00:00
|
|
|
view->hide();
|
|
|
|
return true;
|
2015-07-08 17:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationManagementEditHelper::handleActivation(const QModelIndex& activated)
|
|
|
|
{
|
|
|
|
if (!activated.isValid())
|
|
|
|
return;
|
|
|
|
QModelIndex uuidIdx = activated.model()->index(
|
|
|
|
activated.row(), LocationInformationModel::UUID);
|
|
|
|
last_uuid = uuidIdx.data().toInt();
|
2015-07-14 22:09:37 +00:00
|
|
|
|
2015-08-26 00:03:20 +00:00
|
|
|
/* if we are in 'recently added divesite mode, create a new divesite,
|
|
|
|
* and go to dive site edit edit mode. */
|
|
|
|
if (last_uuid == RECENTLY_ADDED_DIVESITE) {
|
2015-08-29 23:00:22 +00:00
|
|
|
uint32_t ds_uuid = create_dive_site_from_current_dive(qPrintable(activated.data().toString()));
|
2015-08-26 00:03:20 +00:00
|
|
|
qDebug() << "ds_uuid" << ds_uuid;
|
|
|
|
struct dive_site *ds = get_dive_site_by_uuid(ds_uuid);
|
|
|
|
copy_dive_site(ds, &displayed_dive_site);
|
|
|
|
displayed_dive.dive_site_uuid = ds->uuid;
|
|
|
|
last_uuid = ds->uuid;
|
|
|
|
// Move this out of here later.
|
|
|
|
MainWindow::instance()->startDiveSiteEdit();
|
2015-07-14 22:09:37 +00:00
|
|
|
}
|
2015-08-26 00:03:20 +00:00
|
|
|
|
2015-07-08 17:27:17 +00:00
|
|
|
qDebug() << "Selected dive_site: " << last_uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationManagementEditHelper::resetDiveSiteUuid() {
|
2015-07-13 18:14:46 +00:00
|
|
|
last_uuid = 0;
|
|
|
|
qDebug() << "Reset: " << last_uuid;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LocationManagementEditHelper::diveSiteUuid() const {
|
|
|
|
return last_uuid;
|
2015-07-08 17:27:17 +00:00
|
|
|
}
|
2015-08-25 21:45:29 +00:00
|
|
|
|
|
|
|
void LocationInformationWidget::reverseGeocode()
|
|
|
|
{
|
|
|
|
ReverseGeoLookupThread *geoLookup = ReverseGeoLookupThread::instance();
|
|
|
|
geoLookup->lookup(&displayed_dive_site);
|
|
|
|
updateLabels();
|
|
|
|
}
|