fix logic
[personal-kdelibs.git] / khtml / imload / imagepainter.cpp
blob3c1d75a87af630f0f270764f419da3b4464a8e70
1 /*
2 Progressive image displaying library.
4 Copyright (C) 2004,2005 Maks Orlovich (maksim@kde.org)
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "imagepainter.h"
26 #include "image.h"
27 #include "pixmapplane.h"
28 #include "imagemanager.h"
30 namespace khtmlImLoad {
32 ImagePainter::ImagePainter(Image* _image):image(_image), sizeRefd(false)
33 { //No need to ref, default size.
34 size = image->size();
37 ImagePainter::ImagePainter(Image* _image, QSize _size):image(_image), size(_size), sizeRefd(false)
39 if (!ImageManager::isAcceptableScaleSize(_size.width(), _size.height()))
40 setDefaultSize();
43 ImagePainter::~ImagePainter()
45 if (sizeRefd)
46 image->derefSize(size);
49 ImagePainter::ImagePainter(const ImagePainter& src)
51 image = src.image;
52 size = src.size;
53 sizeRefd = false;
56 ImagePainter& ImagePainter::operator=(const ImagePainter& src)
58 if (sizeRefd)
59 image->derefSize(size);
60 image = src.image;
61 size = src.size;
62 sizeRefd = false;
63 return *this;
66 void ImagePainter::setSize(QSize _size)
68 if (!ImageManager::isAcceptableScaleSize(_size.width(), _size.height())) {
69 setDefaultSize();
70 return;
73 // Don't do anything if size didn't change,
74 // to avoid dropping the image..
75 if (size == _size)
76 return;
78 if (sizeRefd)
79 image->derefSize(size);
80 size = _size;
81 sizeRefd = false;
84 void ImagePainter::setDefaultSize()
86 if (sizeRefd)
87 image->derefSize(size);
88 size = image->size();
89 sizeRefd = false;
92 void ImagePainter::paint(int dx, int dy, QPainter* p, int sx, int sy,
93 int width, int height)
95 if (!image->mayPaint())
96 // ### fallback painting in case bg?
97 return;
99 // Do our lazy ref if needed. Safe due to above
100 if (!sizeRefd && size != image->size())
102 image->refSize(size);
103 sizeRefd = true;
106 PixmapPlane* plane = image->getSize(size);
108 if (plane->animProvider)
110 // Clip the request ourselves when animating..
111 QSize imageSize = image->size();
113 if (width == -1)
114 width = imageSize.width();
115 if (height == -1)
116 height = imageSize.height();
118 QRect clippedRect = QRect(0, 0, imageSize.width(), imageSize.height())
119 & QRect(sx, sy, width, height);
120 plane->animProvider->paint(dx, dy, p, clippedRect.x(), clippedRect.y(),
121 clippedRect.width(), clippedRect.height());
122 return;
125 // non-animated, go straight to PixmapPlane; it clips itself
126 plane->paint(dx, dy, p, sx, sy, width, height);
131 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;