environmenteditmodel.cpp 16.7 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 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
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.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
#include <utils/detailswidget.h>
dt's avatar
dt committed
33

34
35
#include <QtGui/QVBoxLayout>
#include <QtGui/QHeaderView>
36
#include <QtGui/QToolButton>
37
38
39
40
41
42
43
#include <QtCore/QDebug>
#include <QtGui/QWidget>
#include <QtGui/QCheckBox>
#include <QtGui/QTreeView>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QStackedWidget>
44

con's avatar
con committed
45
46
47
48
using namespace ProjectExplorer;

EnvironmentModel::EnvironmentModel()
{}
hjk's avatar
hjk committed
49
50
51

EnvironmentModel::~EnvironmentModel()
{}
con's avatar
con committed
52
53
54

QString EnvironmentModel::indexToVariable(const QModelIndex &index) const
{
55
    return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
con's avatar
con committed
56
57
58
59
60
61
}

void EnvironmentModel::updateResultEnvironment()
{
    m_resultEnvironment = m_baseEnvironment;
    m_resultEnvironment.modify(m_items);
Tobias Hunger's avatar
Tobias Hunger committed
62
63
    // Add removed variables again and mark them as "<UNSET>" so
    // that the user can actually see those removals:
con's avatar
con committed
64
    foreach (const EnvironmentItem &item, m_items) {
hjk's avatar
hjk committed
65
        if (item.unset) {
66
            m_resultEnvironment.set(item.name, tr("<UNSET>"));
con's avatar
con committed
67
68
69
70
71
72
        }
    }
}

void EnvironmentModel::setBaseEnvironment(const ProjectExplorer::Environment &env)
{
Tobias Hunger's avatar
Tobias Hunger committed
73
    beginResetModel();
con's avatar
con committed
74
75
    m_baseEnvironment = env;
    updateResultEnvironment();
Tobias Hunger's avatar
Tobias Hunger committed
76
    endResetModel();
con's avatar
con committed
77
78
79
80
81
82
83
}

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

84
    return m_resultEnvironment.size();
con's avatar
con committed
85
86
87
}
int EnvironmentModel::columnCount(const QModelIndex &parent) const
{
Tobias Hunger's avatar
Tobias Hunger committed
88
89
90
    if (parent.isValid())
        return 0;

con's avatar
con committed
91
92
93
94
95
    return 2;
}

bool EnvironmentModel::changes(const QString &name) const
{
Tobias Hunger's avatar
Tobias Hunger committed
96
    return findInChanges(name) >= 0;
con's avatar
con committed
97
98
99
100
}

QVariant EnvironmentModel::data(const QModelIndex &index, int role) const
{
Tobias Hunger's avatar
Tobias Hunger committed
101
102
    if (!index.isValid())
        return QVariant();
con's avatar
con committed
103

Tobias Hunger's avatar
Tobias Hunger committed
104
    if ((role == Qt::DisplayRole || role == Qt::EditRole)) {
hjk's avatar
hjk committed
105
        if (index.column() == 0) {
106
            return m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
hjk's avatar
hjk committed
107
        } else if (index.column() == 1) {
108
109
110
111
            if (role == Qt::EditRole) {
                int pos = findInChanges(indexToVariable(index));
                if (pos != -1)
                    return m_items.at(pos).value;
con's avatar
con committed
112
            }
113
            return m_resultEnvironment.value(m_resultEnvironment.constBegin() + index.row());
con's avatar
con committed
114
115
        }
    }
hjk's avatar
hjk committed
116
    if (role == Qt::FontRole) {
Tobias Hunger's avatar
Tobias Hunger committed
117
        // check whether this environment variable exists in m_items
118
119
120
121
        if (changes(m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row()))) {
            QFont f;
            f.setBold(true);
            return QVariant(f);
con's avatar
con committed
122
123
124
125
126
127
128
129
130
        }
        return QFont();
    }
    return QVariant();
}


Qt::ItemFlags EnvironmentModel::flags(const QModelIndex &index) const
{
131
    Q_UNUSED(index)
con's avatar
con committed
132
133
134
135
136
    return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled;
}

QVariant EnvironmentModel::headerData(int section, Qt::Orientation orientation, int role) const
{
hjk's avatar
hjk committed
137
    if (orientation == Qt::Vertical || role != Qt::DisplayRole)
con's avatar
con committed
138
139
140
141
142
143
144
145
146
        return QVariant();
    return section == 0 ? tr("Variable") : tr("Value");
}

/// *****************
/// Utility functions
/// *****************
int EnvironmentModel::findInChanges(const QString &name) const
{
hjk's avatar
hjk committed
147
148
    for (int i=0; i<m_items.size(); ++i)
        if (m_items.at(i).name == name)
con's avatar
con committed
149
150
151
152
153
154
            return i;
    return -1;
}

int EnvironmentModel::findInChangesInsertPosition(const QString &name) const
{
hjk's avatar
hjk committed
155
156
    for (int i=0; i<m_items.size(); ++i)
        if (m_items.at(i).name > name)
con's avatar
con committed
157
158
159
160
            return i;
    return m_items.size();
}

Tobias Hunger's avatar
Tobias Hunger committed
161
QModelIndex EnvironmentModel::variableToIndex(const QString &name) const
162
163
164
165
166
167
168
{
    int row = findInResult(name);
    if (row == -1)
        return QModelIndex();
    return index(row, 0);
}

con's avatar
con committed
169
170
171
172
int EnvironmentModel::findInResult(const QString &name) const
{
    Environment::const_iterator it;
    int i = 0;
hjk's avatar
hjk committed
173
174
    for (it = m_resultEnvironment.constBegin(); it != m_resultEnvironment.constEnd(); ++it, ++i)
        if (m_resultEnvironment.key(it) == name)
con's avatar
con committed
175
176
177
178
179
180
181
182
            return i;
    return -1;
}

int EnvironmentModel::findInResultInsertPosition(const QString &name) const
{
    Environment::const_iterator it;
    int i = 0;
hjk's avatar
hjk committed
183
184
    for (it = m_resultEnvironment.constBegin(); it != m_resultEnvironment.constEnd(); ++it, ++i)
        if (m_resultEnvironment.key(it) > name)
con's avatar
con committed
185
186
187
188
189
190
            return i;
    return m_resultEnvironment.size();
}

bool EnvironmentModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Tobias Hunger's avatar
Tobias Hunger committed
191
192
193
    if (!index.isValid())
        return false;

hjk's avatar
hjk committed
194
    if (role == Qt::EditRole && index.isValid()) {
195
196
197
198
        // ignore changes to already set values:
        if (data(index, role) == value)
            return true;

hjk's avatar
hjk committed
199
        if (index.column() == 0) {
con's avatar
con committed
200
201
            //fail if a variable with the same name already exists
#ifdef Q_OS_WIN
202
            const QString &newName = value.toString().toUpper();
con's avatar
con committed
203
#else
204
            const QString &newName = value.toString();
con's avatar
con committed
205
#endif
206
207
208
209

            if (findInChanges(newName) != -1)
                return false;

con's avatar
con committed
210
            EnvironmentItem old("", "");
211
212
213
            int pos = findInChanges(indexToVariable(index));
            if (pos != -1) {
                old = m_items.at(pos);
con's avatar
con committed
214
            } else {
215
216
217
                old.name = m_resultEnvironment.key(m_resultEnvironment.constBegin() + index.row());
                old.value = m_resultEnvironment.value(m_resultEnvironment.constBegin() + index.row());
                old.unset = false;
con's avatar
con committed
218
            }
219

hjk's avatar
hjk committed
220
            if (changes(old.name))
221
                resetVariable(old.name);
con's avatar
con committed
222
223
            old.name = newName;
            addVariable(old);
224
            emit renamedVariable(newName);
con's avatar
con committed
225
            return true;
hjk's avatar
hjk committed
226
        } else if (index.column() == 1) {
227
228
229
230
231
232
            const QString &name = indexToVariable(index);
            int pos = findInChanges(name);
            if (pos != -1) {
                m_items[pos].value = value.toString();
                m_items[pos].unset = false;
                updateResultEnvironment();
con's avatar
con committed
233
                emit dataChanged(index, index);
234
                emit userChangesChanged();
con's avatar
con committed
235
236
                return true;
            }
237
238
239
            // not found in m_items, so add it as a new variable
            addVariable(EnvironmentItem(name, value.toString()));
            return true;
con's avatar
con committed
240
241
242
243
244
245
246
        }
    }
    return false;
}

QModelIndex EnvironmentModel::addVariable()
{
247
    const QString name = tr("<VARIABLE>");
248
249
250
    int i = findInResult(name);
    if (i != -1)
        return index(i, 0, QModelIndex());
con's avatar
con committed
251
    // Don't exist, really add them
252
    return addVariable(EnvironmentItem(name, tr("<VALUE>")));
con's avatar
con committed
253
254
255
256
}

QModelIndex EnvironmentModel::addVariable(const EnvironmentItem &item)
{
257
    bool existsInBaseEnvironment = m_baseEnvironment.hasKey(item.name);
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
    int rowInResult;
    if (existsInBaseEnvironment)
        rowInResult = findInResult(item.name);
    else
        rowInResult = findInResultInsertPosition(item.name);
    int rowInChanges = findInChangesInsertPosition(item.name);

    //qDebug() << "addVariable " << item.name << existsInBaseEnvironment << rowInResult << rowInChanges;

    if (existsInBaseEnvironment) {
        m_items.insert(rowInChanges, item);
        updateResultEnvironment();
        emit dataChanged(index(rowInResult, 0, QModelIndex()), index(rowInResult, 1, QModelIndex()));
        emit userChangesChanged();
        return index(rowInResult, 0, QModelIndex());
con's avatar
con committed
273
    } else {
274
275
276
        beginInsertRows(QModelIndex(), rowInResult, rowInResult);
        m_items.insert(rowInChanges, item);
        updateResultEnvironment();
con's avatar
con committed
277
        endInsertRows();
278
        emit userChangesChanged();
279
        return index(rowInResult, 0, QModelIndex());
con's avatar
con committed
280
281
282
    }
}

283
void EnvironmentModel::resetVariable(const QString &name)
con's avatar
con committed
284
{
285
286
    int rowInResult = findInResult(name);
    int rowInChanges = findInChanges(name);
287
    bool existsInBaseEnvironment = m_baseEnvironment.hasKey(name);
288
289
290
291
292
    if (existsInBaseEnvironment) {
        m_items.removeAt(rowInChanges);
        updateResultEnvironment();
        emit dataChanged(index(rowInResult, 0, QModelIndex()), index(rowInResult, 1, QModelIndex()));
        emit userChangesChanged();
con's avatar
con committed
293
    } else {
294
295
        beginRemoveRows(QModelIndex(), rowInResult, rowInResult);
        m_items.removeAt(rowInChanges);
con's avatar
con committed
296
297
        updateResultEnvironment();
        endRemoveRows();
298
        emit userChangesChanged();
con's avatar
con committed
299
300
301
    }
}

302
void EnvironmentModel::unsetVariable(const QString &name)
con's avatar
con committed
303
{
304
305
306
307
    int row = findInResult(name);
    // look in m_items for the variable
    int pos = findInChanges(name);
    if (pos != -1) {
con's avatar
con committed
308
309
310
        m_items[pos].unset = true;
        updateResultEnvironment();
        emit dataChanged(index(row, 0, QModelIndex()), index(row, 1, QModelIndex()));
311
        emit userChangesChanged();
con's avatar
con committed
312
313
        return;
    }
314
315
316
317
318
319
    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 userChangesChanged();
con's avatar
con committed
320
321
}

322
bool EnvironmentModel::canUnset(const QString &name)
con's avatar
con committed
323
324
{
    int pos = findInChanges(name);
hjk's avatar
hjk committed
325
    if (pos != -1)
con's avatar
con committed
326
327
328
329
330
        return m_items.at(pos).unset;
    else
        return false;
}

331
bool EnvironmentModel::canReset(const QString &name)
con's avatar
con committed
332
{
333
    return m_baseEnvironment.hasKey(name);
con's avatar
con committed
334
335
336
337
338
339
340
341
342
}

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

void EnvironmentModel::setUserChanges(QList<EnvironmentItem> list)
{
Tobias Hunger's avatar
Tobias Hunger committed
343
    beginResetModel();
con's avatar
con committed
344
345
    m_items = list;
    updateResultEnvironment();
Tobias Hunger's avatar
Tobias Hunger committed
346
    endResetModel();
con's avatar
con committed
347
}
348
349
350
351
352

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

353
EnvironmentWidget::EnvironmentWidget(QWidget *parent, QWidget *additionalDetailsWidget)
354
    : QWidget(parent)
355
356
{
    m_model = new EnvironmentModel();
357
358
    connect(m_model, SIGNAL(userChangesChanged()),
            this, SIGNAL(userChangesChanged()));
359

360
361
362
    connect(m_model, SIGNAL(renamedVariable(QString)),
            this, SLOT(renamedVariable(QString)));

363
    QVBoxLayout *vbox = new QVBoxLayout(this);
dt's avatar
dt committed
364
    vbox->setContentsMargins(0, 0, 0, 0);
365

dt's avatar
dt committed
366
    m_detailsContainer = new Utils::DetailsWidget(this);
367

dt's avatar
dt committed
368
369
370
    QWidget *details = new QWidget(m_detailsContainer);
    m_detailsContainer->setWidget(details);
    details->setVisible(false);
371

dt's avatar
dt committed
372
    QVBoxLayout *vbox2 = new QVBoxLayout(details);
373
    vbox2->setMargin(0);
374
375
376

    if (additionalDetailsWidget)
        vbox2->addWidget(additionalDetailsWidget);
377
378

    QHBoxLayout *horizontalLayout = new QHBoxLayout();
379
    horizontalLayout->setMargin(0);
380
381
382
383
384
    m_environmentTreeView = new QTreeView(this);
    m_environmentTreeView->setRootIsDecorated(false);
    m_environmentTreeView->setHeaderHidden(false);
    m_environmentTreeView->setModel(m_model);
    m_environmentTreeView->header()->resizeSection(0, 250);
385
    m_environmentTreeView->setMinimumHeight(400);
386
387
    horizontalLayout->addWidget(m_environmentTreeView);

388
    QVBoxLayout *buttonLayout = new QVBoxLayout();
389
390

    m_editButton = new QPushButton(this);
391
    m_editButton->setText(tr("&Edit"));
392
    buttonLayout->addWidget(m_editButton);
393
394

    m_addButton = new QPushButton(this);
395
    m_addButton->setText(tr("&Add"));
396
    buttonLayout->addWidget(m_addButton);
397

398
399
400
401
    m_resetButton = new QPushButton(this);
    m_resetButton->setEnabled(false);
    m_resetButton->setText(tr("&Reset"));
    buttonLayout->addWidget(m_resetButton);
402
403
404

    m_unsetButton = new QPushButton(this);
    m_unsetButton->setEnabled(false);
405
    m_unsetButton->setText(tr("&Unset"));
406
    buttonLayout->addWidget(m_unsetButton);
407
408

    QSpacerItem *verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
409
410
    buttonLayout->addItem(verticalSpacer);
    horizontalLayout->addLayout(buttonLayout);
411
    vbox2->addLayout(horizontalLayout);
412

dt's avatar
dt committed
413
    vbox->addWidget(m_detailsContainer);
414

415
416
417
418
419
420
421
    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()));
422
    connect(m_resetButton, SIGNAL(clicked(bool)),
423
424
425
426
427
            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)));
428

429
    connect(m_model, SIGNAL(userChangesChanged()), this, SLOT(updateSummaryText()));
430
431
432
433
434
435
436
437
}

EnvironmentWidget::~EnvironmentWidget()
{
    delete m_model;
    m_model = 0;
}

Tobias Hunger's avatar
Tobias Hunger committed
438
void EnvironmentWidget::renamedVariable(const QString &name)
439
{
Tobias Hunger's avatar
Tobias Hunger committed
440
    QModelIndex idx = m_model->variableToIndex(name);
441
442
443
444
    m_environmentTreeView->setCurrentIndex(idx);
    m_environmentTreeView->setFocus();
}

445
446
447
448
449
void EnvironmentWidget::setBaseEnvironment(const ProjectExplorer::Environment &env)
{
    m_model->setBaseEnvironment(env);
}

450
451
452
453
454
455
void EnvironmentWidget::setBaseEnvironmentText(const QString &text)
{
    m_baseEnvironmentText = text;
    updateSummaryText();
}

456
457
458
459
460
461
462
463
QList<EnvironmentItem> EnvironmentWidget::userChanges() const
{
    return m_model->userChanges();
}

void EnvironmentWidget::setUserChanges(QList<EnvironmentItem> list)
{
    m_model->setUserChanges(list);
464
465
466
467
468
469
470
471
    updateSummaryText();
}

void EnvironmentWidget::updateSummaryText()
{
    QString text;
    const QList<EnvironmentItem> &list = m_model->userChanges();
    foreach (const EnvironmentItem &item, list) {
472
        if (item.name != EnvironmentModel::tr("<VARIABLE>")) {
473
            text.append("<br>");
474
475
476
477
478
            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));
        }
479
    }
480

481
    if (text.isEmpty())
482
483
484
485
        text.prepend(tr("Using <b>%1</b>").arg(m_baseEnvironmentText));
    else
        text.prepend(tr("Using <b>%1</b> and").arg(m_baseEnvironmentText));

dt's avatar
dt committed
486
    m_detailsContainer->setSummaryText(text);
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
}

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());
510
    m_model->resetVariable(name);
511
512
513
514
515
516
517
518
    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
void EnvironmentWidget::unsetEnvironmentButtonClicked()
{
    const QString &name = m_model->indexToVariable(m_environmentTreeView->currentIndex());
519
520
    if (!m_model->canReset(name))
        m_model->resetVariable(name);
521
    else
522
        m_model->unsetVariable(name);
523
524
525
526
527
528
529
    updateButtons();
}

void EnvironmentWidget::environmentCurrentIndexChanged(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous)
    if (current.isValid()) {
530
        m_editButton->setEnabled(true);
531
        const QString &name = m_model->indexToVariable(current);
532
533
534
        bool modified = m_model->canReset(name) && m_model->changes(name);
        bool unset = m_model->canUnset(name);
        m_resetButton->setEnabled(modified || unset);
535
        m_unsetButton->setEnabled(!unset);
536
    } else {
537
        m_editButton->setEnabled(false);
538
        m_resetButton->setEnabled(false);
539
540
541
        m_unsetButton->setEnabled(false);
    }
}