compile
[kdegraphics.git] / kolourpaint / document / kpDocument.cpp
blob7bc967eb9618c3190fe9432084007f95e8ec2d50
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> // LOTODO: isn't this in KIO?
56 #include <ktemporaryfile.h>
58 #include <kpAbstractSelection.h>
59 #include <kpAbstractImageSelection.h>
60 #include <kpColor.h>
61 #include <kpColorToolBar.h>
62 #include <kpDefs.h>
63 #include <kpDocumentEnvironment.h>
64 #include <kpDocumentSaveOptions.h>
65 #include <kpDocumentMetaInfo.h>
66 #include <kpEffectReduceColors.h>
67 #include <kpPixmapFX.h>
68 #include <kpTool.h>
69 #include <kpToolToolBar.h>
70 #include <kpUrlFormatter.h>
71 #include <kpViewManager.h>
74 kpDocument::kpDocument (int w, int h,
75 kpDocumentEnvironment *environ)
76 : QObject (),
77 m_constructorWidth (w), m_constructorHeight (h),
78 m_isFromURL (false),
79 m_savedAtLeastOnceBefore (false),
80 m_saveOptions (new kpDocumentSaveOptions ()),
81 m_metaInfo (new kpDocumentMetaInfo ()),
82 m_modified (false),
83 m_selection (0),
84 m_oldWidth (-1), m_oldHeight (-1),
85 d (new kpDocumentPrivate ())
87 #if DEBUG_KP_DOCUMENT && 0
88 kDebug () << "kpDocument::kpDocument (" << w << "," << h << ")";
89 #endif
91 m_image = new kpImage (w, h);
92 m_image->fill (Qt::white);
94 d->environ = environ;
97 kpDocument::~kpDocument ()
99 delete d;
101 delete m_image;
103 delete m_saveOptions;
104 delete m_metaInfo;
106 delete m_selection;
110 // public
111 kpDocumentEnvironment *kpDocument::environ () const
113 return d->environ;
116 // public
117 void kpDocument::setEnviron (kpDocumentEnvironment *environ)
119 d->environ = environ;
123 // public
124 bool kpDocument::savedAtLeastOnceBefore () const
126 return m_savedAtLeastOnceBefore;
129 // public
130 KUrl kpDocument::url () const
132 return m_url;
135 // public
136 void kpDocument::setURL (const KUrl &url, bool isFromURL)
138 m_url = url;
139 m_isFromURL = isFromURL;
142 // public
143 bool kpDocument::isFromURL (bool checkURLStillExists) const
145 if (!m_isFromURL)
146 return false;
148 if (!checkURLStillExists)
149 return true;
151 return (!m_url.isEmpty () &&
152 KIO::NetAccess::exists (m_url, KIO::NetAccess::SourceSide/*open*/,
153 d->environ->dialogParent ()));
157 // public
158 QString kpDocument::prettyUrl () const
160 return kpUrlFormatter::PrettyUrl (m_url);
163 // public
164 QString kpDocument::prettyFilename () const
166 return kpUrlFormatter::PrettyFilename (m_url);
170 // public
171 const kpDocumentSaveOptions *kpDocument::saveOptions () const
173 return m_saveOptions;
176 // public
177 void kpDocument::setSaveOptions (const kpDocumentSaveOptions &saveOptions)
179 *m_saveOptions = saveOptions;
183 // public
184 const kpDocumentMetaInfo *kpDocument::metaInfo () const
186 return m_metaInfo;
189 // public
190 void kpDocument::setMetaInfo (const kpDocumentMetaInfo &metaInfo)
192 *m_metaInfo = metaInfo;
197 * Properties
200 void kpDocument::setModified (bool yes)
202 if (yes == m_modified)
203 return;
205 m_modified = yes;
207 if (yes)
208 emit documentModified ();
211 bool kpDocument::isModified () const
213 return m_modified;
216 bool kpDocument::isEmpty () const
218 return url ().isEmpty () && !isModified ();
222 int kpDocument::constructorWidth () const
224 return m_constructorWidth;
227 int kpDocument::width (bool ofSelection) const
229 if (ofSelection && m_selection)
230 return m_selection->width ();
231 else
232 return m_image->width ();
235 int kpDocument::oldWidth () const
237 return m_oldWidth;
240 void kpDocument::setWidth (int w, const kpColor &backgroundColor)
242 resize (w, height (), backgroundColor);
246 int kpDocument::constructorHeight () const
248 return m_constructorHeight;
251 int kpDocument::height (bool ofSelection) const
253 if (ofSelection && m_selection)
254 return m_selection->height ();
255 else
256 return m_image->height ();
259 int kpDocument::oldHeight () const
261 return m_oldHeight;
264 void kpDocument::setHeight (int h, const kpColor &backgroundColor)
266 resize (width (), h, backgroundColor);
269 QRect kpDocument::rect (bool ofSelection) const
271 if (ofSelection && m_selection)
272 return m_selection->boundingRect ();
273 else
274 return m_image->rect ();
278 // public
279 kpImage kpDocument::getImageAt (const QRect &rect) const
281 return kpPixmapFX::getPixmapAt (*m_image, rect);
284 // public
285 void kpDocument::setImageAt (const kpImage &image, const QPoint &at)
287 #if DEBUG_KP_DOCUMENT && 0
288 kDebug () << "kpDocument::setImageAt (image (w="
289 << image.width ()
290 << ",h=" << image.height ()
291 << "), x=" << at.x ()
292 << ",y=" << at.y ()
293 << endl;
294 #endif
296 kpPixmapFX::setPixmapAt (m_image, at, image);
297 slotContentsChanged (QRect (at.x (), at.y (), image.width (), image.height ()));
300 // public
301 void kpDocument::paintImageAt (const kpImage &image, const QPoint &at)
303 kpPixmapFX::paintPixmapAt (m_image, at, image);
304 slotContentsChanged (QRect (at.x (), at.y (), image.width (), image.height ()));
308 // public
309 kpImage kpDocument::image (bool ofSelection) const
311 kpImage ret;
313 if (ofSelection)
315 kpAbstractImageSelection *imageSel = imageSelection ();
316 Q_ASSERT (imageSel);
318 ret = imageSel->baseImage ();
320 else
321 ret = *m_image;
323 KP_PFX_CHECK_NO_ALPHA_CHANNEL (ret);
324 return ret;
327 // public
328 kpImage *kpDocument::imagePointer () const
330 KP_PFX_CHECK_NO_ALPHA_CHANNEL (*m_image);
331 return m_image;
335 // public
336 void kpDocument::setImage (const kpImage &image)
338 KP_PFX_CHECK_NO_ALPHA_CHANNEL (image);
340 m_oldWidth = width (), m_oldHeight = height ();
342 *m_image = image;
344 if (m_oldWidth == width () && m_oldHeight == height ())
345 slotContentsChanged (image.rect ());
346 else
347 slotSizeChanged (width (), height ());
350 // public
351 void kpDocument::setImage (bool ofSelection, const kpImage &image)
353 KP_PFX_CHECK_NO_ALPHA_CHANNEL (image);
355 if (ofSelection)
357 kpAbstractImageSelection *imageSel = imageSelection ();
359 // Have to have an image selection in order to set its pixmap.
360 Q_ASSERT (imageSel);
362 imageSel->setBaseImage (image);
364 else
365 setImage (image);
369 void kpDocument::fill (const kpColor &color)
371 #if DEBUG_KP_DOCUMENT
372 kDebug () << "kpDocument::fill ()";
373 #endif
375 kpPixmapFX::fill (m_image, color);
376 slotContentsChanged (m_image->rect ());
379 void kpDocument::resize (int w, int h, const kpColor &backgroundColor)
381 #if DEBUG_KP_DOCUMENT
382 kDebug () << "kpDocument::resize (" << w << "," << h << ")";
383 #endif
385 m_oldWidth = width (), m_oldHeight = height ();
387 #if DEBUG_KP_DOCUMENT && 1
388 kDebug () << "\toldWidth=" << m_oldWidth
389 << " oldHeight=" << m_oldHeight
390 << endl;
391 #endif
393 if (w == m_oldWidth && h == m_oldHeight)
394 return;
396 kpPixmapFX::resize (m_image, w, h, backgroundColor);
398 slotSizeChanged (width (), height ());
402 void kpDocument::slotContentsChanged (const QRect &rect)
404 setModified ();
405 emit contentsChanged (rect);
408 void kpDocument::slotSizeChanged (int newWidth, int newHeight)
410 setModified ();
411 emit sizeChanged (newWidth, newHeight);
412 emit sizeChanged (QSize (newWidth, newHeight));
415 void kpDocument::slotSizeChanged (const QSize &newSize)
417 slotSizeChanged (newSize.width (), newSize.height ());
421 #include <kpDocument.moc>