1 /****************************************************************************
3 ** Copyright (C) 2011 Tito Eritja Real <jtitoo@gmail.com>
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ****************************************************************************/
20 #include "windowMap.h"
25 #include <QCloseEvent>
27 #include <QAbstractItemView>
28 #include <QAbstractItemModel>
29 #include <QFileDialog>
31 #include <QMessageBox>
32 #include <QProgressBar>
34 //#include "satellitedialog.h"
38 WindowMap::WindowMap(QWidget *parent, Settings* settings_p, Log *log)
39 : QMainWindow(parent),
40 //satelliteInfo(satelliteInfo),
42 log(log),pressed(false),snapped(false), settings(settings_p),track(0),trackToSniff(0),network(false),slippyMap(0),
43 //waitingForFix(false),
45 validPositions(0),invalidPositions(0),mode(Mode_NewActivityMode),possitionPaint(true),nSession(0),location(0){
48 trackToSniff = new Track();
49 trackToSimulate = QList<GpsPoint*>();
50 settings->setIsConfigured(false);
52 settingsWindow = new SettingsWindow(this,settings,log);
53 settingsWindow->hide();
55 loadTracksWindow = new LoadTracksWindow(this,settings,log);
56 loadTracksWindow->hide();
58 infoWindow = new ActivityInfo(this,settings,log);
61 //log->debug("tracks created");
64 autosaveTimer = new QTimer(this);
65 sPossitioningTimer = new QTimer(this);
66 simulatePossitioningTimer = new QTimer(this);
70 zoomInButton = new QToolButton(this);
71 zoomInButton->setIcon(QIcon(":/images/Zoom-In-icon.png"));
72 zoomInButton->adjustSize();
73 //log->debug(QString("width():%1, zoomInButton.width():%2").arg(width()).arg(zoomInButton->width()));
75 zoomOutButton = new QToolButton(this);
76 zoomOutButton->setIcon(QIcon(":/images/Zoom-Out-icon.png"));
77 zoomOutButton->adjustSize();
80 zoomOutButton->hide();
82 connect(zoomInButton,SIGNAL(clicked()),this,SLOT(onZoomIn()));
83 connect(zoomOutButton,SIGNAL(clicked()),this,SLOT(onZoomOut()));
85 connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(autosave()));
86 connect(sPossitioningTimer, SIGNAL(timeout()), this, SLOT(activatePossitionPaint()));
87 connect(simulatePossitioningTimer, SIGNAL(timeout()), this,SLOT(simulatedPossitionUpdated()));
89 sPossitioningTimer->setInterval(1000*TIME_SECONDS_POSSITIONING_PAN);
93 //log->debug("going to create actions");
95 loadTrackAction = new QAction(tr("Load track"),this);
96 downloadMapsTrackAction = new QAction(tr("Download maps from track"),this);
97 newActivityAction = new QAction(tr("New activity"),this);
98 simulatedActivityAction = new QAction(tr("Activity simulation"),this);
99 infoAction = new QAction(tr("Activity info"),this);
100 quitAction = new QAction(tr("Quit"), this);
102 lapAction = new QAction(tr("New lap"), this);
103 settingsAction = new QAction(tr("Settings"), this);
104 stopAction = new QAction(tr("Stop"), this);
105 startAction = new QAction(tr("Start"), this);
106 startAction->setEnabled(false);
107 tracksAction = new QAction(tr("Load track"), this);
108 //satelliteAction = new QAction(tr("Satellite info."), this);
109 startNetworkAction = new QAction(tr("Start Networking"),this);
110 startNetworkAction->setEnabled(false);
112 //log->debug("done, connecting");
114 connect(loadTrackAction, SIGNAL(triggered()),SLOT(loadTrack()));
115 connect(downloadMapsTrackAction, SIGNAL(triggered()),SLOT(downloadMapTrack()));
116 connect(newActivityAction, SIGNAL(triggered()),SLOT(newActivityTrack()));
117 connect(simulatedActivityAction, SIGNAL(triggered()), SLOT(newSimulation()));
118 connect(stopAction, SIGNAL(triggered()), SLOT(stopActivity()));
119 connect(startAction, SIGNAL(triggered()), SLOT(startActivity()));
120 connect(quitAction, SIGNAL(triggered()), SLOT(quit()));
121 connect(lapAction, SIGNAL(triggered()), SLOT(lapActivity()));
122 connect(settingsAction, SIGNAL(triggered()), SLOT(configureSettings()));
123 connect(tracksAction, SIGNAL(triggered()), SLOT(tracksActivity()));
124 //connect(satelliteAction, SIGNAL(triggered()), SLOT(satellite()));
125 connect(startNetworkAction,SIGNAL(triggered()), SLOT(networkStart()));
126 connect(infoAction, SIGNAL(triggered()), SLOT(info()));
129 //log->debug("done!");
131 //log->debug("going to show buttons...");
132 //log->debug("done?");
134 this->setFixedSize(WIDTH_DEFAULT,HEIGHT_DEFAULT);
135 //parent->window()->setFixedSize(WIDTH_DEFAULT,HEIGHT_DEFAULT);
136 downloadingStatus = new DownloadingStatus(this);
137 downloadingStatus->hide();
144 WindowMap::~WindowMap()
146 //log->debug("windowMap destructor");
150 //log->debug("session closed");
152 location->stopUpdates();
154 //log->debug("location closed");
158 void WindowMap::setTrack(QString fileName,QString activity){
160 track = new Track(fileName,activity);
161 //log->debug(QString("track created!!!:%1").arg(track->toString()));
164 void WindowMap::setMenuLoadTracks(){
166 //log->debug("MenuLoadTracks!!!!");
169 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
171 menuBar()->addAction(loadTrackAction);
172 menuBar()->addAction(downloadMapsTrackAction);
173 menuBar()->addAction(newActivityAction);
174 menuBar()->addAction(simulatedActivityAction);
175 menuBar()->addAction(startNetworkAction);
176 menuBar()->addAction(infoAction);
177 menuBar()->addAction(quitAction);
181 QMenu *menu = menuBar()->addMenu("&Options");
183 menu->addAction(loadTrackAction);
184 menu->addAction(downloadMapsTrackAction);
185 menu->addAction(newActivityAction);
186 menu->addAction(simulatedActivityAction);
187 menu->addAction(startNetworkAction);
188 menu->addAction(infoAction);
189 menu->addAction(quitAction);
194 void WindowMap::setMenuActivity(){
197 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
198 menuBar()->addAction(lapAction);
199 menuBar()->addAction(settingsAction);
200 menuBar()->addAction(startAction);
201 menuBar()->addAction(stopAction);
202 menuBar()->addAction(startNetworkAction);
203 menuBar()->addAction(tracksAction);
204 menuBar()->addAction(infoAction);
205 menuBar()->addAction(quitAction);
208 //menuBar()->addAction(satelliteAction);
211 QMenu *menu = menuBar()->addMenu("&Options");
213 menu->addAction(lapAction);
214 menu->addAction(settingsAction);
215 menu->addAction(stopAction);
216 menu->addAction(startAction);
217 menu->addAction(tracksAction);
218 menu->addAction(startNetworkAction);
219 //menu->addAction(satelliteAction);
220 menu->addAction(infoAction);
221 menu->addAction(quitAction);
227 void WindowMap::activatePossitionPaint(){
228 //log->debug("activating possitionPaint");
230 sPossitioningTimer->stop();
232 void WindowMap::desactivatePossitionPaint(){
233 //log->debug("desactivating possition paint, and starting timer");
234 possitionPaint=false;
235 sPossitioningTimer->start();
238 void WindowMap::clearAllMaps(){
240 slippyMap->clearAllMaps();
245 void WindowMap::startServices(){
248 //log->debug("--- startServices ---");
249 if(settings->getIsOnline()){
250 //log->debug("Is online?" + settings->getIsOnline());
251 //log->debug("going to start networking...");
255 //log->debug("going offline");
256 startNetworkAction->setEnabled(true);
262 void WindowMap::showSatelliteInfo(){
265 satelliteInfo = QGeoSatelliteInfoSource::createDefaultSource(this);
268 SatelliteDialog *dialog = new SatelliteDialog(this,
270 SatelliteDialog::ExitOnCancel,
271 SatelliteDialog::OrderByPrnNumber,
272 SatelliteDialog::ScaleToMaxPossible);
274 dialog->connectSources(location, satelliteInfo);
276 //location->startUpdates();
277 satelliteInfo->startUpdates();
281 //satelliteInfo->stopUpdates();
284 //delete satelliteInfo;
291 void WindowMap::waitForFix() {
295 waitingForFix = true;
297 satelliteInfo = QGeoSatelliteInfoSource::createDefaultSource(this);
300 satelliteDialog = new SatelliteDialog(this,
302 SatelliteDialog::ExitOnCancel,
303 SatelliteDialog::OrderByPrnNumber,
304 SatelliteDialog::ScaleToMaxPossible);
306 connect(satelliteDialog,SIGNAL(rejected()),satelliteDialog,SLOT(hide()));
308 satelliteDialog->connectSources(location, satelliteInfo);
310 satelliteInfo->startUpdates();
312 satelliteDialog->exec();
314 //satelliteInfo->stopUpdates();
317 //delete satelliteInfo;
320 waitingForFix = false;
325 void WindowMap::stopNetworking(){
330 slippyMap->setNetwork(false);
333 void WindowMap::startNetworking(){
335 if(!network && ((mode==Mode_NewActivityMode && settings->getIsOnline()) || (mode==Mode_LoadTracksWindow))){
338 // Set Internet Access Point
339 QNetworkConfigurationManager manager;
340 const bool canStartIAP = (manager.capabilities()
341 & QNetworkConfigurationManager::CanStartAndStopInterfaces);
343 #ifdef BEARER_IN_QTNETWORK
344 QNetworkConfiguration cfg = manager.defaultConfiguration();
345 if (!cfg.isValid() || (!canStartIAP && cfg.state() != QNetworkConfiguration::Active)) {
347 QTM_PREPEND_NAMESPACE(QNetworkConfiguration) cfg = manager.defaultConfiguration();
348 if (!cfg.isValid() || (!canStartIAP && cfg.state() != QTM_PREPEND_NAMESPACE(QNetworkConfiguration)::Active)) {
351 QMessageBox::information(this, tr("Network"), tr(
352 "No Access Point found."));
357 nSession = new QNetworkSession(cfg, this);
358 connect(nSession,SIGNAL(error(QNetworkSession::SessionError)),this,SLOT(error(QNetworkSession::SessionError)));
361 if(nSession->waitForOpened(-1)){
362 log->info("session connected");
364 connect(nSession, SIGNAL(opened()), this, SLOT(networkSessionOpened()));
365 //connect(nSession,SIGNAL(error(QNetworkSession::SessionError)),this,SLOT(error(QNetworkSession::SessionError)));
369 slippyMap->setNetwork(true);
372 log->err(nSession->errorString());
380 void WindowMap::networkSessionOpened(){
382 //log->debug("--- signal recived of sesson opened ---");
385 //log->debug("invalidating map");
386 slippyMap->setNetwork(true);
387 slippyMap->updateTiles();
392 void WindowMap::error(QNetworkSession::SessionError error)
394 //log->debug("--- Error in Networking!!! ---");
395 //log->debug(QString("settings is configured?%1").arg(settings->getIsConfigured()));
397 if(mode==Mode_NewActivityMode && (!settings->getIsOnline() || !settings->getIsConfigured()))
400 QMessageBox msgBox(this);
402 msgBox.setText(tr("Downloading maps requires network access!."));
403 msgBox.setInformativeText(tr("Try to connect or cancel to disable maps."));
404 msgBox.setIcon(QMessageBox::Information);
405 msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
406 msgBox.setDefaultButton(QMessageBox::Retry);
408 int ret = msgBox.exec();
409 if (ret == QMessageBox::Retry) {
410 QTimer::singleShot(0, nSession, SLOT(open()));
411 } else if (ret == QMessageBox::Cancel) {
412 if(mode==Mode_NewActivityMode){
414 settings->setIsOnline(false);
415 //log->debug("enabling startNetworking");
416 startNetworkAction->setEnabled(true);
420 // QTimer::singleShot(60000, nSession, SLOT(open()));
424 slippyMap->setNetwork(false);
428 void WindowMap::startMaps(){
431 //log->debug(QString("network:%1").arg(network));
432 slippyMap = new TilesMap(nSession, this, log, zoom,settings->getMapType());
433 connect(slippyMap, SIGNAL(updated(QRect)), SLOT(updateMap(QRect)));
434 slippyMap->setWidth(width());
435 slippyMap->setHeight(height());
436 slippyMap->setNetwork(network);
437 //log->debug(QString("START MAPS!!! slippy.network:%1").arg(slippyMap->network));
440 void WindowMap::changeUpdateInterval(int millis){
443 location->setUpdateInterval(millis);
445 simulatePossitioningTimer->setInterval(millis);
448 void WindowMap::changeMapType(MapType map){
449 slippyMap->setMapType(map);
452 bool WindowMap::checkGPS(){
454 //#if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
455 location = QGeoPositionInfoSource::createDefaultSource(this);
458 //With maemo simulator doesn't work, I've to change this.
464 QMessageBox::information(this, tr("No GPS detected"),
465 tr("No GPS support detected, going to simulate mode"));
471 void WindowMap::startGPS(){
474 if(location && !simulateGPS){
475 location->setUpdateInterval(settings->getGpsInterval());
476 connect(location,SIGNAL(positionUpdated(QGeoPositionInfo)),this,SLOT(positionUpdated(QGeoPositionInfo)));
477 if(!settings->getIsOnline())
478 location->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
479 location->startUpdates();
481 //log->debug("setting signal updateTimeout...");
482 //connect(location, SIGNAL(updateTimeout()), this, SLOT(waitForFix()));
483 //log->debug("start positioning...");
489 void WindowMap::positionUpdated(const QGeoPositionInfo &pos){
491 //log->info("ACTIVITYMAP-POSSITIONUPDATED!!!");
492 //setCenter(pos.coordinate().latitude(), pos.coordinate().longitude());
495 pos.attribute(QGeoPositionInfo::HorizontalAccuracy)<=settings->getHorizontalAccuracy() &&
496 pos.attribute(QGeoPositionInfo::VerticalAccuracy)<=settings->getVerticalAccuracy()){
498 GpsPoint* point = new GpsPoint(pos.coordinate().latitude(),pos.coordinate().longitude(),pos.coordinate().altitude(),pos.timestamp(),
499 pos.attribute(QGeoPositionInfo::GroundSpeed),pos.attribute(QGeoPositionInfo::Direction),
500 pos.attribute(QGeoPositionInfo::MagneticVariation),
501 pos.attribute(QGeoPositionInfo::HorizontalAccuracy),
502 pos.attribute(QGeoPositionInfo::VerticalAccuracy));
505 // log->debug(point->toString());
506 //log->debug("going to add point...");
507 track->addPoint(point);
508 emit(infoWindow->updateSummary());
512 setCenter(point->getLatitude(),point->getLongitude());
514 //if(satelliteDialog)
515 // satelliteDialog->hide();
519 log->debug("position not updated:");
521 log->debug("Horizontal accuracy (meters):" + QString::number(pos.attribute(QGeoPositionInfo::HorizontalAccuracy)));
522 log->debug("Vertical accuracy (meters):" + QString::number(pos.attribute(QGeoPositionInfo::VerticalAccuracy)));
528 void WindowMap::simulatedPossitionUpdated(){
529 log->info("simulated possition!!!");
530 //log->info(QString("num points to simulate:%1").arg(trackToSimulate.size()));
531 GpsPoint* point= new GpsPoint(0,0,0);
534 if(trackToSimulate.size()>0){
536 point = trackToSimulate.first();
538 log->debug(QString("point to simulate:%1").arg(point->toString()));
539 track->addPoint(point);
540 log->debug("going to emit updateSummary!!!");
541 emit(infoWindow->updateSummary());
542 log->debug("point added to track and updated");
543 //log->debug(QString("track num points:%1").arg(track->getNumPoints()));
545 trackToSimulate.removeFirst();
547 //log->info(QString("num points to simulate:%1").arg(trackToSimulate.size()));
549 //log->debug(QString("Possition paint???????????%1").arg(possitionPaint));
551 setCenter(point->getLatitude(),point->getLongitude());
559 void WindowMap::setTopLeft(qreal lat, qreal lng){
561 //log->debug("----setTopLeft----");
562 QPointF ct = slippyMap->coordinate2tile(lat, lng, zoom);
564 qreal tx = ct.x()-static_cast<qreal>(width())/(2.0*static_cast<qreal>(tdim));
565 qreal ty = ct.y()-static_cast<qreal>(height())/(2.0*static_cast<qreal>(tdim));
566 topLeft=QPointF(tx,ty);
569 void WindowMap::setCenter(qreal lat, qreal lng) {
571 //log->debug("--- set Center function ---!");
574 log->err("slippyMap not created!");
580 setTopLeft(lat, lng);
581 slippyMap->updateTiles(lat,lng);
584 void WindowMap::updateMap(const QRect &r) {
588 void WindowMap::closeEvent(QCloseEvent *event){
590 //log->debug("---- WindowMap quit, going to show parent and hide this ----");
591 //log->debug(QString("mode:==Mode_NewActivityMode?:%1, mode:%2").arg(mode==Mode_NewActivityMode).arg(mode));
592 if(mode==Mode_NewActivityMode && !simulateGPS){
593 QMessageBox msgBox(this);
595 msgBox.setText(tr("Do you want to close activity?, it will be saved."));
596 msgBox.setInformativeText(tr("Closing Activity"));
597 msgBox.setIcon(QMessageBox::Information);
598 msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
599 msgBox.setDefaultButton(QMessageBox::Cancel);
600 int ret = msgBox.exec();
602 if (ret == QMessageBox::Cancel){
605 disconnect(nSession,SIGNAL(error(QNetworkSession::SessionError)),this,SLOT(error(QNetworkSession::SessionError)));
608 settings->setIsConfigured(false);
610 this->parentWidget()->showFullScreen();
616 if(mode==Mode_NewActivityMode && simulateGPS){
617 settings->setIsConfigured(false);
618 //log->debug("exit from simulating GPS");
619 //log->debug("setting isconfigured to false and stopping timer");
620 simulatePossitioningTimer->stop();
624 this->parentWidget()->showFullScreen();
626 //log->debug("done");
630 void WindowMap::chooseDisplay(){
632 if(mode==Mode_NewActivityMode && !settings->getIsConfigured()){
634 setWindowTitle(track->getName());
635 settingsWindow->show();
637 }else if(mode==Mode_LoadTracksWindow && !settings->getIsConfigured()){
638 setWindowTitle(QString("Load Tracks ->").append(trackToSniff->getName()));
639 loadTracksWindow->show();
643 void WindowMap::showEvent(QShowEvent *){
647 void WindowMap::networkStart(){
648 startNetworkAction->setEnabled(false);
649 QTimer::singleShot(0, nSession, SLOT(open()));
652 void WindowMap::configureSettings(){
653 settingsWindow->show();
656 void WindowMap::tracksActivity(){
657 //mode=Mode_LoadTracksWindow;
658 possitionPaint=false;
659 //setMenuLoadTracks();
660 loadTracksWindow->show();
663 void WindowMap::loadTrack(){
664 loadTracksWindow->show();
666 void WindowMap::downloadMapTrack(){
670 qreal lat = slippyMap->getLatitude();
671 qreal lng = slippyMap->getLongitude();
675 downloadingStatus->show();
677 //downloadingStatus->move(pos);
678 QProgressBar* pb = downloadingStatus->getProgressBar();
679 int numPoints = trackToSniff->getNumPoints();
680 int zoomsToDown = (MAX_ZOOM_DOWNL - slippyMap->getZoom());
682 pb->setRange(0,numPoints*zoomsToDown);
685 downloadingStatus->setText(QString(tr("Making map requests")));
687 connect(slippyMap,SIGNAL(pointsRequested(int)),pb,SLOT(setValue(int)));
688 tilesToDownload = slippyMap->downloadMaps(trackToSniff);
689 disconnect(slippyMap,SIGNAL(pointsRequested(int)),pb,SLOT(setValue(int)));
691 if(tilesToDownload>0){
692 downloadingStatus->setText(QString(tr("Getting map tiles")));
693 pb->setRange(0,tilesToDownload);
695 connect(slippyMap,SIGNAL(tilesDownloaded(int)),this,SLOT(updateProgress(int)));
697 downloadingStatus->hide();
700 slippyMap->updateTiles(lat,lng);
704 void WindowMap::updateProgress(int val){
706 downloadingStatus->getProgressBar()->setValue(val);
707 if(val==tilesToDownload){
708 downloadingStatus->hide();
711 void WindowMap::newActivityTrack(){
712 mode=Mode_NewActivityMode;
713 settings->setIsConfigured(false);
718 void WindowMap::newSimulation(){
719 mode=Mode_NewActivityMode;
721 settings->setIsConfigured(false);
726 void WindowMap::info(){
730 void WindowMap::startActivity(){
732 stopAction->setEnabled(true);
733 startAction->setEnabled(false);
735 //log->debug("stopStart enabled");
737 void WindowMap::stopActivity(){
739 stopAction->setEnabled(false);
740 startAction->setEnabled(true);
747 void WindowMap::settings(){
748 settingsWindow->show();
752 void WindowMap::lapActivity(){
757 void WindowMap::quit(){
758 QCloseEvent *event = new QCloseEvent();
759 emit closeEvent(event);
762 void WindowMap::tracks(){
768 void WindowMap::satellite(){
770 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
776 void WindowMap::stopPositioning() {
778 simulatePossitioningTimer->stop();
781 location->stopUpdates();
785 void WindowMap::startPositioning() {
787 simulatePossitioningTimer->start();
790 location->startUpdates();
794 void WindowMap::onZoomOut(){
797 if(settings->getMapType()==MapTypeICC)
798 minzoom=MIN_ICC_ZOOM;
800 if (zoom > minzoom) {
802 zoomOutButton->setEnabled(zoom > minzoom);
803 zoomInButton->setEnabled(true);
807 slippyMap->setZoom(zoom);
809 setTopLeft(slippyMap->getLatitude(), slippyMap->getLongitude());
810 slippyMap->updateTiles();
815 void WindowMap::onZoomIn(){
816 if (zoom < MAX_ZOOM - 1) {
818 zoomInButton->setEnabled(zoom < MAX_ZOOM - 1);
819 zoomOutButton->setEnabled(true);
823 slippyMap->setZoom(zoom);
824 setTopLeft(slippyMap->getLatitude(), slippyMap->getLongitude());
825 slippyMap->updateTiles();
830 void WindowMap::updateSummary(){
831 emit(infoWindow->updateSummary());
836 void WindowMap::autosave(){
838 if(track!=0 && track->getNumPoints()>0 && !simulateGPS){
839 log->debug("going to save the TCX file...");
843 log->info("done...");
846 void WindowMap::setOrientation(ScreenOrientation orientation)
849 #if defined(Q_OS_SYMBIAN)
850 // If the version of Qt on the device is < 4.7.2, that attribute won't work
851 if (orientation != ScreenOrientationAuto) {
852 const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
853 if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
854 qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
858 #endif // Q_OS_SYMBIAN
860 Qt::WidgetAttribute attribute;
861 switch (orientation) {
862 #if QT_VERSION < 0x040702
863 // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
864 case ScreenOrientationLockPortrait:
865 attribute = static_cast<Qt::WidgetAttribute>(128);
867 case ScreenOrientationLockLandscape:
868 attribute = static_cast<Qt::WidgetAttribute>(129);
871 case ScreenOrientationAuto:
872 attribute = static_cast<Qt::WidgetAttribute>(130);
874 #else // QT_VERSION < 0x040702
875 case ScreenOrientationLockPortrait:
876 attribute = Qt::WA_LockPortraitOrientation;
878 case ScreenOrientationLockLandscape:
879 attribute = Qt::WA_LockLandscapeOrientation;
882 case ScreenOrientationAuto:
883 attribute = Qt::WA_AutoOrientation;
885 #endif // QT_VERSION < 0x040702
887 setAttribute(attribute, true);
891 void WindowMap::showExpanded()
895 #elif defined(Q_WS_MAEMO_5)
903 void WindowMap::paintEvent(QPaintEvent *event) {
907 log->debug("---- paint Event ----!");
914 #if defined(Q_OS_SYMBIAN)
915 QFont font = p.font();
916 font.setPixelSize(13);
921 log->debug("slippyMap not created!");
926 slippyMap->render(&p, event->rect());
928 //log->info("going to draw tracking route...");
929 drawTrack(trackToSniff,&p, settings->getTrackToSniffColor());
931 if(mode==Mode_NewActivityMode){
932 //log->info("going to draw actual route...");
933 //log->debug(QString("num points track:%1").arg(track->getNumPoints()));
934 //log->debug("entering to drawTrack");
935 drawTrack(track,&p,settings->getTrackColor(),true);
942 //p.drawText(rect(), Qt::AlignBottom | Qt::TextWordWrap,"OpenStreetMap maps");
943 //log->debug("ending Paint event...");
945 //log->debug("done... and now?");
949 void WindowMap::displayInit(QPainter *p){
951 QPixmap emptyTile = QPixmap(tdim,tdim);
952 emptyTile.fill(Qt::lightGray);
954 int numWidth = floor(width()/tdim)+1;
955 int numHeight = floor(height()/tdim)+1;
956 qreal tempx = static_cast<qreal>(width())/tdim;
957 qreal tempy = static_cast<qreal>(height())/tdim;
959 int width_offset=(tempx-numWidth)*tdim/2;
960 int height_offset=(tempy-numHeight)*tdim/2;
962 QPoint topLeft = QPoint(width_offset,height_offset);
964 for (int x = 0; x <= numWidth; ++x){
965 for (int y = 0; y <= numHeight; ++y) {
966 QRect box = QRect(topLeft.x()+x*tdim, topLeft.y()+y*tdim, tdim, tdim);
968 p->drawPixmap(box,emptyTile);
976 void WindowMap::drawFullTrackMaps(Track* track_p){
978 QList<GpsPoint*> gpsPoints = track_p->getGpsPoints();
979 //log->debug(QString("number of points to draw:%1").arg(gpsPoints.size()));
992 while(zoomToTrack>0 && !found){
995 //log->debug(QString("zoomToTrack:%1").arg(zoomToTrack));
996 for (int i = 0;!changeZoom && i < gpsPoints.size(); ++i) {
997 GpsPoint* point = gpsPoints.at(i);
998 QPointF ct = slippyMap->coordinate2tile(point->getLatitude(), point->getLongitude(), zoomToTrack);
1000 //log->debug(QString("point %1: x=%2,y=%3").arg(i).arg(ct.x()).arg(ct.y()));
1008 min_x = min(min_x,ct.x());
1009 max_x = max(max_x,ct.x());
1010 min_y = min(min_y,ct.y());
1011 max_y = max(max_y,ct.y());
1013 //log->debug(QString("min_x:%1, max_x:%2, min_y:%3, max_y:%4").arg(min_x).arg(max_x).arg(min_y).arg(max_y));
1015 // if point is outside screen change zoom
1016 qreal d_x=(max_x-min_x)*tdim;
1017 qreal d_y=(max_y-min_y)*tdim;
1018 //log->debug(QString("x distance:%1, y distance:%2").arg(d_x).arg(d_y));
1019 //log->debug(QString("width-border*2=%1, height-border*2=%2").arg(width()-border*2).arg(height()-border*2));
1020 if(d_x>(width()-border*2) || d_y>(height()-border*2)){
1021 //log->debug("point outside screen, changezoom");
1028 //log->debug(QString("all points are inside screen, zoom found: %1").arg(zoomToTrack));
1032 qreal med_x=qreal(min_x+static_cast<qreal>(width())/(2.0*static_cast<qreal>(tdim)));
1033 qreal med_y=qreal(min_y+static_cast<qreal>(height())/(2.0*static_cast<qreal>(tdim)));
1035 qreal lng=slippyMap->tilex2long(med_x,zoomToTrack);
1036 qreal lat=slippyMap->tiley2lat(med_y,zoomToTrack);
1039 slippyMap->setZoom(zoom);
1040 //log->debug(QString("changing zoom to:%1").arg(zoom));
1041 //log->debug(QString("centering with x_med:%1,y_med:%2 ").arg(med_x).arg(med_y));
1042 //log->debug(QString("centering with lat:%1,lng:%2 ").arg(lat).arg(lng));
1045 //log->debug(QString("minx:%1, miny:%2").arg(min_x).arg(min_y));
1046 topLeft=QPointF(min_x,min_y);
1047 slippyMap->updateTiles(lat,lng);
1051 bool WindowMap::drawTrack(Track* track_p, QPainter* p, QString color_s){
1052 drawTrack(track_p,p,color_s,false);
1055 bool WindowMap::drawTrack(Track* track_p, QPainter* p, QString color_s,bool lastPoint){
1057 if (track_p!=NULL && track_p->getNumPoints()<=0){
1058 log->debug("no points to draw...");
1061 QColor color = QColor(color_s);
1062 p->setBrush(QBrush(color));
1063 QPen pen(color, 5, Qt::SolidLine);
1068 QPointF previous=QPointF(0,0);
1070 log->debug("going to get points...");
1071 //log->debug(QString("num:%1").arg(track_p->getNumPoints()));
1072 QList<GpsPoint*> points = track_p->getGpsPoints();
1073 log->debug(QString("drawing with zoom:%1").arg(zoom));
1074 //log->debug(QString("TopLeft x:%1, y:%2").arg(topLeft.x()).arg(topLeft.y()));
1075 for(int i = 0; i < points.size(); ++i){
1077 GpsPoint* point = points.at(i);
1078 QPointF ct = slippyMap->coordinate2tile(point->getLatitude(), point->getLongitude(), zoom);
1080 //log->debug(QString("point %1: x=%2,y=%3").arg(i).arg(ct.x()).arg(ct.y()));
1081 //log->debug(QString("topLeft.x:%1, topLeft.y:%2").arg(topLeft.x()).arg(topLeft.y()));
1082 qreal x_resized = (ct.x() - topLeft.x())*tdim + border;
1083 qreal y_resized = (ct.y() - topLeft.y())*tdim + border;
1084 QPointF resized = QPoint(x_resized,y_resized);
1085 //log->debug(QString("point resized: x=%1,y=%2").arg(resized.x()).arg(resized.y()));
1086 if(!previous.isNull() && previous!=ct &&
1087 (resized.x()>= 0 && resized.x()<=width() && resized.y()>=0 && resized.y() <= height())){
1088 //log->debug(QString("drawing line from: %1,%2 to %3,%4").arg(previous.x()).arg(previous.y()).arg(resized.x()).arg(resized.y()));
1089 p->drawLine(previous,resized);
1095 p->setBrush(QBrush(QColor(color_s)));
1096 p->drawEllipse(previous,10,10);
1097 p->setBrush(QBrush(Qt::black));
1098 p->drawEllipse(previous,7,7);
1104 void WindowMap::mousePressEvent(QMouseEvent *event) {
1108 if (event->buttons() != Qt::LeftButton)
1110 pressed = snapped = true;
1111 pressPos = dragPos = event->pos();
1113 possitionPaint=false;
1116 //tapTimer.start(HOLD_TIME, this);
1119 void WindowMap::mouseMoveEvent(QMouseEvent *event) {
1123 if (!event->buttons())
1126 //possitionPaint=false;
1128 if (!pressed || !snapped) {
1129 QPoint delta = event->pos() - pressPos;
1130 pressPos = event->pos();
1131 QPointF dx = QPointF(delta);
1133 //log->debug(QString("Changing topleft, from %1,%2").arg(topLeft.x()).arg(topLeft.y()));
1134 //log->debug(QString("distance changed: x=%1,y=%2").arg(dx.x()).arg(dx.y()));
1135 topLeft = topLeft -dx/tdim;
1136 //log->debug(QString("to %1,%2").arg(topLeft.x()).arg(topLeft.y()));
1137 //topLeft = QPoint(delta.x()-width()/2,delta.y()-height()/2);
1138 slippyMap->pan(delta);
1142 const int threshold = 10;
1143 QPoint delta = event->pos() - pressPos;
1145 snapped &= delta.x() < threshold;
1146 snapped &= delta.y() < threshold;
1147 snapped &= delta.x() > -threshold;
1148 snapped &= delta.y() > -threshold;
1155 void WindowMap::mouseReleaseEvent(QMouseEvent *) {
1158 desactivatePossitionPaint();
1163 void WindowMap::fixZoomButtons(){
1165 int x_in=(width()-zoomInButton->width());
1166 int y_in=(height()/2-zoomInButton->height()/2);
1167 //log->debug(QString("ZoomIn possition: x=%1,y=%2").arg(x_in).arg(y_in));
1170 int y_out=(height()/2-zoomOutButton->height()/2);
1171 //log->debug(QString("ZoomOut possition: x=%1,y=%2").arg(x_out).arg(y_out));
1173 zoomInButton->move(x_in,y_in);
1174 zoomOutButton->move(x_out,y_out);
1175 zoomInButton->show();
1176 zoomOutButton->show();
1179 void WindowMap::resizeEvent(QResizeEvent * /* event */)
1185 int x = width() - (zoomInButton->width()
1186 + zoomOutButton->width() + 10);
1187 zoomInButton->move(x, 5);
1188 zoomOutButton->move(x + zoomInButton->width() + 5, 5);
1194 /* QString activity;