SymbolEditor  1.3.0
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
SymbolLibrary.cpp
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2011 by Stephen Allewell *
3  * sallewell@users.sourceforge.net *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  ********************************************************************************/
10 
11 
51 #include "SymbolLibrary.h"
52 
53 #include <QDataStream>
54 #include <QListWidgetItem>
55 #include <QPainter>
56 #include <QtAlgorithms>
57 
58 #include <KLocale>
59 
60 #include "Exceptions.h"
61 #include "SymbolListWidget.h"
62 
63 
71  : m_listWidget(listWidget)
72 {
73  clear();
74 }
75 
76 
81 {
82  clear();
83 }
84 
85 
92 {
93  m_undoStack.clear();
94 
95  if (m_listWidget) {
96  foreach (qint16 index, indexes()) {
97  m_listWidget->removeSymbol(index);
98  }
99  }
100 
101  m_symbols.clear();
102  m_nextIndex = 1;
103 }
104 
105 
115 {
116  return (m_symbols.contains(index) ? m_symbols[index] : Symbol());
117 }
118 
119 
131 {
132  Symbol symbol;
133 
134  if (m_symbols.contains(index)) {
135  symbol = m_symbols.take(index);
136 
137  if (m_listWidget) {
138  m_listWidget->removeSymbol(index);
139  }
140  }
141 
142  return symbol;
143 }
144 
145 
158 qint16 SymbolLibrary::setSymbol(qint16 index, const Symbol &symbol)
159 {
160  if (!index) {
161  index = m_nextIndex++;
162  }
163 
164  m_symbols.insert(index, symbol);
165 
166  if (m_listWidget) {
167  m_listWidget->addSymbol(index, symbol);
168  }
169 
170  return index;
171 }
172 
173 
177 QString SymbolLibrary::name() const
178 {
179  return m_name;
180 }
181 
182 
188 void SymbolLibrary::setName(const QString &name)
189 {
190  m_name = name;
191 }
192 
193 
199 QList<qint16> SymbolLibrary::indexes() const
200 {
201  QList<qint16> keys = m_symbols.keys();
202  qSort(keys.begin(), keys.end());
203  return keys;
204 }
205 
206 
213 {
214  return &m_undoStack;
215 }
216 
217 
226 {
227  if (!m_listWidget) {
228  return;
229  }
230 
232 }
233 
234 
246 QDataStream &operator<<(QDataStream &stream, const SymbolLibrary &library)
247 {
248  qint16 lastIndex = 0;
249 
250  foreach (qint16 index, library.m_symbols.keys()) {
251  if (index > lastIndex) {
252  lastIndex = index;
253  }
254  }
255 
256  lastIndex++;
257 
258  stream.writeRawData("KXStitchSymbols", 15);
259  stream.setVersion(QDataStream::Qt_4_0);
260  stream << library.version;
261  stream << lastIndex;
262 
263  if (stream.status() != QDataStream::Ok) {
264  throw FailedWriteLibrary(stream.status());
265  }
266 
267  stream << library.m_symbols;
268  return stream;
269 }
270 
271 
287 QDataStream &operator>>(QDataStream &stream, SymbolLibrary &library)
288 {
289  library.clear();
290 
291  char magic[15];
292  stream.readRawData(magic, 15);
293 
294  if (strncmp(magic, "KXStitchSymbols", 15) == 0) {
295  stream.setVersion(QDataStream::Qt_4_0);
296  qint32 version;
297  qint16 nextIndex;
298  QMap<qint16, QPainterPath> paths_v100;
299  QList<qint16> paths_v100_keys;
300  stream >> version;
301 
302  switch (version) {
303  case 101:
304  stream >> library.m_nextIndex;
305 
306  if (stream.status() != QDataStream::Ok) {
307  throw FailedReadLibrary(stream.status());
308  }
309 
310  stream >> library.m_symbols;
311  library.generateItems();
312  break;
313 
314  case 100:
315  stream >> library.m_nextIndex;
316  library.m_nextIndex++;
317  stream >> paths_v100;
318  paths_v100_keys = paths_v100.keys();
319 
320  foreach (qint16 index, paths_v100_keys) {
321  Symbol symbol;
322  symbol.setPath(paths_v100[index]);
323  library.m_symbols.insert(index, symbol);
324  }
325 
326  library.generateItems();
327  break;
328 
329  default:
330  throw InvalidFileVersion(version);
331  break;
332  }
333  } else {
334  throw InvalidFile();
335  }
336 
337  return stream;
338 }