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