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