qtoptionspage.cpp 42.4 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
hjk's avatar
hjk committed
3 4
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** 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 31 32
#include "qtoptionspage.h"
#include "ui_showbuildlog.h"
#include "ui_qtversionmanager.h"
33 34
#include "ui_qtversioninfo.h"
#include "ui_debugginghelper.h"
35
#include "qtsupportconstants.h"
36
#include "qtversionmanager.h"
dt's avatar
dt committed
37
#include "qtversionfactory.h"
38 39 40
#include "qmldumptool.h"
#include "qmldebugginglibrary.h"
#include "qmlobservertool.h"
41 42 43

#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
44
#include <utils/treewidgetcolumnstretcher.h>
45
#include <utils/qtcassert.h>
dt's avatar
dt committed
46
#include <utils/buildablehelperlibrary.h>
47
#include <utils/pathchooser.h>
48
#include <projectexplorer/toolchainmanager.h>
49
#include <projectexplorer/toolchain.h>
50
#include <projectexplorer/projectexplorerconstants.h>
51
#include <utils/hostosinfo.h>
52
#include <utils/runextensions.h>
53

54 55 56 57
#include <QDir>
#include <QToolTip>
#include <QMessageBox>
#include <QFileDialog>
58 59
#include <QTextBrowser>
#include <QDesktopServices>
60

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

63 64
using namespace QtSupport;
using namespace QtSupport::Internal;
65
using namespace Utils;
66

67 68 69 70 71
///
// QtOptionsPage
///

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

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

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

96
    m_widget->apply();
97 98
}

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

104
//-----------------------------------------------------
105 106


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

124 125 126
    QWidget *debuggingHelperDetailsWidget = new QWidget();
    m_debuggingHelperUi->setupUi(debuggingHelperDetailsWidget);

127
    m_ui->setupUi(this);
128

129 130 131 132
    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);
133
    connect(m_ui->infoWidget, SIGNAL(expanded(bool)),
134
            this, SLOT(setInfoWidgetVisibility()));
135

136 137 138 139
    m_ui->versionInfoWidget->setWidget(versionInfoWidget);
    m_ui->versionInfoWidget->setState(Utils::DetailsWidget::NoSummary);

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

143 144
    // setup parent items for auto-detected and manual versions
    m_ui->qtdirList->header()->setResizeMode(QHeaderView::ResizeToContents);
dt_'s avatar
dt_ committed
145 146
    m_ui->qtdirList->header()->setStretchLastSection(false);
    m_ui->qtdirList->setTextElideMode(Qt::ElideNone);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    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;
    QList<BaseQtVersion *> versions = QtVersionManager::instance()->versions();
    foreach (BaseQtVersion *v, versions)
        additions.append(v->uniqueId());

    updateQtVersions(additions, QList<int>(), QList<int>());
162 163

    m_ui->qtdirList->expandAll();
164

Robert Loehning's avatar
Robert Loehning committed
165
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(QString)),
166 167
            this, SLOT(updateCurrentQtName()));

dt_'s avatar
dt_ committed
168 169 170
    connect(m_versionUi->editPathPushButton, SIGNAL(clicked()),
            this, SLOT(editPath()));

171 172 173 174 175
    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
176 177
    connect(m_ui->qtdirList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
            this, SLOT(versionChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
178

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

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
191
            this, SLOT(slotShowDebuggingBuildLog()));
192 193
    connect(m_debuggingHelperUi->toolChainComboBox, SIGNAL(activated(int)),
            this, SLOT(selectedToolChainChanged(int)));
194

195
    connect(m_ui->cleanUpButton, SIGNAL(clicked()), this, SLOT(cleanUpQtVersions()));
dt's avatar
dt committed
196 197
    userChangedCurrentVersion();
    updateCleanUpButton();
198

199 200
    connect(QtVersionManager::instance(), SIGNAL(dumpUpdatedFor(Utils::FileName)),
            this, SLOT(qtVersionsDumpUpdated(Utils::FileName)));
201

202 203 204
    connect(QtVersionManager::instance(), SIGNAL(qtVersionsChanged(QList<int>,QList<int>,QList<int>)),
            this, SLOT(updateQtVersions(QList<int>,QList<int>,QList<int>)));

205 206
    connect(ProjectExplorer::ToolChainManager::instance(), SIGNAL(toolChainsChanged()),
            this, SLOT(toolChainsUpdated()));
207 208
}

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

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

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

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

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

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

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

261 262
    if (!success)
        showDebuggingBuildLog(item);
263 264

    updateDebuggingHelperUi();
265 266
}

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

    if (toRemove.isEmpty())
        return;

Friedemann Kleint's avatar
Friedemann Kleint committed
278
    if (QMessageBox::warning(0, tr("Remove Invalid Qt Versions"),
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
                             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
294
    updateCleanUpButton();
295 296
}

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

311 312 313 314 315 316 317 318 319 320 321 322 323 324
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);
}

325
void QtOptionsPageWidget::qtVersionsDumpUpdated(const Utils::FileName &qmakeCommand)
326 327 328 329 330
{
    foreach (BaseQtVersion *version, m_versions) {
        if (version->qmakeCommand() == qmakeCommand)
            version->recheckDumper();
    }
331 332
    if (currentVersion()
            && currentVersion()->qmakeCommand() == qmakeCommand) {
333 334 335 336 337 338
        updateWidgets();
        updateDescriptionLabel();
        updateDebuggingHelperUi();
    }
}

339
void QtOptionsPageWidget::setInfoWidgetVisibility()
340
{
341 342 343 344
    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);
345 346
}

347 348 349 350 351
void QtOptionsPageWidget::infoAnchorClicked(const QUrl &url)
{
    QDesktopServices::openUrl(url);
}

352 353 354 355 356 357 358 359
QtOptionsPageWidget::ValidityInfo QtOptionsPageWidget::validInformation(const BaseQtVersion *version)
{
    ValidityInfo info;
    info.icon = m_validVersionIcon;

    if (!version)
        return info;

360
    info.description = tr("Qt version %1 for %2").arg(version->qtVersionString(), version->description());
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    if (!version->isValid()) {
        info.icon = m_invalidVersionIcon;
        info.message = version->invalidReason();
        return info;
    }

    // Do we have tool chain issues?
    QStringList missingToolChains;
    int abiCount = 0;
    foreach (const ProjectExplorer::Abi &a, version->qtAbis()) {
        if (ProjectExplorer::ToolChainManager::instance()->findToolChains(a).isEmpty())
            missingToolChains.append(a.toString());
        ++abiCount;
    }

376
    bool useable = true;
377
    QStringList warnings;
378 379 380
    if (!missingToolChains.isEmpty()) {
        if (missingToolChains.count() == abiCount) {
            // Yes, this Qt version can't be used at all!
381
            info.message = tr("No compiler can produce code for this Qt version. Please define one or more compilers.");
382 383 384 385
            info.icon = m_invalidVersionIcon;
            useable = false;
        } else {
            // Yes, some ABIs are unsupported
386
            warnings << tr("Not all possible target environments can be supported due to missing compilers.");
387 388 389 390
            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;
        }
391
    }
392 393

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

401 402 403
    return info;
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
QList<ProjectExplorer::ToolChain*> QtOptionsPageWidget::toolChains(const BaseQtVersion *version)
{
    QHash<QString,ProjectExplorer::ToolChain*> toolChains;
    if (!version)
        return toolChains.values();

    foreach (const ProjectExplorer::Abi &a, version->qtAbis()) {
        foreach (ProjectExplorer::ToolChain *tc,
                 ProjectExplorer::ToolChainManager::instance()->findToolChains(a)) {
            toolChains.insert(tc->id(), tc);
        }
    }

    return toolChains.values();
}

420 421 422 423 424 425 426 427
QString QtOptionsPageWidget::defaultToolChainId(const BaseQtVersion *version)
{
    QList<ProjectExplorer::ToolChain*> possibleToolChains = toolChains(version);
    if (!possibleToolChains.isEmpty())
        return possibleToolChains.first()->id();
    return QString();
}

428
void QtOptionsPageWidget::buildDebuggingHelper(DebuggingHelperBuildTask::Tools tools)
429 430 431 432 433
{
    const int index = currentIndex();
    if (index < 0)
        return;

434 435 436
    // remove tools that cannot be build
    tools &= DebuggingHelperBuildTask::availableTools(currentVersion());

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

440 441 442 443
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags |= tools;
    item->setData(0, BuildRunningRole, QVariant::fromValue(buildFlags));
444

dt's avatar
dt committed
445
    BaseQtVersion *version = m_versions.at(index);
446 447 448
    if (!version)
        return;

449
    updateDebuggingHelperUi();
450

451
    // Run a debugging helper build task in the background.
452 453 454 455 456 457 458 459
    QString toolChainId = m_debuggingHelperUi->toolChainComboBox->itemData(
                m_debuggingHelperUi->toolChainComboBox->currentIndex()).toString();
    ProjectExplorer::ToolChainManager *tcMgr = ProjectExplorer::ToolChainManager::instance();
    ProjectExplorer::ToolChain *toolChain = tcMgr->findToolChain(toolChainId);
    if (!toolChain)
        return;

    DebuggingHelperBuildTask *buildTask = new DebuggingHelperBuildTask(version, toolChain, tools);
460 461
    // Don't open General Messages pane with errors
    buildTask->showOutputOnError(false);
462 463
    connect(buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
            this, SLOT(debuggingHelperBuildFinished(int,QString,DebuggingHelperBuildTask::Tools)),
464
            Qt::QueuedConnection);
465
    QFuture<void> task = QtConcurrent::run(&DebuggingHelperBuildTask::run, buildTask);
466
    const QString taskName = tr("Building helpers");
467

hjk's avatar
hjk committed
468
    Core::ICore::progressManager()->addTask(task, taskName,
469
                                                        QLatin1String("Qt4ProjectManager::BuildHelpers"));
470
}
471 472 473 474 475 476 477 478 479 480
void QtOptionsPageWidget::buildGdbHelper()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::GdbDebugging);
}

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

481 482 483 484 485
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

486 487
void QtOptionsPageWidget::buildQmlObserver()
{
488
    DebuggingHelperBuildTask::Tools qmlDbgTools =
Kai Koehne's avatar
Kai Koehne committed
489 490
            DebuggingHelperBuildTask::QmlObserver;
    qmlDbgTools |= DebuggingHelperBuildTask::QmlDebugging;
491
    buildDebuggingHelper(qmlDbgTools);
492
}
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
// 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)
511
{
512 513 514 515
    m_ui.log->setPlainText(text); // Show and scroll to bottom
    m_ui.log->moveCursor(QTextCursor::End);
    m_ui.log->ensureCursorVisible();
}
516

517 518 519 520 521 522 523 524 525
void QtOptionsPageWidget::slotShowDebuggingBuildLog()
{
    if (const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        showDebuggingBuildLog(currentItem);
}

void QtOptionsPageWidget::showDebuggingBuildLog(const QTreeWidgetItem *currentItem)
{
    const int currentItemIndex = indexForTreeItem(currentItem);
526 527
    if (currentItemIndex < 0)
        return;
528
    BuildLogDialog *dialog = new BuildLogDialog(this->window());
529
    dialog->setWindowTitle(tr("Debugging Helper Build Log for '%1'").arg(currentItem->text(0)));
530
    dialog->setText(currentItem->data(0, BuildLogRole).toString());
531
    dialog->show();
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 564 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 591 592 593 594 595 596 597 598 599 600
void QtOptionsPageWidget::updateQtVersions(const QList<int> &additions, const QList<int> &removals,
                                           const QList<int> &changes)
{
    QtVersionManager *vm = QtVersionManager::instance();

    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) {
        BaseQtVersion *version = vm->version(a)->clone();
        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);
    }
}

601
QtOptionsPageWidget::~QtOptionsPageWidget()
602 603
{
    delete m_ui;
dt's avatar
dt committed
604 605
    delete m_versionUi;
    delete m_debuggingHelperUi;
dt's avatar
dt committed
606
    delete m_configurationWidget;
607
    qDeleteAll(m_versions);
608 609
}

dt_'s avatar
dt_ committed
610
static QString filterForQmakeFileDialog()
611
{
612
    QString filter = QLatin1String("qmake (");
613 614 615 616
    const QStringList commands = Utils::BuildableHelperLibrary::possibleQMakeCommands();
    for (int i = 0; i < commands.size(); ++i) {
        if (i)
            filter += QLatin1Char(' ');
617 618 619
        if (Utils::HostOsInfo::isMacHost())
            // work around QTBUG-7739 that prohibits filters that don't start with *
            filter += QLatin1Char('*');
620
        filter += commands.at(i);
621 622 623 624
        if (Utils::HostOsInfo::isAnyUnixHost() && !Utils::HostOsInfo::isMacHost())
            // kde bug, we need at least one wildcard character
            // see QTCREATORBUG-7771
            filter += QLatin1Char('*');
dt's avatar
dt committed
625
    }
626
    filter += QLatin1Char(')');
dt_'s avatar
dt_ committed
627 628
    return filter;
}
629

dt_'s avatar
dt_ committed
630 631
void QtOptionsPageWidget::addQtDir()
{
632
    Utils::FileName qtVersion = Utils::FileName::fromString(
Tobias Hunger's avatar
Tobias Hunger committed
633 634 635 636 637 638
                QFileDialog::getOpenFileName(this,
                                             tr("Select a qmake executable"),
                                             QString(),
                                             filterForQmakeFileDialog(),
                                             0,
                                             QFileDialog::DontResolveSymlinks));
dt's avatar
dt committed
639 640
    if (qtVersion.isNull())
        return;
641 642 643 644 645 646 647
    BaseQtVersion *version = 0;
    foreach (BaseQtVersion *v, m_versions) {
        if (v->qmakeCommand() == qtVersion) {
            version = v;
            break;
        }
    }
648
    if (version) {
dt's avatar
dt committed
649
        // Already exist
Tobias Hunger's avatar
Tobias Hunger committed
650
        QMessageBox::warning(this, tr("Qt known"),
651 652 653
                             tr("This Qt version was already registered as \"%1\".")
                             .arg(version->displayName()));
        return;
dt's avatar
dt committed
654
    }
655

656 657
    QString error;
    version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion, false, QString(), &error);
dt's avatar
dt committed
658 659
    if (version) {
        m_versions.append(version);
660

dt's avatar
dt committed
661 662
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList->topLevelItem(1));
        item->setText(0, version->displayName());
663
        item->setText(1, version->qmakeCommand().toUserOutput());
dt's avatar
dt committed
664
        item->setData(0, VersionIdRole, version->uniqueId());
665
        item->setData(0, ToolChainIdRole, defaultToolChainId(version));
dt's avatar
dt committed
666 667 668 669
        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();
670 671 672 673
    } else {
        QMessageBox::warning(this, tr("Qmake not executable"),
                             tr("The qmake %1 could not be added: %2").arg(qtVersion.toUserOutput()).arg(error));
        return;
dt's avatar
dt committed
674 675
    }
    updateCleanUpButton();
676 677
}

678
void QtOptionsPageWidget::removeQtDir()
679 680
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
681
    int index = indexForTreeItem(item);
682 683 684 685 686
    if (index < 0)
        return;

    delete item;

dt's avatar
dt committed
687
    BaseQtVersion *version = m_versions.at(index);
688
    m_versions.removeAt(index);
689
    delete version;
dt's avatar
dt committed
690
    updateCleanUpButton();
691 692
}

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

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

735
void QtOptionsPageWidget::updateDebuggingHelperUi()
736
{
dt's avatar
dt committed
737
    BaseQtVersion *version = currentVersion();
738
    const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
739

740 741 742
    QList<ProjectExplorer::ToolChain*> toolchains = toolChains(currentVersion());

    if (!version || !version->isValid() || toolchains.isEmpty()) {
743
        m_ui->debuggingHelperWidget->setVisible(false);
744
    } else {
745 746 747 748 749
        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;
750

751 752
        const bool hasGdbHelper = !version->gdbDebuggingHelperLibrary().isEmpty();
        const bool hasQmlDumper = version->hasQmlDump();
753
        const bool needsQmlDumper = version->needsQmlDump();
754
        const bool hasQmlDebuggingLib = version->hasQmlDebuggingLibrary();
755
        const bool needsQmlDebuggingLib = version->needsQmlDebuggingLibrary();
756
        const bool hasQmlObserver = !version->qmlObserverTool().isEmpty();
757

758 759
        bool isBuildingGdbHelper = false;
        bool isBuildingQmlDumper = false;
760
        bool isBuildingQmlDebuggingLib = false;
761 762 763 764 765 766 767
        bool isBuildingQmlObserver = false;

        if (currentItem) {
            DebuggingHelperBuildTask::Tools buildingTools
                    = currentItem->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
            isBuildingGdbHelper = buildingTools & DebuggingHelperBuildTask::GdbDebugging;
            isBuildingQmlDumper = buildingTools & DebuggingHelperBuildTask::QmlDump;
768
            isBuildingQmlDebuggingLib = buildingTools & DebuggingHelperBuildTask::QmlDebugging;
769 770 771
            isBuildingQmlObserver = buildingTools & DebuggingHelperBuildTask::QmlObserver;
        }

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

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

        m_ui->debuggingHelperWidget->setSummaryText(status);

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

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

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

869
        QString qmlObserverStatusText, qmlObserverToolTip;
870
        Qt::TextInteractionFlags qmlObserverStatusTextFlags = Qt::NoTextInteraction;
871
        if (hasQmlObserver) {
872
            qmlObserverStatusText = QDir::toNativeSeparators(version->qmlObserverTool());
873
            qmlObserverStatusTextFlags = Qt::TextSelectableByMouse;
874
        }  else {
875 876 877
            if (!needsQmlDebuggingLib) {
                qmlObserverStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlObserver) {
878 879 880
                qmlObserverStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlObserverStatusText = tr("<i>Cannot be compiled.</i>");
881
                QmlObserverTool::canBuild(version, &qmlObserverToolTip);
882
            }
883
        }
884
        m_debuggingHelperUi->qmlObserverStatus->setText(qmlObserverStatusText);
885
        m_debuggingHelperUi->qmlObserverStatus->setTextInteractionFlags(qmlObserverStatusTextFlags);
886
        m_debuggingHelperUi->qmlObserverStatus->setToolTip(qmlObserverToolTip);
887 888
        m_debuggingHelperUi->qmlObserverBuildButton->setEnabled(canBuildQmlObserver
                                                                & !isBuildingQmlObserver);
889

890
        QList<ProjectExplorer::ToolChain*> toolchains = toolChains(currentVersion());
891
        QString selectedToolChainId = currentItem->data(0, ToolChainIdRole).toString();
892 893 894 895 896 897 898 899 900 901 902 903
        m_debuggingHelperUi->toolChainComboBox->clear();
        for (int i = 0; i < toolchains.size(); ++i) {
            if (!toolchains.at(i)->isValid())
                continue;
            if (i >= m_debuggingHelperUi->toolChainComboBox->count()) {
                m_debuggingHelperUi->toolChainComboBox->insertItem(i, toolchains.at(i)->displayName(),
                                                                   toolchains.at(i)->id());
            }
            if (toolchains.at(i)->id() == selectedToolChainId)
                m_debuggingHelperUi->toolChainComboBox->setCurrentIndex(i);
        }

904 905 906
        const bool hasLog = currentItem && !currentItem->data(0, BuildLogRole).toString().isEmpty();
        m_debuggingHelperUi->showLogButton->setEnabled(hasLog);

907 908 909 910 911 912 913 914 915 916 917
        const bool canBuild = canBuildGdbHelper
                               || canBuildQmlDumper
                               || (canBuildQmlDebuggingLib && needsQmlDebuggingLib)
                               || canBuildQmlObserver;
        const bool isBuilding = isBuildingGdbHelper
                                 || isBuildingQmlDumper
                                 || isBuildingQmlDebuggingLib
                                 || isBuildingQmlObserver;

        m_debuggingHelperUi->rebuildButton->setEnabled(canBuild && !isBuilding);
        m_debuggingHelperUi->toolChainComboBox->setEnabled(canBuild && !isBuilding);
918
        setInfoWidgetVisibility();