Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- #include <QTime>
- //...The player tick interval
- #define TICK_INTERVAL 1000 //... = 1000ms
- //...__construct
- MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass)
- {
- //...Show the Main window
- ui->setupUi(this);
- ui->lcdTime->display("00:00"); //...Reset the timer
- player = new Phonon::MediaObject;
- settings = new QSettings("./settings.cnf", QSettings::NativeFormat, this);
- playing = false;
- Phonon::AudioOutput *output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
- Phonon::createPath(player, output);
- Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider(player, this);
- Phonon::VolumeSlider *volumeSlider = new Phonon::VolumeSlider(output, this);
- ui->sliders->addWidget(seekSlider);
- ui->sliders->addWidget(volumeSlider);
- player->setTickInterval(TICK_INTERVAL);
- //...Connect with menu items
- connect (ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(AboutQtDialog()));
- connect (ui->actionAbout_this_application, SIGNAL(triggered()), this, SLOT(AboutAppDialog()));
- connect (ui->action_Next_track, SIGNAL(triggered()), this, SLOT(NextTrack()));
- connect (ui->action_Previous_track, SIGNAL(triggered()), this, SLOT(PreviousTrack()));
- connect (ui->action_Open_File, SIGNAL(triggered()), this, SLOT(Load()));
- connect (ui->action_Play_Pause, SIGNAL(triggered()), this, SLOT(PlayPause()));
- connect (ui->action_Quit, SIGNAL(triggered()), this, SLOT(close()));
- connect (ui->action_Stop, SIGNAL(triggered()), this, SLOT(Stop()));
- //...Connect with player
- connect (player, SIGNAL(tick(qint64)), this, SLOT(Timer(qint64)));
- //...Connect with clasic buttons
- connect (ui->btnStop, SIGNAL(clicked()), this, SLOT(Stop()));
- connect (ui->btnPrevious, SIGNAL(clicked()), this, SLOT(PreviousTrack()));
- connect (ui->btnNext, SIGNAL(clicked()), this, SLOT(NextTrack()));
- connect (ui->btnLoad, SIGNAL(clicked()), this, SLOT(Load()));
- connect (ui->btnAbout, SIGNAL(clicked()), this, SLOT(AboutAppDialog()));
- connect (ui->btnPlay, SIGNAL(clicked()), this, SLOT(PlayPause()));
- connect (ui->listPlaylist, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(PlayPlaylist(QModelIndex))); //...Vybrani souboru z playlistu
- }
- //...Destructor
- MainWindow::~MainWindow()
- {
- //...Write settings
- //settings
- //...Destruct the ui
- delete ui;
- }
- void MainWindow::AboutAppDialog() {
- QMessageBox dlgapp;
- dlgapp.setText(trUtf8("<center><font size=18>TuxPlay 0.1.0</font><br><br>Based on Qt 4.5.0<br><br><br>Copyright 2009 by <a href=mailto:seberm@gmail.com>Otto Šabart</a>.<br>All rights reserved.</center>"));
- dlgapp.show();
- dlgapp.exec();
- }
- void MainWindow::AboutQtDialog() {
- QMessageBox dlgqt;
- dlgqt.aboutQt(this, tr("About Qt Dialog"));
- }
- void MainWindow::Load() {
- QStringList filters;
- filters << tr("All supported music files (*.mp3 *.waw *.ogg)")
- << tr("Any files (*)");
- QFileDialog dlg;
- dlg.Detail;
- dlg.ExistingFiles;
- dlg.setNameFilters(filters);
- dlg.AcceptOpen;
- if(dlg.exec()) {
- ui->listPlaylist->addItems(dlg.selectedFiles()); //...load to playlist
- ui->lblActSong->setText(player->currentSource().fileName());
- ui->btnStop->setEnabled(true);
- ui->btnPlay->setEnabled(true);
- }
- }
- void MainWindow::PlayPause() {
- if (playing) {
- player->pause();
- playing = false;
- ui->btnPlay->setText(tr("Paused"));
- } else {
- playing = true;
- if (ui->listPlaylist->count()) {
- if (ui->listPlaylist->currentItem()) {
- if (player->currentSource().fileName() != ui->listPlaylist->currentItem()->text()) {
- player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->currentItem()->text()));
- ui->lblActSong->setText(ui->listPlaylist->currentItem()->text());
- }
- } else {
- player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->item(0)->text()));
- ui->listPlaylist->setCurrentRow(0);
- ui->lblActSong->setText(ui->listPlaylist->item(0)->text());
- }
- player->play();
- ui->btnPlay->setText(tr("Pause"));
- }
- }
- }
- void MainWindow::PlayPlaylist(const QModelIndex &model) {
- if (playing) {
- playing = false;
- player->stop();
- }
- player->setCurrentSource(Phonon::MediaSource(model.data().toString()));
- player->play();
- playing = true;
- }
- void MainWindow::Stop() {
- player->stop();
- player->clear();
- playing = false;
- ui->lblActSong->setText(tr("File not loaded"));
- ui->btnPlay->setText(tr("Play"));
- }
- void MainWindow::NextTrack() {
- SwitchTrack(1); //... +1 - switch to next track
- }
- void MainWindow::PreviousTrack() {
- SwitchTrack(-1); //... -1 switch to previous track
- }
- void MainWindow::SwitchTrack(int row = 0) {
- row += ui->listPlaylist->currentRow();
- player->stop();
- playing = false;
- player->clear();
- if (ui->listPlaylist->item(row)) {
- player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->item(row)->text()));
- ui->listPlaylist->setCurrentRow(row);
- ui->lblActSong->setText(ui->listPlaylist->item(row)->text());
- } else {
- row = 0;
- ui->listPlaylist->setCurrentRow(row);
- player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->item(row)->text()));
- ui->listPlaylist->setCurrentRow(row);
- ui->lblActSong->setText(ui->listPlaylist->item(row)->text());
- }
- player->play();
- playing = true;
- }
- void MainWindow::Timer(qint64 time) {
- QTime showTime(0, (time / 60000) % 60, (time / 1000) % 60); //...(hours, minutes, seconds, miliseconds)
- ui->lcdTime->display(showTime.toString("mm:ss"));
- }
Add Comment
Please, Sign In to add comment