summaryrefslogtreecommitdiffhomepage
path: root/opentrack-logic/work.cpp
blob: 8d00270b4f541c1a0b19de6d5335eb8715da50db (plain)
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
#include "work.hpp"

#include <QMessageBox>


std::shared_ptr<TrackLogger> Work::make_logger(const main_settings &s)
{
    if (s.tracklogging_enabled)
    {
        if (static_cast<QString>(s.tracklogging_filename).isEmpty())
        {
            QMessageBox::warning(nullptr, "Logging Error",
                "No filename given for track logging. Proceeding without logging.",
                QMessageBox::Ok,
                QMessageBox::NoButton);
        }
        else
        {
            auto logger = std::make_shared<TrackLoggerCSV>(s.tracklogging_filename);
            if (!logger->is_open())
            {
                logger = nullptr;
                QMessageBox::warning(nullptr, "Logging Error",
                    "Unable to open file: " + s.tracklogging_filename + ". Proceeding  without logging.",
                    QMessageBox::Ok,
                    QMessageBox::NoButton);
            }
            else
            {
                /* As this function has the potential to fill up the hard drive
                   of the unwary with junk data, a warning is in order. */
                QMessageBox::warning(nullptr, "Logging Active",
                    "Just a heads up. You are recoding pose data to " + s.tracklogging_filename + "!",
                    QMessageBox::Ok,
                    QMessageBox::NoButton);
                return logger;
            }
        }
    }
    return std::make_shared<TrackLogger>();
}


Work::Work(Mappings& m, SelectedLibraries& libs, WId handle) :
    libs(libs),
    logger(make_logger(s)),
    tracker(std::make_shared<Tracker>(m, libs, *logger)),
    sc(std::make_shared<Shortcuts>()),
    handle(handle),
    keys {
        key_tuple(s.key_center, [&](bool) -> void { tracker->center(); }, true),
        key_tuple(s.key_toggle, [&](bool) -> void { tracker->toggle_enabled(); }, true),
        key_tuple(s.key_zero, [&](bool) -> void { tracker->zero(); }, true),
        key_tuple(s.key_toggle_press, [&](bool x) -> void { tracker->set_toggle(!x); }, false),
        key_tuple(s.key_zero_press, [&](bool x) -> void { tracker->set_zero(x); }, false),
        }
{
    reload_shortcuts();
    tracker->start();
}

void Work::reload_shortcuts()
{
    sc->reload(keys);
}

Work::~Work()
{
    sc = nullptr;
    // order matters, otherwise use-after-free -sh
    tracker = nullptr;
    libs = SelectedLibraries();
}