]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQImage.cc
Duncan, for you
[duncan/yast2-qt4.git] / src / YQImage.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQImage.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <unistd.h>
21 #include <qpixmap.h>
22 #include <qmovie.h>
23 #include <QIcon>
24 //Added by qt3to4:
25 #include <qlabel.h>
26 #define y2log_component "qt-ui"
27 #include <ycp/y2log.h>
28
29 #include "utf8.h"
30 #include "YQImage.h"
31
32
33
34 YQImage::YQImage( YWidget *             parent,
35                   const string &        imageFileName,
36                   bool                  animated )
37     : QLabel( (QWidget *) parent->widgetRep() )
38     , YImage( parent, imageFileName, animated )
39 {
40     setWidgetRep( this );
41     setAlignment( Qt::AlignLeft | Qt::AlignTop );
42
43     setScaledContents( false );
44     _pixmapHeight = 0;
45     _pixmapWidth  = 0;
46
47     setImage( imageFileName, animated );
48 }
49
50
51 YQImage::~YQImage()
52 {
53     // NOP
54 }
55
56
57 void
58 YQImage::setImage( const string & fileName, bool animated )
59 {
60     YImage::setImage( fileName, animated );
61     
62     if ( animated )
63     {
64         QMovie movie( fromUTF8( imageFileName() ) );
65         
66         if ( movie.isValid() )
67         {
68             y2error( "Couldn't load animation from %s", imageFileName().c_str() );
69         }
70         else
71         {
72             y2debug( "Loading animation from %s", imageFileName().c_str() );
73             QLabel::setMovie( &movie );
74         }
75     }
76     else
77     {
78         QPixmap pixmap( fromUTF8( imageFileName() ) );
79
80         if ( pixmap.isNull() )
81         {
82             y2error( "Couldn't load pixmap from %s", imageFileName().c_str() );
83         }
84         else
85         {
86             if ( autoScale() )
87             {
88                 _pixmapWidth  = 0;
89                 _pixmapHeight = 0;
90             }
91             else
92             {
93                 _pixmapWidth  = pixmap.size().width();
94                 _pixmapHeight = pixmap.size().height();
95             }
96         
97             y2debug( "Loading image from %s (%d x %d)",
98                      imageFileName().c_str(),
99                      pixmap.size().width(),
100                      pixmap.size().height() );
101             
102             QLabel::setPixmap( pixmap );
103         }
104     }
105 }
106
107 void YQImage::setAutoScale( bool newAutoScale )
108 {
109     if ( autoScale() == newAutoScale )
110         return;
111
112     YImage::setAutoScale( newAutoScale );
113     setScaledContents( newAutoScale );
114
115     // Trigger image re-display
116     setImage( imageFileName(), animated() );
117 }
118
119
120 int YQImage::preferredWidth()
121 {
122     if ( hasZeroSize( YD_HORIZ ) )
123         return 0;
124
125     if ( animated() )
126     {
127         // a QMovie doesn't have a size() method, thus use sizeHint() instead.
128         
129         return sizeHint().width();
130     }
131     else
132     {
133         // for non-animated images, the background pixmap is used, thus
134         // sizeHint() will always return ( 0,0 ) - thus, use the internally
135         // stored sizes instead.
136         
137         return _pixmapWidth;
138     }
139 }
140
141
142 int YQImage::preferredHeight()
143 {
144     if ( hasZeroSize( YD_VERT ) )
145         return 0;
146
147     if ( animated() )
148     {
149         // a QMovie doesn't have a size() method, thus use sizeHint() instead.
150
151         return sizeHint().height();
152     }
153     else
154     {
155         // for non-animated images, the background pixmap is used, thus
156         // sizeHint() will always return ( 0,0 ) - thus, use the internally
157         // stored sizes instead.
158         
159         return _pixmapHeight;
160     }
161 }
162
163
164 void YQImage::setSize( int newWidth, int newHeight )
165 {
166     resize( newWidth, newHeight );
167 }
168
169 void YQImage::setEnabled( bool enable )
170 {
171    qDebug("setEnabled %d", enable);
172    if (enable)
173       setImage( imageFileName(), animated() );
174    else {
175       // Trigger image re-display
176       QPixmap pixmap( fromUTF8( imageFileName() ) );
177       QIcon icon(pixmap);
178       QLabel::setPixmap( icon.pixmap( pixmap.size(), QIcon::Disabled, QIcon::Off) );
179    }
180 }
181
182 #include "YQImage.moc"