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