diff options
author | hjk <hjk@qt.io> | 2022-09-22 17:15:26 +0200 |
---|---|---|
committer | hjk <hjk@qt.io> | 2022-09-23 06:22:22 +0000 |
commit | 686bcb3ef59b3f4dbfe4ab054441f34ba9058a8b (patch) | |
tree | dc50c424d3ed4fb9daef4c59a065d8ca7af31c3e /src | |
parent | dc09779044b45da05d587a3acd08b5307c8c8392 (diff) |
TextEditor: Proliferate FilePath use to FontSettings and ColorScheme
Change-Id: I3fd2e57b9b922d7bf6269b608da48f4a2e13dfb2
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/texteditor/colorscheme.cpp | 35 | ||||
-rw-r--r-- | src/plugins/texteditor/colorscheme.h | 18 | ||||
-rw-r--r-- | src/plugins/texteditor/fontsettings.cpp | 41 | ||||
-rw-r--r-- | src/plugins/texteditor/fontsettings.h | 14 | ||||
-rw-r--r-- | src/plugins/texteditor/fontsettingspage.cpp | 46 |
5 files changed, 78 insertions, 76 deletions
diff --git a/src/plugins/texteditor/colorscheme.cpp b/src/plugins/texteditor/colorscheme.cpp index 9768e1bfda9..75e434d6e07 100644 --- a/src/plugins/texteditor/colorscheme.cpp +++ b/src/plugins/texteditor/colorscheme.cpp @@ -11,13 +11,14 @@ #include <QCoreApplication> #include <QXmlStreamWriter> -using namespace TextEditor; +using namespace Utils; -static const char trueString[] = "true"; -static const char falseString[] = "false"; +namespace TextEditor { -// Format +const char trueString[] = "true"; +const char falseString[] = "false"; +// Format Format::Format(const QColor &foreground, const QColor &background) : m_foreground(foreground), @@ -214,9 +215,9 @@ void ColorScheme::clear() m_formats.clear(); } -bool ColorScheme::save(const QString &fileName, QWidget *parent) const +bool ColorScheme::save(const FilePath &filePath, QWidget *parent) const { - Utils::FileSaver saver(Utils::FilePath::fromString(fileName)); + FileSaver saver(filePath); if (!saver.hasError()) { QXmlStreamWriter w(saver.file()); w.setAutoFormatting(true); @@ -268,8 +269,8 @@ namespace { class ColorSchemeReader : public QXmlStreamReader { public: - bool read(const QString &fileName, ColorScheme *scheme); - QString readName(const QString &fileName); + bool read(const FilePath &filePath, ColorScheme *scheme); + QString readName(const FilePath &filePath); private: bool readNextStartElement(); @@ -281,14 +282,14 @@ private: QString m_name; }; -bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme) +bool ColorSchemeReader::read(const FilePath &filePath, ColorScheme *scheme) { m_scheme = scheme; if (m_scheme) m_scheme->clear(); - QFile file(fileName); + QFile file(filePath.toString()); if (!file.open(QFile::ReadOnly | QFile::Text)) return false; @@ -302,9 +303,9 @@ bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme) return true; } -QString ColorSchemeReader::readName(const QString &fileName) +QString ColorSchemeReader::readName(const FilePath &filePath) { - read(fileName, nullptr); + read(filePath, nullptr); return m_name; } @@ -397,13 +398,15 @@ void ColorSchemeReader::readStyle() } // anonymous namespace -bool ColorScheme::load(const QString &fileName) +bool ColorScheme::load(const FilePath &filePath) { ColorSchemeReader reader; - return reader.read(fileName, this) && !reader.hasError(); + return reader.read(filePath, this) && !reader.hasError(); } -QString ColorScheme::readNameOfScheme(const QString &fileName) +QString ColorScheme::readNameOfScheme(const FilePath &filePath) { - return ColorSchemeReader().readName(fileName); + return ColorSchemeReader().readName(filePath); } + +} // TextEdito diff --git a/src/plugins/texteditor/colorscheme.h b/src/plugins/texteditor/colorscheme.h index fe331b96351..7ce816b8d1e 100644 --- a/src/plugins/texteditor/colorscheme.h +++ b/src/plugins/texteditor/colorscheme.h @@ -15,6 +15,8 @@ QT_BEGIN_NAMESPACE class QWidget; QT_END_NAMESPACE +namespace Utils { class FilePath; } + namespace TextEditor { /*! Format for a particular piece of text (text/comment, etc). */ @@ -81,14 +83,10 @@ private: class TEXTEDITOR_EXPORT ColorScheme { public: - void setDisplayName(const QString &name) - { m_displayName = name; } - - QString displayName() const - { return m_displayName; } + void setDisplayName(const QString &name) { m_displayName = name; } + QString displayName() const { return m_displayName; } - inline bool isEmpty() const - { return m_formats.isEmpty(); } + bool isEmpty() const { return m_formats.isEmpty(); } bool contains(TextStyle category) const; @@ -99,15 +97,15 @@ public: void clear(); - bool save(const QString &fileName, QWidget *parent) const; - bool load(const QString &fileName); + bool save(const Utils::FilePath &filePath, QWidget *parent) const; + bool load(const Utils::FilePath &filePath); bool equals(const ColorScheme &cs) const { return m_formats == cs.m_formats && m_displayName == cs.m_displayName; } - static QString readNameOfScheme(const QString &fileName); + static QString readNameOfScheme(const Utils::FilePath &filePath); friend bool operator==(const ColorScheme &cs1, const ColorScheme &cs2) { return cs1.equals(cs2); } friend bool operator!=(const ColorScheme &cs1, const ColorScheme &cs2) { return !cs1.equals(cs2); } diff --git a/src/plugins/texteditor/fontsettings.cpp b/src/plugins/texteditor/fontsettings.cpp index 549106f22b4..f8f2f3db198 100644 --- a/src/plugins/texteditor/fontsettings.cpp +++ b/src/plugins/texteditor/fontsettings.cpp @@ -20,17 +20,16 @@ #include <cmath> -static const char fontFamilyKey[] = "FontFamily"; -static const char fontSizeKey[] = "FontSize"; -static const char fontZoomKey[] = "FontZoom"; -static const char lineSpacingKey[] = "LineSpacing"; -static const char antialiasKey[] = "FontAntialias"; -static const char schemeFileNamesKey[] = "ColorSchemes"; +using namespace Utils; -namespace { -static const bool DEFAULT_ANTIALIAS = true; +const char fontFamilyKey[] = "FontFamily"; +const char fontSizeKey[] = "FontSize"; +const char fontZoomKey[] = "FontZoom"; +const char lineSpacingKey[] = "LineSpacing"; +const char antialiasKey[] = "FontAntialias"; +const char schemeFileNamesKey[] = "ColorSchemes"; -} // anonymous namespace +const bool DEFAULT_ANTIALIAS = true; namespace TextEditor { @@ -81,7 +80,7 @@ void FontSettings::toSettings(QSettings *s) const auto schemeFileNames = s->value(QLatin1String(schemeFileNamesKey)).toMap(); if (m_schemeFileName != defaultSchemeFileName() || schemeFileNames.contains(Utils::creatorTheme()->id())) { - schemeFileNames.insert(Utils::creatorTheme()->id(), m_schemeFileName); + schemeFileNames.insert(Utils::creatorTheme()->id(), m_schemeFileName.toVariant()); s->setValue(QLatin1String(schemeFileNamesKey), schemeFileNames); } @@ -108,7 +107,7 @@ bool FontSettings::fromSettings(const FormatDescriptions &descriptions, const QS // Load the selected color scheme for the current theme auto schemeFileNames = s->value(group + QLatin1String(schemeFileNamesKey)).toMap(); if (schemeFileNames.contains(Utils::creatorTheme()->id())) { - const QString scheme = schemeFileNames.value(Utils::creatorTheme()->id()).toString(); + const FilePath scheme = FilePath::fromVariant(schemeFileNames.value(Utils::creatorTheme()->id())); loadColorScheme(scheme, descriptions); } } @@ -400,7 +399,7 @@ Format FontSettings::formatFor(TextStyle category) const /** * Returns the file name of the currently selected color scheme. */ -QString FontSettings::colorSchemeFileName() const +Utils::FilePath FontSettings::colorSchemeFileName() const { return m_schemeFileName; } @@ -409,23 +408,23 @@ QString FontSettings::colorSchemeFileName() const * Sets the file name of the color scheme. Does not load the scheme from the * given file. If you want to load a scheme, use loadColorScheme() instead. */ -void FontSettings::setColorSchemeFileName(const QString &fileName) +void FontSettings::setColorSchemeFileName(const Utils::FilePath &filePath) { - m_schemeFileName = fileName; + m_schemeFileName = filePath; } -bool FontSettings::loadColorScheme(const QString &fileName, +bool FontSettings::loadColorScheme(const Utils::FilePath &filePath, const FormatDescriptions &descriptions) { clearCaches(); bool loaded = true; - m_schemeFileName = fileName; + m_schemeFileName = filePath; if (!m_scheme.load(m_schemeFileName)) { loaded = false; m_schemeFileName.clear(); - qWarning() << "Failed to load color scheme:" << fileName; + qWarning() << "Failed to load color scheme:" << filePath; } // Apply default formats to undefined categories @@ -463,7 +462,7 @@ bool FontSettings::loadColorScheme(const QString &fileName, return loaded; } -bool FontSettings::saveColorScheme(const QString &fileName) +bool FontSettings::saveColorScheme(const Utils::FilePath &fileName) { const bool saved = m_scheme.save(fileName, Core::ICore::dialogParent()); if (saved) @@ -524,9 +523,9 @@ int FontSettings::defaultFontSize() * Returns the default scheme file name, or the path to a shipped scheme when * one exists with the given \a fileName. */ -QString FontSettings::defaultSchemeFileName(const QString &fileName) +FilePath FontSettings::defaultSchemeFileName(const QString &fileName) { - Utils::FilePath defaultScheme = Core::ICore::resourcePath("styles"); + FilePath defaultScheme = Core::ICore::resourcePath("styles"); if (!fileName.isEmpty() && (defaultScheme / fileName).exists()) { defaultScheme = defaultScheme / fileName; @@ -538,7 +537,7 @@ QString FontSettings::defaultSchemeFileName(const QString &fileName) defaultScheme = defaultScheme / "default.xml"; } - return defaultScheme.toString(); + return defaultScheme; } } // namespace TextEditor diff --git a/src/plugins/texteditor/fontsettings.h b/src/plugins/texteditor/fontsettings.h index bb9b324a2c5..e164664baa1 100644 --- a/src/plugins/texteditor/fontsettings.h +++ b/src/plugins/texteditor/fontsettings.h @@ -8,6 +8,8 @@ #include "colorscheme.h" #include "textstyles.h" +#include <utils/filepath.h> + #include <QHash> #include <QList> #include <QString> @@ -65,10 +67,10 @@ public: Format &formatFor(TextStyle category); Format formatFor(TextStyle category) const; - QString colorSchemeFileName() const; - void setColorSchemeFileName(const QString &fileName); - bool loadColorScheme(const QString &fileName, const FormatDescriptions &descriptions); - bool saveColorScheme(const QString &fileName); + Utils::FilePath colorSchemeFileName() const; + void setColorSchemeFileName(const Utils::FilePath &filePath); + bool loadColorScheme(const Utils::FilePath &filePath, const FormatDescriptions &descriptions); + bool saveColorScheme(const Utils::FilePath &filePath); const ColorScheme &colorScheme() const; void setColorScheme(const ColorScheme &scheme); @@ -78,7 +80,7 @@ public: static QString defaultFixedFontFamily(); static int defaultFontSize(); - static QString defaultSchemeFileName(const QString &fileName = QString()); + static Utils::FilePath defaultSchemeFileName(const QString &fileName = {}); friend bool operator==(const FontSettings &f1, const FontSettings &f2) { return f1.equals(f2); } friend bool operator!=(const FontSettings &f1, const FontSettings &f2) { return !f1.equals(f2); } @@ -89,7 +91,7 @@ private: private: QString m_family; - QString m_schemeFileName; + Utils::FilePath m_schemeFileName; int m_fontSize; int m_fontZoom; int m_lineSpacing; diff --git a/src/plugins/texteditor/fontsettingspage.cpp b/src/plugins/texteditor/fontsettingspage.cpp index dde3f3ec01e..063f2047b69 100644 --- a/src/plugins/texteditor/fontsettingspage.cpp +++ b/src/plugins/texteditor/fontsettingspage.cpp @@ -50,13 +50,13 @@ namespace Internal { struct ColorSchemeEntry { - ColorSchemeEntry(const QString &fileName, bool readOnly) : - fileName(fileName), - name(ColorScheme::readNameOfScheme(fileName)), + ColorSchemeEntry(const FilePath &filePath, bool readOnly) : + filePath(filePath), + name(ColorScheme::readNameOfScheme(filePath)), readOnly(readOnly) { } - QString fileName; + FilePath filePath; QString name; QString id; bool readOnly; @@ -514,7 +514,7 @@ void FontSettingsPageWidget::colorSchemeSelected(int index) const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); readOnly = entry.readOnly; - m_value.loadColorScheme(entry.fileName, m_descriptions); + m_value.loadColorScheme(entry.filePath, m_descriptions); m_schemeEdit->setColorScheme(m_value.colorScheme()); } m_copyButton->setEnabled(index != -1); @@ -543,11 +543,11 @@ void FontSettingsPageWidget::copyColorScheme(const QString &name) const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); - QString baseFileName = QFileInfo(entry.fileName).completeBaseName(); + QString baseFileName = entry.filePath.completeBaseName(); baseFileName += QLatin1String("_copy%1.xml"); - FilePath fileName = createColorSchemeFileName(baseFileName); + FilePath filePath = createColorSchemeFileName(baseFileName); - if (!fileName.isEmpty()) { + if (!filePath.isEmpty()) { // Ask about saving any existing modifications maybeSaveColorScheme(); @@ -556,8 +556,8 @@ void FontSettingsPageWidget::copyColorScheme(const QString &name) ColorScheme scheme = m_value.colorScheme(); scheme.setDisplayName(name); - if (scheme.save(fileName.path(), Core::ICore::dialogParent())) - m_value.setColorSchemeFileName(fileName.path()); + if (scheme.save(filePath, Core::ICore::dialogParent())) + m_value.setColorSchemeFileName(filePath); refreshColorSchemeList(); } @@ -598,7 +598,7 @@ void FontSettingsPageWidget::deleteColorScheme() const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); QTC_ASSERT(!entry.readOnly, return); - if (QFile::remove(entry.fileName)) + if (entry.filePath.removeFile()) m_schemeListModel.removeColorScheme(index); } @@ -631,10 +631,10 @@ void FontSettingsPageWidget::importScheme() importedFile.baseName() + "%1." + importedFile.suffix()); ColorScheme scheme; - if (scheme.load(importedFile.path())) { + if (scheme.load(importedFile)) { scheme.setDisplayName(name); - scheme.save(saveFileName.path(), Core::ICore::dialogParent()); - m_value.loadColorScheme(saveFileName.path(), m_descriptions); + scheme.save(saveFileName, Core::ICore::dialogParent()); + m_value.loadColorScheme(saveFileName, m_descriptions); } else { qWarning() << "Failed to import color scheme:" << importedFile; } @@ -656,11 +656,11 @@ void FontSettingsPageWidget::exportScheme() const FilePath filePath = Utils::FileUtils::getSaveFilePath(this, tr("Export Color Scheme"), - FilePath::fromString(entry.fileName), + entry.filePath, tr("Color scheme (*.xml);;All files (*)")); if (!filePath.isEmpty()) - m_value.colorScheme().save(filePath.toString(), Core::ICore::dialogParent()); + m_value.colorScheme().save(filePath, Core::ICore::dialogParent()); } void FontSettingsPageWidget::maybeSaveColorScheme() @@ -695,7 +695,7 @@ void FontSettingsPageWidget::refreshColorSchemeList() const FilePath styleDir = Core::ICore::resourcePath("styles"); FilePaths schemeList = styleDir.dirEntries(FileFilter({"*.xml"}, QDir::Files)); - const FilePath defaultScheme = FilePath::fromString(FontSettings::defaultSchemeFileName()); + const FilePath defaultScheme = FontSettings::defaultSchemeFileName(); if (schemeList.removeAll(defaultScheme)) schemeList.prepend(defaultScheme); @@ -703,9 +703,9 @@ void FontSettingsPageWidget::refreshColorSchemeList() int selected = 0; for (const FilePath &file : qAsConst(schemeList)) { - if (FilePath::fromString(m_value.colorSchemeFileName()) == file) + if (m_value.colorSchemeFileName() == file) selected = colorSchemes.size(); - colorSchemes.append(ColorSchemeEntry(file.toString(), true)); + colorSchemes.append(ColorSchemeEntry(file, true)); } if (colorSchemes.isEmpty()) @@ -713,9 +713,9 @@ void FontSettingsPageWidget::refreshColorSchemeList() const FilePaths files = customStylesPath().dirEntries(FileFilter({"*.xml"}, QDir::Files)); for (const FilePath &file : files) { - if (FilePath::fromString(m_value.colorSchemeFileName()) == file) + if (m_value.colorSchemeFileName() == file) selected = colorSchemes.size(); - colorSchemes.append(ColorSchemeEntry(file.toString(), false)); + colorSchemes.append(ColorSchemeEntry(file, false)); } m_refreshingSchemeList = true; @@ -743,8 +743,8 @@ void FontSettingsPageWidget::apply() int index = m_schemeComboBox->currentIndex(); if (index != -1) { const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); - if (entry.fileName != m_value.colorSchemeFileName()) - m_value.loadColorScheme(entry.fileName, m_descriptions); + if (entry.filePath != m_value.colorSchemeFileName()) + m_value.loadColorScheme(entry.filePath, m_descriptions); } saveSettings(); |