1 /**********************************************************************************************
2     Copyright (C) 2014 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "canvas/CCanvas.h"
20 #include "gis/trk/CGisItemTrk.h"
21 #include "gis/trk/filter/CFilterSpeed.h"
22 #include "gis/trk/filter/CFilterSpeedConst.h"
23 #include "gis/trk/filter/CFilterSpeedCycle.h"
24 #include "gis/trk/filter/CFilterSpeedHike.h"
25 #include "helpers/CSettings.h"
26 
CFilterSpeed(CGisItemTrk & trk,QWidget * parent)27 CFilterSpeed::CFilterSpeed(CGisItemTrk& trk, QWidget* parent)
28     : QWidget(parent), trk(trk)
29 {
30     setupUi(this);
31 
32     filterConst = new CFilterSpeedConst(this);
33     filterCycle = new CFilterSpeedCycle(this, trk);
34     filterHike = new CFilterSpeedHike(this);
35 
36     stackedWidget->addWidget(filterConst);
37     stackedWidget->addWidget(filterCycle);
38     stackedWidget->addWidget(filterHike);
39 
40     labelWarning->setText("");
41 
42     SETTINGS;
43     cfg.beginGroup("TrackDetails/Filter/Speed/");
44     comboActivityType->setCurrentIndex(cfg.value("activityType", 0).toInt());
45     slotSetActivityType(comboActivityType->currentIndex());
46 
47     connect(comboActivityType, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &CFilterSpeed::slotSetActivityType);
48     connect(toolApply, &QToolButton::clicked, this, &CFilterSpeed::slotApply);
49 
50     cfg.beginGroup("Const");
51     filterConst->loadSettings(cfg);
52     cfg.endGroup(); // Const
53 
54     cfg.beginGroup("Cycle");
55     filterCycle->loadSettings(cfg);
56     cfg.endGroup(); // Cycle
57 
58     cfg.beginGroup("Hike");
59     filterHike->loadSettings(cfg);
60     cfg.endGroup(); // Hike
61 
62     cfg.endGroup(); //TrackDetails/Filter/Speed
63 }
64 
~CFilterSpeed()65 CFilterSpeed::~CFilterSpeed()
66 {
67     SETTINGS;
68     cfg.remove("TrackDetails/Filter/Speed/");
69     cfg.beginGroup("TrackDetails/Filter/Speed/");
70     cfg.setValue("activityType", comboActivityType->currentIndex());
71 
72     cfg.beginGroup("Const");
73     filterConst->saveSettings(cfg);
74     cfg.endGroup(); // Const
75 
76     cfg.beginGroup("Cycle");
77     filterCycle->saveSettings(cfg);
78     cfg.endGroup(); // Cycle
79 
80     cfg.beginGroup("Hike");
81     filterHike->saveSettings(cfg);
82     cfg.endGroup(); // Hike
83 
84     cfg.endGroup(); //TrackDetails/Filter/Speed
85 }
86 
slotApply()87 void CFilterSpeed::slotApply()
88 {
89     CCanvasCursorLock cursorLock(Qt::WaitCursor, __func__);
90 
91     switch (comboActivityType->currentIndex())
92     {
93     case 0:
94         filterConst->apply(trk);
95         break;
96 
97     case 1:
98     {
99         filterCycle->apply(trk);
100         break;
101     }
102 
103     case 2:
104     {
105         filterHike->apply(trk);
106         break;
107     }
108 
109     default:
110         break;
111     }
112 }
113 
slotSetActivityType(int type)114 void CFilterSpeed::slotSetActivityType(int type)
115 {
116     stackedWidget->setCurrentIndex(type);
117     updateUi();
118 }
119 
updateUi()120 void CFilterSpeed::updateUi()
121 {
122     if(trk.isTrkElevationInvalid() && comboActivityType->currentIndex() > 0)
123     {
124         QString str = QString("<b style='color: red;'>" +
125                               tr("Track has no or invalid elevation data. Please correct or set constant speed!") +
126                               "</b><br/>");
127         labelWarning->setText(str);
128         filterCycle->setEnabled(false);
129         filterHike->setEnabled(false);
130         toolApply->setEnabled(false);
131         return;
132     }
133     else
134     {
135         labelWarning->setText("");
136         filterCycle->setEnabled(true);
137         filterHike->setEnabled(true);
138         toolApply->setEnabled(true);
139     }
140 }
141