qtoptionspage.cpp 39 KB
Newer Older
Tobias Hunger's avatar
Tobias Hunger committed
1 2 3 4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
Tobias Hunger's avatar
Tobias Hunger committed
6
**
7
** Contact: Nokia Corporation (qt-info@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
29
** Nokia at qt-info@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 <projectexplorer/projectexplorerconstants.h>
54
#include <qtconcurrent/runextensions.h>
55 56

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

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

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

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

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

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

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

QString QtOptionsPage::category() const
{
88
    return QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
89 90
}

91
QString QtOptionsPage::displayCategory() const
92
{
93 94
    return QCoreApplication::translate("ProjectExplorer",
                                       ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY);
95 96
}

97 98
QIcon QtOptionsPage::categoryIcon() const
{
99
    return QIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
100 101
}

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

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

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

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

126
//-----------------------------------------------------
127 128


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

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

147 148 149
    QWidget *debuggingHelperDetailsWidget = new QWidget();
    m_debuggingHelperUi->setupUi(debuggingHelperDetailsWidget);

150
    m_ui->setupUi(this);
151 152 153 154 155

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

    m_ui->debuggingHelperWidget->setWidget(debuggingHelperDetailsWidget);
156 157
    connect(m_ui->debuggingHelperWidget, SIGNAL(expanded(bool)),
            this, SLOT(handleDebuggingHelperExpanded(bool)));
158

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

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

185
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(const QString &)),
186 187
            this, SLOT(updateCurrentQtName()));

dt_'s avatar
dt_ committed
188 189 190
    connect(m_versionUi->editPathPushButton, SIGNAL(clicked()),
            this, SLOT(editPath()));

191 192 193 194 195 196 197 198
    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 *)));

199
    connect(m_debuggingHelperUi->rebuildButton, SIGNAL(clicked()),
200
            this, SLOT(buildDebuggingHelper()));
201 202 203 204
    connect(m_debuggingHelperUi->gdbHelperBuildButton, SIGNAL(clicked()),
            this, SLOT(buildGdbHelper()));
    connect(m_debuggingHelperUi->qmlDumpBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDump()));
205 206
    connect(m_debuggingHelperUi->qmlDebuggingLibBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDebuggingLibrary()));
207 208 209 210
    connect(m_debuggingHelperUi->qmlObserverBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlObserver()));

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
211
            this, SLOT(slotShowDebuggingBuildLog()));
212 213
    connect(m_debuggingHelperUi->toolChainComboBox, SIGNAL(activated(int)),
            this, SLOT(selectedToolChainChanged(int)));
214

215
    connect(m_ui->cleanUpButton, SIGNAL(clicked()), this, SLOT(cleanUpQtVersions()));
dt's avatar
dt committed
216 217
    userChangedCurrentVersion();
    updateCleanUpButton();
218

219 220
    connect(QtVersionManager::instance(), SIGNAL(dumpUpdatedFor(Utils::FileName)),
            this, SLOT(qtVersionsDumpUpdated(Utils::FileName)));
221 222 223

    connect(ProjectExplorer::ToolChainManager::instance(), SIGNAL(toolChainsChanged()),
            this, SLOT(toolChainsUpdated()));
224 225
}

226 227 228 229 230
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)
231
        return false;
232 233 234 235 236 237 238 239
    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;
240
    const QString tooltip = m_versions.at(index)->toHtml(true);
241 242 243 244 245
    QToolTip::showText(helpEvent->globalPos(), tooltip, m_ui->qtdirList);
    helpEvent->accept();
    return true;
}

246
int QtOptionsPageWidget::currentIndex() const
247
{
248 249 250 251
    if (QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        return indexForTreeItem(currentItem);
    return -1;
}
252

dt's avatar
dt committed
253
BaseQtVersion *QtOptionsPageWidget::currentVersion() const
254 255 256
{
    const int currentItemIndex = currentIndex();
    if (currentItemIndex >= 0 && currentItemIndex < m_versions.size())
257
        return m_versions.at(currentItemIndex);
258 259
    return 0;
}
260

dt's avatar
dt committed
261
static inline int findVersionById(const QList<BaseQtVersion *> &l, int id)
262 263 264
{
    const int size = l.size();
    for (int i = 0; i < size; i++)
265
        if (l.at(i)->uniqueId() == id)
266 267 268
            return i;
    return -1;
}
269

270
// Update with results of terminated helper build
271
void QtOptionsPageWidget::debuggingHelperBuildFinished(int qtVersionId, const QString &output, DebuggingHelperBuildTask::Tools tools)
272
{
273
    const int index = findVersionById(m_versions, qtVersionId);
274 275
    if (index == -1)
        return; // Oops, somebody managed to delete the version
276

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

279 280
    // Update item view
    QTreeWidgetItem *item = treeItemForIndex(index);
281 282 283 284 285
    QTC_ASSERT(item, return);
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags &= ~tools;
    item->setData(0, BuildRunningRole,  QVariant::fromValue(buildFlags));
286
    item->setData(0, BuildLogRole, output);
287

288 289
    bool success = true;
    if (tools & DebuggingHelperBuildTask::GdbDebugging)
dt's avatar
dt committed
290
        success &= version->hasGdbDebuggingHelper();
291
    if (tools & DebuggingHelperBuildTask::QmlDebugging)
dt's avatar
dt committed
292
        success &= version->hasQmlDebuggingLibrary();
293
    if (tools & DebuggingHelperBuildTask::QmlDump)
dt's avatar
dt committed
294
        success &= version->hasQmlDump();
295
    if (tools & DebuggingHelperBuildTask::QmlObserver)
dt's avatar
dt committed
296
        success &= version->hasQmlObserver();
297

298 299
    if (!success)
        showDebuggingBuildLog(item);
300 301

    updateDebuggingHelperUi();
302 303
}

304 305 306
void QtOptionsPageWidget::cleanUpQtVersions()
{
    QStringList toRemove;
dt's avatar
dt committed
307
    foreach (const BaseQtVersion *v, m_versions) {
308
        if (!v->isValid() && !v->isAutodetected())
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
            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
331
    updateCleanUpButton();
332 333
}

334 335 336 337
void QtOptionsPageWidget::toolChainsUpdated()
{
    for (int i = 0; i < m_versions.count(); ++i) {
        QTreeWidgetItem *item = treeItemForIndex(i);
338
        if (item == m_ui->qtdirList->currentItem()) {
339
            updateDescriptionLabel();
340 341
            updateDebuggingHelperUi();
        } else {
342 343 344 345 346 347
            const ValidityInfo info = validInformation(m_versions.at(i));
            item->setIcon(0, info.icon);
        }
    }
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361
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);
}

362
void QtOptionsPageWidget::qtVersionsDumpUpdated(const Utils::FileName &qmakeCommand)
363 364 365 366 367
{
    foreach (BaseQtVersion *version, m_versions) {
        if (version->qmakeCommand() == qmakeCommand)
            version->recheckDumper();
    }
368 369
    if (currentVersion()
            && currentVersion()->qmakeCommand() == qmakeCommand) {
370 371 372 373 374 375
        updateWidgets();
        updateDescriptionLabel();
        updateDebuggingHelperUi();
    }
}

376 377 378 379 380
void QtOptionsPageWidget::handleDebuggingHelperExpanded(bool expanded)
{
    m_ui->versionInfoWidget->setVisible(!expanded);
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
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;
    }

407
    bool useable = true;
408 409 410 411 412 413 414
    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;
415
        useable = false;
416 417 418
    } 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
419
        info.toolTip = tr("The following ABIs are currently not supported:<ul><li>%1</li></ul>")
420 421 422
                       .arg(missingToolChains.join(QLatin1String("</li><li>")));
        info.icon = m_warningVersionIcon;
    }
423 424 425 426 427

    if (useable) {
        QString warning = version->warningReason();
        if (!warning.isEmpty()) {
            if (!info.message.isEmpty())
428
                info.message.append(QLatin1Char('\n'));
429 430 431 432 433
            info.message += warning;
            info.icon = m_warningVersionIcon;
        }
    }

434 435 436
    return info;
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
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();
}

456 457 458 459 460 461 462 463
QString QtOptionsPageWidget::defaultToolChainId(const BaseQtVersion *version)
{
    QList<ProjectExplorer::ToolChain*> possibleToolChains = toolChains(version);
    if (!possibleToolChains.isEmpty())
        return possibleToolChains.first()->id();
    return QString();
}

464
void QtOptionsPageWidget::buildDebuggingHelper(DebuggingHelperBuildTask::Tools tools)
465 466 467 468 469
{
    const int index = currentIndex();
    if (index < 0)
        return;

470 471 472
    // remove tools that cannot be build
    tools &= DebuggingHelperBuildTask::availableTools(currentVersion());

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

476 477 478 479
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags |= tools;
    item->setData(0, BuildRunningRole, QVariant::fromValue(buildFlags));
480

dt's avatar
dt committed
481
    BaseQtVersion *version = m_versions.at(index);
482 483 484
    if (!version)
        return;

485
    updateDebuggingHelperUi();
486

487
    // Run a debugging helper build task in the background.
488 489 490 491 492 493 494 495
    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);
496 497
    // Don't open General Messages pane with errors
    buildTask->showOutputOnError(false);
498 499
    connect(buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
            this, SLOT(debuggingHelperBuildFinished(int,QString,DebuggingHelperBuildTask::Tools)),
500
            Qt::QueuedConnection);
501
    QFuture<void> task = QtConcurrent::run(&DebuggingHelperBuildTask::run, buildTask);
502
    const QString taskName = tr("Building helpers");
503

hjk's avatar
hjk committed
504
    Core::ICore::progressManager()->addTask(task, taskName,
505
                                                        QLatin1String("Qt4ProjectManager::BuildHelpers"));
506
}
507 508 509 510 511 512 513 514 515 516
void QtOptionsPageWidget::buildGdbHelper()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::GdbDebugging);
}

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

517 518 519 520 521
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

522 523
void QtOptionsPageWidget::buildQmlObserver()
{
524
    DebuggingHelperBuildTask::Tools qmlDbgTools =
Kai Koehne's avatar
Kai Koehne committed
525 526
            DebuggingHelperBuildTask::QmlObserver;
    qmlDbgTools |= DebuggingHelperBuildTask::QmlDebugging;
527
    buildDebuggingHelper(qmlDbgTools);
528
}
529

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
// 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)
547
{
548 549 550 551
    m_ui.log->setPlainText(text); // Show and scroll to bottom
    m_ui.log->moveCursor(QTextCursor::End);
    m_ui.log->ensureCursorVisible();
}
552

553 554 555 556 557 558 559 560 561
void QtOptionsPageWidget::slotShowDebuggingBuildLog()
{
    if (const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        showDebuggingBuildLog(currentItem);
}

void QtOptionsPageWidget::showDebuggingBuildLog(const QTreeWidgetItem *currentItem)
{
    const int currentItemIndex = indexForTreeItem(currentItem);
562 563
    if (currentItemIndex < 0)
        return;
564
    BuildLogDialog *dialog = new BuildLogDialog(this->window());
565
    dialog->setWindowTitle(tr("Debugging Helper Build Log for '%1'").arg(currentItem->text(0)));
566
    dialog->setText(currentItem->data(0, BuildLogRole).toString());
567
    dialog->show();
568 569
}

570
QtOptionsPageWidget::~QtOptionsPageWidget()
571 572
{
    delete m_ui;
dt's avatar
dt committed
573 574
    delete m_versionUi;
    delete m_debuggingHelperUi;
dt's avatar
dt committed
575
    delete m_configurationWidget;
576
    qDeleteAll(m_versions);
577 578
}

dt_'s avatar
dt_ committed
579
static QString filterForQmakeFileDialog()
580
{
581
    QString filter = QLatin1String("qmake (");
dt's avatar
dt committed
582
    foreach (const QString &s, Utils::BuildableHelperLibrary::possibleQMakeCommands()) {
583
#ifdef Q_OS_MAC
584 585 586 587
        // work around QTBUG-7739 that prohibits filters that don't start with *
        filter += QLatin1Char('*');
#endif
        filter += s + QLatin1Char(' ');
dt's avatar
dt committed
588
    }
589
    filter += QLatin1Char(')');
dt_'s avatar
dt_ committed
590 591
    return filter;
}
592

dt_'s avatar
dt_ committed
593 594
void QtOptionsPageWidget::addQtDir()
{
595 596 597 598 599
    Utils::FileName qtVersion = Utils::FileName::fromString(
                QFileDialog::getOpenFileName(this,
                                             tr("Select a qmake executable"),
                                             QString(),
                                             filterForQmakeFileDialog()));
dt's avatar
dt committed
600 601 602 603 604
    if (qtVersion.isNull())
        return;
    if (QtVersionManager::instance()->qtVersionForQMakeBinary(qtVersion)) {
        // Already exist
    }
605

dt's avatar
dt committed
606 607 608
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
    if (version) {
        m_versions.append(version);
609

dt's avatar
dt committed
610 611
        QTreeWidgetItem *item = new QTreeWidgetItem(m_ui->qtdirList->topLevelItem(1));
        item->setText(0, version->displayName());
612
        item->setText(1, version->qmakeCommand().toUserOutput());
dt's avatar
dt committed
613
        item->setData(0, VersionIdRole, version->uniqueId());
614
        item->setData(0, ToolChainIdRole, defaultToolChainId(version));
dt's avatar
dt committed
615 616 617 618 619 620
        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();
621 622
}

623
void QtOptionsPageWidget::removeQtDir()
624 625
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
626
    int index = indexForTreeItem(item);
627 628 629 630 631
    if (index < 0)
        return;

    delete item;

dt's avatar
dt committed
632
    BaseQtVersion *version = m_versions.at(index);
633
    m_versions.removeAt(index);
634
    delete version;
dt's avatar
dt committed
635
    updateCleanUpButton();
636 637
}

dt_'s avatar
dt_ committed
638 639
void QtOptionsPageWidget::editPath()
{
640
    BaseQtVersion *current = currentVersion();
641 642 643 644 645
    QString dir = currentVersion()->qmakeCommand().toFileInfo().absolutePath();
    Utils::FileName qtVersion = Utils::FileName::fromString(
                QFileDialog::getOpenFileName(this,
                                             tr("Select a qmake executable"),
                                             dir, filterForQmakeFileDialog()));
dt_'s avatar
dt_ committed
646 647 648
    if (qtVersion.isNull())
        return;
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
Daniel Teske's avatar
Daniel Teske committed
649 650
    if (!version)
        return;
dt_'s avatar
dt_ committed
651 652 653 654 655 656 657 658 659 660 661
    // 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
662 663
    if (current->displayName() != current->defaultDisplayName(current->qtVersionString(), current->qmakeCommand()))
        version->setDisplayName(current->displayName());
dt_'s avatar
dt_ committed
664 665 666 667 668 669 670
    m_versions.replace(m_versions.indexOf(current), version);
    delete current;

    // Update ui
    userChangedCurrentVersion();
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
    item->setText(0, version->displayName());
671
    item->setText(1, version->qmakeCommand().toUserOutput());
dt_'s avatar
dt_ committed
672
    item->setData(0, VersionIdRole, version->uniqueId());
673
    item->setData(0, ToolChainIdRole, defaultToolChainId(version));
dt_'s avatar
dt_ committed
674 675 676
    item->setIcon(0, version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
}

677
void QtOptionsPageWidget::updateDebuggingHelperUi()
678
{
dt's avatar
dt committed
679
    BaseQtVersion *version = currentVersion();
680
    const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
681

682 683 684
    QList<ProjectExplorer::ToolChain*> toolchains = toolChains(currentVersion());

    if (!version || !version->isValid() || toolchains.isEmpty()) {
685
        m_ui->debuggingHelperWidget->setVisible(false);
686
    } else {
687 688 689 690 691
        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;
692

693 694
        const bool hasGdbHelper = !version->gdbDebuggingHelperLibrary().isEmpty();
        const bool hasQmlDumper = version->hasQmlDump();
695
        const bool needsQmlDumper = version->needsQmlDump();
696
        const bool hasQmlDebuggingLib = version->hasQmlDebuggingLibrary();
697
        const bool needsQmlDebuggingLib = version->needsQmlDebuggingLibrary();
698
        const bool hasQmlObserver = !version->qmlObserverTool().isEmpty();
699

700 701
        bool isBuildingGdbHelper = false;
        bool isBuildingQmlDumper = false;
702
        bool isBuildingQmlDebuggingLib = false;
703 704 705 706 707 708 709
        bool isBuildingQmlObserver = false;

        if (currentItem) {
            DebuggingHelperBuildTask::Tools buildingTools
                    = currentItem->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
            isBuildingGdbHelper = buildingTools & DebuggingHelperBuildTask::GdbDebugging;
            isBuildingQmlDumper = buildingTools & DebuggingHelperBuildTask::QmlDump;
710
            isBuildingQmlDebuggingLib = buildingTools & DebuggingHelperBuildTask::QmlDebugging;
711 712 713
            isBuildingQmlObserver = buildingTools & DebuggingHelperBuildTask::QmlObserver;
        }

714 715
        // get names of tools from labels
        QStringList helperNames;
716
        const QChar colon = QLatin1Char(':');
717
        if (hasGdbHelper)
718
            helperNames << m_debuggingHelperUi->gdbHelperLabel->text().remove(colon);
719
        if (hasQmlDumper)
720
            helperNames << m_debuggingHelperUi->qmlDumpLabel->text().remove(colon);
721
        if (hasQmlDebuggingLib)
722
            helperNames << m_debuggingHelperUi->qmlDebuggingLibLabel->text().remove(colon);
723
        if (hasQmlObserver)
724
            helperNames << m_debuggingHelperUi->qmlObserverLabel->text().remove(colon);
725 726 727

        QString status;
        if (helperNames.isEmpty()) {
728
            status = tr("Helpers: None available");
729
        } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
730
            //: %1 is list of tool names.
731
            status = tr("Helpers: %1.").arg(helperNames.join(QLatin1String(", ")));
732 733 734 735
        }

        m_ui->debuggingHelperWidget->setSummaryText(status);

736 737
        QString gdbHelperText;
        Qt::TextInteractionFlags gdbHelperTextFlags = Qt::NoTextInteraction;
738
        if (hasGdbHelper) {
739
            gdbHelperText = QDir::toNativeSeparators(version->gdbDebuggingHelperLibrary());
740
            gdbHelperTextFlags = Qt::TextSelectableByMouse;
741
        } else {
742 743 744 745 746
            if (canBuildGdbHelper) {
                gdbHelperText =  tr("<i>Not yet built.</i>");
            } else {
                gdbHelperText =  tr("<i>Not needed.</i>");
            }
747
        }
748 749
        m_debuggingHelperUi->gdbHelperStatus->setText(gdbHelperText);
        m_debuggingHelperUi->gdbHelperStatus->setTextInteractionFlags(gdbHelperTextFlags);
750
        m_debuggingHelperUi->gdbHelperBuildButton->setEnabled(canBuildGdbHelper && !isBuildingGdbHelper);
751

752
        QString qmlDumpStatusText, qmlDumpStatusToolTip;
753
        Qt::TextInteractionFlags qmlDumpStatusTextFlags = Qt::NoTextInteraction;
754
        if (hasQmlDumper) {
755 756
            qmlDumpStatusText = QDir::toNativeSeparators(version->qmlDumpTool(false));
            const QString debugQmlDumpPath = QDir::toNativeSeparators(version->qmlDumpTool(true));
757
            if (qmlDumpStatusText != debugQmlDumpPath) {
758 759
                if (!qmlDumpStatusText.isEmpty()
                        && !debugQmlDumpPath.isEmpty())
760 761 762
                    qmlDumpStatusText += QLatin1String("\n");
                qmlDumpStatusText += debugQmlDumpPath;
            }
763
            qmlDumpStatusTextFlags = Qt::TextSelectableByMouse;
764
        } else {
765 766 767
            if (!needsQmlDumper) {
                qmlDumpStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDumper) {
768 769 770
                qmlDumpStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDumpStatusText = tr("<i>Cannot be compiled.</i>");
771
                QmlDumpTool::canBuild(version, &qmlDumpStatusToolTip);
772
            }
773
        }
774
        m_debuggingHelperUi->qmlDumpStatus->setText(qmlDumpStatusText);
775
        m_debuggingHelperUi->qmlDumpStatus->setTextInteractionFlags(qmlDumpStatusTextFlags);
776
        m_debuggingHelperUi->qmlDumpStatus->setToolTip(qmlDumpStatusToolTip);
777
        m_debuggingHelperUi->qmlDumpBuildButton->setEnabled(canBuildQmlDumper & !isBuildingQmlDumper);
778

779
        QString qmlDebuggingLibStatusText, qmlDebuggingLibToolTip;
780 781 782 783 784 785 786 787 788 789
        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()) {
790
                    qmlDebuggingLibStatusText += QLatin1Char('\n');
791 792 793 794
                }
                qmlDebuggingLibStatusText += debugPath;
            }
            qmlDebuggingLibStatusTextFlags = Qt::TextSelectableByMouse;
795 796 797 798
        } else {
            if (!needsQmlDebuggingLib) {
                qmlDebuggingLibStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDebuggingLib) {
799 800 801
                qmlDebuggingLibStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDebuggingLibStatusText = tr("<i>Cannot be compiled.</i>");
802
                QmlDebuggingLibrary::canBuild(version, &qmlDebuggingLibToolTip);
803 804 805 806
            }
        }
        m_debuggingHelperUi->qmlDebuggingLibStatus->setText(qmlDebuggingLibStatusText);
        m_debuggingHelperUi->qmlDebuggingLibStatus->setTextInteractionFlags(qmlDebuggingLibStatusTextFlags);
807
        m_debuggingHelperUi->qmlDebuggingLibStatus->setToolTip(qmlDebuggingLibToolTip);
808 809
        m_debuggingHelperUi->qmlDebuggingLibBuildButton->setEnabled(needsQmlDebuggingLib
                                                                    && canBuildQmlDebuggingLib
810 811
                                                                    && !isBuildingQmlDebuggingLib);

812
        QString qmlObserverStatusText, qmlObserverToolTip;
813
        Qt::TextInteractionFlags qmlObserverStatusTextFlags = Qt::NoTextInteraction;
814
        if (hasQmlObserver) {
815
            qmlObserverStatusText = QDir::toNativeSeparators(version->qmlObserverTool());
816
            qmlObserverStatusTextFlags = Qt::TextSelectableByMouse;
817
        }  else {
818 819 820
            if (!needsQmlDebuggingLib) {
                qmlObserverStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlObserver) {
821 822 823
                qmlObserverStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlObserverStatusText = tr("<i>Cannot be compiled.</i>");
824
                QmlObserverTool::canBuild(version, &qmlObserverToolTip);
825
            }
826
        }
827
        m_debuggingHelperUi->qmlObserverStatus->setText(qmlObserverStatusText);
828
        m_debuggingHelperUi->qmlObserverStatus->setTextInteractionFlags(qmlObserverStatusTextFlags);
829
        m_debuggingHelperUi->qmlObserverStatus->setToolTip(qmlObserverToolTip);
830 831
        m_debuggingHelperUi->qmlObserverBuildButton->setEnabled(canBuildQmlObserver
                                                                & !isBuildingQmlObserver);
832

833
        QList<ProjectExplorer::ToolChain*> toolchains = toolChains(currentVersion());
834
        QString selectedToolChainId = currentItem->data(0, ToolChainIdRole).toString();
835 836 837 838 839 840 841 842 843 844 845 846
        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);
        }

847 848 849
        const bool hasLog = currentItem && !currentItem->data(0, BuildLogRole).toString().isEmpty();
        m_debuggingHelperUi->showLogButton->setEnabled(hasLog);

850 851 852 853 854 855 856 857 858 859 860
        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);
861

862
        m_ui->debuggingHelperWidget->setVisible(true);
863 864 865
    }
}

dt's avatar
dt committed
866 867
// To be called if a qt version was removed or added
void QtOptionsPageWidget::updateCleanUpButton()
868
{
869
    bool hasInvalidVersion = false;
870 871
    for (int i = 0; i < m_versions.count(); ++i) {
        if (!m_versions.at(i)->isValid()) {
872
            hasInvalidVersion = true;
dt's avatar
dt committed
873
            break;
874 875
        }
    }
876
    m_ui->cleanUpButton->setEnabled(hasInvalidVersion);
877
}
878

dt's avatar
dt committed
879
void QtOptionsPageWidget::userChangedCurrentVersion()
con's avatar
con committed
880
{
dt's avatar
dt committed
881 882 883
    updateWidgets();
    updateDescriptionLabel();
    updateDebuggingHelperUi();
884 885
}

dt's avatar
dt committed
886
void QtOptionsPageWidget::qtVersionChanged()
887
{
888
    updateDescriptionLabel();