summaryrefslogtreecommitdiffhomepage
path: root/options/tie.hpp
blob: 22b46bd0960c4bc460a6e93380a052fed2ff1c38 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once

#include "value.hpp"

#include <QComboBox>
#include <QCheckBox>
#include <QDoubleSpinBox>
#include <QSpinBox>
#include <QSlider>
#include <QLineEdit>
#include <QLabel>
#include <QTabWidget>

#include <cmath>

namespace options {

template<typename t, typename q>
inline void tie_setting(value<t>&, q*);

template<typename t>
inline
typename std::enable_if<std::is_enum<t>::value>::type
tie_setting(value<t>& v, QComboBox* cb)
{
    cb->setCurrentIndex(cb->findData((unsigned)static_cast<t>(v)));
    v = static_cast<t>(cb->currentData().toInt());

    std::vector<int> enum_cases(unsigned(cb->count()));

    for (int i = 0; i < cb->count(); i++)
        enum_cases[i] = cb->itemData(i).toInt();

    base_value::connect(cb,
                        static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
                        &v,
                        [&, enum_cases](int idx) {
                            if (idx < 0 || idx >= (int)enum_cases.size())
                                v = static_cast<t>(-1);
                            else
                                v = static_cast<t>(enum_cases[idx]);
                        },
                        v.DIRECT_CONNTYPE);
    base_value::connect(&v,
                        static_cast<void (base_value::*)(int) const>(&base_value::valueChanged),
                        cb,
                        [&, enum_cases](int val) {
                            for (unsigned i = 0; i < enum_cases.size(); i++)
                            {
                                if (val == enum_cases[i])
                                {
                                    cb->setCurrentIndex(i);
                                    return;
                                }
                            }
                            cb->setCurrentIndex(-1);
                        },
                        // don't change or else hatire crashes -sh 20160917
                        Qt::QueuedConnection);
}

template<>
inline void tie_setting(value<int>& v, QComboBox* cb)
{
    cb->setCurrentIndex(v);
    v = cb->currentIndex();
    base_value::connect(cb, SIGNAL(currentIndexChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(int)), cb, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<QString>& v, QComboBox* cb)
{
    cb->setCurrentText(v);
    v = cb->currentText();
    base_value::connect(cb, SIGNAL(currentTextChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(QString)), cb, SLOT(setCurrentText(QString)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<bool>& v, QCheckBox* cb)
{
    cb->setChecked(v);
    base_value::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<double>& v, QDoubleSpinBox* dsb)
{
    dsb->setValue(v);
    base_value::connect(dsb, SIGNAL(valueChanged(double)), &v, SLOT(setValue(double)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(double)), dsb, SLOT(setValue(double)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<int>& v, QSpinBox* sb)
{
    sb->setValue(v);
    base_value::connect(sb, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<int>& v, QSlider* sl)
{
    sl->setValue(v);
    v = sl->value();
    base_value::connect(sl, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(int)), sl, SLOT(setValue(int)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<QString>& v, QLineEdit* le)
{
    le->setText(v);
    base_value::connect(le, SIGNAL(textChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(QString)),le, SLOT(setText(QString)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<QString>& v, QLabel* lb)
{
    lb->setText(v);
    base_value::connect(&v, SIGNAL(valueChanged(QString)), lb, SLOT(setText(QString)), v.DIRECT_CONNTYPE);
}

template<>
inline void tie_setting(value<int>& v, QTabWidget* t)
{
    t->setCurrentIndex(v);
    base_value::connect(t, SIGNAL(currentChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE);
    base_value::connect(&v, SIGNAL(valueChanged(int)), t, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE);
}

template<>
inline void tie_setting(value<slider_value>& v, QSlider* w)
{
    // we can't get these at runtime since signals cross threads
    const int q_min = w->minimum();
    const int q_max = w->maximum();

    {
        const int q_diff = q_max - q_min;
        slider_value sv(v);
        const double sv_c = sv.max() - sv.min();

        w->setValue(int((sv.cur() - sv.min()) / sv_c * q_diff + q_min));
        v = slider_value(q_diff <= 0 ? 0 : (w->value() - q_min) * sv_c / (double)q_diff + sv.min(), sv.min(), sv.max());
    }

    base_value::connect(w,
                        &QSlider::valueChanged,
                        &v,
                        [=, &v](int pos) { v = v->update_from_slider(pos, q_min, q_max); },
                        v.DIRECT_CONNTYPE);
    base_value::connect(&v,
                        static_cast<void(base_value::*)(double) const>(&base_value::valueChanged),
                        w,
                        [=, &v](double) { w->setValue(v->to_slider_pos(q_min, q_max)); },
                        v.SAFE_CONNTYPE);
}

}