d37fb584f7aae03c5dd438514f89a6be795fd250
[gpssportsniffer] / settingsWindow.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 "settingsWindow.h"
21 #include "windowMap.h"
22 #include "ui_settingsWindow.h"
23
24 #include <QAbstractItemView>
25 #include <QFileDialog>
26 #include <QColorDialog>
27 #include <QCloseEvent>
28
29 /*
30   * populate settings widget class
31   */
32
33 SettingsWindow::SettingsWindow(WindowMap *parent, Settings *sett, Log *log)
34     : QDialog(parent),
35     activitySettings(sett),
36     activityMap(parent),
37     log(log),
38     ui(new Ui::SettingsWindow){
39
40     ui->setupUi(this);
41
42     currentDir = new QDir(APPLICATION_PATH);
43
44     // Centering all TypeActivity items
45     QAbstractItemView *viewType = ui->typeActivity->view();
46     QAbstractItemModel *modelType = viewType->model();
47
48
49     for(int i=0; i < ui->typeActivity->count();i++){
50
51         modelType->setData(modelType->index(i, 0), QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
52     }
53
54     QColor color = QColor(activitySettings->getTrackColor());
55     ui->colorLabel->setText(color.name());
56     ui->colorLabel->setPalette(QPalette(color));
57     ui->colorLabel->setAutoFillBackground(true);
58
59     // Centering GPS interval items
60     QAbstractItemView *viewGPS = ui->gpsInterval->view();
61     QAbstractItemModel *modelGPS = viewGPS->model();
62
63     for(int i=0; i < ui->gpsInterval->count();i++){
64         modelGPS->setData(modelGPS->index(i, 0), QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
65     }
66
67     // Centering MapType items
68     QAbstractItemView *viewMaps = ui->mapsType->view();
69     QAbstractItemModel *modelMaps = viewMaps->model();
70
71     for(int i=0; i < ui->mapsType->count();i++){
72         modelMaps->setData(modelMaps->index(i, 0), QVariant(Qt::AlignCenter), Qt::TextAlignmentRole);
73     }
74
75     currentDir->setPath(APPLICATION_PATH);
76
77     connect(ui->browseButton, SIGNAL(clicked()), SLOT(browse()));
78     connect(ui->colorButton, SIGNAL(clicked()), this, SLOT(setColor()));
79
80     connect(ui->typeActivity,SIGNAL(currentIndexChanged(int)),this,SLOT(changeEditText()));
81     connect(ui->tcxButton,SIGNAL(toggled(bool)),this,SLOT(changeTCXTypeFile(bool)));
82
83     // if simulating mode connect with gpssimulate
84
85
86
87 }
88
89 SettingsWindow::~SettingsWindow()
90 {
91     delete ui;
92 }
93
94 void SettingsWindow::changeEditText(){
95     ui->fileCombo->setEditText(QString(APPLICATION_PATH).append(QDateTime::currentDateTime().toString(CONTINOUS_DATE_FORMAT))
96                                .append("_").append(ui->typeActivity->currentText()).append(getExtension()));
97 }
98
99 void SettingsWindow::changeTCXTypeFile(bool checked){
100
101     QString extension;
102     if(checked)
103         extension=QString(".tcx");
104     else
105         extension=QString(".gpx");
106
107     ui->fileCombo->setEditText(QString(APPLICATION_PATH).append(QDateTime::currentDateTime().toString(CONTINOUS_DATE_FORMAT))
108                                .append("_").append(ui->typeActivity->currentText()).append(extension));
109
110 }
111
112 QString SettingsWindow::getExtension(){
113
114     if(ui->tcxButton->isChecked()){
115         return QString(".tcx");
116     }else{
117         return QString(".gpx");
118     }
119 }
120
121 void SettingsWindow::showEvent(QShowEvent *){
122
123     if(activityMap->simulatingGPS()){
124         setWindowTitle(tr("Simulating GPS Activity"));
125         ui->xmlTypeLabel->setVisible(false);
126         ui->gpxButton->setVisible(false);
127         ui->tcxButton->setVisible(false);
128         //ui->xmlTypeLayout->setVisible(false);
129
130     }else{
131         setWindowTitle(tr("GPS Activity"));
132         ui->xmlTypeLabel->setVisible(true);
133         //ui->xmlTypeLayout->setVisible(true);
134         ui->gpxButton->setVisible(true);
135         ui->tcxButton->setVisible(true);
136         ui->fileCombo->setEnabled(true);
137         ui->browseButton->setEnabled(true);
138         ui->fileCombo->setEditText(QString(APPLICATION_PATH).append(QDateTime::currentDateTime().toString(CONTINOUS_DATE_FORMAT))
139                                    .append("_").append(ui->typeActivity->currentText()).append(getExtension()));
140     }
141
142     //if(activitySettings->getIsConfigured() && !activityMap->simulatingGPS()){
143      if(activitySettings->getIsConfigured()){
144         //log->debug("DISABLING BUTTONS...");
145         ui->browseButton->setEnabled(false);
146         ui->fileCombo->setEnabled(false);
147         ui->gpxButton->setEnabled(false);
148         ui->tcxButton->setEnabled(false);
149     }
150 }
151
152 void SettingsWindow::browse(){
153
154     //log->debug("browse slot...");
155     //log->debug("current dir:" + currentDir->path());
156
157     QString directory;
158
159     if(activityMap->simulatingGPS()){
160         //log->debug("simulating GPS...");
161         directory = QFileDialog::getOpenFileName(this,tr("Open XML Files"), currentDir->path(), tr("XML Files (*.gpx *.tcx)"));
162     }else{
163         directory = QFileDialog::getExistingDirectory(this, tr("Open Directory"),currentDir->path(),
164                                                       QFileDialog::ShowDirsOnly
165                                                     | QFileDialog::DontResolveSymlinks);
166     }
167
168     //QString directory = QFileDialog::getExistingDirectory (this, tr("Directory"), currentDir->path());
169     //QString directory = QFileDialog::getExistingDirectory (this, tr("Directory"), "./");
170
171
172     log->debug("directory:" + directory);
173     if ( directory.isNull() == false )
174     {
175         currentDir->setPath(directory);
176
177         log->debug("going to catch file...");
178
179         if (!directory.isEmpty()) {
180             if (ui->fileCombo->findText(directory) == -1)
181                 ui->fileCombo->addItem(directory);
182             ui->fileCombo->setCurrentIndex(ui->fileCombo->findText(directory));
183         }
184     }
185
186 }
187
188 void SettingsWindow::setColor(){
189
190
191     QColor color = QColorDialog::getColor(QColor(activitySettings->getTrackColor()), this);
192         if (color.isValid())
193         {
194             ui->colorLabel->setText(color.name());
195             ui->colorLabel->setPalette(QPalette(color));
196             ui->colorLabel->setAutoFillBackground(true);
197             activitySettings->setTrackColor(color.name());
198         }
199 }
200
201
202 void SettingsWindow::closeEvent(QCloseEvent *event){
203     if(!activitySettings->getIsConfigured()){
204         this->parentWidget()->hide();
205         this->parentWidget()->parentWidget()->show();
206         this->hide();
207     }else{
208
209         if(activityMap->simulatingGPS()&&!activityMap->hasTrackToSimulate()){
210             event->ignore();
211         }else{
212             this->hide();
213         }
214     }
215 }
216
217
218
219 void SettingsWindow::accept(){
220     this->hide();
221
222     log->debug("--- SettingsWindows --- closeEvent ");
223
224     if(!activitySettings->getIsConfigured()){
225
226         // first time configuration
227
228         activitySettings->setActivity(ui->typeActivity->currentText());
229         activitySettings->setGpsInterval(intervalFromCombo(ui->gpsInterval->currentIndex()));
230
231         log->debug(QString("---- ONLINE:%1").arg(ui->onlineButton->isChecked()));
232         activitySettings->setIsOnline(ui->onlineButton->isChecked());
233
234         activitySettings->setIsCache(ui->tileCacheOn->isChecked());
235         activityMap->setIsCacheEnabled(ui->tileCacheOn->isChecked());
236
237         activitySettings->setXmlFile(ui->fileCombo->currentText());
238
239         activitySettings->setMapType((MapType)ui->mapsType->currentIndex());
240
241
242         log->debug("going to add activity:" + activitySettings->getActivity());
243
244         activityMap->setTrack(activitySettings->getXmlFile(),activitySettings->getActivity());
245
246
247         activitySettings->setIsConfigured(true);
248         log->debug("starting services");
249         activityMap->startServices();
250         activityMap->changeMapType(activitySettings->getMapType());
251         log->debug("done");
252
253         //activityMap->startSPositioningTimer();
254
255         if(activityMap->simulatingGPS()){
256             log->debug(QString("going to read from XML, file:%1").arg(activitySettings->getXmlFile()));
257             Track* toDo = readFromXML(activitySettings->getXmlFile());
258             //Track* toDo = readFromXML("/home/user/MyDocs/FitxersGPXiTCX/PobletLaPena.tcx");
259             log->debug("XML readed without problem");
260             activityMap->setTrackToSimulate(toDo->getGpsPoints());
261             activityMap->changeUpdateInterval(activitySettings->getGpsInterval());
262
263             emit(activityMap->simulatedPossitionUpdated());
264             activityMap->startSimulatePossitioning();
265
266         }else{
267             activityMap->startAutosaveTimer(60000*TIME_MIN_TIMEAUTOSAVE);
268
269         }
270         log->debug("accept done");
271
272     }else{
273         //need to see what has changed
274          log->debug("going to configure some parameters!");
275
276         if(activitySettings->getActivity()!=QString(ui->typeActivity->currentText())){
277             activityMap->getTrack()->addActivity(activitySettings->getActivity());
278             log->debug("activity changed to " + activitySettings->getActivity());
279         }
280         if(activitySettings->getGpsInterval()!=intervalFromCombo(ui->gpsInterval->currentIndex())){
281             log->debug("interval changed, going to change it from location");
282             activitySettings->setGpsInterval(intervalFromCombo(ui->gpsInterval->currentIndex()));
283             activityMap->changeUpdateInterval(activitySettings->getGpsInterval());
284             log->debug(QString("Interval setted to: %1").arg(activitySettings->getGpsInterval()));
285         }
286         if(activitySettings->getIsOnline()!=ui->onlineButton->isChecked()){
287             activitySettings->setIsOnline(ui->onlineButton->isChecked());
288             if(!activitySettings->getIsOnline()){
289                 log->debug("SettingsWindow stopNetworking");
290                 activityMap->stopNetworking();
291             }if(activitySettings->getIsOnline()){
292                 log->debug("SettingsWindow startNetworking");
293                 activityMap->startNetworking();
294             }
295         }
296         if(activitySettings->getIsCache()!=ui->tileCacheOn->isChecked()){
297             activitySettings->setIsCache(ui->tileCacheOn->isChecked());
298             activityMap->setIsCacheEnabled(activitySettings->getIsCache());
299         }
300
301         if(activitySettings->getMapType()!=(MapType)ui->mapsType->currentIndex()){
302             activitySettings->setMapType((MapType)ui->mapsType->currentIndex());
303             activityMap->changeMapType(activitySettings->getMapType());
304             log->debug(QString("Changing maps to: %1").arg(activitySettings->getMapType()));
305             activityMap->clearAllMaps();
306         }
307
308     }
309     //activityMap->setCenter(FIRST_LATITUDE,FIRST_LONGITUDE);
310
311 }
312
313 void SettingsWindow::setActivityType(){
314
315 }
316
317 int SettingsWindow::intervalFromCombo(int index){
318
319     log->debug(QString("index from combo:%1").arg(index));
320     int returnValue=2000;
321
322     switch(index){
323
324     case 1:
325         returnValue=5000; //5 seconds
326         break;
327     case 2:
328         returnValue=10000; //10 seconds
329         break;
330     case 3:
331         returnValue=20000;  //20 seconds
332         break;
333     case 4:
334         returnValue=30000;  //20 seconds
335         break;
336     case 5:
337         returnValue=60000;
338         break;
339     default:
340     case 0:
341         returnValue=2000; // 2 seconds
342         break;
343     }
344     return returnValue;
345 }
346