summaryrefslogtreecommitdiffstats
path: root/src/k3bwidgetshoweffect.cpp
blob: 1caa97125dc4c17a46609651426f1162945189c6 (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
/* 
 *
 * $Id: sourceheader 380067 2005-01-19 13:03:46Z trueg $
 * Copyright (C) 2005 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * Based on the effects in popupMessage.cpp 
 * Copyright (C) 2005 by Max Howell <max.howell@methylblue.com>
 *               2005 by Seb Ruiz <me@sebruiz.net>
 *
 * Dissolve Mask (c) Kicker Authors kickertip.cpp, 2005/08/17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bwidgetshoweffect.h"

#include <qpainter.h>
#include <qwidget.h>


K3bWidgetShowEffect::K3bWidgetShowEffect( QWidget* widget, Effect e )
  : QObject( widget ),
    m_effect( e ),
    m_widget( widget ),
    m_dissolveSize( 0 ),
    m_dissolveDelta( -1 ),
    m_offset( 0 ),
    m_deleteSelf( false ),
    m_bEffectOnly( false )
{
}


K3bWidgetShowEffect::~K3bWidgetShowEffect()
{
}


void K3bWidgetShowEffect::hide( bool effectOnly )
{
  m_bEffectOnly = effectOnly;
  m_bShow = false;
  m_offset = m_widget->height();
  killTimer( m_timerId );
  m_timerId = startTimer( 6 );
}


void K3bWidgetShowEffect::show( bool effectOnly )
{
  m_bShow = true;
  m_offset = 0;
  m_dissolveSize = 24;
  m_dissolveDelta = -1;

  m_widget->polish();

  if( m_effect == Dissolve ) {
    // necessary to create the mask
    m_mask.resize( m_widget->width(), m_widget->height() );
    // make the mask empty and hence will not show widget with show() called below
    dissolveMask();
    m_timerId = startTimer( 1000 / 30 );
  }
  else {
    m_widget->move( 0, m_widget->parentWidget()->height() );
    m_timerId = startTimer( 6 );
  }
  
  if( !effectOnly )
    m_widget->show();
}


void K3bWidgetShowEffect::timerEvent( QTimerEvent* )
{
  switch( m_effect ) {
  case Slide:
    slideMask();
    break;
    
  case Dissolve:
    dissolveMask();
    break;
  }
}


void K3bWidgetShowEffect::dissolveMask()
{
  if( m_bShow ) {
    m_widget->repaint( false );
    QPainter maskPainter(&m_mask);

    m_mask.fill(Qt::black);

    maskPainter.setBrush(Qt::white);
    maskPainter.setPen(Qt::white);
    maskPainter.drawRect( m_mask.rect() );
    
    m_dissolveSize += m_dissolveDelta;

    if( m_dissolveSize > 0 ) {
      maskPainter.setRasterOp( Qt::EraseROP );

      int x, y, s;
      const int size = 16;

      for( y = 0; y < m_widget->height() + size; y += size ) {
	x = m_widget->width();
	s = m_dissolveSize * x / 128;

	for( ; x > size; x -= size, s -= 2 ) {
	  if( s < 0 )
	    break;

	  maskPainter.drawEllipse(x - s / 2, y - s / 2, s, s);
	}
      }
    }
    else if( m_dissolveSize < 0 ) {
      m_dissolveDelta = 1;
      killTimer( m_timerId );

      emit widgetShown( m_widget );

      if( m_deleteSelf )
	deleteLater();
    }

    m_widget->setMask( m_mask );
  }

  else {
    // just hide it for now
    emit widgetHidden( m_widget );
    if( !m_bEffectOnly )
      m_widget->hide();

    if( m_deleteSelf )
      deleteLater();
  }
}


void K3bWidgetShowEffect::slideMask()
{
  if( m_bShow ) {
    m_widget->move( 0, m_widget->parentWidget()->height() - m_offset );
    
    m_offset++;
    if( m_offset > m_widget->height() ) {
      killTimer( m_timerId );

      emit widgetShown( m_widget );
      
      if( m_deleteSelf )
	deleteLater();
    }
  }
  else {
    m_offset--;
    m_widget->move( 0, m_widget->parentWidget()->height() - m_offset );
    
    if( m_offset < 0 ) {
      // finally hide the widget
      emit widgetHidden( m_widget );
      if( !m_bEffectOnly )
	m_widget->hide();

      if( m_deleteSelf )
	deleteLater();
    }
  }
}



K3bWidgetShowEffect* K3bWidgetShowEffect::showWidget( QWidget* w, Effect m )
{
  K3bWidgetShowEffect* e = new K3bWidgetShowEffect( w, m );
  e->m_deleteSelf = true;
  e->show();
  return e;
}


K3bWidgetShowEffect* K3bWidgetShowEffect::hideWidget( QWidget* w, Effect m )
{
  K3bWidgetShowEffect* e = new K3bWidgetShowEffect( w, m );
  e->m_deleteSelf = true;
  e->hide();
  return e;
}

#include "k3bwidgetshoweffect.moc"