qtoptionspage.cpp 18.6 KB
Newer Older
1
2
3
#include "qtoptionspage.h"
#include "ui_showbuildlog.h"
#include "ui_qtversionmanager.h"
4
#include "qt4projectmanagerconstants.h"
5
6
#include "qtversionmanager.h"
#include <coreplugin/coreconstants.h>
7
#include <utils/treewidgetcolumnstretcher.h>
8

9
#include <QtCore/QDebug>
10
11
#include <QtCore/QDir>

12
13
using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
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
///
// QtOptionsPage
///

QtOptionsPage::QtOptionsPage()
{

}

QtOptionsPage::~QtOptionsPage()
{

}

QString QtOptionsPage::id() const
{
    return QLatin1String(Constants::QTVERSION_PAGE);
}

QString QtOptionsPage::trName() const
{
    return tr(Constants::QTVERSION_PAGE);
}

QString QtOptionsPage::category() const
{
    return Constants::QT_CATEGORY;
}

QString QtOptionsPage::trCategory() const
{
    return tr(Constants::QT_CATEGORY);
}

QWidget *QtOptionsPage::createPage(QWidget *parent)
{
    QtVersionManager *vm = QtVersionManager::instance();
51
    m_widget = new QtOptionsPageWidget(parent, vm->versions(), vm->currentQtVersion());
52
53
54
55
56
57
58
59
60
61
62
63
    return m_widget;
}

void QtOptionsPage::apply()
{
    m_widget->finish();

    QtVersionManager *vm = QtVersionManager::instance();
    vm->setNewQtVersions(m_widget->versions(), m_widget->defaultVersion());
}

//-----------------------------------------------------
64
65


66
QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent, QList<QtVersion *> versions, QtVersion *defaultVersion)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    : QWidget(parent)
    , m_defaultVersion(versions.indexOf(defaultVersion))
    , m_specifyNameString(tr("<specify a name>"))
    , m_specifyPathString(tr("<specify a path>"))
{
    // Initialize m_versions
    foreach(QtVersion *version, versions) {
        m_versions.append(new QtVersion(*version));
    }


    m_ui = new Internal::Ui::QtVersionManager();
    m_ui->setupUi(this);
    m_ui->qtPath->setExpectedKind(Core::Utils::PathChooser::Directory);
    m_ui->qtPath->setPromptDialogTitle(tr("Select QTDIR"));
    m_ui->mingwPath->setExpectedKind(Core::Utils::PathChooser::Directory);
    m_ui->qtPath->setPromptDialogTitle(tr("Select the Qt Directory"));
con's avatar
con committed
84
85
    m_ui->mwcPath->setExpectedKind(Core::Utils::PathChooser::Directory);
    m_ui->mwcPath->setPromptDialogTitle(tr("Select \"x86build\" Directory from Carbide Install"));
86
87
88
89

    m_ui->addButton->setIcon(QIcon(Core::Constants::ICON_PLUS));
    m_ui->delButton->setIcon(QIcon(Core::Constants::ICON_MINUS));

90
91
    new Core::Utils::TreeWidgetColumnStretcher(m_ui->qtdirList, 1);

92
93
94
95
96
97
98
99
100
    // setup parent items for auto-detected and manual versions
    m_ui->qtdirList->header()->setResizeMode(QHeaderView::ResizeToContents);
    QTreeWidgetItem *autoItem = new QTreeWidgetItem(m_ui->qtdirList);
    autoItem->setText(0, tr("Auto-detected"));
    autoItem->setFirstColumnSpanned(true);
    QTreeWidgetItem *manualItem = new QTreeWidgetItem(m_ui->qtdirList);
    manualItem->setText(0, tr("Manual"));
    manualItem->setFirstColumnSpanned(true);

101
102
    for (int i = 0; i < m_versions.count(); ++i) {
        const QtVersion * const version = m_versions.at(i);
103
        QTreeWidgetItem *item = new QTreeWidgetItem(version->isAutodetected()? autoItem : manualItem);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        item->setText(0, version->name());
        item->setText(1, QDir::toNativeSeparators(version->path()));
        item->setData(0, Qt::UserRole, version->uniqueId());

        if (version->isValid()) {
            if (version->hasDebuggingHelper())
                item->setData(2, Qt::DecorationRole, QIcon(":/extensionsystem/images/ok.png"));
            else
                item->setData(2, Qt::DecorationRole, QIcon(":/extensionsystem/images/error.png"));
        } else {
            item->setData(2, Qt::DecorationRole, QIcon());
        }

        m_ui->defaultCombo->addItem(version->name());
        if (i == m_defaultVersion)
            m_ui->defaultCombo->setCurrentIndex(i);
    }
121
    m_ui->qtdirList->expandAll();
122
123
124
125
126
127
128
129
130

    connect(m_ui->nameEdit, SIGNAL(textEdited(const QString &)),
            this, SLOT(updateCurrentQtName()));


    connect(m_ui->qtPath, SIGNAL(changed()),
            this, SLOT(updateCurrentQtPath()));
    connect(m_ui->mingwPath, SIGNAL(changed()),
            this, SLOT(updateCurrentMingwDirectory()));
con's avatar
con committed
131
132
133
134
#ifdef QTCREATOR_WITH_S60
    connect(m_ui->mwcPath, SIGNAL(changed()),
            this, SLOT(updateCurrentMwcDirectory()));
#endif
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

    connect(m_ui->addButton, SIGNAL(clicked()),
            this, SLOT(addQtDir()));
    connect(m_ui->delButton, SIGNAL(clicked()),
            this, SLOT(removeQtDir()));

    connect(m_ui->qtPath, SIGNAL(browsingFinished()),
            this, SLOT(onQtBrowsed()));
    connect(m_ui->mingwPath, SIGNAL(browsingFinished()),
            this, SLOT(onMingwBrowsed()));

    connect(m_ui->qtdirList, SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
            this, SLOT(versionChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
    connect(m_ui->defaultCombo, SIGNAL(currentIndexChanged(int)),
            this, SLOT(defaultChanged(int)));

    connect(m_ui->msvcComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(msvcVersionChanged()));

    connect(m_ui->rebuildButton, SIGNAL(clicked()),
            this, SLOT(buildDebuggingHelper()));
    connect(m_ui->showLogButton, SIGNAL(clicked()),
            this, SLOT(showDebuggingBuildLog()));

    showEnvironmentPage(0);
    updateState();
}

163
void QtOptionsPageWidget::buildDebuggingHelper()
164
165
166
{
    // Find the qt version for this button..
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
167
168
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
        return;

    QtVersion *version = m_versions[currentItemIndex];

    QString result = m_versions.at(currentItemIndex)->buildDebuggingHelperLibrary();
    currentItem->setData(2, Qt::UserRole, result);

    if (version->hasDebuggingHelper()) {
        m_ui->debuggingHelperStateLabel->setPixmap(QPixmap(":/extensionsystem/images/ok.png"));
        currentItem->setData(2, Qt::DecorationRole, QIcon(":/extensionsystem/images/ok.png"));
    } else {
        m_ui->debuggingHelperStateLabel->setPixmap(QPixmap(":/extensionsystem/images/error.png"));
        currentItem->setData(2, Qt::DecorationRole, QIcon(":/extensionsystem/images/error.png"));
    }
    m_ui->showLogButton->setEnabled(true);
}

186
void QtOptionsPageWidget::showDebuggingBuildLog()
187
188
189
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();

190
191
192
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
193
194
195
    QDialog dlg;
    Ui_ShowBuildLog ui;
    ui.setupUi(&dlg);
196
    ui.log->setPlainText(currentItem->data(2, Qt::UserRole).toString());
197
198
199
    dlg.exec();
}

200
QtOptionsPageWidget::~QtOptionsPageWidget()
201
202
203
204
205
{
    qDeleteAll(m_versions);
    delete m_ui;
}

206
void QtOptionsPageWidget::addQtDir()
207
208
209
210
{
    QtVersion *newVersion = new QtVersion(m_specifyNameString, m_specifyPathString);
    m_versions.append(newVersion);

211
    QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList->topLevelItem(1));
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    item->setText(0, newVersion->name());
    item->setText(1, QDir::toNativeSeparators(newVersion->path()));
    item->setData(0, Qt::UserRole, newVersion->uniqueId());
    item->setData(2, Qt::DecorationRole, QIcon());

    m_ui->qtdirList->setCurrentItem(item);

    m_ui->nameEdit->setText(newVersion->name());
    m_ui->qtPath->setPath(newVersion->path());
    m_ui->defaultCombo->addItem(newVersion->name());
    m_ui->nameEdit->setFocus();
    m_ui->nameEdit->selectAll();
}

226
void QtOptionsPageWidget::removeQtDir()
227
228
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
229
    int index = indexForTreeItem(item);
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
    if (index < 0)
        return;

    for (int i = 0; i < m_ui->defaultCombo->count(); ++i) {
        if (m_ui->defaultCombo->itemText(i) == item->text(0)) {
            m_ui->defaultCombo->removeItem(i);
            break;
        }
    }

    delete item;

    delete m_versions.takeAt(index);
    updateState();
}

246
void QtOptionsPageWidget::updateState()
247
{
248
249
    int currentIndex = indexForTreeItem(m_ui->qtdirList->currentItem());
    bool enabled = (currentIndex >= 0);
250
251
252
253
254
    bool isAutodetected = (enabled
        && m_versions.at(currentIndex)->isAutodetected());
    m_ui->delButton->setEnabled(enabled && !isAutodetected);
    m_ui->nameEdit->setEnabled(enabled && !isAutodetected);
    m_ui->qtPath->setEnabled(enabled && !isAutodetected);
255
256
257
258
259
260
261
    m_ui->mingwPath->setEnabled(enabled);

    bool hasLog = enabled && !m_ui->qtdirList->currentItem()->data(2, Qt::UserRole).toString().isEmpty();
    m_ui->showLogButton->setEnabled(hasLog);

    QtVersion *version = 0;
    if (enabled)
262
        version = m_versions.at(currentIndex);
263
264
265
266
267
268
269
270
271
272
273
    if (version) {
        m_ui->rebuildButton->setEnabled(version->isValid());
        if (version->hasDebuggingHelper())
            m_ui->debuggingHelperStateLabel->setPixmap(QPixmap(":/extensionsystem/images/ok.png"));
        else
            m_ui->debuggingHelperStateLabel->setPixmap(QPixmap(":/extensionsystem/images/error.png"));
    } else {
        m_ui->rebuildButton->setEnabled(false);
        m_ui->debuggingHelperStateLabel->setPixmap(QPixmap());
    }
}
274
void QtOptionsPageWidget::makeMingwVisible(bool visible)
275
276
277
278
279
{
    m_ui->mingwLabel->setVisible(visible);
    m_ui->mingwPath->setVisible(visible);
}

280
281
282
283
284
285
286
void QtOptionsPageWidget::makeMSVCVisible(bool visible)
{
    m_ui->msvcLabel->setVisible(visible);
    m_ui->msvcComboBox->setVisible(visible);
    m_ui->msvcNotFoundLabel->setVisible(false);
}

con's avatar
con committed
287
288
289
290
291
292
void QtOptionsPageWidget::makeMWCVisible(bool visible)
{
    m_ui->mwcLabel->setVisible(visible);
    m_ui->mwcPath->setVisible(visible);
}

293
void QtOptionsPageWidget::showEnvironmentPage(QTreeWidgetItem *item)
294
295
{
    if (item) {
296
297
298
299
        int index = indexForTreeItem(item);
        if (index < 0) {
            makeMSVCVisible(false);
            makeMingwVisible(false);
con's avatar
con committed
300
            makeMWCVisible(false);
301
302
            return;
        }
303
        m_ui->errorLabel->setText("");
304
305
        QList<ProjectExplorer::ToolChain::ToolChainType> types = m_versions.at(index)->possibleToolChainTypes();
        if (types.contains(ProjectExplorer::ToolChain::MinGW)) {
306
            makeMSVCVisible(false);
307
            makeMingwVisible(true);
con's avatar
con committed
308
            makeMWCVisible(false);
309
            m_ui->mingwPath->setPath(m_versions.at(index)->mingwDirectory());
310
        } else if (types.contains(ProjectExplorer::ToolChain::MSVC) || types.contains(ProjectExplorer::ToolChain::WINCE)){
311
            makeMSVCVisible(false);
312
            makeMingwVisible(false);
con's avatar
con committed
313
            makeMWCVisible(false);
314
315
            QStringList msvcEnvironments = ProjectExplorer::ToolChain::availableMSVCVersions();
            if (msvcEnvironments.count() == 0) {
316
317
                m_ui->msvcLabel->setVisible(true);
                m_ui->msvcNotFoundLabel->setVisible(true);
318
            } else {
319
                 makeMSVCVisible(true);
320
321
322
323
324
325
326
327
328
329
                 bool block = m_ui->msvcComboBox->blockSignals(true);
                 m_ui->msvcComboBox->clear();
                 foreach(const QString &msvcenv, msvcEnvironments) {
                     m_ui->msvcComboBox->addItem(msvcenv);
                     if (msvcenv == m_versions.at(index)->msvcVersion()) {
                         m_ui->msvcComboBox->setCurrentIndex(m_ui->msvcComboBox->count() - 1);
                     }
                 }
                 m_ui->msvcComboBox->blockSignals(block);
            }
con's avatar
con committed
330
#ifdef QTCREATOR_WITH_S60
331
        } else if (types.contains(ProjectExplorer::ToolChain::WINSCW)) {
con's avatar
con committed
332
333
334
335
336
            makeMSVCVisible(false);
            makeMingwVisible(false);
            makeMWCVisible(true);
            m_ui->mwcPath->setPath(m_versions.at(index)->mwcDirectory());
#endif
337
        } else if (types.contains(ProjectExplorer::ToolChain::INVALID)) {
338
            makeMSVCVisible(false);
339
            makeMingwVisible(false);
con's avatar
con committed
340
            makeMWCVisible(false);
341
342
343
344
            if (!m_versions.at(index)->isInstalled())
                m_ui->errorLabel->setText(tr("The Qt Version %1 is not installed. Run make install")
                                           .arg(QDir::toNativeSeparators(m_versions.at(index)->path())));
            else
345
                m_ui->errorLabel->setText(tr("%1 is not a valid Qt directory").arg(QDir::toNativeSeparators(m_versions.at(index)->path())));
346
        } else { //ProjectExplorer::ToolChain::GCC
347
            makeMSVCVisible(false);
348
            makeMingwVisible(false);
con's avatar
con committed
349
            makeMWCVisible(false);
350
351
352
353
354
            m_ui->errorLabel->setText(tr("Found Qt version %1, using mkspec %2")
                                     .arg(m_versions.at(index)->qtVersionString(),
                                          m_versions.at(index)->mkspec()));
        }
    } else {
355
        makeMSVCVisible(false);
356
        makeMingwVisible(false);
con's avatar
con committed
357
        makeMWCVisible(false);
358
359
360
    }
}

361
362
363
364
365
366
367
368
369
370
371
372
int QtOptionsPageWidget::indexForTreeItem(QTreeWidgetItem *item) const
{
    if (!item || !item->parent())
        return -1;
    int uniqueId = item->data(0, Qt::UserRole).toInt();
    for (int index = 0; index < m_versions.size(); ++index) {
        if (m_versions.at(index)->uniqueId() == uniqueId)
            return index;
    }
    return -1;
}

373
void QtOptionsPageWidget::versionChanged(QTreeWidgetItem *item, QTreeWidgetItem *old)
374
375
{
    if (old) {
376
        fixQtVersionName(indexForTreeItem(old));
377
    }
378
379
    int itemIndex = indexForTreeItem(item);
    if (itemIndex >= 0) {
380
381
382
383
384
385
386
387
388
389
        m_ui->nameEdit->setText(item->text(0));
        m_ui->qtPath->setPath(item->text(1));
    } else {
        m_ui->nameEdit->clear();
        m_ui->qtPath->setPath(""); // clear()
    }
    showEnvironmentPage(item);
    updateState();
}

390
void QtOptionsPageWidget::onQtBrowsed()
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{
    const QString dir = m_ui->qtPath->path();
    if (dir.isEmpty())
        return;

    updateCurrentQtPath();
    if (m_ui->nameEdit->text().isEmpty() || m_ui->nameEdit->text() == m_specifyNameString) {
        QStringList dirSegments = dir.split(QDir::separator(), QString::SkipEmptyParts);
        if (!dirSegments.isEmpty())
            m_ui->nameEdit->setText(dirSegments.last());
        updateCurrentQtName();
    }
    updateState();
}

406
void QtOptionsPageWidget::onMingwBrowsed()
407
408
409
410
411
412
413
414
415
{
    const QString dir = m_ui->mingwPath->path();
    if (dir.isEmpty())
        return;

    updateCurrentMingwDirectory();
    updateState();
}

416
void QtOptionsPageWidget::defaultChanged(int)
417
418
419
420
421
422
423
424
425
426
427
{
    for (int i=0; i<m_ui->defaultCombo->count(); ++i) {
        if (m_versions.at(i)->name() == m_ui->defaultCombo->currentText()) {
            m_defaultVersion = i;
            return;
        }
    }

    m_defaultVersion = 0;
}

428
void QtOptionsPageWidget::updateCurrentQtName()
429
430
431
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
432
433
434
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
435
436
437
438
439
440
441
    m_versions[currentItemIndex]->setName(m_ui->nameEdit->text());
    currentItem->setText(0, m_versions[currentItemIndex]->name());

    m_ui->defaultCombo->setItemText(currentItemIndex, m_versions[currentItemIndex]->name());
}


442
void QtOptionsPageWidget::finish()
443
444
{
    if (QTreeWidgetItem *item = m_ui->qtdirList->currentItem())
445
        fixQtVersionName(indexForTreeItem(item));
446
447
448
449
450
451
}

/* Checks that the qt version name is unique
 * and otherwise changes the name
 *
 */
452
void QtOptionsPageWidget::fixQtVersionName(int index)
453
{
454
455
    if (index < 0)
        return;
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
    int count = m_versions.count();
    for (int i = 0; i < count; ++i) {
        if (i != index) {
            if (m_versions.at(i)->name() == m_versions.at(index)->name()) {
                // Same name, find new name
                QString name = m_versions.at(index)->name();
                QRegExp regexp("^(.*)\\((\\d)\\)$");
                if (regexp.exactMatch(name)) {
                    // Alreay in Name (#) format
                    name = regexp.cap(1) + "(" + QString().setNum(regexp.cap(2).toInt() + 1) + ")";
                } else {
                    name = name + " (2)";
                }
                // set new name
                m_versions[index]->setName(name);
                m_ui->qtdirList->topLevelItem(index)->setText(0, name);
                m_ui->defaultCombo->setItemText(index, name);

                // Now check again...
                fixQtVersionName(index);
            }
        }
    }
}

481
void QtOptionsPageWidget::updateCurrentQtPath()
482
483
484
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
485
486
487
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    if (m_versions[currentItemIndex]->path() == m_ui->qtPath->path())
        return;
    m_versions[currentItemIndex]->setPath(m_ui->qtPath->path());
    currentItem->setText(1, QDir::toNativeSeparators(m_versions[currentItemIndex]->path()));

    showEnvironmentPage(currentItem);

    if (m_versions[currentItemIndex]->isValid()) {
        bool hasLog = !currentItem->data(2, Qt::UserRole).toString().isEmpty();
        bool hasHelper = m_versions[currentItemIndex]->hasDebuggingHelper();
        if (hasHelper) {
            currentItem->setData(2, Qt::DecorationRole, QIcon(":/extensionsystem/images/ok.png"));
            m_ui->debuggingHelperStateLabel->setPixmap(QPixmap(":/extensionsystem/images/ok.png"));
        } else {
            currentItem->setData(2, Qt::DecorationRole, QIcon(":/extensionsystem/images/error.png"));
            m_ui->debuggingHelperStateLabel->setPixmap(QPixmap(":/extensionsystem/images/error.png"));
        }
        m_ui->showLogButton->setEnabled(hasLog);
506
        m_ui->rebuildButton->setEnabled(true);
507
508
509
    } else {
        currentItem->setData(2, Qt::DecorationRole, QIcon());
        m_ui->debuggingHelperStateLabel->setPixmap(QPixmap());
510
        m_ui->rebuildButton->setEnabled(true);
511
512
513
    }
}

514
void QtOptionsPageWidget::updateCurrentMingwDirectory()
515
516
517
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
518
519
520
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
521
522
523
    m_versions[currentItemIndex]->setMingwDirectory(m_ui->mingwPath->path());
}

524
void QtOptionsPageWidget::msvcVersionChanged()
525
526
527
528
{
    const QString &msvcVersion = m_ui->msvcComboBox->currentText();
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
529
530
531
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
532
533
534
    m_versions[currentItemIndex]->setMsvcVersion(msvcVersion);
}

con's avatar
con committed
535
536
537
538
539
540
541
542
543
544
545
546
#ifdef QTCREATOR_WITH_S60
void QtOptionsPageWidget::updateCurrentMwcDirectory()
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
    m_versions[currentItemIndex]->setMwcDirectory(m_ui->mwcPath->path());
}
#endif

547
QList<QtVersion *> QtOptionsPageWidget::versions() const
548
549
550
551
{
    return m_versions;
}

552
int QtOptionsPageWidget::defaultVersion() const
553
554
555
{
    return m_defaultVersion;
}