summaryrefslogtreecommitdiffstats
path: root/beetplayer.cpp
blob: 1fd0aa76c5fb9003c2414760bebd2c98cb31d917 (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
#include <QSettings>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QApplication>
#include <QHBoxLayout>
#include <QAction>
#include <QMenuBar>
#include <QStatusBar>
#include <QLabel>

#include "beetplayer.h"
#include "configurationdialog.h"
#include "playerwidget.h"
#include "globals.h"

BeetPlayer::BeetPlayer(QWidget *parent, Qt::WindowFlags f) : QMainWindow(parent, f) {
    //general setup
    qApp->setWindowIcon(QIcon(":/beetplayer32.png"));
    setMinimumWidth(1024);
    setMinimumHeight(768);
    openDatabase();
    createGlobalActions();
    mPlayerWidget = new PlayerWidget;
    connect(mPlayerWidget, SIGNAL(viewModeChanged(QString)), this, SLOT(setViewMode(QString)));
    connect(mPlayerWidget->player(), SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(setPlayMode(QMediaPlayer::State)));
    connect(mPlayerWidget, SIGNAL(numFilesChanged(int)), this, SLOT(setNumFiles(int)));
    connect(mPlayerWidget, SIGNAL(playListLengthChanged(quint64)), this, SLOT(setPlayListLength(quint64)));
    createStatusbar();
    setCentralWidget(mPlayerWidget);
}

void BeetPlayer::openDatabase(){
    QSettings s;
    QString dbhost = s.value("dbhost").toString();
    QString dbuser = s.value("dbuser").toString();
    QString dbpass = s.value("dbpass").toString();
    QString dbname = s.value("dbname").toString();
    if(!QSqlDatabase::contains("beetplayerdb")){
        QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", "beetplayerdb");
        db.setHostName(dbhost);
        db.setUserName(dbuser);
        db.setPassword(dbpass);
        db.setDatabaseName(dbname);
        if(!db.open()){
            int res = ConfigurationDialog(this).exec();
            if(res == QDialog::Accepted){
                openDatabase();
            }else{
                QMessageBox::critical(this, tr("Error"), tr("Could not open database. Giving up!"));
                qApp->closeAllWindows();
            }
        }
    }
}

void BeetPlayer::createGlobalActions(){
    QAction *quitA = new QAction(tr("Quit"), this);
    quitA->setShortcut(tr("CTRL+Q"));
    connect(quitA, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
    quitA->setData(Globals::QuitAction);
    Globals::instance()->addAction(quitA);
    QAction *configA = new QAction(QIcon(":/chastity_belt.png"), tr("Configure..."), this);
    connect(configA, SIGNAL(triggered()), this, SLOT(configure()));
    configA->setData(Globals::ConfigAction);
    Globals::instance()->addAction(configA);
}

void BeetPlayer::configure(){
    ConfigurationDialog w(this);
    int res = w.exec();
    if(res == QDialog::Accepted){
        openDatabase();
    }
}

void BeetPlayer::setViewMode(const QString &viewMode){
    mModeL->setText(viewMode);
}

void BeetPlayer::setPlayMode(QMediaPlayer::State state){
    if(state == QMediaPlayer::StoppedState){
        mActionL->setText(tr("Stopped"));
    }else if(state == QMediaPlayer::PlayingState){
        mActionL->setText("Playing");
    }else if(state == QMediaPlayer::PausedState){
        mActionL->setText("Paused");
    }else{
        mActionL->setText(tr("Unknown"));
    }
}

void BeetPlayer::setNumFiles(int numFiles){
    QString n = QString("%1").arg(numFiles, 4, 10, QChar('0'));
    mFilesL->setText(n);
}

void BeetPlayer::setPlayListLength(quint64 seconds){
    int h = (seconds / 60 / 60);
    int min = (seconds / 60) % 60;
    int secs = seconds % 60;
    QString r = QString("%1:%2:%3").arg(h, 3, 10, QChar('0')).arg(min, 2, 10, QChar('0')).arg(secs, 2, 10, QChar('0'));
    mPlaylistDurL->setText(r);
}

void BeetPlayer::createStatusbar(){
    QLabel *l1 = new QLabel(tr("View:"));
    mModeL = new QLabel;
    mModeL->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    mModeL->setFont(QFont("courier"));
    mModeL->setText(tr("(none)"));
    statusBar()->addPermanentWidget(l1);
    statusBar()->addPermanentWidget(mModeL);
    mGeneralL = new QLabel;
    mGeneralL->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    statusBar()->addPermanentWidget(mGeneralL, 20); //20 is an arbitray value, stretch to max
    QLabel *l2 = new QLabel(tr("Status:"));
    mActionL = new QLabel;
    mActionL->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    mActionL->setFont(QFont("courier"));
    mActionL->setText(tr("Stopped"));
    statusBar()->addPermanentWidget(l2);
    statusBar()->addPermanentWidget(mActionL);
    QLabel *l3 = new QLabel(tr("NumFiles:"));
    mFilesL = new QLabel;
    mFilesL->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    mFilesL->setFont(QFont("courier"));
    mFilesL->setText(tr("0000"));
    statusBar()->addPermanentWidget(l3);
    statusBar()->addPermanentWidget(mFilesL);
    QLabel *l4 = new QLabel(tr("Dur:"));
    mPlaylistDurL = new QLabel;
    mPlaylistDurL->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    mPlaylistDurL->setFont(QFont("courier"));
    mPlaylistDurL->setText("000:00:00");
    statusBar()->addPermanentWidget(l4);
    statusBar()->addPermanentWidget(mPlaylistDurL);

}