Subversion Repositories Scribus

Rev

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