-rw-r--r-- | noncore/graphics/drawpad/drawpadcanvas.cpp | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/noncore/graphics/drawpad/drawpadcanvas.cpp b/noncore/graphics/drawpad/drawpadcanvas.cpp index 5d0c6e4..5b1aa7e 100644 --- a/noncore/graphics/drawpad/drawpadcanvas.cpp +++ b/noncore/graphics/drawpad/drawpadcanvas.cpp | |||
@@ -1,452 +1,475 @@ | |||
1 | /*************************************************************************** | 1 | /*************************************************************************** |
2 | * * | 2 | * * |
3 | * DrawPad - a drawing program for Opie Environment * | 3 | * DrawPad - a drawing program for Opie Environment * |
4 | * * | 4 | * * |
5 | * (C) 2002 by S. Prud'homme <prudhomme@laposte.net> * | 5 | * (C) 2002 by S. Prud'homme <prudhomme@laposte.net> * |
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 "page.h" | 17 | #include "page.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 <qtextcodec.h> | 26 | #include <qtextcodec.h> |
27 | #include <qxml.h> | 27 | #include <qxml.h> |
28 | 28 | ||
29 | class DrawPadCanvasXmlHandler: public QXmlDefaultHandler | 29 | class DrawPadCanvasXmlHandler: public QXmlDefaultHandler |
30 | { | 30 | { |
31 | public: | 31 | public: |
32 | DrawPadCanvasXmlHandler(); | 32 | DrawPadCanvasXmlHandler(); |
33 | ~DrawPadCanvasXmlHandler(); | 33 | ~DrawPadCanvasXmlHandler(); |
34 | 34 | ||
35 | QList<Page> pages(); | 35 | QList<Page> pages(); |
36 | 36 | ||
37 | bool startElement(const QString& namespaceURI, const QString& localName, | 37 | bool startElement(const QString& namespaceURI, const QString& localName, |
38 | const QString& qName, const QXmlAttributes& atts); | 38 | const QString& qName, const QXmlAttributes& atts); |
39 | bool endElement(const QString& namespaceURI, const QString& localName, | 39 | bool endElement(const QString& namespaceURI, const QString& localName, |
40 | const QString& qName); | 40 | const QString& qName); |
41 | bool characters(const QString& ch); | 41 | bool characters(const QString& ch); |
42 | 42 | ||
43 | private: | 43 | private: |
44 | enum State { | 44 | enum State { |
45 | Unknown, | 45 | Unknown, |
46 | InTitle, | 46 | InTitle, |
47 | InDate, | 47 | InDate, |
48 | InData | 48 | InData |
49 | }; | 49 | }; |
50 | 50 | ||
51 | State m_state; | 51 | State m_state; |
52 | QString m_title; | 52 | QString m_title; |
53 | QDateTime m_date; | 53 | QDateTime m_date; |
54 | ulong m_dataLenght; | 54 | ulong m_dataLenght; |
55 | QString m_dataFormat; | 55 | QString m_dataFormat; |
56 | QList<Page> m_pages; | 56 | QList<Page> m_pages; |
57 | }; | 57 | }; |
58 | 58 | ||
59 | DrawPadCanvasXmlHandler::DrawPadCanvasXmlHandler() | 59 | DrawPadCanvasXmlHandler::DrawPadCanvasXmlHandler() |
60 | { | 60 | { |
61 | m_state = Unknown; | 61 | m_state = Unknown; |
62 | } | 62 | } |
63 | 63 | ||
64 | DrawPadCanvasXmlHandler::~DrawPadCanvasXmlHandler() | 64 | DrawPadCanvasXmlHandler::~DrawPadCanvasXmlHandler() |
65 | { | 65 | { |
66 | } | 66 | } |
67 | 67 | ||
68 | QList<Page> DrawPadCanvasXmlHandler::pages() | 68 | QList<Page> DrawPadCanvasXmlHandler::pages() |
69 | { | 69 | { |
70 | return m_pages; | 70 | return m_pages; |
71 | } | 71 | } |
72 | 72 | ||
73 | bool DrawPadCanvasXmlHandler::startElement(const QString& namespaceURI, const QString& localName, | 73 | bool DrawPadCanvasXmlHandler::startElement(const QString& namespaceURI, const QString& localName, |
74 | const QString& qName, const QXmlAttributes& atts) | 74 | const QString& qName, const QXmlAttributes& atts) |
75 | { | 75 | { |
76 | Q_CONST_UNUSED(namespaceURI) | 76 | Q_CONST_UNUSED(namespaceURI) |
77 | Q_CONST_UNUSED(localName) | 77 | Q_CONST_UNUSED(localName) |
78 | 78 | ||
79 | if (qName == "image") { | 79 | if (qName == "image") { |
80 | m_title = QString(); | 80 | m_title = QString(); |
81 | m_date = QDateTime::currentDateTime(); | 81 | m_date = QDateTime::currentDateTime(); |
82 | } else if (qName == "title") { | 82 | } else if (qName == "title") { |
83 | m_state = InTitle; | 83 | m_state = InTitle; |
84 | } else if (qName == "date") { | 84 | } else if (qName == "date") { |
85 | m_state = InDate; | 85 | m_state = InDate; |
86 | } else if (qName == "data") { | 86 | } else if (qName == "data") { |
87 | m_state = InData; | 87 | m_state = InData; |
88 | m_dataLenght = atts.value("length").toULong(); | 88 | m_dataLenght = atts.value("length").toULong(); |
89 | m_dataFormat = atts.value("format"); | 89 | m_dataFormat = atts.value("format"); |
90 | } | 90 | } |
91 | 91 | ||
92 | return true; | 92 | return true; |
93 | } | 93 | } |
94 | 94 | ||
95 | bool DrawPadCanvasXmlHandler::endElement(const QString& namespaceURI, const QString& localName, | 95 | bool DrawPadCanvasXmlHandler::endElement(const QString& namespaceURI, const QString& localName, |
96 | const QString& qName) | 96 | const QString& qName) |
97 | { | 97 | { |
98 | Q_CONST_UNUSED(namespaceURI) | 98 | Q_CONST_UNUSED(namespaceURI) |
99 | Q_CONST_UNUSED(localName) | 99 | Q_CONST_UNUSED(localName) |
100 | 100 | ||
101 | if (qName == "title") { | 101 | if (qName == "title") { |
102 | m_state = Unknown; | 102 | m_state = Unknown; |
103 | } else if (qName == "date") { | 103 | } else if (qName == "date") { |
104 | m_state = Unknown; | 104 | m_state = Unknown; |
105 | } else if (qName == "data") { | 105 | } else if (qName == "data") { |
106 | m_state = Unknown; | 106 | m_state = Unknown; |
107 | } | 107 | } |
108 | 108 | ||
109 | return true; | 109 | return true; |
110 | } | 110 | } |
111 | 111 | ||
112 | bool DrawPadCanvasXmlHandler::characters(const QString& ch) | 112 | bool DrawPadCanvasXmlHandler::characters(const QString& ch) |
113 | { | 113 | { |
114 | if (m_state == InTitle) { | 114 | if (m_state == InTitle) { |
115 | m_title = ch; | 115 | m_title = ch; |
116 | } else if (m_state == InDate) { | 116 | } else if (m_state == InDate) { |
117 | m_date = QDateTime(QDate(1970, 1, 1)).addSecs(ch.toInt()); | 117 | m_date = QDateTime(QDate(1970, 1, 1)).addSecs(ch.toInt()); |
118 | } else if (m_state == InData) { | 118 | } else if (m_state == InData) { |
119 | QByteArray byteArray(ch.length() / 2); | 119 | QByteArray byteArray(ch.length() / 2); |
120 | 120 | ||
121 | for (int i = 0; i < (int)ch.length() / 2; i++) { | 121 | for (int i = 0; i < (int)ch.length() / 2; i++) { |
122 | char h = ch[2 * i].latin1(); | 122 | char h = ch[2 * i].latin1(); |
123 | char l = ch[2 * i + 1].latin1(); | 123 | char l = ch[2 * i + 1].latin1(); |
124 | uchar r = 0; | 124 | uchar r = 0; |
125 | 125 | ||
126 | if (h <= '9') { | 126 | if (h <= '9') { |
127 | r += h - '0'; | 127 | r += h - '0'; |
128 | } else { | 128 | } else { |
129 | r += h - 'a' + 10; | 129 | r += h - 'a' + 10; |
130 | } | 130 | } |
131 | 131 | ||
132 | r = r << 4; | 132 | r = r << 4; |
133 | 133 | ||
134 | if (l <= '9') { | 134 | if (l <= '9') { |
135 | r += l - '0'; | 135 | r += l - '0'; |
136 | } else { | 136 | } else { |
137 | r += l - 'a' + 10; | 137 | r += l - 'a' + 10; |
138 | } | 138 | } |
139 | 139 | ||
140 | byteArray[i] = r; | 140 | byteArray[i] = r; |
141 | } | 141 | } |
142 | 142 | ||
143 | QImage image; | 143 | QImage image; |
144 | image.loadFromData((const uchar*)byteArray.data(), m_dataLenght, m_dataFormat); | 144 | image.loadFromData((const uchar*)byteArray.data(), m_dataLenght, m_dataFormat); |
145 | 145 | ||
146 | Page* page = new Page(m_title, image.width(), image.height()); | 146 | Page* page = new Page(m_title, image.width(), image.height()); |
147 | page->setLastModified(m_date); | 147 | page->setLastModified(m_date); |
148 | page->pixmap()->convertFromImage(image); | 148 | page->pixmap()->convertFromImage(image); |
149 | m_pages.append(page); | 149 | m_pages.append(page); |
150 | } | 150 | } |
151 | 151 | ||
152 | return true; | 152 | return true; |
153 | } | 153 | } |
154 | 154 | ||
155 | DrawPadCanvas::DrawPadCanvas(DrawPad* drawPad, QWidget* parent, const char* name) | 155 | DrawPadCanvas::DrawPadCanvas(DrawPad* drawPad, QWidget* parent, const char* name) |
156 | : QScrollView(parent, name) | 156 | : QScrollView(parent, name) |
157 | { | 157 | { |
158 | m_pDrawPad = drawPad; | 158 | m_pDrawPad = drawPad; |
159 | m_pages.setAutoDelete(true); | 159 | m_pages.setAutoDelete(true); |
160 | 160 | ||
161 | viewport()->setBackgroundMode(QWidget::NoBackground); | 161 | viewport()->setBackgroundMode(QWidget::NoBackground); |
162 | } | 162 | } |
163 | 163 | ||
164 | DrawPadCanvas::~DrawPadCanvas() | 164 | DrawPadCanvas::~DrawPadCanvas() |
165 | { | 165 | { |
166 | } | 166 | } |
167 | 167 | ||
168 | void DrawPadCanvas::load(QIODevice* ioDevice) | 168 | void DrawPadCanvas::load(QIODevice* ioDevice) |
169 | { | 169 | { |
170 | QTextStream textStream(ioDevice); | 170 | QTextStream textStream(ioDevice); |
171 | textStream.setCodec(QTextCodec::codecForName("UTF-8")); | 171 | textStream.setCodec(QTextCodec::codecForName("UTF-8")); |
172 | 172 | ||
173 | QXmlInputSource xmlInputSource(textStream); | 173 | QXmlInputSource xmlInputSource(textStream); |
174 | QXmlSimpleReader xmlSimpleReader; | 174 | QXmlSimpleReader xmlSimpleReader; |
175 | DrawPadCanvasXmlHandler drawPadCanvasXmlHandler; | 175 | DrawPadCanvasXmlHandler drawPadCanvasXmlHandler; |
176 | 176 | ||
177 | xmlSimpleReader.setContentHandler(&drawPadCanvasXmlHandler); | 177 | xmlSimpleReader.setContentHandler(&drawPadCanvasXmlHandler); |
178 | xmlSimpleReader.parse(xmlInputSource); | 178 | xmlSimpleReader.parse(xmlInputSource); |
179 | 179 | ||
180 | m_pages = drawPadCanvasXmlHandler.pages(); | 180 | /* |
181 | * we could have loaded something from setDocument already | ||
182 | * due the delayed loading we need to make sure we do | ||
183 | * not lose pages | ||
184 | */ | ||
185 | if ( !m_pages.isEmpty() ) { | ||
186 | QList<Page> pages = drawPadCanvasXmlHandler.pages(); | ||
187 | QListIterator<Page> it( pages ); | ||
188 | Page *p; | ||
189 | while ( ( p = it.current() ) ) { | ||
190 | ++it; | ||
191 | m_pages.append( p ); | ||
192 | } | ||
193 | }else | ||
194 | m_pages = drawPadCanvasXmlHandler.pages(); | ||
195 | |||
196 | |||
181 | 197 | ||
182 | if (m_pages.isEmpty()) { | 198 | if (m_pages.isEmpty()) { |
183 | m_pages.append(new Page("", | 199 | m_pages.append(new Page("", |
184 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), | 200 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), |
185 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); | 201 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); |
186 | m_pages.current()->pixmap()->fill(Qt::white); | 202 | m_pages.current()->pixmap()->fill(Qt::white); |
187 | } | 203 | } |
188 | 204 | ||
189 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 205 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
190 | viewport()->update(); | 206 | viewport()->update(); |
191 | 207 | ||
192 | emit pagesChanged(); | 208 | emit pagesChanged(); |
193 | } | 209 | } |
194 | 210 | ||
195 | void DrawPadCanvas::initialPage() | 211 | void DrawPadCanvas::initialPage() |
196 | { | 212 | { |
213 | /* | ||
214 | * by setDocument we've set a page already so | ||
215 | * don't add an empty one. This comes due the delayed initialisation | ||
216 | */ | ||
217 | if (!m_pages.isEmpty() ) | ||
218 | return; | ||
219 | |||
197 | m_pages.append(new Page("", | 220 | m_pages.append(new Page("", |
198 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), | 221 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), |
199 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); | 222 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); |
200 | //236, 232)); no more fixed sizes | 223 | //236, 232)); no more fixed sizes |
201 | 224 | ||
202 | m_pages.current()->pixmap()->fill(Qt::white); | 225 | m_pages.current()->pixmap()->fill(Qt::white); |
203 | 226 | ||
204 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 227 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
205 | viewport()->update(); | 228 | viewport()->update(); |
206 | 229 | ||
207 | emit pagesChanged(); | 230 | emit pagesChanged(); |
208 | } | 231 | } |
209 | 232 | ||
210 | void DrawPadCanvas::save(QIODevice* ioDevice) | 233 | void DrawPadCanvas::save(QIODevice* ioDevice) |
211 | { | 234 | { |
212 | QTextStream textStream(ioDevice); | 235 | QTextStream textStream(ioDevice); |
213 | textStream.setCodec(QTextCodec::codecForName("UTF-8")); | 236 | textStream.setCodec(QTextCodec::codecForName("UTF-8")); |
214 | 237 | ||
215 | textStream << "<drawpad>" << endl; | 238 | textStream << "<drawpad>" << endl; |
216 | textStream << " <images>" << endl; | 239 | textStream << " <images>" << endl; |
217 | 240 | ||
218 | QListIterator<Page> bufferIterator(m_pages); | 241 | QListIterator<Page> bufferIterator(m_pages); |
219 | 242 | ||
220 | for (bufferIterator.toFirst(); bufferIterator.current() != 0; ++bufferIterator) { | 243 | for (bufferIterator.toFirst(); bufferIterator.current() != 0; ++bufferIterator) { |
221 | textStream << " <image>" << endl; | 244 | textStream << " <image>" << endl; |
222 | textStream << " <title>" << bufferIterator.current()->title() << "</title>" << endl; | 245 | textStream << " <title>" << bufferIterator.current()->title() << "</title>" << endl; |
223 | 246 | ||
224 | int intDate = QDateTime(QDate(1970, 1, 1)).secsTo(bufferIterator.current()->lastModified()); | 247 | int intDate = QDateTime(QDate(1970, 1, 1)).secsTo(bufferIterator.current()->lastModified()); |
225 | textStream << " <date>" << intDate << "</date>" << endl; | 248 | textStream << " <date>" << intDate << "</date>" << endl; |
226 | 249 | ||
227 | QImage image = bufferIterator.current()->pixmap()->convertToImage(); | 250 | QImage image = bufferIterator.current()->pixmap()->convertToImage(); |
228 | QByteArray byteArray; | 251 | QByteArray byteArray; |
229 | QBuffer buffer(byteArray); | 252 | QBuffer buffer(byteArray); |
230 | QImageIO imageIO(&buffer, "PNG"); | 253 | QImageIO imageIO(&buffer, "PNG"); |
231 | 254 | ||
232 | buffer.open(IO_WriteOnly); | 255 | buffer.open(IO_WriteOnly); |
233 | imageIO.setImage(image); | 256 | imageIO.setImage(image); |
234 | imageIO.write(); | 257 | imageIO.write(); |
235 | buffer.close(); | 258 | buffer.close(); |
236 | 259 | ||
237 | textStream << " <data length=\"" << byteArray.size() << "\" format=\"PNG\">"; | 260 | textStream << " <data length=\"" << byteArray.size() << "\" format=\"PNG\">"; |
238 | 261 | ||
239 | static const char hexchars[] = "0123456789abcdef"; | 262 | static const char hexchars[] = "0123456789abcdef"; |
240 | 263 | ||
241 | for (int i = 0; i < (int)byteArray.size(); i++ ) { | 264 | for (int i = 0; i < (int)byteArray.size(); i++ ) { |
242 | uchar s = (uchar)byteArray[i]; | 265 | uchar s = (uchar)byteArray[i]; |
243 | textStream << hexchars[s >> 4]; | 266 | textStream << hexchars[s >> 4]; |
244 | textStream << hexchars[s & 0x0f]; | 267 | textStream << hexchars[s & 0x0f]; |
245 | } | 268 | } |
246 | 269 | ||
247 | textStream << "</data>" << endl; | 270 | textStream << "</data>" << endl; |
248 | textStream << " </image>" << endl; | 271 | textStream << " </image>" << endl; |
249 | } | 272 | } |
250 | 273 | ||
251 | textStream << " </images>" << endl; | 274 | textStream << " </images>" << endl; |
252 | textStream << "</drawpad>"; | 275 | textStream << "</drawpad>"; |
253 | } | 276 | } |
254 | 277 | ||
255 | void DrawPadCanvas::importPage(const QString& fileName) | 278 | void DrawPadCanvas::importPage(const QString& fileName) |
256 | { | 279 | { |
257 | Page* importedPage = new Page(); | 280 | Page* importedPage = new Page(); |
258 | 281 | ||
259 | importedPage->pixmap()->load(fileName); | 282 | importedPage->pixmap()->load(fileName); |
260 | m_pages.insert(m_pages.at() + 1, importedPage); | 283 | m_pages.insert(m_pages.at() + 1, importedPage); |
261 | 284 | ||
262 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 285 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
263 | viewport()->update(); | 286 | viewport()->update(); |
264 | 287 | ||
265 | emit pagesChanged(); | 288 | emit pagesChanged(); |
266 | } | 289 | } |
267 | 290 | ||
268 | void DrawPadCanvas::importPixmap( const QPixmap& pix ) { | 291 | void DrawPadCanvas::importPixmap( const QPixmap& pix ) { |
269 | Page* importedPage = new Page(); | 292 | Page* importedPage = new Page(); |
270 | 293 | ||
271 | (*importedPage->pixmap()) = pix; | 294 | (*importedPage->pixmap()) = pix; |
272 | m_pages.insert(m_pages.at()+1, importedPage ); | 295 | m_pages.insert(m_pages.at()+1, importedPage ); |
273 | resizeContents(m_pages.current()->pixmap()->width(), | 296 | resizeContents(m_pages.current()->pixmap()->width(), |
274 | m_pages.current()->pixmap()->height() ); | 297 | m_pages.current()->pixmap()->height() ); |
275 | viewport()->update(); | 298 | viewport()->update(); |
276 | 299 | ||
277 | emit pagesChanged(); | 300 | emit pagesChanged(); |
278 | } | 301 | } |
279 | 302 | ||
280 | void DrawPadCanvas::exportPage(uint fromPage, uint toPage, const QString& name,const QString& format) | 303 | void DrawPadCanvas::exportPage(uint fromPage, uint toPage, const QString& name,const QString& format) |
281 | { | 304 | { |
282 | if (fromPage == toPage) { | 305 | if (fromPage == toPage) { |
283 | DocLnk docLnk; | 306 | DocLnk docLnk; |
284 | MimeType mimeType(format); | 307 | MimeType mimeType(format); |
285 | 308 | ||
286 | docLnk.setName(name); | 309 | docLnk.setName(name); |
287 | docLnk.setType(mimeType.id()); | 310 | docLnk.setType(mimeType.id()); |
288 | 311 | ||
289 | FileManager fileManager; | 312 | FileManager fileManager; |
290 | QIODevice* ioDevice = fileManager.saveFile(docLnk); | 313 | QIODevice* ioDevice = fileManager.saveFile(docLnk); |
291 | QImageIO imageIO(ioDevice, format); | 314 | QImageIO imageIO(ioDevice, format); |
292 | 315 | ||
293 | QImage image = m_pages.current()->pixmap()->convertToImage(); | 316 | QImage image = m_pages.current()->pixmap()->convertToImage(); |
294 | imageIO.setImage(image); | 317 | imageIO.setImage(image); |
295 | imageIO.write(); | 318 | imageIO.write(); |
296 | delete ioDevice; | 319 | delete ioDevice; |
297 | } else { | 320 | } else { |
298 | for (uint i = fromPage; i <= toPage; i++) { | 321 | for (uint i = fromPage; i <= toPage; i++) { |
299 | DocLnk docLnk; | 322 | DocLnk docLnk; |
300 | MimeType mimeType(format); | 323 | MimeType mimeType(format); |
301 | 324 | ||
302 | docLnk.setName(name + QString::number(i)); | 325 | docLnk.setName(name + QString::number(i)); |
303 | docLnk.setType(mimeType.id()); | 326 | docLnk.setType(mimeType.id()); |
304 | 327 | ||
305 | FileManager fileManager; | 328 | FileManager fileManager; |
306 | QIODevice* ioDevice = fileManager.saveFile(docLnk); | 329 | QIODevice* ioDevice = fileManager.saveFile(docLnk); |
307 | QImageIO imageIO(ioDevice, format); | 330 | QImageIO imageIO(ioDevice, format); |
308 | 331 | ||
309 | QImage image = m_pages.at(i - 1)->pixmap()->convertToImage(); | 332 | QImage image = m_pages.at(i - 1)->pixmap()->convertToImage(); |
310 | imageIO.setImage(image); | 333 | imageIO.setImage(image); |
311 | imageIO.write(); | 334 | imageIO.write(); |
312 | delete ioDevice; | 335 | delete ioDevice; |
313 | } | 336 | } |
314 | } | 337 | } |
315 | } | 338 | } |
316 | 339 | ||
317 | Page* DrawPadCanvas::currentPage() | 340 | Page* DrawPadCanvas::currentPage() |
318 | { | 341 | { |
319 | return m_pages.current(); | 342 | return m_pages.current(); |
320 | } | 343 | } |
321 | 344 | ||
322 | QList<Page> DrawPadCanvas::pages() | 345 | QList<Page> DrawPadCanvas::pages() |
323 | { | 346 | { |
324 | return m_pages; | 347 | return m_pages; |
325 | } | 348 | } |
326 | 349 | ||
327 | uint DrawPadCanvas::pagePosition() | 350 | uint DrawPadCanvas::pagePosition() |
328 | { | 351 | { |
329 | return (m_pages.at() + 1); | 352 | return (m_pages.at() + 1); |
330 | } | 353 | } |
331 | 354 | ||
332 | uint DrawPadCanvas::pageCount() | 355 | uint DrawPadCanvas::pageCount() |
333 | { | 356 | { |
334 | return m_pages.count(); | 357 | return m_pages.count(); |
335 | } | 358 | } |
336 | 359 | ||
337 | void DrawPadCanvas::selectPage(Page* page) | 360 | void DrawPadCanvas::selectPage(Page* page) |
338 | { | 361 | { |
339 | m_pages.findRef(page); | 362 | m_pages.findRef(page); |
340 | 363 | ||
341 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 364 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
342 | viewport()->update(); | 365 | viewport()->update(); |
343 | 366 | ||
344 | emit pagesChanged(); | 367 | emit pagesChanged(); |
345 | } | 368 | } |
346 | 369 | ||
347 | void DrawPadCanvas::backupPage() | 370 | void DrawPadCanvas::backupPage() |
348 | { | 371 | { |
349 | m_pages.current()->backup(); | 372 | m_pages.current()->backup(); |
350 | 373 | ||
351 | emit pagesChanged(); | 374 | emit pagesChanged(); |
352 | } | 375 | } |
353 | 376 | ||
354 | void DrawPadCanvas::selectPage(uint pagePosition) | 377 | void DrawPadCanvas::selectPage(uint pagePosition) |
355 | { | 378 | { |
356 | m_pages.at(pagePosition - 1); | 379 | m_pages.at(pagePosition - 1); |
357 | 380 | ||
358 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 381 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
359 | viewport()->update(); | 382 | viewport()->update(); |
360 | 383 | ||
361 | emit pagesChanged(); | 384 | emit pagesChanged(); |
362 | } | 385 | } |
363 | 386 | ||
364 | void DrawPadCanvas::deleteAll() | 387 | void DrawPadCanvas::deleteAll() |
365 | { | 388 | { |
366 | m_pages.clear(); | 389 | m_pages.clear(); |
367 | 390 | ||
368 | m_pages.append(new Page("", | 391 | m_pages.append(new Page("", |
369 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), | 392 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), |
370 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); | 393 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); |
371 | 394 | ||
372 | m_pages.current()->pixmap()->fill(Qt::white); | 395 | m_pages.current()->pixmap()->fill(Qt::white); |
373 | 396 | ||
374 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 397 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
375 | viewport()->update(); | 398 | viewport()->update(); |
376 | 399 | ||
377 | emit pagesChanged(); | 400 | emit pagesChanged(); |
378 | } | 401 | } |
379 | 402 | ||
380 | void DrawPadCanvas::newPage(QString title, uint width, uint height, const QColor& color) | 403 | void DrawPadCanvas::newPage(QString title, uint width, uint height, const QColor& color) |
381 | { | 404 | { |
382 | m_pages.insert(m_pages.at() + 1, new Page(title, width, height)); | 405 | m_pages.insert(m_pages.at() + 1, new Page(title, width, height)); |
383 | m_pages.current()->pixmap()->fill(color); | 406 | m_pages.current()->pixmap()->fill(color); |
384 | 407 | ||
385 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 408 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
386 | viewport()->update(); | 409 | viewport()->update(); |
387 | 410 | ||
388 | emit pagesChanged(); | 411 | emit pagesChanged(); |
389 | } | 412 | } |
390 | 413 | ||
391 | void DrawPadCanvas::clearPage() | 414 | void DrawPadCanvas::clearPage() |
392 | { | 415 | { |
393 | m_pages.current()->pixmap()->fill(Qt::white); | 416 | m_pages.current()->pixmap()->fill(Qt::white); |
394 | 417 | ||
395 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 418 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
396 | viewport()->update(); | 419 | viewport()->update(); |
397 | } | 420 | } |
398 | 421 | ||
399 | void DrawPadCanvas::deletePage() | 422 | void DrawPadCanvas::deletePage() |
400 | { | 423 | { |
401 | m_pages.remove(m_pages.current()); | 424 | m_pages.remove(m_pages.current()); |
402 | 425 | ||
403 | if (m_pages.isEmpty()) { | 426 | if (m_pages.isEmpty()) { |
404 | m_pages.append(new Page("", | 427 | m_pages.append(new Page("", |
405 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), | 428 | clipper()->width()+(verticalScrollBar()->isVisible()?verticalScrollBar()->width():0), |
406 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); | 429 | clipper()->height()+(horizontalScrollBar()->isVisible()?horizontalScrollBar()->height():0))); |
407 | 430 | ||
408 | m_pages.current()->pixmap()->fill(Qt::white); | 431 | m_pages.current()->pixmap()->fill(Qt::white); |
409 | } | 432 | } |
410 | 433 | ||
411 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); | 434 | resizeContents(m_pages.current()->pixmap()->width(), m_pages.current()->pixmap()->height()); |
412 | viewport()->update(); | 435 | viewport()->update(); |
413 | 436 | ||
414 | emit pagesChanged(); | 437 | emit pagesChanged(); |
415 | } | 438 | } |
416 | 439 | ||
417 | void DrawPadCanvas::movePageUp() | 440 | void DrawPadCanvas::movePageUp() |
418 | { | 441 | { |
419 | int index = m_pages.at(); | 442 | int index = m_pages.at(); |
420 | Page* page = m_pages.take(); | 443 | Page* page = m_pages.take(); |
421 | m_pages.insert(index - 1, page); | 444 | m_pages.insert(index - 1, page); |
422 | 445 | ||
423 | emit pagesChanged(); | 446 | emit pagesChanged(); |
424 | } | 447 | } |
425 | 448 | ||
426 | void DrawPadCanvas::movePageDown() | 449 | void DrawPadCanvas::movePageDown() |
427 | { | 450 | { |
428 | int index = m_pages.at(); | 451 | int index = m_pages.at(); |
429 | Page* page = m_pages.take(); | 452 | Page* page = m_pages.take(); |
430 | m_pages.insert(index + 1, page); | 453 | m_pages.insert(index + 1, page); |
431 | 454 | ||
432 | emit pagesChanged(); | 455 | emit pagesChanged(); |
433 | } | 456 | } |
434 | 457 | ||
435 | bool DrawPadCanvas::goPreviousPageEnabled() | 458 | bool DrawPadCanvas::goPreviousPageEnabled() |
436 | { | 459 | { |
437 | return (m_pages.current() != m_pages.getFirst()); | 460 | return (m_pages.current() != m_pages.getFirst()); |
438 | } | 461 | } |
439 | 462 | ||
440 | bool DrawPadCanvas::goNextPageEnabled() | 463 | bool DrawPadCanvas::goNextPageEnabled() |
441 | { | 464 | { |
442 | return (m_pages.current() != m_pages.getLast()); | 465 | return (m_pages.current() != m_pages.getLast()); |
443 | } | 466 | } |
444 | 467 | ||
445 | void DrawPadCanvas::undo() | 468 | void DrawPadCanvas::undo() |
446 | { | 469 | { |
447 | m_pages.current()->undo(); | 470 | m_pages.current()->undo(); |
448 | 471 | ||
449 | viewport()->update(); | 472 | viewport()->update(); |
450 | 473 | ||
451 | emit pagesChanged(); | 474 | emit pagesChanged(); |
452 | } | 475 | } |