cmakebuildsettingswidget.cpp 13.2 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 38 39
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/target.h>

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

47 48 49 50 51 52 53 54
#include <QBoxLayout>
#include <QCheckBox>
#include <QFrame>
#include <QGridLayout>
#include <QLabel>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QSpacerItem>
55
#include <QMenu>
56 57 58 59

namespace CMakeProjectManager {
namespace Internal {

60 61 62 63
// --------------------------------------------------------------------
// CMakeBuildSettingsWidget:
// --------------------------------------------------------------------

Tobias Hunger's avatar
Tobias Hunger committed
64
CMakeBuildSettingsWidget::CMakeBuildSettingsWidget(CMakeBuildConfiguration *bc) :
65 66
    m_buildConfiguration(bc),
    m_configModel(new ConfigModel(this)),
67 68
    m_configFilterModel(new QSortFilterProxyModel),
    m_configTextFilterModel(new QSortFilterProxyModel)
69
{
70 71 72 73
    QTC_CHECK(bc);

    setDisplayName(tr("CMake"));

Tobias Hunger's avatar
Tobias Hunger committed
74
    auto vbox = new QVBoxLayout(this);
75
    vbox->setMargin(0);
Tobias Hunger's avatar
Tobias Hunger committed
76
    auto container = new Utils::DetailsWidget;
77 78 79
    container->setState(Utils::DetailsWidget::NoSummary);
    vbox->addWidget(container);

Tobias Hunger's avatar
Tobias Hunger committed
80
    auto details = new QWidget(container);
81 82
    container->setWidget(details);

83 84 85
    auto mainLayout = new QGridLayout(details);
    mainLayout->setMargin(0);
    mainLayout->setColumnStretch(1, 10);
86

87
    auto project = static_cast<CMakeProject *>(bc->target()->project());
88

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

98 99 100 101
    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);
102

103
    ++row;
104 105 106 107
    mainLayout->addItem(new QSpacerItem(20, 10), row, 0);

    ++row;
    m_errorLabel = new QLabel;
108
    m_errorLabel->setPixmap(Utils::Icons::CRITICAL.pixmap());
109 110 111 112 113 114 115 116
    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);

117 118
    ++row;
    m_warningLabel = new QLabel;
Ulf Hermann's avatar
Ulf Hermann committed
119
    m_warningLabel->setPixmap(Utils::Icons::WARNING.pixmap());
120 121 122 123 124 125 126 127
    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);

128 129
    ++row;
    mainLayout->addItem(new QSpacerItem(20, 10), row, 0);
130

131 132 133 134 135 136
    ++row;
    m_filterEdit = new Utils::FancyLineEdit;
    m_filterEdit->setPlaceholderText(tr("Filter"));
    m_filterEdit->setFiltering(true);
    mainLayout->addWidget(m_filterEdit, row, 0, 1, 2);

137 138 139 140 141
    ++row;
    auto tree = new Utils::TreeView;
    connect(tree, &Utils::TreeView::activated,
            tree, [tree](const QModelIndex &idx) { tree->edit(idx); });
    m_configView = tree;
142

143
    m_configFilterModel->setSourceModel(m_configModel);
144 145 146 147
    m_configFilterModel->setFilterKeyColumn(0);
    m_configFilterModel->setFilterRole(ConfigModel::ItemIsAdvancedRole);
    m_configFilterModel->setFilterFixedString("0");

148 149 150
    m_configTextFilterModel->setSourceModel(m_configFilterModel);
    m_configTextFilterModel->setFilterKeyColumn(-1);
    m_configTextFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
151

152
    m_configView->setModel(m_configTextFilterModel);
153 154
    m_configView->setMinimumHeight(300);
    m_configView->setUniformRowHeights(true);
155
    auto stretcher = new Utils::HeaderViewStretcher(m_configView->header(), 1);
156 157 158
    m_configView->setSelectionMode(QAbstractItemView::SingleSelection);
    m_configView->setSelectionBehavior(QAbstractItemView::SelectItems);
    m_configView->setFrameShape(QFrame::NoFrame);
159
    m_configView->setItemDelegate(new ConfigModelItemDelegate(m_configView));
160 161 162
    QFrame *findWrapper = Core::ItemViewFind::createSearchableWrapper(m_configView, Core::ItemViewFind::LightColored);
    findWrapper->setFrameStyle(QFrame::StyledPanel);

163
    m_progressIndicator = new Utils::ProgressIndicator(Utils::ProgressIndicatorSize::Large, findWrapper);
164 165 166 167 168 169 170 171 172 173
    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;
174 175 176 177 178 179 180 181 182 183 184 185 186 187
    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);
    }
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    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);

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

    updateAdvancedCheckBox();
206
    setError(bc->error());
207
    setWarning(bc->warning());
208

209
    connect(project, &ProjectExplorer::Project::parsingStarted, this, [this]() {
210 211 212
        updateButtonState();
        m_showProgressTimer.start();
    });
213 214 215 216 217 218

    if (m_buildConfiguration->isParsing())
        m_showProgressTimer.start();
    else
        m_configModel->setConfiguration(m_buildConfiguration->completeCMakeConfiguration());

Tobias Hunger's avatar
Tobias Hunger committed
219
    connect(m_buildConfiguration->target()->project(), &ProjectExplorer::Project::parsingFinished,
220
            this, [this, buildDirChooser, stretcher]() {
221
        updateButtonState();
222 223 224
        m_configModel->setConfiguration(m_buildConfiguration->completeCMakeConfiguration());
        stretcher->stretch();
        buildDirChooser->triggerChanged(); // refresh valid state...
225 226 227
        m_showProgressTimer.stop();
        m_progressIndicator->hide();
    });
228 229 230 231 232
    connect(m_buildConfiguration, &CMakeBuildConfiguration::errorOccured,
            this, [this]() {
        m_showProgressTimer.stop();
        m_progressIndicator->hide();
    });
233 234 235 236 237 238 239 240 241

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

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

242 243 244
    connect(m_filterEdit, &QLineEdit::textChanged,
            m_configTextFilterModel, &QSortFilterProxyModel::setFilterFixedString);

245
    connect(m_resetButton, &QPushButton::clicked, m_configModel, &ConfigModel::resetAllChanges);
246 247
    connect(m_reconfigureButton, &QPushButton::clicked, this, [this]() {
        m_buildConfiguration->setCurrentCMakeConfiguration(m_configModel->configurationChanges());
248 249 250 251 252 253 254 255
    });
    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);
    });
256 257 258 259 260 261 262 263 264 265 266 267 268 269
    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);
    });
270 271

    connect(bc, &CMakeBuildConfiguration::errorOccured, this, &CMakeBuildSettingsWidget::setError);
272
    connect(bc, &CMakeBuildConfiguration::warningOccured, this, &CMakeBuildSettingsWidget::setWarning);
273 274 275 276

    updateFromKit();
    connect(m_buildConfiguration->target(), &ProjectExplorer::Target::kitChanged,
            this, &CMakeBuildSettingsWidget::updateFromKit);
277 278
    connect(m_buildConfiguration, &CMakeBuildConfiguration::enabledChanged,
            this, [this]() { setError(m_buildConfiguration->disabledReason()); });
279 280 281 282
}

void CMakeBuildSettingsWidget::setError(const QString &message)
{
283 284
    bool showError = !message.isEmpty();
    m_errorLabel->setVisible(showError);
285
    m_errorLabel->setToolTip(message);
286
    m_errorMessageLabel->setVisible(showError);
287 288 289
    m_errorMessageLabel->setText(message);
    m_errorMessageLabel->setToolTip(message);

290 291 292
    m_editButton->setEnabled(!showError);
    m_resetButton->setEnabled(!showError);
    m_showAdvancedCheckBox->setEnabled(!showError);
293
    m_filterEdit->setEnabled(!showError);
294 295 296 297 298 299 300 301 302 303
}

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);
304 305 306 307
}

void CMakeBuildSettingsWidget::updateButtonState()
{
308
    const bool isParsing = m_buildConfiguration->isParsing();
309 310 311 312 313 314 315
    const bool hasChanges = m_configModel->hasChanges();
    m_resetButton->setEnabled(hasChanges && !isParsing);
    m_reconfigureButton->setEnabled((hasChanges || m_configModel->hasCMakeChanges()) && !isParsing);
}

void CMakeBuildSettingsWidget::updateAdvancedCheckBox()
{
316 317 318 319
    if (m_showAdvancedCheckBox->isChecked())
        m_configTextFilterModel->setSourceModel(m_configModel);
    else
        m_configTextFilterModel->setSourceModel(m_configFilterModel);
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333
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);
}

334 335
} // namespace Internal
} // namespace CMakeProjectManager