Commit 81780b47 authored by Scott Wheeler's avatar Scott Wheeler

Moving more stuff into the PlayerManager class from the JuK class. Also

added a dragging() method to the SliderAction to avoid having to keep track
of that separately.

svn path=/trunk/kdemultimedia/juk/; revision=291482
parent 2a9478e4
......@@ -21,6 +21,7 @@
#include <kdebug.h>
#include <qslider.h>
#include <qtimer.h>
#include "playermanager.h"
#include "slideraction.h"
......@@ -36,14 +37,17 @@ PlayerManager::PlayerManager() :
Player(),
m_actionCollection(0),
m_sliderAction(0),
m_player(0)
m_playlistInterface(0),
m_player(0),
m_timer(0),
m_noSeek(false)
{
setup();
}
PlayerManager::~PlayerManager()
{
delete m_player;
}
////////////////////////////////////////////////////////////////////////////////
......@@ -57,9 +61,66 @@ PlayerManager *PlayerManager::instance() // static
return m_instance;
}
void PlayerManager::play(const QString &fileName)
bool PlayerManager::playing() const
{
if(!m_player)
return false;
return m_player->playing();
}
bool PlayerManager::paused() const
{
if(!m_player)
return false;
return m_player->paused();
}
float PlayerManager::volume() const
{
if(!m_player)
return 0;
return m_player->volume();
}
long PlayerManager::totalTime() const
{
if(!m_player)
return 0;
return m_player->totalTime();
}
long PlayerManager::currentTime() const
{
if(!m_player)
return 0;
return m_player->currentTime();
}
int PlayerManager::position() const
{
if(!m_player)
return 0;
return m_player->position();
}
void PlayerManager::setPlaylistInterface(const PlaylistInterface *interface)
{
m_playlistInterface = interface;
}
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////
void PlayerManager::play(const QString &fileName)
{
if(!m_player || !m_playlistInterface)
return;
if(fileName.isNull()) {
......@@ -67,6 +128,8 @@ void PlayerManager::play(const QString &fileName)
m_player->play();
if(m_player->playing())
m_player->seekPosition(0);
else
m_player->play(m_playlistInterface->nextFile());
}
else {
if(m_player->paused())
......@@ -88,7 +151,8 @@ void PlayerManager::play(const QString &fileName)
m_sliderAction->trackPositionSlider()->setValue(0);
m_sliderAction->trackPositionSlider()->setEnabled(true);
// m_playTimer->start(m_pollInterval);
m_timer->start(m_pollInterval);
}
void PlayerManager::pause()
......@@ -115,68 +179,59 @@ void PlayerManager::setVolume(float volume)
m_player->setVolume(volume);
}
float PlayerManager::volume() const
{
if(!m_player)
return 0;
return m_player->volume();
}
bool PlayerManager::playing() const
void PlayerManager::seek(long seekTime)
{
if(!m_player)
return false;
return;
return m_player->playing();
m_player->seek(seekTime);
}
bool PlayerManager::paused() const
void PlayerManager::seekPosition(int position)
{
if(!m_player)
return false;
return;
return m_player->paused();
m_player->seekPosition(position);
}
long PlayerManager::totalTime() const
{
if(!m_player)
return 0;
return m_player->totalTime();
}
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
long PlayerManager::currentTime() const
void PlayerManager::slotPollPlay()
{
if(!m_player)
return 0;
if(!m_player || !m_playlistInterface)
return;
return m_player->currentTime();
}
m_noSeek = true;
int PlayerManager::position() const
{
if(!m_player)
return 0;
if(!m_player->playing()) {
m_timer->stop();
play(m_playlistInterface->nextFile());
}
else if(!m_sliderAction->dragging()) {
m_sliderAction->trackPositionSlider()->setValue(m_player->position());
return m_player->position();
}
// TODO: figure out something to do with the status bar interface
void PlayerManager::seek(long seekTime)
{
if(!m_player)
return;
// m_statusLabel->setItemTotalTime(m_player->totalTime());
// m_statusLabel->setItemCurrentTime(m_player->currentTime());
}
m_player->seek(seekTime);
}
// Ok, this is weird stuff, but it works pretty well. Ordinarily we don't
// need to check up on our playing time very often, but in the span of the
// last interval, we want to check a lot -- to figure out that we've hit the
// end of the song as soon as possible.
void PlayerManager::seekPosition(int position)
{
if(!m_player)
return;
if(m_player->playing() &&
m_player->totalTime() > 0 &&
float(m_player->totalTime() - m_player->currentTime()) < m_pollInterval * 2)
{
m_timer->changeInterval(50);
}
m_player->seekPosition(position);
m_noSeek = false;
}
////////////////////////////////////////////////////////////////////////////////
......@@ -231,6 +286,9 @@ void PlayerManager::setup()
}
else
m_player = Player::createPlayer();
m_timer = new QTimer(this, "play timer");
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotPollPlay()));
}
#include "playermanager.moc"
......@@ -22,8 +22,21 @@
#include "player.h"
class QTimer;
class SliderAction;
/**
* This is a simple interface that should be implemented by objects used by
* the PlayerManager.
*/
class PlaylistInterface
{
public:
virtual QString nextFile() const = 0;
virtual QString previousFile() const = 0;
};
/**
* This class serves as a proxy to the Player interface and handles managing
* the actions from the top-level mainwindow.
......@@ -40,34 +53,41 @@ protected:
public:
static PlayerManager *instance();
public slots:
// Implementation of the Player interface
virtual void play(const QString &fileName = QString::null);
virtual void pause();
virtual void stop();
virtual void setVolume(float volume = 1.0);
virtual float volume() const;
virtual bool playing() const;
virtual bool paused() const;
virtual float volume() const;
virtual long totalTime() const;
virtual long currentTime() const;
virtual int position() const;
void setPlaylistInterface(const PlaylistInterface *interface);
public slots:
virtual void play(const QString &fileName = QString::null);
virtual void pause();
virtual void stop();
virtual void setVolume(float volume = 1.0);
virtual void seek(long seekTime);
virtual void seekPosition(int position);
private:
void setup();
private slots:
void slotPollPlay();
private:
static PlayerManager *m_instance;
KActionCollection *m_actionCollection;
SliderAction *m_sliderAction;
const PlaylistInterface *m_playlistInterface;
Player *m_player;
QTimer *m_timer;
bool m_noSeek;
static const int m_pollInterval = 800;
};
#endif
......@@ -66,7 +66,6 @@ public:
protected:
virtual void wheelEvent(QWheelEvent *e) {
QWheelEvent transposed(e->pos(), -(e->delta()), e->state(), e->orientation());
QSlider::wheelEvent(&transposed);
}
virtual void focusInEvent(QFocusEvent *) { clearFocus(); }
......@@ -77,7 +76,8 @@ protected:
////////////////////////////////////////////////////////////////////////////////
SliderAction::SliderAction(const QString &text, QObject *parent, const char *name)
: CustomAction(text, parent, name)
: CustomAction(text, parent, name),
m_dragging(false)
{
}
......@@ -138,6 +138,8 @@ QWidget *SliderAction::createWidget(QWidget *parent) // virtual -- used by base
m_trackPositionSlider->setMaxValue(1000);
QToolTip::add(m_trackPositionSlider, i18n("Track position"));
m_layout->addWidget(m_trackPositionSlider);
connect(m_trackPositionSlider, SIGNAL(sliderPressed()), this, SLOT(slotSliderPressed()));
connect(m_trackPositionSlider, SIGNAL(sliderReleased()), this, SLOT(slotSliderReleased()));
m_layout->addItem(new QSpacerItem(10, 1));
......
......@@ -34,6 +34,8 @@ public:
QSlider *volumeSlider() const { return m_volumeSlider; }
QSlider *trackPositionSlider() const { return m_trackPositionSlider; }
bool dragging() const { return m_dragging; }
public slots:
void slotUpdateOrientation(QDockWindow *dockWindow = 0);
......@@ -42,11 +44,14 @@ private:
private slots:
void slotUpdateSize();
void slotSliderPressed() { m_dragging = true; }
void slotSliderReleased() { m_dragging = false; }
private:
QBoxLayout *m_layout;
QSlider *m_trackPositionSlider;
QSlider *m_volumeSlider;
bool m_dragging;
static const int volumeMax = 50;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment