seberm

seberm

Apr 28th, 2009
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QTime>
  4.  
  5.  
  6. //...The player tick interval
  7. #define TICK_INTERVAL 1000 //... = 1000ms
  8.  
  9.  
  10. //...__construct
  11. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindowClass)
  12. {
  13.     //...Show the Main window
  14.     ui->setupUi(this);
  15.     ui->lcdTime->display("00:00"); //...Reset the timer
  16.  
  17.     player = new Phonon::MediaObject;
  18.     settings = new QSettings("./settings.cnf", QSettings::NativeFormat, this);
  19.  
  20.     playing = false;
  21.  
  22.     Phonon::AudioOutput *output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
  23.     Phonon::createPath(player, output);
  24.  
  25.     Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider(player, this);
  26.     Phonon::VolumeSlider *volumeSlider = new Phonon::VolumeSlider(output, this);
  27.  
  28.     ui->sliders->addWidget(seekSlider);
  29.     ui->sliders->addWidget(volumeSlider);
  30.  
  31.     player->setTickInterval(TICK_INTERVAL);
  32.  
  33.  
  34.  
  35.     //...Connect with menu items
  36.     connect (ui->actionAbout_Qt, SIGNAL(triggered()), this, SLOT(AboutQtDialog()));
  37.     connect (ui->actionAbout_this_application, SIGNAL(triggered()), this, SLOT(AboutAppDialog()));
  38.     connect (ui->action_Next_track, SIGNAL(triggered()), this, SLOT(NextTrack()));
  39.     connect (ui->action_Previous_track, SIGNAL(triggered()), this, SLOT(PreviousTrack()));
  40.     connect (ui->action_Open_File, SIGNAL(triggered()), this, SLOT(Load()));
  41.     connect (ui->action_Play_Pause, SIGNAL(triggered()), this, SLOT(PlayPause()));
  42.     connect (ui->action_Quit, SIGNAL(triggered()), this, SLOT(close()));
  43.     connect (ui->action_Stop, SIGNAL(triggered()), this, SLOT(Stop()));
  44.  
  45.     //...Connect with player
  46.     connect (player, SIGNAL(tick(qint64)), this, SLOT(Timer(qint64)));
  47.  
  48.     //...Connect with clasic buttons
  49.     connect (ui->btnStop, SIGNAL(clicked()), this, SLOT(Stop()));
  50.     connect (ui->btnPrevious, SIGNAL(clicked()), this, SLOT(PreviousTrack()));
  51.     connect (ui->btnNext, SIGNAL(clicked()), this, SLOT(NextTrack()));
  52.     connect (ui->btnLoad, SIGNAL(clicked()), this, SLOT(Load()));
  53.     connect (ui->btnAbout, SIGNAL(clicked()), this, SLOT(AboutAppDialog()));
  54.     connect (ui->btnPlay, SIGNAL(clicked()), this, SLOT(PlayPause()));
  55.     connect (ui->listPlaylist, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(PlayPlaylist(QModelIndex))); //...Vybrani souboru z playlistu
  56. }
  57.  
  58.  
  59.  
  60. //...Destructor
  61. MainWindow::~MainWindow()
  62. {
  63.     //...Write settings
  64.     //settings
  65.  
  66.     //...Destruct the ui
  67.     delete ui;
  68. }
  69.  
  70.  
  71. void MainWindow::AboutAppDialog() {
  72.     QMessageBox dlgapp;
  73.  
  74.     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 &#352;abart</a>.<br>All rights reserved.</center>"));
  75.     dlgapp.show();
  76.     dlgapp.exec();
  77. }
  78.  
  79.  
  80. void MainWindow::AboutQtDialog() {
  81.     QMessageBox dlgqt;
  82.     dlgqt.aboutQt(this, tr("About Qt Dialog"));
  83. }
  84.  
  85.  
  86. void MainWindow::Load() {
  87.     QStringList filters;
  88.     filters << tr("All supported music files (*.mp3 *.waw *.ogg)")
  89.             << tr("Any files (*)");
  90.  
  91.     QFileDialog dlg;
  92.     dlg.Detail;
  93.     dlg.ExistingFiles;
  94.     dlg.setNameFilters(filters);
  95.     dlg.AcceptOpen;
  96.  
  97.     if(dlg.exec()) {
  98.         ui->listPlaylist->addItems(dlg.selectedFiles()); //...load to playlist
  99.         ui->lblActSong->setText(player->currentSource().fileName());
  100.         ui->btnStop->setEnabled(true);
  101.         ui->btnPlay->setEnabled(true);
  102.     }
  103. }
  104.  
  105.  
  106. void MainWindow::PlayPause() {
  107.     if (playing) {
  108.         player->pause();
  109.         playing = false;
  110.         ui->btnPlay->setText(tr("Paused"));
  111.     } else {
  112.         playing = true;
  113.  
  114.         if (ui->listPlaylist->count()) {
  115.             if (ui->listPlaylist->currentItem()) {
  116.                 if (player->currentSource().fileName() != ui->listPlaylist->currentItem()->text()) {
  117.                     player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->currentItem()->text()));
  118.                     ui->lblActSong->setText(ui->listPlaylist->currentItem()->text());
  119.                 }
  120.             } else {
  121.                 player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->item(0)->text()));
  122.                 ui->listPlaylist->setCurrentRow(0);
  123.                 ui->lblActSong->setText(ui->listPlaylist->item(0)->text());
  124.             }
  125.             player->play();
  126.             ui->btnPlay->setText(tr("Pause"));
  127.         }
  128.     }
  129. }
  130.  
  131.  
  132. void MainWindow::PlayPlaylist(const QModelIndex &model) {
  133.     if (playing) {
  134.         playing = false;
  135.         player->stop();
  136.     }
  137.  
  138.     player->setCurrentSource(Phonon::MediaSource(model.data().toString()));
  139.     player->play();
  140.     playing = true;
  141. }
  142.  
  143.  
  144. void MainWindow::Stop() {
  145.     player->stop();
  146.     player->clear();
  147.     playing = false;
  148.     ui->lblActSong->setText(tr("File not loaded"));
  149.     ui->btnPlay->setText(tr("Play"));
  150. }
  151.  
  152.  
  153. void MainWindow::NextTrack() {
  154.     SwitchTrack(1); //... +1 - switch to next track
  155. }
  156.  
  157.  
  158. void MainWindow::PreviousTrack() {
  159.     SwitchTrack(-1); //... -1 switch to previous track
  160. }
  161.  
  162.  
  163. void MainWindow::SwitchTrack(int row = 0) {
  164.     row += ui->listPlaylist->currentRow();
  165.  
  166.     player->stop();
  167.     playing = false;
  168.     player->clear();
  169.  
  170.     if (ui->listPlaylist->item(row)) {
  171.         player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->item(row)->text()));
  172.         ui->listPlaylist->setCurrentRow(row);
  173.         ui->lblActSong->setText(ui->listPlaylist->item(row)->text());
  174.     } else {
  175.         row = 0;
  176.         ui->listPlaylist->setCurrentRow(row);
  177.         player->setCurrentSource(Phonon::MediaSource(ui->listPlaylist->item(row)->text()));
  178.         ui->listPlaylist->setCurrentRow(row);
  179.         ui->lblActSong->setText(ui->listPlaylist->item(row)->text());
  180.     }
  181.  
  182.     player->play();
  183.     playing = true;
  184. }
  185.  
  186.  
  187. void MainWindow::Timer(qint64 time) {
  188.     QTime showTime(0, (time / 60000) % 60, (time / 1000) % 60); //...(hours, minutes, seconds, miliseconds)
  189.  
  190.     ui->lcdTime->display(showTime.toString("mm:ss"));
  191. }
  192.  
Add Comment
Please, Sign In to add comment