move key handling to the layers

add layer to show current gps position
This commit is contained in:
Niels 2010-06-22 22:31:29 +02:00
parent 43593f6d4b
commit 2dbc275890
14 changed files with 213 additions and 84 deletions

View File

@ -47,6 +47,11 @@ void AbstractLayer::triggerAction()
{
}
void AbstractLayer::keyPressed(QKeyEvent *event)
{
Q_UNUSED(event)
}
void AbstractLayer::paintLayer(QPainter *painter)
{
if (m_visible) {

View File

@ -21,19 +21,11 @@
#define ABSTRACT_LAYER_H
#include <QtCore/QObject>
#include <QtGui/QKeyEvent>
#include <QtGui/QPainter>
class MapWidget;
enum Layer
{
Tracks,
Marker,
Time,
System,
User
};
class AbstractLayer : public QObject
{
Q_OBJECT
@ -44,6 +36,7 @@ public:
virtual void zoom(int level);
virtual void pan(const QPoint &move);
virtual void triggerAction();
virtual void keyPressed(QKeyEvent *event);
void paintLayer(QPainter *painter);

View File

@ -45,6 +45,14 @@ BatteryLayer::BatteryLayer(MapWidget *map) :
repaint();
}
void BatteryLayer::keyPressed(QKeyEvent *event)
{
if (event->modifiers() == Qt::NoModifier &&
event->key() == Qt::Key_B) {
toggleVisibility();
}
}
void BatteryLayer::paint(QPainter *painter)
{
int w = map()->width();

View File

@ -31,6 +31,8 @@ class BatteryLayer : public AbstractLayer
public:
BatteryLayer(MapWidget *map);
virtual void keyPressed(QKeyEvent *event);
protected:
virtual void paint(QPainter *painter);

83
gpslayer.cpp Normal file
View File

@ -0,0 +1,83 @@
/*
* Copyright 2010 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "gpslayer.h"
#include "gpsclient.h"
#include "mapwidget.h"
#include <QtCore/QPoint>
GpsLayer::GpsLayer(MapWidget *map) :
AbstractLayer(map),
m_gps(new GpsClient(this)),
m_pos(QPointF(9.8, 54))
{
setVisible(false);
connect(m_gps, SIGNAL(position(QPointF)), this, SLOT(position(QPointF)));
connect(m_gps, SIGNAL(connected()), this, SLOT(connected()));
connect(m_gps, SIGNAL(disconnected()), this, SLOT(disconnected()));
m_gps->connectGps();
}
void GpsLayer::triggerAction()
{
if (isVisible()) {
map()->centerOnGeoPos(m_pos.x(), m_pos.y());
}
}
void GpsLayer::keyPressed(QKeyEvent *event)
{
if (event->modifiers() == Qt::NoModifier &&
event->key() == Qt::Key_G) {
triggerAction();
}
}
void GpsLayer::paint(QPainter *painter)
{
QPoint pos = map()->geo2screen(m_pos.x(), m_pos.y());
painter->drawPoint(pos);
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setPen(QPen(QBrush(QColor(0, 0, 255, 110)), 4));
painter->drawEllipse(pos, 8, 8);
}
void GpsLayer::position(const QPointF &pos)
{
m_pos = pos;
if (isVisible()) {
map()->update();
}
}
void GpsLayer::connected()
{
setVisible(true);
map()->update();
}
void GpsLayer::disconnected()
{
setVisible(false);
map()->update();
}

52
gpslayer.h Normal file
View File

@ -0,0 +1,52 @@
/*
* Copyright 2010 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#ifndef GPS_LAYER_H
#define GPS_LAYER_H
#include "abstractlayer.h"
#include <QtGui/QPainter>
class GpsClient;
class GpsLayer : public AbstractLayer
{
Q_OBJECT
public:
GpsLayer(MapWidget *map);
virtual void triggerAction();
virtual void keyPressed(QKeyEvent *event);
protected:
virtual void paint(QPainter *painter);
private slots:
void position(const QPointF &pos);
void connected();
void disconnected();
private:
GpsClient *m_gps;
QPointF m_pos;
};
#endif // GPS_LAYER_H

View File

@ -24,6 +24,7 @@
#include "markerlist.h"
#include "batterylayer.h"
#include "gpslayer.h"
#include "gpxlayer.h"
#include "markerlayer.h"
#include "timelayer.h"
@ -50,17 +51,17 @@ MainWidget::MainWidget(QWidget *parent)
AbstractLayer *l = new TimeLayer(m_map);
l->setVisible(false);
m_map->addLayer(Time, l);
m_map->addLayer(l, 4);
l = new BatteryLayer(m_map);
l->setVisible(false);
m_map->addLayer(System, l);
m_map->addLayer(l, 4);
l = new GpxLayer(m_map);
if (fileName.endsWith(".gpx")) {
l = new GpxLayer(m_map);
l->load(fileName);
m_map->addLayer(l, 2);
}
m_map->addLayer(Tracks, l);
l = new MarkerLayer(m_map);
connect(l, SIGNAL(markerAdded(QString)), m_markerList, SLOT(addMarker(QString)));
@ -68,7 +69,10 @@ MainWidget::MainWidget(QWidget *parent)
connect(m_markerList, SIGNAL(removeMarker(int)), l, SLOT(removeMarker(int)));
connect(m_markerList, SIGNAL(markerRenamed(int, QString)), l, SLOT(renameMarker(int, QString)));
l->load(QDir::homePath()+"/Maps/marker.list");
m_map->addLayer(Marker, l);
m_map->addLayer(l, 3);
l = new GpsLayer(m_map);
m_map->addLayer(l, 1);
connect(m_map, SIGNAL(showMarkerList()), this, SLOT(showList()));
connect(m_map, SIGNAL(downloadArea(int, QRectF)), this, SLOT(downloadArea(int, QRectF)));

View File

@ -131,9 +131,9 @@ MapWidget::~MapWidget()
}
}
void MapWidget::addLayer(Layer l, AbstractLayer *layer)
void MapWidget::addLayer(AbstractLayer *layer, int z)
{
m_layer.insert(l, layer);
m_layer.insertMulti(z, layer);
}
void MapWidget::resizeEvent(QResizeEvent *event)
@ -160,6 +160,8 @@ void MapWidget::resizeEvent(QResizeEvent *event)
void MapWidget::mouseMoveEvent(QMouseEvent *event)
{
event->accept();
if (m_isMoving) {
foreach (AbstractLayer *l, m_layer) {
l->pan((event->pos() - m_startPos) - m_pos);
@ -171,6 +173,8 @@ void MapWidget::mouseMoveEvent(QMouseEvent *event)
void MapWidget::mousePressEvent(QMouseEvent *event)
{
event->accept();
if (m_ui && QRect(9, 14, 13, 13).contains(event->pos())) {
changeZoomLevel(1);
reloadPixmaps();
@ -189,13 +193,15 @@ void MapWidget::mousePressEvent(QMouseEvent *event)
void MapWidget::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event)
event->accept();
m_isMoving = false;
}
void MapWidget::wheelEvent(QWheelEvent *event)
{
event->accept();
if (event->delta() < 0) {
changeZoomLevel(-1);
reloadPixmaps();
@ -208,6 +214,8 @@ void MapWidget::wheelEvent(QWheelEvent *event)
void MapWidget::keyPressEvent(QKeyEvent *event)
{
event->accept();
QPoint move;
int width = 10;
if (event->modifiers() & Qt::AltModifier) {
@ -221,16 +229,9 @@ void MapWidget::keyPressEvent(QKeyEvent *event)
emit showMarkerList();
break;
}
case Qt::Key_M:
case Qt::Key_D:
{
AbstractLayer *l = m_layer.value(Marker);
if (l) {
if (event->modifiers() & Qt::AltModifier) {
l->toggleVisibility();
} else if (event->modifiers() == Qt::NoModifier) {
l->triggerAction();
}
}
emit downloadArea(m_level, geoRect());
break;
}
case Qt::Key_Up:
@ -253,15 +254,6 @@ void MapWidget::keyPressEvent(QKeyEvent *event)
move = QPoint(-width, 0);
break;
}
case Qt::Key_C:
{
m_indexX = (m_minIndexX + m_maxIndexX) / 2;
m_indexY = (m_minIndexY + m_maxIndexY) / 2;
m_pos.setX(m_pixWidth/2);
m_pos.setY(m_pixHeight/2);
reloadPixmaps();
break;
}
case Qt::Key_O:
{
changeZoomLevel(-1);
@ -274,30 +266,14 @@ void MapWidget::keyPressEvent(QKeyEvent *event)
reloadPixmaps();
break;
}
case Qt::Key_D:
{
emit downloadArea(m_level, geoRect());
break;
}
case Qt::Key_U:
{
m_ui = !m_ui;
break;
}
case Qt::Key_T:
case Qt::Key_H:
{
AbstractLayer *l = m_layer.value(Time);
if (l) {
l->toggleVisibility();
}
break;
}
case Qt::Key_B:
{
AbstractLayer *l = m_layer.value(System);
if (l) {
l->toggleVisibility();
}
m_usage = !m_usage;
break;
}
case Qt::Key_Q:
@ -306,13 +282,12 @@ void MapWidget::keyPressEvent(QKeyEvent *event)
qApp->quit();
break;
}
case Qt::Key_Question:
case Qt::Key_H:
{
m_usage = !m_usage;
break;
}
}
foreach (AbstractLayer *l, m_layer) {
l->keyPressed(event);
}
m_pos += move;
foreach (AbstractLayer *l, m_layer) {
l->pan(move);
@ -322,9 +297,7 @@ void MapWidget::keyPressEvent(QKeyEvent *event)
void MapWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
AbstractLayer *l = 0;
event->accept();
QPainter painter(this);
@ -338,24 +311,10 @@ void MapWidget::paintEvent(QPaintEvent *event)
}
}
l = m_layer.value(Tracks);
if (l) {
l->paintLayer(&painter);
}
l = m_layer.value(Marker);
if (l) {
l->paintLayer(&painter);
}
l = m_layer.value(Time);
if (l) {
l->paintLayer(&painter);
}
l = m_layer.value(System);
if (l) {
l->paintLayer(&painter);
QMapIterator<int, AbstractLayer *> i(m_layer);
while (i.hasNext()) {
i.next();
i.value()->paintLayer(&painter);
}
if (m_ui) {
@ -395,7 +354,6 @@ void MapWidget::paintEvent(QPaintEvent *event)
usage << "Esc: Quit application";
usage << "h: Show/hide this message";
usage << "Arrows: Move the map";
//usage << "c: Move to the center of the map";
if (m_zoomable) {
usage << "i: Zoom in";
usage << "o: Zoom out";

View File

@ -22,7 +22,7 @@
#include "abstractlayer.h"
#include <QtCore/QHash>
#include <QtCore/QMap>
#include <QtCore/QPoint>
#include <QtGui/QWidget>
#include <QtNetwork/QNetworkAccessManager>
@ -34,7 +34,7 @@ public:
MapWidget(QWidget *parent = 0);
~MapWidget();
void addLayer(Layer l, AbstractLayer *layer);
void addLayer(AbstractLayer *layer, int z);
QRectF geoRect() const;
QPointF geoPos() const;
@ -89,7 +89,7 @@ private:
QNetworkAccessManager *m_manager;
bool m_networkMode;
QString m_copyright;
QHash<Layer, AbstractLayer *> m_layer;
QMap<int, AbstractLayer *> m_layer;
};

View File

@ -73,6 +73,17 @@ void MarkerLayer::triggerAction()
emit markerAdded(newName);
}
void MarkerLayer::keyPressed(QKeyEvent *event)
{
if (event->key() == Qt::Key_M) {
if (event->modifiers() == Qt::NoModifier) {
triggerAction();
} else if (event->modifiers() == Qt::AltModifier) {
toggleVisibility();
}
}
}
void MarkerLayer::paint(QPainter *painter)
{
int i = 0;

View File

@ -33,6 +33,7 @@ public:
virtual void load(const QString &filename);
virtual void triggerAction();
virtual void keyPressed(QKeyEvent *event);
protected:
virtual void paint(QPainter *painter);

View File

@ -7,6 +7,7 @@ SOURCES += main.cpp \
mainwidget.cpp \
projection.cpp \
abstractlayer.cpp \
gpslayer.cpp \
markerlayer.cpp \
gpxlayer.cpp \
timelayer.cpp \
@ -19,6 +20,7 @@ SOURCES += main.cpp \
HEADERS += mainwidget.h \
projection.h \
abstractlayer.h \
gpslayer.h \
markerlayer.h \
gpxlayer.h \
timelayer.h \

View File

@ -34,6 +34,14 @@ TimeLayer::TimeLayer(MapWidget *map) :
QTimer::singleShot(time, m_updateTimer, SLOT(start()));
}
void TimeLayer::keyPressed(QKeyEvent *event)
{
if (event->modifiers() == Qt::NoModifier &&
event->key() == Qt::Key_T) {
toggleVisibility();
}
}
void TimeLayer::paint(QPainter *painter)
{
int w = map()->width();

View File

@ -31,6 +31,8 @@ class TimeLayer : public AbstractLayer
public:
TimeLayer(MapWidget *map);
virtual void keyPressed(QKeyEvent *event);
protected:
virtual void paint(QPainter *painter);