summaryrefslogtreecommitdiff
path: root/src/plugins/projectexplorer/environmentitemswidget.cpp
blob: ed9e7a5a2f2bbe779f556983383052f409a3b6e4 (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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://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 http://www.qt.io/terms-conditions.  For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** 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.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "environmentitemswidget.h"

#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <texteditor/snippets/snippeteditor.h>
#include <texteditor/texteditorsettings.h>

#include <QVBoxLayout>
#include <QDialogButtonBox>

namespace ProjectExplorer {

class EnvironmentItemsWidgetPrivate
{
public:
    QList<Utils::EnvironmentItem> cleanUp(
            const QList<Utils::EnvironmentItem> &items) const;
    TextEditor::TextEditorWidget *m_editor;
};

QList<Utils::EnvironmentItem> EnvironmentItemsWidgetPrivate::cleanUp(
        const QList<Utils::EnvironmentItem> &items) const
{
    QList<Utils::EnvironmentItem> uniqueItems;
    QSet<QString> uniqueSet;
    for (int i = items.count() - 1; i >= 0; i--) {
        Utils::EnvironmentItem item = items.at(i);
        if (Utils::HostOsInfo::isWindowsHost())
            item.name = item.name.toUpper();
        const QString &itemName = item.name;
        QString emptyName = itemName;
        emptyName.remove(QLatin1Char(' '));
        if (!emptyName.isEmpty() && !uniqueSet.contains(itemName)) {
            uniqueItems.prepend(item);
            uniqueSet.insert(itemName);
        }
    }
    return uniqueItems;
}

EnvironmentItemsWidget::EnvironmentItemsWidget(QWidget *parent) :
    QWidget(parent), d(new EnvironmentItemsWidgetPrivate)
{
    d->m_editor = new TextEditor::SnippetEditorWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(d->m_editor);
}

EnvironmentItemsWidget::~EnvironmentItemsWidget()
{
    delete d;
}

void EnvironmentItemsWidget::setEnvironmentItems(const QList<Utils::EnvironmentItem> &items)
{
    QList<Utils::EnvironmentItem> sortedItems = items;
    Utils::EnvironmentItem::sort(&sortedItems);
    QStringList list = Utils::EnvironmentItem::toStringList(sortedItems);
    d->m_editor->document()->setPlainText(list.join(QLatin1Char('\n')));
}

QList<Utils::EnvironmentItem> EnvironmentItemsWidget::environmentItems() const
{
    const QStringList list = d->m_editor->document()->toPlainText().split(QLatin1String("\n"));
    QList<Utils::EnvironmentItem> items = Utils::EnvironmentItem::fromStringList(list);
    return d->cleanUp(items);
}



class EnvironmentItemsDialogPrivate
{
public:
    EnvironmentItemsWidget *m_editor;
};

EnvironmentItemsDialog::EnvironmentItemsDialog(QWidget *parent) :
    QDialog(parent), d(new EnvironmentItemsDialogPrivate)
{
    resize(640, 480);
    d->m_editor = new EnvironmentItemsWidget(this);
    QDialogButtonBox *box = new QDialogButtonBox(
            QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this);
    connect(box, SIGNAL(accepted()), this, SLOT(accept()));
    connect(box, SIGNAL(rejected()), this, SLOT(reject()));
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(d->m_editor);
    layout->addWidget(box);
    setWindowTitle(tr("Edit Environment"));
}

EnvironmentItemsDialog::~EnvironmentItemsDialog()
{
    delete d;
}

void EnvironmentItemsDialog::setEnvironmentItems(const QList<Utils::EnvironmentItem> &items)
{
    d->m_editor->setEnvironmentItems(items);
}

QList<Utils::EnvironmentItem> EnvironmentItemsDialog::environmentItems() const
{
    return d->m_editor->environmentItems();
}

QList<Utils::EnvironmentItem> EnvironmentItemsDialog::getEnvironmentItems(QWidget *parent,
                const QList<Utils::EnvironmentItem> &initial, bool *ok)
{
    EnvironmentItemsDialog dlg(parent);
    dlg.setEnvironmentItems(initial);
    bool result = dlg.exec() == QDialog::Accepted;
    if (ok)
        *ok = result;
    if (result)
        return dlg.environmentItems();
    return QList<Utils::EnvironmentItem>();
}

} // namespace ProjectExplorer