kpresenter

KPrPicturePreview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
3
4 The code is based on work of
5 Copyright (C) 2002 Toshitaka Fujioka <fujioka@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21*/
22
23#include "KPrPicturePreview.h"
24
25#include <tqimage.h>
26#include <tqpainter.h>
27
28
29KPrPicturePreview::KPrPicturePreview( TQWidget* parent, const char* name)
30: TQFrame( parent, name )
31, mirrorType( PM_NORMAL )
32, depth( 0 )
33, swapRGB( false )
34, bright( 0 )
35, grayscal( false )
36{
37 setFrameStyle( WinPanel | Sunken );
38 setMinimumSize( 300, 200 );
39}
40
41
42void KPrPicturePreview::drawContents( TQPainter *painter )
43{
44 TQSize ext = contentsRect().size();
45
46 TQPixmap _pix = origPixmap;
47 TQImage img( _pix.convertToImage().smoothScale( ext.width(),ext.height(), TQImage::ScaleMin ) );
48
49 bool _horizontal = false;
50 bool _vertical = false;
51 if ( mirrorType == PM_HORIZONTAL )
52 _horizontal = true;
53 else if ( mirrorType == PM_VERTICAL )
54 _vertical = true;
55 else if ( mirrorType == PM_HORIZONTALANDVERTICAL ) {
56 _horizontal = true;
57 _vertical = true;
58 }
59
60 img = img.mirror( _horizontal, _vertical );
61
62 if ( depth != 0 ) {
63 TQImage tmpImg = img.convertDepth( depth );
64 if ( !tmpImg.isNull() )
65 img = tmpImg;
66 }
67
68 if ( swapRGB )
69 img = img.swapRGB();
70
71
72 if ( grayscal ) {
73 if ( depth == 1 || depth == 8 ) {
74 for ( int i = 0; i < img.numColors(); ++i ) {
75 TQRgb rgb = img.color( i );
76 int gray = tqGray( rgb );
77 rgb = tqRgb( gray, gray, gray );
78 img.setColor( i, rgb );
79 }
80 }
81 else {
82 int _width = img.width();
83 int _height = img.height();
84 int _x = 0;
85 int _y = 0;
86
87 for ( _x = 0; _x < _width; ++_x ) {
88 for ( _y = 0; _y < _height; ++_y ) {
89 if ( img.valid( _x, _y ) ) {
90 TQRgb rgb = img.pixel( _x, _y );
91 int gray = tqGray( rgb );
92 rgb = tqRgb( gray, gray, gray );
93 img.setPixel( _x, _y, rgb );
94 }
95 }
96 }
97 }
98 }
99
100
101 if ( bright != 0 ) {
102 if ( depth == 1 || depth == 8 ) {
103 for ( int i = 0; i < img.numColors(); ++i ) {
104 TQRgb rgb = img.color( i );
105 TQColor c( rgb );
106
107 if ( bright > 0 )
108 rgb = c.light( 100 + bright ).rgb();
109 else
110 rgb = c.dark( 100 + abs( bright ) ).rgb();
111
112 img.setColor( i, rgb );
113 }
114 }
115 else {
116 int _width = img.width();
117 int _height = img.height();
118 int _x = 0;
119 int _y = 0;
120
121 for ( _x = 0; _x < _width; ++_x ) {
122 for ( _y = 0; _y < _height; ++_y ) {
123 if ( img.valid( _x, _y ) ) {
124 TQRgb rgb = img.pixel( _x, _y );
125 TQColor c( rgb );
126
127 if ( bright > 0 )
128 rgb = c.light( 100 + bright ).rgb();
129 else
130 rgb = c.dark( 100 + abs( bright ) ).rgb();
131
132 img.setPixel( _x, _y, rgb );
133 }
134 }
135 }
136 }
137 }
138
139 _pix.convertFromImage( img );
140
141 TQPixmap tmpPix( _pix.size() );
142 tmpPix.fill( TQt::white );
143
144 TQPainter _p;
145 _p.begin( &tmpPix );
146 _p.drawPixmap( 0, 0, _pix );
147 _p.end();
148
149 TQSize _pixSize = _pix.size();
150 int _x = 0, _y = 0;
151 int w = _pixSize.width(), h = _pixSize.height();
152 _x = ( ext.width() - w ) / 2;
153 _y = ( ext.height() - h ) / 2;
154
155 painter->drawPixmap( _x, _y, tmpPix );
156}
157
158
159void KPrPicturePreview::slotNormalPicture()
160{
161 if ( mirrorType != PM_NORMAL )
162 {
163 mirrorType = PM_NORMAL;
164 repaint( false );
165 }
166}
167
168
169void KPrPicturePreview::slotHorizontalMirrorPicture()
170{
171 if ( mirrorType != PM_HORIZONTAL )
172 {
173 mirrorType = PM_HORIZONTAL;
174 repaint( false );
175 }
176}
177
178
179void KPrPicturePreview::slotVerticalMirrorPicture()
180{
181 if ( mirrorType != PM_VERTICAL )
182 {
183 mirrorType = PM_VERTICAL;
184 repaint( false );
185 }
186}
187
188
189void KPrPicturePreview::slotHorizontalAndVerticalMirrorPicture()
190{
191 if ( mirrorType != PM_HORIZONTALANDVERTICAL )
192 {
193 mirrorType = PM_HORIZONTALANDVERTICAL;
194 repaint( false );
195 }
196}
197
198
199void KPrPicturePreview::slotPictureDepth0()
200{
201 if ( depth !=0 )
202 {
203 depth = 0;
204 repaint( false );
205 }
206}
207
208
209void KPrPicturePreview::slotPictureDepth1()
210{
211 if ( depth != 1 )
212 {
213 depth = 1;
214 repaint( false );
215 }
216}
217
218
219void KPrPicturePreview::slotPictureDepth8()
220{
221 if ( depth != 8)
222 {
223 depth = 8;
224 repaint( false );
225 }
226}
227
228
229void KPrPicturePreview::slotPictureDepth16()
230{
231 if ( depth != 16 )
232 {
233 depth = 16;
234 repaint( false );
235 }
236}
237
238
239void KPrPicturePreview::slotPictureDepth32()
240{
241 if ( depth !=32 )
242 {
243 depth = 32;
244 repaint( false );
245 }
246}
247
248
249void KPrPicturePreview::slotSwapRGBPicture( bool _on )
250{
251 if ( swapRGB != _on )
252 {
253 swapRGB = _on;
254 repaint( false );
255 }
256}
257
258
259void KPrPicturePreview::slotGrayscalPicture( bool _on )
260{
261 if ( grayscal != _on )
262 {
263 grayscal = _on;
264 repaint( false );
265 }
266}
267
268
269void KPrPicturePreview::slotBrightValue( int _value )
270{
271 if ( bright != _value )
272 {
273 bright = _value;
274 repaint( false );
275 }
276}
277
278
279void KPrPicturePreview::setDepth( int _depth)
280{
281 if ( _depth != depth )
282 {
283 depth = _depth;
284 repaint( false );
285 }
286}
287
288
289void KPrPicturePreview::setMirrorType (PictureMirrorType _t)
290{
291 if ( mirrorType != _t )
292 {
293 mirrorType = _t;
294 repaint( false );
295 }
296}
297
298
299void KPrPicturePreview::setPicturePixmap(const TQPixmap &_pixmap)
300{
301 origPixmap = _pixmap;
302 repaint( false );
303}
304
305
306#include "KPrPicturePreview.moc"