qtoptionspage.cpp 24.7 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
Tobias Hunger's avatar
Tobias Hunger committed
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
Tobias Hunger's avatar
Tobias Hunger committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
Tobias Hunger's avatar
Tobias Hunger committed
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
Tobias Hunger's avatar
Tobias Hunger committed
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
Tobias Hunger's avatar
Tobias Hunger committed
25

26
#include "qtoptionspage.h"
27
#include "qtconfigwidget.h"
28 29
#include "ui_showbuildlog.h"
#include "ui_qtversionmanager.h"
30
#include "ui_qtversioninfo.h"
31
#include "qtsupportconstants.h"
32
#include "qtversionmanager.h"
dt's avatar
dt committed
33
#include "qtversionfactory.h"
34 35

#include <coreplugin/progressmanager/progressmanager.h>
36
#include <coreplugin/coreconstants.h>
hjk's avatar
hjk committed
37
#include <coreplugin/variablechooser.h>
38
#include <projectexplorer/toolchain.h>
39
#include <projectexplorer/toolchainmanager.h>
40
#include <projectexplorer/projectexplorerconstants.h>
41
#include <utils/buildablehelperlibrary.h>
42
#include <utils/hostosinfo.h>
hjk's avatar
hjk committed
43 44
#include <utils/pathchooser.h>
#include <utils/qtcassert.h>
45
#include <utils/runextensions.h>
46
#include <utils/algorithm.h>
47
#include <utils/treemodel.h>
Ulf Hermann's avatar
Ulf Hermann committed
48
#include <utils/utilsicons.h>
49

50 51 52
#include <QDir>
#include <QMessageBox>
#include <QFileDialog>
53 54
#include <QTextBrowser>
#include <QDesktopServices>
55 56 57
#include <QSortFilterProxyModel>

#include <utility>
58

hjk's avatar
hjk committed
59
using namespace ProjectExplorer;
60
using namespace Utils;
61

hjk's avatar
hjk committed
62 63 64
namespace QtSupport {
namespace Internal {

65 66 67
class QtVersionItem : public TreeItem
{
public:
hjk's avatar
hjk committed
68 69
    explicit QtVersionItem(BaseQtVersion *version)
        : m_version(version)
70 71 72 73
    {}

    ~QtVersionItem()
    {
Daniel Teske's avatar
Daniel Teske committed
74
        delete m_version;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    }

    void setVersion(BaseQtVersion *version)
    {
        m_version = version;
        update();
    }

    int uniqueId() const
    {
        return m_version ? m_version->uniqueId() : -1;
    }

    BaseQtVersion *version() const
    {
        return m_version;
    }

    QVariant data(int column, int role) const
    {
        if (!m_version)
            return TreeItem::data(column, role);

        if (role == Qt::DisplayRole) {
            if (column == 0)
                return m_version->displayName();
            if (column == 1)
                return m_version->qmakeCommand().toUserOutput();
        }

105 106 107 108 109 110
        if (role == Qt::FontRole && m_changed) {
            QFont font;
            font.setBold(true);
            return font;
         }

111 112 113 114 115 116 117 118
        if (role == Qt::DecorationRole && column == 0)
            return m_icon;

        return QVariant();
    }

    void setIcon(const QIcon &icon)
    {
119 120
        if (m_icon.cacheKey() == icon.cacheKey())
            return;
121 122 123 124 125 126 127 128 129 130 131 132 133 134
        m_icon = icon;
        update();
    }

    QString buildLog() const
    {
        return m_buildLog;
    }

    void setBuildLog(const QString &buildLog)
    {
        m_buildLog = buildLog;
    }

135 136 137 138 139 140 141 142
    void setChanged(bool changed)
    {
        if (changed == m_changed)
            return;
        m_changed = changed;
        update();
    }

143
private:
Daniel Teske's avatar
Daniel Teske committed
144
    BaseQtVersion *m_version = 0;
145 146
    QIcon m_icon;
    QString m_buildLog;
147
    bool m_changed = false;
148
};
hjk's avatar
hjk committed
149

150 151 152 153 154
///
// QtOptionsPage
///

QtOptionsPage::QtOptionsPage()
dt's avatar
dt committed
155
    : m_widget(0)
156
{
hjk's avatar
hjk committed
157
    setId(Constants::QTVERSION_SETTINGS_PAGE_ID);
158
    setDisplayName(QCoreApplication::translate("QtSupport", Constants::QTVERSION_SETTINGS_PAGE_NAME));
hjk's avatar
hjk committed
159
    setCategory(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
160 161
    setDisplayCategory(QCoreApplication::translate("ProjectExplorer",
        ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY));
162
    setCategoryIcon(Utils::Icon(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
163 164
}

165
QWidget *QtOptionsPage::widget()
166
{
167 168
    if (!m_widget)
        m_widget = new QtOptionsPageWidget;
169 170 171 172 173
    return m_widget;
}

void QtOptionsPage::apply()
{
174 175
    if (!m_widget) // page was never shown
        return;
176
    m_widget->apply();
177 178
}

179
void QtOptionsPage::finish()
180
{
181
    delete m_widget;
182 183
}

184
//-----------------------------------------------------
185 186


187
QtOptionsPageWidget::QtOptionsPageWidget(QWidget *parent)
188 189
    : QWidget(parent)
    , m_specifyNameString(tr("<specify a name>"))
190
    , m_ui(new Internal::Ui::QtVersionManager())
191
    , m_versionUi(new Internal::Ui::QtVersionInfo())
192
    , m_infoBrowser(new QTextBrowser)
193
    , m_invalidVersionIcon(Utils::Icons::CRITICAL.icon())
Ulf Hermann's avatar
Ulf Hermann committed
194
    , m_warningVersionIcon(Utils::Icons::WARNING.icon())
dt's avatar
dt committed
195
    , m_configurationWidget(0)
196
{
197 198
    QWidget *versionInfoWidget = new QWidget();
    m_versionUi->setupUi(versionInfoWidget);
199
    m_versionUi->editPathPushButton->setText(PathChooser::browseButtonLabel());
200

201
    m_ui->setupUi(this);
202

203 204
    m_infoBrowser->setOpenLinks(false);
    m_infoBrowser->setTextInteractionFlags(Qt::TextBrowserInteraction);
205 206
    connect(m_infoBrowser, &QTextBrowser::anchorClicked,
            this, &QtOptionsPageWidget::infoAnchorClicked);
207
    m_ui->infoWidget->setWidget(m_infoBrowser);
208 209
    connect(m_ui->infoWidget, &DetailsWidget::expanded,
            this, &QtOptionsPageWidget::setInfoWidgetVisibility);
210

211
    m_ui->versionInfoWidget->setWidget(versionInfoWidget);
hjk's avatar
hjk committed
212
    m_ui->versionInfoWidget->setState(DetailsWidget::NoSummary);
213

214 215
    m_autoItem = new StaticTreeItem(tr("Auto-detected"));
    m_manualItem = new StaticTreeItem(tr("Manual"));
216

hjk's avatar
hjk committed
217
    m_model = new TreeModel<Utils::TreeItem, Utils::TreeItem, QtVersionItem>();
218
    m_model->setHeader({tr("Name"), tr("qmake Location")});
hjk's avatar
hjk committed
219 220 221
    m_model->rootItem()->appendChild(m_autoItem);
    m_model->rootItem()->appendChild(m_manualItem);

222 223 224 225 226 227 228 229 230 231
    m_filterModel = new QSortFilterProxyModel(this);
    m_filterModel->setSourceModel(m_model);
    m_filterModel->setSortCaseSensitivity(Qt::CaseInsensitive);

    m_ui->qtdirList->setModel(m_filterModel);
    m_ui->qtdirList->setSortingEnabled(true);

    m_ui->qtdirList->setFirstColumnSpanned(0, QModelIndex(), true);
    m_ui->qtdirList->setFirstColumnSpanned(1, QModelIndex(), true);

dt_'s avatar
dt_ committed
232
    m_ui->qtdirList->header()->setStretchLastSection(false);
233 234
    m_ui->qtdirList->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
    m_ui->qtdirList->header()->setSectionResizeMode(1, QHeaderView::Stretch);
235
    m_ui->qtdirList->setTextElideMode(Qt::ElideMiddle);
236
    m_ui->qtdirList->sortByColumn(0, Qt::AscendingOrder);
237

238
    QList<int> additions = transform(QtVersionManager::versions(), &BaseQtVersion::uniqueId);
239 240

    updateQtVersions(additions, QList<int>(), QList<int>());
241 242

    m_ui->qtdirList->expandAll();
243

244 245
    connect(m_versionUi->nameEdit, &QLineEdit::textEdited,
            this, &QtOptionsPageWidget::updateCurrentQtName);
246

247 248
    connect(m_versionUi->editPathPushButton, &QAbstractButton::clicked,
            this, &QtOptionsPageWidget::editPath);
dt_'s avatar
dt_ committed
249

250 251 252 253
    connect(m_ui->addButton, &QAbstractButton::clicked,
            this, &QtOptionsPageWidget::addQtDir);
    connect(m_ui->delButton, &QAbstractButton::clicked,
            this, &QtOptionsPageWidget::removeQtDir);
254

255
    connect(m_ui->qtdirList->selectionModel(), &QItemSelectionModel::currentChanged,
256
            this, &QtOptionsPageWidget::versionChanged);
257

258 259
    connect(m_ui->cleanUpButton, &QAbstractButton::clicked,
            this, &QtOptionsPageWidget::cleanUpQtVersions);
dt's avatar
dt committed
260 261
    userChangedCurrentVersion();
    updateCleanUpButton();
262

263 264
    connect(QtVersionManager::instance(), &QtVersionManager::dumpUpdatedFor,
            this, &QtOptionsPageWidget::qtVersionsDumpUpdated);
265

266 267
    connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
            this, &QtOptionsPageWidget::updateQtVersions);
268

269 270
    connect(ProjectExplorer::ToolChainManager::instance(), &ToolChainManager::toolChainsChanged,
            this, &QtOptionsPageWidget::toolChainsUpdated);
hjk's avatar
hjk committed
271 272

    auto chooser = new Core::VariableChooser(this);
273
    chooser->addSupportedWidget(m_versionUi->nameEdit, "Qt:Name");
hjk's avatar
hjk committed
274 275 276 277 278
    chooser->addMacroExpanderProvider(
        [this]() -> Utils::MacroExpander * {
            BaseQtVersion *version = currentVersion();
            return version ? version->macroExpander() : 0;
        });
279 280
}

dt's avatar
dt committed
281
BaseQtVersion *QtOptionsPageWidget::currentVersion() const
282
{
283 284 285 286
    QtVersionItem *item = currentItem();
    if (!item)
        return 0;
    return item->version();
287
}
288

289
QtVersionItem *QtOptionsPageWidget::currentItem() const
290
{
291 292
    QModelIndex idx = m_ui->qtdirList->selectionModel()->currentIndex();
    QModelIndex sourceIdx = m_filterModel->mapToSource(idx);
hjk's avatar
hjk committed
293
    return m_model->itemForIndexAtLevel<2>(sourceIdx);
294
}
295

296 297
void QtOptionsPageWidget::cleanUpQtVersions()
{
298 299 300
    QVector<QtVersionItem *> toRemove;
    QString text;

hjk's avatar
hjk committed
301
    for (TreeItem *child : *m_manualItem) {
302 303 304 305 306 307 308
        auto item = static_cast<QtVersionItem *>(child);
        if (item->version() && !item->version()->isValid()) {
            toRemove.append(item);
            if (!text.isEmpty())
                text.append(QLatin1String("</li><li>"));
            text.append(item->version()->displayName());
        }
309 310 311 312 313
    }

    if (toRemove.isEmpty())
        return;

314

Friedemann Kleint's avatar
Friedemann Kleint committed
315
    if (QMessageBox::warning(0, tr("Remove Invalid Qt Versions"),
316 317
                             tr("Do you want to remove all invalid Qt Versions?<br>"
                                "<ul><li>%1</li></ul><br>"
318
                                "will be removed.").arg(text),
319 320 321
                             QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
        return;

322 323
    foreach (QtVersionItem *item, toRemove)
        m_model->destroyItem(item);
324

dt's avatar
dt committed
325
    updateCleanUpButton();
326 327
}

328 329
void QtOptionsPageWidget::toolChainsUpdated()
{
hjk's avatar
hjk committed
330
    m_model->forItemsAtLevel<2>([this](QtVersionItem *item) {
331 332 333 334 335
        if (item == currentItem())
            updateDescriptionLabel();
        else
            updateVersionItem(item);
    });
336 337
}

hjk's avatar
hjk committed
338
void QtOptionsPageWidget::qtVersionsDumpUpdated(const FileName &qmakeCommand)
339
{
340
    m_model->forItemsAtLevel<2>([qmakeCommand](QtVersionItem *item) {
341 342 343
        if (item->version()->qmakeCommand() == qmakeCommand)
            item->version()->recheckDumper();
    });
344

345
    if (currentVersion() && currentVersion()->qmakeCommand() == qmakeCommand) {
346 347 348 349 350
        updateWidgets();
        updateDescriptionLabel();
    }
}

351
void QtOptionsPageWidget::setInfoWidgetVisibility()
352
{
353 354
    m_ui->versionInfoWidget->setVisible(m_ui->infoWidget->state() == DetailsWidget::Collapsed);
    m_ui->infoWidget->setVisible(true);
355 356
}

357 358 359 360 361
void QtOptionsPageWidget::infoAnchorClicked(const QUrl &url)
{
    QDesktopServices::openUrl(url);
}

362 363 364 365 366 367 368 369 370 371 372 373
static QString formatAbiHtmlList(const QList<Abi> &abis)
{
    QString result = QStringLiteral("<ul><li>");
    for (int i = 0, count = abis.size(); i < count; ++i) {
        if (i)
            result += QStringLiteral("</li><li>");
        result += abis.at(i).toString();
    }
    result += QStringLiteral("</li></ul>");
    return result;
}

374 375 376 377 378 379 380 381
QtOptionsPageWidget::ValidityInfo QtOptionsPageWidget::validInformation(const BaseQtVersion *version)
{
    ValidityInfo info;
    info.icon = m_validVersionIcon;

    if (!version)
        return info;

382
    info.description = tr("Qt version %1 for %2").arg(version->qtVersionString(), version->description());
383 384 385 386 387 388 389
    if (!version->isValid()) {
        info.icon = m_invalidVersionIcon;
        info.message = version->invalidReason();
        return info;
    }

    // Do we have tool chain issues?
390 391
    QList<Abi> missingToolChains;
    const QList<Abi> qtAbis = version->qtAbis();
392

393
    for (const Abi &abi : qtAbis) {
394 395 396 397 398 399 400
        const auto abiCompatePred = [&abi] (const ToolChain *tc)
        {
            return Utils::contains(tc->supportedAbis(),
                                   [&abi](const Abi &sabi) { return sabi.isCompatibleWith(abi); });
        };

        if (!ToolChainManager::toolChain(abiCompatePred))
401
            missingToolChains.append(abi);
402 403
    }

404
    bool useable = true;
405
    QStringList warnings;
406 407 408
    if (!isNameUnique(version))
        warnings << tr("Display Name is not unique.");

409
    if (!missingToolChains.isEmpty()) {
410
        if (missingToolChains.count() == qtAbis.size()) {
411
            // Yes, this Qt version can't be used at all!
412 413 414
            info.message =
                tr("No compiler can produce code for this Qt version."
                   " Please define one or more compilers for: %1").arg(formatAbiHtmlList(qtAbis));
415 416 417 418
            info.icon = m_invalidVersionIcon;
            useable = false;
        } else {
            // Yes, some ABIs are unsupported
419
            warnings << tr("Not all possible target environments can be supported due to missing compilers.");
420 421
            info.toolTip = tr("The following ABIs are currently not supported: %1")
                    .arg(formatAbiHtmlList(missingToolChains));
422 423
            info.icon = m_warningVersionIcon;
        }
424
    }
425 426

    if (useable) {
427 428
        warnings += version->warningReason();
        if (!warnings.isEmpty()) {
hjk's avatar
hjk committed
429
            info.message = warnings.join(QLatin1Char('\n'));
430 431 432 433
            info.icon = m_warningVersionIcon;
        }
    }

434 435 436
    return info;
}

hjk's avatar
hjk committed
437
QList<ToolChain*> QtOptionsPageWidget::toolChains(const BaseQtVersion *version)
438
{
439
    QList<ToolChain*> toolChains;
440
    if (!version)
441 442
        return toolChains;

443
    QSet<QByteArray> ids;
444 445 446 447 448 449 450 451
    foreach (const Abi &a, version->qtAbis()) {
        foreach (ToolChain *tc, ToolChainManager::findToolChains(a)) {
            if (ids.contains(tc->id()))
                continue;
            ids.insert(tc->id());
            toolChains.append(tc);
        }
    }
452

453
    return toolChains;
454 455
}

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

464 465 466
bool QtOptionsPageWidget::isNameUnique(const BaseQtVersion *version)
{
    const QString name = version->displayName().trimmed();
467

hjk's avatar
hjk committed
468
    return !m_model->findItemAtLevel<2>([name, version](QtVersionItem *item) {
469 470 471
        BaseQtVersion *v = item->version();
        return v != version && v->displayName().trimmed() == name;
    });
472 473
}

474
void QtOptionsPageWidget::updateVersionItem(QtVersionItem *item)
475
{
476 477 478 479 480 481 482 483
    if (!item)
        return;
    if (!item->version())
        return;

    const ValidityInfo info = validInformation(item->version());
    item->update();
    item->setIcon(info.icon);
484 485
}

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

509
void QtOptionsPageWidget::showDebuggingBuildLog(const QtVersionItem *item)
510
{
511 512
    BaseQtVersion *version = item->version();
    if (!version)
513
        return;
514
    BuildLogDialog *dialog = new BuildLogDialog(this->window());
515 516
    dialog->setWindowTitle(tr("Debugging Helper Build Log for \"%1\"").arg(version->displayName()));
    dialog->setText(item->buildLog());
517
    dialog->show();
518 519
}

520 521 522
void QtOptionsPageWidget::updateQtVersions(const QList<int> &additions, const QList<int> &removals,
                                           const QList<int> &changes)
{
523
    QList<QtVersionItem *> toRemove;
524 525 526
    QList<int> toAdd = additions;

    // Find existing items to remove/change:
hjk's avatar
hjk committed
527
    m_model->forItemsAtLevel<2>([&](QtVersionItem *item) {
528
        int id = item->uniqueId();
529 530
        if (removals.contains(id)) {
            toRemove.append(item);
531
        } else if (changes.contains(id)) {
532 533 534
            toAdd.append(id);
            toRemove.append(item);
        }
535
    });
536 537

    // Remove changed/removed items:
538 539
    foreach (QtVersionItem *item, toRemove)
        m_model->destroyItem(item);
540 541 542

    // Add changed/added items:
    foreach (int a, toAdd) {
hjk's avatar
hjk committed
543
        BaseQtVersion *version = QtVersionManager::version(a)->clone();
544
        auto *item = new QtVersionItem(version);
545 546

        // Insert in the right place:
547 548
        Utils::TreeItem *parent = version->isAutodetected()? m_autoItem : m_manualItem;
        parent->appendChild(item);
549
    }
550

hjk's avatar
hjk committed
551
    m_model->forItemsAtLevel<2>([this](QtVersionItem *item) { updateVersionItem(item); });
552 553
}

554
QtOptionsPageWidget::~QtOptionsPageWidget()
555 556
{
    delete m_ui;
dt's avatar
dt committed
557
    delete m_versionUi;
dt's avatar
dt committed
558
    delete m_configurationWidget;
559 560
}

dt_'s avatar
dt_ committed
561 562
void QtOptionsPageWidget::addQtDir()
{
hjk's avatar
hjk committed
563
    FileName qtVersion = FileName::fromString(
Tobias Hunger's avatar
Tobias Hunger committed
564
                QFileDialog::getOpenFileName(this,
Friedemann Kleint's avatar
Friedemann Kleint committed
565
                                             tr("Select a qmake Executable"),
Tobias Hunger's avatar
Tobias Hunger committed
566
                                             QString(),
567
                                             BuildableHelperLibrary::filterForQmakeFileDialog(),
Tobias Hunger's avatar
Tobias Hunger committed
568 569
                                             0,
                                             QFileDialog::DontResolveSymlinks));
dt's avatar
dt committed
570 571
    if (qtVersion.isNull())
        return;
572

573
    QFileInfo fi = qtVersion.toFileInfo();
574 575
    // should add all qt versions here ?
    if (BuildableHelperLibrary::isQtChooser(fi))
hjk's avatar
hjk committed
576
        qtVersion = FileName::fromString(BuildableHelperLibrary::qtChooserToQmakePath(fi.symLinkTarget()));
577

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
    auto checkAlreadyExists = [qtVersion](Utils::TreeItem *parent) {
        for (int i = 0; i < parent->childCount(); ++i) {
            auto item = static_cast<QtVersionItem *>(parent->childAt(i));
            if (item->version()->qmakeCommand() == qtVersion) {
                return std::make_pair(true, item->version()->displayName());
            }
        }
        return std::make_pair(false, QString());
    };

    bool alreadyExists;
    QString otherName;
    std::tie(alreadyExists, otherName) = checkAlreadyExists(m_autoItem);
    if (!alreadyExists)
        std::tie(alreadyExists, otherName) = checkAlreadyExists(m_manualItem);

    if (alreadyExists) {
dt's avatar
dt committed
595
        // Already exist
Friedemann Kleint's avatar
Friedemann Kleint committed
596
        QMessageBox::warning(this, tr("Qt Version Already Known"),
597
                             tr("This Qt version was already registered as \"%1\".")
598
                             .arg(otherName));
599
        return;
dt's avatar
dt committed
600
    }
601

602
    QString error;
603
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion, false, QString(), &error);
dt's avatar
dt committed
604
    if (version) {
605 606 607 608 609
        auto item = new QtVersionItem(version);
        item->setIcon(version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
        m_manualItem->appendChild(item);
        QModelIndex source = m_model->indexForItem(item);
        m_ui->qtdirList->setCurrentIndex(m_filterModel->mapFromSource(source)); // should update the rest of the ui
dt's avatar
dt committed
610 611
        m_versionUi->nameEdit->setFocus();
        m_versionUi->nameEdit->selectAll();
612
    } else {
613
        QMessageBox::warning(this, tr("Qmake Not Executable"),
Friedemann Kleint's avatar
Friedemann Kleint committed
614
                             tr("The qmake executable %1 could not be added: %2").arg(qtVersion.toUserOutput()).arg(error));
615
        return;
dt's avatar
dt committed
616 617
    }
    updateCleanUpButton();
618 619
}

620
void QtOptionsPageWidget::removeQtDir()
621
{
622 623
    QtVersionItem *item = currentItem();
    if (!item)
624 625
        return;

626
    m_model->destroyItem(item);
627

dt's avatar
dt committed
628
    updateCleanUpButton();
629 630
}

dt_'s avatar
dt_ committed
631 632
void QtOptionsPageWidget::editPath()
{
633
    BaseQtVersion *current = currentVersion();
634
    QString dir = currentVersion()->qmakeCommand().toFileInfo().absolutePath();
hjk's avatar
hjk committed
635
    FileName qtVersion = FileName::fromString(
636
                QFileDialog::getOpenFileName(this,
Robert Loehning's avatar
Robert Loehning committed
637
                                             tr("Select a qmake Executable"),
638
                                             dir,
639
                                             BuildableHelperLibrary::filterForQmakeFileDialog(),
640 641
                                             0,
                                             QFileDialog::DontResolveSymlinks));
dt_'s avatar
dt_ committed
642 643 644
    if (qtVersion.isNull())
        return;
    BaseQtVersion *version = QtVersionFactory::createQtVersionFromQMakePath(qtVersion);
Daniel Teske's avatar
Daniel Teske committed
645 646
    if (!version)
        return;
dt_'s avatar
dt_ committed
647 648 649
    // Same type? then replace!
    if (current->type() != version->type()) {
        // not the same type, error out
Friedemann Kleint's avatar
Friedemann Kleint committed
650
        QMessageBox::critical(this, tr("Incompatible Qt Versions"),
Friedemann Kleint's avatar
Friedemann Kleint committed
651
                              tr("The Qt version selected must match the device type."),
dt_'s avatar
dt_ committed
652 653 654 655 656 657
                              QMessageBox::Ok);
        delete version;
        return;
    }
    // same type, replace
    version->setId(current->uniqueId());
658 659
    if (current->unexpandedDisplayName() != current->defaultUnexpandedDisplayName(current->qmakeCommand()))
        version->setUnexpandedDisplayName(current->displayName());
dt_'s avatar
dt_ committed
660 661

    // Update ui
662 663 664 665
    if (QtVersionItem *item = currentItem()) {
        item->setVersion(version);
        item->setIcon(version->isValid()? m_validVersionIcon : m_invalidVersionIcon);
    }
666 667 668
    userChangedCurrentVersion();

    delete current;
dt_'s avatar
dt_ committed
669 670
}

Friedemann Kleint's avatar
Friedemann Kleint committed
671
// To be called if a Qt version was removed or added
dt's avatar
dt committed
672
void QtOptionsPageWidget::updateCleanUpButton()
673
{
674
    bool hasInvalidVersion = false;
hjk's avatar
hjk committed
675
    for (TreeItem *child : *m_manualItem) {
676 677
        auto item = static_cast<QtVersionItem *>(child);
        if (item->version() && !item->version()->isValid()) {
678
            hasInvalidVersion = true;
dt's avatar
dt committed
679
            break;
680 681
        }
    }
682

683
    m_ui->cleanUpButton->setEnabled(hasInvalidVersion);
684
}
685

dt's avatar
dt committed
686
void QtOptionsPageWidget::userChangedCurrentVersion()
con's avatar
con committed
687
{
dt's avatar
dt committed
688 689
    updateWidgets();
    updateDescriptionLabel();
690 691
}

692 693
void QtOptionsPageWidget::updateDescriptionLabel()
{
694
    QtVersionItem *item = currentItem();
695
    const BaseQtVersion *version = item ? item->version() : 0;
696 697 698 699 700 701 702 703 704
    const ValidityInfo info = validInformation(version);
    if (info.message.isEmpty()) {
        m_versionUi->errorLabel->setVisible(false);
    } else {
        m_versionUi->errorLabel->setVisible(true);
        m_versionUi->errorLabel->setText(info.message);
        m_versionUi->errorLabel->setToolTip(info.toolTip);
    }
    m_ui->infoWidget->setSummaryText(info.description);
705 706
    if (item)
        item->setIcon(info.icon);
707 708 709

    if (version) {
        m_infoBrowser->setHtml(version->toHtml(true));
710
        setInfoWidgetVisibility();
711
    } else {
712
        m_infoBrowser->clear();
713
        m_ui->versionInfoWidget->setVisible(false);
714 715
        m_ui->infoWidget->setVisible(false);
    }
716 717
}

718
void QtOptionsPageWidget::versionChanged(const QModelIndex &current, const QModelIndex &previous)
719
{
720 721
    Q_UNUSED(current);
    Q_UNUSED(previous);
dt's avatar
dt committed
722 723 724 725 726 727 728 729 730
    userChangedCurrentVersion();
}

void QtOptionsPageWidget::updateWidgets()
{
    delete m_configurationWidget;
    m_configurationWidget = 0;
    BaseQtVersion *version = currentVersion();
    if (version) {
731
        m_versionUi->nameEdit->setText(version->unexpandedDisplayName());
732
        m_versionUi->qmakePath->setText(version->qmakeCommand().toUserOutput());
dt's avatar
dt committed
733 734 735
        m_configurationWidget = version->createConfigurationWidget();
        if (m_configurationWidget) {
            m_versionUi->formLayout->addRow(m_configurationWidget);
dt's avatar
dt committed
736
            m_configurationWidget->setEnabled(!version->isAutodetected());
737
            connect(m_configurationWidget, &QtConfigWidget::changed,
738
                    this, &QtOptionsPageWidget::updateDescriptionLabel);
dt's avatar
dt committed
739
        }
740
    } else {
741
        m_versionUi->nameEdit->clear();
742
        m_versionUi->qmakePath->clear();
743 744
    }

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

752
void QtOptionsPageWidget::updateCurrentQtName()
753
{
754 755
    QtVersionItem *item = currentItem();
    if (!item || !item->version())
756
        return;
757

758
    item->setChanged(true);
759 760
    item->version()->setUnexpandedDisplayName(m_versionUi->nameEdit->text());

761
    updateDescriptionLabel();
hjk's avatar
hjk committed
762
    m_model->forItemsAtLevel<2>([this](QtVersionItem *item) { updateVersionItem(item); });
763 764
}

765 766
void QtOptionsPageWidget::apply()
{
767 768
    disconnect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
            this, &QtOptionsPageWidget::updateQtVersions);
769

770
    QList<BaseQtVersion *> versions;
771

772
    m_model->forItemsAtLevel<2>([&versions](QtVersionItem *item) {
773 774 775
        item->setChanged(false);
        versions.append(item->version()->clone());
    });
776

777
    QtVersionManager::setNewQtVersions(versions);
778 779


780 781
    connect(QtVersionManager::instance(), &QtVersionManager::qtVersionsChanged,
            this, &QtOptionsPageWidget::updateQtVersions);
782 783
}

hjk's avatar
hjk committed
784 785
} // namespace Internal
} // namespace QtSupport