summaryrefslogtreecommitdiffhomepage
path: root/logic/work.cpp
blob: 62c1540377fa26ed1eaeef3e9d99961a18981854 (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
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
101
102
103
104
105
106
107
#include "work.hpp"
#include "opentrack-library-path.h"

#include <QObject>
#include <QMessageBox>
#include <QFileDialog>


QString Work::browse_datalogging_file(main_settings &s)
{
    QString filename = s.tracklogging_filename;
    if (filename.isEmpty())
        filename = OPENTRACK_BASE_PATH;
    /* Sometimes this function freezes the app before opening the dialog.
       Might be related to https://forum.qt.io/topic/49209/qfiledialog-getopenfilename-hangs-in-windows-when-using-the-native-dialog/8
       and be a known problem. Possible solution is to use the QFileDialog::DontUseNativeDialog flag.
       Since the freeze is apparently random, I'm not sure it helped.
    */
    QString newfilename = QFileDialog::getSaveFileName(nullptr,
                                                       QCoreApplication::translate("Work", "Select filename"),
                                                       filename,
                                                       QCoreApplication::translate("Work", "CSV File (*.csv)"),
                                                       nullptr);
    if (!newfilename.isEmpty())
    {
      s.tracklogging_filename = newfilename;
    }
    // dialog likes to mess with current directory
    QDir::setCurrent(OPENTRACK_BASE_PATH);
    return newfilename;
}

std::shared_ptr<TrackLogger> Work::make_logger(main_settings &s)
{
    if (s.tracklogging_enabled)
    {
        QString filename = browse_datalogging_file(s);
        if (filename.isEmpty())
        {
          // The user probably canceled the file dialog. In this case we don't want to do anything.
        }
        else
        {
            auto logger = std::make_shared<TrackLoggerCSV>(s.tracklogging_filename);
            if (!logger->is_open())
            {
                logger = nullptr;
                QMessageBox::warning(nullptr, QCoreApplication::translate("Work", "Logging error"),
                    QCoreApplication::translate("Work", "Unable to open file '%1'. Proceeding without logging.").arg(s.tracklogging_filename),
                    QMessageBox::Ok,
                    QMessageBox::NoButton);
            }
            else
            {
                return logger;
            }
        }
    }
    return std::make_shared<TrackLogger>();
}


Work::Work(Mappings& m, QFrame* frame, std::shared_ptr<dylib> tracker_, std::shared_ptr<dylib> filter_, std::shared_ptr<dylib> proto_) :
    libs(frame, tracker_, filter_, proto_),
    logger(make_logger(s)),
    tracker(std::make_shared<Tracker>(m, libs, *logger)),
    sc(std::make_shared<Shortcuts>()),
    keys {
        key_tuple(s.key_center1, [&](bool) -> void { tracker->center(); }, true),
        key_tuple(s.key_center2, [&](bool) -> void { tracker->center(); }, true),

        key_tuple(s.key_toggle1, [&](bool) -> void { tracker->toggle_enabled(); }, true),
        key_tuple(s.key_toggle2, [&](bool) -> void { tracker->toggle_enabled(); }, true),

        key_tuple(s.key_zero1, [&](bool) -> void { tracker->zero(); }, true),
        key_tuple(s.key_zero2, [&](bool) -> void { tracker->zero(); }, true),

        key_tuple(s.key_toggle_press1, [&](bool x) -> void { tracker->set_toggle(!x); }, false),
        key_tuple(s.key_toggle_press2, [&](bool x) -> void { tracker->set_toggle(!x); }, false),

        key_tuple(s.key_zero_press1, [&](bool x) -> void { tracker->set_zero(x); }, false),
        key_tuple(s.key_zero_press2, [&](bool x) -> void { tracker->set_zero(x); }, false),
    }
{
    if (!is_ok())
        return;
    reload_shortcuts();
    tracker->start();
}

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

bool Work::is_ok() const
{
    return libs.correct;
}

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