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