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