summaryrefslogtreecommitdiffstats
path: root/konq-plugins/sidebar/mediaplayer/mediawidget.cpp
blob: c460c3cd585d4eef747806192b90aa060fcfe1df (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
/***************************************************************************
                          mediawidget.cpp - The main widget
                             -------------------
    begin                : Sat June 23 13:35:30 CEST 2001
    copyright            : (C) 2001 Joseph Wenninger
    email                : jowenn@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "mediawidget.h"
#include "mediawidget.moc"
#include "player.h"

#include <kdebug.h>
#include <kurl.h>
#include <kurldrag.h>
#include <klocale.h>

#include <qlabel.h>
#include <qwidget.h>
#include <qpushbutton.h>
#include <qlcdnumber.h>
#include <qpopupmenu.h>
#include <qslider.h>
#include <qtooltip.h>

KSB_MediaWidget::KSB_MediaWidget(QWidget *parent):KSB_MediaWidget_skel(parent)
{
	player = new Player(this);
	empty();

	QFont labelFont = time->font();
	labelFont.setPointSize(18);
	labelFont.setBold(true);
	time->setFont(labelFont);

	connect(Play, SIGNAL(clicked()), player, SLOT(play()));
	connect(Pause, SIGNAL(clicked()), player, SLOT(pause()));
	connect(Stop, SIGNAL(clicked()), player, SLOT(stop()));

	connect(player, SIGNAL(timeout()), this, SLOT(playerTimeout()));
	connect(player, SIGNAL(finished()), this, SLOT(playerFinished()));
	connect(player, SIGNAL(playing()), this, SLOT(playing()));
	connect(player, SIGNAL(paused()), this, SLOT(paused()));
	connect(player, SIGNAL(stopped()), this, SLOT(stopped()));
	connect(player, SIGNAL(empty()), this, SLOT(empty()));

	connect(Position, SIGNAL(userChanged(int)), this, SLOT(skipToWrapper(int)));
	connect(this, SIGNAL(skipTo(unsigned long)), player, SLOT(skipTo(unsigned long)));
	setAcceptDrops(true);

	pretty="";
	needLengthUpdate=false;

	QToolTip::add(Play,i18n("Play"));
	QToolTip::add(Pause,i18n("Pause"));
	QToolTip::add(Stop,i18n("Stop"));
}

void KSB_MediaWidget::skipToWrapper(int second)
{
	emit skipTo((unsigned long)(second*1000));
}

void KSB_MediaWidget::dragEnterEvent ( QDragEnterEvent * e)
{
	e->accept(KURLDrag::canDecode(e));
}

void KSB_MediaWidget::dropEvent ( QDropEvent * e)
{
	m_kuri_list.clear();
	if (KURLDrag::decode(e, m_kuri_list))
	{
		playerFinished();
	}
}


void KSB_MediaWidget::playerTimeout()
{
	if(player->current().isEmpty())
		return;

	if(Position->currentlyPressed())
		return;

// update the scrollbar length
	if(player->getLength())
	{
		int range = (int)(player->getLength() / 1000);
		Position->setRange(0, range);
		if (needLengthUpdate)
		{
			int counter = player->lengthString().length() - (player->lengthString().find("/")+1);
			QString length=player->lengthString().right(counter);
			needLengthUpdate=false;
		}
	}
	else
	{
		Position->setRange(0, 1);
	}
	// set the position
	Position->setValue((int)(player->getTime() / 1000));

	// update the time label
	// catch files with duration > 99mins correctly
	time->setText(player->lengthString());
}

void KSB_MediaWidget::playerFinished()
{
	if( m_kuri_list.count() > 0 )
	{
		KURL kurl = m_kuri_list.first();
		m_kuri_list.remove( kurl );
		bool validFile = player->openFile( kurl );
		if (validFile) {
			currentFile->setText( kurl.fileName() );
			player->play();
			needLengthUpdate=true;
			pretty=kurl.prettyURL();
		} else {
			currentFile->setText( i18n("Not a sound file") );
			playerFinished();
		}
	}
}

void KSB_MediaWidget::playing()
{
	Play->setEnabled(false);
	Pause->setEnabled(true);
	Stop->setEnabled(true);
}

void KSB_MediaWidget::paused()
{
	Play->setEnabled(true);
	Pause->setEnabled(false);
	Stop->setEnabled(true);
}

void KSB_MediaWidget::stopped()
{
	Position->setValue(0);
	time->setText("00:00/00:00");
	Play->setEnabled(true);
	Pause->setEnabled(false);
	Stop->setEnabled(false);
}

void KSB_MediaWidget::empty()
{
	Position->setValue(0);
	time->setText("00:00/00:00");
	Play->setEnabled(false);
	Pause->setEnabled(false);
	Stop->setEnabled(false);
}