3584500c49f3ccfcdabd9ffc1b348b23536d77d1
[gpssportsniffer] / loadtracksWindow.cpp
1 /****************************************************************************
2 **
3 **  Copyright (C) 2011  Tito Eritja Real <jtitoo@gmail.com>
4 **
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.
9 **
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.
14 **
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/>.
17 **
18 ****************************************************************************/
19
20 #include "loadTracksWindow.h"
21 #include "ui_loadtracksWindow.h"
22
23 #include "windowMap.h"
24 #include "settings.h"
25
26 #include <QFileDialog>
27 #include <QColorDialog>
28 #include <QMessageBox>
29 #include <QCloseEvent>
30
31 LoadTracksWindow::LoadTracksWindow(WindowMap *parent, Settings* settings_p, Log *log) :
32     QDialog(parent), log(log),windowMap(parent),settings(settings_p),
33     ui(new Ui::LoadTracksWindow)
34 {
35     ui->setupUi(this);
36     currentDir.setPath(APPLICATION_PATH);
37
38     setWindowTitle(tr("Load track"));
39     QColor color = QColor(settings->getTrackToSniffColor());
40     ui->colorLabel->setText(color.name());
41     ui->colorLabel->setPalette(QPalette(color));
42     ui->colorLabel->setAutoFillBackground(true);
43
44     // Centering MapType items
45     QAbstractItemView *viewMaps = ui->mapsType->view();
46     QAbstractItemModel *modelMaps = viewMaps->model();
47
48     for(int i=0; i < ui->mapsType->count();i++){
49         modelMaps->setData(modelMaps->index(i, 0), QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
50     }
51
52     connect(ui->colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
53     connect(ui->browseButton, SIGNAL(clicked()), SLOT(browse()));
54 }
55
56 LoadTracksWindow::~LoadTracksWindow()
57 {
58     delete ui;
59 }
60
61 void LoadTracksWindow::setColor(){
62
63
64     QColor color = QColorDialog::getColor(QColor(settings->getTrackToSniffColor()), this);
65         if (color.isValid())
66         {
67             ui->colorLabel->setText(color.name());
68             ui->colorLabel->setPalette(QPalette(color));
69             ui->colorLabel->setAutoFillBackground(true);
70             settings->setTrackToSniffColor(color.name());
71         }
72 }
73
74 void LoadTracksWindow::browse(){
75
76     QString directory = QFileDialog::getOpenFileName(this,tr("Open XML Files"), currentDir.path(), tr("XML Files (*.gpx *.tcx)"));
77
78     if (!directory.isNull())
79     {
80         currentDir.setPath(directory);
81
82         if (!directory.isEmpty()) {
83             if (ui->fileCombo->findText(directory) == -1)
84                 ui->fileCombo->addItem(directory);
85             ui->fileCombo->setCurrentIndex(ui->fileCombo->findText(directory));
86         }
87     }
88
89 }
90
91
92 void LoadTracksWindow::closeEvent(QCloseEvent *event){
93     /*
94     //event->ignore();
95     log->debug("CLOSE EVENT OF LOADTRACKS!!!!!!!!!!!!!!!!!!!");
96     QMessageBox::warning(this, tr("GPSSniffer"),QString(tr("No file selected!")));
97     this->hide();
98     */
99
100     this->parentWidget()->hide();
101     this->parentWidget()->parentWidget()->show();
102     this->hide();
103 }
104
105 void LoadTracksWindow::showEvent(QShowEvent *){
106
107     if(windowMap->getMode()==Mode_NewActivityMode){
108         // loading track in new activity
109         ui->mapsType->setCurrentIndex(settings->getMapType());
110         ui->mapsType->setEnabled(false);
111     }
112 }
113
114 void LoadTracksWindow::accept(){
115
116
117     QString fileName = ui->fileCombo->currentText();
118     //QString fileName=QString("/home/user/MyDocs/FitxersGPXiTCX/VallDelRiuLac.tcx");
119     //QString fileName=QString("/home/user/MyDocs/GPSSniffer/12-07-2011_09-09-16_Cycling.tcx");
120
121     if(!fileName.endsWith(".gpx",Qt::CaseInsensitive) && !fileName.endsWith(".tcx",Qt::CaseInsensitive) ){
122         QMessageBox::warning(this, tr("GPSSniffer"),QString(tr("No file selected!")));
123     }else{
124         Track* toSniff = readFromXML(fileName);
125         log->debug("XML readed without problem");
126         windowMap->setTrackToSniff(toSniff);
127         settings->setIsCache(ui->mapCacheOn->isChecked());
128
129
130         if(windowMap->getMode()==Mode_LoadTracksWindow){
131             settings->setMapType((MapType)ui->mapsType->currentIndex());
132             windowMap->startNetworking();
133             windowMap->startMaps();
134         }else{
135
136             if(windowMap->simulatingGPS()){
137                 windowMap->desactivatePossitionPaint();
138             }
139         }
140         windowMap->setIsCacheEnabled(ui->mapCacheOn->isChecked());
141         windowMap->updateSummary();
142         windowMap->drawFullTrackMaps(toSniff);
143         settings->setIsConfigured(true);
144         windowMap->fixZoomButtons();
145         this->hide();
146     }
147
148
149 }
150