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> // LOTODO: isn't this in KIO?
56 #include <ktemporaryfile.h>
58 #include <kpAbstractSelection.h>
59 #include <kpAbstractImageSelection.h>
61 #include <kpColorToolBar.h>
63 #include <kpDocumentEnvironment.h>
64 #include <kpDocumentSaveOptions.h>
65 #include <kpDocumentMetaInfo.h>
66 #include <kpEffectReduceColors.h>
67 #include <kpPixmapFX.h>
69 #include <kpToolToolBar.h>
70 #include <kpUrlFormatter.h>
71 #include <kpViewManager.h>
74 kpDocument::kpDocument (int w
, int h
,
75 kpDocumentEnvironment
*environ
)
77 m_constructorWidth (w
), m_constructorHeight (h
),
79 m_savedAtLeastOnceBefore (false),
80 m_saveOptions (new kpDocumentSaveOptions ()),
81 m_metaInfo (new kpDocumentMetaInfo ()),
84 m_oldWidth (-1), m_oldHeight (-1),
85 d (new kpDocumentPrivate ())
87 #if DEBUG_KP_DOCUMENT && 0
88 kDebug () << "kpDocument::kpDocument (" << w
<< "," << h
<< ")";
91 m_image
= new kpImage (w
, h
);
92 m_image
->fill (Qt::white
);
97 kpDocument::~kpDocument ()
103 delete m_saveOptions
;
111 kpDocumentEnvironment
*kpDocument::environ () const
117 void kpDocument::setEnviron (kpDocumentEnvironment
*environ
)
119 d
->environ
= environ
;
124 bool kpDocument::savedAtLeastOnceBefore () const
126 return m_savedAtLeastOnceBefore
;
130 KUrl
kpDocument::url () const
136 void kpDocument::setURL (const KUrl
&url
, bool isFromURL
)
139 m_isFromURL
= isFromURL
;
143 bool kpDocument::isFromURL (bool checkURLStillExists
) const
148 if (!checkURLStillExists
)
151 return (!m_url
.isEmpty () &&
152 KIO::NetAccess::exists (m_url
, KIO::NetAccess::SourceSide
/*open*/,
153 d
->environ
->dialogParent ()));
158 QString
kpDocument::prettyUrl () const
160 return kpUrlFormatter::PrettyUrl (m_url
);
164 QString
kpDocument::prettyFilename () const
166 return kpUrlFormatter::PrettyFilename (m_url
);
171 const kpDocumentSaveOptions
*kpDocument::saveOptions () const
173 return m_saveOptions
;
177 void kpDocument::setSaveOptions (const kpDocumentSaveOptions
&saveOptions
)
179 *m_saveOptions
= saveOptions
;
184 const kpDocumentMetaInfo
*kpDocument::metaInfo () const
190 void kpDocument::setMetaInfo (const kpDocumentMetaInfo
&metaInfo
)
192 *m_metaInfo
= metaInfo
;
200 void kpDocument::setModified (bool yes
)
202 if (yes
== m_modified
)
208 emit
documentModified ();
211 bool kpDocument::isModified () const
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 ();
232 return m_image
->width ();
235 int kpDocument::oldWidth () const
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 ();
256 return m_image
->height ();
259 int kpDocument::oldHeight () const
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 ();
274 return m_image
->rect ();
279 kpImage
kpDocument::getImageAt (const QRect
&rect
) const
281 return kpPixmapFX::getPixmapAt (*m_image
, rect
);
285 void kpDocument::setImageAt (const kpImage
&image
, const QPoint
&at
)
287 #if DEBUG_KP_DOCUMENT && 0
288 kDebug () << "kpDocument::setImageAt (image (w="
290 << ",h=" << image
.height ()
291 << "), x=" << at
.x ()
296 kpPixmapFX::setPixmapAt (m_image
, at
, image
);
297 slotContentsChanged (QRect (at
.x (), at
.y (), image
.width (), image
.height ()));
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 ()));
309 kpImage
kpDocument::image (bool ofSelection
) const
315 kpAbstractImageSelection
*imageSel
= imageSelection ();
318 ret
= imageSel
->baseImage ();
323 KP_PFX_CHECK_NO_ALPHA_CHANNEL (ret
);
328 kpImage
*kpDocument::imagePointer () const
330 KP_PFX_CHECK_NO_ALPHA_CHANNEL (*m_image
);
336 void kpDocument::setImage (const kpImage
&image
)
338 KP_PFX_CHECK_NO_ALPHA_CHANNEL (image
);
340 m_oldWidth
= width (), m_oldHeight
= height ();
344 if (m_oldWidth
== width () && m_oldHeight
== height ())
345 slotContentsChanged (image
.rect ());
347 slotSizeChanged (width (), height ());
351 void kpDocument::setImage (bool ofSelection
, const kpImage
&image
)
353 KP_PFX_CHECK_NO_ALPHA_CHANNEL (image
);
357 kpAbstractImageSelection
*imageSel
= imageSelection ();
359 // Have to have an image selection in order to set its pixmap.
362 imageSel
->setBaseImage (image
);
369 void kpDocument::fill (const kpColor
&color
)
371 #if DEBUG_KP_DOCUMENT
372 kDebug () << "kpDocument::fill ()";
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
<< ")";
385 m_oldWidth
= width (), m_oldHeight
= height ();
387 #if DEBUG_KP_DOCUMENT && 1
388 kDebug () << "\toldWidth=" << m_oldWidth
389 << " oldHeight=" << m_oldHeight
393 if (w
== m_oldWidth
&& h
== m_oldHeight
)
396 kpPixmapFX::resize (m_image
, w
, h
, backgroundColor
);
398 slotSizeChanged (width (), height ());
402 void kpDocument::slotContentsChanged (const QRect
&rect
)
405 emit
contentsChanged (rect
);
408 void kpDocument::slotSizeChanged (int newWidth
, int newHeight
)
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>