]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQImage.cc
restart qt4 porting
[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 #define y2log_component "qt-ui"
24 #include <ycp/y2log.h>
25
26 #include "utf8.h"
27 #include "YQImage.h"
28
29
30
31 YQImage::YQImage( YWidget *             parent,
32                   const string &        imageFileName,
33                   bool                  animated )
34     : QLabel( (QWidget *) parent->widgetRep() )
35     , YImage( parent, imageFileName, animated )
36 {
37     setWidgetRep( this );
38     setAlignment( Qt::AlignLeft | Qt::AlignTop );
39
40     setScaledContents( false );
41     _pixmapHeight = 0;
42     _pixmapWidth  = 0;
43
44     setImage( imageFileName, animated );
45 }
46
47
48 YQImage::~YQImage()
49 {
50     // NOP
51 }
52
53
54 void
55 YQImage::setImage( const string & fileName, bool animated )
56 {
57     YImage::setImage( fileName, animated );
58     
59     if ( animated )
60     {
61         QMovie movie( fromUTF8( imageFileName() ) );
62         
63         if ( movie.isNull() )
64         {
65             y2error( "Couldn't load animation from %s", imageFileName().c_str() );
66         }
67         else
68         {
69             y2debug( "Loading animation from %s", imageFileName().c_str() );
70             QLabel::setMovie( movie );
71         }
72     }
73     else
74     {
75         QPixmap pixmap( fromUTF8( imageFileName() ) );
76
77         if ( pixmap.isNull() )
78         {
79             y2error( "Couldn't load pixmap from %s", imageFileName().c_str() );
80         }
81         else
82         {
83             if ( autoScale() )
84             {
85                 _pixmapWidth  = 0;
86                 _pixmapHeight = 0;
87             }
88             else
89             {
90                 _pixmapWidth  = pixmap.size().width();
91                 _pixmapHeight = pixmap.size().height();
92             }
93         
94             y2debug( "Loading image from %s (%d x %d)",
95                      imageFileName().c_str(),
96                      pixmap.size().width(),
97                      pixmap.size().height() );
98             
99             QLabel::setPixmap( pixmap );
100         }
101     }
102 }
103
104 void YQImage::setAutoScale( bool newAutoScale )
105 {
106     if ( autoScale() == newAutoScale )
107         return;
108
109     YImage::setAutoScale( newAutoScale );
110     setScaledContents( newAutoScale );
111
112     // Trigger image re-display
113     setImage( imageFileName(), animated() );
114 }
115
116
117 int YQImage::preferredWidth()
118 {
119     if ( hasZeroSize( YD_HORIZ ) )
120         return 0;
121
122     if ( animated() )
123     {
124         // a QMovie doesn't have a size() method, thus use sizeHint() instead.
125         
126         return sizeHint().width();
127     }
128     else
129     {
130         // for non-animated images, the background pixmap is used, thus
131         // sizeHint() will always return ( 0,0 ) - thus, use the internally
132         // stored sizes instead.
133         
134         return _pixmapWidth;
135     }
136 }
137
138
139 int YQImage::preferredHeight()
140 {
141     if ( hasZeroSize( YD_VERT ) )
142         return 0;
143
144     if ( animated() )
145     {
146         // a QMovie doesn't have a size() method, thus use sizeHint() instead.
147
148         return sizeHint().height();
149     }
150     else
151     {
152         // for non-animated images, the background pixmap is used, thus
153         // sizeHint() will always return ( 0,0 ) - thus, use the internally
154         // stored sizes instead.
155         
156         return _pixmapHeight;
157     }
158 }
159
160
161 void YQImage::setSize( int newWidth, int newHeight )
162 {
163     resize( newWidth, newHeight );
164 }
165
166
167 #include "YQImage.moc"