Implemented reading the name of the color scheme

This commit is contained in:
Thorbjørn Lindeijer
2009-07-08 09:45:30 +02:00
parent 947b719e69
commit c6d4663ddb
3 changed files with 48 additions and 11 deletions

View File

@@ -162,6 +162,8 @@ bool ColorScheme::save(const QString &fileName)
w.writeStartDocument(); w.writeStartDocument();
w.writeStartElement(QLatin1String("style-scheme")); w.writeStartElement(QLatin1String("style-scheme"));
w.writeAttribute(QLatin1String("version"), QLatin1String("1.0")); w.writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
if (!m_name.isEmpty())
w.writeAttribute(QLatin1String("name"), m_name);
Format textFormat = formatFor(QLatin1String(Constants::C_TEXT)); Format textFormat = formatFor(QLatin1String(Constants::C_TEXT));
@@ -192,11 +194,12 @@ namespace {
class ColorSchemeReader : public QXmlStreamReader class ColorSchemeReader : public QXmlStreamReader
{ {
public: public:
ColorSchemeReader(ColorScheme *scheme) : ColorSchemeReader() :
m_scheme(scheme) m_scheme(0)
{} {}
bool read(const QString &fileName); bool read(const QString &fileName, ColorScheme *scheme);
QString readName(const QString &fileName);
private: private:
void readUnknownElement(); void readUnknownElement();
@@ -204,11 +207,15 @@ private:
void readStyle(); void readStyle();
ColorScheme *m_scheme; ColorScheme *m_scheme;
QString m_name;
}; };
bool ColorSchemeReader::read(const QString &fileName) bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme)
{ {
m_scheme->clear(); m_scheme = scheme;
if (m_scheme)
m_scheme->clear();
QFile file(fileName); QFile file(fileName);
if (!file.open(QFile::ReadOnly | QFile::Text)) if (!file.open(QFile::ReadOnly | QFile::Text))
@@ -228,6 +235,12 @@ bool ColorSchemeReader::read(const QString &fileName)
return true; return true;
} }
QString ColorSchemeReader::readName(const QString &fileName)
{
read(fileName, 0);
return m_name;
}
void ColorSchemeReader::readUnknownElement() void ColorSchemeReader::readUnknownElement()
{ {
Q_ASSERT(isStartElement()); Q_ASSERT(isStartElement());
@@ -245,6 +258,14 @@ void ColorSchemeReader::readStyleScheme()
{ {
Q_ASSERT(isStartElement() && name() == QLatin1String("style-scheme")); Q_ASSERT(isStartElement() && name() == QLatin1String("style-scheme"));
const QXmlStreamAttributes attr = attributes();
m_name = attr.value(QLatin1String("name")).toString();
if (!m_scheme)
// We're done
raiseError(QLatin1String("name loaded"));
else
m_scheme->setName(m_name);
while (readNext() != Invalid) { while (readNext() != Invalid) {
if (isEndElement()) { if (isEndElement()) {
break; break;
@@ -289,6 +310,11 @@ void ColorSchemeReader::readStyle()
bool ColorScheme::load(const QString &fileName) bool ColorScheme::load(const QString &fileName)
{ {
ColorSchemeReader reader(this); ColorSchemeReader reader;
return reader.read(fileName) && !reader.hasError(); return reader.read(fileName, this) && !reader.hasError();
}
QString ColorScheme::readNameOfScheme(const QString &fileName)
{
return ColorSchemeReader().readName(fileName);
} }

View File

@@ -81,6 +81,12 @@ class ColorScheme
public: public:
ColorScheme(); ColorScheme();
void setName(const QString &name)
{ m_name = name; }
QString name() const
{ return m_name; }
inline bool isEmpty() const inline bool isEmpty() const
{ return m_formats.isEmpty(); } { return m_formats.isEmpty(); }
@@ -97,10 +103,16 @@ public:
bool load(const QString &fileName); bool load(const QString &fileName);
inline bool equals(const ColorScheme &cs) const inline bool equals(const ColorScheme &cs) const
{ return m_formats == cs.m_formats; } {
return m_formats == cs.m_formats
&& m_name == cs.m_name;
}
static QString readNameOfScheme(const QString &fileName);
private: private:
QMap<QString, Format> m_formats; QMap<QString, Format> m_formats;
QString m_name;
}; };
inline bool operator==(const ColorScheme &cs1, const ColorScheme &cs2) { return cs1.equals(cs2); } inline bool operator==(const ColorScheme &cs1, const ColorScheme &cs2) { return cs1.equals(cs2); }

View File

@@ -329,10 +329,9 @@ void FontSettingsPage::refreshColorSchemeList()
int count = 0; int count = 0;
foreach (const QString &file, styleDir.entryList()) { foreach (const QString &file, styleDir.entryList()) {
// TODO: Read the name of the style
QListWidgetItem *item = new QListWidgetItem(file);
const QString absFileName = styleDir.absoluteFilePath(file); const QString absFileName = styleDir.absoluteFilePath(file);
item->setData(Qt::UserRole, absFileName ); QListWidgetItem *item = new QListWidgetItem(ColorScheme::readNameOfScheme(absFileName));
item->setData(Qt::UserRole, absFileName);
d_ptr->ui.schemeListWidget->addItem(item); d_ptr->ui.schemeListWidget->addItem(item);
if (d_ptr->m_value.colorSchemeFileName() == absFileName) if (d_ptr->m_value.colorSchemeFileName() == absFileName)
selected = count; selected = count;