File: mesonoptionbaseview.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (312 lines) | stat: -rw-r--r-- 8,416 bytes parent folder | download
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* This file is part of KDevelop
    Copyright 2019 Daniel Mensinger <daniel@mensinger-ka.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "mesonoptionbaseview.h"

#include "mesonlisteditor.h"
#include "ui_mesonoptionbaseview.h"
#include <debug.h>

#include <KColorScheme>

#include <QCheckBox>
#include <QComboBox>
#include <QLineEdit>
#include <QPalette>
#include <QPushButton>
#include <QSpinBox>
#include <QtGlobal>

using namespace std;

// Helper class for RAII signal blocking
class SignalBlocker
{
public:
    SignalBlocker(QWidget* widget)
        : m_widget(widget)
    {
        if (m_widget) {
            m_widget->blockSignals(true);
        }
    }

    ~SignalBlocker()
    {
        if (m_widget) {
            m_widget->blockSignals(false);
        }
    }

private:
    QWidget* m_widget = nullptr;
};

MesonOptionBaseView::MesonOptionBaseView(MesonOptionPtr option, QWidget* parent)
    : QWidget(parent)
{
    Q_ASSERT(option);

    m_ui = new Ui::MesonOptionBaseView;
    m_ui->setupUi(this);

    m_ui->l_name->setText(option->name() + QStringLiteral(":"));
    m_ui->l_name->setToolTip(option->description());
    setToolTip(option->description());
}

MesonOptionBaseView::~MesonOptionBaseView()
{
    if (m_ui) {
        delete m_ui;
    }
}

// Base class functions

int MesonOptionBaseView::nameWidth()
{
    // Make the name a bit (by 25) wider than it actually is to create a margin. Maybe do
    // something smarter in the future (TODO)
    return m_ui->l_name->fontMetrics().boundingRect(m_ui->l_name->text()).width() + 25;
}

void MesonOptionBaseView::setMinNameWidth(int width)
{
    m_ui->l_name->setMinimumWidth(width);
}

void MesonOptionBaseView::setInputWidget(QWidget* input)
{
    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(input->sizePolicy().hasHeightForWidth());
    input->setSizePolicy(sizePolicy);
    input->setToolTip(option()->description());
    m_ui->layout->insertWidget(1, input);
    updateInput();
    setChanged(false);
}

void MesonOptionBaseView::reset()
{
    option()->reset();
    updateInput();
    setChanged(false);
}

void MesonOptionBaseView::setChanged(bool changed)
{
    KColorScheme scheme(QPalette::Normal);
    KColorScheme::ForegroundRole role;

    if (changed) {
        m_ui->l_name->setStyleSheet(QStringLiteral("font-weight: bold"));
        m_ui->b_reset->setDisabled(false);
        role = KColorScheme::NeutralText;
    } else {
        m_ui->l_name->setStyleSheet(QString());
        m_ui->b_reset->setDisabled(true);
        role = KColorScheme::NormalText;
    }

    QPalette pal = m_ui->l_name->palette();
    pal.setColor(QPalette::WindowText, scheme.foreground(role).color());
    m_ui->l_name->setPalette(pal);
    emit configChanged();
}

std::shared_ptr<MesonOptionBaseView> MesonOptionBaseView::fromOption(MesonOptionPtr option, QWidget* parent)
{
    std::shared_ptr<MesonOptionBaseView> opt = nullptr;
    switch (option->type()) {
    case MesonOptionBase::ARRAY:
        opt = make_shared<MesonOptionArrayView>(option, parent);
        break;
    case MesonOptionBase::BOOLEAN:
        opt = make_shared<MesonOptionBoolView>(option, parent);
        break;
    case MesonOptionBase::COMBO:
        opt = make_shared<MesonOptionComboView>(option, parent);
        break;
    case MesonOptionBase::INTEGER:
        opt = make_shared<MesonOptionIntegerView>(option, parent);
        break;
    case MesonOptionBase::STRING:
        opt = make_shared<MesonOptionStringView>(option, parent);
        break;
    }

    return opt;
}

// Derived class constructors

MesonOptionArrayView::MesonOptionArrayView(MesonOptionPtr option, QWidget* parent)
    : MesonOptionBaseView(option, parent)
    , m_option(dynamic_pointer_cast<MesonOptionArray>(option))
{
    Q_ASSERT(m_option);

    m_input = new QPushButton(this);
    connect(m_input, &QPushButton::clicked, this, [this]() {
        MesonListEditor editor(m_option->rawValue(), this);
        if (editor.exec() == QDialog::Accepted) {
            m_option->setValue(editor.content());
            m_input->setText(m_option->value());
            setChanged(m_option->isUpdated());
        }
    });
    setInputWidget(m_input);
}

MesonOptionBoolView::MesonOptionBoolView(MesonOptionPtr option, QWidget* parent)
    : MesonOptionBaseView(option, parent)
    , m_option(dynamic_pointer_cast<MesonOptionBool>(option))
{
    Q_ASSERT(m_option);

    m_input = new QCheckBox(this);
    connect(m_input, &QCheckBox::stateChanged, this, &MesonOptionBoolView::updated);
    setInputWidget(m_input);
}

MesonOptionComboView::MesonOptionComboView(MesonOptionPtr option, QWidget* parent)
    : MesonOptionBaseView(option, parent)
    , m_option(dynamic_pointer_cast<MesonOptionCombo>(option))
{
    Q_ASSERT(m_option);

    m_input = new QComboBox(this);
    m_input->clear();
    m_input->addItems(m_option->choices());
    m_input->setEditable(false);
    connect(m_input, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MesonOptionComboView::updated);
    setInputWidget(m_input);
}

MesonOptionIntegerView::MesonOptionIntegerView(MesonOptionPtr option, QWidget* parent)
    : MesonOptionBaseView(option, parent)
    , m_option(dynamic_pointer_cast<MesonOptionInteger>(option))
{
    Q_ASSERT(m_option);

    m_input = new QSpinBox(this);
    m_input->setMinimum(INT32_MIN);
    m_input->setMaximum(INT32_MAX);
    connect(m_input, QOverload<int>::of(&QSpinBox::valueChanged), this, &MesonOptionIntegerView::updated);
    setInputWidget(m_input);
}

MesonOptionStringView::MesonOptionStringView(MesonOptionPtr option, QWidget* parent)
    : MesonOptionBaseView(option, parent)
    , m_option(dynamic_pointer_cast<MesonOptionString>(option))
{
    Q_ASSERT(m_option);

    m_input = new QLineEdit(this);
    connect(m_input, &QLineEdit::textChanged, this, &MesonOptionStringView::updated);
    setInputWidget(m_input);
}

// Option getters

MesonOptionBase* MesonOptionArrayView::option()
{
    return m_option.get();
}

MesonOptionBase* MesonOptionBoolView::option()
{
    return m_option.get();
}

MesonOptionBase* MesonOptionComboView::option()
{
    return m_option.get();
}

MesonOptionBase* MesonOptionIntegerView::option()
{
    return m_option.get();
}

MesonOptionBase* MesonOptionStringView::option()
{
    return m_option.get();
}

// Updaters for the input widget

void MesonOptionArrayView::updateInput()
{
    SignalBlocker block(m_input);
    m_input->setText(m_option->value());
}

void MesonOptionBoolView::updateInput()
{
    SignalBlocker block(m_input);
    m_input->setCheckState(m_option->rawValue() ? Qt::Checked : Qt::Unchecked);
}

void MesonOptionComboView::updateInput()
{
    SignalBlocker block(m_input);
    m_input->setCurrentText(m_option->rawValue());
}

void MesonOptionIntegerView::updateInput()
{
    SignalBlocker block(m_input);
    m_input->setValue(m_option->rawValue());
}

void MesonOptionStringView::updateInput()
{
    SignalBlocker block(m_input);
    m_input->setText(m_option->rawValue());
}

// Slots to update the option value

void MesonOptionBoolView::updated()
{
    m_option->setValue(m_input->isChecked());
    setChanged(m_option->isUpdated());
}

void MesonOptionComboView::updated()
{
    m_option->setValue(m_input->currentText());
    setChanged(m_option->isUpdated());
}

void MesonOptionIntegerView::updated()
{
    m_option->setValue(m_input->value());
    setChanged(m_option->isUpdated());
}

void MesonOptionStringView::updated()
{
    m_option->setValue(m_input->text());
    setChanged(m_option->isUpdated());
}