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
|
#include <QSettings>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QApplication>
#include <QHBoxLayout>
#include <QAction>
#include <QMenuBar>
#include <QStatusBar>
#include <QLabel>
#include <QSplashScreen>
#include <QApplication>
#include <QKeyEvent>
#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"));
QSplashScreen splash(QPixmap(":/splash.png"));
splash.show();
setMinimumWidth(1024);
setMinimumHeight(768);
splash.showMessage(tr("Opening database..."), Qt::AlignHCenter, Qt::yellow);
qApp->processEvents();
openDatabase();
splash.showMessage(tr("Creating global actions..."), Qt::AlignHCenter, Qt::yellow);
qApp->processEvents();
createGlobalActions();
splash.showMessage(tr("Reading BeetPlayer settings..."), Qt::AlignHCenter, Qt::yellow);
qApp->processEvents();
readSettings();
splash.showMessage(tr("Constructing Player..."), Qt::AlignHCenter, Qt::yellow);
qApp->processEvents();
mPlayerWidget = new PlayerWidget(&splash);
connect(mPlayerWidget, &PlayerWidget::playModeChanged, this, &BeetPlayer::setPlayMode);
connect(mPlayerWidget, &PlayerWidget::numFilesChanged, this, &BeetPlayer::setNumFiles);
connect(mPlayerWidget, &PlayerWidget::playListLengthChanged, this, &BeetPlayer::setPlayListLength);
connect(mPlayerWidget, &PlayerWidget::message, this, &BeetPlayer::setMessage);
connect(mPlayerWidget, &PlayerWidget::setWinTitle, this, &BeetPlayer::setWindowTitle);
createStatusbar();
setCentralWidget(mPlayerWidget);
splash.showMessage(tr("Populating..."), Qt::AlignHCenter, Qt::yellow);
qApp->processEvents();
mPlayerWidget->doStop();
//mPlayerWidget->doPopulateByArtist();
splash.showMessage(tr("Reading settings..."), Qt::AlignHCenter, Qt::yellow);
qApp->processEvents();
mPlayerWidget->readSettings();
mPlayerWidget->randomPlay();
}
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::readSettings(){
QSettings s;
bool useAltColors = s.value("usealtcolors", false).toBool();
if(useAltColors){
QPalette curPal = qApp->palette();
QVariant baseColorV = s.value("basecolor", palette().base().color());
QColor baseColor = baseColorV.value<QColor>();
curPal.setColor(QPalette::Base, baseColor);
QVariant altColorV = s.value("altcolor", palette().alternateBase().color());
QColor altColor = altColorV.value<QColor>();
curPal.setColor(QPalette::AlternateBase, altColor);
qApp->setPalette(curPal);
}
}
void BeetPlayer::createGlobalActions(){
QAction *quitA = new QAction(tr("Quit"), this);
quitA->setShortcut(tr("CTRL+Q"));
connect(quitA, &QAction::triggered, qApp, &QApplication::closeAllWindows);
quitA->setData(Globals::QuitAction);
Globals::instance()->addAction(quitA);
QAction *configA = new QAction(QIcon(":/chastity_belt_light.png"), tr("Configure..."), this);
connect(configA, &QAction::triggered, this, &BeetPlayer::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::setPlayMode(const QString &playMode){
mActionL->setText(playMode);
}
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 = static_cast<int>((seconds / 60 / 60));
int days = 0;
if(h > 24){
days = h / 24;
h = h % 24;
}
int min = (seconds / 60) % 60;
int secs = seconds % 60;
QString r;
if(days > 0){
r = QString("%1d, %2:%3:%4h").arg(days, 1, 10, QChar('0')).arg(h, 2, 10, QChar('0')).arg(min, 2, 10, QChar('0')).arg(secs, 2, 10, QChar('0'));
}else{
r = QString("%1:%2:%3h").arg(h, 2, 10, QChar('0')).arg(min, 2, 10, QChar('0')).arg(secs, 2, 10, QChar('0'));
}
mPlaylistDurL->setText(r);
}
void BeetPlayer::setMessage(const QString &msg){
mGeneralL->setText(msg);
}
void BeetPlayer::createStatusbar(){
mGeneralL = new QLabel;
mGeneralL->setFrameStyle(QFrame::Panel | QFrame::Sunken);
mGeneralL->setFont(QFont("courier"));
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);
}
void BeetPlayer::closeEvent(QCloseEvent *event){
Q_UNUSED(event);
qApp->quit();
}
#include <QDebug>
void BeetPlayer::keyPressEvent(QKeyEvent *e){
if(e->modifiers() & Qt::AltModifier){
const QActionGroup *ag = mPlayerWidget->bottomAG();
int keyNum = e->key();
if(keyNum < Qt::Key_7 && keyNum > Qt::Key_0){
qDebug() << keyNum << (keyNum - Qt::Key_1);
int action = keyNum - Qt::Key_1;
ag->actions().at(action)->trigger();
return;
}
}
return QMainWindow::keyPressEvent(e);
}
|