From c1198e5ab9802902681d04f8958917dc9fb85f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <thorbjorn.lindeijer@nokia.com> Date: Wed, 15 Jul 2009 12:27:02 +0200 Subject: [PATCH] Added importing of customized color schemes from the ini file So people's customized color schemes don't get lost, but get converted to the new format instead. --- src/plugins/texteditor/fontsettings.cpp | 37 +++++++++++++++-- src/plugins/texteditor/fontsettings.h | 4 +- src/plugins/texteditor/fontsettingspage.cpp | 44 ++++++++++++++------- src/plugins/texteditor/fontsettingspage.h | 1 - src/plugins/texteditor/texteditorplugin.cpp | 2 +- 5 files changed, 66 insertions(+), 22 deletions(-) diff --git a/src/plugins/texteditor/fontsettings.cpp b/src/plugins/texteditor/fontsettings.cpp index 39121e545ea..5ad025d815d 100644 --- a/src/plugins/texteditor/fontsettings.cpp +++ b/src/plugins/texteditor/fontsettings.cpp @@ -77,7 +77,6 @@ void FontSettings::clear() } void FontSettings::toSettings(const QString &category, - const FormatDescriptions &descriptions, QSettings *s) const { s->beginGroup(category); @@ -111,8 +110,30 @@ bool FontSettings::fromSettings(const QString &category, m_family = s->value(group + QLatin1String(fontFamilyKey), defaultFixedFontFamily()).toString(); m_fontSize = s->value(group + QLatin1String(fontSizeKey), m_fontSize).toInt(); m_antialias = s->value(group + QLatin1String(antialiasKey), DEFAULT_ANTIALIAS).toBool(); - loadColorScheme(s->value(group + QLatin1String(schemeFileNameKey), defaultSchemeFileName()).toString(), - descriptions); + + if (s->contains(group + QLatin1String(schemeFileNameKey))) { + // Load the selected color scheme + loadColorScheme(s->value(group + QLatin1String(schemeFileNameKey), defaultSchemeFileName()).toString(), + descriptions); + } else { + // Load color scheme from ini file + foreach (const FormatDescription &desc, descriptions) { + const QString name = desc.name(); + const QString fmt = s->value(group + name, QString()).toString(); + Format format; + if (fmt.isEmpty()) { + format.setForeground(desc.foreground()); + format.setBackground(desc.background()); + format.setBold(desc.format().bold()); + format.setItalic(desc.format().italic()); + } else { + format.fromString(fmt); + } + m_scheme.setFormatFor(name, format); + } + + m_scheme.setName(QObject::tr("Customized")); + } return true; } @@ -256,10 +277,18 @@ bool FontSettings::loadColorScheme(const QString &fileName, return loaded; } +bool FontSettings::saveColorScheme(const QString &fileName) +{ + const bool saved = m_scheme.save(fileName); + if (saved) + m_schemeFileName = fileName; + return saved; +} + /** * Returns the currently active color scheme. */ -ColorScheme FontSettings::colorScheme() const +const ColorScheme &FontSettings::colorScheme() const { return m_scheme; } diff --git a/src/plugins/texteditor/fontsettings.h b/src/plugins/texteditor/fontsettings.h index 0fa93bbe7bb..9e55aed5b0e 100644 --- a/src/plugins/texteditor/fontsettings.h +++ b/src/plugins/texteditor/fontsettings.h @@ -62,7 +62,6 @@ public: inline bool isEmpty() const { return m_scheme.isEmpty(); } void toSettings(const QString &category, - const FormatDescriptions &descriptions, QSettings *s) const; bool fromSettings(const QString &category, @@ -86,8 +85,9 @@ public: QString colorSchemeFileName() const; void setColorSchemeFileName(const QString &fileName); bool loadColorScheme(const QString &fileName, const FormatDescriptions &descriptions); + bool saveColorScheme(const QString &fileName); - ColorScheme colorScheme() const; + const ColorScheme &colorScheme() const; void setColorScheme(const ColorScheme &scheme); bool equals(const FontSettings &f) const; diff --git a/src/plugins/texteditor/fontsettingspage.cpp b/src/plugins/texteditor/fontsettingspage.cpp index 18c4e723e51..675ae98d6a1 100644 --- a/src/plugins/texteditor/fontsettingspage.cpp +++ b/src/plugins/texteditor/fontsettingspage.cpp @@ -90,6 +90,13 @@ Q_DECLARE_METATYPE(TextEditor::Internal::ColorSchemeEntry) using namespace TextEditor; using namespace TextEditor::Internal; +static QString customStylesPath() +{ + QString path = QFileInfo(Core::ICore::instance()->settings()->fileName()).path(); + path.append(QLatin1String("/qtcreator/styles/")); + return path; +} + // ------- FontSettingsPagePrivate FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescriptions &fd, @@ -103,22 +110,38 @@ FontSettingsPagePrivate::FontSettingsPagePrivate(const TextEditor::FormatDescrip m_descriptions(fd) { bool settingsFound = false; - if (const QSettings *settings = Core::ICore::instance()->settings()) + QSettings *settings = Core::ICore::instance()->settings(); + if (settings) settingsFound = m_value.fromSettings(m_settingsGroup, m_descriptions, settings); + if (!settingsFound) { // Apply defaults foreach (const FormatDescription &f, m_descriptions) { const QString name = f.name(); - m_lastValue.formatFor(name).setForeground(f.foreground()); - m_lastValue.formatFor(name).setBackground(f.background()); - m_lastValue.formatFor(name).setBold(f.format().bold()); - m_lastValue.formatFor(name).setItalic(f.format().italic()); - m_value.formatFor(name).setForeground(f.foreground()); m_value.formatFor(name).setBackground(f.background()); m_value.formatFor(name).setBold(f.format().bold()); m_value.formatFor(name).setItalic(f.format().italic()); } + } else if (m_value.colorSchemeFileName().isEmpty()) { + // No color scheme was loaded, but one might be imported from the ini file + ColorScheme defaultScheme; + foreach (const FormatDescription &f, m_descriptions) { + const QString name = f.name(); + defaultScheme.formatFor(name).setForeground(f.foreground()); + defaultScheme.formatFor(name).setBackground(f.background()); + defaultScheme.formatFor(name).setBold(f.format().bold()); + defaultScheme.formatFor(name).setItalic(f.format().italic()); + } + if (m_value.colorScheme() != defaultScheme) { + // Save it as a color scheme file + QString stylesPath = customStylesPath(); + if (QFile::exists(stylesPath) || QDir().mkpath(stylesPath)) { + QString schemeFileName = stylesPath + QLatin1String("customized.xml"); + if (m_value.saveColorScheme(schemeFileName) && settings) + m_value.toSettings(m_category, settings); + } + } } m_lastValue = m_value; @@ -436,13 +459,6 @@ void FontSettingsPage::refreshColorSchemeList() d_ptr->ui.schemeListWidget->setCurrentIndex(s); } -QString FontSettingsPage::customStylesPath() -{ - QString path = QFileInfo(Core::ICore::instance()->settings()->fileName()).path(); - path.append(QLatin1String("/qtcreator/styles/")); - return path; -} - void FontSettingsPage::delayedChange() { emit changed(d_ptr->m_value); @@ -471,7 +487,7 @@ void FontSettingsPage::saveSettings() if (d_ptr->m_value != d_ptr->m_lastValue) { d_ptr->m_lastValue = d_ptr->m_value; if (QSettings *settings = Core::ICore::instance()->settings()) - d_ptr->m_value.toSettings(d_ptr->m_settingsGroup, d_ptr->m_descriptions, settings); + d_ptr->m_value.toSettings(d_ptr->m_settingsGroup, settings); QTimer::singleShot(0, this, SLOT(delayedChange())); } diff --git a/src/plugins/texteditor/fontsettingspage.h b/src/plugins/texteditor/fontsettingspage.h index a57761d2992..af009d75d44 100644 --- a/src/plugins/texteditor/fontsettingspage.h +++ b/src/plugins/texteditor/fontsettingspage.h @@ -119,7 +119,6 @@ private slots: private: void addColorSchemeEntry(const QString &fileName, bool readOnly); void refreshColorSchemeList(); - static QString customStylesPath(); Internal::FontSettingsPagePrivate *d_ptr; }; diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index 86ea887bc91..5948825edaf 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -131,7 +131,7 @@ bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMe #endif connect(completionShortcut, SIGNAL(activated()), this, SLOT(invokeCompletion())); - // Add shortcut for invoking automatic completion + // Add shortcut for invoking quick fix options QShortcut *quickFixShortcut = new QShortcut(core->mainWindow()); quickFixShortcut->setWhatsThis(tr("Triggers a quick fix in this scope")); // Make sure the shortcut still works when the quick fix widget is active -- GitLab