qtoptionspage.cpp 30.8 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 <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 QtSupport;
using namespace QtSupport::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
    autoItem->setText(0, tr("Auto-detected"));
    autoItem->setFirstColumnSpanned(true);
158
    autoItem->setFlags(Qt::ItemIsEnabled);
159 160 161
    QTreeWidgetItem *manualItem = new QTreeWidgetItem(m_ui->qtdirList);
    manualItem->setText(0, tr("Manual"));
    manualItem->setFirstColumnSpanned(true);
162
    manualItem->setFlags(Qt::ItemIsEnabled);
163

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

174
    connect(m_versionUi->nameEdit, SIGNAL(textEdited(const QString &)),
175 176 177 178 179 180 181 182 183 184
            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 *)));

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

    connect(m_debuggingHelperUi->showLogButton, SIGNAL(clicked()),
197
            this, SLOT(slotShowDebuggingBuildLog()));
198

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

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

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

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

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

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

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

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

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

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

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

284 285
    if (!success)
        showDebuggingBuildLog(item);
286 287
}

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

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

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

338 339
    QTreeWidgetItem *item = treeItemForIndex(index);
    QTC_ASSERT(item, return);
340

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

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

350
    updateDebuggingHelperUi();
351

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

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

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

375 376 377 378 379
void QtOptionsPageWidget::buildQmlDebuggingLibrary()
{
    buildDebuggingHelper(DebuggingHelperBuildTask::QmlDebugging);
}

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

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

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

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

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

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

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

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

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

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

    delete item;

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

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

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

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

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

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

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

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

        m_ui->debuggingHelperWidget->setSummaryText(status);

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

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

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

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

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

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

646
        m_ui->debuggingHelperWidget->setVisible(true);
647 648 649
    }
}

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

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

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

void QtOptionsPageWidget::updateDescriptionLabel()
{
678 679 680 681 682
    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
683 684
        m_versionUi->errorLabel->setText( tr("Qt version %1 for %2").arg(version->qtVersionString(),
                                                                         version->description()));
685 686
        item->setIcon(0, m_validVersionIcon);
    } else {
687
        m_versionUi->errorLabel->setText(version->invalidReason());
688 689
        item->setIcon(0, m_invalidVersionIcon);
    }
690 691
}

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

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

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

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

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


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

/* Checks that the qt version name is unique
 * and otherwise changes the name
 *
 */
776
void QtOptionsPageWidget::fixQtVersionName(int index)
777
{
778 779
    if (index < 0)
        return;
780
    int count = m_versions.count();
781
    QString name = m_versions.at(index)->displayName();
dt_'s avatar
dt_ committed
782 783
    if (name.isEmpty())
        return;
784 785
    for (int i = 0; i < count; ++i) {
        if (i != index) {
786
            if (m_versions.at(i)->displayName() == m_versions.at(index)->displayName()) {
787 788 789
                // Same name, find new name
                QRegExp regexp("^(.*)\\((\\d)\\)$");
                if (regexp.exactMatch(name)) {
Tobias Hunger's avatar
Tobias Hunger committed
790
                    // Already in Name (#) format
791 792 793 794
                    name = regexp.cap(1);
                    name += QLatin1Char('(');
                    name += QString::number(regexp.cap(2).toInt() + 1);
                    name += QLatin1Char(')');
795
                } else {
796
                    name +=  QLatin1String(" (2)");
797 798
                }
                // set new name
799
                m_versions[index]->setDisplayName(name);
800
                treeItemForIndex(index)->setText(0, name);
801 802 803 804 805 806 807 808

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

dt's avatar
dt committed
809
QList<BaseQtVersion *> QtOptionsPageWidget::versions() const
810
{
dt's avatar
dt committed
811
    QList<BaseQtVersion *> result;
812
    for (int i = 0; i < m_versions.count(); ++i)
dt's avatar
dt committed
813
        result.append(m_versions.at(i)->clone());
814
    return result;
815 816
}

817 818 819
QString QtOptionsPageWidget::searchKeywords() const
{
    QString rc;
820
    QLatin1Char sep(' ');
dt's avatar
dt committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834
    QTextStream ts(&rc);
    ts << sep << m_versionUi->versionNameLabel->text()
       << sep << m_versionUi->pathLabel->text()
       << sep << m_debuggingHelperUi->gdbHelperLabel->text()
       << sep << m_debuggingHelperUi->qmlDumpLabel->text()
       << sep << m_debuggingHelperUi->qmlObserverLabel->text();

    // Symbian specific, could be factored out to the factory
    // checking m_configurationWidget is not enough, we want them to be a keyword
    // regardless of which qt versions configuration widget is currently active
    ts << sep << tr("S60 SDK:")
       << sep << tr("SBS v2 directory:");


835 836 837
    rc.remove(QLatin1Char('&'));
    return rc;
}