author | zecke <zecke> | 2004-07-14 11:22:14 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-07-14 11:22:14 (UTC) |
commit | ca486a4558dc7aa23e3e5c677f6eedd26ed02746 (patch) (unidiff) | |
tree | 56afc5086ffcd07a5f205017f1940b86e9747628 | |
parent | b09d76574ca6d2ebafca0640ea36c3b785e7f3a3 (diff) | |
download | opie-ca486a4558dc7aa23e3e5c677f6eedd26ed02746.zip opie-ca486a4558dc7aa23e3e5c677f6eedd26ed02746.tar.gz opie-ca486a4558dc7aa23e3e5c677f6eedd26ed02746.tar.bz2 |
Problem: Opie-Drawpad loads its document delayed. This means a setDocument
like from the Screenshot Applet could be executed prior to the final
initialisation of drawpad and the just added page vanishes.
Solve: Take the fact into account and 1st) don't create an empty page
if there is already a page which is the case with the setDocument call
2nd) if we load but already have pages we need to add the new pages
-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 | |||
@@ -52,273 +52,296 @@ private: | |||
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; |