/**************************************************************************** ** ** Copyright (C) 2022 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** 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 ** 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. ** ** 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. ** ****************************************************************************/ #include "clangformatglobalconfigwidget.h" #include "clangformatconstants.h" #include "clangformatsettings.h" #include "clangformatutils.h" #include #include #include #include #include #include #include using namespace ProjectExplorer; using namespace Utils; namespace ClangFormat { ClangFormatGlobalConfigWidget::ClangFormatGlobalConfigWidget(ProjectExplorer::Project *project, QWidget *parent) : CppCodeStyleWidget(parent) , m_project(project) { resize(489, 305); m_projectHasClangFormat = new QLabel(this); m_formattingModeLabel = new QLabel(tr("Formatting mode:")); m_indentingOrFormatting = new QComboBox(this); m_formatWhileTyping = new QCheckBox(tr("Format while typing")); m_formatOnSave = new QCheckBox(tr("Format edited code on file save")); m_overrideDefault = new QCheckBox(tr("Override Clang Format configuration file")); using namespace Layouting; Group globalSettingsGroupBox { title(tr("ClangFormat global setting:")), Column { Row { m_formattingModeLabel, m_indentingOrFormatting, st }, m_formatWhileTyping, m_formatOnSave, m_projectHasClangFormat, m_overrideDefault } }; Column { globalSettingsGroupBox }.attachTo(this); initCheckBoxes(); initIndentationOrFormattingCombobox(); initOverrideCheckBox(); if (project) { m_formattingModeLabel->hide(); m_formatOnSave->hide(); m_formatWhileTyping->hide(); m_indentingOrFormatting->hide(); return; } globalSettingsGroupBox.widget->show(); } ClangFormatGlobalConfigWidget::~ClangFormatGlobalConfigWidget() = default; void ClangFormatGlobalConfigWidget::initCheckBoxes() { auto setEnableCheckBoxes = [this](int index) { bool isFormatting = index == static_cast(ClangFormatSettings::Mode::Formatting); m_formatOnSave->setEnabled(isFormatting); m_formatWhileTyping->setEnabled(isFormatting); }; setEnableCheckBoxes(m_indentingOrFormatting->currentIndex()); connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, this, setEnableCheckBoxes); m_formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); m_formatWhileTyping->setChecked(ClangFormatSettings::instance().formatWhileTyping()); } void ClangFormatGlobalConfigWidget::initIndentationOrFormattingCombobox() { m_indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Indenting), tr("Indenting only")); m_indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Formatting), tr("Full formatting")); m_indentingOrFormatting->insertItem(static_cast(ClangFormatSettings::Mode::Disable), tr("Disable")); m_indentingOrFormatting->setCurrentIndex( static_cast(ClangFormatSettings::instance().mode())); } bool ClangFormatGlobalConfigWidget::projectClangFormatFileExists() { llvm::Expected styleFromProjectFolder = clang::format::getStyle("file", m_project->projectFilePath().path().toStdString(), "none"); return styleFromProjectFolder && !(*styleFromProjectFolder == clang::format::getNoStyle()); } void ClangFormatGlobalConfigWidget::initOverrideCheckBox() { if (!m_project || !projectClangFormatFileExists()) { m_projectHasClangFormat->hide(); } else { m_projectHasClangFormat->show(); m_projectHasClangFormat->setText(tr("The current project has its own .clang-format file which " "can be overridden by the settings below.")); } auto setEnableOverrideCheckBox = [this](int index) { bool isDisable = index == static_cast(ClangFormatSettings::Mode::Disable); m_overrideDefault->setEnabled(!isDisable); }; setEnableOverrideCheckBox(m_indentingOrFormatting->currentIndex()); connect(m_indentingOrFormatting, &QComboBox::currentIndexChanged, this, setEnableOverrideCheckBox); m_overrideDefault->setToolTip( tr("Override Clang Format configuration file with the chosen configuration.")); if (m_project) m_overrideDefault->setChecked( m_project->namedSettings(Constants::OVERRIDE_FILE_ID).toBool()); else m_overrideDefault->setChecked(ClangFormatSettings::instance().overrideDefaultFile()); connect(m_overrideDefault, &QCheckBox::toggled, this, [this](bool checked) { if (m_project) m_project->setNamedSettings(Constants::OVERRIDE_FILE_ID, checked); else { ClangFormatSettings::instance().setOverrideDefaultFile(checked); ClangFormatSettings::instance().write(); } }); } void ClangFormatGlobalConfigWidget::apply() { ClangFormatSettings &settings = ClangFormatSettings::instance(); settings.setFormatOnSave(m_formatOnSave->isChecked()); settings.setFormatWhileTyping(m_formatWhileTyping->isChecked()); settings.setMode(static_cast(m_indentingOrFormatting->currentIndex())); settings.write(); } } // namespace ClangFormat