summaryrefslogtreecommitdiff
path: root/src/plugins/qt4projectmanager/qtoptionspage.cpp
blob: 275229cfa48135fe0aad9a072f6ccc6850913160 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#include "qtoptionspage.h"
#include "ui_showbuildlog.h"
#include "ui_qtversionmanager.h"
#include "qt4projectmanagerconstants.h"
#include "qtversionmanager.h"
#include <coreplugin/coreconstants.h>
#include <utils/treewidgetcolumnstretcher.h>

#include <QtCore/QDebug>
#include <QtCore/QDir>

using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
///
// 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();
    m_widget = new QtOptionsPageWidget(parent, vm->versions(), vm->currentQtVersion());
    return m_widget;
}

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

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

//-----------------------------------------------------


QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent, QList<QtVersion *> versions, QtVersion *defaultVersion)
    : 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"));
    m_ui->mwcPath->setExpectedKind(Core::Utils::PathChooser::Directory);
    m_ui->mwcPath->setPromptDialogTitle(tr("Select \"x86build\" Directory from Carbide Install"));

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

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

    // 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);

    for (int i = 0; i < m_versions.count(); ++i) {
        const QtVersion * const version = m_versions.at(i);
        QTreeWidgetItem *item = new QTreeWidgetItem(version->isAutodetected()? autoItem : manualItem);
        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);
    }
    m_ui->qtdirList->expandAll();

    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()));
#ifdef QTCREATOR_WITH_S60
    connect(m_ui->mwcPath, SIGNAL(changed()),
            this, SLOT(updateCurrentMwcDirectory()));
#endif

    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();
}

void QtOptionsPageWidget::buildDebuggingHelper()
{
    // Find the qt version for this button..
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        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);
}

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

    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
    QDialog dlg;
    Ui_ShowBuildLog ui;
    ui.setupUi(&dlg);
    ui.log->setPlainText(currentItem->data(2, Qt::UserRole).toString());
    dlg.exec();
}

QtOptionsPageWidget::~QtOptionsPageWidget()
{
    qDeleteAll(m_versions);
    delete m_ui;
}

void QtOptionsPageWidget::addQtDir()
{
    QtVersion *newVersion = new QtVersion(m_specifyNameString, m_specifyPathString);
    m_versions.append(newVersion);

    QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList->topLevelItem(1));
    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();
}

void QtOptionsPageWidget::removeQtDir()
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
    int index = indexForTreeItem(item);
    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();
}

void QtOptionsPageWidget::updateState()
{
    int currentIndex = indexForTreeItem(m_ui->qtdirList->currentItem());
    bool enabled = (currentIndex >= 0);
    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);
    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)
        version = m_versions.at(currentIndex);
    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());
    }
}
void QtOptionsPageWidget::makeMingwVisible(bool visible)
{
    m_ui->mingwLabel->setVisible(visible);
    m_ui->mingwPath->setVisible(visible);
}

void QtOptionsPageWidget::makeMSVCVisible(bool visible)
{
    m_ui->msvcLabel->setVisible(visible);
    m_ui->msvcComboBox->setVisible(visible);
    m_ui->msvcNotFoundLabel->setVisible(false);
}

void QtOptionsPageWidget::makeMWCVisible(bool visible)
{
    m_ui->mwcLabel->setVisible(visible);
    m_ui->mwcPath->setVisible(visible);
}

void QtOptionsPageWidget::showEnvironmentPage(QTreeWidgetItem *item)
{
    if (item) {
        int index = indexForTreeItem(item);
        if (index < 0) {
            makeMSVCVisible(false);
            makeMingwVisible(false);
            makeMWCVisible(false);
            return;
        }
        m_ui->errorLabel->setText("");
        QList<ProjectExplorer::ToolChain::ToolChainType> types = m_versions.at(index)->possibleToolChainTypes();
        if (types.contains(ProjectExplorer::ToolChain::MinGW)) {
            makeMSVCVisible(false);
            makeMingwVisible(true);
            makeMWCVisible(false);
            m_ui->mingwPath->setPath(m_versions.at(index)->mingwDirectory());
        } else if (types.contains(ProjectExplorer::ToolChain::MSVC) || types.contains(ProjectExplorer::ToolChain::WINCE)){
            makeMSVCVisible(false);
            makeMingwVisible(false);
            makeMWCVisible(false);
            QStringList msvcEnvironments = ProjectExplorer::ToolChain::availableMSVCVersions();
            if (msvcEnvironments.count() == 0) {
                m_ui->msvcLabel->setVisible(true);
                m_ui->msvcNotFoundLabel->setVisible(true);
            } else {
                 makeMSVCVisible(true);
                 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);
            }
#ifdef QTCREATOR_WITH_S60
        } else if (types.contains(ProjectExplorer::ToolChain::WINSCW)) {
            makeMSVCVisible(false);
            makeMingwVisible(false);
            makeMWCVisible(true);
            m_ui->mwcPath->setPath(m_versions.at(index)->mwcDirectory());
#endif
        } else if (types.contains(ProjectExplorer::ToolChain::INVALID)) {
            makeMSVCVisible(false);
            makeMingwVisible(false);
            makeMWCVisible(false);
            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
                m_ui->errorLabel->setText(tr("%1 is not a valid Qt directory").arg(QDir::toNativeSeparators(m_versions.at(index)->path())));
        } else { //ProjectExplorer::ToolChain::GCC
            makeMSVCVisible(false);
            makeMingwVisible(false);
            makeMWCVisible(false);
            m_ui->errorLabel->setText(tr("Found Qt version %1, using mkspec %2")
                                     .arg(m_versions.at(index)->qtVersionString(),
                                          m_versions.at(index)->mkspec()));
        }
    } else {
        makeMSVCVisible(false);
        makeMingwVisible(false);
        makeMWCVisible(false);
    }
}

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;
}

void QtOptionsPageWidget::versionChanged(QTreeWidgetItem *item, QTreeWidgetItem *old)
{
    if (old) {
        fixQtVersionName(indexForTreeItem(old));
    }
    int itemIndex = indexForTreeItem(item);
    if (itemIndex >= 0) {
        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();
}

void QtOptionsPageWidget::onQtBrowsed()
{
    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();
}

void QtOptionsPageWidget::onMingwBrowsed()
{
    const QString dir = m_ui->mingwPath->path();
    if (dir.isEmpty())
        return;

    updateCurrentMingwDirectory();
    updateState();
}

void QtOptionsPageWidget::defaultChanged(int)
{
    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;
}

void QtOptionsPageWidget::updateCurrentQtName()
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
    m_versions[currentItemIndex]->setName(m_ui->nameEdit->text());
    currentItem->setText(0, m_versions[currentItemIndex]->name());

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


void QtOptionsPageWidget::finish()
{
    if (QTreeWidgetItem *item = m_ui->qtdirList->currentItem())
        fixQtVersionName(indexForTreeItem(item));
}

/* Checks that the qt version name is unique
 * and otherwise changes the name
 *
 */
void QtOptionsPageWidget::fixQtVersionName(int index)
{
    if (index < 0)
        return;
    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);
            }
        }
    }
}

void QtOptionsPageWidget::updateCurrentQtPath()
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
    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);
        m_ui->rebuildButton->setEnabled(true);
    } else {
        currentItem->setData(2, Qt::DecorationRole, QIcon());
        m_ui->debuggingHelperStateLabel->setPixmap(QPixmap());
        m_ui->rebuildButton->setEnabled(true);
    }
}

void QtOptionsPageWidget::updateCurrentMingwDirectory()
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
    m_versions[currentItemIndex]->setMingwDirectory(m_ui->mingwPath->path());
}

void QtOptionsPageWidget::msvcVersionChanged()
{
    const QString &msvcVersion = m_ui->msvcComboBox->currentText();
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
    m_versions[currentItemIndex]->setMsvcVersion(msvcVersion);
}

#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

QList<QtVersion *> QtOptionsPageWidget::versions() const
{
    return m_versions;
}

int QtOptionsPageWidget::defaultVersion() const
{
    return m_defaultVersion;
}