2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
#include "icodestylepreferences.h"
|
|
|
|
|
#include "codestylepool.h"
|
|
|
|
|
#include "tabsettings.h"
|
|
|
|
|
#include <utils/settingsutils.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QSettings>
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2012-11-22 10:29:58 +02:00
|
|
|
static const char currentPreferencesKey[] = "CurrentPreferences";
|
2011-08-16 10:45:23 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
class ICodeStylePreferencesPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-09-20 01:16:01 +03:00
|
|
|
CodeStylePool *m_pool = nullptr;
|
|
|
|
|
ICodeStylePreferences *m_currentDelegate = nullptr;
|
2011-08-16 10:45:23 +02:00
|
|
|
TabSettings m_tabSettings;
|
2013-09-20 10:35:16 +02:00
|
|
|
QByteArray m_id;
|
2011-08-16 10:45:23 +02:00
|
|
|
QString m_displayName;
|
2018-09-20 01:16:01 +03:00
|
|
|
bool m_readOnly = false;
|
2023-04-04 14:28:27 +02:00
|
|
|
bool m_temporarilyReadOnly = false;
|
2023-05-08 11:00:04 +02:00
|
|
|
bool m_isAdditionalTabDisabled = false;
|
2020-01-28 18:48:32 +01:00
|
|
|
QString m_settingsSuffix;
|
2011-08-16 10:45:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences::ICodeStylePreferences(QObject *parent) :
|
|
|
|
|
QObject(parent),
|
|
|
|
|
d(new Internal::ICodeStylePreferencesPrivate)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences::~ICodeStylePreferences()
|
|
|
|
|
{
|
|
|
|
|
delete d;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
QByteArray ICodeStylePreferences::id() const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
return d->m_id;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
void ICodeStylePreferences::setId(const QByteArray &name)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
d->m_id = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ICodeStylePreferences::displayName() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_displayName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ICodeStylePreferences::setDisplayName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
d->m_displayName = name;
|
|
|
|
|
emit displayNameChanged(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ICodeStylePreferences::isReadOnly() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_readOnly;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ICodeStylePreferences::setReadOnly(bool on)
|
|
|
|
|
{
|
|
|
|
|
d->m_readOnly = on;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 14:28:27 +02:00
|
|
|
void ICodeStylePreferences::setTemporarilyReadOnly(bool on)
|
|
|
|
|
{
|
|
|
|
|
d->m_temporarilyReadOnly = on;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ICodeStylePreferences::isTemporarilyReadOnly() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_temporarilyReadOnly;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 11:00:04 +02:00
|
|
|
bool ICodeStylePreferences::isAdditionalTabDisabled() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_isAdditionalTabDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ICodeStylePreferences::setIsAdditionalTabDisabled(bool on)
|
|
|
|
|
{
|
|
|
|
|
d->m_isAdditionalTabDisabled = on;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
void ICodeStylePreferences::setTabSettings(const TabSettings &settings)
|
|
|
|
|
{
|
|
|
|
|
if (d->m_tabSettings == settings)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
d->m_tabSettings = settings;
|
|
|
|
|
|
|
|
|
|
emit tabSettingsChanged(d->m_tabSettings);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (!currentDelegate())
|
2011-08-16 10:45:23 +02:00
|
|
|
emit currentTabSettingsChanged(d->m_tabSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TabSettings ICodeStylePreferences::tabSettings() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_tabSettings;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TabSettings ICodeStylePreferences::currentTabSettings() const
|
|
|
|
|
{
|
|
|
|
|
return currentPreferences()->tabSettings();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant ICodeStylePreferences::currentValue() const
|
|
|
|
|
{
|
|
|
|
|
return currentPreferences()->value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences *ICodeStylePreferences::currentPreferences() const
|
|
|
|
|
{
|
2018-09-20 01:16:01 +03:00
|
|
|
auto prefs = (ICodeStylePreferences *)this;
|
2011-08-16 10:45:23 +02:00
|
|
|
while (prefs->currentDelegate())
|
|
|
|
|
prefs = prefs->currentDelegate();
|
|
|
|
|
return prefs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodeStylePool *ICodeStylePreferences::delegatingPool() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ICodeStylePreferences::setDelegatingPool(CodeStylePool *pool)
|
|
|
|
|
{
|
|
|
|
|
if (pool == d->m_pool)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-09-20 01:16:01 +03:00
|
|
|
setCurrentDelegate(nullptr);
|
2011-08-16 10:45:23 +02:00
|
|
|
if (d->m_pool) {
|
2015-12-13 01:18:33 +02:00
|
|
|
disconnect(d->m_pool, &CodeStylePool::codeStyleRemoved,
|
2015-12-18 10:30:00 +02:00
|
|
|
this, &ICodeStylePreferences::codeStyleRemoved);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
d->m_pool = pool;
|
|
|
|
|
if (d->m_pool) {
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(d->m_pool, &CodeStylePool::codeStyleRemoved,
|
2015-12-18 10:30:00 +02:00
|
|
|
this, &ICodeStylePreferences::codeStyleRemoved);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICodeStylePreferences *ICodeStylePreferences::currentDelegate() const
|
|
|
|
|
{
|
|
|
|
|
return d->m_currentDelegate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ICodeStylePreferences::setCurrentDelegate(ICodeStylePreferences *delegate)
|
|
|
|
|
{
|
|
|
|
|
if (delegate && d->m_pool && !d->m_pool->codeStyles().contains(delegate)) {
|
|
|
|
|
// warning
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (delegate == this || (delegate && delegate->id() == id())) {
|
|
|
|
|
// warning
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (d->m_currentDelegate == delegate)
|
|
|
|
|
return; // nothing changes
|
|
|
|
|
|
|
|
|
|
if (d->m_currentDelegate) {
|
2015-12-13 01:18:33 +02:00
|
|
|
disconnect(d->m_currentDelegate, &ICodeStylePreferences::currentTabSettingsChanged,
|
|
|
|
|
this, &ICodeStylePreferences::currentTabSettingsChanged);
|
|
|
|
|
disconnect(d->m_currentDelegate, &ICodeStylePreferences::currentValueChanged,
|
|
|
|
|
this, &ICodeStylePreferences::currentValueChanged);
|
|
|
|
|
disconnect(d->m_currentDelegate, &ICodeStylePreferences::currentPreferencesChanged,
|
|
|
|
|
this, &ICodeStylePreferences::currentPreferencesChanged);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
d->m_currentDelegate = delegate;
|
|
|
|
|
if (d->m_currentDelegate) {
|
2015-12-13 01:18:33 +02:00
|
|
|
connect(d->m_currentDelegate, &ICodeStylePreferences::currentTabSettingsChanged,
|
|
|
|
|
this, &ICodeStylePreferences::currentTabSettingsChanged);
|
|
|
|
|
connect(d->m_currentDelegate, &ICodeStylePreferences::currentValueChanged,
|
|
|
|
|
this, &ICodeStylePreferences::currentValueChanged);
|
|
|
|
|
connect(d->m_currentDelegate, &ICodeStylePreferences::currentPreferencesChanged,
|
|
|
|
|
this, &ICodeStylePreferences::currentPreferencesChanged);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
emit currentDelegateChanged(d->m_currentDelegate);
|
|
|
|
|
emit currentPreferencesChanged(currentPreferences());
|
|
|
|
|
emit currentTabSettingsChanged(currentTabSettings());
|
|
|
|
|
emit currentValueChanged(currentValue());
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
QByteArray ICodeStylePreferences::currentDelegateId() const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
if (currentDelegate())
|
|
|
|
|
return currentDelegate()->id();
|
|
|
|
|
return id(); // or 0?
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-20 10:35:16 +02:00
|
|
|
void ICodeStylePreferences::setCurrentDelegate(const QByteArray &id)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
if (d->m_pool)
|
|
|
|
|
setCurrentDelegate(d->m_pool->codeStyle(id));
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 18:48:32 +01:00
|
|
|
void ICodeStylePreferences::setSettingsSuffix(const QString &suffix)
|
|
|
|
|
{
|
|
|
|
|
d->m_settingsSuffix = suffix;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 10:45:23 +02:00
|
|
|
void ICodeStylePreferences::toSettings(const QString &category, QSettings *s) const
|
|
|
|
|
{
|
2020-01-28 18:48:32 +01:00
|
|
|
Utils::toSettings(d->m_settingsSuffix, category, s, this);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:41:12 +02:00
|
|
|
void ICodeStylePreferences::fromSettings(const QString &category, QSettings *s)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2020-01-28 18:48:32 +01:00
|
|
|
Utils::fromSettings(d->m_settingsSuffix, category, s, this);
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:41:12 +02:00
|
|
|
QVariantMap ICodeStylePreferences::toMap() const
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
if (!currentDelegate())
|
2021-12-01 15:41:12 +02:00
|
|
|
return d->m_tabSettings.toMap();
|
2022-12-07 19:48:18 +01:00
|
|
|
return {{currentPreferencesKey, currentDelegateId()}};
|
2011-08-16 10:45:23 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 15:41:12 +02:00
|
|
|
void ICodeStylePreferences::fromMap(const QVariantMap &map)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
2021-12-01 15:41:12 +02:00
|
|
|
d->m_tabSettings.fromMap(map);
|
|
|
|
|
const QByteArray delegateId = map.value(currentPreferencesKey).toByteArray();
|
2011-08-16 10:45:23 +02:00
|
|
|
if (delegatingPool()) {
|
|
|
|
|
ICodeStylePreferences *delegate = delegatingPool()->codeStyle(delegateId);
|
|
|
|
|
if (!delegateId.isEmpty() && delegate)
|
|
|
|
|
setCurrentDelegate(delegate);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-18 10:30:00 +02:00
|
|
|
void ICodeStylePreferences::codeStyleRemoved(ICodeStylePreferences *preferences)
|
2011-08-16 10:45:23 +02:00
|
|
|
{
|
|
|
|
|
if (currentDelegate() == preferences) {
|
|
|
|
|
CodeStylePool *pool = delegatingPool();
|
|
|
|
|
QList<ICodeStylePreferences *> codeStyles = pool->codeStyles();
|
|
|
|
|
const int idx = codeStyles.indexOf(preferences);
|
2018-09-20 01:16:01 +03:00
|
|
|
ICodeStylePreferences *newCurrentPreferences = nullptr;
|
2011-08-16 10:45:23 +02:00
|
|
|
int i = idx + 1;
|
|
|
|
|
// go forward
|
|
|
|
|
while (i < codeStyles.count()) {
|
|
|
|
|
ICodeStylePreferences *prefs = codeStyles.at(i);
|
|
|
|
|
if (prefs->id() != id()) {
|
|
|
|
|
newCurrentPreferences = prefs;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
// go backward if still empty
|
|
|
|
|
if (!newCurrentPreferences) {
|
|
|
|
|
i = idx - 1;
|
|
|
|
|
while (i >= 0) {
|
|
|
|
|
ICodeStylePreferences *prefs = codeStyles.at(i);
|
|
|
|
|
if (prefs->id() != id()) {
|
|
|
|
|
newCurrentPreferences = prefs;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
setCurrentDelegate(newCurrentPreferences);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 18:48:32 +01:00
|
|
|
} // TextEditor
|