~patrick-hetu/+junk/evopedia-app

« back to all changes in this revision

Viewing changes to src/mapwindow.cpp

  • Committer: Patrick Hetu
  • Date: 2013-07-22 02:35:22 UTC
  • Revision ID: patrick.hetu@gmail.com-20130722023522-jrfmj5s6eb3mfdv8
initial test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * evopedia: An offline Wikipedia reader.
 
3
 *
 
4
 * Copyright (C) 2010-2011 evopedia developers
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 */
 
20
 
 
21
#include "mapwindow.h"
 
22
#include "ui_mapwindow.h"
 
23
 
 
24
#ifdef Q_WS_MAEMO_5
 
25
#include <QtGui/QX11Info>
 
26
#include <X11/Xlib.h>
 
27
#include <X11/Xatom.h>
 
28
#endif
 
29
 
 
30
#include <QtWidgets/QMessageBox>
 
31
#include <QTimer>
 
32
 
 
33
MapWindow::MapWindow(QWidget *parent) :
 
34
    QMainWindow(parent),
 
35
#if defined(USE_MOBILITY)
 
36
    posSource(QGeoPositionInfoSource::createDefaultSource(this)),
 
37
#endif
 
38
    ui(new Ui::MapWindow)
 
39
{
 
40
    ui->setupUi(this);
 
41
    ui->mapWidget->setFocus();
 
42
#if defined(USE_MOBILITY)
 
43
    if (posSource != 0)
 
44
        connect(posSource, SIGNAL(positionUpdated(QGeoPositionInfo)), SLOT(positionUpdated(QGeoPositionInfo)));
 
45
#else
 
46
    ui->actionGo_to_GPS_Position->setEnabled(false);
 
47
    ui->actionUse_GPS->setEnabled(false);
 
48
    ui->actionFollow_GPS->setEnabled(false);
 
49
#endif
 
50
 
 
51
#if defined(Q_OS_SYMBIAN)
 
52
    QAction *closeAction = new QAction(tr("Back"), ui->menuMenu);
 
53
    connect(closeAction, SIGNAL(triggered()), SLOT(close()));
 
54
    ui->menuMenu->addAction(closeAction);
 
55
#endif
 
56
 
 
57
    connect(ui->actionShow_Articles, SIGNAL(toggled(bool)), ui->mapWidget, SLOT(overlaysEnable(bool)));
 
58
    connect(ui->actionZoom_In, SIGNAL(triggered()), ui->mapWidget, SLOT(zoomIn()));
 
59
    connect(ui->actionZoom_Out, SIGNAL(triggered()), ui->mapWidget, SLOT(zoomOut()));
 
60
 
 
61
    QTimer::singleShot(0, this, SLOT(delayedInit()));
 
62
}
 
63
 
 
64
MapWindow::~MapWindow()
 
65
{
 
66
    delete ui;
 
67
}
 
68
 
 
69
#ifdef Q_WS_MAEMO_5
 
70
void MapWindow::grabZoomKeys(bool grab)
 
71
{
 
72
    if (!winId()) {
 
73
        qWarning("Can't grab keys unless we have a window id");
 
74
        return;
 
75
    }
 
76
 
 
77
    unsigned long val = (grab) ? 1 : 0;
 
78
    Atom atom = XInternAtom(QX11Info::display(), "_HILDON_ZOOM_KEY_ATOM", False);
 
79
    if (!atom) {
 
80
        qWarning("Unable to obtain _HILDON_ZOOM_KEY_ATOM. This example will only work "
 
81
                 "on a Maemo 5 device!");
 
82
        return;
 
83
    }
 
84
 
 
85
    XChangeProperty (QX11Info::display(),
 
86
                     winId(),
 
87
                     atom,
 
88
                     XA_INTEGER,
 
89
                     32,
 
90
                     PropModeReplace,
 
91
                     reinterpret_cast<unsigned char *>(&val),
 
92
                     1);
 
93
}
 
94
#endif
 
95
 
 
96
 
 
97
void MapWindow::setPosition(qreal lat, qreal lon, int zoom)
 
98
{
 
99
    ui->mapWidget->setPosition(lat, lon, zoom);
 
100
}
 
101
 
 
102
void MapWindow::getPosition(qreal &lat, qreal &lng, int &zoom)
 
103
{
 
104
    ui->mapWidget->getPosition(lat, lng, zoom);
 
105
}
 
106
 
 
107
#if defined(USE_MOBILITY)
 
108
void MapWindow::positionUpdated(const QGeoPositionInfo &posInfo)
 
109
{
 
110
    const QGeoCoordinate coordinate = posInfo.coordinate();
 
111
    if (ui->actionFollow_GPS->isChecked() && coordinate.isValid()) {
 
112
        setPosition(coordinate.latitude(), coordinate.longitude());
 
113
    }
 
114
}
 
115
#endif
 
116
 
 
117
void MapWindow::on_actionUse_GPS_toggled(bool value)
 
118
{
 
119
#if defined(USE_MOBILITY)
 
120
    if (posSource == 0) {
 
121
        QMessageBox::critical(this, tr("Error"), tr("No position info source available."));
 
122
        return;
 
123
    }
 
124
 
 
125
    if (value)
 
126
        posSource->startUpdates();
 
127
    else
 
128
        posSource->stopUpdates();
 
129
    ui->actionFollow_GPS->setEnabled(value);
 
130
    ui->actionGo_to_GPS_Position->setEnabled(value);
 
131
#else
 
132
    Q_UNUSED(value);
 
133
#endif
 
134
}
 
135
 
 
136
void MapWindow::on_actionGo_to_GPS_Position_triggered()
 
137
{
 
138
#if defined(USE_MOBILITY)
 
139
    if (posSource == 0) return;
 
140
    const QGeoCoordinate coordinate = posSource->lastKnownPosition().coordinate();
 
141
    if (coordinate.isValid())
 
142
        setPosition(coordinate.latitude(), coordinate.longitude());
 
143
#endif
 
144
}
 
145
 
 
146
void MapWindow::on_actionFollow_GPS_toggled(bool value)
 
147
{
 
148
    if (value)
 
149
        on_actionGo_to_GPS_Position_triggered();
 
150
}