Commit 22449118 authored by Jarek Kobus's avatar Jarek Kobus Committed by Jarek Kobus

Implement Code Style schemes

Task-number: QTCREATORBUG-5092

Change-Id: I218cef02f7c242e4dfae59b1b8021ea618e60d07
Reviewed-on: http://codereview.qt-project.org/5160Reviewed-by: default avatarLeandro T. C. Melo <leandro.melo@nokia.com>
parent 3955df78
......@@ -47,8 +47,9 @@
#include <utils/qtcassert.h>
/*!
\class ProjectExplorer::PersistentSettingsReader
\class Utils::PersistentSettingsReader
\brief Reads a QVariantMap of arbitrary, nested data structures from a XML file.
......@@ -80,10 +81,10 @@
and added to the stack entry below (added to list or inserted into map). The first element
of the stack is the value of the <data> element.
\sa ProjectExplorer::PersistentSettingsWriter
\sa Utils::PersistentSettingsWriter
*/
namespace ProjectExplorer {
namespace Utils {
struct Context // Basic context containing element name string constants.
{
......@@ -300,7 +301,7 @@ PersistentSettingsReader::PersistentSettingsReader()
{
}
QVariant PersistentSettingsReader::restoreValue(const QString & variable) const
QVariant PersistentSettingsReader::restoreValue(const QString &variable) const
{
if (m_valueMap.contains(variable))
return m_valueMap.value(variable);
......@@ -312,7 +313,7 @@ QVariantMap PersistentSettingsReader::restoreValues() const
return m_valueMap;
}
bool PersistentSettingsReader::load(const QString & fileName)
bool PersistentSettingsReader::load(const QString &fileName)
{
m_valueMap.clear();
......@@ -326,10 +327,10 @@ bool PersistentSettingsReader::load(const QString & fileName)
}
/*!
\class ProjectExplorer::PersistentSettingsWriter
\class Utils::PersistentSettingsWriter
\brief Serializes a QVariantMap of arbitrary, nested data structures to a XML file.
\sa ProjectExplorer::PersistentSettingsReader
\sa Utils::PersistentSettingsReader
*/
PersistentSettingsWriter::PersistentSettingsWriter()
......@@ -373,12 +374,12 @@ static void writeVariantValue(QXmlStreamWriter &w, const Context &ctx,
}
}
void PersistentSettingsWriter::saveValue(const QString & variable, const QVariant &value)
void PersistentSettingsWriter::saveValue(const QString &variable, const QVariant &value)
{
m_valueMap.insert(variable, value);
}
bool PersistentSettingsWriter::save(const QString & fileName, const QString &docType,
bool PersistentSettingsWriter::save(const QString &fileName, const QString &docType,
QWidget *parent) const
{
Utils::FileSaver saver(fileName, QIODevice::Text);
......@@ -406,4 +407,4 @@ bool PersistentSettingsWriter::save(const QString & fileName, const QString &doc
}
return saver.finalize(parent);
}
} // namespace ProjectExplorer
} // namespace Utils
......@@ -33,7 +33,7 @@
#ifndef PERSISTENTSETTINGS_H
#define PERSISTENTSETTINGS_H
#include "projectexplorer_export.h"
#include "utils_global.h"
#include <QtCore/QMap>
#include <QtCore/QVariant>
......@@ -42,31 +42,31 @@ QT_BEGIN_NAMESPACE
class QWidget;
QT_END_NAMESPACE
namespace ProjectExplorer {
namespace Utils {
class PROJECTEXPLORER_EXPORT PersistentSettingsReader
class QTCREATOR_UTILS_EXPORT PersistentSettingsReader
{
public:
PersistentSettingsReader();
QVariant restoreValue(const QString & variable) const;
QVariant restoreValue(const QString &variable) const;
QVariantMap restoreValues() const;
bool load(const QString & fileName);
bool load(const QString &fileName);
private:
QMap<QString, QVariant> m_valueMap;
};
class PROJECTEXPLORER_EXPORT PersistentSettingsWriter
class QTCREATOR_UTILS_EXPORT PersistentSettingsWriter
{
public:
PersistentSettingsWriter();
void saveValue(const QString & variable, const QVariant &value);
void saveValue(const QString &variable, const QVariant &value);
bool save(const QString &fileName, const QString &docType, QWidget *parent) const;
private:
QMap<QString, QVariant> m_valueMap;
};
} // namespace ProjectExplorer
} // namespace Utils
#endif // PERSISTENTSETTINGS_H
......@@ -90,7 +90,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/ssh/sshkeypasswordretriever.cpp \
$$PWD/outputformatter.cpp \
$$PWD/flowlayout.cpp \
$$PWD/networkaccessmanager.cpp
$$PWD/networkaccessmanager.cpp \
$$PWD/persistentsettings.cpp
win32 {
SOURCES += \
......@@ -194,7 +195,8 @@ HEADERS += \
$$PWD/outputformatter.h \
$$PWD/outputformat.h \
$$PWD/flowlayout.h \
$$PWD/networkaccessmanager.h
$$PWD/networkaccessmanager.h \
$$PWD/persistentsettings.h
FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \
......
......@@ -53,9 +53,6 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/navigationwidget.h>
#include <texteditor/fontsettings.h>
#include <texteditor/tabpreferences.h>
#include <texteditor/storagesettings.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorplugin.h>
#include <texteditor/texteditorsettings.h>
......
......@@ -36,10 +36,8 @@ using namespace CppTools;
static const char *settingsSuffixKey = "CodeStyleSettings";
static const char *currentFallbackKey = "CurrentFallback";
CppCodeStylePreferences::CppCodeStylePreferences(const QList<TextEditor::IFallbackPreferences *> &fallbacks, QObject *parent) :
IFallbackPreferences(fallbacks, parent)
CppCodeStylePreferences::CppCodeStylePreferences(QObject *parent) :
ICodeStylePreferences(parent)
{
connect(this, SIGNAL(currentValueChanged(QVariant)),
this, SLOT(slotCurrentValueChanged(QVariant)));
......@@ -48,7 +46,7 @@ CppCodeStylePreferences::CppCodeStylePreferences(const QList<TextEditor::IFallba
QVariant CppCodeStylePreferences::value() const
{
QVariant v;
v.setValue(settings());
v.setValue(codeStyleSettings());
return v;
}
......@@ -57,15 +55,15 @@ void CppCodeStylePreferences::setValue(const QVariant &data)
if (!data.canConvert<CppCodeStyleSettings>())
return;
setSettings(data.value<CppCodeStyleSettings>());
setCodeStyleSettings(data.value<CppCodeStyleSettings>());
}
CppCodeStyleSettings CppCodeStylePreferences::settings() const
CppCodeStyleSettings CppCodeStylePreferences::codeStyleSettings() const
{
return m_data;
}
void CppCodeStylePreferences::setSettings(const CppCodeStyleSettings &data)
void CppCodeStylePreferences::setCodeStyleSettings(const CppCodeStyleSettings &data)
{
if (m_data == data)
return;
......@@ -75,13 +73,13 @@ void CppCodeStylePreferences::setSettings(const CppCodeStyleSettings &data)
QVariant v;
v.setValue(data);
emit valueChanged(v);
emit settingsChanged(m_data);
if (!currentFallback()) {
emit codeStyleSettingsChanged(m_data);
if (!currentDelegate()) {
emit currentValueChanged(v);
}
}
CppCodeStyleSettings CppCodeStylePreferences::currentSettings() const
CppCodeStyleSettings CppCodeStylePreferences::currentCodeStyleSettings() const
{
QVariant v = currentValue();
if (!v.canConvert<CppCodeStyleSettings>()) {
......@@ -96,7 +94,7 @@ void CppCodeStylePreferences::slotCurrentValueChanged(const QVariant &value)
if (!value.canConvert<CppCodeStyleSettings>())
return;
emit currentSettingsChanged(value.value<CppCodeStyleSettings>());
emit currentCodeStyleSettingsChanged(value.value<CppCodeStyleSettings>());
}
QString CppCodeStylePreferences::settingsSuffix() const
......@@ -106,13 +104,19 @@ QString CppCodeStylePreferences::settingsSuffix() const
void CppCodeStylePreferences::toMap(const QString &prefix, QVariantMap *map) const
{
ICodeStylePreferences::toMap(prefix, map);
if (currentDelegate())
return;
m_data.toMap(prefix, map);
map->insert(prefix + QLatin1String(currentFallbackKey), currentFallbackId());
}
void CppCodeStylePreferences::fromMap(const QString &prefix, const QVariantMap &map)
{
ICodeStylePreferences::fromMap(prefix, map);
if (currentDelegate())
return;
m_data.fromMap(prefix, map);
setCurrentFallback(map.value(prefix + QLatin1String(currentFallbackKey), QLatin1String("Global")).toString());
}
......@@ -35,35 +35,34 @@
#include "cpptools_global.h"
#include "cppcodestylesettings.h"
#include <texteditor/ifallbackpreferences.h>
#include <texteditor/icodestylepreferences.h>
namespace CppTools {
class CPPTOOLS_EXPORT CppCodeStylePreferences : public TextEditor::IFallbackPreferences
class CPPTOOLS_EXPORT CppCodeStylePreferences : public TextEditor::ICodeStylePreferences
{
Q_OBJECT
public:
explicit CppCodeStylePreferences(
const QList<TextEditor::IFallbackPreferences *> &fallbacks,
QObject *parent = 0);
virtual QVariant value() const;
virtual void setValue(const QVariant &);
CppCodeStyleSettings settings() const;
CppCodeStyleSettings codeStyleSettings() const;
// tracks parent hierarchy until currentParentSettings is null
CppCodeStyleSettings currentSettings() const;
CppCodeStyleSettings currentCodeStyleSettings() const;
virtual void toMap(const QString &prefix, QVariantMap *map) const;
virtual void fromMap(const QString &prefix, const QVariantMap &map);
public slots:
void setSettings(const CppTools::CppCodeStyleSettings &data);
void setCodeStyleSettings(const CppTools::CppCodeStyleSettings &data);
signals:
void settingsChanged(const CppTools::CppCodeStyleSettings &);
void currentSettingsChanged(const CppTools::CppCodeStyleSettings &);
void codeStyleSettingsChanged(const CppTools::CppCodeStyleSettings &);
void currentCodeStyleSettingsChanged(const CppTools::CppCodeStyleSettings &);
protected:
virtual QString settingsSuffix() const;
......
......@@ -30,18 +30,66 @@
**
**************************************************************************/
#include "cppcodestylesettingsfactory.h"
#include "cppcodestylepreferencesfactory.h"
#include "cppcodestylesettings.h"
#include "cppcodestylesettingspage.h"
#include "cppcodestylepreferences.h"
#include "cpptoolsconstants.h"
#include "cppqtstyleindenter.h"
#include <texteditor/tabpreferences.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/tabsettings.h>
#include <texteditor/snippets/isnippetprovider.h>
#include <cppeditor/cppeditorconstants.h>
#include <QtGui/QLayout>
using namespace CppTools;
static const char *defaultPreviewText =
"#include <math.h>\n"
"\n"
"class Complex\n"
" {\n"
"public:\n"
" Complex(double re, double im)\n"
" : _re(re), _im(im)\n"
" {}\n"
" double modulus() const\n"
" {\n"
" return sqrt(_re * _re + _im * _im);\n"
" }\n"
"private:\n"
" double _re;\n"
" double _im;\n"
" };\n"
"\n"
"void bar(int i)\n"
" {\n"
" static int counter = 0;\n"
" counter += i;\n"
" }\n"
"\n"
"namespace Foo\n"
" {\n"
" namespace Bar\n"
" {\n"
" void foo(int a, int b)\n"
" {\n"
" for (int i = 0; i < a; i++)\n"
" {\n"
" if (i < b)\n"
" bar(i);\n"
" else\n"
" {\n"
" bar(i);\n"
" bar(b);\n"
" }\n"
" }\n"
" }\n"
" } // namespace Bar\n"
" } // namespace Foo\n"
;
CppCodeStylePreferencesFactory::CppCodeStylePreferencesFactory()
{
}
......@@ -56,14 +104,12 @@ QString CppCodeStylePreferencesFactory::displayName()
return Constants::CPP_SETTINGS_NAME;
}
TextEditor::IFallbackPreferences *CppCodeStylePreferencesFactory::createPreferences(
const QList<TextEditor::IFallbackPreferences *> &fallbacks) const
TextEditor::ICodeStylePreferences *CppCodeStylePreferencesFactory::createCodeStyle() const
{
return new CppCodeStylePreferences(fallbacks);
return new CppCodeStylePreferences();
}
QWidget *CppCodeStylePreferencesFactory::createEditor(TextEditor::IFallbackPreferences *preferences,
TextEditor::TabPreferences *tabPreferences,
QWidget *CppCodeStylePreferencesFactory::createEditor(TextEditor::ICodeStylePreferences *preferences,
QWidget *parent) const
{
CppCodeStylePreferences *cppPreferences = qobject_cast<CppCodeStylePreferences *>(preferences);
......@@ -71,7 +117,7 @@ QWidget *CppCodeStylePreferencesFactory::createEditor(TextEditor::IFallbackPrefe
return 0;
Internal::CppCodeStylePreferencesWidget *widget = new Internal::CppCodeStylePreferencesWidget(parent);
widget->layout()->setMargin(0);
widget->setPreferences(cppPreferences, tabPreferences);
widget->setCodeStyle(cppPreferences);
return widget;
}
......@@ -80,3 +126,18 @@ TextEditor::Indenter *CppCodeStylePreferencesFactory::createIndenter() const
return new CppQtStyleIndenter();
}
TextEditor::ISnippetProvider *CppCodeStylePreferencesFactory::snippetProvider() const
{
const QList<TextEditor::ISnippetProvider *> &providers =
ExtensionSystem::PluginManager::instance()->getObjects<TextEditor::ISnippetProvider>();
foreach (TextEditor::ISnippetProvider *provider, providers)
if (provider->groupId() == QLatin1String(CppEditor::Constants::CPP_SNIPPETS_GROUP_ID))
return provider;
return 0;
}
QString CppCodeStylePreferencesFactory::previewText() const
{
return QLatin1String(defaultPreviewText);
}
......@@ -30,8 +30,8 @@
**
**************************************************************************/
#ifndef CPPCODESTYLESETTINGSFACTORY_H
#define CPPCODESTYLESETTINGSFACTORY_H
#ifndef CPPCODESTYLEPREFERENCESFACTORY_H
#define CPPCODESTYLEPREFERENCESFACTORY_H
#include <texteditor/icodestylepreferencesfactory.h>
......@@ -44,14 +44,14 @@ public:
virtual QString languageId();
virtual QString displayName();
virtual TextEditor::IFallbackPreferences *createPreferences(const QList<TextEditor::IFallbackPreferences *> &fallbacks) const;
virtual QWidget *createEditor(TextEditor::IFallbackPreferences *settings,
TextEditor::TabPreferences *tabSettings,
QWidget *parent) const;
virtual TextEditor::ICodeStylePreferences *createCodeStyle() const;
virtual QWidget *createEditor(TextEditor::ICodeStylePreferences *settings,
QWidget *parent) const;
virtual TextEditor::Indenter *createIndenter() const;
virtual TextEditor::ISnippetProvider *snippetProvider() const;
virtual QString previewText() const;
};
} // namespace CppTools
#endif // CPPCODESTYLESETTINGSFACTORY_H
#endif // CPPCODESTYLEPREFERENCESFACTORY_H
......@@ -48,9 +48,9 @@
namespace TextEditor {
class FontSettings;
class TabSettings;
class TabPreferences;
class IFallbackPreferences;
class ICodeStylePreferences;
class SnippetEditorWidget;
class CodeStyleEditor;
}
namespace CppTools {
......@@ -70,24 +70,24 @@ public:
explicit CppCodeStylePreferencesWidget(QWidget *parent = 0);
virtual ~CppCodeStylePreferencesWidget();
void setPreferences(CppTools::CppCodeStylePreferences *codeStylePreferences,
TextEditor::TabPreferences *tabPreferences);
void setCodeStyle(CppTools::CppCodeStylePreferences *codeStylePreferences);
QString searchKeywords() const;
private slots:
void decorateEditors(const TextEditor::FontSettings &fontSettings);
void setVisualizeWhitespace(bool on);
void slotCppCodeStyleSettingsChanged();
void slotTabSettingsChanged(const TextEditor::TabSettings &settings);
void slotCodeStyleSettingsChanged();
void updatePreview();
void setCppCodeStyleSettings(const CppTools::CppCodeStyleSettings &settings, bool preview = true);
void slotCurrentPreferencesChanged(TextEditor::IFallbackPreferences *, bool preview = true);
void setTabSettings(const TextEditor::TabSettings &settings);
void setCodeStyleSettings(const CppTools::CppCodeStyleSettings &settings, bool preview = true);
void slotCurrentPreferencesChanged(TextEditor::ICodeStylePreferences *, bool preview = true);
private:
CppCodeStyleSettings cppCodeStyleSettings() const;
TextEditor::TabPreferences *m_tabPreferences;
CppCodeStylePreferences *m_cppCodeStylePreferences;
CppCodeStylePreferences *m_preferences;
Ui::CppCodeStyleSettingsPage *m_ui;
QList<TextEditor::SnippetEditorWidget *> m_previews;
bool m_blockUpdates;
......@@ -115,9 +115,8 @@ public:
private:
QString m_searchKeywords;
TextEditor::TabPreferences *m_pageTabPreferences;
CppCodeStylePreferences *m_pageCppCodeStylePreferences;
QPointer<CppCodeStylePreferencesWidget> m_widget;
QPointer<TextEditor::CodeStyleEditor> m_widget;
};
} // namespace Internal
......
......@@ -6,63 +6,57 @@
<rect>
<x>0</x>
<y>0</y>
<width>757</width>
<height>465</height>
<width>463</width>
<height>314</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QWidget" name="fallbackContainer" native="true">
<layout class="QHBoxLayout" name="fallbackLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="TextEditor::FallbackSelectorWidget" name="fallbackWidget" native="true"/>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QTabWidget" name="categoryTab">
<property name="currentIndex">
<number>4</number>
<number>0</number>
</property>
<widget class="QWidget" name="generalTab">
<attribute name="title">
<string>General</string>
</attribute>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="TextEditor::TabPreferencesWidget" name="tabPreferencesWidget" native="true">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="TextEditor::TabSettingsWidget" name="tabSettingsWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::TabFocus</enum>
</property>
</widget>
</item>
<item>
<item row="0" column="1" rowspan="2">
<widget class="TextEditor::SnippetEditorWidget" name="previewTextEditGeneral">
<property name="plainText">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>347</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QWidget" name="contentTab">
......@@ -414,21 +408,15 @@ if they would align to the next line</string>
</widget>
<customwidgets>
<customwidget>
<class>TextEditor::SnippetEditorWidget</class>
<extends>QPlainTextEdit</extends>
<header location="global">texteditor/snippets/snippeteditor.h</header>
</customwidget>
<customwidget>
<class>TextEditor::TabPreferencesWidget</class>
<class>TextEditor::TabSettingsWidget</class>
<extends>QWidget</extends>
<header location="global">texteditor/tabpreferenceswidget.h</header>
<header location="global">texteditor/tabsettingswidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>TextEditor::FallbackSelectorWidget</class>
<extends>QWidget</extends>
<header location="global">texteditor/fallbackselectorwidget.h</header>
<container>1</container>
<class>TextEditor::SnippetEditorWidget</class>
<extends>QPlainTextEdit</extends>
<header location="global">texteditor/snippets/snippeteditor.h</header>
</customwidget>
</customwidgets>
<resources/>
......
......@@ -51,7 +51,7 @@ CppQtStyleIndenter::CppQtStyleIndenter()
{
// Just for safety. setCodeStylePreferences should be called when the editor the
// indenter belongs to gets initialized.
m_cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStylePreferences();
m_cppCodeStylePreferences = CppToolsSettings::instance()->cppCodeStyle();
}
CppQtStyleIndenter::~CppQtStyleIndenter()
......@@ -148,7 +148,7 @@ void CppQtStyleIndenter::indent(QTextDocument *doc,
}
}
void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences)
void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences)
{
CppTools::CppCodeStylePreferences *cppCodeStylePreferences
= qobject_cast<CppTools::CppCodeStylePreferences *>(preferences);
......@@ -156,9 +156,15 @@ void CppQtStyleIndenter::setCodeStylePreferences(TextEditor::IFallbackPreference
m_cppCodeStylePreferences = cppCodeStylePreferences;
}
void CppQtStyleIndenter::invalidateCache(QTextDocument *doc)
{
CppTools::QtStyleCodeFormatter formatter;
formatter.invalidateCache(doc);
}
CppCodeStyleSettings CppQtStyleIndenter::codeStyleSettings() const
{
if (m_cppCodeStylePreferences)
return m_cppCodeStylePreferences->currentSettings();
return m_cppCodeStylePreferences->currentCodeStyleSettings();
return CppCodeStyleSettings();
}
......@@ -39,7 +39,7 @@
namespace TextEditor
{
class IFallbackPreferences;
class ICodeStylePreferences;
}
namespace CppTools {
......@@ -63,7 +63,8 @@ public:
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings);
virtual void setCodeStylePreferences(TextEditor::IFallbackPreferences *preferences);
virtual void setCodeStylePreferences(TextEditor::ICodeStylePreferences *preferences);
virtual void invalidateCache(QTextDocument *doc);
private:
CppCodeStyleSettings codeStyleSettings() const;
CppCodeStylePreferences *m_cppCodeStylePreferences;
......
......@@ -72,7 +72,7 @@ public:
ProjectExplorer::actualTabSettings(fileName, textEditor);
// TODO: add similar method like above one
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings,
CppToolsSettings::instance()->cppCodeStylePreferences()->settings());
CppToolsSettings::instance()->cppCodeStyle()->codeStyleSettings());
codeFormatter.updateStateUntil(block);
do {
......
......@@ -33,7 +33,7 @@ HEADERS += completionsettingspage.h \
cppcodestylesettingspage.h \
cpptoolssettings.h \
cppcodestylesettings.h \
cppcodestylesettingsfactory.h \