Subversion Repositories Scribus

Rev

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