1 /*
2     Large 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 "pixmapplane.h"
26 
27 #include <QPainter>
28 
29 namespace khtmlImLoad
30 {
31 
paint(int dx,int dy,QPainter * p,int sx,int sy,int sWidth,int sHeight)32 void PixmapPlane::paint(int dx, int dy, QPainter *p,
33                         int sx, int sy, int sWidth, int sHeight)
34 {
35     //Do some basic clipping, discarding invalid requests and adjusting sizes of others.
36     if (sy >= (int)height) {
37         return;
38     }
39     if (sx >= (int)width) {
40         return;
41     }
42 
43     if (sWidth == -1) {
44         sWidth = width;
45     }
46 
47     if (sHeight == -1) {
48         sHeight = height;
49     }
50 
51     unsigned int ey = sy + sHeight - 1;
52     if (ey > height - 1) {
53         ey = height - 1;
54     }
55 
56     unsigned int ex = sx + sWidth - 1;
57     if (ex > width - 1) {
58         ex = width - 1;
59     }
60 
61     sHeight = ey - sy + 1;
62     sWidth  = ex - sx + 1;
63 
64     //Calculate the range of tiles to paint, in both directions
65     unsigned int startTileY = sy / Tile::TileSize;
66     unsigned int endTileY   = ey / Tile::TileSize;
67 
68     unsigned int startTileX = sx / Tile::TileSize;
69     unsigned int endTileX   = ex / Tile::TileSize;
70 
71     //Walk through all the rows
72     unsigned int paintY = dy;
73     for (unsigned int tileY = startTileY; tileY <= endTileY; ++tileY) {
74         //see how much we have to paint -- end points are different
75         unsigned int startY = 0;
76         unsigned int endY   = Tile::TileSize - 1;
77 
78         if (tileY == startTileY) {
79             startY = sy % Tile::TileSize;
80         }
81 
82         if (tileY == endTileY) {
83             endY   = ey % Tile::TileSize;
84         }
85 
86         unsigned int paintHeight = endY - startY + 1;
87 
88         //Now through some columns
89         unsigned int paintX = dx;
90         for (unsigned int tileX = startTileX; tileX <= endTileX; ++tileX) {
91             //calculate the horizontal size. Some redundancy here,
92             //since these are the same for all rows, but I'd rather
93             //avoid heap allocation or alloca..
94             unsigned int startX = 0;
95             unsigned int endX   = Tile::TileSize - 1;
96 
97             if (tileX == startTileX) {
98                 startX = sx % Tile::TileSize;
99             }
100 
101             if (tileX == endTileX) {
102                 endX   = ex % Tile::TileSize;
103             }
104 
105             int paintWidth = endX - startX + 1;
106 
107             //Update from image plane if need be
108             PixmapTile &tile = tiles.at(tileX, tileY);
109             if (!parent->isUpToDate(tileX, tileY, &tile)) {
110                 parent->ensureUpToDate(tileX, tileY, &tile);
111             }
112 
113             //Draw as much as we have
114             if (tile.pixmap) {
115                 //Scan the versions to see how much to paint.
116                 unsigned int h = 0;
117                 for (int checkY = startY; checkY < Tile::TileSize && tile.versions[checkY]; ++checkY) {
118                     ++h;
119                 }
120 
121                 //Draw it, if there is anything (note: Qt would interpret 0 as everything)
122                 if (h)
123                     p->drawPixmap(paintX, paintY, *tile.pixmap, startX, startY,
124                                   paintWidth, qMin(h, paintHeight));
125             }
126             paintX += paintWidth;
127         }
128         paintY += paintHeight;
129     }
130 }
131 
flushCache()132 void PixmapPlane::flushCache()
133 {
134     parent->flushCache();
135     for (unsigned tileX = 0; tileX < tilesWidth; ++tileX) {
136         for (unsigned tileY = 0; tileY < tilesHeight; ++tileY) {
137             PixmapTile &pixTile = tiles.at(tileX, tileY);
138             if (pixTile.pixmap) {
139                 ImageManager::pixmapCache()->removeEntry(&pixTile);
140             }
141         }
142     }
143 }
144 
145 }
146 
147