compile
[kdegraphics.git] / kolourpaint / document / kpDocument_Open.cpp
blob4cab9a2dcd97a421c98fe1e5e5b1e9c028449391
2 /*
3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #define DEBUG_KP_DOCUMENT 0
32 #include <kpDocument.h>
33 #include <kpDocumentPrivate.h>
35 #include <math.h>
37 #include <qcolor.h>
38 #include <qbitmap.h>
39 #include <qbrush.h>
40 #include <qfile.h>
41 #include <qimage.h>
42 #include <qlist.h>
43 #include <qpixmap.h>
44 #include <qpainter.h>
45 #include <qrect.h>
46 #include <qsize.h>
47 #include <qmatrix.h>
49 #include <kdebug.h>
50 #include <kglobal.h>
51 #include <kimageio.h>
52 #include <kio/netaccess.h>
53 #include <klocale.h>
54 #include <kmessagebox.h>
55 #include <kmimetype.h> // TODO: isn't this in KIO?
56 #include <ktemporaryfile.h>
58 #include <kpColor.h>
59 #include <kpColorToolBar.h>
60 #include <kpDefs.h>
61 #include <kpDocumentEnvironment.h>
62 #include <kpDocumentSaveOptions.h>
63 #include <kpDocumentMetaInfo.h>
64 #include <kpEffectReduceColors.h>
65 #include <kpPixmapFX.h>
66 #include <kpTool.h>
67 #include <kpToolToolBar.h>
68 #include <kpUrlFormatter.h>
69 #include <kpViewManager.h>
72 QPixmap kpDocument::convertToPixmapAsLosslessAsPossible (
73 const QImage &image,
74 const kpPixmapFX::WarnAboutLossInfo &wali,
76 kpDocumentSaveOptions *saveOptions,
77 kpDocumentMetaInfo *metaInfo)
79 if (image.isNull ())
80 return QPixmap ();
82 #if DEBUG_KP_DOCUMENT
83 kDebug () << "\timage: depth=" << image.depth ()
84 << " (X display=" << QPixmap::defaultDepth () << ")"
85 << " hasAlphaChannel=" << image.hasAlphaChannel ()
86 << endl;
87 #endif
89 if (saveOptions)
91 saveOptions->setColorDepth (image.depth ());
92 saveOptions->setDither (false); // avoid double dithering when saving
95 if (metaInfo)
97 metaInfo->setDotsPerMeterX (image.dotsPerMeterX ());
98 metaInfo->setDotsPerMeterY (image.dotsPerMeterY ());
99 metaInfo->setOffset (image.offset ());
101 QList <QString> keyList = image.textKeys ();
102 for (QList <QString>::const_iterator it = keyList.constBegin ();
103 it != keyList.constEnd ();
104 it++)
106 metaInfo->setText (*it, image.text (*it));
109 #if DEBUG_KP_DOCUMENT
110 metaInfo->printDebug ("\tmetaInfo");
111 #endif
114 #if DEBUG_KP_DOCUMENT && 1
116 if (image.width () <= 16 && image.height () <= 16)
118 kDebug () << "Image dump:";
120 for (int y = 0; y < image.height (); y++)
122 for (int x = 0; x < image.width (); x++)
124 const QRgb rgb = image.pixel (x, y);
125 fprintf (stderr, " %08X", rgb);
127 fprintf (stderr, "\n");
131 #endif
134 QPixmap newPixmap = kpPixmapFX::convertToPixmapAsLosslessAsPossible (image, wali);
137 #if DEBUG_KP_DOCUMENT && 1
139 const QImage image2 = kpPixmapFX::convertToQImage (newPixmap);
140 kDebug () << "(Converted to pixmap) Image dump:";
142 bool differsFromOrgImage = false;
143 unsigned long hash = 0;
144 int numDiff = 0;
145 for (int y = 0; y < image2.height (); y++)
147 for (int x = 0; x < image2.width (); x++)
149 const QRgb rgb = image2.pixel (x, y);
150 hash += ((x % 2) + 1) * rgb;
151 if (rgb != image.pixel (x, y))
153 differsFromOrgImage = true;
154 numDiff++;
156 if (image2.width () <= 16 && image2.height () <= 16)
157 fprintf (stderr, " %08X", rgb);
159 if (image2.width () <= 16 && image2.height () <= 16)
160 fprintf (stderr, "\n");
163 kDebug () << "\tdiffersFromOrgImage="
164 << differsFromOrgImage
165 << " numDiff="
166 << numDiff
167 << " hash=" << hash << endl;
169 #endif
171 return newPixmap;
174 // public static
175 QPixmap kpDocument::getPixmapFromFile (const KUrl &url, bool suppressDoesntExistDialog,
176 QWidget *parent,
177 kpDocumentSaveOptions *saveOptions,
178 kpDocumentMetaInfo *metaInfo)
180 #if DEBUG_KP_DOCUMENT
181 kDebug () << "kpDocument::getPixmapFromFile(" << url << "," << parent << ")";
182 #endif
184 if (saveOptions)
185 *saveOptions = kpDocumentSaveOptions ();
187 if (metaInfo)
188 *metaInfo = kpDocumentMetaInfo ();
191 QString tempFile;
192 if (url.isEmpty () || !KIO::NetAccess::download (url, tempFile, parent))
194 if (!suppressDoesntExistDialog)
196 // TODO: Use "Cannot" instead of "Could not" in all dialogs in KolourPaint.
197 // Or at least choose one consistently.
199 // TODO: Have captions for all dialogs in KolourPaint.
200 KMessageBox::sorry (parent,
201 i18n ("Could not open \"%1\".",
202 kpUrlFormatter::PrettyFilename (url)));
205 return QPixmap ();
209 QImage image;
211 // sync: remember to "KIO::NetAccess::removeTempFile (tempFile)" in all exit paths
213 QString detectedMimeType;
215 KMimeType::Ptr detectedMimeTypePtr = KMimeType::findByUrl (url);
216 if(detectedMimeTypePtr &&
217 detectedMimeTypePtr != KMimeType::defaultMimeTypePtr ())
219 detectedMimeType = detectedMimeTypePtr->name ();
222 if (saveOptions)
223 saveOptions->setMimeType (detectedMimeType);
225 #if DEBUG_KP_DOCUMENT
226 kDebug () << "\ttempFile=" << tempFile;
227 kDebug () << "\tmimetype=" << detectedMimeType;
228 kDebug () << "\tsrc=" << url.path ();
229 #endif
231 if (detectedMimeType.isEmpty ())
233 KMessageBox::sorry (parent,
234 i18n ("Could not open \"%1\" - unknown mimetype.",
235 kpUrlFormatter::PrettyFilename (url)));
236 KIO::NetAccess::removeTempFile (tempFile);
237 return QPixmap ();
241 // TODO: <detectedMimeType> might be different.
242 // Should we feed it into QImage to solve this problem?
244 // If so, should we have used KMimeType::findByContent()
245 // instead? Are some image types not detectable by findByContent()
246 // (e.g. image types that are only detected by extension)?
248 // Currently, opening a PNG with a ".jpg" extension does not
249 // work -- QImage and findByUrl() both think it's a JPG based
250 // on the extension, but findByContent() correctly detects
251 // it as a PNG.
252 image = QImage (tempFile);
253 KIO::NetAccess::removeTempFile (tempFile);
256 if (image.isNull ())
258 KMessageBox::sorry (parent,
259 i18n ("Could not open \"%1\" - unsupported image format.\n"
260 "The file may be corrupt.",
261 kpUrlFormatter::PrettyFilename (url)));
262 return QPixmap ();
265 const QPixmap newPixmap = kpDocument::convertToPixmapAsLosslessAsPossible (image,
266 kpPixmapFX::WarnAboutLossInfo (
267 ki18n ("<qt><p>The image \"%1\""
268 " may have more colors than the current screen mode can support."
269 " In order to display it, some color information may be removed.</p>"
271 "<p><b>If you save this image, any color loss will become"
272 " permanent.</b></p>"
274 "<p>To avoid this issue, increase your screen depth to at"
275 " least %2bpp and then restart KolourPaint.</p>"
277 "<hr/>"
279 "<p>It also"
281 " contains translucency which is not fully"
282 " supported. The translucency data will be"
283 " approximated with a 1-bit transparency mask.</p>"
285 "<p><b>If you save this image, this loss of translucency will"
286 " become permanent.</b></p></qt>")
287 .subs (kpUrlFormatter::PrettyFilename (url)),
288 ki18n ("<qt><p>The image \"%1\""
289 " may have more colors than the current screen mode can support."
290 " In order to display it, some color information may be removed.</p>"
292 "<p><b>If you save this image, any color loss will become"
293 " permanent.</b></p>"
295 "<p>To avoid this issue, increase your screen depth to at"
296 " least %2bpp and then restart KolourPaint.</p></qt>")
297 .subs (kpUrlFormatter::PrettyFilename (url)),
298 i18n ("<qt><p>The image \"%1\""
299 " contains translucency which is not fully"
300 " supported. The translucency data will be"
301 " approximated with a 1-bit transparency mask.</p>"
303 "<p><b>If you save this image, this loss of translucency will"
304 " become permanent.</b></p></qt>",
305 kpUrlFormatter::PrettyFilename (url)),
306 "docOpen",
307 parent),
308 saveOptions,
309 metaInfo);
311 if (newPixmap.isNull ())
313 KMessageBox::sorry (parent,
314 i18n ("Could not open \"%1\" - out of graphics memory.",
315 kpUrlFormatter::PrettyFilename (url)));
316 return QPixmap ();
319 #if DEBUG_KP_DOCUMENT
320 kDebug () << "\tpixmap: depth=" << newPixmap.depth ()
321 << " hasAlphaChannelOrMask=" << newPixmap.hasAlpha ()
322 << " hasAlphaChannel=" << newPixmap.hasAlphaChannel ()
323 << endl;
324 #endif
327 return newPixmap;
330 void kpDocument::openNew (const KUrl &url)
332 #if DEBUG_KP_DOCUMENT
333 kDebug () << "kpDocument::openNew (" << url << ")";
334 #endif
336 m_image->fill (Qt::white);
338 setURL (url, false/*not from url*/);
339 // TODO: Maybe we should guess the mimetype from "url"'s filename
340 // extension.
342 // That way "kolourpaint doesnotexist.bmp" would automatically
343 // select the BMP file format when the save dialog comes up for
344 // the first time.
345 *m_saveOptions = kpDocumentSaveOptions ();
346 *m_metaInfo = kpDocumentMetaInfo ();
347 m_modified = false;
349 emit documentOpened ();
352 bool kpDocument::open (const KUrl &url, bool newDocSameNameIfNotExist)
354 #if DEBUG_KP_DOCUMENT
355 kDebug () << "kpDocument::open (" << url << ")";
356 #endif
358 kpDocumentSaveOptions newSaveOptions;
359 kpDocumentMetaInfo newMetaInfo;
360 QPixmap newPixmap = kpDocument::getPixmapFromFile (url,
361 newDocSameNameIfNotExist/*suppress "doesn't exist" dialog*/,
362 d->environ->dialogParent (),
363 &newSaveOptions,
364 &newMetaInfo);
366 if (!newPixmap.isNull ())
368 delete m_image;
369 m_image = new kpImage (newPixmap);
371 setURL (url, true/*is from url*/);
372 *m_saveOptions = newSaveOptions;
373 *m_metaInfo = newMetaInfo;
374 m_modified = false;
376 emit documentOpened ();
377 return true;
380 if (newDocSameNameIfNotExist)
382 if (!url.isEmpty () &&
383 // not just a permission error?
384 !KIO::NetAccess::exists (url, KIO::NetAccess::SourceSide/*open*/, d->environ->dialogParent ()))
386 openNew (url);
388 else
390 openNew (KUrl ());
393 return true;
395 else
397 return false;