diff --git a/src/libs/utils/genericconstants.h b/src/libs/utils/genericconstants.h new file mode 100644 index 00000000000..a380fc15ba0 --- /dev/null +++ b/src/libs/utils/genericconstants.h @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2016 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. +** +****************************************************************************/ + +#pragma once + +namespace Utils { + +namespace Constants { + +const char BEAUTIFIER_SETTINGS_GROUP[] = "Beautifier"; +const char BEAUTIFIER_GENERAL_GROUP[] = "General"; +const char BEAUTIFIER_AUTO_FORMAT_ON_SAVE[] = "autoFormatOnSave"; + +} // namespace Constants +} // namespace Utils diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index a71cfaeb993..c7fef47c97f 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -132,6 +132,7 @@ win32:SOURCES += $$PWD/consoleprocess_win.cpp else:SOURCES += $$PWD/consoleprocess_unix.cpp HEADERS += \ + $$PWD/genericconstants.h \ $$PWD/globalfilechangeblocker.h \ $$PWD/benchmarker.h \ $$PWD/environment.h \ diff --git a/src/plugins/beautifier/abstractsettings.cpp b/src/plugins/beautifier/abstractsettings.cpp index 4d47a756d58..c7c091df902 100644 --- a/src/plugins/beautifier/abstractsettings.cpp +++ b/src/plugins/beautifier/abstractsettings.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -204,7 +205,7 @@ void AbstractSettings::save() { // Save settings, except styles QSettings *s = Core::ICore::settings(); - s->beginGroup(Constants::SETTINGS_GROUP); + s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP); s->beginGroup(m_name); QMap::const_iterator iSettings = m_settings.constBegin(); while (iSettings != m_settings.constEnd()) { @@ -276,7 +277,7 @@ void AbstractSettings::read() // Read settings, except styles QSettings *s = Core::ICore::settings(); - s->beginGroup(Constants::SETTINGS_GROUP); + s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP); s->beginGroup(m_name); const QStringList keys = s->allKeys(); for (const QString &key : keys) { diff --git a/src/plugins/beautifier/beautifierconstants.h b/src/plugins/beautifier/beautifierconstants.h index 4430e62f6ea..269f41bc84d 100644 --- a/src/plugins/beautifier/beautifierconstants.h +++ b/src/plugins/beautifier/beautifierconstants.h @@ -34,7 +34,6 @@ const char ACTION_ID[] = "Beautifier.Action"; const char MENU_ID[] = "Beautifier.Menu"; const char OPTION_CATEGORY[] = "II.Beautifier"; const char OPTION_GENERAL_ID[] = "aaa.General"; -const char SETTINGS_GROUP[] = "Beautifier"; const char SETTINGS_DIRNAME[] = "beautifier"; const char DOCUMENTATION_DIRNAME[] = "documentation"; const char DOCUMENTATION_XMLROOT[] = "beautifier_documentation"; diff --git a/src/plugins/beautifier/generalsettings.cpp b/src/plugins/beautifier/generalsettings.cpp index a3018950d17..9546ec5d251 100644 --- a/src/plugins/beautifier/generalsettings.cpp +++ b/src/plugins/beautifier/generalsettings.cpp @@ -29,14 +29,13 @@ #include #include +#include #include namespace Beautifier { namespace Internal { namespace { -const char GROUP[] = "General"; -const char AUTO_FORMAT_ON_SAVE[] = "autoFormatOnSave"; const char AUTO_FORMAT_TOOL[] = "autoFormatTool"; const char AUTO_FORMAT_MIME[] = "autoFormatMime"; const char AUTO_FORMAT_ONLY_CURRENT_PROJECT[] = "autoFormatOnlyCurrentProject"; @@ -50,9 +49,9 @@ GeneralSettings::GeneralSettings() void GeneralSettings::read() { QSettings *s = Core::ICore::settings(); - s->beginGroup(Constants::SETTINGS_GROUP); - s->beginGroup(GROUP); - m_autoFormatOnSave = s->value(AUTO_FORMAT_ON_SAVE, false).toBool(); + s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP); + s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP); + m_autoFormatOnSave = s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool(); m_autoFormatTool = s->value(AUTO_FORMAT_TOOL, QString()).toString(); setAutoFormatMime(s->value(AUTO_FORMAT_MIME, "text/x-c++src;text/x-c++hdr").toString()); m_autoFormatOnlyCurrentProject = s->value(AUTO_FORMAT_ONLY_CURRENT_PROJECT, true).toBool(); @@ -63,9 +62,9 @@ void GeneralSettings::read() void GeneralSettings::save() { QSettings *s = Core::ICore::settings(); - s->beginGroup(Constants::SETTINGS_GROUP); - s->beginGroup(GROUP); - s->setValue(AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave); + s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP); + s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP); + s->setValue(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, m_autoFormatOnSave); s->setValue(AUTO_FORMAT_TOOL, m_autoFormatTool); s->setValue(AUTO_FORMAT_MIME, autoFormatMimeAsString()); s->setValue(AUTO_FORMAT_ONLY_CURRENT_PROJECT, m_autoFormatOnlyCurrentProject); diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index 61287bcb38b..3e311c1e4e2 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -37,6 +37,8 @@ #include #include #include +#include +#include #include #include #include @@ -45,6 +47,7 @@ #include #include #include +#include #include #include @@ -55,6 +58,36 @@ using namespace ProjectExplorer; namespace ClangFormat { +static const char kFileSaveWarning[] + = "Disable formatting on file save in the Beautifier plugin to enable this check"; + +static bool isBeautifierPluginActivated() +{ + const QList specs = ExtensionSystem::PluginManager::plugins(); + return std::find_if(specs.begin(), + specs.end(), + [](ExtensionSystem::PluginSpec *spec) { + return spec->name() == "Beautifier"; + }) + != specs.end(); +} + +static bool isBeautifierOnSaveActivated() +{ + if (!isBeautifierPluginActivated()) + return false; + + QSettings *s = Core::ICore::settings(); + bool activated = false; + s->beginGroup(Utils::Constants::BEAUTIFIER_SETTINGS_GROUP); + s->beginGroup(Utils::Constants::BEAUTIFIER_GENERAL_GROUP); + if (s->value(Utils::Constants::BEAUTIFIER_AUTO_FORMAT_ON_SAVE, false).toBool()) + activated = true; + s->endGroup(); + s->endGroup(); + return activated; +} + bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event) { if (event->type() == QEvent::Wheel && qobject_cast(object)) { @@ -64,6 +97,22 @@ bool ClangFormatConfigWidget::eventFilter(QObject *object, QEvent *event) return QWidget::eventFilter(object, event); } +void ClangFormatConfigWidget::showEvent(QShowEvent *event) +{ + TextEditor::CodeStyleEditorWidget::showEvent(event); + if (isBeautifierOnSaveActivated()) { + bool wasEnabled = m_ui->formatOnSave->isEnabled(); + m_ui->formatOnSave->setChecked(false); + m_ui->formatOnSave->setEnabled(false); + m_ui->fileSaveWarning->setText(tr(kFileSaveWarning)); + if (wasEnabled) + apply(); + } else { + m_ui->formatOnSave->setEnabled(true); + m_ui->fileSaveWarning->setText(""); + } +} + ClangFormatConfigWidget::ClangFormatConfigWidget(ProjectExplorer::Project *project, QWidget *parent) : CodeStyleEditorWidget(parent) , m_project(project) @@ -183,6 +232,11 @@ void ClangFormatConfigWidget::showGlobalCheckboxes() m_ui->formatOnSave->setChecked(ClangFormatSettings::instance().formatOnSave()); m_ui->formatOnSave->show(); + if (isBeautifierOnSaveActivated()) { + m_ui->formatOnSave->setChecked(false); + m_ui->formatOnSave->setEnabled(false); + m_ui->fileSaveWarning->setText(tr(kFileSaveWarning)); + } } static bool projectConfigExists() diff --git a/src/plugins/clangformat/clangformatconfigwidget.h b/src/plugins/clangformat/clangformatconfigwidget.h index 5da2bd35f2e..869ddeccb78 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.h +++ b/src/plugins/clangformat/clangformatconfigwidget.h @@ -59,6 +59,7 @@ private: void onTableChanged(); bool eventFilter(QObject *object, QEvent *event) override; + void showEvent(QShowEvent *event) override; void showOrHideWidgets(); void initChecksAndPreview(); diff --git a/src/plugins/clangformat/clangformatconfigwidget.ui b/src/plugins/clangformat/clangformatconfigwidget.ui index d9126a4c269..45b9801d7ff 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.ui +++ b/src/plugins/clangformat/clangformatconfigwidget.ui @@ -41,11 +41,35 @@ - - - Format edited code on file save - - + + + + + Format edited code on file save + + + + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + +