Singletonify XMMSHandler and kick out everything not XMMSisch from the class.

A class that needs to talk to xmms2 now calls XMMSHandler::getInstance()
and all communication is handled by signals'n'slots.
This commit is contained in:
Daniel Svensson 2006-02-26 19:16:43 +01:00
parent 56dd4cf3ee
commit 787a5c7b5b
10 changed files with 150 additions and 85 deletions

View file

@ -5,7 +5,7 @@
MainDisplay::MainDisplay (QWidget *parent) : SkinDisplay(parent) MainDisplay::MainDisplay (QWidget *parent) : SkinDisplay(parent)
{ {
m_mw = dynamic_cast<MainWindow*>(parent); XMMSHandler *xmmsh = XMMSHandler::getInstance ();
m_tbar = new TitleBar(this, false); m_tbar = new TitleBar(this, false);
m_tbar->move(0, 0); m_tbar->move(0, 0);
@ -43,6 +43,12 @@ MainDisplay::MainDisplay (QWidget *parent) : SkinDisplay(parent)
m_playstatus = new PlayStatus (this); m_playstatus = new PlayStatus (this);
m_playstatus->move (24, 28); m_playstatus->move (24, 28);
connect (xmmsh, SIGNAL(mediainfoChanged(QString,int,int,int,int)),
this, SLOT(setMediainfo(QString,int,int,int,int)));
connect (xmmsh, SIGNAL(playbackStatusChanged(uint)),
this, SLOT(setStatus(uint)));
connect (xmmsh, SIGNAL(playtimeChanged(uint)),
this, SLOT(setPlaytime(uint)));
} }
void void
@ -58,6 +64,49 @@ MainDisplay::setPixmaps (Skin *skin)
setMinimumSize(QSize(275, 116)); setMinimumSize(QSize(275, 116));
} }
void
MainDisplay::setStatus (uint status)
{
if (status == XMMS_PLAYBACK_STATUS_STOP) {
m_number->setNumber (0, 0);
m_number2->setNumber (0, 0);
m_slider->setPos (0);
m_slider->hideBar (true);
}
}
void
MainDisplay::setPlaytime (uint time)
{
uint sec, min;
sec = (time / 1000) % 60;
min = (time / 1000) / 60;
m_number->setNumber (min / 10, min % 10);
m_number2->setNumber (sec / 10, sec % 10);
// update slider
m_slider->setPos (time);
}
void
MainDisplay::setMediainfo (QString str, int bitrate, int samplerate,
int channels, int duration)
{
m_text->setText (str);
m_kbps->setNumber (bitrate/1000, 3);
m_khz->setNumber (samplerate/1000, 2);
if (channels > 1) {
m_stereo->setStereoMono (1, 0);
} else {
m_stereo->setStereoMono (0, 1);
}
m_slider->setMax (duration);
m_slider->hideBar (false);
}
void void
MainDisplay::SetupToggleButtons (void) MainDisplay::SetupToggleButtons (void)
{ {
@ -81,28 +130,28 @@ MainDisplay::SetupToggleButtons (void)
void void
MainDisplay::SetupPushButtons (void) MainDisplay::SetupPushButtons (void)
{ {
MainWindow *mw = dynamic_cast<MainWindow *>(window ()); XMMSHandler *xmmsh = XMMSHandler::getInstance ();
/* Normal buttons */ /* Normal buttons */
m_prev = new Button (this, Skin::BTN_PREV_0, Skin::BTN_PREV_1); m_prev = new Button (this, Skin::BTN_PREV_0, Skin::BTN_PREV_1);
m_prev->move(16, 88); m_prev->move(16, 88);
connect (m_prev, SIGNAL(clicked()), mw->getHandler (), SLOT(prev())); connect (m_prev, SIGNAL(clicked()), xmmsh, SLOT(prev()));
m_play = new Button (this, Skin::BTN_PLAY_0, Skin::BTN_PLAY_1); m_play = new Button (this, Skin::BTN_PLAY_0, Skin::BTN_PLAY_1);
m_play->move(39, 88); m_play->move(39, 88);
connect (m_play, SIGNAL(clicked()), mw->getHandler (), SLOT(play())); connect (m_play, SIGNAL(clicked()), xmmsh, SLOT(play()));
m_pause = new Button (this, Skin::BTN_PAUSE_0, Skin::BTN_PAUSE_1); m_pause = new Button (this, Skin::BTN_PAUSE_0, Skin::BTN_PAUSE_1);
m_pause->move(62, 88); m_pause->move(62, 88);
connect (m_pause, SIGNAL(clicked()), mw->getHandler (), SLOT(pause())); connect (m_pause, SIGNAL(clicked()), xmmsh, SLOT(pause()));
m_stop = new Button (this, Skin::BTN_STOP_0, Skin::BTN_STOP_1); m_stop = new Button (this, Skin::BTN_STOP_0, Skin::BTN_STOP_1);
m_stop->move(85, 88); m_stop->move(85, 88);
connect (m_stop, SIGNAL(clicked()), mw->getHandler (), SLOT(stop())); connect (m_stop, SIGNAL(clicked()), xmmsh, SLOT(stop()));
m_next = new Button (this, Skin::BTN_NEXT_0, Skin::BTN_NEXT_1); m_next = new Button (this, Skin::BTN_NEXT_0, Skin::BTN_NEXT_1);
m_next->move(108, 88); m_next->move(108, 88);
connect (m_next, SIGNAL(clicked()), mw->getHandler (), SLOT(next())); connect (m_next, SIGNAL(clicked()), xmmsh, SLOT(next()));
m_eject = new Button (this, Skin::BTN_EJECT_0, Skin::BTN_EJECT_1); m_eject = new Button (this, Skin::BTN_EJECT_0, Skin::BTN_EJECT_1);
m_eject->move(136, 89); m_eject->move(136, 89);

View file

@ -23,6 +23,7 @@ class MainDisplay;
#include "StereoMono.h" #include "StereoMono.h"
#include "Slider.h" #include "Slider.h"
#include "PlayStatus.h" #include "PlayStatus.h"
#include "MainWindow.h"
using namespace std; using namespace std;
@ -47,6 +48,10 @@ class MainDisplay : public SkinDisplay
public slots: public slots:
void setPixmaps(Skin *skin); void setPixmaps(Skin *skin);
void setStatus (uint status);
void setPlaytime (uint time);
void setMediainfo (QString str, int bitrate, int samplerate,
int channels, int duration);
protected: protected:
void SetupPushButtons (void); void SetupPushButtons (void);

View file

@ -10,7 +10,7 @@ MainWindow::MainWindow (QWidget *parent) : QMainWindow (parent)
* Initialize the Handler that will * Initialize the Handler that will
* update the display and the buttons * update the display and the buttons
*/ */
m_handler = new XMMSHandler (this); m_handler = XMMSHandler::getInstance();
/* /*
* Initialize skin, but don't open one * Initialize skin, but don't open one

View file

@ -2,10 +2,15 @@
PlayStatus::PlayStatus (QWidget *parent) : PixWidget (parent) PlayStatus::PlayStatus (QWidget *parent) : PixWidget (parent)
{ {
setMinimumSize(11, 9); XMMSHandler *xmmsh = XMMSHandler::getInstance();
setMaximumSize(11, 9);
m_status = XMMS_PLAYBACK_STATUS_STOP; setMinimumSize(11, 9);
setMaximumSize(11, 9);
m_status = XMMS_PLAYBACK_STATUS_STOP;
connect (xmmsh, SIGNAL(playbackStatusChanged(uint)),
this, SLOT(setStatus(uint)));
} }
void void
@ -19,9 +24,9 @@ PlayStatus::setPixmaps (Skin *skin)
} }
void void
PlayStatus::setStatus (int status) PlayStatus::setStatus (uint status)
{ {
qDebug("funkar");
if (status == XMMS_PLAYBACK_STATUS_STOP) { if (status == XMMS_PLAYBACK_STATUS_STOP) {
m_pixmap = m_pixmap_stop; m_pixmap = m_pixmap_stop;
} else if (status == XMMS_PLAYBACK_STATUS_PLAY) { } else if (status == XMMS_PLAYBACK_STATUS_PLAY) {

View file

@ -1,19 +1,20 @@
#ifndef __PLAYSTATUS_H__ #ifndef __PLAYSTATUS_H__
#define __PLAYSTATUS_H__ #define __PLAYSTATUS_H__
#include <xmmsclient/xmmsclient.h> #include "XMMSHandler.h"
#include "PixWidget.h" #include "PixWidget.h"
class PlayStatus : public PixWidget class PlayStatus : public PixWidget
{ {
Q_OBJECT
public: public:
PlayStatus (QWidget *parent); PlayStatus (QWidget *parent);
~PlayStatus () { } ~PlayStatus () { }
void setStatus (int);
public slots: public slots:
void setPixmaps (Skin *skin); void setPixmaps (Skin *skin);
void setStatus (uint status);
private: private:
int m_status; int m_status;
@ -21,7 +22,6 @@ class PlayStatus : public PixWidget
QPixmap m_pixmap_stop; QPixmap m_pixmap_stop;
QPixmap m_pixmap_play; QPixmap m_pixmap_play;
QPixmap m_pixmap_pause; QPixmap m_pixmap_pause;
}; };
#endif #endif

View file

@ -2,8 +2,7 @@
ShadedDisplay::ShadedDisplay (QWidget *parent) : SkinDisplay (parent) ShadedDisplay::ShadedDisplay (QWidget *parent) : SkinDisplay (parent)
{ {
MainWindow *mw = dynamic_cast<MainWindow*>(parent); XMMSHandler *xmmsh = XMMSHandler::getInstance ();
m_mw = parent;
setMinimumSize (275, 14); setMinimumSize (275, 14);
setMaximumSize (275, 14); setMaximumSize (275, 14);
@ -26,31 +25,57 @@ ShadedDisplay::ShadedDisplay (QWidget *parent) : SkinDisplay (parent)
m_prev = new Button (this); m_prev = new Button (this);
m_prev->move(169, 4); m_prev->move(169, 4);
m_prev->resize (8, 7); m_prev->resize (8, 7);
connect (m_prev, SIGNAL(clicked()), mw->getHandler (), SLOT(prev())); connect (m_prev, SIGNAL(clicked()), xmmsh, SLOT(prev()));
m_play = new Button (this); m_play = new Button (this);
m_play->move(177, 4); m_play->move(177, 4);
m_play->resize (10, 7); m_play->resize (10, 7);
connect (m_play, SIGNAL(clicked()), mw->getHandler (), SLOT(play())); connect (m_play, SIGNAL(clicked()), xmmsh, SLOT(play()));
m_pause = new Button (this); m_pause = new Button (this);
m_pause->move(187, 4); m_pause->move(187, 4);
m_pause->resize (10, 7); m_pause->resize (10, 7);
connect (m_pause, SIGNAL(clicked()), mw->getHandler (), SLOT(pause())); connect (m_pause, SIGNAL(clicked()), xmmsh, SLOT(pause()));
m_stop = new Button (this); m_stop = new Button (this);
m_stop->move(197, 4); m_stop->move(197, 4);
m_stop->resize (9, 7); m_stop->resize (9, 7);
connect (m_stop, SIGNAL(clicked()), mw->getHandler (), SLOT(stop())); connect (m_stop, SIGNAL(clicked()), xmmsh, SLOT(stop()));
m_next = new Button (this); m_next = new Button (this);
m_next->move(206, 4); m_next->move(206, 4);
m_next->resize (8, 7); m_next->resize (8, 7);
connect (m_next, SIGNAL(clicked()), mw->getHandler (), SLOT(next())); connect (m_next, SIGNAL(clicked()), xmmsh, SLOT(next()));
m_eject = new Button (this); m_eject = new Button (this);
m_eject->move(216, 4); m_eject->move(216, 4);
m_eject->resize (9, 7); m_eject->resize (9, 7);
connect (xmmsh, SIGNAL(playbackStatusChanged(uint)),
this, SLOT(setStatus(uint)));
connect (xmmsh, SIGNAL(playtimeChanged(uint)),
this, SLOT(setPlaytime(uint)));
} }
void
ShadedDisplay::setStatus (uint status)
{
if (status == XMMS_PLAYBACK_STATUS_STOP) {
m_number->setNumber (0, 2);
m_number2->setNumber (0, 2);
}
}
void
ShadedDisplay::setPlaytime (uint time)
{
uint sec, min;
sec = (time / 1000) % 60;
min = (time / 1000) / 60;
m_number->setNumber (min, 2);
m_number2->setNumber (sec, 2);
}

View file

@ -11,6 +11,7 @@ class ShadedDisplay;
class ShadedDisplay : public SkinDisplay class ShadedDisplay : public SkinDisplay
{ {
Q_OBJECT
public: public:
ShadedDisplay (QWidget *parent); ShadedDisplay (QWidget *parent);
~ShadedDisplay () { } ~ShadedDisplay () { }
@ -27,6 +28,10 @@ class ShadedDisplay : public SkinDisplay
Button *m_next; Button *m_next;
Button *m_eject; Button *m_eject;
public slots:
void setStatus (uint status);
void setPlaytime (uint time);
}; };
#endif #endif

View file

@ -124,7 +124,6 @@ Slider::setPos (uint p)
m_pos = x; m_pos = x;
update (); update ();
} }
} }
void void

View file

@ -1,15 +1,23 @@
#include <xmmsclient/xmmsclient++.h> #include <xmmsclient/xmmsclient++.h>
#include "XmmsQT4.h" #include "XmmsQT4.h"
#include "MainDisplay.h"
#include "XMMSHandler.h" #include "XMMSHandler.h"
#include <QErrorMessage> #include <QErrorMessage>
XMMSHandler::XMMSHandler (MainWindow *mw) : sigc::trackable () XMMSHandler *XMMSHandler::singleton = NULL;
{
m_mw = mw;
XMMSHandler *XMMSHandler::getInstance (void)
{
if (!singleton) {
singleton = new XMMSHandler ();
}
return singleton;
}
XMMSHandler::XMMSHandler (void) : sigc::trackable ()
{
m_xmmsc = new XMMSClient ("promoe"); m_xmmsc = new XMMSClient ("promoe");
if (!m_xmmsc->connect (getenv ("XMMS_PATH"))) { if (!m_xmmsc->connect (getenv ("XMMS_PATH"))) {
@ -45,40 +53,20 @@ XMMSHandler::playlist_list (XMMSResultValueList<uint> *res)
void void
XMMSHandler::playback_status (XMMSResultValue<uint> *res) XMMSHandler::playback_status (XMMSResultValue<uint> *res)
{ {
uint i; uint status;
res->getValue (&i); res->getValue (&status);
m_mw->getMD ()->m_playstatus->setStatus (i);
if (i == XMMS_PLAYBACK_STATUS_STOP) {
m_mw->getSD ()->m_number->setNumber (0, 2);
m_mw->getSD ()->m_number2->setNumber (0, 2);
m_mw->getMD ()->m_number->setNumber (0, 0);
m_mw->getMD ()->m_number2->setNumber (0, 0);
m_mw->getMD ()->m_slider->setPos (0);
}
emit playbackStatusChanged (status);
} }
void void
XMMSHandler::playback_playtime (XMMSResultValue<uint> *res) XMMSHandler::playback_playtime (XMMSResultValue<uint> *res)
{ {
uint i, sec, min; uint i;
res->getValue (&i); res->getValue (&i);
sec = (i / 1000) % 60;
min = (i / 1000) / 60;
if (m_mw->getShaded ()) {
m_mw->getSD ()->m_number->setNumber (min, 2);
m_mw->getSD ()->m_number2->setNumber (sec, 2);
} else {
m_mw->getMD ()->m_number->setNumber (min / 10, min % 10);
m_mw->getMD ()->m_number2->setNumber (sec / 10, sec % 10);
/* update slider */
m_mw->getMD ()->m_slider->setPos (i);
}
emit playtimeChanged (i);
res->restart (); res->restart ();
} }
@ -101,45 +89,31 @@ XMMSHandler::playback_current_id (XMMSResultValue<uint> *res)
void void
XMMSHandler::setPlaytime (void) XMMSHandler::setPlaytime (void)
{ {
/*
uint pos = m_mw->getMD ()->m_slider->getPos(); uint pos = m_mw->getMD ()->m_slider->getPos();
qDebug ("pos = %d", pos); qDebug ("pos = %d", pos);
delete m_xmmsc->playback_seek_ms (pos); delete m_xmmsc->playback_seek_ms (pos);
*/
} }
void void
XMMSHandler::medialib_info (XMMSResultDict *res) XMMSHandler::medialib_info (XMMSResultDict *res)
{ {
int bitrate, samplerate, channels, duration;
char str[4096]; char str[4096];
int b;
/* Make this NICER! */ // Make this NICER!
res->entryFormat (str, 4096, "${artist} - ${album} - ${title}"); res->entryFormat (str, 4096, "${artist} - ${album} - ${title}");
m_mw->getMD ()->m_text->setText (QString::fromUtf8 (str));
m_mw->getSD ()->m_title->setText (QString::fromUtf8 (str));
if (res->getValue ("bitrate", &b)) {
m_mw->getMD ()->m_kbps->setNumber (b/1000, 3);
}
if (res->getValue ("samplerate", &b)) { res->getValue ("bitrate", &bitrate);
m_mw->getMD ()->m_khz->setNumber (b/1000, 2); res->getValue ("samplerate", &samplerate);
} res->getValue ("channels:out", &channels);
res->getValue ("duration", &duration);
if (res->getValue ("channels:out", &b)) { emit mediainfoChanged (QString::fromUtf8 (str), bitrate,
if (b == 1) { samplerate, channels, duration);
m_mw->getMD ()->m_stereo->setStereoMono (0, 1);
} else {
m_mw->getMD ()->m_stereo->setStereoMono (1, 0);
}
}
if (res->getValue ("duration", &b)) {
if (b > 0) {
m_mw->getMD ()->m_slider->setMax (b);
m_mw->getMD ()->m_slider->hideBar (false);
}
}
delete res; delete res;
} }

View file

@ -3,9 +3,6 @@
#include <xmmsclient/xmmsclient++.h> #include <xmmsclient/xmmsclient++.h>
class XMMSHandler;
#include "MainWindow.h"
#include "XmmsQT4.h" #include "XmmsQT4.h"
#include <QObject> #include <QObject>
@ -13,7 +10,8 @@ class XMMSHandler;
class XMMSHandler : public QObject, public sigc::trackable { class XMMSHandler : public QObject, public sigc::trackable {
Q_OBJECT Q_OBJECT
public: public:
XMMSHandler (MainWindow *mw); static XMMSHandler *getInstance (void);
XMMSHandler (void);
~XMMSHandler (); ~XMMSHandler ();
void playback_playtime (XMMSResultValue<uint> *res); void playback_playtime (XMMSResultValue<uint> *res);
void playback_current_id (XMMSResultValue<uint> *res); void playback_current_id (XMMSResultValue<uint> *res);
@ -38,11 +36,16 @@ class XMMSHandler : public QObject, public sigc::trackable {
delete m_xmmsc->playback_tickle (); delete m_xmmsc->playback_tickle ();
} }
signals:
void playbackStatusChanged (uint status);
void playtimeChanged (uint time);
void mediainfoChanged (QString str, int bitrate, int samplerate,
int channels, int duration);
private: private:
MainWindow *m_mw;
XmmsQT4 *m_qt4; XmmsQT4 *m_qt4;
XMMSClient *m_xmmsc; XMMSClient *m_xmmsc;
static XMMSHandler *singleton;
}; };
#endif #endif