libyui-qt  2.52.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 "YQUI.h"
36 #include "YQImage.h"
37 
38 using std::string;
39 
40 
41 
42 YQImage::YQImage( YWidget * parent,
43  const string & imageFileName,
44  bool animated )
45  : QLabel( (QWidget *) parent->widgetRep() )
46  , YImage( parent, imageFileName, animated )
47 {
48  setWidgetRep( this );
49  setAlignment( Qt::AlignLeft | Qt::AlignTop );
50 
51  setScaledContents( false );
52  _pixmapHeight = 0;
53  _pixmapWidth = 0;
54 
55  setImage( imageFileName, animated );
56 }
57 
58 
60 {
61  // NOP
62 }
63 
64 
65 void
66 YQImage::setImage( const string & fileName, bool animated )
67 {
68  YImage::setImage ( fileName, animated );
69 
70  if ( animated )
71  {
72  QMovie movie ( fromUTF8 ( imageFileName() ) );
73 
74  if ( movie.isValid() )
75  {
76  yuiError() << "Couldn't load animation from " << imageFileName() << endl;
77  }
78  else
79  {
80  yuiDebug() << "Loading animation from " << imageFileName() << endl;
81  QLabel::setMovie ( &movie );
82  }
83  }
84  else
85  {
86  QPixmap pixmap;
87 
88  if ( fromUTF8( imageFileName() ).startsWith( "/" ) )
89  {
90  // Absolute path specified - bypass the icon loader which might try
91  // to use a theme icon or the compiled-in Qt resources
92 
93  yuiDebug() << "Loading pixmap from absolute path: \""
94  << imageFileName() << "\"" << endl;
95 
96  pixmap = QPixmap( fromUTF8 ( imageFileName() ) );
97  }
98  else
99  {
100  yuiDebug() << "Using icon loader for \"" << imageFileName() << "\"" << endl;
101 
102  pixmap = YQUI::ui()->loadIcon( imageFileName() ).pixmap( 22 );
103  }
104 
105  if ( pixmap.isNull() )
106  {
107  yuiError() << "Couldn't load pixmap from \""
108  << imageFileName() << "\"" << endl;
109  }
110  else
111  {
112  if ( autoScale() )
113  {
114  QImage scaledImg = pixmap.toImage();
115  scaledImg = scaledImg.scaled( this->width(), this->height(), Qt::KeepAspectRatio );
116  pixmap = pixmap.fromImage( scaledImg );
117  }
118 
119  _pixmapWidth = pixmap.size().width();
120  _pixmapHeight = pixmap.size().height();
121 
122  QLabel::setPixmap ( pixmap );
123  }
124  }
125 }
126 
127 
128 void YQImage::setAutoScale( bool newAutoScale )
129 {
130  if ( autoScale() == newAutoScale )
131  return;
132 
133  YImage::setAutoScale( newAutoScale );
134  setScaledContents( newAutoScale );
135 
136  // Trigger image re-display
137  setImage( imageFileName(), animated() );
138 }
139 
140 
142 {
143  if ( hasZeroSize( YD_HORIZ ) )
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().width();
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 _pixmapWidth;
159  }
160 }
161 
162 
164 {
165  if ( hasZeroSize( YD_VERT ) )
166  return 0;
167 
168  if ( animated() )
169  {
170  // a QMovie doesn't have a size() method, thus use sizeHint() instead.
171 
172  return sizeHint().height();
173  }
174  else
175  {
176  // for non-animated images, the background pixmap is used, thus
177  // sizeHint() will always return ( 0,0 ) - thus, use the internally
178  // stored sizes instead.
179 
180  return _pixmapHeight;
181  }
182 }
183 
184 
185 void YQImage::setSize( int newWidth, int newHeight )
186 {
187  resize( newWidth, newHeight );
188 }
189 
190 
191 void YQImage::setEnabled( bool enable )
192 {
193  yuiDebug() << "setEnabled: " << enable << endl;
194 
195  if (enable)
196  {
197  setImage( imageFileName(), animated() );
198  }
199  else
200  {
201  // Trigger image re-display
202  QPixmap pixmap( fromUTF8( imageFileName() ) );
203  QIcon icon(pixmap);
204  QLabel::setPixmap( icon.pixmap( pixmap.size(), QIcon::Disabled, QIcon::Off) );
205  }
206 }
207 
208 
virtual void setImage(const std::string &imageFileName, bool animated=false)
Set and display a new image.
Definition: YQImage.cc:66
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQImage.cc:163
virtual void setAutoScale(bool autoScale=true)
Make the image fit into the available space.
Definition: YQImage.cc:128
YQImage(YWidget *parent, const std::string &imageFileName, bool animated=false)
Constructor.
Definition: YQImage.cc:42
QIcon loadIcon(const string &iconName) const
Load an icon.
Definition: YQUI.cc:708
virtual ~YQImage()
Destructor.
Definition: YQImage.cc:59
virtual void setEnabled(bool enabled)
if false, the image will be displayed in gray
Definition: YQImage.cc:191
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQImage.cc:141
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQImage.cc:185
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:83