qtoptionspage.cpp 41.9 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
29

30
#include "qtoptionspage.h"
31
#include "qtconfigwidget.h"
32 33
#include "ui_showbuildlog.h"
#include "ui_qtversionmanager.h"
34 35
#include "ui_qtversioninfo.h"
#include "ui_debugginghelper.h"
36
#include "qtsupportconstants.h"
37
#include "qtversionmanager.h"
dt's avatar
dt committed
38
#include "qtversionfactory.h"
39 40 41
#include "qmldumptool.h"
#include "qmldebugginglibrary.h"
#include "qmlobservertool.h"
42 43

#include <coreplugin/progressmanager/progressmanager.h>
44
#include <projectexplorer/toolchainmanager.h>
45
#include <projectexplorer/projectexplorerconstants.h>
46
#include <utils/hostosinfo.h>
hjk's avatar
hjk committed
47 48
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
49
#include <utils/runextensions.h>
50

51 52 53
#include <QDir>
#include <QMessageBox>
#include <QFileDialog>
54 55
#include <QTextBrowser>
#include <QDesktopServices>
56

hjk's avatar
hjk committed
57
using namespace ProjectExplorer;
58
using namespace Utils;
59

hjk's avatar
hjk committed
60 61 62 63 64
namespace QtSupport {
namespace Internal {

enum ModelRoles { VersionIdRole = Qt::UserRole, ToolChainIdRole, BuildLogRole, BuildRunningRole};

65 66 67 68 69
///
// QtOptionsPage
///

QtOptionsPage::QtOptionsPage()
dt's avatar
dt committed
70
    : m_widget(0)
71
{
hjk's avatar
hjk committed
72
    setId(Constants::QTVERSION_SETTINGS_PAGE_ID);
73
    setDisplayName(QCoreApplication::translate("Qt4ProjectManager", Constants::QTVERSION_SETTINGS_PAGE_NAME));
hjk's avatar
hjk committed
74
    setCategory(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
75 76 77
    setDisplayCategory(QCoreApplication::translate("ProjectExplorer",
        ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY));
    setCategoryIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
78 79
}

80 81
QWidget *QtOptionsPage::createPage(QWidget *parent)
{
82
    m_widget = new QtOptionsPageWidget(parent);
83 84
    if (m_searchKeywords.isEmpty())
        m_searchKeywords = m_widget->searchKeywords();
85 86 87 88 89
    return m_widget;
}

void QtOptionsPage::apply()
{
90 91
    if (!m_widget) // page was never shown
        return;
92 93
    m_widget->finish();

94
    m_widget->apply();
95 96
}

97 98 99 100 101
bool QtOptionsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

102
//-----------------------------------------------------
103 104


105
QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent)
106 107
    : QWidget(parent)
    , m_specifyNameString(tr("<specify a name>"))
108
    , m_ui(new Internal::Ui::QtVersionManager())
109 110
    , m_versionUi(new Internal::Ui::QtVersionInfo())
    , m_debuggingHelperUi(new Internal::Ui::DebuggingHelper())
111
    , m_infoBrowser(new QTextBrowser)
112 113
    , m_invalidVersionIcon(QLatin1String(":/projectexplorer/images/compile_error.png"))
    , m_warningVersionIcon(QLatin1String(":/projectexplorer/images/compile_warning.png"))
dt's avatar
dt committed
114
    , m_configurationWidget(0)
115 116
    , m_autoItem(0)
    , m_manualItem(0)
117
{
118 119
    QWidget *versionInfoWidget = new QWidget();
    m_versionUi->setupUi(versionInfoWidget);
hjk's avatar
hjk committed
120
    m_versionUi->editPathPushButton->setText(QCoreApplication::translate("Utils::PathChooser", PathChooser::browseButtonLabel));
121

122 123 124
    QWidget *debuggingHelperDetailsWidget = new QWidget();
    m_debuggingHelperUi->setupUi(debuggingHelperDetailsWidget);

125
    m_ui->setupUi(this);
126

127 128 129 130
    m_infoBrowser->setOpenLinks(false);
    m_infoBrowser->setTextInteractionFlags(Qt::TextBrowserInteraction);
    connect(m_infoBrowser, SIGNAL(anchorClicked(QUrl)), this, SLOT(infoAnchorClicked(QUrl)));
    m_ui->infoWidget->setWidget(m_infoBrowser);
131
    connect(m_ui->infoWidget, SIGNAL(expanded(bool)),
132
            this, SLOT(setInfoWidgetVisibility()));
133

134
    m_ui->versionInfoWidget->setWidget(versionInfoWidget);
hjk's avatar
hjk committed
135
    m_ui->versionInfoWidget->setState(DetailsWidget::NoSummary);
136 137

    m_ui->debuggingHelperWidget->setWidget(debuggingHelperDetailsWidget);
138
    connect(m_ui->debuggingHelperWidget, SIGNAL(expanded(bool)),
139
            this, SLOT(setInfoWidgetVisibility()));
140

141 142
    // setup parent items for auto-detected and manual versions
    m_ui->qtdirList->header()->setResizeMode(QHeaderView::ResizeToContents);
dt_'s avatar
dt_ committed
143 144
    m_ui->qtdirList->header()->setStretchLastSection(false);
    m_ui->qtdirList->setTextElideMode(Qt::ElideNone);
145 146 147 148 149 150 151 152 153 154
    m_autoItem = new QTreeWidgetItem(m_ui->qtdirList);
    m_autoItem->setText(0, tr("Auto-detected"));
    m_autoItem->setFirstColumnSpanned(true);
    m_autoItem->setFlags(Qt::ItemIsEnabled);
    m_manualItem = new QTreeWidgetItem(m_ui->qtdirList);
    m_manualItem->setText(0, tr("Manual"));
    m_manualItem->setFirstColumnSpanned(true);
    m_manualItem->setFlags(Qt::ItemIsEnabled);

    QList<int> additions;
hjk's avatar
hjk committed
155
    foreach (BaseQtVersion *v, QtVersionManager::versions())
156 157 158
        additions.append(v->uniqueId());

    updateQtVersions(additions, QList<int>(), QList<int>());
159 160

    m_ui->qtdirList->expandAll();
161

Robert Loehning's avatar
Robert Loehning committed
162
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(QString)),
163 164
            this, SLOT(updateCurrentQtName()));

dt_'s avatar
dt_ committed
165 166 167
    connect(m_versionUi->editPathPushButton, SIGNAL(clicked()),
            this, SLOT(editPath()));

168 169 170 171 172
    connect(m_ui->addButton, SIGNAL(clicked()),
            this, SLOT(addQtDir()));
    connect(m_ui->delButton, SIGNAL(clicked()),
            this, SLOT(removeQtDir()));

Robert Loehning's avatar
Robert Loehning committed
173 174
    connect(m_ui->qtdirList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
            this, SLOT(versionChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
175

176
    connect(m_debuggingHelperUi->rebuildButton, SIGNAL(clicked()),
177
            this, SLOT(buildDebuggingHelper()));
178 179 180 181
    connect(m_debuggingHelperUi->gdbHelperBuildButton, SIGNAL(clicked()),
            this, SLOT(buildGdbHelper()));
    connect(m_debuggingHelperUi->qmlDumpBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDump()));
182 183
    connect(m_debuggingHelperUi->qmlDebuggingLibBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDebuggingLibrary()));
184 185 186 187
    connect(m_debuggingHelperUi->qmlObserverBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlObserver()));

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
188
            this, SLOT(slotShowDebuggingBuildLog()));
189 190
    connect(m_debuggingHelperUi->toolChainComboBox, SIGNAL(activated(int)),
            this, SLOT(selectedToolChainChanged(int)));
191

192
    connect(m_ui->cleanUpButton, SIGNAL(clicked()), this, SLOT(cleanUpQtVersions()));
dt's avatar
dt committed
193 194
    userChangedCurrentVersion();
    updateCleanUpButton();
195

196 197
    connect(QtVersionManager::instance(), SIGNAL(dumpUpdatedFor(Utils::FileName)),
            this, SLOT(qtVersionsDumpUpdated(Utils::FileName)));
198

199 200 201
    connect(QtVersionManager::instance(), SIGNAL(qtVersionsChanged(QList<int>,QList<int>,QList<int>)),
            this, SLOT(updateQtVersions(QList<int>,QList<int>,QList<int>)));

202 203
    connect(ProjectExplorer::ToolChainManager::instance(), SIGNAL(toolChainsChanged()),
            this, SLOT(toolChainsUpdated()));
204 205
}

206
int QtOptionsPageWidget::currentIndex() const
207
{
208 209 210 211
    if (QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        return indexForTreeItem(currentItem);
    return -1;
}
212

dt's avatar
dt committed
213
BaseQtVersion *QtOptionsPageWidget::currentVersion() const
214 215 216
{
    const int currentItemIndex = currentIndex();
    if (currentItemIndex >= 0 && currentItemIndex < m_versions.size())
217
        return m_versions.at(currentItemIndex);
218 219
    return 0;
}
220

dt's avatar
dt committed
221
static inline int findVersionById(const QList<BaseQtVersion *> &l, int id)
222 223 224
{
    const int size = l.size();
    for (int i = 0; i < size; i++)
225
        if (l.at(i)->uniqueId() == id)
226 227 228
            return i;
    return -1;
}
229

230
// Update with results of terminated helper build
231
void QtOptionsPageWidget::debuggingHelperBuildFinished(int qtVersionId, const QString &output, DebuggingHelperBuildTask::Tools tools)
232
{
233
    const int index = findVersionById(m_versions, qtVersionId);
234 235
    if (index == -1)
        return; // Oops, somebody managed to delete the version
236

dt's avatar
dt committed
237
    BaseQtVersion *version = m_versions.at(index);
238

239 240
    // Update item view
    QTreeWidgetItem *item = treeItemForIndex(index);
241 242 243 244 245
    QTC_ASSERT(item, return);
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags &= ~tools;
    item->setData(0, BuildRunningRole,  QVariant::fromValue(buildFlags));
246
    item->setData(0, BuildLogRole, output);
247

248 249
    bool success = true;
    if (tools & DebuggingHelperBuildTask::GdbDebugging)
dt's avatar
dt committed
250
        success &= version->hasGdbDebuggingHelper();
251
    if (tools & DebuggingHelperBuildTask::QmlDebugging)
dt's avatar
dt committed
252
        success &= version->hasQmlDebuggingLibrary();
253
    if (tools & DebuggingHelperBuildTask::QmlDump)
dt's avatar
dt committed
254
        success &= version->hasQmlDump();
255
    if (tools & DebuggingHelperBuildTask::QmlObserver)
dt's avatar
dt committed
256
        success &= version->hasQmlObserver();
257

258 259
    if (!success)
        showDebuggingBuildLog(item);
260 261

    updateDebuggingHelperUi();
262 263
}

264 265 266
void QtOptionsPageWidget::cleanUpQtVersions()
{
    QStringList toRemove;
dt's avatar
dt committed
267
    foreach (const BaseQtVersion *v, m_versions) {
268
        if (!v->isValid())
269 270 271 272 273 274
            toRemove.append(v->displayName());
    }

    if (toRemove.isEmpty())
        return;

Friedemann Kleint's avatar
Friedemann Kleint committed
275
    if (QMessageBox::warning(0, tr("Remove Invalid Qt Versions"),
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
                             tr("Do you want to remove all invalid Qt Versions?<br>"
                                "<ul><li>%1</li></ul><br>"
                                "will be removed.").arg(toRemove.join(QLatin1String("</li><li>"))),
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;

    for (int i = m_versions.count() - 1; i >= 0; --i) {
        if (!m_versions.at(i)->isValid()) {
            QTreeWidgetItem *item = treeItemForIndex(i);
            delete item;

            delete m_versions.at(i);
            m_versions.removeAt(i);
        }
    }
dt's avatar
dt committed
291
    updateCleanUpButton();
292 293
}

294 295 296 297
void QtOptionsPageWidget::toolChainsUpdated()
{
    for (int i = 0; i < m_versions.count(); ++i) {
        QTreeWidgetItem *item = treeItemForIndex(i);
298
        if (item == m_ui->qtdirList->currentItem()) {
299
            updateDescriptionLabel();
300 301
            updateDebuggingHelperUi();
        } else {
302 303 304 305 306 307
            const ValidityInfo info = validInformation(m_versions.at(i));
            item->setIcon(0, info.icon);
        }
    }
}

308 309 310 311 312 313 314 315 316 317 318 319 320 321
void QtOptionsPageWidget::selectedToolChainChanged(int comboIndex)
{
    const int index = currentIndex();
    if (index < 0)
        return;

    QTreeWidgetItem *item = treeItemForIndex(index);
    QTC_ASSERT(item, return);

    QString toolChainId = m_debuggingHelperUi->toolChainComboBox->itemData(comboIndex).toString();

    item->setData(0, ToolChainIdRole, toolChainId);
}

hjk's avatar
hjk committed
322
void QtOptionsPageWidget::qtVersionsDumpUpdated(const FileName &qmakeCommand)
323 324 325 326 327
{
    foreach (BaseQtVersion *version, m_versions) {
        if (version->qmakeCommand() == qmakeCommand)
            version->recheckDumper();
    }
328 329
    if (currentVersion()
            && currentVersion()->qmakeCommand() == qmakeCommand) {
330 331 332 333 334 335
        updateWidgets();
        updateDescriptionLabel();
        updateDebuggingHelperUi();
    }
}

336
void QtOptionsPageWidget::setInfoWidgetVisibility()
337
{
338 339 340 341
    m_ui->versionInfoWidget->setVisible((m_ui->infoWidget->state() == DetailsWidget::Collapsed)
                                        && (m_ui->debuggingHelperWidget->state() == DetailsWidget::Collapsed));
    m_ui->infoWidget->setVisible(m_ui->debuggingHelperWidget->state() == DetailsWidget::Collapsed);
    m_ui->debuggingHelperWidget->setVisible(m_ui->infoWidget->state() == DetailsWidget::Collapsed);
342 343
}

344 345 346 347 348
void QtOptionsPageWidget::infoAnchorClicked(const QUrl &url)
{
    QDesktopServices::openUrl(url);
}

349 350 351 352 353 354 355 356
QtOptionsPageWidget::ValidityInfo QtOptionsPageWidget::validInformation(const BaseQtVersion *version)
{
    ValidityInfo info;
    info.icon = m_validVersionIcon;

    if (!version)
        return info;

357
    info.description = tr("Qt version %1 for %2").arg(version->qtVersionString(), version->description());
358 359 360 361 362 363 364 365 366
    if (!version->isValid()) {
        info.icon = m_invalidVersionIcon;
        info.message = version->invalidReason();
        return info;
    }

    // Do we have tool chain issues?
    QStringList missingToolChains;
    int abiCount = 0;
hjk's avatar
hjk committed
367 368 369
    foreach (const Abi &abi, version->qtAbis()) {
        if (ToolChainManager::findToolChains(abi).isEmpty())
            missingToolChains.append(abi.toString());
370 371 372
        ++abiCount;
    }

373
    bool useable = true;
374
    QStringList warnings;
375 376 377
    if (!missingToolChains.isEmpty()) {
        if (missingToolChains.count() == abiCount) {
            // Yes, this Qt version can't be used at all!
378
            info.message = tr("No compiler can produce code for this Qt version. Please define one or more compilers.");
379 380 381 382
            info.icon = m_invalidVersionIcon;
            useable = false;
        } else {
            // Yes, some ABIs are unsupported
383
            warnings << tr("Not all possible target environments can be supported due to missing compilers.");
384 385 386 387
            info.toolTip = tr("The following ABIs are currently not supported:<ul><li>%1</li></ul>")
                    .arg(missingToolChains.join(QLatin1String("</li><li>")));
            info.icon = m_warningVersionIcon;
        }
388
    }
389 390

    if (useable) {
391 392 393
        warnings += version->warningReason();
        if (!warnings.isEmpty()) {
            info.message = warnings.join(QLatin1String("\n"));
394 395 396 397
            info.icon = m_warningVersionIcon;
        }
    }

398 399 400
    return info;
}

hjk's avatar
hjk committed
401
QList<ToolChain*> QtOptionsPageWidget::toolChains(const BaseQtVersion *version)
402
{
hjk's avatar
hjk committed
403
    QHash<QString,ToolChain*> toolChains;
404 405 406
    if (!version)
        return toolChains.values();

hjk's avatar
hjk committed
407 408
    foreach (const Abi &a, version->qtAbis())
        foreach (ToolChain *tc, ToolChainManager::findToolChains(a))
409 410 411 412 413
            toolChains.insert(tc->id(), tc);

    return toolChains.values();
}

414 415
QString QtOptionsPageWidget::defaultToolChainId(const BaseQtVersion *version)
{
hjk's avatar
hjk committed
416
    QList<ToolChain*> possibleToolChains = toolChains(version);
417 418 419 420 421
    if (!possibleToolChains.isEmpty())
        return possibleToolChains.first()->id();
    return QString();
}

422
void QtOptionsPageWidget::buildDebuggingHelper(DebuggingHelperBuildTask::Tools tools)
423 424 425 426 427
{
    const int index = currentIndex();
    if (index < 0)
        return;

428 429 430
    // remove tools that cannot be build
    tools &= DebuggingHelperBuildTask::availableTools(currentVersion());

431 432
    QTreeWidgetItem *item = treeItemForIndex(index);
    QTC_ASSERT(item, return);
433

434 435 436 437
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags |= tools;
    item->setData(0, BuildRunningRole, QVariant::fromValue(buildFlags));
438

dt's avatar
dt committed
439
    BaseQtVersion *version = m_versions.at(index);
440 441 442
    if (!version)
        return;

443
    updateDebuggingHelperUi();
444

445
    // Run a debugging helper build task in the background.
446 447
    QString toolChainId = m_debuggingHelperUi->toolChainComboBox->itemData(
                m_debuggingHelperUi->toolChainComboBox->currentIndex()).toString();
hjk's avatar
hjk committed
448
    ToolChain *toolChain = ToolChainManager::findToolChain(toolChainId);
449 450 451 452
    if (!toolChain)
        return;

    DebuggingHelperBuildTask *buildTask = new DebuggingHelperBuildTask(version, toolChain, tools);
453 454
    // Don't open General Messages pane with errors
    buildTask->showOutputOnError(false);
455 456
    connect(buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
            this, SLOT(debuggingHelperBuildFinished(int,QString,DebuggingHelperBuildTask::Tools)),
457
            Qt::QueuedConnection);
458
    QFuture<void> task = QtConcurrent::run(&DebuggingHelperBuildTask::run, buildTask);
459
    const QString taskName = tr("Building helpers");
460

461
    Core::ProgressManager::addTask(task, taskName, "Qt4ProjectManager::BuildHelpers");
462
}
463 464 465 466 467 468 469 470 471 472
void QtOptionsPageWidget::buildGdbHelper()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::GdbDebugging);
}

void QtOptionsPageWidget::buildQmlDump()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDump);
}

473 474 475 476 477
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

478 479
void QtOptionsPageWidget::buildQmlObserver()
{
480
    DebuggingHelperBuildTask::Tools qmlDbgTools =
Kai Koehne's avatar
Kai Koehne committed
481 482
            DebuggingHelperBuildTask::QmlObserver;
    qmlDbgTools |= DebuggingHelperBuildTask::QmlDebugging;
483
    buildDebuggingHelper(qmlDbgTools);
484
}
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
// Non-modal dialog
class BuildLogDialog : public QDialog {
public:
    explicit BuildLogDialog(QWidget *parent = 0);
    void setText(const QString &text);

private:
    Ui_ShowBuildLog m_ui;
};

BuildLogDialog::BuildLogDialog(QWidget *parent) : QDialog(parent)
{
    m_ui.setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose, true);
}

void BuildLogDialog::setText(const QString &text)
503
{
504 505 506 507
    m_ui.log->setPlainText(text); // Show and scroll to bottom
    m_ui.log->moveCursor(QTextCursor::End);
    m_ui.log->ensureCursorVisible();
}
508

509 510 511 512 513 514 515 516 517
void QtOptionsPageWidget::slotShowDebuggingBuildLog()
{
    if (const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        showDebuggingBuildLog(currentItem);
}

void QtOptionsPageWidget::showDebuggingBuildLog(const QTreeWidgetItem *currentItem)
{
    const int currentItemIndex = indexForTreeItem(currentItem);
518 519
    if (currentItemIndex < 0)
        return;
520
    BuildLogDialog *dialog = new BuildLogDialog(this->window());
521
    dialog->setWindowTitle(tr("Debugging Helper Build Log for '%1'").arg(currentItem->text(0)));
522
    dialog->setText(currentItem->data(0, BuildLogRole).toString());
523
    dialog->show();
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 556 557 558 559 560 561 562 563
void QtOptionsPageWidget::updateQtVersions(const QList<int> &additions, const QList<int> &removals,
                                           const QList<int> &changes)
{
    QList<QTreeWidgetItem *> toRemove;
    QList<int> toAdd = additions;

    // Generate list of all existing items:
    QList<QTreeWidgetItem *> itemList;
    for (int i = 0; i < m_autoItem->childCount(); ++i)
        itemList.append(m_autoItem->child(i));
    for (int i = 0; i < m_manualItem->childCount(); ++i)
        itemList.append(m_manualItem->child(i));

    // Find existing items to remove/change:
    foreach (QTreeWidgetItem *item, itemList) {
        int id = item->data(0, VersionIdRole).toInt();
        if (removals.contains(id)) {
            toRemove.append(item);
            continue;
        }

        if (changes.contains(id)) {
            toAdd.append(id);
            toRemove.append(item);
            continue;
        }
    }

    // Remove changed/removed items:
    foreach (QTreeWidgetItem *item, toRemove) {
        int index = indexForTreeItem(item);
        delete m_versions.at(index);
        m_versions.removeAt(index);
        delete item;
    }

    // Add changed/added items:
    foreach (int a, toAdd) {
hjk's avatar
hjk committed
564
        BaseQtVersion *version = QtVersionManager::version(a)->clone();
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
        m_versions.append(version);
        QTreeWidgetItem *item = new QTreeWidgetItem;

        item->setText(0, version->displayName());
        item->setText(1, version->qmakeCommand().toUserOutput());
        item->setData(0, VersionIdRole, version->uniqueId());
        item->setData(0, ToolChainIdRole, defaultToolChainId(version));
        const ValidityInfo info = validInformation(version);
        item->setIcon(0, info.icon);

        // Insert in the right place:
        QTreeWidgetItem *parent = version->isAutodetected()? m_autoItem : m_manualItem;
        for (int i = 0; i < parent->childCount(); ++i) {
            BaseQtVersion *currentVersion = m_versions.at(indexForTreeItem(parent->child(i)));
            if (currentVersion->qtVersion() > version->qtVersion())
                continue;
            parent->insertChild(i, item);
            parent = 0;
            break;
        }

        if (parent)
            parent->addChild(item);
    }
}

591
QtOptionsPageWidget::~QtOptionsPageWidget()
592 593
{
    delete m_ui;
dt's avatar
dt committed
594 595
    delete m_versionUi;
    delete m_debuggingHelperUi;
dt's avatar
dt committed
596
    delete m_configurationWidget;
597
    qDeleteAll(m_versions);
598 599
}

dt_'s avatar
dt_ committed
600
static QString filterForQmakeFileDialog()
601
{
602
    QString filter = QLatin1String("qmake (");
hjk's avatar
hjk committed
603
    const QStringList commands = BuildableHelperLibrary::possibleQMakeCommands();
604 605 606
    for (int i = 0; i < commands.size(); ++i) {
        if (i)
            filter += QLatin1Char(' ');
hjk's avatar
hjk committed
607
        if (HostOsInfo::isMacHost())
608 609
            // work around QTBUG-7739 that prohibits filters that don't start with *
            filter += QLatin1Char('*');
610
        filter += commands.at(i);
hjk's avatar
hjk committed
611
        if (HostOsInfo::isAnyUnixHost() && !HostOsInfo::isMacHost())
612 613 614
            // kde bug, we need at least one wildcard character
            // see QTCREATORBUG-7771
            filter += QLatin1Char('*');
dt's avatar
dt committed
615
    }
616
    filter += QLatin1Char(')');
dt_'s avatar
dt_ committed
617 618
    return filter;
}
619

dt_'s avatar
dt_ committed
620 621
void QtOptionsPageWidget::addQtDir()
{
hjk's avatar
hjk committed
622
    FileName qtVersion = FileName::fromString(
Tobias Hunger's avatar
Tobias Hunger committed
623
                QFileDialog::getOpenFileName(this,
Friedemann Kleint's avatar
Friedemann Kleint committed
624
                                             tr("Select a qmake Executable"),
Tobias Hunger's avatar
Tobias Hunger committed
625 626 627 628
                                             QString(),
                                             filterForQmakeFileDialog(),
                                             0,
                                             QFileDialog::DontResolveSymlinks));
dt's avatar
dt committed
629 630
    if (qtVersion.isNull())
        return;
631 632 633 634

    QFileInfo fi(qtVersion.toString());
    // should add all qt versions here ?
    if (BuildableHelperLibrary::isQtChooser(fi))
hjk's avatar
hjk committed
635
        qtVersion = FileName::fromString(BuildableHelperLibrary::qtChooserToQmakePath(fi.symLinkTarget()));
636

637 638 639 640 641 642 643
    BaseQtVersion *version = 0;
    foreach (BaseQtVersion *v, m_versions) {
        if (v->qmakeCommand() == qtVersion) {
            version = v;
            break;
        }
    }
644
    if (version) {
dt's avatar
dt committed
645
        // Already exist
Friedemann Kleint's avatar
Friedemann Kleint committed
646
        QMessageBox::warning(this, tr("Qt Version Already Known"),
647 648 649
                             tr("This Qt version was already registered as \"%1\".")
                             .arg(version->displayName()));
        return;
dt's avatar
dt committed
650
    }
651

652 653
    QString error;
    version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion, false, QString(), &error);
dt's avatar
dt committed
654 655
    if (version) {
        m_versions.append(version);
656

dt's avatar
dt committed
657 658
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList->topLevelItem(1));
        item->setText(0, version->displayName());
659
        item->setText(1, version->qmakeCommand().toUserOutput());
dt's avatar
dt committed
660
        item->setData(0, VersionIdRole, version->uniqueId());
661
        item->setData(0, ToolChainIdRole, defaultToolChainId(version));
dt's avatar
dt committed
662 663 664 665
        item->setIcon(0, version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
        m_ui->qtdirList->setCurrentItem(item); // should update the rest of the ui
        m_versionUi->nameEdit->setFocus();
        m_versionUi->nameEdit->selectAll();
666
    } else {
667
        QMessageBox::warning(this, tr("Qmake Not Executable"),
Friedemann Kleint's avatar
Friedemann Kleint committed
668
                             tr("The qmake executable %1 could not be added: %2").arg(qtVersion.toUserOutput()).arg(error));
669
        return;
dt's avatar
dt committed
670 671
    }
    updateCleanUpButton();
672 673
}

674
void QtOptionsPageWidget::removeQtDir()
675 676
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
677
    int index = indexForTreeItem(item);
678 679 680 681 682
    if (index < 0)
        return;

    delete item;

dt's avatar
dt committed
683
    BaseQtVersion *version = m_versions.at(index);
684
    m_versions.removeAt(index);
685
    delete version;
dt's avatar
dt committed
686
    updateCleanUpButton();
687 688
}

dt_'s avatar
dt_ committed
689 690
void QtOptionsPageWidget::editPath()
{
691
    BaseQtVersion *current = currentVersion();
692
    QString dir = currentVersion()->qmakeCommand().toFileInfo().absolutePath();
hjk's avatar
hjk committed
693
    FileName qtVersion = FileName::fromString(
694 695
                QFileDialog::getOpenFileName(this,
                                             tr("Select a qmake executable"),
696 697 698 699
                                             dir,
                                             filterForQmakeFileDialog(),
                                             0,
                                             QFileDialog::DontResolveSymlinks));
dt_'s avatar
dt_ committed
700 701 702
    if (qtVersion.isNull())
        return;
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
Daniel Teske's avatar
Daniel Teske committed
703 704
    if (!version)
        return;
dt_'s avatar
dt_ committed
705 706 707
    // Same type? then replace!
    if (current->type() != version->type()) {
        // not the same type, error out
Friedemann Kleint's avatar
Friedemann Kleint committed
708
        QMessageBox::critical(this, tr("Incompatible Qt Versions"),
Friedemann Kleint's avatar
Friedemann Kleint committed
709
                              tr("The Qt version selected must match the device type."),
dt_'s avatar
dt_ committed
710 711 712 713 714 715
                              QMessageBox::Ok);
        delete version;
        return;
    }
    // same type, replace
    version->setId(current->uniqueId());
dt_'s avatar
dt_ committed
716 717
    if (current->displayName() != current->defaultDisplayName(current->qtVersionString(), current->qmakeCommand()))
        version->setDisplayName(current->displayName());
dt_'s avatar
dt_ committed
718 719 720 721 722 723 724
    m_versions.replace(m_versions.indexOf(current), version);
    delete current;

    // Update ui
    userChangedCurrentVersion();
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
    item->setText(0, version->displayName());
725
    item->setText(1, version->qmakeCommand().toUserOutput());
dt_'s avatar
dt_ committed
726
    item->setData(0, VersionIdRole, version->uniqueId());
727
    item->setData(0, ToolChainIdRole, defaultToolChainId(version));
dt_'s avatar
dt_ committed
728 729 730
    item->setIcon(0, version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
}

731
void QtOptionsPageWidget::updateDebuggingHelperUi()
732
{
dt's avatar
dt committed
733
    BaseQtVersion *version = currentVersion();
734
    const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
735

hjk's avatar
hjk committed
736
    QList<ToolChain*> toolchains = toolChains(currentVersion());
737 738

    if (!version || !version->isValid() || toolchains.isEmpty()) {
739
        m_ui->debuggingHelperWidget->setVisible(false);
740
    } else {
741 742 743 744 745
        const DebuggingHelperBuildTask::Tools availableTools = DebuggingHelperBuildTask::availableTools(version);
        const bool canBuildGdbHelper = availableTools & DebuggingHelperBuildTask::GdbDebugging;
        const bool canBuildQmlDumper = availableTools & DebuggingHelperBuildTask::QmlDump;
        const bool canBuildQmlDebuggingLib = availableTools & DebuggingHelperBuildTask::QmlDebugging;
        const bool canBuildQmlObserver = availableTools & DebuggingHelperBuildTask::QmlObserver;
746

747 748
        const bool hasGdbHelper = !version->gdbDebuggingHelperLibrary().isEmpty();
        const bool hasQmlDumper = version->hasQmlDump();
749
        const bool needsQmlDumper = version->needsQmlDump();
750
        const bool hasQmlDebuggingLib = version->hasQmlDebuggingLibrary();
751
        const bool needsQmlDebuggingLib = version->needsQmlDebuggingLibrary();
752
        const bool hasQmlObserver = !version->qmlObserverTool().isEmpty();
753

754 755
        bool isBuildingGdbHelper = false;
        bool isBuildingQmlDumper = false;
756
        bool isBuildingQmlDebuggingLib = false;
757 758 759 760 761 762 763
        bool isBuildingQmlObserver = false;

        if (currentItem) {
            DebuggingHelperBuildTask::Tools buildingTools
                    = currentItem->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
            isBuildingGdbHelper = buildingTools & DebuggingHelperBuildTask::GdbDebugging;
            isBuildingQmlDumper = buildingTools & DebuggingHelperBuildTask::QmlDump;
764
            isBuildingQmlDebuggingLib = buildingTools & DebuggingHelperBuildTask::QmlDebugging;
765 766 767
            isBuildingQmlObserver = buildingTools & DebuggingHelperBuildTask::QmlObserver;
        }

768 769
        // get names of tools from labels
        QStringList helperNames;
770
        const QChar colon = QLatin1Char(':');
771
        if (hasGdbHelper)
772
            helperNames << m_debuggingHelperUi->gdbHelperLabel->text().remove(colon);
773
        if (hasQmlDumper)
774
            helperNames << m_debuggingHelperUi->qmlDumpLabel->text().remove(colon);
775
        if (hasQmlDebuggingLib)
776
            helperNames << m_debuggingHelperUi->qmlDebuggingLibLabel->text().remove(colon);
777
        if (hasQmlObserver)
778
            helperNames << m_debuggingHelperUi->qmlObserverLabel->text().remove(colon);
779 780 781

        QString status;
        if (helperNames.isEmpty()) {
782
            status = tr("Helpers: None available");
783
        } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
784
            //: %1 is list of tool names.
785
            status = tr("Helpers: %1.").arg(helperNames.join(QLatin1String(", ")));
786 787 788 789
        }

        m_ui->debuggingHelperWidget->setSummaryText(status);

790 791
        QString gdbHelperText;
        Qt::TextInteractionFlags gdbHelperTextFlags = Qt::NoTextInteraction;
792
        if (hasGdbHelper) {
793
            gdbHelperText = QDir::toNativeSeparators(version->gdbDebuggingHelperLibrary());
794
            gdbHelperTextFlags = Qt::TextSelectableByMouse;
795
        } else {
796
            if (canBuildGdbHelper)
797
                gdbHelperText =  tr("<i>Not yet built.</i>");
798
            else
799
                gdbHelperText =  tr("<i>Not needed.</i>");
800
        }
801 802
        m_debuggingHelperUi->gdbHelperStatus->setText(gdbHelperText);
        m_debuggingHelperUi->gdbHelperStatus->setTextInteractionFlags(gdbHelperTextFlags);
803
        m_debuggingHelperUi->gdbHelperBuildButton->setEnabled(canBuildGdbHelper && !isBuildingGdbHelper);
804

805
        QString qmlDumpStatusText, qmlDumpStatusToolTip;
806
        Qt::TextInteractionFlags qmlDumpStatusTextFlags = Qt::NoTextInteraction;
807
        if (hasQmlDumper) {
808 809
            qmlDumpStatusText = QDir::toNativeSeparators(version->qmlDumpTool(false));
            const QString debugQmlDumpPath = QDir::toNativeSeparators(version->qmlDumpTool(true));
810
            if (qmlDumpStatusText != debugQmlDumpPath) {
811 812
                if (!qmlDumpStatusText.isEmpty()
                        && !debugQmlDumpPath.isEmpty())
813 814 815
                    qmlDumpStatusText += QLatin1String("\n");
                qmlDumpStatusText += debugQmlDumpPath;
            }
816
            qmlDumpStatusTextFlags = Qt::TextSelectableByMouse;
817
        } else {
818 819 820
            if (!needsQmlDumper) {
                qmlDumpStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDumper) {
821 822 823
                qmlDumpStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDumpStatusText = tr("<i>Cannot be compiled.</i>");
824
                QmlDumpTool::canBuild(version, &qmlDumpStatusToolTip);
825
            }
826
        }
827
        m_debuggingHelperUi->qmlDumpStatus->setText(qmlDumpStatusText);
828
        m_debuggingHelperUi->qmlDumpStatus->setTextInteractionFlags(qmlDumpStatusTextFlags);
829
        m_debuggingHelperUi->qmlDumpStatus->setToolTip(qmlDumpStatusToolTip);
830
        m_debuggingHelperUi->qmlDumpBuildButton->setEnabled(canBuildQmlDumper & !isBuildingQmlDumper);
831

832
        QString qmlDebuggingLibStatusText, qmlDebuggingLibToolTip;
833 834 835 836 837 838 839 840 841 842
        Qt::TextInteractionFlags qmlDebuggingLibStatusTextFlags = Qt::NoTextInteraction;
        if (hasQmlDebuggingLib) {
            qmlDebuggingLibStatusText = QDir::toNativeSeparators(
                        version->qmlDebuggingHelperLibrary(false));
            const QString debugPath = QDir::toNativeSeparators(
                        version->qmlDebuggingHelperLibrary(true));

            if (qmlDebuggingLibStatusText != debugPath) {
                if (!qmlDebuggingLibStatusText.isEmpty()
                        && !debugPath.isEmpty()) {
843
                    qmlDebuggingLibStatusText += QLatin1Char('\n');
844 845 846 847
                }
                qmlDebuggingLibStatusText += debugPath;
            }
            qmlDebuggingLibStatusTextFlags = Qt::TextSelectableByMouse;
848 849 850 851
        } else {
            if (!needsQmlDebuggingLib) {
                qmlDebuggingLibStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDebuggingLib) {
852 853 854
                qmlDebuggingLibStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDebuggingLibStatusText = tr("<i>Cannot be compiled.</i>");
855
                QmlDebuggingLibrary::canBuild(version, &qmlDebuggingLibToolTip);
856 857 858 859
            }
        }
        m_debuggingHelperUi->qmlDebuggingLibStatus->setText(qmlDebuggingLibStatusText);
        m_debuggingHelperUi->qmlDebuggingLibStatus->setTextInteractionFlags(qmlDebuggingLibStatusTextFlags);
860
        m_debuggingHelperUi->qmlDebuggingLibStatus->setToolTip(qmlDebuggingLibToolTip);
861 862
        m_debuggingHelperUi->qmlDebuggingLibBuildButton->setEnabled(needsQmlDebuggingLib
                                                                    && canBuildQmlDebuggingLib
863 864
                                                                    && !isBuildingQmlDebuggingLib);

865
        QString qmlObserverStatusText, qmlObserverToolTip;
866
        Qt::TextInteractionFlags qmlObserverStatusTextFlags = Qt::NoTextInteraction;
867
        if (hasQmlObserver) {
868
            qmlObserverStatusText = QDir::toNativeSeparators(version->qmlObserverTool());
869
            qmlObserverStatusTextFlags = Qt::TextSelectableByMouse;
870
        }  else {
871 872 873
            if (!needsQmlDebuggingLib) {
                qmlObserverStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlObserver) {
874 875 876
                qmlObserverStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlObserverStatusText = tr("<i>Cannot be compiled.</i>");
877
                QmlObserverTool::canBuild(version,