jsonfieldpage.cpp 30.1 KB
Newer Older
1
2
/****************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
6
7
8
9
10
11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
16
17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
27
28
29
30
31
32
33
34
35
36
37
38
39
40
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "jsonfieldpage.h"

#include "jsonwizard.h"
#include "jsonwizardfactory.h"

#include <utils/algorithm.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <utils/textfieldcheckbox.h>
#include <utils/textfieldcombobox.h>
41
#include <utils/theme/theme.h>
42
43

#include <QCheckBox>
44
#include <QApplication>
45
46
47
48
#include <QDebug>
#include <QFormLayout>
#include <QLabel>
#include <QLineEdit>
49
#include <QRegularExpressionValidator>
50
51
52
53
54
#include <QTextEdit>
#include <QVariant>
#include <QVariantMap>
#include <QVBoxLayout>

55
using namespace Utils;
56

57
58
const char NAME_KEY[] = "name";
const char DISPLAY_NAME_KEY[] = "trDisplayName";
59
const char TOOLTIP_KEY[] = "trToolTip";
60
61
62
63
64
65
const char MANDATORY_KEY[] = "mandatory";
const char VISIBLE_KEY[] = "visible";
const char ENABLED_KEY[] = "enabled";
const char SPAN_KEY[] = "span";
const char TYPE_KEY[] = "type";
const char DATA_KEY[] = "data";
66
67
const char IS_COMPLETE_KEY[] = "isComplete";
const char IS_COMPLETE_MESSAGE_KEY[] = "trIncompleteMessage";
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

namespace ProjectExplorer {

// --------------------------------------------------------------------
// Helper:
// --------------------------------------------------------------------

static JsonFieldPage::Field *createFieldData(const QString &type)
{
    if (type == QLatin1String("Label"))
        return new JsonFieldPage::LabelField;
    else if (type == QLatin1String("Spacer"))
        return new JsonFieldPage::SpacerField;
    else if (type == QLatin1String("LineEdit"))
        return new JsonFieldPage::LineEditField;
    else if (type == QLatin1String("TextEdit"))
        return new JsonFieldPage::TextEditField;
    else if (type == QLatin1String("PathChooser"))
        return new JsonFieldPage::PathChooserField;
    else if (type == QLatin1String("CheckBox"))
        return new JsonFieldPage::CheckBoxField;
    else if (type == QLatin1String("ComboBox"))
        return new JsonFieldPage::ComboBoxField;
    return 0;
}

94
95
96
97
98
99
class LineEditValidator : public QRegularExpressionValidator
{
public:
    LineEditValidator(MacroExpander *expander, const QRegularExpression &pattern, QObject *parent) :
        QRegularExpressionValidator(pattern, parent)
    {
100
        m_expander.setDisplayName(JsonFieldPage::tr("Line Edit Validator Expander"));
101
        m_expander.setAccumulating(true);
102
        m_expander.registerVariable("INPUT", JsonFieldPage::tr("The text edit input to fix up."),
103
                                    [this]() { return m_currentInput; });
104
        m_expander.registerSubProvider([expander]() -> MacroExpander * { return expander; });
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
    }

    void setFixupExpando(const QString &expando)
    {
        m_fixupExpando = expando;
    }

    QValidator::State validate(QString &input, int &pos) const
    {
        fixup(input);
        return QRegularExpressionValidator::validate(input, pos);
    }

    void fixup(QString &fixup) const
    {
        if (m_fixupExpando.isEmpty())
            return;

        m_currentInput = fixup;
        fixup = m_expander.expand(m_fixupExpando);
    }

private:
    MacroExpander m_expander;
    QString m_fixupExpando;
    mutable QString m_currentInput;
};

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// --------------------------------------------------------------------
// JsonFieldPage::FieldData:
// --------------------------------------------------------------------

JsonFieldPage::Field *JsonFieldPage::Field::parse(const QVariant &input, QString *errorMessage)
{
    if (input.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "Field is not an object.");
        return 0;
    }

    QVariantMap tmp = input.toMap();
    const QString name = tmp.value(QLatin1String(NAME_KEY)).toString();
    if (name.isEmpty()) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "Field has no name.");
        return 0;
    }
    const QString type = tmp.value(QLatin1String(TYPE_KEY)).toString();
    if (type.isEmpty()) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
155
                                                    "Field \"%1\" has no type.").arg(name);
156
157
158
159
160
161
        return 0;
    }

    Field *data = createFieldData(type);
    if (!data) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
162
                                                    "Field \"%1\" has unsupported type \"%2\".")
163
164
165
166
                .arg(name).arg(type);
        return 0;
    }
    data->name = name;
167
    data->toolTip = tmp.value(QLatin1String(TOOLTIP_KEY)).toString();
168
169
170
171
172

    data->m_visibleExpression = tmp.value(QLatin1String(VISIBLE_KEY), true);
    data->m_enabledExpression = tmp.value(QLatin1String(ENABLED_KEY), true);
    data->mandatory = tmp.value(QLatin1String(MANDATORY_KEY), true).toBool();
    data->span = tmp.value(QLatin1String(SPAN_KEY), false).toBool();
173
174
    data->m_isCompleteExpando = tmp.value(QLatin1String(IS_COMPLETE_KEY), true);
    data->m_isCompleteExpandoMessage = tmp.value(QLatin1String(IS_COMPLETE_MESSAGE_KEY)).toString();
175
176
177
178
179
180

    data->displayName = JsonWizardFactory::localizedString(tmp.value(QLatin1String(DISPLAY_NAME_KEY)).toString());

    QVariant dataVal = tmp.value(QLatin1String(DATA_KEY));
    if (!data->parseData(dataVal, errorMessage)) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
181
                                                    "When parsing Field \"%1\": %2")
182
183
184
185
186
187
188
189
190
191
                .arg(name).arg(*errorMessage);
        delete data;
        return 0;
    }

    return data;
}

void JsonFieldPage::Field::createWidget(JsonFieldPage *page)
{
192
    QWidget *w = widget(displayName, page);
193
194
195
196
197
198
199
200
201
202
203
204
205
    w->setObjectName(name);
    QFormLayout *layout = page->layout();

    if (suppressName())
        layout->addWidget(w);
    else if (span)
        layout->addRow(w);
    else
        layout->addRow(displayName, w);

    setup(page, name);
}

206
void JsonFieldPage::Field::adjustState(MacroExpander *expander)
207
208
209
{
    setVisible(JsonWizard::boolFromVariant(m_visibleExpression, expander));
    setEnabled(JsonWizard::boolFromVariant(m_enabledExpression, expander));
210
211
    QTC_ASSERT(m_widget, return);
    m_widget->setToolTip(expander->expand(toolTip));
212
213
}

214
215
216
217
218
219
220
221
222
223
bool JsonFieldPage::Field::validate(MacroExpander *expander, QString *message)
{
    if (!JsonWizard::boolFromVariant(m_isCompleteExpando, expander)) {
        if (message)
            *message = expander->expand(m_isCompleteExpandoMessage);
        return false;
    }
    return true;
}

224
void JsonFieldPage::Field::initialize(MacroExpander *expander)
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
{
    adjustState(expander);
    initializeData(expander);
}

// --------------------------------------------------------------------
// JsonFieldPage::LabelFieldData:
// --------------------------------------------------------------------

JsonFieldPage::LabelField::LabelField() :
    m_wordWrap(false)
{ }

bool JsonFieldPage::LabelField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "Label data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    m_wordWrap = tmp.value(QLatin1String("wordWrap"), false).toBool();
    m_text = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trText")));

    if (m_text.isEmpty()) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "No text given for Label.");
        return false;
    }

    return true;
}

260
QWidget *JsonFieldPage::LabelField::widget(const QString &displayName, JsonFieldPage *page)
261
262
{
    Q_UNUSED(displayName);
263
    Q_UNUSED(page);
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    QTC_ASSERT(!m_widget, return m_widget);

    QLabel *w = new QLabel();
    w->setWordWrap(m_wordWrap);
    w->setText(m_text);

    m_widget = w;
    return m_widget;
}

// --------------------------------------------------------------------
// JsonFieldPage::SpacerFieldData:
// --------------------------------------------------------------------

JsonFieldPage::SpacerField::SpacerField() :
279
    m_factor(1)
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{ }

bool JsonFieldPage::SpacerField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.isNull())
        return true;

    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "Spacer data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    bool ok;
296
    m_factor = tmp.value(QLatin1String("factor"), 1).toInt(&ok);
297
298
299

    if (!ok) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
300
                                                    "\"factor\" is no integer value.");
301
302
303
304
305
306
        return false;
    }

    return true;
}

307
QWidget *JsonFieldPage::SpacerField::widget(const QString &displayName, JsonFieldPage *page)
308
309
{
    Q_UNUSED(displayName);
310
    Q_UNUSED(page);
311
312
    QTC_ASSERT(!m_widget, return m_widget);

313
314
    int size = qApp->style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing) * m_factor;

315
316
317
318
319
320
321
322
323
324
325
    m_widget = new QWidget();
    m_widget->setMinimumSize(size, size);
    m_widget->setMaximumSize(size, size);
    m_widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    return m_widget;
}

// --------------------------------------------------------------------
// JsonFieldPage::LineEditFieldData:
// --------------------------------------------------------------------

326
JsonFieldPage::LineEditField::LineEditField() : m_isModified(false), m_isValidating(false)
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
{ }

bool JsonFieldPage::LineEditField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.isNull())
        return true;

    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "LineEdit data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    m_defaultText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trText")).toString());
    m_disabledText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trDisabledText")).toString());
    m_placeholderText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trPlaceholder")).toString());
345
346
    QString pattern = tmp.value(QLatin1String("validator")).toString();
    if (!pattern.isEmpty()) {
347
348
        m_validatorRegExp = QRegularExpression(pattern);
        if (!m_validatorRegExp.isValid()) {
349
350
351
            *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                        "Invalid regular expression \"%1\" in \"validator\".")
                    .arg(pattern);
352
            m_validatorRegExp = QRegularExpression();
353
354
355
            return false;
        }
    }
356
    m_fixupExpando = tmp.value(QLatin1String("fixup")).toString();
357
358
359
360

    return true;
}

361
QWidget *JsonFieldPage::LineEditField::widget(const QString &displayName, JsonFieldPage *page)
362
363
364
365
366
{
    Q_UNUSED(displayName);
    QTC_ASSERT(!m_widget, return m_widget);
    QLineEdit *w = new QLineEdit;

367
368
369
370
371
    if (m_validatorRegExp.isValid()) {
        LineEditValidator *lv = new LineEditValidator(page->expander(), m_validatorRegExp, w);
        lv->setFixupExpando(m_fixupExpando);
        w->setValidator(lv);
    }
372

373
374
375
376
377
378
379
380
    m_widget = w;
    return m_widget;
}

void JsonFieldPage::LineEditField::setup(JsonFieldPage *page, const QString &name)
{
    QLineEdit *w = static_cast<QLineEdit *>(m_widget);
    page->registerFieldWithName(name, w);
381
382
    connect(w, &QLineEdit::textChanged,
            page, [this, page]() -> void { m_isModified = true; emit page->completeChanged(); });
383
384
}

385
bool JsonFieldPage::LineEditField::validate(MacroExpander *expander, QString *message)
386
{
387
388
389
    if (!JsonFieldPage::Field::validate(expander, message))
        return false;

390
391
392
393
394
    if (m_isValidating)
        return true;

    m_isValidating = true;

395
396
    QLineEdit *w = static_cast<QLineEdit *>(m_widget);

397
398
399
400
401
402
403
404
405
406
407
408
409
    if (w->isEnabled()) {
        if (m_isModified) {
            if (!m_currentText.isNull()) {
                w->setText(m_currentText);
                m_currentText.clear();
            }
        } else {
            w->setText(expander->expand(m_defaultText));
            m_isModified = false;
        }
    } else {
        if (!m_disabledText.isNull() && m_currentText.isNull())
            m_currentText = w->text();
410
411
    }

412
413
    m_isValidating = false;

414
415
416
    return !w->text().isEmpty();
}

417
void JsonFieldPage::LineEditField::initializeData(MacroExpander *expander)
418
419
420
421
{
    QTC_ASSERT(m_widget, return);

    QLineEdit *w = static_cast<QLineEdit *>(m_widget);
422
    m_isValidating = true;
423
    w->setText(expander->expand(m_defaultText));
424
    w->setPlaceholderText(m_placeholderText);
425
426
    m_isModified = false;
    m_isValidating = false;
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
}

// --------------------------------------------------------------------
// JsonFieldPage::TextEditFieldData:
// --------------------------------------------------------------------


JsonFieldPage::TextEditField::TextEditField() :
    m_acceptRichText(false)
{ }

bool JsonFieldPage::TextEditField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.isNull())
        return true;

    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "TextEdit data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    m_defaultText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trText")).toString());
    m_disabledText = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trDisabledText")).toString());
    m_acceptRichText = tmp.value(QLatin1String("richText"), true).toBool();

    return true;
}

458
QWidget *JsonFieldPage::TextEditField::widget(const QString &displayName, JsonFieldPage *page)
459
460
461
{
    // TODO: Set up modification monitoring...
    Q_UNUSED(displayName);
462
    Q_UNUSED(page);
463
464
465
466
467
468
469
470
471
472
473
474
475
476
    QTC_ASSERT(!m_widget, return m_widget);
    QTextEdit *w = new QTextEdit;
    w->setAcceptRichText(m_acceptRichText);
    m_widget = w;
    return m_widget;
}

void JsonFieldPage::TextEditField::setup(JsonFieldPage *page, const QString &name)
{
    QTextEdit *w = static_cast<QTextEdit *>(m_widget);
    page->registerFieldWithName(name, w, "plainText", SIGNAL(textChanged()));
    connect(w, &QTextEdit::textChanged, page, &QWizardPage::completeChanged);
}

477
bool JsonFieldPage::TextEditField::validate(MacroExpander *expander, QString *message)
478
{
479
480
    if (!JsonFieldPage::Field::validate(expander, message))
        return false;
481
482
483
484
485

    QTextEdit *w = static_cast<QTextEdit *>(m_widget);

    if (!w->isEnabled() && !m_disabledText.isNull() && m_currentText.isNull()) {
        m_currentText = w->toHtml();
486
        w->setPlainText(expander->expand(m_disabledText));
487
488
489
490
491
492
493
494
    } else if (w->isEnabled() && !m_currentText.isNull()) {
        w->setText(m_currentText);
        m_currentText.clear();
    }

    return !w->toPlainText().isEmpty();
}

495
void JsonFieldPage::TextEditField::initializeData(MacroExpander *expander)
496
497
{
    QTextEdit *w = static_cast<QTextEdit *>(m_widget);
498
    w->setPlainText(expander->expand(m_defaultText));
499
500
501
502
503
504
505
}

// --------------------------------------------------------------------
// JsonFieldPage::PathChooserFieldData:
// --------------------------------------------------------------------

JsonFieldPage::PathChooserField::PathChooserField() :
506
    m_kind(PathChooser::ExistingDirectory)
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
{ }

bool JsonFieldPage::PathChooserField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.isNull())
        return true;

    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "PathChooser data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    m_path = tmp.value(QLatin1String("path")).toString();
    m_basePath = tmp.value(QLatin1String("basePath")).toString();

    QString kindStr = tmp.value(QLatin1String("kind"), QLatin1String("existingDirectory")).toString();
    if (kindStr == QLatin1String("existingDirectory")) {
527
        m_kind = PathChooser::ExistingDirectory;
528
    } else if (kindStr == QLatin1String("directory")) {
529
        m_kind = PathChooser::Directory;
530
    } else if (kindStr == QLatin1String("file")) {
531
        m_kind = PathChooser::File;
532
    } else if (kindStr == QLatin1String("saveFile")) {
533
        m_kind = PathChooser::SaveFile;
534
    } else if (kindStr == QLatin1String("existingCommand")) {
535
        m_kind = PathChooser::ExistingCommand;
536
    } else if (kindStr == QLatin1String("command")) {
537
        m_kind = PathChooser::Command;
538
    } else if (kindStr == QLatin1String("any")) {
539
        m_kind = PathChooser::Any;
540
541
    } else {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
542
543
544
                                                    "kind \"%1\" is not one of the supported \"existingDirectory\", "
                                                    "\"directory\", \"file\", \"saveFile\", \"existingCommand\", "
                                                    "\"command\", \"any\".")
545
546
547
548
549
550
551
                .arg(kindStr);
        return false;
    }

    return true;
}

552
QWidget *JsonFieldPage::PathChooserField::widget(const QString &displayName, JsonFieldPage *page)
553
554
{
    Q_UNUSED(displayName);
555
    Q_UNUSED(page);
556
    QTC_ASSERT(!m_widget, return m_widget);
557
    m_widget = new PathChooser;
558
559
560
561
562
563
    return m_widget;
}

void JsonFieldPage::PathChooserField::setEnabled(bool e)
{
    QTC_ASSERT(m_widget, return);
564
    PathChooser *w = static_cast<PathChooser *>(m_widget);
565
566
567
568
569
    w->setReadOnly(!e);
}

void JsonFieldPage::PathChooserField::setup(JsonFieldPage *page, const QString &name)
{
570
    PathChooser *w = static_cast<PathChooser *>(m_widget);
571
    page->registerFieldWithName(name, w, "path", SIGNAL(changed(QString)));
572
    connect(w, &PathChooser::changed, page, [page](QString) { page->completeChanged(); });
573
574
}

575
bool JsonFieldPage::PathChooserField::validate(MacroExpander *expander, QString *message)
576
{
577
578
579
    if (!JsonFieldPage::Field::validate(expander, message))
        return false;

580
    PathChooser *w = static_cast<PathChooser *>(m_widget);
581
582
583
    return w->isValid();
}

584
void JsonFieldPage::PathChooserField::initializeData(MacroExpander *expander)
585
586
{
    QTC_ASSERT(m_widget, return);
587
588
    PathChooser *w = static_cast<PathChooser *>(m_widget);
    w->setBaseDirectory(expander->expand(m_basePath));
589
590
591
    w->setExpectedKind(m_kind);

    if (m_currentPath.isNull())
592
        w->setPath(expander->expand(m_path));
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
    else
        w->setPath(m_currentPath);
}

// --------------------------------------------------------------------
// JsonFieldPage::CheckBoxFieldData:
// --------------------------------------------------------------------

JsonFieldPage::CheckBoxField::CheckBoxField() :
    m_checkedValue(QLatin1String("0")),
    m_uncheckedValue(QLatin1String("1")),
    m_isModified(false)
{ }

bool JsonFieldPage::CheckBoxField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.isNull())
        return true;

    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "CheckBox data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    m_checkedValue = tmp.value(QLatin1String("checkedValue"), QLatin1String("0")).toString();
    m_uncheckedValue = tmp.value(QLatin1String("uncheckedValue"), QLatin1String("1")).toString();
    if (m_checkedValue == m_uncheckedValue) {
        *errorMessage= QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                   "CheckBox values for checked and unchecked state are identical.");
       return false;
    }
    m_checkedExpression = tmp.value(QLatin1String("checked"), false);

    return true;
}

632
QWidget *JsonFieldPage::CheckBoxField::widget(const QString &displayName, JsonFieldPage *page)
633
{
634
    Q_UNUSED(page);
635
    QTC_ASSERT(!m_widget, return m_widget);
636
    TextFieldCheckBox *w = new TextFieldCheckBox(displayName);
637
638
639
640
641
642
    m_widget = w;
    return m_widget;
}

void JsonFieldPage::CheckBoxField::setup(JsonFieldPage *page, const QString &name)
{
643
    TextFieldCheckBox *w = static_cast<TextFieldCheckBox *>(m_widget);
644
    connect(w, &TextFieldCheckBox::clicked, [this, page]() { m_isModified = true; page->completeChanged();});
645
646
647
    page->registerFieldWithName(name, w, "text", SIGNAL(textChanged(QString)));
}

648
bool JsonFieldPage::CheckBoxField::validate(MacroExpander *expander, QString *message)
649
{
650
651
652
    if (!JsonFieldPage::Field::validate(expander, message))
        return false;

653
    if (!m_isModified) {
654
        TextFieldCheckBox *w = static_cast<TextFieldCheckBox *>(m_widget);
655
656
657
658
659
        w->setChecked(JsonWizard::boolFromVariant(m_checkedExpression, expander));
    }
    return true;
}

660
void JsonFieldPage::CheckBoxField::initializeData(MacroExpander *expander)
661
662
663
{
    QTC_ASSERT(m_widget, return);

664
665
666
    TextFieldCheckBox *w = static_cast<TextFieldCheckBox *>(m_widget);
    w->setTrueText(expander->expand(m_checkedValue));
    w->setFalseText(expander->expand(m_uncheckedValue));
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690

    w->setChecked(JsonWizard::boolFromVariant(m_checkedExpression, expander));
}

// --------------------------------------------------------------------
// JsonFieldPage::ComboBoxFieldData:
// --------------------------------------------------------------------

JsonFieldPage::ComboBoxField::ComboBoxField() :
    m_index(-1), m_disabledIndex(-1), m_savedIndex(-1), m_currentIndex(-1)
{ }

QPair<QString, QString> parseComboBoxItem(const QVariant &item, QString *errorMessage)
{
    if (item.type() == QVariant::List) {
        *errorMessage  = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                     "No lists allowed inside ComboBox items list.");
        return qMakePair(QString(), QString());
    } else if (item.type() == QVariant::Map) {
        QVariantMap tmp = item.toMap();
        QString key = JsonWizardFactory::localizedString(tmp.value(QLatin1String("trKey"), QString()).toString());
        QString value = tmp.value(QLatin1String("value"), QString()).toString();
        if (key.isNull() || key.isEmpty()) {
            *errorMessage  = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
691
                                                         "No \"key\" found in ComboBox items.");
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
            return qMakePair(QString(), QString());
        }
        if (value.isNull())
            value = key;
        return qMakePair(key, value);
    } else {
        QString keyvalue = item.toString();
        return qMakePair(keyvalue, keyvalue);
    }
}

bool JsonFieldPage::ComboBoxField::parseData(const QVariant &data, QString *errorMessage)
{
    if (data.type() != QVariant::Map) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
                                                    "ComboBox data is not an object.");
        return false;
    }

    QVariantMap tmp = data.toMap();

    bool ok;
    m_index = tmp.value(QLatin1String("index"), 0).toInt(&ok);
    if (!ok) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
717
                                                    "ComboBox \"index\" is not an integer value.");
718
719
720
721
722
        return false;
    }
    m_disabledIndex = tmp.value(QLatin1String("disabledIndex"), -1).toInt(&ok);
    if (!ok) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
723
                                                    "ComboBox \"disabledIndex\" is not an integer value.");
724
725
726
727
728
729
        return false;
    }

    QVariant value = tmp.value(QLatin1String("items"));
    if (value.isNull()) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
730
                                                    "ComboBox \"items\" missing.");
731
732
733
734
        return false;
    }
    if (value.type() != QVariant::List) {
        *errorMessage = QCoreApplication::translate("ProjectExplorer::JsonFieldPage",
735
                                                    "ComboBox \"items\" is not a list.");
736
737
738
739
740
741
742
743
744
745
746
747
748
749
        return false;
    }

    foreach (const QVariant &i, value.toList()) {
        QPair<QString, QString> keyValue = parseComboBoxItem(i, errorMessage);
        if (keyValue.first.isNull())
            return false; // an error happened...
        m_itemList.append(keyValue.first);
        m_itemDataList.append(keyValue.second);
    }

    return true;
}

750
QWidget *JsonFieldPage::ComboBoxField::widget(const QString &displayName, JsonFieldPage *page)
751
752
{
    Q_UNUSED(displayName);
753
    Q_UNUSED(page);
754
    QTC_ASSERT(!m_widget, return m_widget);
755
    m_widget = new TextFieldComboBox;
756
757
758
759
760
    return m_widget;
}

void JsonFieldPage::ComboBoxField::setup(JsonFieldPage *page, const QString &name)
{
761
    TextFieldComboBox *w = static_cast<TextFieldComboBox *>(m_widget);
762
    page->registerFieldWithName(name, w, "text", SIGNAL(text4Changed(QString)));
763
    connect(w, &TextFieldComboBox::text4Changed, page, [page](QString) { page->completeChanged(); });
764
765
}

766
bool JsonFieldPage::ComboBoxField::validate(MacroExpander *expander, QString *message)
767
{
768
769
    if (!JsonFieldPage::Field::validate(expander, message))
        return false;
770

771
    TextFieldComboBox *w = static_cast<TextFieldComboBox *>(m_widget);
772
773
774
775
776
777
778
779
780
781
782
    if (!w->isEnabled() && m_disabledIndex >= 0 && m_savedIndex < 0) {
        m_savedIndex = w->currentIndex();
        w->setCurrentIndex(m_disabledIndex);
    } else if (w->isEnabled() && m_savedIndex >= 0) {
        w->setCurrentIndex(m_savedIndex);
        m_savedIndex = -1;
    }

    return true;
}

783
void JsonFieldPage::ComboBoxField::initializeData(MacroExpander *expander)
784
{
785
    TextFieldComboBox *w = static_cast<TextFieldComboBox *>(m_widget);
786
787
    QStringList tmpItems
            = Utils::transform(m_itemList,
788
                               [expander](const QString &i) { return expander->expand(i); });
789
790
    QStringList tmpData
            = Utils::transform(m_itemDataList,
791
                               [expander](const QString &i) { return expander->expand(i); });
792
793
794
795
796
797
798
799
800
801
802
803
804
    w->setItems(tmpItems, tmpData);
    w->setInsertPolicy(QComboBox::NoInsert);

    if (m_currentIndex >= 0)
        w->setCurrentIndex(m_currentIndex);
    else
        w->setCurrentIndex(m_index);
}

// --------------------------------------------------------------------
// JsonFieldPage:
// --------------------------------------------------------------------

805
806
JsonFieldPage::JsonFieldPage(MacroExpander *expander, QWidget *parent) :
    WizardPage(parent),
807
808
809
810
811
812
813
814
815
816
    m_formLayout(new QFormLayout),
    m_errorLabel(new QLabel),
    m_expander(expander)
{
    QTC_CHECK(m_expander);

    QVBoxLayout *vLayout = new QVBoxLayout;
    m_formLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
    vLayout->addLayout(m_formLayout);
    m_errorLabel->setVisible(false);
817
818
819
    QPalette palette = m_errorLabel->palette();
    palette.setColor(QPalette::WindowText, creatorTheme()->color(Theme::TextColorError));
    m_errorLabel->setPalette(palette);
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
    vLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
    vLayout->addWidget(m_errorLabel);
    setLayout(vLayout);
}

JsonFieldPage::~JsonFieldPage()
{
    // Do not delete m_expander, it belongs to the wizard!
    qDeleteAll(m_fields);
}

bool JsonFieldPage::setup(const QVariant &data)
{
    QString errorMessage;
    QList<QVariant> fieldList = JsonWizardFactory::objectOrList(data, &errorMessage);
    foreach (const QVariant &field, fieldList) {
        Field *f = JsonFieldPage::Field::parse(field, &errorMessage);
        if (!f)
            continue;
        f->createWidget(this);
        m_fields.append(f);
    }

    return true;
}

bool JsonFieldPage::isComplete() const
{
    QString message;

    bool result = true;
    bool hasErrorMessage = false;
    foreach (Field *f, m_fields) {
        f->adjustState(m_expander);
        if (!f->validate(m_expander, &message)) {
            if (!message.isEmpty()) {
                showError(message);
                hasErrorMessage = true;
            }
            if (f->mandatory)
                result = false;
        }
    }

    if (!hasErrorMessage)
        clearError();

    return result;
}

void JsonFieldPage::initializePage()
{
    foreach (Field *f, m_fields)
        f->initialize(m_expander);
}

void JsonFieldPage::cleanupPage()
{
    foreach (Field *f, m_fields)
        f->cleanup(m_expander);
}

void JsonFieldPage::showError(const QString &m) const
{
    m_errorLabel->setText(m);
    m_errorLabel->setVisible(true);
}

void JsonFieldPage::clearError() const
{
    m_errorLabel->setText(QString());
    m_errorLabel->setVisible(false);
}

894
MacroExpander *JsonFieldPage::expander()
895
896
897
898
899
{
    return m_expander;
}

} // namespace ProjectExplorer