-
Notifications
You must be signed in to change notification settings - Fork 4
/
colormanager.cpp
292 lines (237 loc) · 6.23 KB
/
colormanager.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <QFile>
#include <QTextStream>
#include <QVariant>
#include <qjson/parser.h>
#include "color.h"
#include "settings.h"
#include "stitch.h"
#include "colormanager.h"
ColorManager::ColorManager(QObject *parent)
: QObject(parent), name_(QObject::tr("My Colors"))
{
isDependent_ = true;
}
ColorManager::ColorManager(const QString &id, const QString &name,
QObject *parent)
: QObject(parent), id_(id), name_(name)
{
isDependent_ = false;
}
ColorManager::ColorManager(const QString &id, const QString &name,
const QString &path, QObject *parent)
: QObject(parent), id_(id), name_(name)
{
isDependent_ = false;
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
qFatal("Could not open color table!");
QTextStream stream(&file);
QString jsonText = stream.readAll();
file.close();
QJson::Parser parser;
bool ok;
QVariant result = parser.parse(jsonText.toUtf8(), &ok);
if (!ok) {
qFatal("Could not read color table!");
}
load(result);
}
ColorManager::~ColorManager()
{
if (!isDependent_) {
for (QHash<QString, const Color *>::iterator it = colorMap_.begin();
it != colorMap_.end();
++it) {
delete it.value();
}
}
colorMap_.clear();
colorList_.clear();
}
void ColorManager::load(const QVariant &l)
{
const QList<QVariant> &list = l.toList();
foreach(const QVariant &i, list) {
const QMap<QString, QVariant> &map = i.toMap();
Color *c = new Color(map["name"].toString(),
map["id"].toString(),
QColor(map["color"].toString()));
c->setParent(this);
add(c);
}
}
void ColorManager::add(const Color *c)
{
if (!colorMap_.contains(c->id())) {
colorMap_.insert(c->id(), c);
emit colorAppended();
colorList_.append(colorMap_[c->id()]);
emit listChanged();
}
}
void ColorManager::insert(const Color *c, int before)
{
if (!colorMap_.contains(c->id())) {
colorMap_.insert(c->id(), c);
emit colorInserted(before);
colorList_.insert(before, colorMap_[c->id()]);
emit listChanged();
}
}
void ColorManager::remove(const QString &id)
{
QHash<QString, const Color *>::Iterator it = colorMap_.find(id);
if (it == colorMap_.end())
return;
int index = colorList_.indexOf(*it);
if (index >= 0) {
colorList_.remove(index);
emit colorDeleted(index);
}
colorMap_.erase(it);
emit listChanged();
}
void ColorManager::swap(int index1, int index2)
{
const Color *temp;
int size = colorList_.size();
if (index1 >= size || index2 >= size)
return;
temp = colorList_[index1];
colorList_[index1] = colorList_[index2];
colorList_[index2] = temp;
emit listChanged();
emit colorSwapped(index1, index2);
}
const Color* ColorManager::get(const QString &id) const
{
QHash<QString, const Color *>::const_iterator i = colorMap_.find(id);
if (i == colorMap_.end())
return NULL;
return *i;
}
const Color* ColorManager::itemAt(int index) const
{
if (index < 0 || index >= colorList_.count())
return NULL;
return colorList_[index];
}
int ColorManager::count() const
{
return colorList_.size();
}
void ColorManager::clear()
{
colorMap_.clear();
colorList_.clear();
}
ColorUsageTracker::ColorUsageTracker(QObject *parent)
: ColorManager(parent), total_(0.0)
{
}
ColorUsageTracker::~ColorUsageTracker()
{
}
void ColorUsageTracker::acquire(StitchItem *item)
{
const Color *key = item->color();
backrefMap_[key].insert(item);
weightMap_[key] += item->weight();
total_ += item->weight();
const Color *c = item->color();
if (colorMap_.find(c->id()) == colorMap_.end())
add(c);
}
void ColorUsageTracker::release(StitchItem *item)
{
const Color *key = item->color();
QSet<StitchItem *> &set = backrefMap_[key];
total_ -= item->weight();
weightMap_[key] -= item->weight();
set.remove(item);
if (set.size() == 0) {
remove(item->color()->id());
weightMap_.remove(key);
backrefMap_.remove(key);
}
}
const QSet<StitchItem *>* ColorUsageTracker::items(const Color *color) const
{
BackRefMap::ConstIterator it = backrefMap_.find(color);
if (it == backrefMap_.end())
return NULL;
return &(*it);
}
MetaColorManager::MetaColorManager(QObject *parent)
: QObject(parent)
{
localSwatches_.setDependent(true);
}
MetaColorManager::MetaColorManager(const QString &path, QObject *parent)
: QObject(parent)
{
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
qFatal("Could not open color table!");
QTextStream stream(&file);
QString jsonText = stream.readAll();
file.close();
QJson::Parser parser;
bool ok;
QVariant result = parser.parse(jsonText.toUtf8(), &ok);
if (!ok) {
qFatal("Could not read color table!");
}
const QList<QVariant> &list = result.toList();
foreach (const QVariant &v, list) {
const QMap<QString, QVariant> &map = v.toMap();
ColorManager *cm = createColorManager(map["id"].toString(), map["name"].toString());
cm->load(map["colors"]);
}
populateMyColors();
}
MetaColorManager::~MetaColorManager()
{
for (QHash<QString, ColorManager *>::Iterator it = colorManagers_.begin();
it != colorManagers_.end();
++it) {
delete it.value();
}
}
void MetaColorManager::populateMyColors()
{
localSwatches_.clear();
QStringList colors = Settings::self()->myColors();
foreach (const QString &s, colors) {
QStringList fr = s.split("|");
const Color *c = get(fr[0], fr[1]);
if (c)
localSwatches_.add(c);
}
}
const Color* MetaColorManager::get(const QString &category, const QString &id)
{
QHash<QString, ColorManager *>::Iterator it = colorManagers_.find(category);
if (it == colorManagers_.end())
return NULL;
return it.value()->get(id);
}
ColorManager* MetaColorManager::createColorManager(const QString &id,
const QString &name)
{
QHash<QString, ColorManager *>::Iterator it = colorManagers_.find(id);
if (it != colorManagers_.end()) {
colorManagerList_.removeAll(it.value());
delete it.value();
}
ColorManager *cm = new ColorManager(id, name);
colorManagers_[id] = cm;
colorManagerList_.append(cm);
return cm;
}
ColorManager* MetaColorManager::colorManager(const QString &id)
{
if (colorManagers_.contains(id))
return colorManagers_[id];
return NULL;
}