3 Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
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>
52 #include <kio/netaccess.h>
54 #include <kmessagebox.h>
55 #include <kmimetype.h> // TODO: isn't this in KIO?
56 #include <ktemporaryfile.h>
59 #include <kpColorToolBar.h>
61 #include <kpDocumentEnvironment.h>
62 #include <kpDocumentSaveOptions.h>
63 #include <kpDocumentMetaInfo.h>
64 #include <kpEffectReduceColors.h>
65 #include <kpPixmapFX.h>
67 #include <kpToolToolBar.h>
68 #include <kpUrlFormatter.h>
69 #include <kpViewManager.h>
72 QPixmap
kpDocument::convertToPixmapAsLosslessAsPossible (
74 const kpPixmapFX::WarnAboutLossInfo
&wali
,
76 kpDocumentSaveOptions
*saveOptions
,
77 kpDocumentMetaInfo
*metaInfo
)
83 kDebug () << "\timage: depth=" << image
.depth ()
84 << " (X display=" << QPixmap::defaultDepth () << ")"
85 << " hasAlphaChannel=" << image
.hasAlphaChannel ()
91 saveOptions
->setColorDepth (image
.depth ());
92 saveOptions
->setDither (false); // avoid double dithering when saving
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 ();
106 metaInfo
->setText (*it
, image
.text (*it
));
109 #if DEBUG_KP_DOCUMENT
110 metaInfo
->printDebug ("\tmetaInfo");
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");
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;
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;
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
167 << " hash=" << hash
<< endl
;
175 QPixmap
kpDocument::getPixmapFromFile (const KUrl
&url
, bool suppressDoesntExistDialog
,
177 kpDocumentSaveOptions
*saveOptions
,
178 kpDocumentMetaInfo
*metaInfo
)
180 #if DEBUG_KP_DOCUMENT
181 kDebug () << "kpDocument::getPixmapFromFile(" << url
<< "," << parent
<< ")";
185 *saveOptions
= kpDocumentSaveOptions ();
188 *metaInfo
= kpDocumentMetaInfo ();
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
)));
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 ();
223 saveOptions
->setMimeType (detectedMimeType
);
225 #if DEBUG_KP_DOCUMENT
226 kDebug () << "\ttempFile=" << tempFile
;
227 kDebug () << "\tmimetype=" << detectedMimeType
;
228 kDebug () << "\tsrc=" << url
.path ();
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
);
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
252 image
= QImage (tempFile
);
253 KIO::NetAccess::removeTempFile (tempFile
);
258 KMessageBox::sorry (parent
,
259 i18n ("Could not open \"%1\" - unsupported image format.\n"
260 "The file may be corrupt.",
261 kpUrlFormatter::PrettyFilename (url
)));
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>"
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
)),
311 if (newPixmap
.isNull ())
313 KMessageBox::sorry (parent
,
314 i18n ("Could not open \"%1\" - out of graphics memory.",
315 kpUrlFormatter::PrettyFilename (url
)));
319 #if DEBUG_KP_DOCUMENT
320 kDebug () << "\tpixmap: depth=" << newPixmap
.depth ()
321 << " hasAlphaChannelOrMask=" << newPixmap
.hasAlpha ()
322 << " hasAlphaChannel=" << newPixmap
.hasAlphaChannel ()
330 void kpDocument::openNew (const KUrl
&url
)
332 #if DEBUG_KP_DOCUMENT
333 kDebug () << "kpDocument::openNew (" << url
<< ")";
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
342 // That way "kolourpaint doesnotexist.bmp" would automatically
343 // select the BMP file format when the save dialog comes up for
345 *m_saveOptions
= kpDocumentSaveOptions ();
346 *m_metaInfo
= kpDocumentMetaInfo ();
349 emit
documentOpened ();
352 bool kpDocument::open (const KUrl
&url
, bool newDocSameNameIfNotExist
)
354 #if DEBUG_KP_DOCUMENT
355 kDebug () << "kpDocument::open (" << url
<< ")";
358 kpDocumentSaveOptions newSaveOptions
;
359 kpDocumentMetaInfo newMetaInfo
;
360 QPixmap newPixmap
= kpDocument::getPixmapFromFile (url
,
361 newDocSameNameIfNotExist
/*suppress "doesn't exist" dialog*/,
362 d
->environ
->dialogParent (),
366 if (!newPixmap
.isNull ())
369 m_image
= new kpImage (newPixmap
);
371 setURL (url
, true/*is from url*/);
372 *m_saveOptions
= newSaveOptions
;
373 *m_metaInfo
= newMetaInfo
;
376 emit
documentOpened ();
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 ()))