qtoptionspage.cpp 37.1 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
con's avatar
con committed
5
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
Tobias Hunger's avatar
Tobias Hunger committed
6
**
hjk's avatar
hjk committed
7
** Contact: Nokia Corporation (info@qt.nokia.com)
Tobias Hunger's avatar
Tobias Hunger committed
8 9 10 11
**
**
** GNU Lesser General Public License Usage
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
Tobias Hunger's avatar
Tobias Hunger committed
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
Tobias Hunger's avatar
Tobias Hunger committed
29
** Nokia at info@qt.nokia.com.
Tobias Hunger's avatar
Tobias Hunger committed
30 31 32
**
**************************************************************************/

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

#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
47
#include <utils/treewidgetcolumnstretcher.h>
48
#include <utils/qtcassert.h>
dt's avatar
dt committed
49
#include <utils/buildablehelperlibrary.h>
50
#include <utils/pathchooser.h>
51
#include <projectexplorer/toolchainmanager.h>
52
#include <projectexplorer/toolchain.h>
53
#include <qtconcurrent/runextensions.h>
54 55

#include <QtCore/QDir>
56
#include <QtGui/QToolTip>
57
#include <QtGui/QMessageBox>
dt's avatar
dt committed
58 59
#include <QtGui/QFileDialog>
#include <QtGui/QMainWindow>
60

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

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

66 67 68 69 70
///
// QtOptionsPage
///

QtOptionsPage::QtOptionsPage()
dt's avatar
dt committed
71
    : m_widget(0)
72 73 74 75 76
{
}

QString QtOptionsPage::id() const
{
77
    return QLatin1String(Constants::QTVERSION_SETTINGS_PAGE_ID);
78 79
}

80
QString QtOptionsPage::displayName() const
81
{
82
    return QCoreApplication::translate("Qt4ProjectManager", Constants::QTVERSION_SETTINGS_PAGE_NAME);
83 84 85 86
}

QString QtOptionsPage::category() const
{
87
    return QLatin1String(Constants::QT_SETTINGS_CATEGORY);
88 89
}

90
QString QtOptionsPage::displayCategory() const
91
{
92
    return QCoreApplication::translate("Qt4ProjectManager", Constants::QT_SETTINGS_TR_CATEGORY);
93 94
}

95 96 97 98 99
QIcon QtOptionsPage::categoryIcon() const
{
    return QIcon(QLatin1String(Constants::QT_SETTINGS_CATEGORY_ICON));
}

100 101 102
QWidget *QtOptionsPage::createPage(QWidget *parent)
{
    QtVersionManager *vm = QtVersionManager::instance();
Tobias Hunger's avatar
Tobias Hunger committed
103
    m_widget = new QtOptionsPageWidget(parent, vm->versions());
104 105
    if (m_searchKeywords.isEmpty())
        m_searchKeywords = m_widget->searchKeywords();
106 107 108 109 110
    return m_widget;
}

void QtOptionsPage::apply()
{
111 112
    if (!m_widget) // page was never shown
        return;
113 114 115
    m_widget->finish();

    QtVersionManager *vm = QtVersionManager::instance();
116
    vm->setNewQtVersions(m_widget->versions());
117 118
}

119 120 121 122 123
bool QtOptionsPage::matches(const QString &s) const
{
    return m_searchKeywords.contains(s, Qt::CaseInsensitive);
}

124
//-----------------------------------------------------
125 126


dt's avatar
dt committed
127
QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent, QList<BaseQtVersion *> versions)
128 129
    : QWidget(parent)
    , m_specifyNameString(tr("<specify a name>"))
130
    , m_ui(new Internal::Ui::QtVersionManager())
131 132
    , m_versionUi(new Internal::Ui::QtVersionInfo())
    , m_debuggingHelperUi(new Internal::Ui::DebuggingHelper())
133
    , m_invalidVersionIcon(":/projectexplorer/images/compile_error.png")
134
    , m_warningVersionIcon(":/projectexplorer/images/compile_warning.png")
dt's avatar
dt committed
135
    , m_configurationWidget(0)
136 137
{
    // Initialize m_versions
dt's avatar
dt committed
138
    foreach (BaseQtVersion *version, versions)
dt's avatar
dt committed
139
        m_versions.push_back(version->clone());
140

141 142
    QWidget *versionInfoWidget = new QWidget();
    m_versionUi->setupUi(versionInfoWidget);
143
    m_versionUi->editPathPushButton->setText(tr(Utils::PathChooser::browseButtonLabel));
144

145 146 147
    QWidget *debuggingHelperDetailsWidget = new QWidget();
    m_debuggingHelperUi->setupUi(debuggingHelperDetailsWidget);

148
    m_ui->setupUi(this);
149 150 151 152 153

    m_ui->versionInfoWidget->setWidget(versionInfoWidget);
    m_ui->versionInfoWidget->setState(Utils::DetailsWidget::NoSummary);

    m_ui->debuggingHelperWidget->setWidget(debuggingHelperDetailsWidget);
154

155 156
    // setup parent items for auto-detected and manual versions
    m_ui->qtdirList->header()->setResizeMode(QHeaderView::ResizeToContents);
dt_'s avatar
dt_ committed
157 158
    m_ui->qtdirList->header()->setStretchLastSection(false);
    m_ui->qtdirList->setTextElideMode(Qt::ElideNone);
159
    QTreeWidgetItem *autoItem = new QTreeWidgetItem(m_ui->qtdirList);
160
    m_ui->qtdirList->installEventFilter(this);
161 162
    autoItem->setText(0, tr("Auto-detected"));
    autoItem->setFirstColumnSpanned(true);
163
    autoItem->setFlags(Qt::ItemIsEnabled);
164 165 166
    QTreeWidgetItem *manualItem = new QTreeWidgetItem(m_ui->qtdirList);
    manualItem->setText(0, tr("Manual"));
    manualItem->setFirstColumnSpanned(true);
167
    manualItem->setFlags(Qt::ItemIsEnabled);
168

169
    for (int i = 0; i < m_versions.count(); ++i) {
dt's avatar
dt committed
170
        BaseQtVersion *version = m_versions.at(i);
171
        QTreeWidgetItem *item = new QTreeWidgetItem(version->isAutodetected()? autoItem : manualItem);
172
        item->setText(0, version->displayName());
173
        item->setText(1, QDir::toNativeSeparators(version->qmakeCommand()));
174
        item->setData(0, VersionIdRole, version->uniqueId());
175 176
        const ValidityInfo info = validInformation(version);
        item->setIcon(0, info.icon);
177
    }
178
    m_ui->qtdirList->expandAll();
179

180
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(const QString &)),
181 182
            this, SLOT(updateCurrentQtName()));

dt_'s avatar
dt_ committed
183 184 185
    connect(m_versionUi->editPathPushButton, SIGNAL(clicked()),
            this, SLOT(editPath()));

186 187 188 189 190 191 192 193
    connect(m_ui->addButton, SIGNAL(clicked()),
            this, SLOT(addQtDir()));
    connect(m_ui->delButton, SIGNAL(clicked()),
            this, SLOT(removeQtDir()));

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

194
    connect(m_debuggingHelperUi->rebuildButton, SIGNAL(clicked()),
195
            this, SLOT(buildDebuggingHelper()));
196 197 198 199
    connect(m_debuggingHelperUi->gdbHelperBuildButton, SIGNAL(clicked()),
            this, SLOT(buildGdbHelper()));
    connect(m_debuggingHelperUi->qmlDumpBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDump()));
200 201
    connect(m_debuggingHelperUi->qmlDebuggingLibBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDebuggingLibrary()));
202 203 204 205
    connect(m_debuggingHelperUi->qmlObserverBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlObserver()));

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
206
            this, SLOT(slotShowDebuggingBuildLog()));
207

208
    connect(m_ui->cleanUpButton, SIGNAL(clicked()), this, SLOT(cleanUpQtVersions()));
dt's avatar
dt committed
209 210
    userChangedCurrentVersion();
    updateCleanUpButton();
211

212 213
    connect(QtVersionManager::instance(), SIGNAL(dumpUpdatedFor(QString)),
            this, SLOT(qtVersionsDumpUpdated(QString)));
214 215 216

    connect(ProjectExplorer::ToolChainManager::instance(), SIGNAL(toolChainsChanged()),
            this, SLOT(toolChainsUpdated()));
217 218
}

219 220 221 222 223
bool QtOptionsPageWidget::eventFilter(QObject *o, QEvent *e)
{
    // Set the items tooltip, which may cause costly initialization
    // of QtVersion and must be up-to-date
    if (o != m_ui->qtdirList || e->type() != QEvent::ToolTip)
224
        return false;
225 226 227 228 229 230 231 232
    QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
    const QPoint treePos = helpEvent->pos() - QPoint(0, m_ui->qtdirList->header()->height());
    QTreeWidgetItem *item = m_ui->qtdirList->itemAt(treePos);
    if (!item)
        return false;
    const int index = indexForTreeItem(item);
    if (index == -1)
        return false;
233
    const QString tooltip = m_versions.at(index)->toHtml(true);
234 235 236 237 238
    QToolTip::showText(helpEvent->globalPos(), tooltip, m_ui->qtdirList);
    helpEvent->accept();
    return true;
}

239
int QtOptionsPageWidget::currentIndex() const
240
{
241 242 243 244
    if (QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        return indexForTreeItem(currentItem);
    return -1;
}
245

dt's avatar
dt committed
246
BaseQtVersion *QtOptionsPageWidget::currentVersion() const
247 248 249
{
    const int currentItemIndex = currentIndex();
    if (currentItemIndex >= 0 && currentItemIndex < m_versions.size())
250
        return m_versions.at(currentItemIndex);
251 252
    return 0;
}
253

dt's avatar
dt committed
254
static inline int findVersionById(const QList<BaseQtVersion *> &l, int id)
255 256 257
{
    const int size = l.size();
    for (int i = 0; i < size; i++)
258
        if (l.at(i)->uniqueId() == id)
259 260 261
            return i;
    return -1;
}
262

263
// Update with results of terminated helper build
264
void QtOptionsPageWidget::debuggingHelperBuildFinished(int qtVersionId, const QString &output, DebuggingHelperBuildTask::Tools tools)
265
{
266
    const int index = findVersionById(m_versions, qtVersionId);
267 268
    if (index == -1)
        return; // Oops, somebody managed to delete the version
269

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

272 273
    // Update item view
    QTreeWidgetItem *item = treeItemForIndex(index);
274 275 276 277 278
    QTC_ASSERT(item, return);
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags &= ~tools;
    item->setData(0, BuildRunningRole,  QVariant::fromValue(buildFlags));
279
    item->setData(0, BuildLogRole, output);
280

281 282
    bool success = true;
    if (tools & DebuggingHelperBuildTask::GdbDebugging)
dt's avatar
dt committed
283
        success &= version->hasGdbDebuggingHelper();
284
    if (tools & DebuggingHelperBuildTask::QmlDebugging)
dt's avatar
dt committed
285
        success &= version->hasQmlDebuggingLibrary();
286
    if (tools & DebuggingHelperBuildTask::QmlDump)
dt's avatar
dt committed
287
        success &= version->hasQmlDump();
288
    if (tools & DebuggingHelperBuildTask::QmlObserver)
dt's avatar
dt committed
289
        success &= version->hasQmlObserver();
290

291 292
    if (!success)
        showDebuggingBuildLog(item);
293 294
}

295 296 297
void QtOptionsPageWidget::cleanUpQtVersions()
{
    QStringList toRemove;
dt's avatar
dt committed
298
    foreach (const BaseQtVersion *v, m_versions) {
299
        if (!v->isValid() && !v->isAutodetected())
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
            toRemove.append(v->displayName());
    }

    if (toRemove.isEmpty())
        return;

    if (QMessageBox::warning(0, tr("Remove invalid Qt Versions"),
                             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
322
    updateCleanUpButton();
323 324
}

325 326 327 328 329 330 331 332 333 334 335 336 337
void QtOptionsPageWidget::toolChainsUpdated()
{
    for (int i = 0; i < m_versions.count(); ++i) {
        QTreeWidgetItem *item = treeItemForIndex(i);
        if (item == m_ui->qtdirList->currentItem())
            updateDescriptionLabel();
        else {
            const ValidityInfo info = validInformation(m_versions.at(i));
            item->setIcon(0, info.icon);
        }
    }
}

338
void QtOptionsPageWidget::qtVersionsDumpUpdated(const QString &qmakeCommand)
339 340 341 342 343
{
    foreach (BaseQtVersion *version, m_versions) {
        if (version->qmakeCommand() == qmakeCommand)
            version->recheckDumper();
    }
344 345
    if (currentVersion()
            && currentVersion()->qmakeCommand() == qmakeCommand) {
346 347 348 349 350 351
        updateWidgets();
        updateDescriptionLabel();
        updateDebuggingHelperUi();
    }
}

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
QtOptionsPageWidget::ValidityInfo QtOptionsPageWidget::validInformation(const BaseQtVersion *version)
{
    ValidityInfo info;
    info.icon = m_validVersionIcon;

    if (!version)
        return info;

    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()) {
        // Ignore symbian emulator since we do not support it.
        if (a.osFlavor() == ProjectExplorer::Abi::SymbianEmulatorFlavor)
            continue;
        if (ProjectExplorer::ToolChainManager::instance()->findToolChains(a).isEmpty())
            missingToolChains.append(a.toString());
        ++abiCount;
    }

378
    bool useable = true;
379 380 381 382 383 384 385
    if (missingToolChains.isEmpty()) {
        // No:
        info.message = tr("Qt version %1 for %2").arg(version->qtVersionString(), version->description());
    } else if (missingToolChains.count() == abiCount) {
        // Yes, this Qt version can't be used at all!
        info.message = tr("No tool chain can produce code for this Qt version. Please define one or more tool chains.");
        info.icon = m_invalidVersionIcon;
386
        useable = false;
387 388 389
    } else {
        // Yes, some ABIs are unsupported
        info.message = tr("Not all possible target environments can be supported due to missing tool chains.");
Jarek Kobus's avatar
Jarek Kobus committed
390
        info.toolTip = tr("The following ABIs are currently not supported:<ul><li>%1</li></ul>")
391 392 393
                       .arg(missingToolChains.join(QLatin1String("</li><li>")));
        info.icon = m_warningVersionIcon;
    }
394 395 396 397 398 399 400 401 402 403 404

    if (useable) {
        QString warning = version->warningReason();
        if (!warning.isEmpty()) {
            if (!info.message.isEmpty())
                info.message.append('\n');
            info.message += warning;
            info.icon = m_warningVersionIcon;
        }
    }

405 406 407
    return info;
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
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()) {
        // Ignore symbian emulator since we do not support it.
        if (a.osFlavor() == ProjectExplorer::Abi::SymbianEmulatorFlavor)
            continue;
        foreach (ProjectExplorer::ToolChain *tc,
                 ProjectExplorer::ToolChainManager::instance()->findToolChains(a)) {
            toolChains.insert(tc->id(), tc);
        }
    }

    return toolChains.values();
}

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

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

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

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

445
    updateDebuggingHelperUi();
446

447
    // Run a debugging helper build task in the background.
448 449 450 451 452 453 454 455
    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);
456 457
    // Don't open General Messages pane with errors
    buildTask->showOutputOnError(false);
458 459
    connect(buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
            this, SLOT(debuggingHelperBuildFinished(int,QString,DebuggingHelperBuildTask::Tools)),
460
            Qt::QueuedConnection);
461
    QFuture<void> task = QtConcurrent::run(&DebuggingHelperBuildTask::run, buildTask);
462
    const QString taskName = tr("Building helpers");
463

464
    Core::ICore::instance()->progressManager()->addTask(task, taskName,
465
                                                        QLatin1String("Qt4ProjectManager::BuildHelpers"));
466
}
467 468 469 470 471 472 473 474 475 476
void QtOptionsPageWidget::buildGdbHelper()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::GdbDebugging);
}

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

477 478 479 480 481
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

482 483
void QtOptionsPageWidget::buildQmlObserver()
{
484
    DebuggingHelperBuildTask::Tools qmlDbgTools =
Kai Koehne's avatar
Kai Koehne committed
485 486
            DebuggingHelperBuildTask::QmlObserver;
    qmlDbgTools |= DebuggingHelperBuildTask::QmlDebugging;
487
    buildDebuggingHelper(qmlDbgTools);
488
}
489

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

513 514 515 516 517 518 519 520 521
void QtOptionsPageWidget::slotShowDebuggingBuildLog()
{
    if (const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        showDebuggingBuildLog(currentItem);
}

void QtOptionsPageWidget::showDebuggingBuildLog(const QTreeWidgetItem *currentItem)
{
    const int currentItemIndex = indexForTreeItem(currentItem);
522 523
    if (currentItemIndex < 0)
        return;
524
    BuildLogDialog *dialog = new BuildLogDialog(this->window());
525
    dialog->setWindowTitle(tr("Debugging Helper Build Log for '%1'").arg(currentItem->text(0)));
526
    dialog->setText(currentItem->data(0, BuildLogRole).toString());
527
    dialog->show();
528 529
}

530
QtOptionsPageWidget::~QtOptionsPageWidget()
531 532
{
    delete m_ui;
dt's avatar
dt committed
533 534
    delete m_versionUi;
    delete m_debuggingHelperUi;
dt's avatar
dt committed
535
    delete m_configurationWidget;
536
    qDeleteAll(m_versions);
537 538
}

dt_'s avatar
dt_ committed
539
static QString filterForQmakeFileDialog()
540
{
dt's avatar
dt committed
541 542
    QString filter("qmake (");
    foreach (const QString &s, Utils::BuildableHelperLibrary::possibleQMakeCommands()) {
543 544 545 546 547
#ifdef Q_WS_MAC
        // work around QTBUG-7739 that prohibits filters that don't start with *
        filter += QLatin1Char('*');
#endif
        filter += s + QLatin1Char(' ');
dt's avatar
dt committed
548
    }
549
    filter += QLatin1Char(')');
dt_'s avatar
dt_ committed
550 551
    return filter;
}
552

dt_'s avatar
dt_ committed
553 554
void QtOptionsPageWidget::addQtDir()
{
dt's avatar
dt committed
555
    QString qtVersion = QFileDialog::getOpenFileName(this,
dt_'s avatar
dt_ committed
556 557
                                                     tr("Select a qmake executable"),
                                                     QString(), filterForQmakeFileDialog());
dt's avatar
dt committed
558 559 560 561 562
    if (qtVersion.isNull())
        return;
    if (QtVersionManager::instance()->qtVersionForQMakeBinary(qtVersion)) {
        // Already exist
    }
563

dt's avatar
dt committed
564 565 566
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
    if (version) {
        m_versions.append(version);
567

dt's avatar
dt committed
568 569 570 571 572 573 574 575 576 577
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList->topLevelItem(1));
        item->setText(0, version->displayName());
        item->setText(1, QDir::toNativeSeparators(version->qmakeCommand()));
        item->setData(0, VersionIdRole, version->uniqueId());
        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();
    }
    updateCleanUpButton();
578 579
}

580
void QtOptionsPageWidget::removeQtDir()
581 582
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
583
    int index = indexForTreeItem(item);
584 585 586 587 588
    if (index < 0)
        return;

    delete item;

dt's avatar
dt committed
589
    BaseQtVersion *version = m_versions.at(index);
590
    m_versions.removeAt(index);
591
    delete version;
dt's avatar
dt committed
592
    updateCleanUpButton();
593 594
}

dt_'s avatar
dt_ committed
595 596
void QtOptionsPageWidget::editPath()
{
597 598
    BaseQtVersion *current = currentVersion();
    QString dir = QFileInfo(currentVersion()->qmakeCommand()).absolutePath();
dt_'s avatar
dt_ committed
599 600
    QString qtVersion = QFileDialog::getOpenFileName(this,
                                                     tr("Select a qmake executable"),
601
                                                     dir, filterForQmakeFileDialog());
dt_'s avatar
dt_ committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615
    if (qtVersion.isNull())
        return;
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
    // Same type? then replace!
    if (current->type() != version->type()) {
        // not the same type, error out
        QMessageBox::critical(this, tr("Qt versions incompatible"),
                              tr("The qt version selected must be for the same target."),
                              QMessageBox::Ok);
        delete version;
        return;
    }
    // same type, replace
    version->setId(current->uniqueId());
dt_'s avatar
dt_ committed
616 617
    if (current->displayName() != current->defaultDisplayName(current->qtVersionString(), current->qmakeCommand()))
        version->setDisplayName(current->displayName());
dt_'s avatar
dt_ committed
618 619 620 621 622 623 624 625 626 627 628 629
    m_versions.replace(m_versions.indexOf(current), version);
    delete current;

    // Update ui
    userChangedCurrentVersion();
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
    item->setText(0, version->displayName());
    item->setText(1, QDir::toNativeSeparators(version->qmakeCommand()));
    item->setData(0, VersionIdRole, version->uniqueId());
    item->setIcon(0, version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
}

630
void QtOptionsPageWidget::updateDebuggingHelperUi()
631
{
dt's avatar
dt committed
632
    BaseQtVersion *version = currentVersion();
633
    const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
634

635 636 637
    QList<ProjectExplorer::ToolChain*> toolchains = toolChains(currentVersion());

    if (!version || !version->isValid() || toolchains.isEmpty()) {
638
        m_ui->debuggingHelperWidget->setVisible(false);
639
    } else {
640 641 642 643 644
        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;
645

646 647 648
        const bool hasGdbHelper = !version->gdbDebuggingHelperLibrary().isEmpty();
        const bool hasQmlDumper = version->hasQmlDump();
        const bool hasQmlDebuggingLib = version->hasQmlDebuggingLibrary();
649
        const bool needsQmlDebuggingLib = version->needsQmlDebuggingLibrary();
650
        const bool hasQmlObserver = !version->qmlObserverTool().isEmpty();
651

652 653
        bool isBuildingGdbHelper = false;
        bool isBuildingQmlDumper = false;
654
        bool isBuildingQmlDebuggingLib = false;
655 656 657 658 659 660 661
        bool isBuildingQmlObserver = false;

        if (currentItem) {
            DebuggingHelperBuildTask::Tools buildingTools
                    = currentItem->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
            isBuildingGdbHelper = buildingTools & DebuggingHelperBuildTask::GdbDebugging;
            isBuildingQmlDumper = buildingTools & DebuggingHelperBuildTask::QmlDump;
662
            isBuildingQmlDebuggingLib = buildingTools & DebuggingHelperBuildTask::QmlDebugging;
663 664 665
            isBuildingQmlObserver = buildingTools & DebuggingHelperBuildTask::QmlObserver;
        }

666 667 668 669 670 671
        // get names of tools from labels
        QStringList helperNames;
        if (hasGdbHelper)
            helperNames << m_debuggingHelperUi->gdbHelperLabel->text().remove(':');
        if (hasQmlDumper)
            helperNames << m_debuggingHelperUi->qmlDumpLabel->text().remove(':');
672 673
        if (hasQmlDebuggingLib)
            helperNames << m_debuggingHelperUi->qmlDebuggingLibLabel->text().remove(':');
674 675 676 677 678
        if (hasQmlObserver)
            helperNames << m_debuggingHelperUi->qmlObserverLabel->text().remove(':');

        QString status;
        if (helperNames.isEmpty()) {
679
            status = tr("Helpers: None available");
680
        } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
681
            //: %1 is list of tool names.
682
            status = tr("Helpers: %1.").arg(helperNames.join(QLatin1String(", ")));
683 684 685 686
        }

        m_ui->debuggingHelperWidget->setSummaryText(status);

687 688
        QString gdbHelperText;
        Qt::TextInteractionFlags gdbHelperTextFlags = Qt::NoTextInteraction;
689
        if (hasGdbHelper) {
690
            gdbHelperText = QDir::toNativeSeparators(version->gdbDebuggingHelperLibrary());
691
            gdbHelperTextFlags = Qt::TextSelectableByMouse;
692
        } else {
693 694 695 696 697
            if (canBuildGdbHelper) {
                gdbHelperText =  tr("<i>Not yet built.</i>");
            } else {
                gdbHelperText =  tr("<i>Not needed.</i>");
            }
698
        }
699 700
        m_debuggingHelperUi->gdbHelperStatus->setText(gdbHelperText);
        m_debuggingHelperUi->gdbHelperStatus->setTextInteractionFlags(gdbHelperTextFlags);
701
        m_debuggingHelperUi->gdbHelperBuildButton->setEnabled(canBuildGdbHelper && !isBuildingGdbHelper);
702

703
        QString qmlDumpStatusText, qmlDumpStatusToolTip;
704
        Qt::TextInteractionFlags qmlDumpStatusTextFlags = Qt::NoTextInteraction;
705
        if (hasQmlDumper) {
706 707
            qmlDumpStatusText = QDir::toNativeSeparators(version->qmlDumpTool(false));
            const QString debugQmlDumpPath = QDir::toNativeSeparators(version->qmlDumpTool(true));
708
            if (qmlDumpStatusText != debugQmlDumpPath) {
709 710
                if (!qmlDumpStatusText.isEmpty()
                        && !debugQmlDumpPath.isEmpty())
711 712 713
                    qmlDumpStatusText += QLatin1String("\n");
                qmlDumpStatusText += debugQmlDumpPath;
            }
714
            qmlDumpStatusTextFlags = Qt::TextSelectableByMouse;
715
        } else {
716 717 718 719
            if (canBuildQmlDumper) {
                qmlDumpStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDumpStatusText = tr("<i>Cannot be compiled.</i>");
720
                QmlDumpTool::canBuild(version, &qmlDumpStatusToolTip);
721
            }
722
        }
723
        m_debuggingHelperUi->qmlDumpStatus->setText(qmlDumpStatusText);
724
        m_debuggingHelperUi->qmlDumpStatus->setTextInteractionFlags(qmlDumpStatusTextFlags);
725
        m_debuggingHelperUi->qmlDumpStatus->setToolTip(qmlDumpStatusToolTip);
726
        m_debuggingHelperUi->qmlDumpBuildButton->setEnabled(canBuildQmlDumper & !isBuildingQmlDumper);
727

728
        QString qmlDebuggingLibStatusText, qmlDebuggingLibToolTip;
729 730 731 732 733 734 735 736 737 738
        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()) {
739
                    qmlDebuggingLibStatusText += QLatin1Char('\n');
740 741 742 743
                }
                qmlDebuggingLibStatusText += debugPath;
            }
            qmlDebuggingLibStatusTextFlags = Qt::TextSelectableByMouse;
744 745 746 747
        } else {
            if (!needsQmlDebuggingLib) {
                qmlDebuggingLibStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDebuggingLib) {
748 749 750
                qmlDebuggingLibStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDebuggingLibStatusText = tr("<i>Cannot be compiled.</i>");
751
                QmlDebuggingLibrary::canBuild(version, &qmlDebuggingLibToolTip);
752 753 754 755
            }
        }
        m_debuggingHelperUi->qmlDebuggingLibStatus->setText(qmlDebuggingLibStatusText);
        m_debuggingHelperUi->qmlDebuggingLibStatus->setTextInteractionFlags(qmlDebuggingLibStatusTextFlags);
756
        m_debuggingHelperUi->qmlDebuggingLibStatus->setToolTip(qmlDebuggingLibToolTip);
757 758
        m_debuggingHelperUi->qmlDebuggingLibBuildButton->setEnabled(needsQmlDebuggingLib
                                                                    && canBuildQmlDebuggingLib
759 760
                                                                    && !isBuildingQmlDebuggingLib);

761
        QString qmlObserverStatusText, qmlObserverToolTip;
762
        Qt::TextInteractionFlags qmlObserverStatusTextFlags = Qt::NoTextInteraction;
763
        if (hasQmlObserver) {
764
            qmlObserverStatusText = QDir::toNativeSeparators(version->qmlObserverTool());
765
            qmlObserverStatusTextFlags = Qt::TextSelectableByMouse;
766
        }  else {
767 768 769
            if (!needsQmlDebuggingLib) {
                qmlObserverStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlObserver) {
770 771 772
                qmlObserverStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlObserverStatusText = tr("<i>Cannot be compiled.</i>");
773
                QmlObserverTool::canBuild(version, &qmlObserverToolTip);
774
            }
775
        }
776
        m_debuggingHelperUi->qmlObserverStatus->setText(qmlObserverStatusText);
777
        m_debuggingHelperUi->qmlObserverStatus->setTextInteractionFlags(qmlObserverStatusTextFlags);
778
        m_debuggingHelperUi->qmlObserverStatus->setToolTip(qmlObserverToolTip);
779 780
        m_debuggingHelperUi->qmlObserverBuildButton->setEnabled(canBuildQmlObserver
                                                                & !isBuildingQmlObserver);
781

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
        QList<ProjectExplorer::ToolChain*> toolchains = toolChains(currentVersion());
        QString selectedToolChainId = m_debuggingHelperUi->toolChainComboBox->itemData(
                    m_debuggingHelperUi->toolChainComboBox->currentIndex()).toString();
        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);
        }

797 798 799
        const bool hasLog = currentItem && !currentItem->data(0, BuildLogRole).toString().isEmpty();
        m_debuggingHelperUi->showLogButton->setEnabled(hasLog);

800 801 802 803 804 805 806 807 808 809 810
        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);
811

812
        m_ui->debuggingHelperWidget->setVisible(true);
813 814 815
    }
}

dt's avatar
dt committed
816 817
// To be called if a qt version was removed or added
void QtOptionsPageWidget::updateCleanUpButton()
818
{
819
    bool hasInvalidVersion = false;
820 821
    for (int i = 0; i < m_versions.count(); ++i) {
        if (!m_versions.at(i)->isValid()) {
822
            hasInvalidVersion = true;
dt's avatar
dt committed
823
            break;
824 825
        }
    }
826
    m_ui->cleanUpButton->setEnabled(hasInvalidVersion);
827
}
828

dt's avatar
dt committed
829
void QtOptionsPageWidget::userChangedCurrentVersion()
con's avatar
con committed
830
{
dt's avatar
dt committed
831 832 833
    updateWidgets();
    updateDescriptionLabel();
    updateDebuggingHelperUi();
834 835
}

dt's avatar
dt committed
836
void QtOptionsPageWidget::qtVersionChanged()
837
{
838
    updateDescriptionLabel();
dt's avatar
dt committed
839
    updateDebuggingHelperUi();
840 841 842 843
}

void QtOptionsPageWidget::updateDescriptionLabel()
{
844
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
845 846 847 848 849
    const ValidityInfo info = validInformation(currentVersion());
    m_versionUi->errorLabel->setText(info.message);
    m_versionUi->errorLabel->setToolTip(info.toolTip);
    if (item)
        item->setIcon(0, info.icon);
850 851
}

852
int QtOptionsPageWidget::indexForTreeItem(const QTreeWidgetItem *item) const
853 854 855
{
    if (!item || !item->parent())
        return -1;
856
    const int uniqueId = item->data(0, VersionIdRole).toInt();
857 858 859 860 861 862 863
    for (int index = 0; index < m_versions.size(); ++index) {
        if (m_versions.at(index)->uniqueId() == uniqueId)
            return index;
    }
    return -1;
}

864 865
QTreeWidgetItem *QtOptionsPageWidget::treeItemForIndex(int index) const
{
866
    const int uniqueId = m_versions.at(index)->uniqueId();
867 868 869 870
    for (int i = 0; i < m_ui->qtdirList->topLevelItemCount(); ++i) {
        QTreeWidgetItem *toplevelItem = m_ui->qtdirList->topLevelItem(i);
        for (int j = 0; j < toplevelItem->childCount(); ++j) {
            QTreeWidgetItem *item = toplevelItem->child(j);
871
            if (item->data(0, VersionIdRole).toInt() == uniqueId) {
872 873 874 875 876 877 878
                return item;
            }
        }
    }
    return 0;
}

dt's avatar
dt committed
879
void QtOptionsPageWidget::versionChanged(QTreeWidgetItem *newItem, QTreeWidgetItem *old)
880
{
dt's avatar
dt committed
881 882
    Q_UNUSED(newItem)
    if (old)
883
        fixQtVersionName(indexForTreeItem(old));
dt's avatar
dt committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897
    userChangedCurrentVersion();
}

void QtOptionsPageWidget::updateWidgets()
{
    delete m_configurationWidget;
    m_configurationWidget = 0;
    BaseQtVersion *version = currentVersion();
    if (version) {
        m_versionUi->nameEdit->setText(version->displayName());
        m_versionUi->qmakePath->setText(QDir::toNativeSeparators(version->qmakeCommand()));
        m_configurationWidget = version->createConfigurationWidget();
        if (m_configurationWidget) {
            m_versionUi->formLayout->addRow(m_configurationWidget);
dt's avatar
dt committed
898
            m_configurationWidget->setEnabled(!version->isAutodetected());
dt's avatar
dt committed
899 900 901
            connect(m_configurationWidget, SIGNAL(changed()),
                    this, SLOT(qtVersionChanged()));
        }
902
    } else {
903
        m_versionUi->nameEdit->clear();
dt's avatar
dt committed
904
        m_versionUi->qmakePath->setText(QString()); // clear()
905 906
    }

dt's avatar
dt committed
907 908 909 910
    const bool enabled = version != 0;
    const bool isAutodetected = enabled && version->isAutodetected();
    m_ui->delButton->setEnabled(enabled && !isAutodetected);
    m_versionUi->nameEdit->setEnabled(enabled && !isAutodetected);
dt_'s avatar
dt_ committed
911
    m_versionUi->editPathPushButton->setEnabled(enabled && !isAutodetected);
912 913
}

914
void QtOptionsPageWidget::updateCurrentQtName()
915 916 917
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
918