File indexing completed on 2024-05-19 11:38:33

0001 /*
0002     Progressive image displaying library.
0003 
0004     Copyright (C) 2004,2005 Maks Orlovich (maksim@kde.org)
0005 
0006     Permission is hereby granted, free of charge, to any person obtaining a copy
0007     of this software and associated documentation files (the "Software"), to deal
0008     in the Software without restriction, including without limitation the rights
0009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0010     copies of the Software, and to permit persons to whom the Software is
0011     furnished to do so, subject to the following conditions:
0012 
0013     The above copyright notice and this permission notice shall be included in
0014     all copies or substantial portions of the Software.
0015 
0016     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0017     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0018     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
0019     AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
0020     AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
0021     CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022 
0023 */
0024 
0025 #include "imagepainter.h"
0026 #include "image.h"
0027 #include "pixmapplane.h"
0028 #include "imagemanager.h"
0029 
0030 namespace khtmlImLoad
0031 {
0032 
0033 ImagePainter::ImagePainter(Image *_image): image(_image), sizeRefd(false)
0034 {
0035     //No need to ref, default size.
0036     size = image->size();
0037 }
0038 
0039 ImagePainter::ImagePainter(Image *_image, QSize _size): image(_image), size(_size), sizeRefd(false)
0040 {
0041     if (!ImageManager::isAcceptableScaleSize(_size.width(), _size.height())) {
0042         setDefaultSize();
0043     }
0044 }
0045 
0046 ImagePainter::~ImagePainter()
0047 {
0048     if (sizeRefd) {
0049         image->derefSize(size);
0050     }
0051 }
0052 
0053 ImagePainter::ImagePainter(const ImagePainter &src)
0054 {
0055     image = src.image;
0056     size  = src.size;
0057     sizeRefd = false;
0058 }
0059 
0060 ImagePainter &ImagePainter::operator=(const ImagePainter &src)
0061 {
0062     if (sizeRefd) {
0063         image->derefSize(size);
0064     }
0065     image = src.image;
0066     size  = src.size;
0067     sizeRefd = false;
0068     return *this;
0069 }
0070 
0071 void ImagePainter::setSize(QSize _size)
0072 {
0073     if (!ImageManager::isAcceptableScaleSize(_size.width(), _size.height())) {
0074         setDefaultSize();
0075         return;
0076     }
0077 
0078     // Don't do anything if size didn't change,
0079     // to avoid dropping the image..
0080     if (size == _size) {
0081         return;
0082     }
0083 
0084     if (sizeRefd) {
0085         image->derefSize(size);
0086     }
0087     size = _size;
0088     sizeRefd = false;
0089 }
0090 
0091 void ImagePainter::setDefaultSize()
0092 {
0093     if (sizeRefd) {
0094         image->derefSize(size);
0095     }
0096     size = image->size();
0097     sizeRefd = false;
0098 }
0099 
0100 void ImagePainter::paint(int dx, int dy, QPainter *p, int sx, int sy,
0101                          int width, int height)
0102 {
0103     if (!image->mayPaint())
0104         // ### fallback painting in case bg?
0105     {
0106         return;
0107     }
0108 
0109     // Do our lazy ref if needed. Safe due to above
0110     if (!sizeRefd && size != image->size()) {
0111         image->refSize(size);
0112         sizeRefd = true;
0113     }
0114 
0115     PixmapPlane *plane = image->getSize(size);
0116 
0117     if (plane->animProvider) {
0118         // Clip the request ourselves when animating..
0119 
0120         if (width == -1) {
0121             width  = size.width();
0122         }
0123         if (height == -1) {
0124             height = size.height();
0125         }
0126 
0127         QRect clippedRect = QRect(0, 0, size.width(), size.height())
0128                             & QRect(sx, sy, width, height);
0129         plane->animProvider->paint(dx, dy, p, clippedRect.x(), clippedRect.y(),
0130                                    clippedRect.width(), clippedRect.height());
0131         return;
0132     }
0133 
0134     // non-animated, go straight to PixmapPlane; it clips itself
0135     plane->paint(dx, dy, p, sx, sy, width, height);
0136 }
0137 
0138 }
0139