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