1 /*
2     Progressive image displaying library.
3 
4     Copyright (C) 2004,2005 Maks Orlovich (maksim@kde.org)
5 
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:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
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.
22 
23 */
24 
25 #include "imagepainter.h"
26 #include "image.h"
27 #include "pixmapplane.h"
28 #include "imagemanager.h"
29 
30 namespace khtmlImLoad
31 {
32 
ImagePainter(Image * _image)33 ImagePainter::ImagePainter(Image *_image): image(_image), sizeRefd(false)
34 {
35     //No need to ref, default size.
36     size = image->size();
37 }
38 
ImagePainter(Image * _image,QSize _size)39 ImagePainter::ImagePainter(Image *_image, QSize _size): image(_image), size(_size), sizeRefd(false)
40 {
41     if (!ImageManager::isAcceptableScaleSize(_size.width(), _size.height())) {
42         setDefaultSize();
43     }
44 }
45 
~ImagePainter()46 ImagePainter::~ImagePainter()
47 {
48     if (sizeRefd) {
49         image->derefSize(size);
50     }
51 }
52 
ImagePainter(const ImagePainter & src)53 ImagePainter::ImagePainter(const ImagePainter &src)
54 {
55     image = src.image;
56     size  = src.size;
57     sizeRefd = false;
58 }
59 
operator =(const ImagePainter & src)60 ImagePainter &ImagePainter::operator=(const ImagePainter &src)
61 {
62     if (sizeRefd) {
63         image->derefSize(size);
64     }
65     image = src.image;
66     size  = src.size;
67     sizeRefd = false;
68     return *this;
69 }
70 
setSize(QSize _size)71 void ImagePainter::setSize(QSize _size)
72 {
73     if (!ImageManager::isAcceptableScaleSize(_size.width(), _size.height())) {
74         setDefaultSize();
75         return;
76     }
77 
78     // Don't do anything if size didn't change,
79     // to avoid dropping the image..
80     if (size == _size) {
81         return;
82     }
83 
84     if (sizeRefd) {
85         image->derefSize(size);
86     }
87     size = _size;
88     sizeRefd = false;
89 }
90 
setDefaultSize()91 void ImagePainter::setDefaultSize()
92 {
93     if (sizeRefd) {
94         image->derefSize(size);
95     }
96     size = image->size();
97     sizeRefd = false;
98 }
99 
paint(int dx,int dy,QPainter * p,int sx,int sy,int width,int height)100 void ImagePainter::paint(int dx, int dy, QPainter *p, int sx, int sy,
101                          int width, int height)
102 {
103     if (!image->mayPaint())
104         // ### fallback painting in case bg?
105     {
106         return;
107     }
108 
109     // Do our lazy ref if needed. Safe due to above
110     if (!sizeRefd && size != image->size()) {
111         image->refSize(size);
112         sizeRefd = true;
113     }
114 
115     PixmapPlane *plane = image->getSize(size);
116 
117     if (plane->animProvider) {
118         // Clip the request ourselves when animating..
119 
120         if (width == -1) {
121             width  = size.width();
122         }
123         if (height == -1) {
124             height = size.height();
125         }
126 
127         QRect clippedRect = QRect(0, 0, size.width(), size.height())
128                             & QRect(sx, sy, width, height);
129         plane->animProvider->paint(dx, dy, p, clippedRect.x(), clippedRect.y(),
130                                    clippedRect.width(), clippedRect.height());
131         return;
132     }
133 
134     // non-animated, go straight to PixmapPlane; it clips itself
135     plane->paint(dx, dy, p, sx, sy, width, height);
136 }
137 
138 }
139 
140