aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/namevaluesdialog.cpp
blob: 0b009c7568e5ff673a320a66bc059325b07ae21e (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
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "namevaluesdialog.h"

#include "algorithm.h"
#include "hostosinfo.h"
#include "utilstr.h"

#include <QDialogButtonBox>
#include <QLabel>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QSet>
#include <QTextBlock>
#include <QTimer>
#include <QVBoxLayout>

namespace Utils {
namespace Internal {

static EnvironmentItems cleanUp(const EnvironmentItems &items)
{
    EnvironmentItems cleanedItems;
    for (int i = items.count() - 1; i >= 0; i--) {
        EnvironmentItem item = items.at(i);
        if (HostOsInfo::isWindowsHost())
            item.name = item.name.toUpper();
        const QString &itemName = item.name;
        QString emptyName = itemName;
        emptyName.remove(QLatin1Char(' '));
        if (!emptyName.isEmpty())
            cleanedItems.prepend(item);
    }
    return cleanedItems;
}

class TextEditHelper : public QPlainTextEdit
{
    Q_OBJECT
public:
    using QPlainTextEdit::QPlainTextEdit;

signals:
    void lostFocus();

private:
    void focusOutEvent(QFocusEvent *e) override
    {
        QPlainTextEdit::focusOutEvent(e);
        emit lostFocus();
    }
};

} // namespace Internal

NameValueItemsWidget::NameValueItemsWidget(QWidget *parent)
    : QWidget(parent)
{
    const QString helpText = Tr::tr(
        "Enter one environment variable per line.\n"
        "To set or change a variable, use VARIABLE=VALUE.\n"
        "To append to a variable, use VARIABLE+=VALUE.\n"
        "To prepend to a variable, use VARIABLE=+VALUE.\n"
        "Existing variables can be referenced in a VALUE with ${OTHER}.\n"
        "To clear a variable, put its name on a line with nothing else on it.\n"
        "To disable a variable, prefix the line with \"#\".");

    m_editor = new Internal::TextEditHelper(this);
    auto layout = new QVBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->addWidget(m_editor);
    layout->addWidget(new QLabel(helpText, this));

    const auto checkForItemChange = [this] {
        const EnvironmentItems newItems = environmentItems();
        if (newItems != m_originalItems) {
            m_originalItems = newItems;
            emit userChangedItems(newItems);
        }
    };
    const auto timer = new QTimer(this);
    timer->setSingleShot(true);
    timer->setInterval(1000);
    connect(m_editor, &QPlainTextEdit::textChanged, timer, qOverload<>(&QTimer::start));
    connect(timer, &QTimer::timeout, this, checkForItemChange);
    connect(m_editor, &Internal::TextEditHelper::lostFocus, this, [timer, checkForItemChange] {
        timer->stop();
        checkForItemChange();
    });
}

void NameValueItemsWidget::setEnvironmentItems(const EnvironmentItems &items)
{
    m_originalItems = items;
    m_editor->document()->setPlainText(EnvironmentItem::toStringList(items)
                                           .join(QLatin1Char('\n')));
}

EnvironmentItems NameValueItemsWidget::environmentItems() const
{
    const QStringList list = m_editor->document()->toPlainText().split(QLatin1String("\n"));
    return Internal::cleanUp(EnvironmentItem::fromStringList(list));
}

void NameValueItemsWidget::setPlaceholderText(const QString &text)
{
    m_editor->setPlaceholderText(text);
}

bool NameValueItemsWidget::editVariable(const QString &name, Selection selection)
{
    QTextDocument * const doc = m_editor->document();
    for (QTextBlock b = doc->lastBlock(); b.isValid(); b = b.previous()) {
        const QString &line = b.text();
        qsizetype offset = 0;
        const auto skipWhiteSpace = [&] {
            for (; offset < line.length(); ++offset) {
                if (!line.at(offset).isSpace())
                    return;
            }
        };
        skipWhiteSpace();
        if (line.mid(offset, name.size()) != name)
            continue;
        offset += name.size();

        const auto updateCursor = [&](int anchor, int pos) {
            QTextCursor newCursor(doc);
            newCursor.setPosition(anchor);
            newCursor.setPosition(pos, QTextCursor::KeepAnchor);
            m_editor->setTextCursor(newCursor);
        };

        if (selection == Selection::Name) {
            m_editor->setFocus();
            updateCursor(b.position() + offset, b.position());
            return true;
        }

        skipWhiteSpace();
        if (offset < line.length()) {
            QChar nextChar = line.at(offset);
            if (nextChar.isLetterOrNumber())
                continue;
            if (nextChar == '=') {
                if (++offset < line.length() && line.at(offset) == '+')
                    ++offset;
            } else if (nextChar == '+') {
                if (++offset < line.length() && line.at(offset) == '=')
                    ++offset;
            }
        }
        m_editor->setFocus();
        updateCursor(b.position() + b.length() - 1, b.position() + offset);
        return true;
    }
    return false;
}

NameValuesDialog::NameValuesDialog(const QString &windowTitle, QWidget *parent)
    : QDialog(parent)
{
    resize(640, 480);
    m_editor = new NameValueItemsWidget(this);
    auto box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
                                    Qt::Horizontal,
                                    this);
    box->button(QDialogButtonBox::Ok)->setText(Tr::tr("&OK"));
    box->button(QDialogButtonBox::Cancel)->setText(Tr::tr("&Cancel"));
    connect(box, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(box, &QDialogButtonBox::rejected, this, &QDialog::reject);

    auto layout = new QVBoxLayout(this);
    layout->addWidget(m_editor);
    layout->addWidget(box);

    setWindowTitle(windowTitle);
}

void NameValuesDialog::setNameValueItems(const EnvironmentItems &items)
{
    m_editor->setEnvironmentItems(items);
}

EnvironmentItems NameValuesDialog::nameValueItems() const
{
    return m_editor->environmentItems();
}

void NameValuesDialog::setPlaceholderText(const QString &text)
{
    m_editor->setPlaceholderText(text);
}

std::optional<EnvironmentItems> NameValuesDialog::getNameValueItems(QWidget *parent,
                                                                    const EnvironmentItems &initial,
                                                                    const QString &placeholderText,
                                                                    Polisher polisher,
                                                                    const QString &windowTitle)
{
    NameValuesDialog dialog(windowTitle, parent);
    if (polisher)
        polisher(&dialog);
    dialog.setNameValueItems(initial);
    dialog.setPlaceholderText(placeholderText);
    bool result = dialog.exec() == QDialog::Accepted;
    if (result)
        return dialog.nameValueItems();

    return {};
}

} // namespace Utils

#include <namevaluesdialog.moc>