summaryrefslogtreecommitdiff
path: root/src/libaudqt/prefs-builder.cc
blob: 33cd0e95486f3725ed2187858dd6189f93955ad8 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * prefs-builder.cc
 * Copyright 2014 William Pitcock and John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "libaudqt.h"
#include "prefs-widget.h"

#include <QButtonGroup>
#include <QFrame>
#include <QLayout>

#include <libaudcore/i18n.h>
#include <libaudcore/preferences.h>
#include <libaudcore/runtime.h>

namespace audqt {

void prefs_populate (QBoxLayout * layout, ArrayRef<PreferencesWidget> widgets, const char * domain)
{
    /* layout prior to header label */
    QBoxLayout * orig_layout = layout;

    /* layouts prior to check box */
    QBoxLayout * parent_layout = nullptr;
    QBoxLayout * parent_orig_layout = nullptr;

    BooleanWidget * parent_widget = nullptr;
    QButtonGroup * radio_btn_group = nullptr;

    for (const PreferencesWidget & w : widgets)
    {
        if (w.child)
        {
            if (! parent_layout)
            {
                /* save prior layouts */
                parent_layout = layout;
                parent_orig_layout = orig_layout;

                /* create new layout for child widgets */
                if (dynamic_cast<QHBoxLayout *> (parent_layout))
                    layout = new QHBoxLayout;
                else
                {
                    layout = new QVBoxLayout;
                    layout->setContentsMargins (12, 0, 0, 0);
                }

                layout->setSpacing (parent_layout->spacing ());
                parent_layout->addLayout (layout);

                orig_layout = layout;

                if (parent_widget)
                    parent_widget->set_child_layout (layout);
            }
        }
        else
        {
            if (parent_layout)
            {
                /* restore prior layouts */
                layout = parent_layout;
                orig_layout = parent_orig_layout;

                parent_layout = nullptr;
                parent_orig_layout = nullptr;
            }

            /* enable/disable child widgets */
            if (parent_widget)
                parent_widget->update_from_cfg ();

            parent_widget = nullptr;
        }

        if (radio_btn_group && w.type != PreferencesWidget::RadioButton)
            radio_btn_group = nullptr;

        switch (w.type)
        {
        case PreferencesWidget::Button:
            layout->addWidget (new ButtonWidget (& w, domain));
            break;

        case PreferencesWidget::CheckButton:
        {
            auto checkbox = new BooleanWidget (& w, domain);
            layout->addWidget (checkbox);

            if (! w.child)
                parent_widget = checkbox;

            break;
        }

        case PreferencesWidget::Label:
        {
            auto label = new QLabel (translate_str (w.label, domain));

            if (strstr (w.label, "<b>"))
            {
                /* double spacing above a header */
                if (orig_layout->itemAt (0))
                    orig_layout->addSpacing (orig_layout->spacing ());

                orig_layout->addWidget (label);

                /* create indented layout below header */
                layout = new QVBoxLayout;
                layout->setContentsMargins (12, 0, 0, 0);
                layout->setSpacing (orig_layout->spacing ());
                orig_layout->addLayout (layout);
            }
            else
                layout->addWidget (label);

            break;
        }

        case PreferencesWidget::SpinButton:
            switch (w.cfg.type)
            {
            case WidgetConfig::Int:
                layout->addWidget (new IntegerWidget (& w, domain));
                break;
            case WidgetConfig::Float:
                layout->addWidget (new DoubleWidget (& w, domain));
                break;
            default:
                AUDDBG ("encountered unhandled configuration type %d for PreferencesWidget::SpinButton\n", w.cfg.type);
                break;
            }
            break;

        case PreferencesWidget::Entry:
            layout->addWidget (new StringWidget (& w, domain));
            break;

        case PreferencesWidget::RadioButton:
            if (! radio_btn_group)
                radio_btn_group = new QButtonGroup;

            layout->addWidget (new RadioButtonWidget (& w, domain, radio_btn_group));
            break;

        case PreferencesWidget::FontButton:
            /* XXX: unimplemented */
            AUDDBG ("font buttons are unimplemented\n");
            break;

        case PreferencesWidget::ComboBox:
            layout->addWidget (new ComboBoxWidget (& w, domain));
            break;

        case PreferencesWidget::CustomQt:
            if (w.data.populate)
                layout->addWidget ((QWidget *) w.data.populate ());
            break;

        /* layout widgets follow */
        case PreferencesWidget::Box:
            layout->addWidget (new BoxWidget (& w, domain,
             (bool) dynamic_cast<QHBoxLayout *> (layout)));
            break;

        case PreferencesWidget::Table:
            layout->addWidget (new TableWidget (& w, domain));
            break;

        case PreferencesWidget::Notebook:
            layout->addWidget (new NotebookWidget (& w, domain));
            break;

        case PreferencesWidget::Separator:
        {
            QFrame * f = new QFrame;
            f->setFrameShape (w.data.separator.horizontal ? QFrame::HLine : QFrame::VLine);
            f->setFrameShadow (QFrame::Sunken);
            layout->addWidget (f);
            break;
        }

        /* stub handler */
        default:
            AUDDBG ("invoked stub handler for PreferencesWidget type %d\n", w.type);
            break;
        }
    }

    /* enable/disable child widgets */
    if (parent_widget)
        parent_widget->update_from_cfg ();
}

} // namespace audqt