ImageBox.cpp
Go to the documentation of this file.00001 #include "ImageBox.h"
00002 #include "icons/void.xpm"
00003
00004 ImageBox::ImageBox( const QString& title, const QString& hiddenLabel, const QString& hiddenTooltip, QWidget *parent = 0, const char* name = 0 )
00005 : QVGroupBox( title, parent, name ), imageWidth( -1 ), imageHeight( -1 ) {
00006 imageStack = new QWidgetStack( this, "ImageStack" );
00007 imageStack->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00008
00009 imageWrapper = new QVBox( imageStack, "ImageWrapper" );
00010 image = new QLabel( imageWrapper, "Image" );
00011 image->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Expanding ) );
00012 image->setAlignment( AlignHCenter | AlignVCenter );
00013 image->setScaledContents( true );
00014
00015 imageButton = new QPushButton( hiddenLabel, imageStack, "ImageButton" );
00016 imageButton->installEventFilter( this );
00017 QToolTip::add( imageButton, hiddenTooltip );
00018 connect( imageButton, SIGNAL( clicked() ), this, SLOT( revealImage() ) );
00019 imageStack->addWidget( imageWrapper, 0 );
00020 imageStack->addWidget( imageButton, 1 );
00021 }
00022
00023 ImageBox::~ImageBox() {
00024 }
00025
00026 QSize ImageBox::sizeHint() const {
00027 int width = ( imageWidth == -1 ? 200 : height() * imageWidth / imageHeight );
00028 return( QSize( width, height() ) );
00029 }
00030
00031 void ImageBox::show() {
00032 QVGroupBox::show();
00033 restartMovie();
00034 }
00035
00036 bool ImageBox::isImageRevealed() const {
00037 return( imageStack->visibleWidget() == imageStack->widget( 0 ) );
00038 }
00039
00040 bool ImageBox::containsValidImage() const {
00041
00042
00043 return( imageWidth != -1 );
00044 }
00045
00046 void ImageBox::revealImage() {
00047 imageStack->raiseWidget( 0 );
00048 restartMovie();
00049 }
00050
00051 void ImageBox::hideImage() {
00052 imageStack->raiseWidget( 1 );
00053 }
00054
00055 void ImageBox::restartMovie() const {
00056 QMovie* movie = image->movie();
00057 if( movie )
00058 movie->restart();
00059 }
00060
00061 void ImageBox::setImage( const QString& path ) {
00062 imageWidth = imageHeight = -1;
00063 if( !path.isNull() ) {
00064 QFileInfo info( path );
00065 if( info.exists() ) {
00066 const QString& format = QPixmap::imageFormat( path );
00067 if( format == "GIF" || format == "PNG" ) {
00068 const QPixmap& pixmap( path );
00069 imageWidth = pixmap.width();
00070 imageHeight = pixmap.height();
00071 if( format == "GIF" ) {
00072 const QMovie& movie( path );
00073 image->setMovie( movie );
00074 }
00075 else if( format == "PNG" ) {
00076 image->setPixmap( pixmap );
00077 }
00078 show();
00079 setFixedWidth( sizeHint().width() );
00080 }
00081 }
00082 }
00083
00084 if( imageWidth == -1 ) {
00085 image->setPixmap( ZPIXMAP( void_xpm ) );
00086 hide();
00087 }
00088 }
00089
00090 void ImageBox::resizeEvent( QResizeEvent* evt ) {
00091 QVGroupBox::resizeEvent( evt );
00092 if( evt->size().width() != sizeHint().width() )
00093 setFixedWidth( sizeHint().width() );
00094 }