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>
This commit is contained in:
hjk
2022-09-22 17:15:26 +02:00
parent dc09779044
commit 686bcb3ef5
5 changed files with 78 additions and 76 deletions

View File

@@ -11,14 +11,15 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QXmlStreamWriter> #include <QXmlStreamWriter>
using namespace TextEditor; using namespace Utils;
static const char trueString[] = "true"; namespace TextEditor {
static const char falseString[] = "false";
const char trueString[] = "true";
const char falseString[] = "false";
// Format // Format
Format::Format(const QColor &foreground, const QColor &background) : Format::Format(const QColor &foreground, const QColor &background) :
m_foreground(foreground), m_foreground(foreground),
m_background(background) m_background(background)
@@ -214,9 +215,9 @@ void ColorScheme::clear()
m_formats.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()) { if (!saver.hasError()) {
QXmlStreamWriter w(saver.file()); QXmlStreamWriter w(saver.file());
w.setAutoFormatting(true); w.setAutoFormatting(true);
@@ -268,8 +269,8 @@ namespace {
class ColorSchemeReader : public QXmlStreamReader class ColorSchemeReader : public QXmlStreamReader
{ {
public: public:
bool read(const QString &fileName, ColorScheme *scheme); bool read(const FilePath &filePath, ColorScheme *scheme);
QString readName(const QString &fileName); QString readName(const FilePath &filePath);
private: private:
bool readNextStartElement(); bool readNextStartElement();
@@ -281,14 +282,14 @@ private:
QString m_name; QString m_name;
}; };
bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme) bool ColorSchemeReader::read(const FilePath &filePath, ColorScheme *scheme)
{ {
m_scheme = scheme; m_scheme = scheme;
if (m_scheme) if (m_scheme)
m_scheme->clear(); m_scheme->clear();
QFile file(fileName); QFile file(filePath.toString());
if (!file.open(QFile::ReadOnly | QFile::Text)) if (!file.open(QFile::ReadOnly | QFile::Text))
return false; return false;
@@ -302,9 +303,9 @@ bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme)
return true; return true;
} }
QString ColorSchemeReader::readName(const QString &fileName) QString ColorSchemeReader::readName(const FilePath &filePath)
{ {
read(fileName, nullptr); read(filePath, nullptr);
return m_name; return m_name;
} }
@@ -397,13 +398,15 @@ void ColorSchemeReader::readStyle()
} // anonymous namespace } // anonymous namespace
bool ColorScheme::load(const QString &fileName) bool ColorScheme::load(const FilePath &filePath)
{ {
ColorSchemeReader reader; 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

View File

@@ -15,6 +15,8 @@ QT_BEGIN_NAMESPACE
class QWidget; class QWidget;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace Utils { class FilePath; }
namespace TextEditor { namespace TextEditor {
/*! Format for a particular piece of text (text/comment, etc). */ /*! Format for a particular piece of text (text/comment, etc). */
@@ -81,14 +83,10 @@ private:
class TEXTEDITOR_EXPORT ColorScheme class TEXTEDITOR_EXPORT ColorScheme
{ {
public: public:
void setDisplayName(const QString &name) void setDisplayName(const QString &name) { m_displayName = name; }
{ m_displayName = name; } QString displayName() const { return m_displayName; }
QString displayName() const bool isEmpty() const { return m_formats.isEmpty(); }
{ return m_displayName; }
inline bool isEmpty() const
{ return m_formats.isEmpty(); }
bool contains(TextStyle category) const; bool contains(TextStyle category) const;
@@ -99,15 +97,15 @@ public:
void clear(); void clear();
bool save(const QString &fileName, QWidget *parent) const; bool save(const Utils::FilePath &filePath, QWidget *parent) const;
bool load(const QString &fileName); bool load(const Utils::FilePath &filePath);
bool equals(const ColorScheme &cs) const bool equals(const ColorScheme &cs) const
{ {
return m_formats == cs.m_formats && m_displayName == cs.m_displayName; 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); }
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); }

View File

@@ -20,17 +20,16 @@
#include <cmath> #include <cmath>
static const char fontFamilyKey[] = "FontFamily"; using namespace Utils;
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";
namespace { const char fontFamilyKey[] = "FontFamily";
static const bool DEFAULT_ANTIALIAS = true; 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 { namespace TextEditor {
@@ -81,7 +80,7 @@ void FontSettings::toSettings(QSettings *s) const
auto schemeFileNames = s->value(QLatin1String(schemeFileNamesKey)).toMap(); auto schemeFileNames = s->value(QLatin1String(schemeFileNamesKey)).toMap();
if (m_schemeFileName != defaultSchemeFileName() || schemeFileNames.contains(Utils::creatorTheme()->id())) { 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); 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 // Load the selected color scheme for the current theme
auto schemeFileNames = s->value(group + QLatin1String(schemeFileNamesKey)).toMap(); auto schemeFileNames = s->value(group + QLatin1String(schemeFileNamesKey)).toMap();
if (schemeFileNames.contains(Utils::creatorTheme()->id())) { 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); loadColorScheme(scheme, descriptions);
} }
} }
@@ -400,7 +399,7 @@ Format FontSettings::formatFor(TextStyle category) const
/** /**
* Returns the file name of the currently selected color scheme. * Returns the file name of the currently selected color scheme.
*/ */
QString FontSettings::colorSchemeFileName() const Utils::FilePath FontSettings::colorSchemeFileName() const
{ {
return m_schemeFileName; 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 * 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. * 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) const FormatDescriptions &descriptions)
{ {
clearCaches(); clearCaches();
bool loaded = true; bool loaded = true;
m_schemeFileName = fileName; m_schemeFileName = filePath;
if (!m_scheme.load(m_schemeFileName)) { if (!m_scheme.load(m_schemeFileName)) {
loaded = false; loaded = false;
m_schemeFileName.clear(); m_schemeFileName.clear();
qWarning() << "Failed to load color scheme:" << fileName; qWarning() << "Failed to load color scheme:" << filePath;
} }
// Apply default formats to undefined categories // Apply default formats to undefined categories
@@ -463,7 +462,7 @@ bool FontSettings::loadColorScheme(const QString &fileName,
return loaded; 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()); const bool saved = m_scheme.save(fileName, Core::ICore::dialogParent());
if (saved) if (saved)
@@ -524,9 +523,9 @@ int FontSettings::defaultFontSize()
* Returns the default scheme file name, or the path to a shipped scheme when * Returns the default scheme file name, or the path to a shipped scheme when
* one exists with the given \a fileName. * 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()) { if (!fileName.isEmpty() && (defaultScheme / fileName).exists()) {
defaultScheme = defaultScheme / fileName; defaultScheme = defaultScheme / fileName;
@@ -538,7 +537,7 @@ QString FontSettings::defaultSchemeFileName(const QString &fileName)
defaultScheme = defaultScheme / "default.xml"; defaultScheme = defaultScheme / "default.xml";
} }
return defaultScheme.toString(); return defaultScheme;
} }
} // namespace TextEditor } // namespace TextEditor

View File

@@ -8,6 +8,8 @@
#include "colorscheme.h" #include "colorscheme.h"
#include "textstyles.h" #include "textstyles.h"
#include <utils/filepath.h>
#include <QHash> #include <QHash>
#include <QList> #include <QList>
#include <QString> #include <QString>
@@ -65,10 +67,10 @@ public:
Format &formatFor(TextStyle category); Format &formatFor(TextStyle category);
Format formatFor(TextStyle category) const; Format formatFor(TextStyle category) const;
QString colorSchemeFileName() const; Utils::FilePath colorSchemeFileName() const;
void setColorSchemeFileName(const QString &fileName); void setColorSchemeFileName(const Utils::FilePath &filePath);
bool loadColorScheme(const QString &fileName, const FormatDescriptions &descriptions); bool loadColorScheme(const Utils::FilePath &filePath, const FormatDescriptions &descriptions);
bool saveColorScheme(const QString &fileName); bool saveColorScheme(const Utils::FilePath &filePath);
const ColorScheme &colorScheme() const; const ColorScheme &colorScheme() const;
void setColorScheme(const ColorScheme &scheme); void setColorScheme(const ColorScheme &scheme);
@@ -78,7 +80,7 @@ public:
static QString defaultFixedFontFamily(); static QString defaultFixedFontFamily();
static int defaultFontSize(); 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); }
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: private:
QString m_family; QString m_family;
QString m_schemeFileName; Utils::FilePath m_schemeFileName;
int m_fontSize; int m_fontSize;
int m_fontZoom; int m_fontZoom;
int m_lineSpacing; int m_lineSpacing;

View File

@@ -50,13 +50,13 @@ namespace Internal {
struct ColorSchemeEntry struct ColorSchemeEntry
{ {
ColorSchemeEntry(const QString &fileName, bool readOnly) : ColorSchemeEntry(const FilePath &filePath, bool readOnly) :
fileName(fileName), filePath(filePath),
name(ColorScheme::readNameOfScheme(fileName)), name(ColorScheme::readNameOfScheme(filePath)),
readOnly(readOnly) readOnly(readOnly)
{ } { }
QString fileName; FilePath filePath;
QString name; QString name;
QString id; QString id;
bool readOnly; bool readOnly;
@@ -514,7 +514,7 @@ void FontSettingsPageWidget::colorSchemeSelected(int index)
const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index);
readOnly = entry.readOnly; readOnly = entry.readOnly;
m_value.loadColorScheme(entry.fileName, m_descriptions); m_value.loadColorScheme(entry.filePath, m_descriptions);
m_schemeEdit->setColorScheme(m_value.colorScheme()); m_schemeEdit->setColorScheme(m_value.colorScheme());
} }
m_copyButton->setEnabled(index != -1); m_copyButton->setEnabled(index != -1);
@@ -543,11 +543,11 @@ void FontSettingsPageWidget::copyColorScheme(const QString &name)
const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index);
QString baseFileName = QFileInfo(entry.fileName).completeBaseName(); QString baseFileName = entry.filePath.completeBaseName();
baseFileName += QLatin1String("_copy%1.xml"); 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 // Ask about saving any existing modifications
maybeSaveColorScheme(); maybeSaveColorScheme();
@@ -556,8 +556,8 @@ void FontSettingsPageWidget::copyColorScheme(const QString &name)
ColorScheme scheme = m_value.colorScheme(); ColorScheme scheme = m_value.colorScheme();
scheme.setDisplayName(name); scheme.setDisplayName(name);
if (scheme.save(fileName.path(), Core::ICore::dialogParent())) if (scheme.save(filePath, Core::ICore::dialogParent()))
m_value.setColorSchemeFileName(fileName.path()); m_value.setColorSchemeFileName(filePath);
refreshColorSchemeList(); refreshColorSchemeList();
} }
@@ -598,7 +598,7 @@ void FontSettingsPageWidget::deleteColorScheme()
const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index);
QTC_ASSERT(!entry.readOnly, return); QTC_ASSERT(!entry.readOnly, return);
if (QFile::remove(entry.fileName)) if (entry.filePath.removeFile())
m_schemeListModel.removeColorScheme(index); m_schemeListModel.removeColorScheme(index);
} }
@@ -631,10 +631,10 @@ void FontSettingsPageWidget::importScheme()
importedFile.baseName() + "%1." + importedFile.suffix()); importedFile.baseName() + "%1." + importedFile.suffix());
ColorScheme scheme; ColorScheme scheme;
if (scheme.load(importedFile.path())) { if (scheme.load(importedFile)) {
scheme.setDisplayName(name); scheme.setDisplayName(name);
scheme.save(saveFileName.path(), Core::ICore::dialogParent()); scheme.save(saveFileName, Core::ICore::dialogParent());
m_value.loadColorScheme(saveFileName.path(), m_descriptions); m_value.loadColorScheme(saveFileName, m_descriptions);
} else { } else {
qWarning() << "Failed to import color scheme:" << importedFile; qWarning() << "Failed to import color scheme:" << importedFile;
} }
@@ -656,11 +656,11 @@ void FontSettingsPageWidget::exportScheme()
const FilePath filePath const FilePath filePath
= Utils::FileUtils::getSaveFilePath(this, = Utils::FileUtils::getSaveFilePath(this,
tr("Export Color Scheme"), tr("Export Color Scheme"),
FilePath::fromString(entry.fileName), entry.filePath,
tr("Color scheme (*.xml);;All files (*)")); tr("Color scheme (*.xml);;All files (*)"));
if (!filePath.isEmpty()) if (!filePath.isEmpty())
m_value.colorScheme().save(filePath.toString(), Core::ICore::dialogParent()); m_value.colorScheme().save(filePath, Core::ICore::dialogParent());
} }
void FontSettingsPageWidget::maybeSaveColorScheme() void FontSettingsPageWidget::maybeSaveColorScheme()
@@ -695,7 +695,7 @@ void FontSettingsPageWidget::refreshColorSchemeList()
const FilePath styleDir = Core::ICore::resourcePath("styles"); const FilePath styleDir = Core::ICore::resourcePath("styles");
FilePaths schemeList = styleDir.dirEntries(FileFilter({"*.xml"}, QDir::Files)); FilePaths schemeList = styleDir.dirEntries(FileFilter({"*.xml"}, QDir::Files));
const FilePath defaultScheme = FilePath::fromString(FontSettings::defaultSchemeFileName()); const FilePath defaultScheme = FontSettings::defaultSchemeFileName();
if (schemeList.removeAll(defaultScheme)) if (schemeList.removeAll(defaultScheme))
schemeList.prepend(defaultScheme); schemeList.prepend(defaultScheme);
@@ -703,9 +703,9 @@ void FontSettingsPageWidget::refreshColorSchemeList()
int selected = 0; int selected = 0;
for (const FilePath &file : qAsConst(schemeList)) { for (const FilePath &file : qAsConst(schemeList)) {
if (FilePath::fromString(m_value.colorSchemeFileName()) == file) if (m_value.colorSchemeFileName() == file)
selected = colorSchemes.size(); selected = colorSchemes.size();
colorSchemes.append(ColorSchemeEntry(file.toString(), true)); colorSchemes.append(ColorSchemeEntry(file, true));
} }
if (colorSchemes.isEmpty()) if (colorSchemes.isEmpty())
@@ -713,9 +713,9 @@ void FontSettingsPageWidget::refreshColorSchemeList()
const FilePaths files = customStylesPath().dirEntries(FileFilter({"*.xml"}, QDir::Files)); const FilePaths files = customStylesPath().dirEntries(FileFilter({"*.xml"}, QDir::Files));
for (const FilePath &file : files) { for (const FilePath &file : files) {
if (FilePath::fromString(m_value.colorSchemeFileName()) == file) if (m_value.colorSchemeFileName() == file)
selected = colorSchemes.size(); selected = colorSchemes.size();
colorSchemes.append(ColorSchemeEntry(file.toString(), false)); colorSchemes.append(ColorSchemeEntry(file, false));
} }
m_refreshingSchemeList = true; m_refreshingSchemeList = true;
@@ -743,8 +743,8 @@ void FontSettingsPageWidget::apply()
int index = m_schemeComboBox->currentIndex(); int index = m_schemeComboBox->currentIndex();
if (index != -1) { if (index != -1) {
const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index); const ColorSchemeEntry &entry = m_schemeListModel.colorSchemeAt(index);
if (entry.fileName != m_value.colorSchemeFileName()) if (entry.filePath != m_value.colorSchemeFileName())
m_value.loadColorScheme(entry.fileName, m_descriptions); m_value.loadColorScheme(entry.filePath, m_descriptions);
} }
saveSettings(); saveSettings();