libyui-qt  2.49.2
YQImage.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQImage.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <unistd.h>
27 #include <qpixmap.h>
28 #include <qmovie.h>
29 #include <qlabel.h>
30 #include <QIcon>
31 #define YUILogComponent "qt-ui"
32 #include <yui/YUILog.h>
33 
34 #include "utf8.h"
35 #include "YQImage.h"
36 
37 
38 
39 YQImage::YQImage( YWidget * parent,
40  const std::string & imageFileName,
41  bool animated )
42  : QLabel( (QWidget *) parent->widgetRep() )
43  , YImage( parent, imageFileName, animated )
44 {
45  setWidgetRep( this );
46  setAlignment( Qt::AlignLeft | Qt::AlignTop );
47 
48  setScaledContents( false );
49  _pixmapHeight = 0;
50  _pixmapWidth = 0;
51 
52  setImage( imageFileName, animated );
53 }
54 
55 
57 {
58  // NOP
59 }
60 
61 
62 void
63 YQImage::setImage( const std::string & fileName, bool animated )
64 {
65  YImage::setImage ( fileName, animated );
66 
67  if ( animated )
68  {
69  QMovie movie ( fromUTF8 ( imageFileName() ) );
70 
71  if ( movie.isValid() )
72  {
73  yuiError() << "Couldn't load animation from " << imageFileName() << std::endl;
74  }
75  else
76  {
77  yuiDebug() << "Loading animation from " << imageFileName() << std::endl;
78  QLabel::setMovie ( &movie );
79  }
80  }
81  else
82  {
83  QPixmap pixmap ( fromUTF8 ( imageFileName() ) );
84 
85  if ( pixmap.isNull() )
86  {
87  yuiError() << "Couldn't load pixmap from " << imageFileName() << std::endl;
88  }
89  else
90  {
91  if ( autoScale() )
92  {
93  QImage scaledImg = pixmap.toImage();
94  scaledImg = scaledImg.scaled ( this->width(), this->height(), Qt::KeepAspectRatio );
95  pixmap = pixmap.fromImage ( scaledImg );
96  }
97  _pixmapWidth = pixmap.size().width();
98  _pixmapHeight = pixmap.size().height();
99 
100  yuiDebug() << "Loading image from " << imageFileName()
101  << " (" << pixmap.size().width() << " x " << pixmap.size().height() << ")"
102  << std::endl;
103 
104  QLabel::setPixmap ( pixmap );
105  }
106  }
107 }
108 
109 void YQImage::setAutoScale( bool newAutoScale )
110 {
111  if ( autoScale() == newAutoScale )
112  return;
113 
114  YImage::setAutoScale( newAutoScale );
115  setScaledContents( newAutoScale );
116 
117  // Trigger image re-display
118  setImage( imageFileName(), animated() );
119 }
120 
121 
123 {
124  if ( hasZeroSize( YD_HORIZ ) )
125  return 0;
126 
127  if ( animated() )
128  {
129  // a QMovie doesn't have a size() method, thus use sizeHint() instead.
130 
131  return sizeHint().width();
132  }
133  else
134  {
135  // for non-animated images, the background pixmap is used, thus
136  // sizeHint() will always return ( 0,0 ) - thus, use the internally
137  // stored sizes instead.
138 
139  return _pixmapWidth;
140  }
141 }
142 
143 
145 {
146  if ( hasZeroSize( YD_VERT ) )
147  return 0;
148 
149  if ( animated() )
150  {
151  // a QMovie doesn't have a size() method, thus use sizeHint() instead.
152 
153  return sizeHint().height();
154  }
155  else
156  {
157  // for non-animated images, the background pixmap is used, thus
158  // sizeHint() will always return ( 0,0 ) - thus, use the internally
159  // stored sizes instead.
160 
161  return _pixmapHeight;
162  }
163 }
164 
165 
166 void YQImage::setSize( int newWidth, int newHeight )
167 {
168  resize( newWidth, newHeight );
169 }
170 
171 void YQImage::setEnabled( bool enable )
172 {
173  yuiDebug() << "setEnabled: " << enable << std::endl;
174 
175  if (enable)
176  {
177  setImage( imageFileName(), animated() );
178  }
179  else
180  {
181  // Trigger image re-display
182  QPixmap pixmap( fromUTF8( imageFileName() ) );
183  QIcon icon(pixmap);
184  QLabel::setPixmap( icon.pixmap( pixmap.size(), QIcon::Disabled, QIcon::Off) );
185  }
186 }
187 
188 #include "YQImage.moc"
virtual void setImage(const std::string &imageFileName, bool animated=false)
Set and display a new image.
Definition: YQImage.cc:63
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQImage.cc:144
virtual void setAutoScale(bool autoScale=true)
Make the image fit into the available space.
Definition: YQImage.cc:109
YQImage(YWidget *parent, const std::string &imageFileName, bool animated=false)
Constructor.
Definition: YQImage.cc:39
virtual ~YQImage()
Destructor.
Definition: YQImage.cc:56
virtual void setEnabled(bool enabled)
if false, the image will be displayed in gray
Definition: YQImage.cc:171
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQImage.cc:122
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQImage.cc:166