Subversion Repositories Scribus

Rev

Rev 24209 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21855 jghali 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
8
#include "colorlistmodel.h"
9
 
10
#include "colorlistbox.h"
11
#include "commonstrings.h"
12
 
23653 craig 13
ColorPixmapValue::ColorPixmapValue() : m_name("invalid")
23345 jghali 14
{}
15
 
16
ColorPixmapValue::ColorPixmapValue(const ColorPixmapValue& other) : m_color(other.m_color), m_doc(other.m_doc), m_name(other.m_name)
17
{}
18
 
19
ColorPixmapValue& ColorPixmapValue::operator= (const ColorPixmapValue& other)
20
{
21
        m_color = other.m_color;
22
        m_doc = other.m_doc;
23
        m_name = other.m_name;
24
        return *this;
25
}
26
 
24209 jghali 27
ColorPixmapValue::ColorPixmapValue(const ScColor& col, ScribusDoc* doc, const QString& colName)
23345 jghali 28
{
29
        m_doc = (doc) ? doc->guardedPtr() : nullptr;
30
        m_color = col;
31
        m_name = colName;
32
}
33
 
21855 jghali 34
ColorListModel::ColorListModel(QObject *parent)
35
                  : QAbstractItemModel(parent)
36
{
37
        m_isNoneColorShown = false;
38
        m_sortRule = SortByName;
39
}
40
 
41
void ColorListModel::clear()
42
{
43
        beginResetModel();
44
        m_colors.clear();
45
        endResetModel();
46
}
47
 
48
int ColorListModel::columnCount(const QModelIndex &/*parent*/) const
49
{
50
        return 1;
51
}
52
 
53
QVariant ColorListModel::data(const QModelIndex &index, int role) const
54
{
55
        if (!index.isValid())
56
                return QVariant();
57
 
58
        ColorPixmapValue* pColorValue = static_cast<ColorPixmapValue*>(index.internalPointer());
59
        if (!pColorValue)
60
                return QVariant();
61
 
62
        if (role == Qt::DisplayRole)
63
        {
64
                if (pColorValue->m_name == CommonStrings::None)
65
                        return CommonStrings::tr_NoneColor;
66
                return pColorValue->m_name;
67
        }
68
 
21881 jghali 69
        if (role == Qt::ToolTipRole)
70
        {
71
                const ScColor& color = pColorValue->m_color;
23000 jghali 72
                if (pColorValue->m_name == CommonStrings::None)
73
                        return QVariant();
21881 jghali 74
                if (color.getColorModel() == colorModelRGB)
75
                {
76
                        int r, g, b;
77
                        color.getRawRGBColor(&r, &g, &b);
21885 jghali 78
                        return tr("R: %1 G: %2 B: %3").arg(r).arg(g).arg(b);
21881 jghali 79
                }
22603 craig 80
                if (color.getColorModel() == colorModelCMYK)
21881 jghali 81
                {
22212 jghali 82
                        double c, m, y, k;
21881 jghali 83
                        color.getCMYK(&c, &m, &y, &k);
22212 jghali 84
                        return tr("C: %1% M: %2% Y: %3% K: %4%").arg(c * 100, 0, 'f', 2).arg(m * 100, 0, 'f', 2).arg(y * 100, 0, 'f', 2).arg(k * 100, 0, 'f', 2);
21881 jghali 85
                }
22603 craig 86
                if (color.getColorModel() == colorModelLab)
21881 jghali 87
                {
88
                        double L, a, b;
89
                        color.getLab(&L, &a, &b);
21886 jghali 90
                        return tr("L: %1 a: %2 b: %3").arg(L, 0, 'f', 2).arg(a, 0, 'f', 2).arg(b, 0, 'f', 2);
21881 jghali 91
                }
92
                return QVariant();
93
        }
94
 
21855 jghali 95
        if (role == Qt::UserRole)
96
        {
97
                if (pColorValue->m_name == CommonStrings::None)
98
                        return CommonStrings::None;
99
                return QVariant::fromValue(*pColorValue);
100
        }
101
 
102
        return QVariant();
103
}
104
 
105
Qt::ItemFlags ColorListModel::flags(const QModelIndex &index) const
106
{
107
        if (!index.isValid())
24209 jghali 108
                return Qt::ItemFlags();
21855 jghali 109
 
110
        Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
111
        return flags;
112
}
113
 
114
QModelIndex ColorListModel::index(int row, int column, const QModelIndex &parent) const
115
{
116
        if (!hasIndex(row, column, parent))
117
                return QModelIndex();
118
 
119
        ColorPixmapValue* pColorValue = static_cast<ColorPixmapValue*>(parent.internalPointer());
120
        if (pColorValue)
121
                return QModelIndex();
122
 
123
        if (row < 0 || (row >= rowCount()) || (column != 0))
124
                return QModelIndex();
125
 
126
        const ColorPixmapValue& colorValue = m_colors.at(row);
127
        return createIndex(row, column, const_cast<ColorPixmapValue*>(&colorValue));
128
}
129
 
130
void ColorListModel::insert(int i, const ColorPixmapValue& value)
131
{
132
        beginInsertRows(QModelIndex(), i, i);
133
        m_colors.insert(i, value);
134
        endInsertRows();
135
}
136
 
137
QModelIndex ColorListModel::parent(const QModelIndex &/*child*/) const
138
{
139
        return QModelIndex();
140
}
141
 
142
bool ColorListModel::removeRow(int row, const QModelIndex& parent)
143
{
144
        if (row < 0 || row >= rowCount())
145
                return false;
146
 
147
        beginRemoveRows(parent, row, row);
148
        m_colors.remove(row);
149
        endRemoveRows();
150
 
151
        return true;
152
}
153
 
154
bool ColorListModel::removeRows(int row, int count, const QModelIndex& parent)
155
{
156
        if (row < 0 || row >= rowCount())
157
                return false;
158
        if (count <= 0)
159
                return false;
160
 
161
        beginRemoveRows(parent, row, row + count - 1);
162
        m_colors.remove(row, count);
163
        endRemoveRows();
164
 
165
        return true;
166
}
167
 
168
int ColorListModel::rowCount(const QModelIndex &parent) const
169
{
170
        if (m_colors.count() == 0)
171
                return 0;
172
 
173
        ColorPixmapValue* pColorValue = static_cast<ColorPixmapValue*>(parent.internalPointer());
174
        if (pColorValue)
175
                return 0;
176
 
177
        return m_colors.count();
178
}
179
 
180
void ColorListModel::setColorList(const ColorList& colorList)
181
{
182
        setColorList(colorList, m_isNoneColorShown);
183
}
184
 
185
void ColorListModel::setColorList(const ColorList& colorList, bool showNone)
186
{
187
        ScribusDoc* doc = colorList.document();
188
 
189
        beginResetModel();
190
 
191
        m_isNoneColorShown = showNone;
192
        m_colors.clear();
193
        m_colors.reserve(colorList.count());
194
 
195
        if (m_isNoneColorShown)
22603 craig 196
                m_colors.append(ColorPixmapValue(ScColor(), nullptr, CommonStrings::None));
21855 jghali 197
 
198
        ColorList::const_iterator iter;
199
        for (iter = colorList.begin(); iter != colorList.end(); ++iter)
200
        {
22603 craig 201
                const QString& colorName = iter.key();
21855 jghali 202
                const ScColor& color = iter.value();
203
                m_colors.append(ColorPixmapValue(color, doc, colorName));
204
        }
205
 
206
        if (m_sortRule != SortByName)
207
        {
208
                if (m_sortRule == SortByValues)
23210 craig 209
                        std::sort(m_colors.begin(), m_colors.end(), compareColorValues);
21855 jghali 210
                else if (m_sortRule == SortByType)
23210 craig 211
                        std::sort(m_colors.begin(), m_colors.end(), compareColorTypes);
21855 jghali 212
        }
213
 
214
        endResetModel();
215
}
216
 
217
void ColorListModel::setShowNoneColor(bool showNone)
218
{
219
        if (m_isNoneColorShown == showNone)
220
                return;
221
 
222
        beginResetModel();
223
        m_isNoneColorShown = showNone;
224
        endResetModel();
225
}
226
 
227
void ColorListModel::setSortRule(SortRule sortRule)
228
{
229
        if (m_sortRule == sortRule)
230
                return;
231
 
232
        beginResetModel();
233
 
234
        m_sortRule = sortRule;
235
        if (m_sortRule == SortByValues)
23210 craig 236
                std::sort(m_colors.begin(), m_colors.end(), compareColorValues);
21855 jghali 237
        else if (m_sortRule == SortByType)
23210 craig 238
                std::sort(m_colors.begin(), m_colors.end(), compareColorTypes);
21855 jghali 239
        else
23210 craig 240
                std::sort(m_colors.begin(), m_colors.end(), compareColorNames);
21855 jghali 241
 
242
        endResetModel();
243
}
244
 
245
bool ColorListModel::compareColorNames(const ColorPixmapValue& v1, const ColorPixmapValue& v2)
246
{
247
        if (v1.m_name == CommonStrings::None || v1.m_name == CommonStrings::tr_None)
248
                return true;
249
        if (v2.m_name == CommonStrings::None || v2.m_name == CommonStrings::tr_None)
250
                return false;
251
 
252
        return (v1.m_name < v2.m_name);
253
}
254
 
255
bool ColorListModel::compareColorValues(const ColorPixmapValue& v1, const ColorPixmapValue& v2)
256
{
257
        if (v1.m_name == CommonStrings::None || v1.m_name == CommonStrings::tr_None)
258
                return true;
259
        if (v2.m_name == CommonStrings::None || v2.m_name == CommonStrings::tr_None)
260
                return false;
261
 
262
        QColor c1 = v1.m_color.getRawRGBColor();
263
        QColor c2 = v2.m_color.getRawRGBColor();
264
 
265
        QString sortString1 = QString("%1-%2-%3-%4").arg(c1.hue(), 3, 10, QChar('0')).arg(c1.saturation(), 3, 10, QChar('0')).arg(c1.value(), 3, 10, QChar('0')).arg(v1.m_name);
266
        QString sortString2 = QString("%1-%2-%3-%4").arg(c2.hue(), 3, 10, QChar('0')).arg(c2.saturation(), 3, 10, QChar('0')).arg(c2.value(), 3, 10, QChar('0')).arg(v2.m_name);
267
        return (sortString1 < sortString2);
268
}
269
 
270
bool ColorListModel::compareColorTypes(const ColorPixmapValue& v1, const ColorPixmapValue& v2)
271
{
272
        if (v1.m_name == CommonStrings::None || v1.m_name == CommonStrings::tr_None)
273
                return true;
274
        if (v2.m_name == CommonStrings::None || v2.m_name == CommonStrings::tr_None)
275
                return false;
276
 
277
        QString sortString1 = QString("%1-%2");
278
        QString sortString2 = QString("%1-%2");
279
 
280
        if (v1.m_color.isRegistrationColor())
21935 craig 281
                sortString1 = sortString1.arg("A", v1.m_name);
21855 jghali 282
        else if (v1.m_color.isSpotColor())
21935 craig 283
                sortString1 = sortString1.arg("B", v1.m_name);
21855 jghali 284
        else if (v1.m_color.getColorModel() == colorModelCMYK)
21935 craig 285
                sortString1 = sortString1.arg("C", v1.m_name);
21855 jghali 286
        else
21935 craig 287
                sortString1 = sortString1.arg("D", v1.m_name);
21855 jghali 288
 
289
        if (v2.m_color.isRegistrationColor())
21935 craig 290
                sortString2 = sortString2.arg("A", v2.m_name);
21855 jghali 291
        else if (v2.m_color.isSpotColor())
21935 craig 292
                sortString2 = sortString2.arg("B", v2.m_name);
21855 jghali 293
        else if (v2.m_color.getColorModel() == colorModelCMYK)
21935 craig 294
                sortString2 = sortString2.arg("C", v2.m_name);
21855 jghali 295
        else
21935 craig 296
                sortString2 = sortString2.arg("D", v2.m_name);
21855 jghali 297
 
298
        return (sortString1 < sortString2);
21933 craig 299
}