summaryrefslogtreecommitdiff
authorleseb <leseb>2002-04-07 10:27:38 (UTC)
committer leseb <leseb>2002-04-07 10:27:38 (UTC)
commitd7664dfe7f78fe89f85e168cad8cbf9c175ad6e3 (patch) (unidiff)
treeacab53680e94465be80ae20e08bec96316256c0b
parente237c0758206c34f96c11870e033412b89e37463 (diff)
downloadopie-d7664dfe7f78fe89f85e168cad8cbf9c175ad6e3.zip
opie-d7664dfe7f78fe89f85e168cad8cbf9c175ad6e3.tar.gz
opie-d7664dfe7f78fe89f85e168cad8cbf9c175ad6e3.tar.bz2
Fix load/save of XML file
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/graphics/drawpad/drawpadcanvas.cpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/noncore/graphics/drawpad/drawpadcanvas.cpp b/noncore/graphics/drawpad/drawpadcanvas.cpp
index b8a1c2e..4e17245 100644
--- a/noncore/graphics/drawpad/drawpadcanvas.cpp
+++ b/noncore/graphics/drawpad/drawpadcanvas.cpp
@@ -6,363 +6,371 @@
6 * * 6 * *
7 * This program is free software; you can redistribute it and/or modify * 7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by * 8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or * 9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. * 10 * (at your option) any later version. *
11 * * 11 * *
12 ***************************************************************************/ 12 ***************************************************************************/
13 13
14#include "drawpadcanvas.h" 14#include "drawpadcanvas.h"
15 15
16#include "drawpad.h" 16#include "drawpad.h"
17#include "newpagedialog.h" 17#include "newpagedialog.h"
18#include "tool.h" 18#include "tool.h"
19 19
20#include <qpe/applnk.h> 20#include <qpe/applnk.h>
21#include <qpe/filemanager.h> 21#include <qpe/filemanager.h>
22#include <qpe/mimetype.h> 22#include <qpe/mimetype.h>
23 23
24#include <qbuffer.h> 24#include <qbuffer.h>
25#include <qimage.h> 25#include <qimage.h>
26#include <qmessagebox.h> 26#include <qmessagebox.h>
27#include <qpainter.h> 27#include <qpainter.h>
28#include <qpixmap.h> 28#include <qpixmap.h>
29#include <qtextcodec.h> 29#include <qtextcodec.h>
30#include <qtextstream.h> 30#include <qtextstream.h>
31#include <qxml.h> 31#include <qxml.h>
32 32
33#include <zlib.h> 33#include <zlib.h>
34 34
35class DrawPadCanvasXmlHandler: public QXmlDefaultHandler 35class DrawPadCanvasXmlHandler: public QXmlDefaultHandler
36{ 36{
37public: 37public:
38 DrawPadCanvasXmlHandler(); 38 DrawPadCanvasXmlHandler();
39 ~DrawPadCanvasXmlHandler(); 39 ~DrawPadCanvasXmlHandler();
40 40
41 QList<QPixmap> pixmaps(); 41 QList<QPixmap> pixmaps();
42 42
43 bool startElement(const QString& namespaceURI, const QString& localName, 43 bool startElement(const QString& namespaceURI, const QString& localName,
44 const QString& qName, const QXmlAttributes& atts); 44 const QString& qName, const QXmlAttributes& atts);
45 bool endElement(const QString& namespaceURI, const QString& localName, 45 bool endElement(const QString& namespaceURI, const QString& localName,
46 const QString& qName); 46 const QString& qName);
47 bool characters(const QString& ch); 47 bool characters(const QString& ch);
48 48
49private: 49private:
50 enum State { 50 enum State {
51 Unknown, 51 Unknown,
52 InData 52 InData
53 }; 53 };
54 54
55 State m_state; 55 State m_state;
56 ulong m_dataLenght; 56 ulong m_dataLenght;
57 QString m_dataFormat; 57 QString m_dataFormat;
58 QList<QPixmap> m_pixmaps; 58 QList<QPixmap> m_pixmaps;
59}; 59};
60 60
61DrawPadCanvasXmlHandler::DrawPadCanvasXmlHandler() 61DrawPadCanvasXmlHandler::DrawPadCanvasXmlHandler()
62{ 62{
63 m_state = Unknown; 63 m_state = Unknown;
64} 64}
65 65
66DrawPadCanvasXmlHandler::~DrawPadCanvasXmlHandler() 66DrawPadCanvasXmlHandler::~DrawPadCanvasXmlHandler()
67{ 67{
68} 68}
69 69
70QList<QPixmap> DrawPadCanvasXmlHandler::pixmaps() 70QList<QPixmap> DrawPadCanvasXmlHandler::pixmaps()
71{ 71{
72 return m_pixmaps; 72 return m_pixmaps;
73} 73}
74 74
75bool DrawPadCanvasXmlHandler::startElement(const QString& namespaceURI, const QString& localName, 75bool DrawPadCanvasXmlHandler::startElement(const QString& namespaceURI, const QString& localName,
76 const QString& qName, const QXmlAttributes& atts) 76 const QString& qName, const QXmlAttributes& atts)
77{ 77{
78 Q_CONST_UNUSED(namespaceURI) 78 Q_CONST_UNUSED(namespaceURI)
79 Q_CONST_UNUSED(localName) 79 Q_CONST_UNUSED(localName)
80 80
81 if (qName.compare("data") == 0) { 81 if (qName.compare("data") == 0) {
82 m_state = InData; 82 m_state = InData;
83 m_dataLenght = atts.value("length").toULong(); 83 m_dataLenght = atts.value("length").toULong();
84 m_dataFormat = atts.value("format"); 84 m_dataFormat = atts.value("format");
85 85
86 if (m_dataFormat.isEmpty()) { 86 if (m_dataFormat.isEmpty()) {
87 m_dataFormat = "XPM"; 87 m_dataFormat = "XPM";
88 } 88 }
89 } 89 }
90 90
91 return true; 91 return true;
92} 92}
93 93
94bool DrawPadCanvasXmlHandler::endElement(const QString& namespaceURI, const QString& localName, 94bool DrawPadCanvasXmlHandler::endElement(const QString& namespaceURI, const QString& localName,
95 const QString& qName) 95 const QString& qName)
96{ 96{
97 Q_CONST_UNUSED(namespaceURI) 97 Q_CONST_UNUSED(namespaceURI)
98 Q_CONST_UNUSED(localName) 98 Q_CONST_UNUSED(localName)
99 99
100 if (qName.compare("data") == 0) { 100 if (qName.compare("data") == 0) {
101 m_state = Unknown; 101 m_state = Unknown;
102 } 102 }
103 103
104 return true; 104 return true;
105} 105}
106 106
107bool DrawPadCanvasXmlHandler::characters(const QString& ch) 107bool DrawPadCanvasXmlHandler::characters(const QString& ch)
108{ 108{
109 if (m_state == InData) { 109 if (m_state == InData) {
110 QByteArray byteArray(ch.length() / 2); 110 QByteArray byteArray(ch.length() / 2);
111 111
112 for (int i = 0; i < (int)ch.length() / 2; i++) { 112 for (int i = 0; i < (int)ch.length() / 2; i++) {
113 char h = ch[2 * i].latin1(); 113 char h = ch[2 * i].latin1();
114 char l = ch[2 * i + 1].latin1(); 114 char l = ch[2 * i + 1].latin1();
115 uchar r = 0; 115 uchar r = 0;
116 116
117 if (h <= '9') { 117 if (h <= '9') {
118 r += h - '0'; 118 r += h - '0';
119 } else { 119 } else {
120 r += h - 'a' + 10; 120 r += h - 'a' + 10;
121 } 121 }
122 122
123 r = r << 4; 123 r = r << 4;
124 124
125 if (l <= '9') { 125 if (l <= '9') {
126 r += l - '0'; 126 r += l - '0';
127 } else { 127 } else {
128 r += l - 'a' + 10; 128 r += l - 'a' + 10;
129 } 129 }
130 130
131 byteArray[i] = r; 131 byteArray[i] = r;
132 } 132 }
133 133
134 if (m_dataLenght < ch.length() * 5) {
135 m_dataLenght = ch.length() * 5;
136 }
137
138 QByteArray byteArrayUnzipped(m_dataLenght);
139 ::uncompress((uchar*)byteArrayUnzipped.data(), &m_dataLenght, (uchar*)byteArray.data(), byteArray.size());
140 134
141 QImage image; 135 QImage image;
142 image.loadFromData((const uchar*)byteArrayUnzipped.data(), m_dataLenght, m_dataFormat); 136
137 if (m_dataFormat == "XPM") {
138 if (m_dataLenght < ch.length() * 5) {
139 m_dataLenght = ch.length() * 5;
140 }
141
142 QByteArray byteArrayUnzipped(m_dataLenght);
143 ::uncompress((uchar*)byteArrayUnzipped.data(), &m_dataLenght, (uchar*)byteArray.data(), byteArray.size());
144
145 image.loadFromData((const uchar*)byteArrayUnzipped.data(), m_dataLenght, m_dataFormat);
146 } else {
147 image.loadFromData((const uchar*)byteArray.data(), m_dataLenght, m_dataFormat);
148 }
143 149
144 QPixmap* pixmap = new QPixmap(image.width(), image.height()); 150 QPixmap* pixmap = new QPixmap(image.width(), image.height());
145 pixmap->convertFromImage(image); 151 pixmap->convertFromImage(image);
146 m_pixmaps.append(pixmap); 152 m_pixmaps.append(pixmap);
147 } 153 }
148 154
149 return true; 155 return true;
150} 156}
151 157
152DrawPadCanvas::DrawPadCanvas(DrawPad* drawPad, QWidget* parent, const char* name) 158DrawPadCanvas::DrawPadCanvas(DrawPad* drawPad, QWidget* parent, const char* name)
153 : QScrollView(parent, name) 159 : QScrollView(parent, name)
154{ 160{
155 m_pDrawPad = drawPad; 161 m_pDrawPad = drawPad;
156 m_pages.setAutoDelete(true); 162 m_pages.setAutoDelete(true);
157 m_pageBackups.setAutoDelete(true); 163 m_pageBackups.setAutoDelete(true);
158} 164}
159 165
160DrawPadCanvas::~DrawPadCanvas() 166DrawPadCanvas::~DrawPadCanvas()
161{ 167{
162} 168}
163 169
164void DrawPadCanvas::load(QIODevice* ioDevice) 170void DrawPadCanvas::load(QIODevice* ioDevice)
165{ 171{
166 QTextStream textStream(ioDevice); 172 QTextStream textStream(ioDevice);
167 textStream.setCodec(QTextCodec::codecForName("UTF-8")); 173 textStream.setCodec(QTextCodec::codecForName("UTF-8"));
168 174
169 QXmlInputSource xmlInputSource(textStream); 175 QXmlInputSource xmlInputSource(textStream);
170 QXmlSimpleReader xmlSimpleReader; 176 QXmlSimpleReader xmlSimpleReader;
171 DrawPadCanvasXmlHandler drawPadCanvasXmlHandler; 177 DrawPadCanvasXmlHandler drawPadCanvasXmlHandler;
172 178
173 xmlSimpleReader.setContentHandler(&drawPadCanvasXmlHandler); 179 xmlSimpleReader.setContentHandler(&drawPadCanvasXmlHandler);
174 xmlSimpleReader.parse(xmlInputSource); 180 xmlSimpleReader.parse(xmlInputSource);
175 181
176 m_pages = drawPadCanvasXmlHandler.pixmaps(); 182 m_pages = drawPadCanvasXmlHandler.pixmaps();
177 183
178 if (m_pages.isEmpty()) { 184 if (m_pages.isEmpty()) {
179 m_pages.append(new QPixmap(contentsRect().size())); 185 m_pages.append(new QPixmap(contentsRect().size()));
180 m_pages.current()->fill(Qt::white); 186 m_pages.current()->fill(Qt::white);
181 } 187 }
182 188
183 m_pageBackups.clear(); 189 m_pageBackups.clear();
184 m_pageBackups.append(new QPixmap(*(m_pages.current()))); 190 m_pageBackups.append(new QPixmap(*(m_pages.current())));
185 191
186 resizeContents(m_pages.current()->width(), m_pages.current()->height()); 192 resizeContents(m_pages.current()->width(), m_pages.current()->height());
187 viewport()->update(); 193 viewport()->update();
188 194
189 emit pagesChanged(); 195 emit pagesChanged();
190 emit pageBackupsChanged(); 196 emit pageBackupsChanged();
191} 197}
192 198
193void DrawPadCanvas::initialPage() 199void DrawPadCanvas::initialPage()
194{ 200{
195 m_pages.append(new QPixmap(234, 233)); 201 m_pages.append(new QPixmap(234, 233));
196 m_pages.current()->fill(Qt::white); 202 m_pages.current()->fill(Qt::white);
197 203
198 m_pageBackups.clear(); 204 m_pageBackups.clear();
199 m_pageBackups.append(new QPixmap(*(m_pages.current()))); 205 m_pageBackups.append(new QPixmap(*(m_pages.current())));
200 206
201 resizeContents(m_pages.current()->width(), m_pages.current()->height()); 207 resizeContents(m_pages.current()->width(), m_pages.current()->height());
202 viewport()->update(); 208 viewport()->update();
203 209
204 emit pagesChanged(); 210 emit pagesChanged();
205 emit pageBackupsChanged(); 211 emit pageBackupsChanged();
206} 212}
207 213
208void DrawPadCanvas::save(QIODevice* ioDevice) 214void DrawPadCanvas::save(QIODevice* ioDevice)
209{ 215{
210 QTextStream textStream(ioDevice); 216 QTextStream textStream(ioDevice);
211 textStream.setCodec(QTextCodec::codecForName("UTF-8")); 217 textStream.setCodec(QTextCodec::codecForName("UTF-8"));
212 218
213 textStream << "<drawpad>" << endl; 219 textStream << "<drawpad>" << endl;
214 textStream << " <images>" << endl; 220 textStream << " <images>" << endl;
215 221
216 QListIterator<QPixmap> bufferIterator(m_pages); 222 QListIterator<QPixmap> bufferIterator(m_pages);
217 223
218 for (bufferIterator.toFirst(); bufferIterator.current() != 0; ++bufferIterator) { 224 for (bufferIterator.toFirst(); bufferIterator.current() != 0; ++bufferIterator) {
219 textStream << " <image>" << endl; 225 textStream << " <image>" << endl;
220 226
221 QImage image = bufferIterator.current()->convertToImage(); 227 QImage image = bufferIterator.current()->convertToImage();
222 QByteArray byteArray; 228 QByteArray byteArray;
223 QBuffer buffer(byteArray); 229 QBuffer buffer(byteArray);
224 QImageIO imageIO(&buffer, "PNG"); 230 QImageIO imageIO(&buffer, "PNG");
225 231
226 buffer.open(IO_WriteOnly); 232 buffer.open(IO_WriteOnly);
227 imageIO.setImage(image); 233 imageIO.setImage(image);
228 imageIO.write(); 234 imageIO.write();
229 buffer.close(); 235 buffer.close();
230 236
237/*
231 ulong size = byteArray.size() * 2; 238 ulong size = byteArray.size() * 2;
232 QByteArray byteArrayZipped(size); 239 QByteArray byteArrayZipped(size);
233 ::compress((uchar*)byteArrayZipped.data(), &size, (uchar*)byteArray.data(), byteArray.size()); 240 ::compress((uchar*)byteArrayZipped.data(), &size, (uchar*)byteArray.data(), byteArray.size());
241*/
234 242
235 textStream << " <data length=\"" << byteArray.size() << "\" format=\"PNG\">"; 243 textStream << " <data length=\"" << byteArray.size() << "\" format=\"PNG\">";
236 244
237 static const char hexchars[] = "0123456789abcdef"; 245 static const char hexchars[] = "0123456789abcdef";
238 246
239 for (int i = 0; i < (int)size; i++ ) { 247 for (int i = 0; i < (int)byteArray.size(); i++ ) {
240 uchar s = (uchar)byteArrayZipped[i]; 248 uchar s = (uchar)byteArray[i];
241 textStream << hexchars[s >> 4]; 249 textStream << hexchars[s >> 4];
242 textStream << hexchars[s & 0x0f]; 250 textStream << hexchars[s & 0x0f];
243 } 251 }
244 252
245 textStream << "</data>" << endl; 253 textStream << "</data>" << endl;
246 textStream << " </image>" << endl; 254 textStream << " </image>" << endl;
247 } 255 }
248 256
249 textStream << " </images>" << endl; 257 textStream << " </images>" << endl;
250 textStream << "</drawpad>"; 258 textStream << "</drawpad>";
251} 259}
252 260
253void DrawPadCanvas::importPage(const QString& fileName) 261void DrawPadCanvas::importPage(const QString& fileName)
254{ 262{
255 QPixmap* importedPixmap = new QPixmap(); 263 QPixmap* importedPixmap = new QPixmap();
256 264
257 importedPixmap->load(fileName); 265 importedPixmap->load(fileName);
258 m_pages.insert(m_pages.at() + 1, importedPixmap); 266 m_pages.insert(m_pages.at() + 1, importedPixmap);
259 267
260 m_pageBackups.clear(); 268 m_pageBackups.clear();
261 m_pageBackups.append(new QPixmap(*(m_pages.current()))); 269 m_pageBackups.append(new QPixmap(*(m_pages.current())));
262 270
263 resizeContents(m_pages.current()->width(), m_pages.current()->height()); 271 resizeContents(m_pages.current()->width(), m_pages.current()->height());
264 viewport()->update(); 272 viewport()->update();
265 273
266 emit pagesChanged(); 274 emit pagesChanged();
267 emit pageBackupsChanged(); 275 emit pageBackupsChanged();
268} 276}
269 277
270void DrawPadCanvas::exportPage(uint fromPage, uint toPage, const QString& name,const QString& format) 278void DrawPadCanvas::exportPage(uint fromPage, uint toPage, const QString& name,const QString& format)
271{ 279{
272 if (fromPage == toPage) { 280 if (fromPage == toPage) {
273 DocLnk docLnk; 281 DocLnk docLnk;
274 MimeType mimeType(format); 282 MimeType mimeType(format);
275 283
276 docLnk.setName(name); 284 docLnk.setName(name);
277 docLnk.setType(mimeType.id()); 285 docLnk.setType(mimeType.id());
278 286
279 FileManager fileManager; 287 FileManager fileManager;
280 QIODevice* ioDevice = fileManager.saveFile(docLnk); 288 QIODevice* ioDevice = fileManager.saveFile(docLnk);
281 QImageIO imageIO(ioDevice, format); 289 QImageIO imageIO(ioDevice, format);
282 290
283 QImage image = m_pages.current()->convertToImage(); 291 QImage image = m_pages.current()->convertToImage();
284 imageIO.setImage(image); 292 imageIO.setImage(image);
285 imageIO.write(); 293 imageIO.write();
286 delete ioDevice; 294 delete ioDevice;
287 } else { 295 } else {
288 for (uint i = fromPage; i <= toPage; i++) { 296 for (uint i = fromPage; i <= toPage; i++) {
289 DocLnk docLnk; 297 DocLnk docLnk;
290 MimeType mimeType(format); 298 MimeType mimeType(format);
291 299
292 docLnk.setName(name + QString::number(i)); 300 docLnk.setName(name + QString::number(i));
293 docLnk.setType(mimeType.id()); 301 docLnk.setType(mimeType.id());
294 302
295 FileManager fileManager; 303 FileManager fileManager;
296 QIODevice* ioDevice = fileManager.saveFile(docLnk); 304 QIODevice* ioDevice = fileManager.saveFile(docLnk);
297 QImageIO imageIO(ioDevice, format); 305 QImageIO imageIO(ioDevice, format);
298 306
299 QImage image = m_pages.at(i - 1)->convertToImage(); 307 QImage image = m_pages.at(i - 1)->convertToImage();
300 imageIO.setImage(image); 308 imageIO.setImage(image);
301 imageIO.write(); 309 imageIO.write();
302 delete ioDevice; 310 delete ioDevice;
303 } 311 }
304 } 312 }
305} 313}
306 314
307QPixmap* DrawPadCanvas::currentPage() 315QPixmap* DrawPadCanvas::currentPage()
308{ 316{
309 return m_pages.current(); 317 return m_pages.current();
310} 318}
311 319
312uint DrawPadCanvas::pagePosition() 320uint DrawPadCanvas::pagePosition()
313{ 321{
314 return (m_pages.at() + 1); 322 return (m_pages.at() + 1);
315} 323}
316 324
317uint DrawPadCanvas::pageCount() 325uint DrawPadCanvas::pageCount()
318{ 326{
319 return m_pages.count(); 327 return m_pages.count();
320} 328}
321 329
322void DrawPadCanvas::deleteAll() 330void DrawPadCanvas::deleteAll()
323{ 331{
324 QMessageBox messageBox(tr("Delete All"), tr("Do you want to delete\nall the pages?"), 332 QMessageBox messageBox(tr("Delete All"), tr("Do you want to delete\nall the pages?"),
325 QMessageBox::Information, QMessageBox::Yes, 333 QMessageBox::Information, QMessageBox::Yes,
326 QMessageBox::No | QMessageBox::Escape | QMessageBox::Default, 334 QMessageBox::No | QMessageBox::Escape | QMessageBox::Default,
327 QMessageBox::NoButton, this); 335 QMessageBox::NoButton, this);
328 336
329 if (messageBox.exec() == QMessageBox::Yes) { 337 if (messageBox.exec() == QMessageBox::Yes) {
330 m_pages.clear(); 338 m_pages.clear();
331 339
332 m_pages.append(new QPixmap(contentsRect().size())); 340 m_pages.append(new QPixmap(contentsRect().size()));
333 m_pages.current()->fill(Qt::white); 341 m_pages.current()->fill(Qt::white);
334 342
335 m_pageBackups.clear(); 343 m_pageBackups.clear();
336 m_pageBackups.append(new QPixmap(*(m_pages.current()))); 344 m_pageBackups.append(new QPixmap(*(m_pages.current())));
337 345
338 resizeContents(m_pages.current()->width(), m_pages.current()->height()); 346 resizeContents(m_pages.current()->width(), m_pages.current()->height());
339 viewport()->update(); 347 viewport()->update();
340 348
341 emit pagesChanged(); 349 emit pagesChanged();
342 emit pageBackupsChanged(); 350 emit pageBackupsChanged();
343 } 351 }
344} 352}
345 353
346void DrawPadCanvas::newPage() 354void DrawPadCanvas::newPage()
347{ 355{
348 QRect rect = contentsRect(); 356 QRect rect = contentsRect();
349 357
350 NewPageDialog newPageDialog(rect.width(), rect.height(), m_pDrawPad->pen().color(), 358 NewPageDialog newPageDialog(rect.width(), rect.height(), m_pDrawPad->pen().color(),
351 m_pDrawPad->brush().color(), this); 359 m_pDrawPad->brush().color(), this);
352 360
353 if (newPageDialog.exec() == QDialog::Accepted) { 361 if (newPageDialog.exec() == QDialog::Accepted) {
354 m_pages.insert(m_pages.at() + 1, new QPixmap(newPageDialog.selectedWidth(), 362 m_pages.insert(m_pages.at() + 1, new QPixmap(newPageDialog.selectedWidth(),
355 newPageDialog.selectedHeight())); 363 newPageDialog.selectedHeight()));
356 m_pages.current()->fill(newPageDialog.selectedColor()); 364 m_pages.current()->fill(newPageDialog.selectedColor());
357 365
358 m_pageBackups.clear(); 366 m_pageBackups.clear();
359 m_pageBackups.append(new QPixmap(*(m_pages.current()))); 367 m_pageBackups.append(new QPixmap(*(m_pages.current())));
360 368
361 resizeContents(m_pages.current()->width(), m_pages.current()->height()); 369 resizeContents(m_pages.current()->width(), m_pages.current()->height());
362 viewport()->update(); 370 viewport()->update();
363 371
364 emit pagesChanged(); 372 emit pagesChanged();
365 emit pageBackupsChanged(); 373 emit pageBackupsChanged();
366 } 374 }
367} 375}
368 376