qtoptionspage.cpp 32.9 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 <qtconcurrent/runextensions.h>
52 53

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

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

61 62
using namespace QtSupport;
using namespace QtSupport::Internal;
63

64 65 66 67 68
///
// QtOptionsPage
///

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

QString QtOptionsPage::id() const
{
75
    return QLatin1String(Constants::QTVERSION_SETTINGS_PAGE_ID);
76 77
}

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

QString QtOptionsPage::category() const
{
85
    return QLatin1String(Constants::QT_SETTINGS_CATEGORY);
86 87
}

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

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

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

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

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

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

122
//-----------------------------------------------------
123 124


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

138 139
    QWidget *versionInfoWidget = new QWidget();
    m_versionUi->setupUi(versionInfoWidget);
140
    m_versionUi->editPathPushButton->setText(tr(Utils::PathChooser::browseButtonLabel));
141

142 143 144
    QWidget *debuggingHelperDetailsWidget = new QWidget();
    m_debuggingHelperUi->setupUi(debuggingHelperDetailsWidget);

145
    m_ui->setupUi(this);
146 147 148 149 150

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

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

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

166
    for (int i = 0; i < m_versions.count(); ++i) {
dt's avatar
dt committed
167
        BaseQtVersion *version = m_versions.at(i);
168
        QTreeWidgetItem *item = new QTreeWidgetItem(version->isAutodetected()? autoItem : manualItem);
169
        item->setText(0, version->displayName());
170
        item->setText(1, QDir::toNativeSeparators(version->qmakeCommand()));
171
        item->setData(0, VersionIdRole, version->uniqueId());
dt's avatar
dt committed
172
        item->setIcon(0, version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
173
    }
174
    m_ui->qtdirList->expandAll();
175

176
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(const QString &)),
177 178
            this, SLOT(updateCurrentQtName()));

dt_'s avatar
dt_ committed
179 180 181
    connect(m_versionUi->editPathPushButton, SIGNAL(clicked()),
            this, SLOT(editPath()));

182 183 184 185 186 187 188 189
    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 *)));

190
    connect(m_debuggingHelperUi->rebuildButton, SIGNAL(clicked()),
191
            this, SLOT(buildDebuggingHelper()));
192 193 194 195
    connect(m_debuggingHelperUi->gdbHelperBuildButton, SIGNAL(clicked()),
            this, SLOT(buildGdbHelper()));
    connect(m_debuggingHelperUi->qmlDumpBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDump()));
196 197
    connect(m_debuggingHelperUi->qmlDebuggingLibBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDebuggingLibrary()));
198 199 200 201
    connect(m_debuggingHelperUi->qmlObserverBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlObserver()));

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
202
            this, SLOT(slotShowDebuggingBuildLog()));
203

204
    connect(m_ui->cleanUpButton, SIGNAL(clicked()), this, SLOT(cleanUpQtVersions()));
dt's avatar
dt committed
205 206
    userChangedCurrentVersion();
    updateCleanUpButton();
207

208 209
    connect(QtVersionManager::instance(), SIGNAL(dumpUpdatedFor(QString)),
            this, SLOT(qtVersionsDumpUpdated(QString)));
210 211
}

212 213 214 215 216
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)
217
        return false;
218 219 220 221 222 223 224 225
    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;
226
    const QString tooltip = m_versions.at(index)->toHtml(true);
227 228 229 230 231
    QToolTip::showText(helpEvent->globalPos(), tooltip, m_ui->qtdirList);
    helpEvent->accept();
    return true;
}

232
int QtOptionsPageWidget::currentIndex() const
233
{
234 235 236 237
    if (QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        return indexForTreeItem(currentItem);
    return -1;
}
238

dt's avatar
dt committed
239
BaseQtVersion *QtOptionsPageWidget::currentVersion() const
240 241 242
{
    const int currentItemIndex = currentIndex();
    if (currentItemIndex >= 0 && currentItemIndex < m_versions.size())
243
        return m_versions.at(currentItemIndex);
244 245
    return 0;
}
246

dt's avatar
dt committed
247
static inline int findVersionById(const QList<BaseQtVersion *> &l, int id)
248 249 250
{
    const int size = l.size();
    for (int i = 0; i < size; i++)
251
        if (l.at(i)->uniqueId() == id)
252 253 254
            return i;
    return -1;
}
255

256
// Update with results of terminated helper build
257
void QtOptionsPageWidget::debuggingHelperBuildFinished(int qtVersionId, const QString &output, DebuggingHelperBuildTask::Tools tools)
258
{
259
    const int index = findVersionById(m_versions, qtVersionId);
260 261
    if (index == -1)
        return; // Oops, somebody managed to delete the version
262

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

265 266
    // Update item view
    QTreeWidgetItem *item = treeItemForIndex(index);
267 268 269 270 271
    QTC_ASSERT(item, return);
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags &= ~tools;
    item->setData(0, BuildRunningRole,  QVariant::fromValue(buildFlags));
272
    item->setData(0, BuildLogRole, output);
273

274 275
    bool success = true;
    if (tools & DebuggingHelperBuildTask::GdbDebugging)
dt's avatar
dt committed
276
        success &= version->hasGdbDebuggingHelper();
277
    if (tools & DebuggingHelperBuildTask::QmlDebugging)
dt's avatar
dt committed
278
        success &= version->hasQmlDebuggingLibrary();
279
    if (tools & DebuggingHelperBuildTask::QmlDump)
dt's avatar
dt committed
280
        success &= version->hasQmlDump();
281
    if (tools & DebuggingHelperBuildTask::QmlObserver)
dt's avatar
dt committed
282
        success &= version->hasQmlObserver();
283 284 285

    // Update bottom control if the selection is still the same
    if (index == currentIndex()) {
286
        updateDebuggingHelperUi();
287
    }
288

289 290
    if (!success)
        showDebuggingBuildLog(item);
291 292
}

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

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

337
void QtOptionsPageWidget::buildDebuggingHelper(DebuggingHelperBuildTask::Tools tools)
338 339 340 341 342
{
    const int index = currentIndex();
    if (index < 0)
        return;

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

346 347 348 349
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags |= tools;
    item->setData(0, BuildRunningRole, QVariant::fromValue(buildFlags));
350

dt's avatar
dt committed
351
    BaseQtVersion *version = m_versions.at(index);
352 353 354
    if (!version)
        return;

355
    updateDebuggingHelperUi();
356

357
    // Run a debugging helper build task in the background.
358
    DebuggingHelperBuildTask *buildTask = new DebuggingHelperBuildTask(version, tools);
359 360
    // Don't open General Messages pane with errors
    buildTask->showOutputOnError(false);
361 362
    connect(buildTask, SIGNAL(finished(int,QString,DebuggingHelperBuildTask::Tools)),
            this, SLOT(debuggingHelperBuildFinished(int,QString,DebuggingHelperBuildTask::Tools)),
363
            Qt::QueuedConnection);
364
    QFuture<void> task = QtConcurrent::run(&DebuggingHelperBuildTask::run, buildTask);
365
    const QString taskName = tr("Building helpers");
366

367
    Core::ICore::instance()->progressManager()->addTask(task, taskName,
368
                                                        QLatin1String("Qt4ProjectManager::BuildHelpers"));
369
}
370 371 372 373 374 375 376 377 378 379
void QtOptionsPageWidget::buildGdbHelper()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::GdbDebugging);
}

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

380 381 382 383 384
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

385 386
void QtOptionsPageWidget::buildQmlObserver()
{
387
    DebuggingHelperBuildTask::Tools qmlDbgTools =
Kai Koehne's avatar
Kai Koehne committed
388 389
            DebuggingHelperBuildTask::QmlObserver;
    qmlDbgTools |= DebuggingHelperBuildTask::QmlDebugging;
390
    buildDebuggingHelper(qmlDbgTools);
391
}
392

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
// 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)
410
{
411 412 413 414
    m_ui.log->setPlainText(text); // Show and scroll to bottom
    m_ui.log->moveCursor(QTextCursor::End);
    m_ui.log->ensureCursorVisible();
}
415

416 417 418 419 420 421 422 423 424
void QtOptionsPageWidget::slotShowDebuggingBuildLog()
{
    if (const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        showDebuggingBuildLog(currentItem);
}

void QtOptionsPageWidget::showDebuggingBuildLog(const QTreeWidgetItem *currentItem)
{
    const int currentItemIndex = indexForTreeItem(currentItem);
425 426
    if (currentItemIndex < 0)
        return;
427 428
    BuildLogDialog *dialog = new BuildLogDialog(this);
    dialog->setWindowTitle(tr("Debugging Helper Build Log for '%1'").arg(currentItem->text(0)));
429
    dialog->setText(currentItem->data(0, BuildLogRole).toString());
430
    dialog->show();
431 432
}

433
QtOptionsPageWidget::~QtOptionsPageWidget()
434 435
{
    delete m_ui;
dt's avatar
dt committed
436 437
    delete m_versionUi;
    delete m_debuggingHelperUi;
dt's avatar
dt committed
438
    delete m_configurationWidget;
439
    qDeleteAll(m_versions);
440 441
}

dt_'s avatar
dt_ committed
442
static QString filterForQmakeFileDialog()
443
{
dt's avatar
dt committed
444 445
    QString filter("qmake (");
    foreach (const QString &s, Utils::BuildableHelperLibrary::possibleQMakeCommands()) {
446 447 448 449 450
#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
451
    }
452
    filter += QLatin1Char(')');
dt_'s avatar
dt_ committed
453 454
    return filter;
}
455

dt_'s avatar
dt_ committed
456 457
void QtOptionsPageWidget::addQtDir()
{
dt's avatar
dt committed
458
    QString qtVersion = QFileDialog::getOpenFileName(this,
dt_'s avatar
dt_ committed
459 460
                                                     tr("Select a qmake executable"),
                                                     QString(), filterForQmakeFileDialog());
dt's avatar
dt committed
461 462 463 464 465
    if (qtVersion.isNull())
        return;
    if (QtVersionManager::instance()->qtVersionForQMakeBinary(qtVersion)) {
        // Already exist
    }
466

dt's avatar
dt committed
467 468 469
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
    if (version) {
        m_versions.append(version);
470

dt's avatar
dt committed
471 472 473 474 475 476 477 478 479 480
        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();
481 482
}

483
void QtOptionsPageWidget::removeQtDir()
484 485
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
486
    int index = indexForTreeItem(item);
487 488 489 490 491
    if (index < 0)
        return;

    delete item;

dt's avatar
dt committed
492
    BaseQtVersion *version = m_versions.at(index);
493
    m_versions.removeAt(index);
494
    delete version;
dt's avatar
dt committed
495
    updateCleanUpButton();
496 497
}

dt_'s avatar
dt_ committed
498 499
void QtOptionsPageWidget::editPath()
{
500 501
    BaseQtVersion *current = currentVersion();
    QString dir = QFileInfo(currentVersion()->qmakeCommand()).absolutePath();
dt_'s avatar
dt_ committed
502 503
    QString qtVersion = QFileDialog::getOpenFileName(this,
                                                     tr("Select a qmake executable"),
504
                                                     dir, filterForQmakeFileDialog());
dt_'s avatar
dt_ committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518
    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
519 520
    if (current->displayName() != current->defaultDisplayName(current->qtVersionString(), current->qmakeCommand()))
        version->setDisplayName(current->displayName());
dt_'s avatar
dt_ committed
521 522 523 524 525 526 527 528 529 530 531 532
    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);
}

533
void QtOptionsPageWidget::updateDebuggingHelperUi()
534
{
dt's avatar
dt committed
535
    BaseQtVersion *version = currentVersion();
536
    const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
537

538
    if (!version || !version->isValid()) {
539
        m_ui->debuggingHelperWidget->setVisible(false);
540
    } else {
541 542 543 544 545
        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;
546

547 548 549
        const bool hasGdbHelper = !version->gdbDebuggingHelperLibrary().isEmpty();
        const bool hasQmlDumper = version->hasQmlDump();
        const bool hasQmlDebuggingLib = version->hasQmlDebuggingLibrary();
550
        const bool needsQmlDebuggingLib = version->needsQmlDebuggingLibrary();
551
        const bool hasQmlObserver = !version->qmlObserverTool().isEmpty();
552

553 554
        bool isBuildingGdbHelper = false;
        bool isBuildingQmlDumper = false;
555
        bool isBuildingQmlDebuggingLib = false;
556 557 558 559 560 561 562
        bool isBuildingQmlObserver = false;

        if (currentItem) {
            DebuggingHelperBuildTask::Tools buildingTools
                    = currentItem->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
            isBuildingGdbHelper = buildingTools & DebuggingHelperBuildTask::GdbDebugging;
            isBuildingQmlDumper = buildingTools & DebuggingHelperBuildTask::QmlDump;
563
            isBuildingQmlDebuggingLib = buildingTools & DebuggingHelperBuildTask::QmlDebugging;
564 565 566
            isBuildingQmlObserver = buildingTools & DebuggingHelperBuildTask::QmlObserver;
        }

567 568 569 570 571 572
        // get names of tools from labels
        QStringList helperNames;
        if (hasGdbHelper)
            helperNames << m_debuggingHelperUi->gdbHelperLabel->text().remove(':');
        if (hasQmlDumper)
            helperNames << m_debuggingHelperUi->qmlDumpLabel->text().remove(':');
573 574
        if (hasQmlDebuggingLib)
            helperNames << m_debuggingHelperUi->qmlDebuggingLibLabel->text().remove(':');
575 576 577 578 579
        if (hasQmlObserver)
            helperNames << m_debuggingHelperUi->qmlObserverLabel->text().remove(':');

        QString status;
        if (helperNames.isEmpty()) {
580
            status = tr("Helpers: None available");
581
        } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
582
            //: %1 is list of tool names.
583
            status = tr("Helpers: %1.").arg(helperNames.join(QLatin1String(", ")));
584 585 586 587
        }

        m_ui->debuggingHelperWidget->setSummaryText(status);

588 589
        QString gdbHelperText;
        Qt::TextInteractionFlags gdbHelperTextFlags = Qt::NoTextInteraction;
590
        if (hasGdbHelper) {
591
            gdbHelperText = QDir::toNativeSeparators(version->gdbDebuggingHelperLibrary());
592
            gdbHelperTextFlags = Qt::TextSelectableByMouse;
593
        } else {
594 595 596 597 598
            if (canBuildGdbHelper) {
                gdbHelperText =  tr("<i>Not yet built.</i>");
            } else {
                gdbHelperText =  tr("<i>Not needed.</i>");
            }
599
        }
600 601
        m_debuggingHelperUi->gdbHelperStatus->setText(gdbHelperText);
        m_debuggingHelperUi->gdbHelperStatus->setTextInteractionFlags(gdbHelperTextFlags);
602
        m_debuggingHelperUi->gdbHelperBuildButton->setEnabled(canBuildGdbHelper && !isBuildingGdbHelper);
603

604
        QString qmlDumpStatusText, qmlDumpStatusToolTip;
605
        Qt::TextInteractionFlags qmlDumpStatusTextFlags = Qt::NoTextInteraction;
606
        if (hasQmlDumper) {
607 608
            qmlDumpStatusText = QDir::toNativeSeparators(version->qmlDumpTool(false));
            const QString debugQmlDumpPath = QDir::toNativeSeparators(version->qmlDumpTool(true));
609
            if (qmlDumpStatusText != debugQmlDumpPath) {
610 611
                if (!qmlDumpStatusText.isEmpty()
                        && !debugQmlDumpPath.isEmpty())
612 613 614
                    qmlDumpStatusText += QLatin1String("\n");
                qmlDumpStatusText += debugQmlDumpPath;
            }
615
            qmlDumpStatusTextFlags = Qt::TextSelectableByMouse;
616
        } else {
617 618 619 620
            if (canBuildQmlDumper) {
                qmlDumpStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDumpStatusText = tr("<i>Cannot be compiled.</i>");
621
                QmlDumpTool::canBuild(version, &qmlDumpStatusToolTip);
622
            }
623
        }
624
        m_debuggingHelperUi->qmlDumpStatus->setText(qmlDumpStatusText);
625
        m_debuggingHelperUi->qmlDumpStatus->setTextInteractionFlags(qmlDumpStatusTextFlags);
626
        m_debuggingHelperUi->qmlDumpStatus->setToolTip(qmlDumpStatusToolTip);
627
        m_debuggingHelperUi->qmlDumpBuildButton->setEnabled(canBuildQmlDumper & !isBuildingQmlDumper);
628

629
        QString qmlDebuggingLibStatusText, qmlDebuggingLibToolTip;
630 631 632 633 634 635 636 637 638 639
        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()) {
640
                    qmlDebuggingLibStatusText += QLatin1Char('\n');
641 642 643 644
                }
                qmlDebuggingLibStatusText += debugPath;
            }
            qmlDebuggingLibStatusTextFlags = Qt::TextSelectableByMouse;
645 646 647 648
        } else {
            if (!needsQmlDebuggingLib) {
                qmlDebuggingLibStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDebuggingLib) {
649 650 651
                qmlDebuggingLibStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDebuggingLibStatusText = tr("<i>Cannot be compiled.</i>");
652
                QmlDebuggingLibrary::canBuild(version, &qmlDebuggingLibToolTip);
653 654 655 656
            }
        }
        m_debuggingHelperUi->qmlDebuggingLibStatus->setText(qmlDebuggingLibStatusText);
        m_debuggingHelperUi->qmlDebuggingLibStatus->setTextInteractionFlags(qmlDebuggingLibStatusTextFlags);
657
        m_debuggingHelperUi->qmlDebuggingLibStatus->setToolTip(qmlDebuggingLibToolTip);
658 659
        m_debuggingHelperUi->qmlDebuggingLibBuildButton->setEnabled(needsQmlDebuggingLib
                                                                    && canBuildQmlDebuggingLib
660 661
                                                                    && !isBuildingQmlDebuggingLib);

662
        QString qmlObserverStatusText, qmlObserverToolTip;
663
        Qt::TextInteractionFlags qmlObserverStatusTextFlags = Qt::NoTextInteraction;
664
        if (hasQmlObserver) {
665
            qmlObserverStatusText = QDir::toNativeSeparators(version->qmlObserverTool());
666
            qmlObserverStatusTextFlags = Qt::TextSelectableByMouse;
667
        }  else {
668 669 670
            if (!needsQmlDebuggingLib) {
                qmlObserverStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlObserver) {
671 672 673
                qmlObserverStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlObserverStatusText = tr("<i>Cannot be compiled.</i>");
674
                QmlObserverTool::canBuild(version, &qmlObserverToolTip);
675
            }
676
        }
677
        m_debuggingHelperUi->qmlObserverStatus->setText(qmlObserverStatusText);
678
        m_debuggingHelperUi->qmlObserverStatus->setTextInteractionFlags(qmlObserverStatusTextFlags);
679
        m_debuggingHelperUi->qmlObserverStatus->setToolTip(qmlObserverToolTip);
680 681
        m_debuggingHelperUi->qmlObserverBuildButton->setEnabled(canBuildQmlObserver
                                                                & !isBuildingQmlObserver);
682 683 684 685

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

686 687 688 689 690 691
        m_debuggingHelperUi->rebuildButton->setEnabled((!isBuildingGdbHelper
                                                        && !isBuildingQmlDumper
                                                        && !isBuildingQmlDebuggingLib
                                                        && !isBuildingQmlObserver)
                                                       && (canBuildGdbHelper
                                                           || canBuildQmlDumper
692
                                                           || (canBuildQmlDebuggingLib && needsQmlDebuggingLib)
693
                                                           || canBuildQmlObserver));
694

695
        m_ui->debuggingHelperWidget->setVisible(true);
696 697 698
    }
}

dt's avatar
dt committed
699 700
// To be called if a qt version was removed or added
void QtOptionsPageWidget::updateCleanUpButton()
701
{
702
    bool hasInvalidVersion = false;
703 704
    for (int i = 0; i < m_versions.count(); ++i) {
        if (!m_versions.at(i)->isValid()) {
705
            hasInvalidVersion = true;
dt's avatar
dt committed
706
            break;
707 708
        }
    }
709
    m_ui->cleanUpButton->setEnabled(hasInvalidVersion);
710
}
711

dt's avatar
dt committed
712
void QtOptionsPageWidget::userChangedCurrentVersion()
con's avatar
con committed
713
{
dt's avatar
dt committed
714 715 716
    updateWidgets();
    updateDescriptionLabel();
    updateDebuggingHelperUi();
717 718
}

dt's avatar
dt committed
719
void QtOptionsPageWidget::qtVersionChanged()
720
{
721
    updateDescriptionLabel();
dt's avatar
dt committed
722
    updateDebuggingHelperUi();
723 724 725 726
}

void QtOptionsPageWidget::updateDescriptionLabel()
{
727 728 729 730 731
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
    const BaseQtVersion *version = currentVersion();
    if (!version) {
        m_versionUi->errorLabel->setText(QString());
    } else if (version->isValid()) {
dt's avatar
dt committed
732 733
        m_versionUi->errorLabel->setText( tr("Qt version %1 for %2").arg(version->qtVersionString(),
                                                                         version->description()));
734 735
        item->setIcon(0, m_validVersionIcon);
    } else {
736
        m_versionUi->errorLabel->setText(version->invalidReason());
737 738
        item->setIcon(0, m_invalidVersionIcon);
    }
739 740
}

741
int QtOptionsPageWidget::indexForTreeItem(const QTreeWidgetItem *item) const
742 743 744
{
    if (!item || !item->parent())
        return -1;
745
    const int uniqueId = item->data(0, VersionIdRole).toInt();
746 747 748 749 750 751 752
    for (int index = 0; index < m_versions.size(); ++index) {
        if (m_versions.at(index)->uniqueId() == uniqueId)
            return index;
    }
    return -1;
}

753 754
QTreeWidgetItem *QtOptionsPageWidget::treeItemForIndex(int index) const
{
755
    const int uniqueId = m_versions.at(index)->uniqueId();
756 757 758 759
    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);
760
            if (item->data(0, VersionIdRole).toInt() == uniqueId) {
761 762 763 764 765 766 767
                return item;
            }
        }
    }
    return 0;
}

dt's avatar
dt committed
768
void QtOptionsPageWidget::versionChanged(QTreeWidgetItem *newItem, QTreeWidgetItem *old)
769
{
dt's avatar
dt committed
770 771
    Q_UNUSED(newItem)
    if (old)
772
        fixQtVersionName(indexForTreeItem(old));
dt's avatar
dt committed
773 774 775 776 777 778 779 780 781 782 783 784 785 786
    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
787
            m_configurationWidget->setEnabled(!version->isAutodetected());
dt's avatar
dt committed
788 789 790
            connect(m_configurationWidget, SIGNAL(changed()),
                    this, SLOT(qtVersionChanged()));
        }
791
    } else {
792
        m_versionUi->nameEdit->clear();
dt's avatar
dt committed
793
        m_versionUi->qmakePath->setText(QString()); // clear()
794 795
    }

dt's avatar
dt committed
796 797 798 799
    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
800
    m_versionUi->editPathPushButton->setEnabled(enabled && !isAutodetected);
801 802
}

803
void QtOptionsPageWidget::updateCurrentQtName()
804 805 806
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
807 808 809
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
810
    m_versions[currentItemIndex]->setDisplayName(m_versionUi->nameEdit->text());
811
    currentItem->setText(0, m_versions[currentItemIndex]->displayName());
812
    updateDescriptionLabel();
813 814 815
}


816
void QtOptionsPageWidget::finish()
817 818
{
    if (QTreeWidgetItem *item = m_ui->qtdirList->currentItem())
819
        fixQtVersionName(indexForTreeItem(item));
820 821 822 823 824 825
}

/* Checks that the qt version name is unique
 * and otherwise changes the name
 *
 */
826
void QtOptionsPageWidget::fixQtVersionName(int index)
827
{
828 829
    if (index < 0)
        return;
830
    int count = m_versions.count();
831
    QString name = m_versions.at(index)->displayName();
dt_'s avatar
dt_ committed
832 833
    if (name.isEmpty())
        return;
834 835
    for (int i = 0; i < count; ++i) {
        if (i != index) {
836
            if (m_versions.at(i)->displayName() == m_versions.at(index)->displayName()) {
837 838 839
                // Same name, find new name
                QRegExp regexp("^(.*)\\((\\d)\\)$");
                if (regexp.exactMatch(name)) {
Tobias Hunger's avatar
Tobias Hunger committed
840
                    // Already in Name (#) format
841 842 843 844
                    name = regexp.cap(1);
                    name += QLatin1Char('(');
                    name += QString::number(regexp.cap(2).toInt() + 1);
                    name += QLatin1Char(')');
845
                } else {
846
                    name +=  QLatin1String(" (2)");
847 848
                }
                // set new name
849
                m_versions[index]->setDisplayName(name);
850
                treeItemForIndex(index)->setText(0, name);
851 852 853 854 855 856 857 858

                // Now check again...
                fixQtVersionName(index);
            }
        }
    }
}

dt's avatar
dt committed
859
QList<BaseQtVersion *> QtOptionsPageWidget::versions() const
860
{
dt's avatar
dt committed
861
    QList<BaseQtVersion *> result;