#include #include #include #include #include #include #include #include #include #include #include #include #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(); curPal.setColor(QPalette::Base, baseColor); QVariant altColorV = s.value("altcolor", palette().alternateBase().color()); QColor altColor = altColorV.value(); 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((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 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); }