forked from knossos-project/knossos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.cpp
98 lines (83 loc) · 3.44 KB
/
session.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
/*
* This file is a part of KNOSSOS.
*
* (C) Copyright 2007-2016
* Max-Planck-Gesellschaft zur Foerderung der Wissenschaften e.V.
*
* KNOSSOS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 of
* the License as published by the Free Software Foundation.
*
* 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/>.
*
*
* For further information, visit https://knossostool.org
* or contact knossos-team@mpimf-heidelberg.mpg.de
*/
#include "session.h"
#include "stateInfo.h"
#include <QApplication>
class Session::ActivityEventFilter : public QObject {
bool & timeSliceActivity;
public:
ActivityEventFilter(Session & session) : QObject(&session), timeSliceActivity(session.timeSliceActivity) {}
bool eventFilter(QObject *object, QEvent *event) {
// mark time slice as valid on any user actions except just moving the mouse
int type = event->type();
if (type == QEvent::MouseButtonPress || type == QEvent::KeyPress || type == QEvent::Wheel) {
timeSliceActivity = true;
}
return QObject::eventFilter(object, event);
}
};
Session::Session() : annotationMode(AnnotationMode::Mode_Tracing) {
qApp->installEventFilter(new ActivityEventFilter(*this));
annotationTimer.setTimerType(Qt::PreciseTimer);
QObject::connect(&annotationTimer, &QTimer::timeout, this, &Session::handleTimeSlice);
annotationTimer.start(TIME_SLICE_MS);
lastTimeSlice.start();
autoSaveTimer.setTimerType(Qt::PreciseTimer);
QObject::connect(&autoSaveTimer, &QTimer::timeout, [this]() {
if(unsavedChanges) {
emit autoSaveSignal();
}
});
}
bool Session::outsideMovementArea(const Coordinate & pos) {
return pos.x < movementAreaMin.x || pos.x > movementAreaMax.x ||
pos.y < movementAreaMin.y || pos.y > movementAreaMax.y ||
pos.z < movementAreaMin.z || pos.z > movementAreaMax.z;
}
void Session::updateMovementArea(const Coordinate & min, const Coordinate & max) {
movementAreaMin = min.capped({0, 0, 0}, state->boundary);
movementAreaMax = max.capped({0, 0, 0}, state->boundary);
emit movementAreaChanged();
}
void Session::resetMovementArea() {
updateMovementArea({0, 0, 0}, state->boundary);
}
decltype(Session::annotationTimeMilliseconds) Session::getAnnotationTime() const {
return annotationTimeMilliseconds;
}
void Session::setAnnotationTime(const decltype(annotationTimeMilliseconds) & ms) {
annotationTimeMilliseconds = ms;
const auto absoluteMinutes = annotationTimeMilliseconds / 1000 / 60;
const auto hours = absoluteMinutes / 60;
const auto minutes = absoluteMinutes % 60;
emit annotationTimeChanged(QString("%1:%2 h annotation time").arg(hours).arg(minutes, 2, 10, QChar('0')));
}
decltype(Session::annotationTimeMilliseconds) Session::currentTimeSliceMs() const {
return lastTimeSlice.elapsed();
}
void Session::handleTimeSlice() {
if (timeSliceActivity) {
setAnnotationTime(annotationTimeMilliseconds + TIME_SLICE_MS);
timeSliceActivity = false;
}
lastTimeSlice.restart();
}