kpresenter

KPrPicturePreview.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
00004 
00005    The code is based on work of
00006    Copyright (C) 2002 Toshitaka Fujioka <fujioka@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "KPrPicturePreview.h"
00025 
00026 #include <qimage.h>
00027 #include <qpainter.h>
00028 
00029 
00030 KPrPicturePreview::KPrPicturePreview( QWidget* parent, const char* name)
00031 : QFrame( parent, name )
00032 , mirrorType( PM_NORMAL )
00033 , depth( 0 )
00034 , swapRGB( false )
00035 , bright( 0 )
00036 , grayscal( false )
00037 {
00038     setFrameStyle( WinPanel | Sunken );
00039     setMinimumSize( 300, 200 );
00040 }
00041 
00042 
00043 void KPrPicturePreview::drawContents( QPainter *painter )
00044 {
00045     QSize ext = contentsRect().size();
00046 
00047     QPixmap _pix = origPixmap;
00048     QImage img( _pix.convertToImage().smoothScale( ext.width(),ext.height(), QImage::ScaleMin ) );
00049 
00050     bool _horizontal = false;
00051     bool _vertical = false;
00052     if ( mirrorType == PM_HORIZONTAL )
00053         _horizontal = true;
00054     else if ( mirrorType == PM_VERTICAL )
00055         _vertical = true;
00056     else if ( mirrorType == PM_HORIZONTALANDVERTICAL ) {
00057         _horizontal = true;
00058         _vertical = true;
00059     }
00060 
00061     img = img.mirror( _horizontal, _vertical );
00062 
00063     if ( depth != 0 ) {
00064         QImage tmpImg = img.convertDepth( depth );
00065         if ( !tmpImg.isNull() )
00066             img = tmpImg;
00067     }
00068 
00069     if ( swapRGB )
00070         img = img.swapRGB();
00071 
00072 
00073     if ( grayscal ) {
00074         if ( depth == 1 || depth == 8 ) {
00075             for ( int i = 0; i < img.numColors(); ++i ) {
00076                 QRgb rgb = img.color( i );
00077                 int gray = qGray( rgb );
00078                 rgb = qRgb( gray, gray, gray );
00079                 img.setColor( i, rgb );
00080             }
00081         }
00082         else {
00083             int _width = img.width();
00084             int _height = img.height();
00085             int _x = 0;
00086             int _y = 0;
00087 
00088             for ( _x = 0; _x < _width; ++_x ) {
00089                 for ( _y = 0; _y < _height; ++_y ) {
00090                     if ( img.valid( _x, _y ) ) {
00091                         QRgb rgb = img.pixel( _x, _y );
00092                         int gray = qGray( rgb );
00093                         rgb = qRgb( gray, gray, gray );
00094                         img.setPixel( _x, _y, rgb );
00095                     }
00096                 }
00097             }
00098         }
00099     }
00100 
00101 
00102     if ( bright != 0 ) {
00103         if ( depth == 1 || depth == 8 ) {
00104             for ( int i = 0; i < img.numColors(); ++i ) {
00105                 QRgb rgb = img.color( i );
00106                 QColor c( rgb );
00107 
00108                 if ( bright > 0 )
00109                     rgb = c.light( 100 + bright ).rgb();
00110                 else
00111                     rgb = c.dark( 100 + abs( bright ) ).rgb();
00112 
00113                 img.setColor( i, rgb );
00114             }
00115         }
00116         else {
00117             int _width = img.width();
00118             int _height = img.height();
00119             int _x = 0;
00120             int _y = 0;
00121 
00122             for ( _x = 0; _x < _width; ++_x ) {
00123                 for ( _y = 0; _y < _height; ++_y ) {
00124                     if ( img.valid( _x, _y ) ) {
00125                         QRgb rgb = img.pixel( _x, _y );
00126                         QColor c( rgb );
00127 
00128                         if ( bright > 0 )
00129                             rgb = c.light( 100 + bright ).rgb();
00130                         else
00131                             rgb = c.dark( 100 + abs( bright ) ).rgb();
00132 
00133                         img.setPixel( _x, _y, rgb );
00134                     }
00135                 }
00136             }
00137         }
00138     }
00139 
00140     _pix.convertFromImage( img );
00141 
00142     QPixmap tmpPix( _pix.size() );
00143     tmpPix.fill( Qt::white );
00144 
00145     QPainter _p;
00146     _p.begin( &tmpPix );
00147     _p.drawPixmap( 0, 0, _pix );
00148     _p.end();
00149 
00150     QSize _pixSize = _pix.size();
00151     int _x = 0, _y = 0;
00152     int w = _pixSize.width(), h = _pixSize.height();
00153     _x = ( ext.width() - w ) / 2;
00154     _y = ( ext.height() - h ) / 2;
00155 
00156     painter->drawPixmap( _x, _y, tmpPix );
00157 }
00158 
00159 
00160 void KPrPicturePreview::slotNormalPicture()
00161 {
00162     if ( mirrorType != PM_NORMAL )
00163     {
00164         mirrorType = PM_NORMAL;
00165         repaint( false );
00166     }
00167 }
00168 
00169 
00170 void KPrPicturePreview::slotHorizontalMirrorPicture()
00171 {
00172     if ( mirrorType != PM_HORIZONTAL )
00173     {
00174         mirrorType = PM_HORIZONTAL;
00175         repaint( false );
00176     }
00177 }
00178 
00179 
00180 void KPrPicturePreview::slotVerticalMirrorPicture()
00181 {
00182     if ( mirrorType != PM_VERTICAL )
00183     {
00184         mirrorType = PM_VERTICAL;
00185         repaint( false );
00186     }
00187 }
00188 
00189 
00190 void KPrPicturePreview::slotHorizontalAndVerticalMirrorPicture()
00191 {
00192     if ( mirrorType != PM_HORIZONTALANDVERTICAL )
00193     {
00194         mirrorType = PM_HORIZONTALANDVERTICAL;
00195         repaint( false );
00196     }
00197 }
00198 
00199 
00200 void KPrPicturePreview::slotPictureDepth0()
00201 {
00202     if ( depth !=0 )
00203     {
00204         depth = 0;
00205         repaint( false );
00206     }
00207 }
00208 
00209 
00210 void KPrPicturePreview::slotPictureDepth1()
00211 {
00212     if ( depth != 1 )
00213     {
00214         depth = 1;
00215         repaint( false );
00216     }
00217 }
00218 
00219 
00220 void KPrPicturePreview::slotPictureDepth8()
00221 {
00222     if ( depth != 8)
00223     {
00224         depth = 8;
00225         repaint( false );
00226     }
00227 }
00228 
00229 
00230 void KPrPicturePreview::slotPictureDepth16()
00231 {
00232     if ( depth != 16 )
00233     {
00234         depth = 16;
00235         repaint( false );
00236     }
00237 }
00238 
00239 
00240 void KPrPicturePreview::slotPictureDepth32()
00241 {
00242     if ( depth !=32 )
00243     {
00244         depth = 32;
00245         repaint( false );
00246     }
00247 }
00248 
00249 
00250 void KPrPicturePreview::slotSwapRGBPicture( bool _on )
00251 {
00252     if ( swapRGB != _on )
00253     {
00254         swapRGB = _on;
00255         repaint( false );
00256     }
00257 }
00258 
00259 
00260 void KPrPicturePreview::slotGrayscalPicture( bool _on )
00261 {
00262     if ( grayscal != _on )
00263     {
00264         grayscal = _on;
00265         repaint( false );
00266     }
00267 }
00268 
00269 
00270 void KPrPicturePreview::slotBrightValue( int _value )
00271 {
00272     if ( bright != _value )
00273     {
00274         bright = _value;
00275         repaint( false );
00276     }
00277 }
00278 
00279 
00280 void KPrPicturePreview::setDepth( int _depth)
00281 {
00282     if ( _depth != depth )
00283     {
00284         depth = _depth;
00285         repaint( false );
00286     }
00287 }
00288 
00289 
00290 void KPrPicturePreview::setMirrorType (PictureMirrorType _t)
00291 {
00292     if ( mirrorType != _t )
00293     {
00294         mirrorType = _t;
00295         repaint( false );
00296     }
00297 }
00298 
00299 
00300 void KPrPicturePreview::setPicturePixmap(const QPixmap &_pixmap)
00301 {
00302     origPixmap = _pixmap;
00303     repaint( false );
00304 }
00305 
00306 
00307 #include "KPrPicturePreview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys