-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.cpp
100 lines (84 loc) · 3.41 KB
/
controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// File: controller.cpp
// Author: Daniele Picciaia
// Project: CrossMonitor
// url: https://github.com/picciaia/crossmonitor
// Controller class that is responsible to create signal/slots to interfa with the View and the CrossProvider
// object to update cross information
// For more information please refer to 'readme.md' file
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "controller.h"
using namespace std;
Controller::Controller(Model& model, MainWindow& view) :
currentModel {model}, currentView {view}, isRunning{false}
{
}
void Controller::Run()
{
//Configure signals/slots
connect(¤tModel, SIGNAL(CrossChanged(const Cross&)),
¤tView, SLOT(OnCrossChanged(const Cross&)));
connect(&timer, SIGNAL(timeout()), this, SLOT(TimerUpdate()));
connect(¤tView, SIGNAL(UpdatePeriod(int)), this, SLOT(OnUpdatePeriod(int)));
connect(¤tView, SIGNAL(CloseWindowEvent()), this, SLOT(OnCloseWindowEvent()));
//start the timer to update cross info, by supporting signals/slots
timer.start(currentModel.UpdateSec * 1000);
timer.setInterval(currentModel.UpdateSec * 1000);
crossProvider.Init();
isRunning = true;
currentView.show();
//First cross info reading
UpdateCross();
}
//This method (slot) is called by the view, when the application is closing
void Controller::Exit()
{
isRunning = false;
timer.stop();
disconnect(¤tModel, SIGNAL(CrossChanged(const Cross&)),
¤tView, SLOT(OnCrossChanged(const Cross&)));
disconnect(&timer, SIGNAL(timeout()), this, SLOT(TimerUpdate()));
disconnect(¤tView, SIGNAL(UpdatePeriod(int)), this, SLOT(OnUpdatePeriod(int)));
disconnect(¤tView, SIGNAL(CloseWindowEvent()), this, SLOT(OnCloseWindowEvent()));
}
//This method (slot) is called by the view, to update the refresh period
void Controller::OnUpdatePeriod(int seconds)
{
currentModel.UpdateSec = seconds;
timer.setInterval(seconds * 1000);
}
//This method (slot) is called by the internal timer to execute a cross reading operation
void Controller::TimerUpdate()
{
UpdateCross();
}
//get cross data (by using CrossProvider) and update the Model
void Controller::UpdateCross()
{
try
{
auto cross = this->crossProvider.GetCross(this->currentModel.CrossURL);
currentModel.Cross_Set(cross);
}
catch (exception& e)
{
this->currentView.AddLog(e.what());
}
catch (...)
{
this->currentView.AddLog("Error on server communication");
}
}
void Controller::OnCloseWindowEvent()
{
Exit();
}