summaryrefslogtreecommitdiff
path: root/src/plugins/projectexplorer/projectconfigurationaspects.cpp
blob: 60b1ff400bd19a7d16fafa4bff45f780483ef901 (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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** 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
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "projectconfigurationaspects.h"

#include "environmentaspect.h"
#include "project.h"
#include "projectexplorer.h"
#include "projectexplorersettings.h"
#include "runconfiguration.h"
#include "target.h"

#include <utils/utilsicons.h>
#include <utils/fancylineedit.h>
#include <utils/pathchooser.h>
#include <utils/qtcprocess.h>

#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QFormLayout>
#include <QSpinBox>
#include <QToolButton>
#include <QTextEdit>
#include <QRadioButton>
#include <QButtonGroup>

using namespace Utils;

namespace ProjectExplorer {
namespace Internal {

class BaseBoolAspectPrivate
{
public:
    bool m_value = false;
    bool m_defaultValue = false;
    QString m_label;
    QString m_tooltip;
    QPointer<QCheckBox> m_checkBox; // Owned by configuration widget
};

class BaseSelectionAspectPrivate
{
public:
    int m_value = 0;
    int m_defaultValue = 0;
    struct Option { QString displayName; QString tooltip; };
    QVector<Option> m_options;
    QList<QPointer<QRadioButton>> m_buttons; // Owned by configuration widget
    QPointer<QButtonGroup> m_buttonGroup;
};

class BaseStringAspectPrivate
{
public:
    BaseStringAspect::DisplayStyle m_displayStyle = BaseStringAspect::LabelDisplay;
    QString m_labelText;
    std::function<QString(const QString &)> m_displayFilter;
    std::unique_ptr<BaseBoolAspect> m_checker;

    QString m_value;
    QString m_placeHolderText;
    QString m_historyCompleterKey;
    PathChooser::Kind m_expectedKind = PathChooser::File;
    Environment m_environment;
    QPointer<QLabel> m_label;
    QPointer<QLabel> m_labelDisplay;
    QPointer<FancyLineEdit> m_lineEditDisplay;
    QPointer<PathChooser> m_pathChooserDisplay;
    QPointer<QTextEdit> m_textEditDisplay;
    QPixmap m_labelPixmap;
    Utils::FilePath m_baseFileName;
    bool m_readOnly = false;
    bool m_showToolTipOnLabel = false;
};

class BaseIntegerAspectPrivate
{
public:
    qint64 m_value = 0;
    QVariant m_minimumValue;
    QVariant m_maximumValue;
    int m_displayIntegerBase = 10;
    qint64 m_displayScaleFactor = 1;
    QString m_label;
    QString m_prefix;
    QString m_suffix;
    QPointer<QSpinBox> m_spinBox; // Owned by configuration widget
};

} // Internal

/*!
    \class ProjectExplorer::BaseStringAspect
*/

BaseStringAspect::BaseStringAspect()
    : d(new Internal::BaseStringAspectPrivate)
{}

BaseStringAspect::~BaseStringAspect() = default;

QString BaseStringAspect::value() const
{
    return d->m_value;
}

void BaseStringAspect::setValue(const QString &value)
{
    const bool isSame = value == d->m_value;
    d->m_value = value;
    update();
    if (!isSame)
        emit changed();
}

void BaseStringAspect::fromMap(const QVariantMap &map)
{
    if (!settingsKey().isEmpty())
        d->m_value = map.value(settingsKey()).toString();
    if (d->m_checker)
        d->m_checker->fromMap(map);
}

void BaseStringAspect::toMap(QVariantMap &map) const
{
    if (!settingsKey().isEmpty())
        map.insert(settingsKey(), d->m_value);
    if (d->m_checker)
        d->m_checker->toMap(map);
}

FilePath BaseStringAspect::filePath() const
{
    return FilePath::fromString(d->m_value);
}

void BaseStringAspect::setFilePath(const FilePath &val)
{
    setValue(val.toString());
}

void BaseStringAspect::setLabelText(const QString &labelText)
{
    d->m_labelText = labelText;
    if (d->m_label)
        d->m_label->setText(labelText);
}

void BaseStringAspect::setLabelPixmap(const QPixmap &labelPixmap)
{
    d->m_labelPixmap = labelPixmap;
    if (d->m_label)
        d->m_label->setPixmap(labelPixmap);
}

void BaseStringAspect::setShowToolTipOnLabel(bool show)
{
    d->m_showToolTipOnLabel = show;
    update();
}

QString BaseStringAspect::labelText() const
{
    return d->m_labelText;
}

void BaseStringAspect::setDisplayFilter(const std::function<QString(const QString &)> &displayFilter)
{
    d->m_displayFilter = displayFilter;
}

bool BaseStringAspect::isChecked() const
{
    return !d->m_checker || d->m_checker->value();
}

void BaseStringAspect::setDisplayStyle(DisplayStyle displayStyle)
{
    d->m_displayStyle = displayStyle;
}

void BaseStringAspect::setPlaceHolderText(const QString &placeHolderText)
{
    d->m_placeHolderText = placeHolderText;
    if (d->m_lineEditDisplay)
        d->m_lineEditDisplay->setPlaceholderText(placeHolderText);
    if (d->m_textEditDisplay)
        d->m_textEditDisplay->setPlaceholderText(placeHolderText);
}

void BaseStringAspect::setHistoryCompleter(const QString &historyCompleterKey)
{
    d->m_historyCompleterKey = historyCompleterKey;
    if (d->m_lineEditDisplay)
        d->m_lineEditDisplay->setHistoryCompleter(historyCompleterKey);
    if (d->m_pathChooserDisplay)
        d->m_pathChooserDisplay->setHistoryCompleter(historyCompleterKey);
}

void BaseStringAspect::setExpectedKind(const PathChooser::Kind expectedKind)
{
    d->m_expectedKind = expectedKind;
    if (d->m_pathChooserDisplay)
        d->m_pathChooserDisplay->setExpectedKind(expectedKind);
}

void BaseStringAspect::setEnvironment(const Environment &env)
{
    d->m_environment = env;
    if (d->m_pathChooserDisplay)
        d->m_pathChooserDisplay->setEnvironment(env);
}

void BaseStringAspect::setBaseFileName(const FilePath &baseFileName)
{
    d->m_baseFileName = baseFileName;
    if (d->m_pathChooserDisplay)
        d->m_pathChooserDisplay->setBaseFileName(baseFileName);
}

void BaseStringAspect::setReadOnly(bool readOnly)
{
    d->m_readOnly = readOnly;
    if (d->m_pathChooserDisplay)
        d->m_pathChooserDisplay->setReadOnly(readOnly);
    if (d->m_lineEditDisplay)
        d->m_lineEditDisplay->setReadOnly(readOnly);
    if (d->m_textEditDisplay)
        d->m_textEditDisplay->setReadOnly(readOnly);
}

void BaseStringAspect::addToLayout(LayoutBuilder &builder)
{
    QTC_CHECK(!d->m_label);
    d->m_label = new QLabel;
    d->m_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
    d->m_label->setText(d->m_labelText);
    if (!d->m_labelPixmap.isNull())
        d->m_label->setPixmap(d->m_labelPixmap);
    builder.addItem(d->m_label.data());

    switch (d->m_displayStyle) {
    case PathChooserDisplay:
        d->m_pathChooserDisplay = new PathChooser;
        d->m_pathChooserDisplay->setExpectedKind(d->m_expectedKind);
        if (!d->m_historyCompleterKey.isEmpty())
            d->m_pathChooserDisplay->setHistoryCompleter(d->m_historyCompleterKey);
        d->m_pathChooserDisplay->setEnvironment(d->m_environment);
        d->m_pathChooserDisplay->setBaseFileName(d->m_baseFileName);
        d->m_pathChooserDisplay->setReadOnly(d->m_readOnly);
        connect(d->m_pathChooserDisplay, &PathChooser::pathChanged,
                this, &BaseStringAspect::setValue);
        builder.addItem(d->m_pathChooserDisplay.data());
        break;
    case LineEditDisplay:
        d->m_lineEditDisplay = new FancyLineEdit;
        d->m_lineEditDisplay->setPlaceholderText(d->m_placeHolderText);
        if (!d->m_historyCompleterKey.isEmpty())
            d->m_lineEditDisplay->setHistoryCompleter(d->m_historyCompleterKey);
        d->m_lineEditDisplay->setReadOnly(d->m_readOnly);
        connect(d->m_lineEditDisplay, &FancyLineEdit::textEdited,
                this, &BaseStringAspect::setValue);
        builder.addItem(d->m_lineEditDisplay.data());
        break;
    case TextEditDisplay:
        d->m_textEditDisplay = new QTextEdit;
        d->m_textEditDisplay->setPlaceholderText(d->m_placeHolderText);
        d->m_textEditDisplay->setReadOnly(d->m_readOnly);
        connect(d->m_textEditDisplay, &QTextEdit::textChanged, this, [this] {
            const QString value = d->m_textEditDisplay->document()->toPlainText();
            if (value != d->m_value) {
                d->m_value = value;
                emit changed();
            }
        });
        builder.addItem(d->m_textEditDisplay.data());
        break;
    case LabelDisplay:
        d->m_labelDisplay = new QLabel;
        d->m_labelDisplay->setTextInteractionFlags(Qt::TextSelectableByMouse);
        builder.addItem(d->m_labelDisplay.data());
        break;
    }

    if (d->m_checker)
        d->m_checker->addToLayout(builder);

    update();
}

void BaseStringAspect::update()
{
    const QString displayedString = d->m_displayFilter ? d->m_displayFilter(d->m_value)
                                                       : d->m_value;

    const bool enabled = !d->m_checker || d->m_checker->value();

    if (d->m_pathChooserDisplay) {
        d->m_pathChooserDisplay->setFileName(FilePath::fromString(displayedString));
        d->m_pathChooserDisplay->setEnabled(enabled);
    }

    if (d->m_lineEditDisplay) {
        d->m_lineEditDisplay->setTextKeepingActiveCursor(displayedString);
        d->m_lineEditDisplay->setEnabled(enabled);
    }

    if (d->m_textEditDisplay) {
        d->m_textEditDisplay->setText(displayedString);
        d->m_textEditDisplay->setEnabled(enabled);
    }

    if (d->m_labelDisplay) {
        d->m_labelDisplay->setText(displayedString);
        d->m_labelDisplay->setToolTip(d->m_showToolTipOnLabel ? displayedString : QString());
    }

    if (d->m_label) {
        d->m_label->setText(d->m_labelText);
        if (!d->m_labelPixmap.isNull())
            d->m_label->setPixmap(d->m_labelPixmap);
    }
}

void BaseStringAspect::makeCheckable(const QString &checkerLabel, const QString &checkerKey)
{
    QTC_ASSERT(!d->m_checker, return);
    d->m_checker.reset(new BaseBoolAspect);
    d->m_checker->setLabel(checkerLabel);
    d->m_checker->setSettingsKey(checkerKey);

    connect(d->m_checker.get(), &BaseBoolAspect::changed, this, &BaseStringAspect::update);
    connect(d->m_checker.get(), &BaseBoolAspect::changed, this, &BaseStringAspect::changed);

    update();
}

/*!
    \class ProjectExplorer::BaseBoolAspect
*/

BaseBoolAspect::BaseBoolAspect(const QString &settingsKey)
    : d(new Internal::BaseBoolAspectPrivate)
{
    setSettingsKey(settingsKey);
}

BaseBoolAspect::~BaseBoolAspect() = default;

void BaseBoolAspect::addToLayout(LayoutBuilder &builder)
{
    QTC_CHECK(!d->m_checkBox);
    d->m_checkBox = new QCheckBox(d->m_label);
    d->m_checkBox->setChecked(d->m_value);
    d->m_checkBox->setToolTip(d->m_tooltip);
    builder.addItem(QString());
    builder.addItem(d->m_checkBox.data());
    connect(d->m_checkBox.data(), &QAbstractButton::clicked, this, [this] {
        d->m_value = d->m_checkBox->isChecked();
        emit changed();
    });
}

void BaseBoolAspect::fromMap(const QVariantMap &map)
{
    d->m_value = map.value(settingsKey(), d->m_defaultValue).toBool();
}

void BaseBoolAspect::toMap(QVariantMap &data) const
{
    data.insert(settingsKey(), d->m_value);
}

bool BaseBoolAspect::defaultValue() const
{
    return d->m_defaultValue;
}

void BaseBoolAspect::setDefaultValue(bool defaultValue)
{
    d->m_defaultValue = defaultValue;
    d->m_value = defaultValue;
}

bool BaseBoolAspect::value() const
{
    return d->m_value;
}

void BaseBoolAspect::setValue(bool value)
{
    d->m_value = value;
    if (d->m_checkBox)
        d->m_checkBox->setChecked(d->m_value);
}

void BaseBoolAspect::setLabel(const QString &label)
{
    d->m_label = label;
}

void BaseBoolAspect::setToolTip(const QString &tooltip)
{
    d->m_tooltip = tooltip;
}

/*!
    \class ProjectExplorer::BaseSelectionAspect
*/

BaseSelectionAspect::BaseSelectionAspect()
    : d(new Internal::BaseSelectionAspectPrivate)
{}

BaseSelectionAspect::~BaseSelectionAspect() = default;

void BaseSelectionAspect::addToLayout(LayoutBuilder &builder)
{
    QTC_CHECK(d->m_buttonGroup == nullptr);
    d->m_buttonGroup = new QButtonGroup;
    d->m_buttonGroup->setExclusive(true);

    QTC_ASSERT(d->m_buttons.isEmpty(), d->m_buttons.clear());
    for (int i = 0, n = d->m_options.size(); i < n; ++i) {
        const Internal::BaseSelectionAspectPrivate::Option &option = d->m_options.at(i);
        auto button = new QRadioButton(option.displayName);
        button->setChecked(i == d->m_value);
        button->setToolTip(option.tooltip);
        builder.addItem(QString());
        builder.addItem(button);
        d->m_buttons.append(button);
        d->m_buttonGroup->addButton(button);
        connect(button, &QAbstractButton::clicked, this, [this, i] {
            d->m_value = i;
            emit changed();
        });
    }
}

void BaseSelectionAspect::fromMap(const QVariantMap &map)
{
    d->m_value = map.value(settingsKey(), d->m_defaultValue).toInt();
}

void BaseSelectionAspect::toMap(QVariantMap &data) const
{
    data.insert(settingsKey(), d->m_value);
}

int BaseSelectionAspect::defaultValue() const
{
    return d->m_defaultValue;
}

void BaseSelectionAspect::setDefaultValue(int defaultValue)
{
    d->m_defaultValue = defaultValue;
}

int BaseSelectionAspect::value() const
{
    return d->m_value;
}

void BaseSelectionAspect::setValue(int value)
{
    d->m_value = value;
    if (d->m_buttonGroup && 0 <= value && value < d->m_buttons.size())
        d->m_buttons.at(value)->setChecked(true);
}

void BaseSelectionAspect::addOption(const QString &displayName, const QString &toolTip)
{
    d->m_options.append({displayName, toolTip});
}

/*!
    \class ProjectExplorer::BaseIntegerAspect
*/

// BaseIntegerAspect

BaseIntegerAspect::BaseIntegerAspect()
    : d(new Internal::BaseIntegerAspectPrivate)
{}

BaseIntegerAspect::~BaseIntegerAspect() = default;

void BaseIntegerAspect::addToLayout(LayoutBuilder &builder)
{
    QTC_CHECK(!d->m_spinBox);
    d->m_spinBox = new QSpinBox;
    d->m_spinBox->setValue(int(d->m_value / d->m_displayScaleFactor));
    d->m_spinBox->setDisplayIntegerBase(d->m_displayIntegerBase);
    d->m_spinBox->setPrefix(d->m_prefix);
    d->m_spinBox->setSuffix(d->m_suffix);
    if (d->m_maximumValue.isValid() && d->m_maximumValue.isValid())
        d->m_spinBox->setRange(int(d->m_minimumValue.toLongLong() / d->m_displayScaleFactor),
                               int(d->m_maximumValue.toLongLong() / d->m_displayScaleFactor));
    builder.addItem(d->m_label);
    builder.addItem(d->m_spinBox.data());
    connect(d->m_spinBox.data(), QOverload<int>::of(&QSpinBox::valueChanged),
            this, [this](int value) {
        d->m_value = value * d->m_displayScaleFactor;
        emit changed();
    });
}

void BaseIntegerAspect::fromMap(const QVariantMap &map)
{
    d->m_value = map.value(settingsKey()).toLongLong();
}

void BaseIntegerAspect::toMap(QVariantMap &data) const
{
    data.insert(settingsKey(), d->m_value);
}

qint64 BaseIntegerAspect::value() const
{
    return d->m_value;
}

void BaseIntegerAspect::setValue(qint64 value)
{
    d->m_value = value;
    if (d->m_spinBox)
        d->m_spinBox->setValue(int(d->m_value / d->m_displayScaleFactor));
}

void BaseIntegerAspect::setRange(qint64 min, qint64 max)
{
    d->m_minimumValue = min;
    d->m_maximumValue = max;
}

void BaseIntegerAspect::setLabel(const QString &label)
{
    d->m_label = label;
}

void BaseIntegerAspect::setPrefix(const QString &prefix)
{
    d->m_prefix = prefix;
}

void BaseIntegerAspect::setSuffix(const QString &suffix)
{
    d->m_suffix = suffix;
}

void BaseIntegerAspect::setDisplayIntegerBase(int base)
{
    d->m_displayIntegerBase = base;
}

void BaseIntegerAspect::setDisplayScaleFactor(qint64 factor)
{
    d->m_displayScaleFactor = factor;
}

} // namespace ProjectExplorer