libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQImage.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       YQImage.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <unistd.h>
00027 #include <qpixmap.h>
00028 #include <qmovie.h>
00029 #include <qlabel.h>
00030 #include <QIcon>
00031 #define YUILogComponent "qt-ui"
00032 #include <yui/YUILog.h>
00033 
00034 #include "utf8.h"
00035 #include "YQImage.h"
00036 
00037 
00038 
00039 YQImage::YQImage( YWidget *             parent,
00040                   const std::string &   imageFileName,
00041                   bool                  animated )
00042     : QLabel( (QWidget *) parent->widgetRep() )
00043     , YImage( parent, imageFileName, animated )
00044 {
00045     setWidgetRep( this );
00046     setAlignment( Qt::AlignLeft | Qt::AlignTop );
00047 
00048     setScaledContents( false );
00049     _pixmapHeight = 0;
00050     _pixmapWidth  = 0;
00051 
00052     setImage( imageFileName, animated );
00053 }
00054 
00055 
00056 YQImage::~YQImage()
00057 {
00058     // NOP
00059 }
00060 
00061 
00062 void
00063 YQImage::setImage( const std::string & fileName, bool animated )
00064 {
00065   YImage::setImage ( fileName, animated );
00066 
00067   if ( animated )
00068   {
00069     QMovie movie ( fromUTF8 ( imageFileName() ) );
00070 
00071     if ( movie.isValid() )
00072     {
00073       yuiError() << "Couldn't load animation from " << imageFileName() << std::endl;
00074     }
00075     else
00076     {
00077       yuiDebug() << "Loading animation from " << imageFileName() << std::endl;
00078       QLabel::setMovie ( &movie );
00079     }
00080   }
00081   else
00082   {
00083     QPixmap pixmap ( fromUTF8 ( imageFileName() ) );
00084 
00085     if ( pixmap.isNull() )
00086     {
00087       yuiError() << "Couldn't load pixmap from " << imageFileName() << std::endl;
00088     }
00089     else
00090     {
00091       if ( autoScale() )
00092       {
00093         QImage scaledImg = pixmap.toImage();
00094         scaledImg = scaledImg.scaled ( this->width(), this->height(), Qt::KeepAspectRatio );
00095         pixmap = pixmap.fromImage ( scaledImg );
00096       }
00097       _pixmapWidth  = pixmap.size().width();
00098       _pixmapHeight = pixmap.size().height();
00099 
00100       yuiDebug() << "Loading image from " << imageFileName()
00101                  << " (" << pixmap.size().width() << " x " << pixmap.size().height() << ")"
00102                  << std::endl;
00103 
00104       QLabel::setPixmap ( pixmap );
00105     }
00106   }
00107 }
00108 
00109 void YQImage::setAutoScale( bool newAutoScale )
00110 {
00111     if ( autoScale() == newAutoScale )
00112         return;
00113 
00114     YImage::setAutoScale( newAutoScale );
00115     setScaledContents( newAutoScale );
00116 
00117     // Trigger image re-display
00118     setImage( imageFileName(), animated() );
00119 }
00120 
00121 
00122 int YQImage::preferredWidth()
00123 {
00124     if ( hasZeroSize( YD_HORIZ ) )
00125         return 0;
00126 
00127     if ( animated() )
00128     {
00129         // a QMovie doesn't have a size() method, thus use sizeHint() instead.
00130 
00131         return sizeHint().width();
00132     }
00133     else
00134     {
00135         // for non-animated images, the background pixmap is used, thus
00136         // sizeHint() will always return ( 0,0 ) - thus, use the internally
00137         // stored sizes instead.
00138 
00139         return _pixmapWidth;
00140     }
00141 }
00142 
00143 
00144 int YQImage::preferredHeight()
00145 {
00146     if ( hasZeroSize( YD_VERT ) )
00147         return 0;
00148 
00149     if ( animated() )
00150     {
00151         // a QMovie doesn't have a size() method, thus use sizeHint() instead.
00152 
00153         return sizeHint().height();
00154     }
00155     else
00156     {
00157         // for non-animated images, the background pixmap is used, thus
00158         // sizeHint() will always return ( 0,0 ) - thus, use the internally
00159         // stored sizes instead.
00160 
00161         return _pixmapHeight;
00162     }
00163 }
00164 
00165 
00166 void YQImage::setSize( int newWidth, int newHeight )
00167 {
00168     resize( newWidth, newHeight );
00169 }
00170 
00171 void YQImage::setEnabled( bool enable )
00172 {
00173     yuiDebug() << "setEnabled: " << enable << std::endl;
00174 
00175     if (enable)
00176     {
00177         setImage( imageFileName(), animated() );
00178     }
00179     else
00180     {
00181         // Trigger image re-display
00182         QPixmap pixmap( fromUTF8( imageFileName() ) );
00183         QIcon icon(pixmap);
00184         QLabel::setPixmap( icon.pixmap( pixmap.size(), QIcon::Disabled, QIcon::Off) );
00185     }
00186 }
00187 
00188 #include "YQImage.moc"
 All Classes Functions Variables