libyui-qt  2.49.11
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;
84  if (QIcon::hasThemeIcon(imageFileName().c_str()))
85  {
86  yuiDebug() << "Trying theme icon from " << imageFileName() << std::endl;
87  QIcon icon = QIcon::fromTheme(imageFileName().c_str());
88  pixmap = icon.pixmap(22);
89  }
90  else
91  {
92  pixmap = QPixmap( fromUTF8 ( imageFileName() ) );
93  }
94 
95  if ( pixmap.isNull() )
96  {
97  yuiError() << "Couldn't load pixmap from " << imageFileName() << std::endl;
98  }
99  else
100  {
101  if ( autoScale() )
102  {
103  QImage scaledImg = pixmap.toImage();
104  scaledImg = scaledImg.scaled ( this->width(), this->height(), Qt::KeepAspectRatio );
105  pixmap = pixmap.fromImage ( scaledImg );
106  }
107  _pixmapWidth = pixmap.size().width();
108  _pixmapHeight = pixmap.size().height();
109 
110  yuiDebug() << "Loading image from " << imageFileName()
111  << " (" << pixmap.size().width() << " x " << pixmap.size().height() << ")"
112  << std::endl;
113 
114  QLabel::setPixmap ( pixmap );
115  }
116  }
117 }
118 
119 void YQImage::setAutoScale( bool newAutoScale )
120 {
121  if ( autoScale() == newAutoScale )
122  return;
123 
124  YImage::setAutoScale( newAutoScale );
125  setScaledContents( newAutoScale );
126 
127  // Trigger image re-display
128  setImage( imageFileName(), animated() );
129 }
130 
131 
133 {
134  if ( hasZeroSize( YD_HORIZ ) )
135  return 0;
136 
137  if ( animated() )
138  {
139  // a QMovie doesn't have a size() method, thus use sizeHint() instead.
140 
141  return sizeHint().width();
142  }
143  else
144  {
145  // for non-animated images, the background pixmap is used, thus
146  // sizeHint() will always return ( 0,0 ) - thus, use the internally
147  // stored sizes instead.
148 
149  return _pixmapWidth;
150  }
151 }
152 
153 
155 {
156  if ( hasZeroSize( YD_VERT ) )
157  return 0;
158 
159  if ( animated() )
160  {
161  // a QMovie doesn't have a size() method, thus use sizeHint() instead.
162 
163  return sizeHint().height();
164  }
165  else
166  {
167  // for non-animated images, the background pixmap is used, thus
168  // sizeHint() will always return ( 0,0 ) - thus, use the internally
169  // stored sizes instead.
170 
171  return _pixmapHeight;
172  }
173 }
174 
175 
176 void YQImage::setSize( int newWidth, int newHeight )
177 {
178  resize( newWidth, newHeight );
179 }
180 
181 void YQImage::setEnabled( bool enable )
182 {
183  yuiDebug() << "setEnabled: " << enable << std::endl;
184 
185  if (enable)
186  {
187  setImage( imageFileName(), animated() );
188  }
189  else
190  {
191  // Trigger image re-display
192  QPixmap pixmap( fromUTF8( imageFileName() ) );
193  QIcon icon(pixmap);
194  QLabel::setPixmap( icon.pixmap( pixmap.size(), QIcon::Disabled, QIcon::Off) );
195  }
196 }
197 
198 
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:154
virtual void setAutoScale(bool autoScale=true)
Make the image fit into the available space.
Definition: YQImage.cc:119
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:181
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQImage.cc:132
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQImage.cc:176