qtoptionspage.cpp 30.7 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 "qt4projectmanagerconstants.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 <qtconcurrent/runextensions.h>
51 52

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

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

60 61
using namespace Qt4ProjectManager;
using namespace Qt4ProjectManager::Internal;
62

63 64 65 66 67
///
// QtOptionsPage
///

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

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

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

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

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

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

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

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

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

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

121
//-----------------------------------------------------
122 123


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

137 138
    QWidget *versionInfoWidget = new QWidget();
    m_versionUi->setupUi(versionInfoWidget);
139

140 141 142
    QWidget *debuggingHelperDetailsWidget = new QWidget();
    m_debuggingHelperUi->setupUi(debuggingHelperDetailsWidget);

143
    m_ui->setupUi(this);
144 145 146 147 148

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

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

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

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

172
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(const QString &)),
173 174 175 176 177 178 179 180 181 182
            this, SLOT(updateCurrentQtName()));

    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 *)));

183
    connect(m_debuggingHelperUi->rebuildButton, SIGNAL(clicked()),
184
            this, SLOT(buildDebuggingHelper()));
185 186 187 188
    connect(m_debuggingHelperUi->gdbHelperBuildButton, SIGNAL(clicked()),
            this, SLOT(buildGdbHelper()));
    connect(m_debuggingHelperUi->qmlDumpBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDump()));
189 190
    connect(m_debuggingHelperUi->qmlDebuggingLibBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlDebuggingLibrary()));
191 192 193 194
    connect(m_debuggingHelperUi->qmlObserverBuildButton, SIGNAL(clicked()),
            this, SLOT(buildQmlObserver()));

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
195
            this, SLOT(slotShowDebuggingBuildLog()));
196

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

201 202
    connect(QtVersionManager::instance(), SIGNAL(dumpUpdatedFor(QString)),
            this, SLOT(qtVersionsDumpUpdated(QString)));
203 204
}

205 206 207 208 209
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)
210
        return false;
211 212 213 214 215 216 217 218
    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;
219
    const QString tooltip = m_versions.at(index)->toHtml(true);
220 221 222 223 224
    QToolTip::showText(helpEvent->globalPos(), tooltip, m_ui->qtdirList);
    helpEvent->accept();
    return true;
}

225
int QtOptionsPageWidget::currentIndex() const
226
{
227 228 229 230
    if (QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        return indexForTreeItem(currentItem);
    return -1;
}
231

dt's avatar
dt committed
232
BaseQtVersion *QtOptionsPageWidget::currentVersion() const
233 234 235
{
    const int currentItemIndex = currentIndex();
    if (currentItemIndex >= 0 && currentItemIndex < m_versions.size())
236
        return m_versions.at(currentItemIndex);
237 238
    return 0;
}
239

dt's avatar
dt committed
240
static inline int findVersionById(const QList<BaseQtVersion *> &l, int id)
241 242 243
{
    const int size = l.size();
    for (int i = 0; i < size; i++)
244
        if (l.at(i)->uniqueId() == id)
245 246 247
            return i;
    return -1;
}
248

249
// Update with results of terminated helper build
250
void QtOptionsPageWidget::debuggingHelperBuildFinished(int qtVersionId, const QString &output, DebuggingHelperBuildTask::Tools tools)
251
{
252
    const int index = findVersionById(m_versions, qtVersionId);
253 254
    if (index == -1)
        return; // Oops, somebody managed to delete the version
255

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

258 259
    // Update item view
    QTreeWidgetItem *item = treeItemForIndex(index);
260 261 262 263 264
    QTC_ASSERT(item, return);
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags &= ~tools;
    item->setData(0, BuildRunningRole,  QVariant::fromValue(buildFlags));
265
    item->setData(0, BuildLogRole, output);
266

267 268
    bool success = true;
    if (tools & DebuggingHelperBuildTask::GdbDebugging)
dt's avatar
dt committed
269
        success &= version->hasGdbDebuggingHelper();
270
    if (tools & DebuggingHelperBuildTask::QmlDebugging)
dt's avatar
dt committed
271
        success &= version->hasQmlDebuggingLibrary();
272
    if (tools & DebuggingHelperBuildTask::QmlDump)
dt's avatar
dt committed
273
        success &= version->hasQmlDump();
274
    if (tools & DebuggingHelperBuildTask::QmlObserver)
dt's avatar
dt committed
275
        success &= version->hasQmlObserver();
276 277 278

    // Update bottom control if the selection is still the same
    if (index == currentIndex()) {
279
        updateDebuggingHelperUi();
280
    }
281

282 283
    if (!success)
        showDebuggingBuildLog(item);
284 285
}

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

316
void QtOptionsPageWidget::qtVersionsDumpUpdated(const QString &qmakeCommand)
317 318 319 320 321
{
    foreach (BaseQtVersion *version, m_versions) {
        if (version->qmakeCommand() == qmakeCommand)
            version->recheckDumper();
    }
322 323
    if (currentVersion()
            && currentVersion()->qmakeCommand() == qmakeCommand) {
324 325 326 327 328 329
        updateWidgets();
        updateDescriptionLabel();
        updateDebuggingHelperUi();
    }
}

330
void QtOptionsPageWidget::buildDebuggingHelper(DebuggingHelperBuildTask::Tools tools)
331 332 333 334 335
{
    const int index = currentIndex();
    if (index < 0)
        return;

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

339 340 341 342
    DebuggingHelperBuildTask::Tools buildFlags
            = item->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
    buildFlags |= tools;
    item->setData(0, BuildRunningRole, QVariant::fromValue(buildFlags));
343

dt's avatar
dt committed
344
    BaseQtVersion *version = m_versions.at(index);
345 346 347
    if (!version)
        return;

348
    updateDebuggingHelperUi();
349

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

360
    Core::ICore::instance()->progressManager()->addTask(task, taskName,
361
                                                        QLatin1String("Qt4ProjectManager::BuildHelpers"));
362
}
363 364 365 366 367 368 369 370 371 372
void QtOptionsPageWidget::buildGdbHelper()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::GdbDebugging);
}

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

373 374 375 376 377
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

378 379
void QtOptionsPageWidget::buildQmlObserver()
{
380
    DebuggingHelperBuildTask::Tools qmlDbgTools =
Kai Koehne's avatar
Kai Koehne committed
381 382
            DebuggingHelperBuildTask::QmlObserver;
    qmlDbgTools |= DebuggingHelperBuildTask::QmlDebugging;
383
    buildDebuggingHelper(qmlDbgTools);
384
}
385

386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
// 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)
403
{
404 405 406 407
    m_ui.log->setPlainText(text); // Show and scroll to bottom
    m_ui.log->moveCursor(QTextCursor::End);
    m_ui.log->ensureCursorVisible();
}
408

409 410 411 412 413 414 415 416 417
void QtOptionsPageWidget::slotShowDebuggingBuildLog()
{
    if (const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem())
        showDebuggingBuildLog(currentItem);
}

void QtOptionsPageWidget::showDebuggingBuildLog(const QTreeWidgetItem *currentItem)
{
    const int currentItemIndex = indexForTreeItem(currentItem);
418 419
    if (currentItemIndex < 0)
        return;
420 421
    BuildLogDialog *dialog = new BuildLogDialog(this);
    dialog->setWindowTitle(tr("Debugging Helper Build Log for '%1'").arg(currentItem->text(0)));
422
    dialog->setText(currentItem->data(0, BuildLogRole).toString());
423
    dialog->show();
424 425
}

426
QtOptionsPageWidget::~QtOptionsPageWidget()
427 428
{
    delete m_ui;
dt's avatar
dt committed
429 430
    delete m_versionUi;
    delete m_debuggingHelperUi;
dt's avatar
dt committed
431
    delete m_configurationWidget;
432
    qDeleteAll(m_versions);
433 434
}

435
void QtOptionsPageWidget::addQtDir()
436
{
dt's avatar
dt committed
437 438 439 440 441
    QString filter("qmake (");
    foreach (const QString &s, Utils::BuildableHelperLibrary::possibleQMakeCommands()) {
        filter += s + " ";
    }
    filter += ")";
442

dt's avatar
dt committed
443
    QString qtVersion = QFileDialog::getOpenFileName(this,
dt's avatar
dt committed
444 445 446 447 448 449
                                                     tr("Select a qmake executable"), QString(), filter);
    if (qtVersion.isNull())
        return;
    if (QtVersionManager::instance()->qtVersionForQMakeBinary(qtVersion)) {
        // Already exist
    }
450

dt's avatar
dt committed
451 452 453
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
    if (version) {
        m_versions.append(version);
454

dt's avatar
dt committed
455 456 457 458 459 460 461 462 463 464
        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();
465 466
}

467
void QtOptionsPageWidget::removeQtDir()
468 469
{
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
470
    int index = indexForTreeItem(item);
471 472 473 474 475
    if (index < 0)
        return;

    delete item;

dt's avatar
dt committed
476
    BaseQtVersion *version = m_versions.at(index);
477
    m_versions.removeAt(index);
478
    delete version;
dt's avatar
dt committed
479
    updateCleanUpButton();
480 481
}

482
void QtOptionsPageWidget::updateDebuggingHelperUi()
483
{
dt's avatar
dt committed
484
    BaseQtVersion *version = currentVersion();
485
    const QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
486

487
    if (!version || !version->isValid()) {
488
        m_ui->debuggingHelperWidget->setVisible(false);
489
    } else {
490 491 492 493 494
        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;
495

496 497 498
        const bool hasGdbHelper = !version->gdbDebuggingHelperLibrary().isEmpty();
        const bool hasQmlDumper = version->hasQmlDump();
        const bool hasQmlDebuggingLib = version->hasQmlDebuggingLibrary();
499
        const bool needsQmlDebuggingLib = version->needsQmlDebuggingLibrary();
500
        const bool hasQmlObserver = !version->qmlObserverTool().isEmpty();
501

502 503
        bool isBuildingGdbHelper = false;
        bool isBuildingQmlDumper = false;
504
        bool isBuildingQmlDebuggingLib = false;
505 506 507 508 509 510 511
        bool isBuildingQmlObserver = false;

        if (currentItem) {
            DebuggingHelperBuildTask::Tools buildingTools
                    = currentItem->data(0, BuildRunningRole).value<DebuggingHelperBuildTask::Tools>();
            isBuildingGdbHelper = buildingTools & DebuggingHelperBuildTask::GdbDebugging;
            isBuildingQmlDumper = buildingTools & DebuggingHelperBuildTask::QmlDump;
512
            isBuildingQmlDebuggingLib = buildingTools & DebuggingHelperBuildTask::QmlDebugging;
513 514 515
            isBuildingQmlObserver = buildingTools & DebuggingHelperBuildTask::QmlObserver;
        }

516 517 518 519 520 521
        // get names of tools from labels
        QStringList helperNames;
        if (hasGdbHelper)
            helperNames << m_debuggingHelperUi->gdbHelperLabel->text().remove(':');
        if (hasQmlDumper)
            helperNames << m_debuggingHelperUi->qmlDumpLabel->text().remove(':');
522 523
        if (hasQmlDebuggingLib)
            helperNames << m_debuggingHelperUi->qmlDebuggingLibLabel->text().remove(':');
524 525 526 527 528
        if (hasQmlObserver)
            helperNames << m_debuggingHelperUi->qmlObserverLabel->text().remove(':');

        QString status;
        if (helperNames.isEmpty()) {
529
            status = tr("Helpers: None available");
530
        } else {
Friedemann Kleint's avatar
Friedemann Kleint committed
531
            //: %1 is list of tool names.
532
            status = tr("Helpers: %1.").arg(helperNames.join(QLatin1String(", ")));
533 534 535 536
        }

        m_ui->debuggingHelperWidget->setSummaryText(status);

537 538
        QString gdbHelperText;
        Qt::TextInteractionFlags gdbHelperTextFlags = Qt::NoTextInteraction;
539
        if (hasGdbHelper) {
540
            gdbHelperText = QDir::toNativeSeparators(version->gdbDebuggingHelperLibrary());
541
            gdbHelperTextFlags = Qt::TextSelectableByMouse;
542
        } else {
543 544 545 546 547
            if (canBuildGdbHelper) {
                gdbHelperText =  tr("<i>Not yet built.</i>");
            } else {
                gdbHelperText =  tr("<i>Not needed.</i>");
            }
548
        }
549 550
        m_debuggingHelperUi->gdbHelperStatus->setText(gdbHelperText);
        m_debuggingHelperUi->gdbHelperStatus->setTextInteractionFlags(gdbHelperTextFlags);
551
        m_debuggingHelperUi->gdbHelperBuildButton->setEnabled(canBuildGdbHelper && !isBuildingGdbHelper);
552

553
        QString qmlDumpStatusText, qmlDumpStatusToolTip;
554
        Qt::TextInteractionFlags qmlDumpStatusTextFlags = Qt::NoTextInteraction;
555
        if (hasQmlDumper) {
556 557
            qmlDumpStatusText = QDir::toNativeSeparators(version->qmlDumpTool(false));
            const QString debugQmlDumpPath = QDir::toNativeSeparators(version->qmlDumpTool(true));
558
            if (qmlDumpStatusText != debugQmlDumpPath) {
559 560
                if (!qmlDumpStatusText.isEmpty()
                        && !debugQmlDumpPath.isEmpty())
561 562 563
                    qmlDumpStatusText += QLatin1String("\n");
                qmlDumpStatusText += debugQmlDumpPath;
            }
564
            qmlDumpStatusTextFlags = Qt::TextSelectableByMouse;
565
        } else {
566 567 568 569
            if (canBuildQmlDumper) {
                qmlDumpStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDumpStatusText = tr("<i>Cannot be compiled.</i>");
570
                QmlDumpTool::canBuild(version, &qmlDumpStatusToolTip);
571
            }
572
        }
573
        m_debuggingHelperUi->qmlDumpStatus->setText(qmlDumpStatusText);
574
        m_debuggingHelperUi->qmlDumpStatus->setTextInteractionFlags(qmlDumpStatusTextFlags);
575
        m_debuggingHelperUi->qmlDumpStatus->setToolTip(qmlDumpStatusToolTip);
576
        m_debuggingHelperUi->qmlDumpBuildButton->setEnabled(canBuildQmlDumper & !isBuildingQmlDumper);
577

578
        QString qmlDebuggingLibStatusText, qmlDebuggingLibToolTip;
579 580 581 582 583 584 585 586 587 588
        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()) {
589
                    qmlDebuggingLibStatusText += QLatin1Char('\n');
590 591 592 593
                }
                qmlDebuggingLibStatusText += debugPath;
            }
            qmlDebuggingLibStatusTextFlags = Qt::TextSelectableByMouse;
594 595 596 597
        } else {
            if (!needsQmlDebuggingLib) {
                qmlDebuggingLibStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlDebuggingLib) {
598 599 600
                qmlDebuggingLibStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlDebuggingLibStatusText = tr("<i>Cannot be compiled.</i>");
601
                QmlDebuggingLibrary::canBuild(version, &qmlDebuggingLibToolTip);
602 603 604 605
            }
        }
        m_debuggingHelperUi->qmlDebuggingLibStatus->setText(qmlDebuggingLibStatusText);
        m_debuggingHelperUi->qmlDebuggingLibStatus->setTextInteractionFlags(qmlDebuggingLibStatusTextFlags);
606
        m_debuggingHelperUi->qmlDebuggingLibStatus->setToolTip(qmlDebuggingLibToolTip);
607 608
        m_debuggingHelperUi->qmlDebuggingLibBuildButton->setEnabled(needsQmlDebuggingLib
                                                                    && canBuildQmlDebuggingLib
609 610
                                                                    && !isBuildingQmlDebuggingLib);

611
        QString qmlObserverStatusText, qmlObserverToolTip;
612
        Qt::TextInteractionFlags qmlObserverStatusTextFlags = Qt::NoTextInteraction;
613
        if (hasQmlObserver) {
614
            qmlObserverStatusText = QDir::toNativeSeparators(version->qmlObserverTool());
615
            qmlObserverStatusTextFlags = Qt::TextSelectableByMouse;
616
        }  else {
617 618 619
            if (!needsQmlDebuggingLib) {
                qmlObserverStatusText = tr("<i>Not needed.</i>");
            } else if (canBuildQmlObserver) {
620 621 622
                qmlObserverStatusText = tr("<i>Not yet built.</i>");
            } else {
                qmlObserverStatusText = tr("<i>Cannot be compiled.</i>");
623
                QmlObserverTool::canBuild(version, &qmlObserverToolTip);
624
            }
625
        }
626
        m_debuggingHelperUi->qmlObserverStatus->setText(qmlObserverStatusText);
627
        m_debuggingHelperUi->qmlObserverStatus->setTextInteractionFlags(qmlObserverStatusTextFlags);
628
        m_debuggingHelperUi->qmlObserverStatus->setToolTip(qmlObserverToolTip);
629 630
        m_debuggingHelperUi->qmlObserverBuildButton->setEnabled(canBuildQmlObserver
                                                                & !isBuildingQmlObserver);
631 632 633 634

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

635 636 637 638 639 640
        m_debuggingHelperUi->rebuildButton->setEnabled((!isBuildingGdbHelper
                                                        && !isBuildingQmlDumper
                                                        && !isBuildingQmlDebuggingLib
                                                        && !isBuildingQmlObserver)
                                                       && (canBuildGdbHelper
                                                           || canBuildQmlDumper
641
                                                           || (canBuildQmlDebuggingLib && needsQmlDebuggingLib)
642
                                                           || canBuildQmlObserver));
643

644
        m_ui->debuggingHelperWidget->setVisible(true);
645 646 647
    }
}

dt's avatar
dt committed
648 649
// To be called if a qt version was removed or added
void QtOptionsPageWidget::updateCleanUpButton()
650
{
651
    bool hasInvalidVersion = false;
652 653
    for (int i = 0; i < m_versions.count(); ++i) {
        if (!m_versions.at(i)->isValid()) {
654
            hasInvalidVersion = true;
dt's avatar
dt committed
655
            break;
656 657
        }
    }
658
    m_ui->cleanUpButton->setEnabled(hasInvalidVersion);
659
}
660

dt's avatar
dt committed
661
void QtOptionsPageWidget::userChangedCurrentVersion()
con's avatar
con committed
662
{
dt's avatar
dt committed
663 664 665
    updateWidgets();
    updateDescriptionLabel();
    updateDebuggingHelperUi();
666 667
}

dt's avatar
dt committed
668
void QtOptionsPageWidget::qtVersionChanged()
669
{
670
    updateDescriptionLabel();
dt's avatar
dt committed
671
    updateDebuggingHelperUi();
672 673 674 675
}

void QtOptionsPageWidget::updateDescriptionLabel()
{
dt's avatar
dt committed
676
    BaseQtVersion *version = currentVersion();
677 678 679
    if (!version)
        m_versionUi->errorLabel->setText("");
    else if (version->isValid())
dt's avatar
dt committed
680 681
        m_versionUi->errorLabel->setText( tr("Qt version %1 for %2").arg(version->qtVersionString(),
                                                                         version->description()));
682 683
    else
        m_versionUi->errorLabel->setText(version->invalidReason());
dt_'s avatar
dt_ committed
684 685 686
    QTreeWidgetItem *item = m_ui->qtdirList->currentItem();
    if (item)
        item->setIcon(0, version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
687 688
}

689
int QtOptionsPageWidget::indexForTreeItem(const QTreeWidgetItem *item) const
690 691 692
{
    if (!item || !item->parent())
        return -1;
693
    const int uniqueId = item->data(0, VersionIdRole).toInt();
694 695 696 697 698 699 700
    for (int index = 0; index < m_versions.size(); ++index) {
        if (m_versions.at(index)->uniqueId() == uniqueId)
            return index;
    }
    return -1;
}

701 702
QTreeWidgetItem *QtOptionsPageWidget::treeItemForIndex(int index) const
{
703
    const int uniqueId = m_versions.at(index)->uniqueId();
704 705 706 707
    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);
708
            if (item->data(0, VersionIdRole).toInt() == uniqueId) {
709 710 711 712 713 714 715
                return item;
            }
        }
    }
    return 0;
}

dt's avatar
dt committed
716
void QtOptionsPageWidget::versionChanged(QTreeWidgetItem *newItem, QTreeWidgetItem *old)
717
{
dt's avatar
dt committed
718 719
    Q_UNUSED(newItem)
    if (old)
720
        fixQtVersionName(indexForTreeItem(old));
dt's avatar
dt committed
721 722 723 724 725 726 727 728 729 730 731 732 733 734
    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
735
            m_configurationWidget->setEnabled(!version->isAutodetected());
dt's avatar
dt committed
736 737 738
            connect(m_configurationWidget, SIGNAL(changed()),
                    this, SLOT(qtVersionChanged()));
        }
739
    } else {
740
        m_versionUi->nameEdit->clear();
dt's avatar
dt committed
741
        m_versionUi->qmakePath->setText(QString()); // clear()
742 743
    }

dt's avatar
dt committed
744 745 746 747
    const bool enabled = version != 0;
    const bool isAutodetected = enabled && version->isAutodetected();
    m_ui->delButton->setEnabled(enabled && !isAutodetected);
    m_versionUi->nameEdit->setEnabled(enabled && !isAutodetected);
748 749
}

750
void QtOptionsPageWidget::updateCurrentQtName()
751 752 753
{
    QTreeWidgetItem *currentItem = m_ui->qtdirList->currentItem();
    Q_ASSERT(currentItem);
754 755 756
    int currentItemIndex = indexForTreeItem(currentItem);
    if (currentItemIndex < 0)
        return;
757
    m_versions[currentItemIndex]->setDisplayName(m_versionUi->nameEdit->text());
758
    currentItem->setText(0, m_versions[currentItemIndex]->displayName());
759
    updateDescriptionLabel();
760 761 762
}


763
void QtOptionsPageWidget::finish()
764 765
{
    if (QTreeWidgetItem *item = m_ui->qtdirList->currentItem())
766
        fixQtVersionName(indexForTreeItem(item));
767 768 769 770 771 772
}

/* Checks that the qt version name is unique
 * and otherwise changes the name
 *
 */
773
void QtOptionsPageWidget::fixQtVersionName(int index)
774
{
775 776
    if (index < 0)
        return;
777
    int count = m_versions.count();
778
    QString name = m_versions.at(index)->displayName();
dt_'s avatar
dt_ committed
779 780
    if (name.isEmpty())
        return;
781 782
    for (int i = 0; i < count; ++i) {
        if (i != index) {
783
            if (m_versions.at(i)->displayName() == m_versions.at(index)->displayName()) {
784 785 786
                // Same name, find new name
                QRegExp regexp("^(.*)\\((\\d)\\)$");
                if (regexp.exactMatch(name)) {
Tobias Hunger's avatar
Tobias Hunger committed
787
                    // Already in Name (#) format
788 789 790 791
                    name = regexp.cap(1);
                    name += QLatin1Char('(');
                    name += QString::number(regexp.cap(2).toInt() + 1);
                    name += QLatin1Char(')');