Added qanimationlabel to show loading icon while loading stuff form server
[meego-garage:garage-netbook-ui.git] / src / qanimationlabel.cpp
1 /*
2  * Copyright (c) 2009 Nokia Corporation
3  */
4  
5 #include "qanimationlabel.h"
6 #include <QDebug>
7
8 QAnimationLabel::QAnimationLabel(QWidget* parent) : QWidget(parent) {
9         init(":/loading.gif", parent->sizeHint(), tr("Loading ..."));
10         start();
11 }
12
13 QAnimationLabel::QAnimationLabel(QString animationPath,
14                                  QString text,
15                                  QWidget* parent) : QWidget(parent) {
16         init(animationPath, parent->sizeHint(), text);
17         start();
18 }
19  
20 QAnimationLabel::QAnimationLabel(QString animationPath,
21                                  QSize size,
22                                  QString text,
23                                  QWidget* parent) : QWidget(parent) {
24         init(animationPath, size, text);
25         start();
26 }
27  
28 QAnimationLabel::~QAnimationLabel() {
29         _container->deleteLater();
30         _animation->deleteLater();
31 }
32  
33 void QAnimationLabel::init(const QString& animationPath,
34                            const QSize& size,
35                            const QString& text) {
36         /* We'll create the QMovie for the animation */
37         _animation = new QMovie(animationPath);
38         QSize s = size;
39         /* If the size is empty we'll try to detect it.*/
40         if(s.isEmpty()) {
41                 /* Go to the first frame.*/
42                 _animation->jumpToNextFrame();
43                 /* Take the size from there. */
44                 s = _animation->currentPixmap().size();
45                 /* Go back to the beginning. */
46                 _animation->jumpToFrame(0);
47         }
48         /* We're not automatically shown, so lets hide. */
49         setHidden(true);
50         /* We need a container for the QMovie, let's use QLabel */
51         _container = new QLabel(this);
52         /*
53          * We'll set a fixed size to the QLabel
54          * because we don't want to be resized
55          */
56         //_container->setFixedSize(s);
57         /* To get the movie displayed on the QLabel */
58         _container->setMovie(_animation);
59         /* To get the QLabel displayed we'll use a layout */
60         QVBoxLayout* layout = new QVBoxLayout(this);
61         /* Remove the all the extra space. */
62         layout->setSpacing(5);
63         layout->setMargin(5);
64         layout->setSizeConstraint(QLayout::SetNoConstraint);
65         /* Add the movie container to the layout */
66         //layout->addWidget(_container);
67         layout->addWidget(_container, 0, Qt::AlignHCenter);
68         /* Add text */
69         if (!text.isEmpty()) {
70             _text = new QLabel(this);
71             _text->setText(text);
72             _text->setMinimumWidth(s.width());
73             _text->setAlignment(Qt::AlignHCenter);
74             _text->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
75             layout->addWidget(_text, 0, Qt::AlignHCenter);
76         } else {
77             _text = 0;
78         }
79         /* Set the layout as our layout */
80         setLayout(layout);
81         /* Set our size fixed. */
82         //setFixedSize(s);
83         QWidget * parent = this->parentWidget();        
84         qDebug() << "size of parent" << parent->size();
85         qDebug() << "size of QAnimationLabel" << this->size();
86         qDebug() << "size of layout" << layout->minimumSize() << layout->maximumSize();
87         qDebug() << "size of container" << _container->size();
88         qDebug() << "size of text" << _text->size();
89 }
90  
91  
92 void QAnimationLabel::start() {
93         /* Let's check if the movie can be started. */
94         if(!this->_animation.isNull() &&
95            (this->_animation->state() == QMovie::NotRunning ||
96             this->_animation->state() == QMovie::Paused)) {
97                 /* It can so we'll start the animation... */
98                 this->_animation->start();
99                 /* ...and reveal ourself. */
100                 this->setHidden(false);
101         }
102 }
103  
104  
105 void QAnimationLabel::stop() {
106         /* Check if the animation can be stopped. */
107         if(!this->_animation.isNull()) {
108                 if(this->_animation->state() == QMovie::Running) {
109                         /* It can so we'll stop the animation... */
110                         this->_animation->stop();
111                         /* ...and hide. */
112                         this->setHidden(true);
113                 }
114         }
115 }