summaryrefslogtreecommitdiffstats
path: root/tdeui/kanimwidget.cpp
blob: 899ba7bd37deb6fcb90f5e7b7b6aeaff49bafa4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// -*- c-basic-offset: 2 -*-

/* This file is part of the KDE libraries
   Copyright (C) 2000 Kurt Granroth <[email protected]>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include <kanimwidget.h>
#include <tqpixmap.h>
#include <tqtimer.h>
#include <tqpainter.h>
#include <tqimage.h>
#include <ktoolbar.h>
#include <kdebug.h>
#include <kiconloader.h>

class KAnimWidgetPrivate
{
public:
  bool                   loadingCompleted : 1;
  bool                   initDone         : 1;
  bool                   transparent      : 1;
  int                    frames;
  int                    current_frame;
  TQPixmap                pixmap;
  TQTimer                 timer;
  TQString                icon_name;
  int                    size;
};

KAnimWidget::KAnimWidget( const TQString& icons, int size, TQWidget *parent,
                          const char *name )
  : TQFrame( parent, name ),
    d( new KAnimWidgetPrivate )
{
  connect( &d->timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotTimerUpdate()));

  if (parent && parent->inherits( "KToolBar" ))
    connect(parent, TQT_SIGNAL(modechange()), this, TQT_SLOT(updateIcons()));

  d->loadingCompleted = false;
  d->size = size;
  d->initDone = false;
  setIcons( icons );
  setFrameStyle( StyledPanel | Sunken );
}

KAnimWidget::~KAnimWidget()
{
  d->timer.stop();

  delete d; d = 0;
}

void KAnimWidget::start()
{
  d->current_frame = 0;
  d->timer.start( 50 );
}

void KAnimWidget::stop()
{
  d->current_frame = 0;
  d->timer.stop();
  repaint();
}

void KAnimWidget::setSize( int size )
{
  if ( d->size == size )
    return;

  d->size = size;
  updateIcons();
}

void KAnimWidget::setIcons( const TQString& icons )
{
  if ( d->icon_name == icons )
    return;

  d->icon_name = icons;
  updateIcons();
}

TQString KAnimWidget::icons( ) const
{
   return d->icon_name;
}

int KAnimWidget::size( ) const
{
   return d->size;
}


void KAnimWidget::showEvent(TQShowEvent* e)
{
  if (!d->initDone)
  {
     d->initDone = true;
     updateIcons();
  }
  TQFrame::showEvent(e);
}

void KAnimWidget::hideEvent(TQHideEvent* e)
{
  TQFrame::hideEvent(e);
}

void KAnimWidget::enterEvent( TQEvent *e )
{
  setFrameStyle( Panel | Raised );

  TQFrame::enterEvent( e );
}

void KAnimWidget::leaveEvent( TQEvent *e )
{
  setFrameStyle( StyledPanel | Sunken );

  TQFrame::leaveEvent( e );
}

void KAnimWidget::mousePressEvent( TQMouseEvent *e )
{
  TQFrame::mousePressEvent( e );
}

void KAnimWidget::mouseReleaseEvent( TQMouseEvent *e )
{
  if ( e->button() == Qt::LeftButton &&
       rect().contains( e->pos() ) )
    emit clicked();

  TQFrame::mouseReleaseEvent( e );
}

void KAnimWidget::slotTimerUpdate()
{
  if(!isVisible())
    return;

  d->current_frame++;
  if (d->current_frame == d->frames)
     d->current_frame = 0;

  // TODO
  // We have to clear the widget when repainting a transparent image
  // By doing it like this we get a bit of flicker though. A better
  // way might be to merge it with the background in drawContents.
  repaint(d->transparent);
}

void KAnimWidget::drawContents( TQPainter *p )
{
  if ( d->pixmap.isNull() )
    return;

  int w = d->pixmap.width();
  int h = w;
  int x = (width()  - w) / 2;
  int y = (height() - h) / 2;
  p->drawPixmap(TQPoint(x, y), d->pixmap, TQRect(0, d->current_frame*h, w, h));
}

void KAnimWidget::updateIcons()
{
  if (!d->initDone)
     return;

  if (parent()->inherits( "KToolBar" ))
    d->size = ((KToolBar*)parent())->iconSize();
  if (!d->size)
     d->size = KGlobal::iconLoader()->currentSize(KIcon::MainToolbar);

  TQString path = KGlobal::iconLoader()->iconPath(d->icon_name, -d->size);
  TQImage img(path);
  
  if (img.isNull())
     return;

  d->current_frame = 0;
  d->frames = img.height() / img.width();
  d->transparent = img.hasAlphaBuffer();
  if (d->pixmap.width() != d->size)
  {
     img = img.smoothScale(d->size, d->size*d->frames);     
  }
  d->pixmap = img;

  setFixedSize( d->size+2, d->size+2 );
  resize( d->size+2, d->size+2 );
}

void KAnimWidget::virtual_hook( int, void* )
{ /*BASE::virtual_hook( id, data );*/ }

#include "kanimwidget.moc"