Commit 09aad1ac authored by hjk's avatar hjk Committed by David Schulz

CodeStyleSettings: De-noise user code by using QByteArray ids.

Change-Id: Ia7210b0ff85667951e4e5554155d89ed6925f9ec
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent e8a1b8c5
......@@ -149,7 +149,7 @@ private:
QList<TestDocumentPtr> testFiles;
CppCodeStylePreferences *cppCodeStylePreferences;
QString cppCodeStylePreferencesOriginalDelegateId;
QByteArray cppCodeStylePreferencesOriginalDelegateId;
QStringList includePathsToRestore;
bool restoreIncludePaths;
......@@ -249,7 +249,7 @@ void TestCase::init(const QStringList &includePaths)
cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
QVERIFY(cppCodeStylePreferences);
cppCodeStylePreferencesOriginalDelegateId = cppCodeStylePreferences->currentDelegateId();
cppCodeStylePreferences->setCurrentDelegate(QLatin1String("qt"));
cppCodeStylePreferences->setCurrentDelegate("qt");
}
TestCase::~TestCase()
......
......@@ -101,7 +101,7 @@ CppToolsSettings::CppToolsSettings(QObject *parent)
d->m_globalCodeStyle = new CppCodeStylePreferences(this);
d->m_globalCodeStyle->setDelegatingPool(pool);
d->m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
d->m_globalCodeStyle->setId(QLatin1String(idKey));
d->m_globalCodeStyle->setId(idKey);
pool->addCodeStyle(d->m_globalCodeStyle);
TextEditorSettings::registerCodeStyle(CppTools::Constants::CPP_SETTINGS_ID, d->m_globalCodeStyle);
......@@ -133,7 +133,7 @@ CppToolsSettings::CppToolsSettings(QObject *parent)
// built-in settings
// Qt style
CppCodeStylePreferences *qtCodeStyle = new CppCodeStylePreferences();
qtCodeStyle->setId(QLatin1String("qt"));
qtCodeStyle->setId("qt");
qtCodeStyle->setDisplayName(tr("Qt"));
qtCodeStyle->setReadOnly(true);
TabSettings qtTabSettings;
......@@ -146,7 +146,7 @@ CppToolsSettings::CppToolsSettings(QObject *parent)
// GNU style
CppCodeStylePreferences *gnuCodeStyle = new CppCodeStylePreferences();
gnuCodeStyle->setId(QLatin1String("gnu"));
gnuCodeStyle->setId("gnu");
gnuCodeStyle->setDisplayName(tr("GNU"));
gnuCodeStyle->setReadOnly(true);
TabSettings gnuTabSettings;
......@@ -208,8 +208,7 @@ CppToolsSettings::CppToolsSettings(QObject *parent)
QVariant v;
v.setValue(legacyCodeStyleSettings);
TextEditor::ICodeStylePreferences *oldCreator = pool->createCodeStyle(
QLatin1String("legacy"), legacyTabSettings,
v, tr("Old Creator"));
"legacy", legacyTabSettings, v, tr("Old Creator"));
// change the current delegate and save
d->m_globalCodeStyle->setCurrentDelegate(oldCreator);
......
......@@ -53,7 +53,6 @@ static const QLatin1String kUseGlobal("EditorConfiguration.UseGlobal");
static const QLatin1String kCodec("EditorConfiguration.Codec");
static const QLatin1String kCodeStylePrefix("EditorConfiguration.CodeStyle.");
static const QLatin1String kCodeStyleCount("EditorConfiguration.CodeStyle.Count");
static const QLatin1String kId("Project");
using namespace TextEditor;
......@@ -93,7 +92,7 @@ EditorConfiguration::EditorConfiguration() : d(new EditorConfigurationPrivate)
ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(languageId);
ICodeStylePreferences *preferences = factory->createCodeStyle();
preferences->setDelegatingPool(TextEditorSettings::codeStylePool(languageId));
preferences->setId(languageId.toString() + QLatin1String("Project"));
preferences->setId(languageId.name() + "Project");
preferences->setDisplayName(tr("Project %1", "Settings, %1 is a language (C++ or QML)").arg(factory->displayName()));
preferences->setCurrentDelegate(originalPreferences);
d->m_languageCodeStylePreferences.insert(languageId, preferences);
......@@ -102,7 +101,7 @@ EditorConfiguration::EditorConfiguration() : d(new EditorConfigurationPrivate)
d->m_defaultCodeStyle = new SimpleCodeStylePreferences(this);
d->m_defaultCodeStyle->setDelegatingPool(TextEditorSettings::codeStylePool());
d->m_defaultCodeStyle->setDisplayName(tr("Project", "Settings"));
d->m_defaultCodeStyle->setId(kId);
d->m_defaultCodeStyle->setId("Project");
d->m_defaultCodeStyle->setCurrentDelegate(d->m_useGlobal ? TextEditorSettings::codeStyle() : 0);
}
......
......@@ -67,14 +67,14 @@ QmlJSToolsSettings::QmlJSToolsSettings(QObject *parent)
m_globalCodeStyle = new SimpleCodeStylePreferences(this);
m_globalCodeStyle->setDelegatingPool(pool);
m_globalCodeStyle->setDisplayName(tr("Global", "Settings"));
m_globalCodeStyle->setId(QLatin1String(idKey));
m_globalCodeStyle->setId(idKey);
pool->addCodeStyle(m_globalCodeStyle);
TextEditorSettings::registerCodeStyle(QmlJSTools::Constants::QML_JS_SETTINGS_ID, m_globalCodeStyle);
// built-in settings
// Qt style
SimpleCodeStylePreferences *qtCodeStyle = new SimpleCodeStylePreferences();
qtCodeStyle->setId(QLatin1String("qt"));
qtCodeStyle->setId("qt");
qtCodeStyle->setDisplayName(tr("Qt"));
qtCodeStyle->setReadOnly(true);
TabSettings qtTabSettings;
......@@ -122,8 +122,7 @@ QmlJSToolsSettings::QmlJSToolsSettings(QObject *parent)
// create custom code style out of old settings
ICodeStylePreferences *oldCreator = pool->createCodeStyle(
QLatin1String("legacy"), legacyTabSettings,
QVariant(), tr("Old Creator"));
"legacy", legacyTabSettings, QVariant(), tr("Old Creator"));
// change the current delegate and save
m_globalCodeStyle->setCurrentDelegate(oldCreator);
......
......@@ -98,7 +98,7 @@ BehaviorSettingsPage::BehaviorSettingsPage(const BehaviorSettingsPageParameters
// global tab preferences for all other languages
d->m_codeStyle = new SimpleCodeStylePreferences(this);
d->m_codeStyle->setDisplayName(tr("Global", "Settings"));
d->m_codeStyle->setId(QLatin1String(Constants::GLOBAL_SETTINGS_ID));
d->m_codeStyle->setId(Constants::GLOBAL_SETTINGS_ID);
// default pool for all other languages
d->m_defaultCodeStylePool = new CodeStylePool(0, this); // Any language
......
......@@ -55,13 +55,13 @@ public:
{}
~CodeStylePoolPrivate();
QString generateUniqueId(const QString &id) const;
QByteArray generateUniqueId(const QByteArray &id) const;
ICodeStylePreferencesFactory *m_factory;
QList<ICodeStylePreferences *> m_pool;
QList<ICodeStylePreferences *> m_builtInPool;
QList<ICodeStylePreferences *> m_customPool;
QMap<QString, ICodeStylePreferences *> m_idToCodeStyle;
QMap<QByteArray, ICodeStylePreferences *> m_idToCodeStyle;
QString m_settingsPath;
};
......@@ -70,23 +70,23 @@ CodeStylePoolPrivate::~CodeStylePoolPrivate()
delete m_factory;
}
QString CodeStylePoolPrivate::generateUniqueId(const QString &id) const
QByteArray CodeStylePoolPrivate::generateUniqueId(const QByteArray &id) const
{
if (!id.isEmpty() && !m_idToCodeStyle.contains(id))
return id;
int idx = id.size();
while (idx > 0) {
if (!id.at(idx - 1).isDigit())
if (!isdigit(id.at(idx - 1)))
break;
idx--;
}
const QString baseName = id.left(idx);
QString newName = baseName.isEmpty() ? QLatin1String("codestyle") : baseName;
const QByteArray baseName = id.left(idx);
QByteArray newName = baseName.isEmpty() ? "codestyle" : baseName;
int i = 2;
while (m_idToCodeStyle.contains(newName))
newName = baseName + QString::number(i++);
newName = baseName + QByteArray::number(i++);
return newName;
}
......@@ -119,10 +119,10 @@ QString CodeStylePool::settingsDir() const
return customCodeStylesPath().append(suffix);
}
Utils::FileName CodeStylePool::settingsPath(const QString &id) const
Utils::FileName CodeStylePool::settingsPath(const QByteArray &id) const
{
Utils::FileName path = Utils::FileName::fromString(settingsDir());
path.appendPath(id + QLatin1String(".xml"));
path.appendPath(QString::fromUtf8(id + ".xml"));
return path;
}
......@@ -147,7 +147,7 @@ ICodeStylePreferences *CodeStylePool::cloneCodeStyle(ICodeStylePreferences *orig
originalCodeStyle->value(), originalCodeStyle->displayName());
}
ICodeStylePreferences *CodeStylePool::createCodeStyle(const QString &id, const TabSettings &tabSettings,
ICodeStylePreferences *CodeStylePool::createCodeStyle(const QByteArray &id, const TabSettings &tabSettings,
const QVariant &codeStyleData, const QString &displayName)
{
if (!d->m_factory)
......@@ -168,7 +168,7 @@ ICodeStylePreferences *CodeStylePool::createCodeStyle(const QString &id, const T
void CodeStylePool::addCodeStyle(ICodeStylePreferences *codeStyle)
{
const QString newId = d->generateUniqueId(codeStyle->id());
const QByteArray newId = d->generateUniqueId(codeStyle->id());
codeStyle->setId(newId);
d->m_pool.append(codeStyle);
......@@ -206,7 +206,7 @@ void CodeStylePool::removeCodeStyle(ICodeStylePreferences *codeStyle)
delete codeStyle;
}
ICodeStylePreferences *CodeStylePool::codeStyle(const QString &id) const
ICodeStylePreferences *CodeStylePool::codeStyle(const QByteArray &id) const
{
return d->m_idToCodeStyle.value(id);
}
......@@ -218,7 +218,7 @@ void CodeStylePool::loadCustomCodeStyles()
for (int i = 0; i < codeStyleFiles.count(); i++) {
const QString codeStyleFile = codeStyleFiles.at(i);
// filter out styles which id is the same as one of built-in styles
if (!d->m_idToCodeStyle.contains(QFileInfo(codeStyleFile).completeBaseName()))
if (!d->m_idToCodeStyle.contains(QFileInfo(codeStyleFile).completeBaseName().toUtf8()))
loadCodeStyle(Utils::FileName::fromString(dir.absoluteFilePath(codeStyleFile)));
}
}
......@@ -238,7 +238,7 @@ ICodeStylePreferences *CodeStylePool::loadCodeStyle(const Utils::FileName &fileN
reader.load(fileName);
QVariantMap m = reader.restoreValues();
if (m.contains(QLatin1String(codeStyleDataKey))) {
const QString id = fileName.toFileInfo().completeBaseName();
const QByteArray id = fileName.toFileInfo().completeBaseName().toUtf8();
const QString displayName = reader.restoreValue(QLatin1String(displayNameKey)).toString();
const QVariantMap map = reader.restoreValue(QLatin1String(codeStyleDataKey)).toMap();
if (d->m_factory) {
......
......@@ -58,14 +58,14 @@ public:
QList<ICodeStylePreferences *> customCodeStyles() const;
ICodeStylePreferences *cloneCodeStyle(ICodeStylePreferences *originalCodeStyle);
ICodeStylePreferences *createCodeStyle(const QString &id, const TabSettings &tabSettings,
ICodeStylePreferences *createCodeStyle(const QByteArray &id, const TabSettings &tabSettings,
const QVariant &codeStyleData, const QString &displayName);
// ownership is passed to the pool
void addCodeStyle(ICodeStylePreferences *codeStyle);
// is removed and deleted
void removeCodeStyle(ICodeStylePreferences *codeStyle);
ICodeStylePreferences *codeStyle(const QString &id) const;
ICodeStylePreferences *codeStyle(const QByteArray &id) const;
void loadCustomCodeStyles();
......@@ -81,7 +81,7 @@ private slots:
private:
QString settingsDir() const;
Utils::FileName settingsPath(const QString &id) const;
Utils::FileName settingsPath(const QByteArray &id) const;
ICodeStylePreferences *loadCodeStyle(const Utils::FileName &fileName);
void saveCodeStyle(ICodeStylePreferences *codeStyle) const;
......
......@@ -354,7 +354,7 @@ void CodeStyleSelectorWidget::slotExportClicked()
{
ICodeStylePreferences *currentPreferences = m_codeStyle->currentPreferences();
const QString fileName = QFileDialog::getSaveFileName(this, tr("Export Code Style"),
currentPreferences->id() + QLatin1String(".xml"),
QString::fromUtf8(currentPreferences->id() + ".xml"),
tr("Code styles (*.xml);;All files (*)"));
if (!fileName.isEmpty()) {
CodeStylePool *codeStylePool = m_codeStyle->delegatingPool();
......
......@@ -53,7 +53,7 @@ public:
CodeStylePool *m_pool;
ICodeStylePreferences *m_currentDelegate;
TabSettings m_tabSettings;
QString m_id;
QByteArray m_id;
QString m_displayName;
bool m_readOnly;
};
......@@ -72,12 +72,12 @@ ICodeStylePreferences::~ICodeStylePreferences()
delete d;
}
QString ICodeStylePreferences::id() const
QByteArray ICodeStylePreferences::id() const
{
return d->m_id;
}
void ICodeStylePreferences::setId(const QString &name)
void ICodeStylePreferences::setId(const QByteArray &name)
{
d->m_id = name;
}
......@@ -203,14 +203,14 @@ void ICodeStylePreferences::setCurrentDelegate(ICodeStylePreferences *delegate)
emit currentValueChanged(currentValue());
}
QString ICodeStylePreferences::currentDelegateId() const
QByteArray ICodeStylePreferences::currentDelegateId() const
{
if (currentDelegate())
return currentDelegate()->id();
return id(); // or 0?
}
void ICodeStylePreferences::setCurrentDelegate(const QString &id)
void ICodeStylePreferences::setCurrentDelegate(const QByteArray &id)
{
if (d->m_pool)
setCurrentDelegate(d->m_pool->codeStyle(id));
......@@ -237,7 +237,7 @@ void ICodeStylePreferences::toMap(const QString &prefix, QVariantMap *map) const
void ICodeStylePreferences::fromMap(const QString &prefix, const QVariantMap &map)
{
d->m_tabSettings.fromMap(prefix, map);
const QString delegateId = map.value(prefix + QLatin1String(currentPreferencesKey)).toString();
const QByteArray delegateId = map.value(prefix + QLatin1String(currentPreferencesKey)).toByteArray();
if (delegatingPool()) {
ICodeStylePreferences *delegate = delegatingPool()->codeStyle(delegateId);
if (!delegateId.isEmpty() && delegate)
......
......@@ -57,8 +57,8 @@ public:
explicit ICodeStylePreferences(QObject *parentObject = 0);
virtual ~ICodeStylePreferences();
QString id() const;
void setId(const QString &name);
QByteArray id() const;
void setId(const QByteArray &name);
QString displayName() const;
void setDisplayName(const QString &name);
......@@ -83,8 +83,8 @@ public:
ICodeStylePreferences *currentDelegate() const; // null or one of delegates from the pool
void setCurrentDelegate(ICodeStylePreferences *delegate);
QString currentDelegateId() const;
void setCurrentDelegate(const QString &id);
QByteArray currentDelegateId() const;
void setCurrentDelegate(const QByteArray &id);
void toSettings(const QString &category, QSettings *s) const;
void fromSettings(const QString &category, const QSettings *s);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment