-
Notifications
You must be signed in to change notification settings - Fork 0
/
stylemodel.cpp
236 lines (209 loc) · 5.8 KB
/
stylemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* <one line to give the program's name and a brief idea of what it does.>
* Copyright (C) 2016 Arek <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "stylemodel.h"
#include <QDebug>
#include <QFile>
#include <QDir>
StyleModel *StyleModel::model;
StyleModel::StyleModel()
{
loadColors();
}
StyleModel::~StyleModel()
{
}
void StyleModel::loadColors()
{
QFile f(QDir::currentPath() + "/defaultstyles.xml");
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
{
surpressSignals = true;
addStyle("default", Qt::black, Qt::white);
addStyle("black", Qt::white, Qt::black);
addStyle("lightgray", Qt::black, Qt::lightGray);
addStyle("darkgray", Qt::white, Qt::darkGray);
addStyle("brownish", Qt::black, Qt::darkYellow);
addStyle("cyan", Qt::black, Qt::cyan);
addStyle("red", Qt::white, Qt::darkRed);
//addColor("", Qt::, Qt::);
surpressSignals = false;
return;
}
QXmlStreamReader reader(&f);
QXmlStreamAttributes attr;
while (!reader.atEnd()) {
reader.readNext();
if (reader.isStartElement() && reader.name() == "styles")
loadStyles(reader);
}
f.close();
surpressSignals = false;
emit stylesChanged(All, nullptr);
}
void StyleModel::addStyle(QString name, QColor fg, QColor bg)
{
while (styles.contains(name))
name += "_";
Style *newStyle = new Style(name, fg, bg);
styles.insert(name, newStyle);
stylesByPointer[newStyle] = name;
if (!surpressSignals)
emit stylesChanged(StyleModel::New, newStyle);
}
void StyleModel::addStyle(Style* style)
{
while (styles.contains(style->name))
style->name += "_";
styles.insert(style->name, style);
stylesByPointer[style] = style->name;
if (!surpressSignals)
emit stylesChanged(StyleModel::New, style);
}
QVariant StyleModel::getBrushV(QString name, int role) const
{
if (name == "")
return getBrushV("default", role);
QBrush brush;
if (role == Qt::ForegroundRole)
brush.setColor(styles[name]->fg.color());
else
brush = styles[name]->bg;
return QVariant(brush);
}
QColor StyleModel::getColor(QString name, int role)
{
if (name == "")
return getColor("default", role);
if (!styles.contains(name)) {
qDebug() << "no style named " << name;
return QColor();
}
if (role == Qt::ForegroundRole)
return styles[name]->fg.color();
else if (role == Qt::BackgroundRole)
return styles[name]->bg.color();
else
qDebug() << "WTF???";
}
void StyleModel::deleteStyle(Style* style)
{
if (style->name == "default")
return;
if (styles[style->name] == style)
delete styles.take(style->name);
else {
QString name = stylesByPointer[style];
delete styles.take(name);
}
stylesByPointer.remove(style);
if (!surpressSignals)
emit stylesChanged(StyleModel::Delete, style);
}
void StyleModel::deleteStyle(QString style)
{
deleteStyle(styles[style]);
}
bool StyleModel::styleChangedOk(Style* style)
{
if (styles.value(style->name) == style) {
if (!surpressSignals)
emit stylesChanged(StyleModel::Edit, style);
return true;
}
bool result = true;
while (styles.contains(style->name)) {
result = false;
style->name += "_";
}
QString name = stylesByPointer.take(style);
styles.remove(name);
styles[style->name] = style;
stylesByPointer[style] = name;
if (!surpressSignals)
emit stylesChanged(StyleModel::Name, style);
return result;
}
QFont StyleModel::getFont(QString name) const
{
if (styles.contains(name))
return styles[name]->font;
return QFont();
}
bool StyleModel::loadStyles(QXmlStreamReader& reader)
{
surpressSignals = true;
foreach (Style *style, stylesByPointer.keys())
delete style;
stylesByPointer.clear();
styles.clear();
QXmlStreamAttributes attr;
while (!reader.atEnd()) {
reader.readNext();
if (reader.isEndElement() && reader.name() == "styles" )
break;
if ( (reader.isEndElement() && reader.name() != "style" )
|| reader.isEndDocument()) {
qDebug() << "not ok";
surpressSignals = false;
return false;
}
if (!reader.isStartElement() || reader.name() != "style")
continue;
attr = reader.attributes();
Style *style = new Style(
attr.value("name").toString(),
QColor(attr.value("fg").toString()),
QColor(attr.value("bg").toString())
);
style->setFont(attr.value("font").toString());
style->setSize(attr.value("maxFontSize").toDouble());
addStyle(style);
}
surpressSignals = false;
emit stylesChanged(All, nullptr);
}
void StyleModel::saveStyles(QXmlStreamWriter& writer)
{
writer.writeStartElement("styles");
foreach(Style *style, stylesByPointer.keys()) {
writer.writeStartElement("style");
writer.writeAttribute("name", style->name);
writer.writeAttribute("fg", style->fg.color().name());
writer.writeAttribute("bg", style->bg.color().name());
writer.writeAttribute("font", style->font.family());
writer.writeAttribute("maxFontSize", QString::number(style->font.pointSizeF()));
writer.writeEndElement();
}
writer.writeEndElement();
}
void StyleModel::saveDefaultStyles()
{
QFile f(QDir::currentPath() + "/defaultstyles.xml");
if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "could not open \"defaultstyles.xml\" for writing";
return;
}
QXmlStreamWriter writer(&f);
writer.setAutoFormatting(true);
writer.writeStartDocument();
saveStyles(writer);
writer.writeEndDocument();
f.close();
}