Subversion Repositories Scribus

Rev

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