summaryrefslogtreecommitdiffhomepage
path: root/gui/mapping-window.cpp
blob: 7e5ad6e3c0ad393df8f6a1d5e4005509404b04ae (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Copyright (c) 2014-2015, Stanislaw Halik <sthalik@misaki.pl>

 * Permission to use, copy, modify, and/or distribute this
 * software for any purpose with or without fee is hereby granted,
 * provided that the above copyright notice and this permission
 * notice appear in all copies.
 */

#include "mapping-window.hpp"
#include "logic/main-settings.hpp"
#include "spline-widget/spline-widget.hpp"

MapWidget::MapWidget(Mappings& m) : m(m)
{
    ui.setupUi(this);

    load();

    connect(ui.buttonBox, SIGNAL(accepted()), this, SLOT(doOK()));
    connect(ui.buttonBox, SIGNAL(rejected()), this, SLOT(doCancel()));

    tie_setting(s.a_x.altp, ui.tx_altp);
    tie_setting(s.a_y.altp, ui.ty_altp);
    tie_setting(s.a_z.altp, ui.tz_altp);
    tie_setting(s.a_yaw.altp, ui.rx_altp);
    tie_setting(s.a_pitch.altp, ui.ry_altp);
    tie_setting(s.a_roll.altp, ui.rz_altp);

    tie_setting(s.a_yaw.clamp, ui.max_yaw_rotation);
    tie_setting(s.a_pitch.clamp, ui.max_pitch_rotation);
    tie_setting(s.a_roll.clamp, ui.max_roll_rotation);
}

void MapWidget::load()
{
    struct {
        spline_widget* qfc;
        Axis axis;
        QCheckBox* checkbox;
        bool altp;
    } qfcs[] =
    {
    { ui.rxconfig, Yaw,   nullptr, false, },
    { ui.ryconfig, Pitch, nullptr, false, },
    { ui.rzconfig, Roll,  nullptr, false, },
    { ui.txconfig, TX,    nullptr, false, },
    { ui.tyconfig, TY,    nullptr, false, },
    { ui.tzconfig, TZ,    nullptr, false, },
    { ui.rxconfig_alt, Yaw,   ui.rx_altp, true, },
    { ui.ryconfig_alt, Pitch, ui.ry_altp, true, },
    { ui.rzconfig_alt, Roll,  ui.rz_altp, true, },
    { ui.txconfig_alt, TX,    ui.tx_altp, true, },
    { ui.tyconfig_alt, TY,    ui.ty_altp, true, },
    { ui.tzconfig_alt, TZ,    ui.tz_altp, true, },
    { nullptr, Yaw, nullptr, false }
    };

    for (QComboBox* x : { ui.max_yaw_rotation, ui.max_pitch_rotation, ui.max_roll_rotation })
    {
        using a = axis_opts::max_rotation;
        for (a y : { a::r180, a::r90, a::r60, a::r45, a::r30, a::r20 })
            x->addItem(QString::number(y) + "°", y);
    }

    for (int i = 0; qfcs[i].qfc; i++)
    {
        const bool altp = qfcs[i].altp;

        Map& axis = m(qfcs[i].axis);
        spline& conf = altp ? axis.spline_alt : axis.spline_main;
        spline_widget& qfc = *qfcs[i].qfc;

        if (altp)
        {
            connect(&axis.opts.altp,
                    static_cast<void(base_value::*)(bool) const>(&base_value::valueChanged),
                    this,
                    [&](bool f) -> void {qfc.setEnabled(f); qfc.force_redraw();});
            qfc.setEnabled(qfcs[i].checkbox->isChecked());
            qfc.force_redraw();
        }

        connect(&axis.opts.clamp, static_cast<void(base_value::*)(int) const>(&base_value::valueChanged),
                &qfc, [&conf, &qfc](int value) {
            conf.set_max_input(value);
            qfc.reload_spline();
            qfc.set_x_step(value + 1e-2 >= 90 ? 10 : 5);
        });
        // force signal to avoid duplicating the slot's logic
        axis.opts.clamp.valueChanged(axis.opts.clamp);

        if (qfcs[i].axis >= 3)
            qfcs[i].qfc->set_snap(1, 2.5);
        else
            qfcs[i].qfc->set_snap(.5, 1);

        qfcs[i].qfc->setConfig(&conf);
    }

}

void MapWidget::closeEvent(QCloseEvent*)
{
    invalidate_dialog();
}

void MapWidget::save_dialog()
{
    mem<QSettings> settings_ = group::ini_file();
    QSettings& settings = *settings_;

    s.b_map->save_deferred(settings);

    for (int i = 0; i < 6; i++)
    {
        m.forall([&](Map& s)
        {
            s.spline_main.save(settings);
            s.spline_alt.save(settings);
            s.opts.b_mapping_window->save_deferred(settings);
        });
    }
}

void MapWidget::invalidate_dialog()
{
    s.b_map->reload();

    m.forall([](Map& s)
    {
        s.spline_main.reload();
        s.spline_alt.reload();
        s.opts.b_mapping_window->reload();
    });
}

void MapWidget::doOK()
{
    save_dialog();
    close();
}

void MapWidget::doCancel()
{
    invalidate_dialog();
    close();
}