Commit ea313f3e authored by Leandro Melo's avatar Leandro Melo
Browse files

Text editor: Introduce per project settings

With some refactorings to make the code look better.

Reviewed-by: con
parent cbafc50a
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef SETTINGSUTILS_H
#define SETTINGSUTILS_H
#include <QtCore/QString>
#include <QtCore/QLatin1String>
#include <QtCore/QSettings>
#include <QtCore/QVariant>
namespace Utils {
template <class SettingsClassT>
void fromSettings(const QString &postFix,
const QString &category,
const QSettings *s,
SettingsClassT *obj)
{
QVariantMap map;
const QStringList &keys = s->allKeys();
foreach (const QString &key, keys)
map.insert(key, s->value(key));
QString group = postFix;
if (!category.isEmpty())
group.insert(0, category);
group += QLatin1Char('/');
obj->fromMap(group, map);
}
template <class SettingsClassT>
void toSettings(const QString &postFix,
const QString &category,
QSettings *s,
const SettingsClassT *obj)
{
QString group = postFix;
if (!category.isEmpty())
group.insert(0, category);
group += QLatin1Char('/');
QVariantMap map;
obj->toMap(group, &map);
QVariantMap::const_iterator it = map.constBegin();
for (; it != map.constEnd(); ++it)
s->setValue(it.key(), it.value());
}
} // Utils
#endif // SETTINGSUTILS_H
......@@ -167,7 +167,8 @@ HEADERS += $$PWD/environment.h \
$$PWD/ssh/sftpdefs.h \
$$PWD/ssh/sftpchannel.h \
$$PWD/ssh/sftpchannel_p.h \
$$PWD/ssh/sshremoteprocessrunner.h
$$PWD/ssh/sshremoteprocessrunner.h \
$$PWD/settingsutils.h
FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \
......
......@@ -220,7 +220,6 @@ struct EditorManagerPrivate {
OpenEditorsModel *m_editorModel;
IFile::ReloadSetting m_reloadSetting;
IFile::Utf8BomSetting m_utf8BomSetting;
QString m_titleAddition;
};
......@@ -242,8 +241,7 @@ EditorManagerPrivate::EditorManagerPrivate(ICore *core, QWidget *parent) :
m_goForwardAction(new QAction(QIcon(QLatin1String(Constants::ICON_NEXT)), EditorManager::tr("Go Forward"), parent)),
m_windowPopup(0),
m_coreListener(0),
m_reloadSetting(IFile::AlwaysAsk),
m_utf8BomSetting(IFile::OnlyKeep)
m_reloadSetting(IFile::AlwaysAsk)
{
m_editorModel = new OpenEditorsModel(parent);
}
......@@ -1803,14 +1801,12 @@ bool EditorManager::restoreState(const QByteArray &state)
static const char * const documentStatesKey = "EditorManager/DocumentStates";
static const char * const reloadBehaviorKey = "EditorManager/ReloadBehavior";
static const char * const utf8BomBehaviorKey = "EditorManager/Utf8BomBehavior";
void EditorManager::saveSettings()
{
SettingsDatabase *settings = m_d->m_core->settingsDatabase();
settings->setValue(QLatin1String(documentStatesKey), m_d->m_editorStates);
settings->setValue(QLatin1String(reloadBehaviorKey), m_d->m_reloadSetting);
settings->setValue(QLatin1String(utf8BomBehaviorKey), m_d->m_utf8BomSetting);
}
void EditorManager::readSettings()
......@@ -1830,9 +1826,6 @@ void EditorManager::readSettings()
if (settings->contains(QLatin1String(reloadBehaviorKey)))
m_d->m_reloadSetting = (IFile::ReloadSetting)settings->value(QLatin1String(reloadBehaviorKey)).toInt();
if (settings->contains(QLatin1String(utf8BomBehaviorKey)))
m_d->m_utf8BomSetting = (IFile::Utf8BomSetting)settings->value(QLatin1String(utf8BomBehaviorKey)).toInt();
}
......@@ -1900,17 +1893,7 @@ IFile::ReloadSetting EditorManager::reloadSetting() const
return m_d->m_reloadSetting;
}
void EditorManager::setUtf8BomSetting(IFile::Utf8BomSetting behavior)
{
m_d->m_utf8BomSetting = behavior;
}
IFile::Utf8BomSetting EditorManager::utf8BomSetting() const
{
return m_d->m_utf8BomSetting;
}
QTextCodec *EditorManager::defaultTextEncoding() const
QTextCodec *EditorManager::defaultTextCodec() const
{
QSettings *settings = Core::ICore::instance()->settings();
if (QTextCodec *candidate = QTextCodec::codecForName(
......
......@@ -187,10 +187,7 @@ public:
void setReloadSetting(IFile::ReloadSetting behavior);
IFile::ReloadSetting reloadSetting() const;
void setUtf8BomSetting(IFile::Utf8BomSetting behavior);
IFile::Utf8BomSetting utf8BomSetting() const;
QTextCodec *defaultTextEncoding() const;
QTextCodec *defaultTextCodec() const;
static qint64 maxTextFileSize();
......
......@@ -214,7 +214,7 @@ QString CppFileSettings::licenseTemplate(const QString &fileName, const QString
return QString();
}
QTextCodec *codec = Core::EditorManager::instance()->defaultTextEncoding();
QTextCodec *codec = Core::EditorManager::instance()->defaultTextCodec();
QTextStream licenseStream(&file);
licenseStream.setCodec(codec);
licenseStream.setAutoDetectUnicode(true);
......
......@@ -39,6 +39,7 @@
#include <cpptools/cppmodelmanager.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <projectexplorer/editorconfiguration.h>
#include <QtGui/QTextBlock>
......@@ -64,7 +65,9 @@ CppRefactoringFile CppRefactoringChanges::file(const QString &fileName)
return CppRefactoringFile(fileName, this);
}
void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const
void CppRefactoringChanges::indentSelection(const QTextCursor &selection,
const QString &fileName,
const TextEditor::BaseTextEditor *textEditor) const
{
// ### shares code with CPPEditor::indent()
QTextDocument *doc = selection.document();
......@@ -72,7 +75,8 @@ void CppRefactoringChanges::indentSelection(const QTextCursor &selection) const
QTextBlock block = doc->findBlock(selection.selectionStart());
const QTextBlock end = doc->findBlock(selection.selectionEnd()).next();
const TextEditor::TabSettings &tabSettings(TextEditor::TextEditorSettings::instance()->tabSettings());
const TextEditor::TabSettings &tabSettings =
ProjectExplorer::actualTabSettings(fileName, textEditor);
CppTools::QtStyleCodeFormatter codeFormatter(tabSettings);
codeFormatter.updateStateUntil(block);
......
......@@ -92,7 +92,9 @@ public:
CppRefactoringFile file(const QString &fileName);
private:
virtual void indentSelection(const QTextCursor &selection) const;
virtual void indentSelection(const QTextCursor &selection,
const QString &fileName,
const TextEditor::BaseTextEditor *textEditor) const;
virtual void fileChanged(const QString &fileName);
private:
......
......@@ -1169,9 +1169,12 @@ void FakeVimPluginPrivate::setUseFakeVim(const QVariant &value)
//core->updateAdditionalContexts(Core::Context(),
// Core::Context(FAKEVIM_CONTEXT));
showCommandBuffer(QString());
TabSettings ts = TextEditorSettings::instance()->tabSettings();
foreach (Core::IEditor *editor, m_editorToHandler.keys())
m_editorToHandler[editor]->restoreWidget(ts.m_tabSize);
foreach (Core::IEditor *editor, m_editorToHandler.keys()) {
if (TextEditor::BaseTextEditor *textEditor =
qobject_cast<TextEditor::BaseTextEditor *>(editor->widget())) {
m_editorToHandler[editor]->restoreWidget(textEditor->tabSettings().m_tabSize);
}
}
}
}
......
......@@ -111,9 +111,7 @@ Utils::FileIterator *AllProjectsFind::files() const
foreach (const QString &fileName, filteredFiles) {
QTextCodec *codec = openEditorEncodings.value(fileName);
if (!codec)
codec = project->editorConfiguration()->defaultTextCodec();
if (!codec)
codec = Core::EditorManager::instance()->defaultTextEncoding();
codec = project->editorConfiguration()->textCodec();
encodings.insert(fileName, codec);
}
}
......
......@@ -32,43 +32,324 @@
**************************************************************************/
#include "editorconfiguration.h"
#include "session.h"
#include "projectexplorer.h"
#include "project.h"
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>
#include <texteditor/storagesettings.h>
#include <texteditor/behaviorsettings.h>
#include <texteditor/extraencodingsettings.h>
#include <QtCore/QLatin1String>
#include <QtCore/QByteArray>
#include <QtCore/QTextCodec>
using namespace ProjectExplorer;
static const QLatin1String kPrefix("EditorConfiguration.");
static const QLatin1String kUseGlobal("EditorConfiguration.UseGlobal");
static const QLatin1String kCodec("EditorConfiguration.Codec");
using namespace TextEditor;
namespace ProjectExplorer {
struct EditorConfigurationPrivate
{
EditorConfigurationPrivate()
: m_useGlobal(true)
, m_tabSettings(TextEditorSettings::instance()->tabSettings())
, m_storageSettings(TextEditorSettings::instance()->storageSettings())
, m_behaviorSettings(TextEditorSettings::instance()->behaviorSettings())
, m_extraEncodingSettings(TextEditorSettings::instance()->extraEncodingSettings())
, m_textCodec(Core::EditorManager::instance()->defaultTextCodec())
{}
bool m_useGlobal;
TabSettings m_tabSettings;
StorageSettings m_storageSettings;
BehaviorSettings m_behaviorSettings;
ExtraEncodingSettings m_extraEncodingSettings;
QTextCodec *m_textCodec;
};
namespace {
const char * const CODEC("EditorConfiguration.Codec");
EditorConfiguration::EditorConfiguration() : m_d(new EditorConfigurationPrivate)
{
}
EditorConfiguration::EditorConfiguration()
: m_defaultTextCodec(0)
EditorConfiguration::~EditorConfiguration()
{
}
QTextCodec *EditorConfiguration::defaultTextCodec() const
bool EditorConfiguration::useGlobalSettings() const
{
return m_defaultTextCodec;
return m_d->m_useGlobal;
}
void EditorConfiguration::setDefaultTextCodec(QTextCodec *codec)
void EditorConfiguration::cloneGlobalSettings()
{
m_defaultTextCodec = codec;
m_d->m_tabSettings = TextEditorSettings::instance()->tabSettings();
m_d->m_storageSettings = TextEditorSettings::instance()->storageSettings();
m_d->m_behaviorSettings = TextEditorSettings::instance()->behaviorSettings();
m_d->m_extraEncodingSettings = TextEditorSettings::instance()->extraEncodingSettings();
m_d->m_textCodec = Core::EditorManager::instance()->defaultTextCodec();
emitTabSettingsChanged();
emitStorageSettingsChanged();
emitBehaviorSettingsChanged();
emitExtraEncodingSettingsChanged();
}
QTextCodec *EditorConfiguration::textCodec() const
{
return m_d->m_textCodec;
}
const TabSettings &EditorConfiguration::tabSettings() const
{
return m_d->m_tabSettings;
}
const StorageSettings &EditorConfiguration::storageSettings() const
{
return m_d->m_storageSettings;
}
const BehaviorSettings &EditorConfiguration::behaviorSettings() const
{
return m_d->m_behaviorSettings;
}
const ExtraEncodingSettings &EditorConfiguration::extraEncodingSettings() const
{
return m_d->m_extraEncodingSettings;
}
QVariantMap EditorConfiguration::toMap() const
{
QVariantMap map;
QByteArray name = "Default";
if (m_defaultTextCodec)
name = m_defaultTextCodec->name();
map.insert(QLatin1String(CODEC), name);
map.insert(kUseGlobal, m_d->m_useGlobal);
map.insert(kCodec, m_d->m_textCodec->name());
m_d->m_tabSettings.toMap(kPrefix, &map);
m_d->m_storageSettings.toMap(kPrefix, &map);
m_d->m_behaviorSettings.toMap(kPrefix, &map);
m_d->m_extraEncodingSettings.toMap(kPrefix, &map);
return map;
}
void EditorConfiguration::fromMap(const QVariantMap &map)
{
QByteArray name = map.value(QLatin1String(CODEC)).toString().toLocal8Bit();
QTextCodec *codec = QTextCodec::codecForName(name);
m_defaultTextCodec = codec;
m_d->m_useGlobal = map.value(kUseGlobal, m_d->m_useGlobal).toBool();
const QByteArray &codecName = map.value(kCodec, m_d->m_textCodec->name()).toByteArray();
m_d->m_textCodec = QTextCodec::codecForName(codecName);
if (!m_d->m_textCodec)
m_d->m_textCodec = Core::EditorManager::instance()->defaultTextCodec();
m_d->m_tabSettings.fromMap(kPrefix, map);
m_d->m_storageSettings.fromMap(kPrefix, map);
m_d->m_behaviorSettings.fromMap(kPrefix, map);
m_d->m_extraEncodingSettings.fromMap(kPrefix, map);
}
void EditorConfiguration::apply(ITextEditor *textEditor) const
{
if (!m_d->m_useGlobal) {
textEditor->setTextCodec(m_d->m_textCodec, ITextEditor::TextCodecFromProjectSetting);
if (BaseTextEditor *baseTextEditor = qobject_cast<BaseTextEditor *>(textEditor->widget()))
switchSettings(baseTextEditor);
}
}
void EditorConfiguration::setUseGlobalSettings(bool use)
{
m_d->m_useGlobal = use;
const SessionManager *session = ProjectExplorerPlugin::instance()->session();
QList<Core::IEditor *> opened = Core::EditorManager::instance()->openedEditors();
foreach (Core::IEditor *editor, opened) {
if (BaseTextEditor *baseTextEditor = qobject_cast<BaseTextEditor *>(editor->widget())) {
Project *project = session->projectForFile(editor->file()->fileName());
if (project && project->editorConfiguration() == this)
switchSettings(baseTextEditor);
}
}
}
void EditorConfiguration::switchSettings(BaseTextEditor *baseTextEditor) const
{
if (m_d->m_useGlobal)
switchSettings_helper(TextEditorSettings::instance(), this, baseTextEditor);
else
switchSettings_helper(this, TextEditorSettings::instance(), baseTextEditor);
}
template <class NewSenderT, class OldSenderT>
void EditorConfiguration::switchSettings_helper(const NewSenderT *newSender,
const OldSenderT *oldSender,
BaseTextEditor *baseTextEditor) const
{
baseTextEditor->setTabSettings(newSender->tabSettings());
baseTextEditor->setStorageSettings(newSender->storageSettings());
baseTextEditor->setBehaviorSettings(newSender->behaviorSettings());
baseTextEditor->setExtraEncodingSettings(newSender->extraEncodingSettings());
disconnect(oldSender, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
baseTextEditor, SLOT(setTabSettings(TextEditor::TabSettings)));
disconnect(oldSender, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
baseTextEditor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
disconnect(oldSender, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
baseTextEditor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
disconnect(oldSender, SIGNAL(extraEncodingSettingsChanged(TextEditor::ExtraEncodingSettings)),
baseTextEditor, SLOT(setExtraEncodingSettings(TextEditor::ExtraEncodingSettings)));
connect(newSender, SIGNAL(tabSettingsChanged(TextEditor::TabSettings)),
baseTextEditor, SLOT(setTabSettings(TextEditor::TabSettings)));
connect(newSender, SIGNAL(storageSettingsChanged(TextEditor::StorageSettings)),
baseTextEditor, SLOT(setStorageSettings(TextEditor::StorageSettings)));
connect(newSender, SIGNAL(behaviorSettingsChanged(TextEditor::BehaviorSettings)),
baseTextEditor, SLOT(setBehaviorSettings(TextEditor::BehaviorSettings)));
connect(newSender, SIGNAL(extraEncodingSettingsChanged(TextEditor::ExtraEncodingSettings)),
baseTextEditor, SLOT(setExtraEncodingSettings(TextEditor::ExtraEncodingSettings)));
}
void EditorConfiguration::setInsertSpaces(bool spaces)
{
m_d->m_tabSettings.m_spacesForTabs = spaces;
emitTabSettingsChanged();
}
void EditorConfiguration::setAutoInsertSpaces(bool autoSpaces)
{
m_d->m_tabSettings.m_autoSpacesForTabs = autoSpaces;
emitTabSettingsChanged();
}
void EditorConfiguration::setAutoIndent(bool autoIndent)
{
m_d->m_tabSettings.m_autoIndent = autoIndent;
emitTabSettingsChanged();
}
void EditorConfiguration::setSmartBackSpace(bool smartBackSpace)
{
m_d->m_tabSettings.m_smartBackspace = smartBackSpace;
emitTabSettingsChanged();
}
void EditorConfiguration::setTabSize(int size)
{
m_d->m_tabSettings.m_tabSize = size;
emitTabSettingsChanged();
}
void EditorConfiguration::setIndentSize(int size)
{
m_d->m_tabSettings.m_indentSize = size;
emitTabSettingsChanged();
}
void EditorConfiguration::setIndentBlocksBehavior(int index)
{
m_d->m_tabSettings.m_indentBraces = index >= 1;
m_d->m_tabSettings.m_doubleIndentBlocks = index >= 2;
emitTabSettingsChanged();
}
void EditorConfiguration::setTabKeyBehavior(int index)
{
m_d->m_tabSettings.m_tabKeyBehavior = (TabSettings::TabKeyBehavior)index;
emitTabSettingsChanged();
}
void EditorConfiguration::setContinuationAlignBehavior(int index)
{
m_d->m_tabSettings.m_continuationAlignBehavior = (TabSettings::ContinuationAlignBehavior)index;
emitTabSettingsChanged();
}
void EditorConfiguration::setCleanWhiteSpace(bool cleanWhiteSpace)
{
m_d->m_storageSettings.m_cleanWhitespace = cleanWhiteSpace;
emitStorageSettingsChanged();
}
void EditorConfiguration::setInEntireDocument(bool entireDocument)
{
m_d->m_storageSettings.m_inEntireDocument = entireDocument;
emitStorageSettingsChanged();
}
void EditorConfiguration::setAddFinalNewLine(bool newLine)
{
m_d->m_storageSettings.m_addFinalNewLine = newLine;
emitStorageSettingsChanged();
}
void EditorConfiguration::setCleanIndentation(bool cleanIndentation)
{
m_d->m_storageSettings.m_cleanIndentation = cleanIndentation;
emitStorageSettingsChanged();
}
void EditorConfiguration::setMouseNavigation(bool mouseNavigation)
{
m_d->m_behaviorSettings.m_mouseNavigation = mouseNavigation;
emitBehaviorSettingsChanged();
}
void EditorConfiguration::setScrollWheelZooming(bool scrollZooming)
{
m_d->m_behaviorSettings.m_scrollWheelZooming = scrollZooming;
emitBehaviorSettingsChanged();
}
void EditorConfiguration::setUtf8BomSettings(int index)
{
m_d->m_extraEncodingSettings.m_utf8BomSetting = (ExtraEncodingSettings::Utf8BomSetting)index;
emitExtraEncodingSettingsChanged();
}
void EditorConfiguration::setTextCodec(QTextCodec *textCodec)
{
m_d->m_textCodec = textCodec;
}
void EditorConfiguration::emitTabSettingsChanged()
{
emit tabSettingsChanged(m_d->m_tabSettings);
}
void EditorConfiguration::emitStorageSettingsChanged()
{
emit storageSettingsChanged(m_d->m_storageSettings);
}
void EditorConfiguration::emitBehaviorSettingsChanged()
{
emit behaviorSettingsChanged(m_d->m_behaviorSettings);
}
void EditorConfiguration::emitExtraEncodingSettingsChanged()
{
emit extraEncodingSettingsChanged(m_d->m_extraEncodingSettings);
}
const TabSettings &actualTabSettings(const QString &fileName, const BaseTextEditor *baseTextEditor)
{
if (baseTextEditor) {
return baseTextEditor->tabSettings();
} else {
const SessionManager *session = ProjectExplorerPlugin::instance()->session();
if (Project *project = session->projectForFile(fileName))
return project->editorConfiguration()->tabSettings();
else
return TextEditorSettings::instance()->tabSettings();
}
}
} // ProjectExplorer
......@@ -36,30 +36,99 @@
#include "projectexplorer_export.h"