Commit 2cbb64e0 authored by jkobus's avatar jkobus Committed by Jarek Kobus
Browse files

Fix a crash on codestylesettings



Task-number: QTCREATORBUG-10235

Instead of rely on ICodeStylePreferences::destroyed() signal,
when all other objects might be in destruction phase,
clear project code style settings explicitly, when project
closes.

Change-Id: I0dd6675d54c5495d4006acbc9ad12c95f1d0a00c
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
Reviewed-by: default avatarJarek Kobus <jaroslaw.kobus@digia.com>
parent 1f0cbbc8
......@@ -33,6 +33,7 @@
#include "project.h"
#include <coreplugin/id.h>
#include <coreplugin/icore.h>
#include <texteditor/basetexteditor.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/simplecodestylepreferences.h>
......@@ -88,21 +89,31 @@ EditorConfiguration::EditorConfiguration() : d(new EditorConfigurationPrivate)
while (itCodeStyle.hasNext()) {
itCodeStyle.next();
Core::Id languageId = itCodeStyle.key();
// global prefs for language
ICodeStylePreferences *originalPreferences = itCodeStyle.value();
ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(languageId);
// clone of global prefs for language - it will became project prefs for language
ICodeStylePreferences *preferences = factory->createCodeStyle();
// project prefs can point to the global language pool, which contains also the global language prefs
preferences->setDelegatingPool(TextEditorSettings::codeStylePool(languageId));
preferences->setId(languageId.name() + "Project");
preferences->setDisplayName(tr("Project %1", "Settings, %1 is a language (C++ or QML)").arg(factory->displayName()));
// project prefs by default point to global prefs (which in turn can delegate to anything else or not)
preferences->setCurrentDelegate(originalPreferences);
d->m_languageCodeStylePreferences.insert(languageId, preferences);
}
// clone of global prefs (not language specific), for project scope
d->m_defaultCodeStyle = new SimpleCodeStylePreferences(this);
d->m_defaultCodeStyle->setDelegatingPool(TextEditorSettings::codeStylePool());
d->m_defaultCodeStyle->setDisplayName(tr("Project", "Settings"));
d->m_defaultCodeStyle->setId("Project");
d->m_defaultCodeStyle->setCurrentDelegate(d->m_useGlobal ? TextEditorSettings::codeStyle() : 0);
// if setCurrentDelegate is 0 values are read from *this prefs
d->m_defaultCodeStyle->setCurrentDelegate(d->m_useGlobal
? TextEditorSettings::codeStyle() : 0);
connect(SessionManager::instance(), SIGNAL(aboutToRemoveProject(ProjectExplorer::Project*)),
this, SLOT(slotAboutToRemoveProject(ProjectExplorer::Project*)));
}
EditorConfiguration::~EditorConfiguration()
......@@ -237,6 +248,15 @@ void EditorConfiguration::configureEditor(ITextEditor *textEditor) const
}
}
void EditorConfiguration::deconfigureEditor(ITextEditor *textEditor) const
{
BaseTextEditorWidget *baseTextEditor = qobject_cast<BaseTextEditorWidget *>(textEditor->widget());
if (baseTextEditor)
baseTextEditor->setCodeStyle(TextEditorSettings::codeStyle(baseTextEditor->languageSettingsId()));
// TODO: what about text codec and switching settings?
}
void EditorConfiguration::setUseGlobalSettings(bool use)
{
d->m_useGlobal = use;
......@@ -321,7 +341,27 @@ void EditorConfiguration::setTextCodec(QTextCodec *textCodec)
d->m_textCodec = textCodec;
}
TabSettings actualTabSettings(const QString &fileName, const BaseTextEditorWidget *baseTextEditor)
void EditorConfiguration::slotAboutToRemoveProject(ProjectExplorer::Project *project)
{
if (project->editorConfiguration() != this)
return;
Core::DocumentModel *model = Core::EditorManager::documentModel();
QList<Core::IEditor *> editors = model->editorsForDocuments(model->openedDocuments());
foreach (Core::IEditor *editor, editors) {
if (TextEditor::ITextEditor *textEditor = qobject_cast<TextEditor::ITextEditor*>(editor)) {
Core::IDocument *document = editor->document();
if (document) {
Project *editorProject = SessionManager::projectForFile(document->filePath());
if (project == editorProject)
deconfigureEditor(textEditor);
}
}
}
}
TabSettings actualTabSettings(const QString &fileName,
const BaseTextEditorWidget *baseTextEditor)
{
if (baseTextEditor)
return baseTextEditor->tabSettings();
......
......@@ -50,6 +50,7 @@ class ExtraEncodingSettings;
namespace ProjectExplorer {
class Project;
struct EditorConfigurationPrivate;
class PROJECTEXPLORER_EXPORT EditorConfiguration : public QObject
......@@ -77,6 +78,7 @@ public:
QMap<Core::Id, TextEditor::ICodeStylePreferences *> codeStyles() const;
void configureEditor(TextEditor::ITextEditor *textEditor) const;
void deconfigureEditor(TextEditor::ITextEditor *textEditor) const;
QVariantMap toMap() const;
void fromMap(const QVariantMap &map);
......@@ -96,6 +98,7 @@ private slots:
void setTextCodec(QTextCodec *textCodec);
void slotAboutToRemoveProject(ProjectExplorer::Project *project);
private:
void switchSettings(TextEditor::BaseTextEditorWidget *baseTextEditor) const;
......
......@@ -4537,8 +4537,6 @@ void BaseTextEditorWidget::setCodeStyle(ICodeStylePreferences *preferences)
this, SLOT(setTabSettings(TextEditor::TabSettings)));
disconnect(d->m_codeStylePreferences, SIGNAL(currentValueChanged(QVariant)),
this, SLOT(slotCodeStyleSettingsChanged(QVariant)));
disconnect(d->m_codeStylePreferences, SIGNAL(destroyed()),
this, SLOT(onCodeStylePreferencesDestroyed()));
}
d->m_codeStylePreferences = preferences;
if (d->m_codeStylePreferences) {
......@@ -4546,25 +4544,11 @@ void BaseTextEditorWidget::setCodeStyle(ICodeStylePreferences *preferences)
this, SLOT(setTabSettings(TextEditor::TabSettings)));
connect(d->m_codeStylePreferences, SIGNAL(currentValueChanged(QVariant)),
this, SLOT(slotCodeStyleSettingsChanged(QVariant)));
connect(d->m_codeStylePreferences, SIGNAL(destroyed()),
this, SLOT(onCodeStylePreferencesDestroyed()));
setTabSettings(d->m_codeStylePreferences->currentTabSettings());
slotCodeStyleSettingsChanged(d->m_codeStylePreferences->currentValue());
}
}
void BaseTextEditorWidget::onCodeStylePreferencesDestroyed()
{
if (sender() != d->m_codeStylePreferences)
return;
ICodeStylePreferences *prefs = TextEditorSettings::codeStyle(languageSettingsId());
if (prefs == d->m_codeStylePreferences)
prefs = 0;
// avoid failing disconnects, m_codeStylePreferences has already been reduced to QObject
d->m_codeStylePreferences = 0;
setCodeStyle(prefs);
}
void BaseTextEditorWidget::slotCodeStyleSettingsChanged(const QVariant &)
{
......
......@@ -377,7 +377,6 @@ private slots:
bool inFindScope(const QTextCursor &cursor);
bool inFindScope(int selectionStart, int selectionEnd);
void inSnippetMode(bool *active);
void onCodeStylePreferencesDestroyed();
private:
Internal::BaseTextEditorWidgetPrivate *d;
......
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