kpDocument.cpp 11.1 KB
Newer Older
1 2

/*
3
   Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#define DEBUG_KP_DOCUMENT 0


#include <kpDocument.h>
#include <kpDocumentPrivate.h>

#include <math.h>

#include <qcolor.h>
#include <qbitmap.h>
#include <qbrush.h>
#include <qfile.h>
#include <qimage.h>
#include <qlist.h>
43
#include <QImage>
44 45 46 47 48 49 50 51 52 53 54 55
#include <qpainter.h>
#include <qrect.h>
#include <qsize.h>
#include <qmatrix.h>

#include <kdebug.h>
#include <kglobal.h>
#include <kimageio.h>
#include <kio/netaccess.h>
#include <klocale.h>
#include <ktemporaryfile.h>

56 57
#include <kpAbstractSelection.h>
#include <kpAbstractImageSelection.h>
58 59 60
#include <kpColor.h>
#include <kpColorToolBar.h>
#include <kpDefs.h>
61
#include <kpDocumentEnvironment.h>
62 63
#include <kpDocumentSaveOptions.h>
#include <kpDocumentMetaInfo.h>
64
#include <kpEffectReduceColors.h>
65 66 67
#include <kpPixmapFX.h>
#include <kpTool.h>
#include <kpToolToolBar.h>
68
#include <kpUrlFormatter.h>
69

70
//---------------------------------------------------------------------
71

72 73 74 75
kpDocument::kpDocument (int w, int h,
        kpDocumentEnvironment *environ)
    : QObject (),
      m_constructorWidth (w), m_constructorHeight (h),
76 77 78 79 80 81 82 83 84 85
      m_isFromURL (false),
      m_savedAtLeastOnceBefore (false),
      m_saveOptions (new kpDocumentSaveOptions ()),
      m_metaInfo (new kpDocumentMetaInfo ()),
      m_modified (false),
      m_selection (0),
      m_oldWidth (-1), m_oldHeight (-1),
      d (new kpDocumentPrivate ())
{
#if DEBUG_KP_DOCUMENT && 0
86
    kDebug () << "kpDocument::kpDocument (" << w << "," << h << ")";
87 88
#endif

89 90
    m_image = new kpImage(w, h, QImage::Format_ARGB32_Premultiplied);
    m_image->fill(QColor(Qt::white).rgb());
91 92

    d->environ = environ;
93 94
}

95 96
//---------------------------------------------------------------------

97 98 99 100
kpDocument::~kpDocument ()
{
    delete d;

101
    delete m_image;
102 103 104 105 106 107 108

    delete m_saveOptions;
    delete m_metaInfo;

    delete m_selection;
}

109
//---------------------------------------------------------------------
110

111 112
// public
kpDocumentEnvironment *kpDocument::environ () const
113
{
114
    return d->environ;
115 116
}

117 118
//---------------------------------------------------------------------

119 120
// public
void kpDocument::setEnviron (kpDocumentEnvironment *environ)
121
{
122
    d->environ = environ;
123 124
}

125
//---------------------------------------------------------------------
126 127 128 129 130 131 132

// public
bool kpDocument::savedAtLeastOnceBefore () const
{
    return m_savedAtLeastOnceBefore;
}

133 134
//---------------------------------------------------------------------

135
// public
Christoph Feck's avatar
Christoph Feck committed
136
QUrl kpDocument::url () const
137 138 139 140
{
    return m_url;
}

141 142
//---------------------------------------------------------------------

143
// public
Christoph Feck's avatar
Christoph Feck committed
144
void kpDocument::setURL (const QUrl &url, bool isFromURL)
145 146 147 148 149
{
    m_url = url;
    m_isFromURL = isFromURL;
}

150 151
//---------------------------------------------------------------------

152 153 154 155 156 157 158 159 160 161
// public
bool kpDocument::isFromURL (bool checkURLStillExists) const
{
    if (!m_isFromURL)
        return false;

    if (!checkURLStillExists)
        return true;

    return (!m_url.isEmpty () &&
162
            KIO::NetAccess::exists (m_url, KIO::NetAccess::SourceSide/*open*/,
163
                d->environ->dialogParent ()));
164 165
}

166
//---------------------------------------------------------------------
167

168
// public
169 170
QString kpDocument::prettyUrl () const
{
171
    return kpUrlFormatter::PrettyUrl (m_url);
172 173
}

174 175
//---------------------------------------------------------------------

176
// public
177 178
QString kpDocument::prettyFilename () const
{
179
    return kpUrlFormatter::PrettyFilename (m_url);
180 181
}

182
//---------------------------------------------------------------------
183 184 185 186 187 188 189

// public
const kpDocumentSaveOptions *kpDocument::saveOptions () const
{
    return m_saveOptions;
}

190 191
//---------------------------------------------------------------------

192 193 194 195 196 197
// public
void kpDocument::setSaveOptions (const kpDocumentSaveOptions &saveOptions)
{
    *m_saveOptions = saveOptions;
}

198
//---------------------------------------------------------------------
199 200 201 202 203 204 205

// public
const kpDocumentMetaInfo *kpDocument::metaInfo () const
{
    return m_metaInfo;
}

206 207
//---------------------------------------------------------------------

208 209 210 211 212 213
// public
void kpDocument::setMetaInfo (const kpDocumentMetaInfo &metaInfo)
{
    *m_metaInfo = metaInfo;
}

214
//---------------------------------------------------------------------
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

/*
 * Properties
 */

void kpDocument::setModified (bool yes)
{
    if (yes == m_modified)
        return;

    m_modified = yes;

    if (yes)
        emit documentModified ();
}

231 232
//---------------------------------------------------------------------

233 234 235 236 237
bool kpDocument::isModified () const
{
    return m_modified;
}

238 239
//---------------------------------------------------------------------

240 241 242 243 244
bool kpDocument::isEmpty () const
{
    return url ().isEmpty () && !isModified ();
}

245
//---------------------------------------------------------------------
246 247 248 249 250 251

int kpDocument::constructorWidth () const
{
    return m_constructorWidth;
}

252 253
//---------------------------------------------------------------------

254 255 256 257 258
int kpDocument::width (bool ofSelection) const
{
    if (ofSelection && m_selection)
        return m_selection->width ();
    else
259
        return m_image->width ();
260 261
}

262 263
//---------------------------------------------------------------------

264 265 266 267 268
int kpDocument::oldWidth () const
{
    return m_oldWidth;
}

269 270
//---------------------------------------------------------------------

271 272 273 274 275
void kpDocument::setWidth (int w, const kpColor &backgroundColor)
{
    resize (w, height (), backgroundColor);
}

276
//---------------------------------------------------------------------
277 278 279 280 281 282

int kpDocument::constructorHeight () const
{
    return m_constructorHeight;
}

283 284
//---------------------------------------------------------------------

285 286 287 288 289
int kpDocument::height (bool ofSelection) const
{
    if (ofSelection && m_selection)
        return m_selection->height ();
    else
290
        return m_image->height ();
291 292
}

293 294
//---------------------------------------------------------------------

295 296 297 298 299
int kpDocument::oldHeight () const
{
    return m_oldHeight;
}

300 301
//---------------------------------------------------------------------

302 303 304 305 306
void kpDocument::setHeight (int h, const kpColor &backgroundColor)
{
    resize (width (), h, backgroundColor);
}

307 308
//---------------------------------------------------------------------

309 310 311 312 313
QRect kpDocument::rect (bool ofSelection) const
{
    if (ofSelection && m_selection)
        return m_selection->boundingRect ();
    else
314
        return m_image->rect ();
315 316
}

317
//---------------------------------------------------------------------
318 319

// public
320
kpImage kpDocument::getImageAt (const QRect &rect) const
321
{
322
    return kpPixmapFX::getPixmapAt (*m_image, rect);
323 324
}

325 326
//---------------------------------------------------------------------

327
// public
328
void kpDocument::setImageAt (const kpImage &image, const QPoint &at)
329 330
{
#if DEBUG_KP_DOCUMENT && 0
331 332 333
    kDebug () << "kpDocument::setImageAt (image (w="
               << image.width ()
               << ",h=" << image.height ()
334 335 336 337 338
               << "), x=" << at.x ()
               << ",y=" << at.y ()
               << endl;
#endif

339 340
    kpPixmapFX::setPixmapAt (m_image, at, image);
    slotContentsChanged (QRect (at.x (), at.y (), image.width (), image.height ()));
341 342
}

343
//---------------------------------------------------------------------
344 345

// public
346
kpImage kpDocument::image (bool ofSelection) const
347
{
348 349
    kpImage ret;

350 351
    if (ofSelection)
    {
352 353 354 355
        kpAbstractImageSelection *imageSel = imageSelection ();
        Q_ASSERT (imageSel);

        ret = imageSel->baseImage ();
356 357
    }
    else
358 359 360
        ret = *m_image;

    return ret;
361 362
}

363 364
//---------------------------------------------------------------------

365
// public
366
kpImage *kpDocument::imagePointer () const
367
{
368 369 370
    return m_image;
}

371
//---------------------------------------------------------------------
372 373 374 375

// public
void kpDocument::setImage (const kpImage &image)
{
376 377
    m_oldWidth = width (), m_oldHeight = height ();

378
    *m_image = image;
379 380

    if (m_oldWidth == width () && m_oldHeight == height ())
381
        slotContentsChanged (image.rect ());
382 383 384 385
    else
        slotSizeChanged (width (), height ());
}

386 387
//---------------------------------------------------------------------

388
// public
389
void kpDocument::setImage (bool ofSelection, const kpImage &image)
390 391 392
{
    if (ofSelection)
    {
393 394 395 396 397 398
        kpAbstractImageSelection *imageSel = imageSelection ();

        // Have to have an image selection in order to set its pixmap.
        Q_ASSERT (imageSel);

        imageSel->setBaseImage (image);
399 400
    }
    else
401
        setImage (image);
402 403
}

404
//---------------------------------------------------------------------
405 406 407 408

void kpDocument::fill (const kpColor &color)
{
#if DEBUG_KP_DOCUMENT
409
    kDebug () << "kpDocument::fill ()";
410 411
#endif

412
    m_image->fill(color.toQRgb());
413
    slotContentsChanged (m_image->rect ());
414 415
}

416 417
//---------------------------------------------------------------------

418 419 420
void kpDocument::resize (int w, int h, const kpColor &backgroundColor)
{
#if DEBUG_KP_DOCUMENT
421
    kDebug () << "kpDocument::resize (" << w << "," << h << ")";
422 423 424 425 426 427 428 429 430 431 432 433 434
#endif

    m_oldWidth = width (), m_oldHeight = height ();

#if DEBUG_KP_DOCUMENT && 1
    kDebug () << "\toldWidth=" << m_oldWidth
               << " oldHeight=" << m_oldHeight
               << endl;
#endif

    if (w == m_oldWidth && h == m_oldHeight)
        return;

435
    kpPixmapFX::resize (m_image, w, h, backgroundColor);
436 437 438 439

    slotSizeChanged (width (), height ());
}

440
//---------------------------------------------------------------------
441 442 443 444 445 446 447

void kpDocument::slotContentsChanged (const QRect &rect)
{
    setModified ();
    emit contentsChanged (rect);
}

448 449
//---------------------------------------------------------------------

450 451 452 453 454 455 456
void kpDocument::slotSizeChanged (int newWidth, int newHeight)
{
    setModified ();
    emit sizeChanged (newWidth, newHeight);
    emit sizeChanged (QSize (newWidth, newHeight));
}

457 458
//---------------------------------------------------------------------

459 460 461 462 463
void kpDocument::slotSizeChanged (const QSize &newSize)
{
    slotSizeChanged (newSize.width (), newSize.height ());
}

464 465
//---------------------------------------------------------------------

466

467 468
#include <kpDocument.moc>