environmenteditmodel.cpp 20.5 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
26
** contact the sales department at http://www.qtsoftware.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "environmenteditmodel.h"

32
33
34
#include <QtGui/QVBoxLayout>
#include <QtGui/QHeaderView>

con's avatar
con committed
35
36
37
38
39
using namespace ProjectExplorer;

EnvironmentModel::EnvironmentModel()
    : m_mergedEnvironments(false)
{}
hjk's avatar
hjk committed
40
41
42

EnvironmentModel::~EnvironmentModel()
{}
con's avatar
con committed
43
44
45

QString EnvironmentModel::indexToVariable(const QModelIndex &index) const
{
hjk's avatar
hjk committed
46
    if (m_mergedEnvironments)
con's avatar
con committed
47
48
49
50
51
52
53
54
55
56
        return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
    else
        return m_items.at(index.row()).name;
}

void EnvironmentModel::updateResultEnvironment()
{
    m_resultEnvironment = m_baseEnvironment;
    m_resultEnvironment.modify(m_items);
    foreach (const EnvironmentItem &item, m_items) {
hjk's avatar
hjk committed
57
        if (item.unset) {
58
            m_resultEnvironment.set(item.name, QLatin1String("<UNSET>"));
con's avatar
con committed
59
60
61
62
63
64
65
66
67
68
69
70
71
        }
    }
}

void EnvironmentModel::setBaseEnvironment(const ProjectExplorer::Environment &env)
{
    m_baseEnvironment = env;
    updateResultEnvironment();
    reset();
}

void EnvironmentModel::setMergedEnvironments(bool b)
{
hjk's avatar
hjk committed
72
    if (m_mergedEnvironments == b)
con's avatar
con committed
73
74
        return;
    m_mergedEnvironments = b;
hjk's avatar
hjk committed
75
    if (b)
con's avatar
con committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
        updateResultEnvironment();
    reset();
}

bool EnvironmentModel::mergedEnvironments()
{
    return m_mergedEnvironments;
}

int EnvironmentModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;

    return m_mergedEnvironments ? m_resultEnvironment.size() : m_items.count();
}
int EnvironmentModel::columnCount(const QModelIndex &parent) const
{
94
    Q_UNUSED(parent)
con's avatar
con committed
95
96
97
98
99
    return 2;
}

bool EnvironmentModel::changes(const QString &name) const
{
hjk's avatar
hjk committed
100
101
    foreach (const EnvironmentItem& item, m_items)
        if (item.name == name)
con's avatar
con committed
102
103
104
105
106
107
            return true;
    return false;
}

QVariant EnvironmentModel::data(const QModelIndex &index, int role) const
{
hjk's avatar
hjk committed
108
109
    if ((role == Qt::DisplayRole || role == Qt::EditRole) && index.isValid()) {
        if ((m_mergedEnvironments && index.row() >= m_resultEnvironment.size()) ||
con's avatar
con committed
110
111
112
113
           (!m_mergedEnvironments && index.row() >= m_items.count())) {
            return QVariant();
        }

hjk's avatar
hjk committed
114
115
        if (index.column() == 0) {
            if (m_mergedEnvironments) {
con's avatar
con committed
116
117
118
119
                return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
            } else {
                return m_items.at(index.row()).name;
            }
hjk's avatar
hjk committed
120
121
122
        } else if (index.column() == 1) {
            if (m_mergedEnvironments) {
                if (role == Qt::EditRole) {
con's avatar
con committed
123
                    int pos = findInChanges(indexToVariable(index));
hjk's avatar
hjk committed
124
                    if (pos != -1)
con's avatar
con committed
125
126
127
128
                        return m_items.at(pos).value;
                }
                return m_resultEnvironment.value(m_resultEnvironment.constBegin() + index.row());
            } else {
hjk's avatar
hjk committed
129
                if (m_items.at(index.row()).unset)
130
                    return QLatin1String("<UNSET>");
con's avatar
con committed
131
132
133
134
135
                else
                    return m_items.at(index.row()).value;
            }
        }
    }
hjk's avatar
hjk committed
136
137
    if (role == Qt::FontRole) {
        if (m_mergedEnvironments) {
con's avatar
con committed
138
            // check wheter this environment variable exists in m_items
hjk's avatar
hjk committed
139
            if (changes(m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row()))) {
con's avatar
con committed
140
141
142
143
144
145
146
147
148
149
150
151
152
                QFont f;
                f.setBold(true);
                return QVariant(f);
            }
        }
        return QFont();
    }
    return QVariant();
}


Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const
{
153
    Q_UNUSED(index)
con's avatar
con committed
154
155
156
157
158
    return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}

bool EnvironmentModel::hasChildren(const QModelIndex &index) const
{
hjk's avatar
hjk committed
159
    if (!index.isValid())
con's avatar
con committed
160
161
162
163
164
165
166
        return true;
    else
        return false;
}

QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const
{
hjk's avatar
hjk committed
167
    if (orientation == Qt::Vertical || role != Qt::DisplayRole)
con's avatar
con committed
168
169
170
171
172
173
        return QVariant();
    return section == 0 ? tr("Variable") : tr("Value");
}

QModelIndex EnvironmentModel::index(int row, int column, const QModelIndex &parent) const
{
hjk's avatar
hjk committed
174
    if (!parent.isValid())
con's avatar
con committed
175
176
177
178
179
180
        return createIndex(row, column, 0);
    return QModelIndex();
}

QModelIndex EnvironmentModel::parent(const QModelIndex &index) const
{
181
    Q_UNUSED(index)
con's avatar
con committed
182
183
184
185
186
187
188
189
    return QModelIndex();
}

/// *****************
/// Utility functions
/// *****************
int EnvironmentModel::findInChanges(const QString &name) const
{
hjk's avatar
hjk committed
190
191
    for (int i=0; i<m_items.size(); ++i)
        if (m_items.at(i).name == name)
con's avatar
con committed
192
193
194
195
196
197
            return i;
    return -1;
}

int EnvironmentModel::findInChangesInsertPosition(const QString &name) const
{
hjk's avatar
hjk committed
198
199
    for (int i=0; i<m_items.size(); ++i)
        if (m_items.at(i).name > name)
con's avatar
con committed
200
201
202
203
204
205
206
207
            return i;
    return m_items.size();
}

int EnvironmentModel::findInResult(const QString &name) const
{
    Environment::const_iterator it;
    int i = 0;
hjk's avatar
hjk committed
208
209
    for (it = m_resultEnvironment.constBegin(); it != m_resultEnvironment.constEnd(); ++it, ++i)
        if (m_resultEnvironment.key(it) == name)
con's avatar
con committed
210
211
212
213
214
215
216
217
            return i;
    return -1;
}

int EnvironmentModel::findInResultInsertPosition(const QString &name) const
{
    Environment::const_iterator it;
    int i = 0;
hjk's avatar
hjk committed
218
219
    for (it = m_resultEnvironment.constBegin(); it != m_resultEnvironment.constEnd(); ++it, ++i)
        if (m_resultEnvironment.key(it) > name)
con's avatar
con committed
220
221
222
223
224
225
            return i;
    return m_resultEnvironment.size();
}

bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
hjk's avatar
hjk committed
226
227
    if (role == Qt::EditRole && index.isValid()) {
        if (index.column() == 0) {
con's avatar
con committed
228
229
            //fail if a variable with the same name already exists
#ifdef Q_OS_WIN
hjk's avatar
hjk committed
230
            if (findInChanges(value.toString().toUpper()) != -1)
con's avatar
con committed
231
232
                return false;
#else
hjk's avatar
hjk committed
233
            if (findInChanges(value.toString()) != -1)
con's avatar
con committed
234
235
236
                return false;
#endif
            EnvironmentItem old("", "");
hjk's avatar
hjk committed
237
            if (m_mergedEnvironments) {
con's avatar
con committed
238
                int pos = findInChanges(indexToVariable(index));
hjk's avatar
hjk committed
239
                if (pos != -1) {
con's avatar
con committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
                    old = m_items.at(pos);
                } else {
                    old.name = m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
                    old.value = m_resultEnvironment.value(m_resultEnvironment.constBegin() + index.row());
                    old.unset = false;
                }
            } else {
                old = m_items.at(index.row());
            }
#ifdef Q_OS_WIN
            const QString &newName = value.toString().toUpper();
#else
            const QString &newName = value.toString();
#endif
hjk's avatar
hjk committed
254
            if (changes(old.name))
con's avatar
con committed
255
256
257
258
                removeVariable(old.name);
            old.name = newName;
            addVariable(old);
            return true;
hjk's avatar
hjk committed
259
260
        } else if (index.column() == 1) {
            if (m_mergedEnvironments) {
con's avatar
con committed
261
262
                const QString &name = indexToVariable(index);
                int pos = findInChanges(name);
hjk's avatar
hjk committed
263
                if (pos != -1) {
con's avatar
con committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
                    m_items[pos].value = value.toString();
                    m_items[pos].unset = false;
                    updateResultEnvironment();
                    emit dataChanged(index, index);
                    emit userChangesUpdated();
                    return true;
                }
                // not found in m_items, so add it as a new variable
                addVariable(EnvironmentItem(name, value.toString()));
                return true;
            } else {
                m_items[index.row()].value = value.toString();
                m_items[index.row()].unset = false;
                emit dataChanged(index, index);
                emit userChangesUpdated();
                return true;
            }
        }
    }
    return false;
}

QModelIndex EnvironmentModel::addVariable()
{
288
    const QString &name = QLatin1String("<VARIABLE>");
hjk's avatar
hjk committed
289
    if (m_mergedEnvironments) {
con's avatar
con committed
290
        int i = findInResult(name);
hjk's avatar
hjk committed
291
        if (i != -1)
con's avatar
con committed
292
293
294
            return index(i, 0, QModelIndex());
    } else {
        int i = findInChanges(name);
hjk's avatar
hjk committed
295
        if (i != -1)
con's avatar
con committed
296
297
298
            return index(i, 0, QModelIndex());
    }
    // Don't exist, really add them
299
    return addVariable(EnvironmentItem(name, QLatin1String("<VALUE>")));
con's avatar
con committed
300
301
302
303
}

QModelIndex EnvironmentModel::addVariable(const EnvironmentItem &item)
{
hjk's avatar
hjk committed
304
    if (m_mergedEnvironments) {
con's avatar
con committed
305
306
        bool existsInBaseEnvironment = (m_baseEnvironment.find(item.name) != m_baseEnvironment.constEnd());
        int rowInResult;
hjk's avatar
hjk committed
307
        if (existsInBaseEnvironment)
con's avatar
con committed
308
309
310
311
312
            rowInResult = findInResult(item.name);
        else
            rowInResult = findInResultInsertPosition(item.name);
        int rowInChanges = findInChangesInsertPosition(item.name);

313
        //qDebug() << "addVariable " << item.name << existsInBaseEnvironment << rowInResult << rowInChanges;
con's avatar
con committed
314

hjk's avatar
hjk committed
315
        if (existsInBaseEnvironment) {
con's avatar
con committed
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
            m_items.insert(rowInChanges, item);
            updateResultEnvironment();
            emit dataChanged(index(rowInResult, 0, QModelIndex()), index(rowInResult, 1, QModelIndex()));
            emit userChangesUpdated();
            return index(rowInResult, 0, QModelIndex());
        } else {
            beginInsertRows(QModelIndex(), rowInResult, rowInResult);
            m_items.insert(rowInChanges, item);
            updateResultEnvironment();
            endInsertRows();
            emit userChangesUpdated();
            return index(rowInResult, 0, QModelIndex());
        }
    } else {
        int newPos = findInChangesInsertPosition(item.name);
        beginInsertRows(QModelIndex(), newPos, newPos);
        m_items.insert(newPos, item);
        endInsertRows();
        emit userChangesUpdated();
        return index(newPos, 0, QModelIndex());
    }
}

void EnvironmentModel::removeVariable(const QString &name)
{
hjk's avatar
hjk committed
341
    if (m_mergedEnvironments) {
con's avatar
con committed
342
343
344
        int rowInResult = findInResult(name);
        int rowInChanges = findInChanges(name);
        bool existsInBaseEnvironment = m_baseEnvironment.find(name) != m_baseEnvironment.constEnd();
hjk's avatar
hjk committed
345
        if (existsInBaseEnvironment) {
con's avatar
con committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
            m_items.removeAt(rowInChanges);
            updateResultEnvironment();
            emit dataChanged(index(rowInResult, 0, QModelIndex()), index(rowInResult, 1, QModelIndex()));
            emit userChangesUpdated();
        } else {
            beginRemoveRows(QModelIndex(), rowInResult, rowInResult);
            m_items.removeAt(rowInChanges);
            updateResultEnvironment();
            endRemoveRows();
            emit userChangesUpdated();
        }
    } else {
        int removePos = findInChanges(name);
        beginRemoveRows(QModelIndex(), removePos, removePos);
        m_items.removeAt(removePos);
        updateResultEnvironment();
        endRemoveRows();
        emit userChangesUpdated();
    }
}

void EnvironmentModel::unset(const QString &name)
{
hjk's avatar
hjk committed
369
    if (m_mergedEnvironments) {
con's avatar
con committed
370
371
372
        int row = findInResult(name);
        // look in m_items for the variable
        int pos = findInChanges(name);
hjk's avatar
hjk committed
373
        if (pos != -1) {
con's avatar
con committed
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
            m_items[pos].unset = true;
            updateResultEnvironment();
            emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
            emit userChangesUpdated();
            return;
        }
        pos = findInChangesInsertPosition(name);
        m_items.insert(pos, EnvironmentItem(name, ""));
        m_items[pos].unset = true;
        updateResultEnvironment();
        emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
        emit userChangesUpdated();
        return;
    } else {
        int pos = findInChanges(name);
        m_items[pos].unset = true;
        emit dataChanged(index(pos, 1, QModelIndex()), index(pos, 1, QModelIndex()));
        emit userChangesUpdated();
        return;
    }
}

bool EnvironmentModel::isUnset(const QString &name)
{
    int pos = findInChanges(name);
hjk's avatar
hjk committed
399
    if (pos != -1)
con's avatar
con committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
        return m_items.at(pos).unset;
    else
        return false;
}

bool EnvironmentModel::isInBaseEnvironment(const QString &name)
{
    return m_baseEnvironment.find(name) != m_baseEnvironment.constEnd();
}

QList<EnvironmentItem> EnvironmentModel::userChanges() const
{
    return m_items;
}

void EnvironmentModel::setUserChanges(QList<EnvironmentItem> list)
{
    m_items = list;
    updateResultEnvironment();
    emit reset();
}
421
422
423
424
425

////
// EnvironmentWidget::EnvironmentWidget
////

426
EnvironmentWidget::EnvironmentWidget(QWidget *parent, QWidget *additionalDetailsWidget)
427
    : QStackedWidget(parent)
428
429
430
431
432
433
{
    m_model = new EnvironmentModel();
    m_model->setMergedEnvironments(true);
    connect(m_model, SIGNAL(userChangesUpdated()),
            this, SIGNAL(userChangesUpdated()));

434
435
436
    m_summaryPage = new QWidget();
    addWidget(m_summaryPage);
    QVBoxLayout *vbox = new QVBoxLayout(m_summaryPage);
con's avatar
con committed
437
    vbox->setContentsMargins(0, -1, 0, -1);
438
439
440
441

    QPushButton *detailsButton = new QPushButton(this);
    detailsButton->setText(tr("Show Details"));
    QHBoxLayout *hdetailsButtonLayout = new QHBoxLayout();
442
    hdetailsButtonLayout->setMargin(0);
443
    hdetailsButtonLayout->addWidget(detailsButton);
444
    hdetailsButtonLayout->addStretch(10);
445
446
    connect(detailsButton, SIGNAL(clicked()),
            this, SLOT(switchToDetails()));
447
448
449
450
451
    vbox->addLayout(hdetailsButtonLayout);

    m_summaryText = new QLabel(this);
    m_summaryText->setText("");
    vbox->addWidget(m_summaryText);
452
453
454
455

    m_detailsPage = new QWidget();
    //addWidget(m_detailsPage);
    QVBoxLayout *vbox2 = new QVBoxLayout(m_detailsPage);
456
457
458
459
460
461
462
463
464
465
466
467
468
469
    vbox2->setContentsMargins(0, -1, 0, -1);

    QPushButton *summaryButton = new QPushButton(this);
    summaryButton->setText(tr("Hide Details"));
    QHBoxLayout *hbox = new QHBoxLayout();
    hbox->setMargin(0);
    hbox->addWidget(summaryButton);
    hbox->addStretch(10);
    connect(summaryButton, SIGNAL(clicked()),
        this, SLOT(switchToSummary()));
    vbox2->addLayout(hbox);

    if (additionalDetailsWidget)
        vbox2->addWidget(additionalDetailsWidget);
470
471

    QHBoxLayout *horizontalLayout = new QHBoxLayout();
472
    horizontalLayout->setMargin(0);
473
474
475
476
477
    m_environmentTreeView = new QTreeView(this);
    m_environmentTreeView->setRootIsDecorated(false);
    m_environmentTreeView->setHeaderHidden(false);
    m_environmentTreeView->setModel(m_model);
    m_environmentTreeView->header()->resizeSection(0, 250);
478
    m_environmentTreeView->setMinimumHeight(400);
479
480
481
482
483
    horizontalLayout->addWidget(m_environmentTreeView);

    QVBoxLayout *verticalLayout_2 = new QVBoxLayout();

    m_editButton = new QPushButton(this);
484
    m_editButton->setText(tr("&Edit"));
485
486
487
    verticalLayout_2->addWidget(m_editButton);

    m_addButton = new QPushButton(this);
488
    m_addButton->setText(tr("&Add"));
489
490
491
492
    verticalLayout_2->addWidget(m_addButton);

    m_removeButton = new QPushButton(this);
    m_removeButton->setEnabled(false);
493
    m_removeButton->setText(tr("&Reset"));
494
495
496
497
    verticalLayout_2->addWidget(m_removeButton);

    m_unsetButton = new QPushButton(this);
    m_unsetButton->setEnabled(false);
498
    m_unsetButton->setText(tr("&Unset"));
499
500
501
502
503
    verticalLayout_2->addWidget(m_unsetButton);

    QSpacerItem *verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
    verticalLayout_2->addItem(verticalSpacer);
    horizontalLayout->addLayout(verticalLayout_2);
504
505
    vbox2->addLayout(horizontalLayout);
    
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    connect(m_model, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
            this, SLOT(updateButtons()));

    connect(m_editButton, SIGNAL(clicked(bool)),
            this, SLOT(editEnvironmentButtonClicked()));
    connect(m_addButton, SIGNAL(clicked(bool)),
            this, SLOT(addEnvironmentButtonClicked()));
    connect(m_removeButton, SIGNAL(clicked(bool)),
            this, SLOT(removeEnvironmentButtonClicked()));
    connect(m_unsetButton, SIGNAL(clicked(bool)),
            this, SLOT(unsetEnvironmentButtonClicked()));
    connect(m_environmentTreeView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)),
            this, SLOT(environmentCurrentIndexChanged(QModelIndex, QModelIndex)));
}

EnvironmentWidget::~EnvironmentWidget()
{
523
524
    delete m_summaryPage;
    delete m_detailsPage;
525
526
527
528
    delete m_model;
    m_model = 0;
}

529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
void EnvironmentWidget::switchToDetails()
{
    addWidget(m_detailsPage);
    setCurrentWidget(m_detailsPage);
    removeWidget(m_summaryPage);
}


void EnvironmentWidget::switchToSummary()
{
    updateSummaryText();
    addWidget(m_summaryPage);
    setCurrentWidget(m_summaryPage);
    removeWidget(m_detailsPage);
}

545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
void EnvironmentWidget::setBaseEnvironment(const ProjectExplorer::Environment &env)
{
    m_model->setBaseEnvironment(env);
}

void EnvironmentWidget::setMergedEnvironments(bool b)
{
    m_model->setMergedEnvironments(b);
}

bool EnvironmentWidget::mergedEnvironments()
{
    return m_model->mergedEnvironments();
}

QList<EnvironmentItem> EnvironmentWidget::userChanges() const
{
    return m_model->userChanges();
}

void EnvironmentWidget::setUserChanges(QList<EnvironmentItem> list)
{
    m_model->setUserChanges(list);
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
    updateSummaryText();
}

void EnvironmentWidget::updateSummaryText()
{
    QString text;
    const QList<EnvironmentItem> &list = m_model->userChanges();
    foreach (const EnvironmentItem &item, list) {
        if (!text.isEmpty())
            text.append("<br>");
        if (item.unset)
            text.append(tr("Unset <b>%1</b>").arg(item.name));
        else
            text.append(tr("Set <b>%1</b> to <b>%2</b>").arg(item.name, item.value));
    }
    if (text.isEmpty())
584
        text = tr("Summary: No changes to Environment");
585
    m_summaryText->setText(text);
586
587
588
589
590
591
592
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
}

void EnvironmentWidget::updateButtons()
{
    environmentCurrentIndexChanged(m_environmentTreeView->currentIndex(), QModelIndex());
}

void EnvironmentWidget::editEnvironmentButtonClicked()
{
    m_environmentTreeView->edit(m_environmentTreeView->currentIndex());
}

void EnvironmentWidget::addEnvironmentButtonClicked()
{
    QModelIndex index = m_model->addVariable();
    m_environmentTreeView->setCurrentIndex(index);
    m_environmentTreeView->edit(index);
    updateButtons();
}

void EnvironmentWidget::removeEnvironmentButtonClicked()
{
    const QString &name = m_model->indexToVariable(m_environmentTreeView->currentIndex());
    m_model->removeVariable(name);
    updateButtons();
}

// unset in Merged Environment Mode means, unset if it comes from the base environment
// or remove when it is just a change we added
// unset in changes view, means just unset
void EnvironmentWidget::unsetEnvironmentButtonClicked()
{
    const QString &name = m_model->indexToVariable(m_environmentTreeView->currentIndex());
    if (!m_model->isInBaseEnvironment(name) && m_model->mergedEnvironments())
        m_model->removeVariable(name);
    else
        m_model->unset(name);
    updateButtons();
}

void EnvironmentWidget::environmentCurrentIndexChanged(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous)
    if (current.isValid()) {
630
        m_editButton->setEnabled(true);
631
632
633
634
635
636
637
638
639
640
641
        if (m_model->mergedEnvironments()) {
            const QString &name = m_model->indexToVariable(current);
            bool modified = m_model->isInBaseEnvironment(name) && m_model->changes(name);
            bool unset = m_model->isUnset(name);
            m_removeButton->setEnabled(modified || unset);
            m_unsetButton->setEnabled(!unset);
        } else {
            m_removeButton->setEnabled(true);
            m_unsetButton->setEnabled(!m_model->isUnset(m_model->indexToVariable(current)));
        }
    } else {
642
        m_editButton->setEnabled(false);
643
644
645
646
        m_removeButton->setEnabled(false);
        m_unsetButton->setEnabled(false);
    }
}