cmakebuildsettingswidget.cpp 15.8 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** 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.
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.
23 24 25 26
**
****************************************************************************/

#include "cmakebuildsettingswidget.h"
27 28

#include "configmodel.h"
29
#include "configmodelitemdelegate.h"
30
#include "cmakekitinformation.h"
31 32 33 34
#include "cmakeproject.h"
#include "cmakebuildconfiguration.h"

#include <coreplugin/icore.h>
35
#include <coreplugin/find/itemviewfind.h>
36
#include <projectexplorer/kitmanager.h>
37
#include <projectexplorer/project.h>
38 39 40
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h>

41
#include <utils/asconst.h>
42
#include <utils/detailswidget.h>
43
#include <utils/fancylineedit.h>
44
#include <utils/headerviewstretcher.h>
45
#include <utils/pathchooser.h>
46
#include <utils/itemviews.h>
Ulf Hermann's avatar
Ulf Hermann committed
47
#include <utils/utilsicons.h>
48

49 50
#include <QBoxLayout>
#include <QCheckBox>
51
#include <QComboBox>
52 53 54 55 56 57
#include <QFrame>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QSpacerItem>
58
#include <QStyledItemDelegate>
59
#include <QMenu>
60 61 62 63

namespace CMakeProjectManager {
namespace Internal {

64 65 66 67
// --------------------------------------------------------------------
// CMakeBuildSettingsWidget:
// --------------------------------------------------------------------

Tobias Hunger's avatar
Tobias Hunger committed
68
CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc) :
69 70
    m_buildConfiguration(bc),
    m_configModel(new ConfigModel(this)),
71 72
    m_configFilterModel(new QSortFilterProxyModel),
    m_configTextFilterModel(new QSortFilterProxyModel)
73
{
74 75 76 77
    QTC_CHECK(bc);

    setDisplayName(tr("CMake"));

Tobias Hunger's avatar
Tobias Hunger committed
78
    auto vbox = new QVBoxLayout(this);
79
    vbox->setMargin(0);
Tobias Hunger's avatar
Tobias Hunger committed
80
    auto container = new Utils::DetailsWidget;
81 82 83
    container->setState(Utils::DetailsWidget::NoSummary);
    vbox->addWidget(container);

Tobias Hunger's avatar
Tobias Hunger committed
84
    auto details = new QWidget(container);
85 86
    container->setWidget(details);

87 88 89
    auto mainLayout = new QGridLayout(details);
    mainLayout->setMargin(0);
    mainLayout->setColumnStretch(1, 10);
90

91
    auto project = static_cast<CMakeProject *>(bc->target()->project());
92

93 94 95 96
    auto buildDirChooser = new Utils::PathChooser;
    buildDirChooser->setBaseFileName(project->projectDirectory());
    buildDirChooser->setFileName(bc->buildDirectory());
    connect(buildDirChooser, &Utils::PathChooser::rawPathChanged, this,
97
            [this](const QString &path) {
98
                m_configModel->flush(); // clear out config cache...
99
                m_buildConfiguration->setBuildDirectory(Utils::FileName::fromString(path));
100
            });
101

102 103 104 105
    int row = 0;
    mainLayout->addWidget(new QLabel(tr("Build directory:")), row, 0);
    mainLayout->addWidget(buildDirChooser->lineEdit(), row, 1);
    mainLayout->addWidget(buildDirChooser->buttonAtIndex(0), row, 2);
106

107
    ++row;
108 109 110 111
    mainLayout->addItem(new QSpacerItem(20, 10), row, 0);

    ++row;
    m_errorLabel = new QLabel;
112
    m_errorLabel->setPixmap(Utils::Icons::CRITICAL.pixmap());
113 114 115 116 117 118 119 120
    m_errorLabel->setVisible(false);
    m_errorMessageLabel = new QLabel;
    m_errorMessageLabel->setVisible(false);
    auto boxLayout = new QHBoxLayout;
    boxLayout->addWidget(m_errorLabel);
    boxLayout->addWidget(m_errorMessageLabel);
    mainLayout->addLayout(boxLayout, row, 0, 1, 3, Qt::AlignHCenter);

121 122
    ++row;
    m_warningLabel = new QLabel;
Ulf Hermann's avatar
Ulf Hermann committed
123
    m_warningLabel->setPixmap(Utils::Icons::WARNING.pixmap());
124 125 126 127 128 129 130 131
    m_warningLabel->setVisible(false);
    m_warningMessageLabel = new QLabel;
    m_warningMessageLabel->setVisible(false);
    auto boxLayout2 = new QHBoxLayout;
    boxLayout2->addWidget(m_warningLabel);
    boxLayout2->addWidget(m_warningMessageLabel);
    mainLayout->addLayout(boxLayout2, row, 0, 1, 3, Qt::AlignHCenter);

132 133
    ++row;
    mainLayout->addItem(new QSpacerItem(20, 10), row, 0);
134

135 136 137 138 139 140
    ++row;
    m_filterEdit = new Utils::FancyLineEdit;
    m_filterEdit->setPlaceholderText(tr("Filter"));
    m_filterEdit->setFiltering(true);
    mainLayout->addWidget(m_filterEdit, row, 0, 1, 2);

141 142 143 144 145
    ++row;
    auto tree = new Utils::TreeView;
    connect(tree, &Utils::TreeView::activated,
            tree, [tree](const QModelIndex &idx) { tree->edit(idx); });
    m_configView = tree;
146

147 148
    m_configView->viewport()->installEventFilter(this);

149
    m_configFilterModel->setSourceModel(m_configModel);
150 151 152 153
    m_configFilterModel->setFilterKeyColumn(0);
    m_configFilterModel->setFilterRole(ConfigModel::ItemIsAdvancedRole);
    m_configFilterModel->setFilterFixedString("0");

154
    m_configTextFilterModel->setSourceModel(m_configFilterModel);
155
    m_configTextFilterModel->setSortRole(Qt::DisplayRole);
156 157
    m_configTextFilterModel->setFilterKeyColumn(-1);
    m_configTextFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
158

159
    m_configView->setModel(m_configTextFilterModel);
160 161
    m_configView->setMinimumHeight(300);
    m_configView->setUniformRowHeights(true);
162 163
    m_configView->setSortingEnabled(true);
    m_configView->sortByColumn(0, Qt::AscendingOrder);
164
    auto stretcher = new Utils::HeaderViewStretcher(m_configView->header(), 1);
165 166 167
    m_configView->setSelectionMode(QAbstractItemView::SingleSelection);
    m_configView->setSelectionBehavior(QAbstractItemView::SelectItems);
    m_configView->setFrameShape(QFrame::NoFrame);
168 169
    m_configView->setItemDelegate(new ConfigModelItemDelegate(m_buildConfiguration->project()->projectDirectory(),
                                                              m_configView));
170 171 172
    QFrame *findWrapper = Core::ItemViewFind::createSearchableWrapper(m_configView, Core::ItemViewFind::LightColored);
    findWrapper->setFrameStyle(QFrame::StyledPanel);

173
    m_progressIndicator = new Utils::ProgressIndicator(Utils::ProgressIndicatorSize::Large, findWrapper);
174 175 176 177 178 179 180 181 182 183
    m_progressIndicator->attachToWidget(findWrapper);
    m_progressIndicator->raise();
    m_progressIndicator->hide();
    m_showProgressTimer.setSingleShot(true);
    m_showProgressTimer.setInterval(50); // don't show progress for < 50ms tasks
    connect(&m_showProgressTimer, &QTimer::timeout, [this]() { m_progressIndicator->show(); });

    mainLayout->addWidget(findWrapper, row, 0, 1, 2);

    auto buttonLayout = new QVBoxLayout;
184 185 186 187 188 189 190 191 192 193 194 195 196 197
    m_addButton = new QPushButton(tr("&Add"));
    buttonLayout->addWidget(m_addButton);
    {
        m_addButtonMenu = new QMenu;
        m_addButtonMenu->addAction(tr("&Boolean"))->setData(
                    QVariant::fromValue(static_cast<int>(ConfigModel::DataItem::BOOLEAN)));
        m_addButtonMenu->addAction(tr("&String"))->setData(
                    QVariant::fromValue(static_cast<int>(ConfigModel::DataItem::STRING)));
        m_addButtonMenu->addAction(tr("&Directory"))->setData(
                    QVariant::fromValue(static_cast<int>(ConfigModel::DataItem::DIRECTORY)));
        m_addButtonMenu->addAction(tr("&File"))->setData(
                    QVariant::fromValue(static_cast<int>(ConfigModel::DataItem::FILE)));
        m_addButton->setMenu(m_addButtonMenu);
    }
198 199 200 201 202 203 204 205 206 207 208 209
    m_editButton = new QPushButton(tr("&Edit"));
    buttonLayout->addWidget(m_editButton);
    m_resetButton = new QPushButton(tr("&Reset"));
    m_resetButton->setEnabled(false);
    buttonLayout->addWidget(m_resetButton);
    buttonLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Fixed, QSizePolicy::Fixed));
    m_showAdvancedCheckBox = new QCheckBox(tr("Advanced"));
    buttonLayout->addWidget(m_showAdvancedCheckBox);
    buttonLayout->addItem(new QSpacerItem(10, 10, QSizePolicy::Minimum, QSizePolicy::Expanding));

    mainLayout->addLayout(buttonLayout, row, 2);

210 211 212
    connect(m_configView->selectionModel(), &QItemSelectionModel::currentChanged,
            this, &CMakeBuildSettingsWidget::updateSelection);

213 214 215 216 217 218
    ++row;
    m_reconfigureButton = new QPushButton(tr("Apply Configuration Changes"));
    m_reconfigureButton->setEnabled(false);
    mainLayout->addWidget(m_reconfigureButton, row, 0, 1, 3);

    updateAdvancedCheckBox();
219
    setError(bc->error());
220
    setWarning(bc->warning());
221

222
    connect(project, &ProjectExplorer::Project::parsingStarted, this, [this]() {
223 224 225
        updateButtonState();
        m_showProgressTimer.start();
    });
226 227 228

    if (m_buildConfiguration->isParsing())
        m_showProgressTimer.start();
229
    else {
230
        m_configModel->setConfiguration(m_buildConfiguration->completeCMakeConfiguration());
231 232
        m_configView->expandAll();
    }
233

Tobias Hunger's avatar
Tobias Hunger committed
234
    connect(m_buildConfiguration->target()->project(), &ProjectExplorer::Project::parsingFinished,
235 236
            this, [this, buildDirChooser, stretcher]() {
        m_configModel->setConfiguration(m_buildConfiguration->completeCMakeConfiguration());
237
        m_configView->expandAll();
238
        stretcher->stretch();
239
        updateButtonState();
240
        buildDirChooser->triggerChanged(); // refresh valid state...
241 242 243
        m_showProgressTimer.stop();
        m_progressIndicator->hide();
    });
244 245 246 247 248
    connect(m_buildConfiguration, &CMakeBuildConfiguration::errorOccured,
            this, [this]() {
        m_showProgressTimer.stop();
        m_progressIndicator->hide();
    });
249 250 251 252
    connect(m_configTextFilterModel, &QAbstractItemModel::modelReset, this, [this, stretcher]() {
        m_configView->expandAll();
        stretcher->stretch();
    });
253 254 255 256 257 258 259 260 261

    connect(m_configModel, &QAbstractItemModel::dataChanged,
            this, &CMakeBuildSettingsWidget::updateButtonState);
    connect(m_configModel, &QAbstractItemModel::modelReset,
            this, &CMakeBuildSettingsWidget::updateButtonState);

    connect(m_showAdvancedCheckBox, &QCheckBox::stateChanged,
            this, &CMakeBuildSettingsWidget::updateAdvancedCheckBox);

262 263 264
    connect(m_filterEdit, &QLineEdit::textChanged,
            m_configTextFilterModel, &QSortFilterProxyModel::setFilterFixedString);

265
    connect(m_resetButton, &QPushButton::clicked, m_configModel, &ConfigModel::resetAllChanges);
266 267
    connect(m_reconfigureButton, &QPushButton::clicked, this, [this]() {
        m_buildConfiguration->setCurrentCMakeConfiguration(m_configModel->configurationChanges());
268 269 270 271 272 273 274 275
    });
    connect(m_editButton, &QPushButton::clicked, this, [this]() {
        QModelIndex idx = m_configView->currentIndex();
        if (idx.column() != 1)
            idx = idx.sibling(idx.row(), 1);
        m_configView->setCurrentIndex(idx);
        m_configView->edit(idx);
    });
276 277 278 279 280 281 282 283 284 285 286 287 288 289
    connect(m_addButtonMenu, &QMenu::triggered, this, [this](QAction *action) {
        ConfigModel::DataItem::Type type =
                static_cast<ConfigModel::DataItem::Type>(action->data().value<int>());
        QString value = tr("<UNSET>");
        if (type == ConfigModel::DataItem::BOOLEAN)
            value = QString::fromLatin1("OFF");

        m_configModel->appendConfiguration(tr("<UNSET>"), value, type);
        QModelIndex idx;
        idx = m_configView->model()->index(
                    m_configView->model()->rowCount(idx) - 1, 0);
        m_configView->setCurrentIndex(idx);
        m_configView->edit(idx);
    });
290 291

    connect(bc, &CMakeBuildConfiguration::errorOccured, this, &CMakeBuildSettingsWidget::setError);
292
    connect(bc, &CMakeBuildConfiguration::warningOccured, this, &CMakeBuildSettingsWidget::setWarning);
293 294 295 296

    updateFromKit();
    connect(m_buildConfiguration->target(), &ProjectExplorer::Target::kitChanged,
            this, &CMakeBuildSettingsWidget::updateFromKit);
297 298
    connect(m_buildConfiguration, &CMakeBuildConfiguration::enabledChanged,
            this, [this]() { setError(m_buildConfiguration->disabledReason()); });
299 300 301 302
}

void CMakeBuildSettingsWidget::setError(const QString &message)
{
303 304
    bool showError = !message.isEmpty();
    m_errorLabel->setVisible(showError);
305
    m_errorLabel->setToolTip(message);
306
    m_errorMessageLabel->setVisible(showError);
307 308 309
    m_errorMessageLabel->setText(message);
    m_errorMessageLabel->setToolTip(message);

310 311 312
    m_editButton->setEnabled(!showError);
    m_resetButton->setEnabled(!showError);
    m_showAdvancedCheckBox->setEnabled(!showError);
313
    m_filterEdit->setEnabled(!showError);
314 315 316 317 318 319 320 321 322 323
}

void CMakeBuildSettingsWidget::setWarning(const QString &message)
{
    bool showWarning = !message.isEmpty();
    m_warningLabel->setVisible(showWarning);
    m_warningLabel->setToolTip(message);
    m_warningMessageLabel->setVisible(showWarning);
    m_warningMessageLabel->setText(message);
    m_warningMessageLabel->setToolTip(message);
324 325 326 327
}

void CMakeBuildSettingsWidget::updateButtonState()
{
328
    const bool isParsing = m_buildConfiguration->isParsing();
329 330 331 332 333 334 335
    const bool hasChanges = m_configModel->hasChanges();
    m_resetButton->setEnabled(hasChanges && !isParsing);
    m_reconfigureButton->setEnabled((hasChanges || m_configModel->hasCMakeChanges()) && !isParsing);
}

void CMakeBuildSettingsWidget::updateAdvancedCheckBox()
{
336 337
    if (m_showAdvancedCheckBox->isChecked()) {
        m_configFilterModel->setSourceModel(nullptr);
338
        m_configTextFilterModel->setSourceModel(m_configModel);
339 340 341 342

    } else {
        m_configTextFilterModel->setSourceModel(nullptr);
        m_configFilterModel->setSourceModel(m_configModel);
343
        m_configTextFilterModel->setSourceModel(m_configFilterModel);
344
    }
345 346
}

347 348 349 350 351 352 353 354 355 356 357 358
void CMakeBuildSettingsWidget::updateFromKit()
{
    const ProjectExplorer::Kit *k = m_buildConfiguration->target()->kit();
    const CMakeConfig config = CMakeConfigurationKitInformation::configuration(k);

    QHash<QString, QString> configHash;
    for (const CMakeConfigItem &i : config)
        configHash.insert(QString::fromUtf8(i.key), i.expandedValue(k));

    m_configModel->setKitConfiguration(configHash);
}

359 360
static QModelIndex mapToSource(const QAbstractItemView *view, const QModelIndex &idx)
{
361 362 363
    if (!idx.isValid())
        return idx;

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    QAbstractItemModel *model = view->model();
    QModelIndex result = idx;
    while (QSortFilterProxyModel *proxy = qobject_cast<QSortFilterProxyModel *>(model)) {
        result = proxy->mapToSource(result);
        model = proxy->sourceModel();
    }
    return result;
}

void CMakeBuildSettingsWidget::updateSelection(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous);
    const QModelIndex currentModelIndex = mapToSource(m_configView, current);
    if (currentModelIndex.isValid())
        m_editButton->setEnabled(currentModelIndex.flags().testFlag(Qt::ItemIsEditable));
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394
bool CMakeBuildSettingsWidget::eventFilter(QObject *target, QEvent *event)
{
    // handle context menu events:
    if (target != m_configView->viewport() || event->type() != QEvent::ContextMenu)
        return false;

    auto e = static_cast<QContextMenuEvent *>(event);
    const QModelIndex idx = mapToSource(m_configView, m_configView->indexAt(e->pos()));
    if (!idx.isValid())
        return false;

    QMenu *menu = new QMenu(this);
    connect(menu, &QMenu::triggered, menu, &QMenu::deleteLater);

395
    QAction *forceToStringAction = new QAction(tr("Force to String"), nullptr);
396 397 398 399 400 401 402 403 404 405
    forceToStringAction->setEnabled(m_configModel->canForceToString(idx));
    menu->addAction(forceToStringAction);
    connect(forceToStringAction, &QAction::triggered, this, [this, idx]() { m_configModel->forceToString(idx); });

    menu->move(e->globalPos());
    menu->show();

    return true;
}

406 407
} // namespace Internal
} // namespace CMakeProjectManager