aboutsummaryrefslogtreecommitdiff
path: root/lumina-fm/widgets/MultimediaWidget.cpp
blob: 406f9098d05e6cc4fa49458edcfed60397f572ba (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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2015, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "MultimediaWidget.h"
#include "ui_MultimediaWidget.h"

#include <QTimer>

MultimediaWidget::MultimediaWidget(QWidget *parent) : QWidget(parent), ui(new Ui::MultimediaWidget){
  ui->setupUi(this); //load the designer file
	
  //Add in the special QMultimediaWidgets
  mediaObj = new QMediaPlayer(this);
    mediaObj->setVolume(100);
    mediaObj->setNotifyInterval(500); //only every 1/2 second update
  videoDisplay = new QVideoWidget(this);
    videoDisplay->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    ui->videoLayout->addWidget(videoDisplay);
    mediaObj->setVideoOutput(videoDisplay);
    videoDisplay->setVisible(false);
	
  UpdateIcons();
  UpdateText();
	
  //Connect the special signals/slots for the media object
  connect(mediaObj, SIGNAL(durationChanged(qint64)), this, SLOT(playerDurationChanged(qint64)) );
  connect(mediaObj, SIGNAL(seekableChanged(bool)), ui->playerSlider, SLOT(setEnabled(bool)) );
  connect(mediaObj, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)), this, SLOT(playerStatusChanged(QMediaPlayer::MediaStatus)) );
  connect(mediaObj, SIGNAL(positionChanged(qint64)), this, SLOT(playerTimeChanged(qint64)) );
  connect(mediaObj, SIGNAL(stateChanged(QMediaPlayer::State)), this, SLOT(playerStateChanged(QMediaPlayer::State)) );
  connect(mediaObj, SIGNAL(videoAvailableChanged(bool)), this, SLOT(playerVideoAvailable(bool)) );
  connect(mediaObj, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(playerError()) );
  
  //Disable some of the initial states
  ui->tool_player_stop->setEnabled(false); //nothing to stop yet
  ui->tool_player_pause->setVisible(false); //nothing to pause yet
  ui->playerSlider->setEnabled(false); //nothing to seek yet
}

MultimediaWidget::~MultimediaWidget(){
	
}

// ================
//    PUBLIC SLOTS
// ================
void MultimediaWidget::ClearPlaylist(){
  mediaObj->stop();
  ui->combo_player_list->clear();
}

void MultimediaWidget::LoadMultimedia(QList<LFileInfo> list){
  for(int i=0; i<list.length(); i++){
    if(list[i].isAVFile()){ ui->combo_player_list->addItem(list[i].fileName(), list[i].absoluteFilePath() ); }
  }
}

//Theme change functions
void MultimediaWidget::UpdateIcons(){
  ui->tool_player_next->setIcon( LXDG::findIcon("media-skip-forward","") );
  ui->tool_player_prev->setIcon( LXDG::findIcon("media-skip-backward","") );
  ui->tool_player_pause->setIcon( LXDG::findIcon("media-playback-pause","") );
  ui->tool_player_play->setIcon( LXDG::findIcon("media-playback-start","") );
  ui->tool_player_stop->setIcon( LXDG::findIcon("media-playback-stop","") );
}

void MultimediaWidget::UpdateText(){
  ui->retranslateUi(this);
}


// =================
//       PRIVATE
// =================
QString MultimediaWidget::msToText(qint64 ms){
  QString disp;
  if(ms>3600000){
    disp.append( QString::number(ms/3600000)+":" ); 
    ms = ms%3600000; 
  }
  if(ms>60000){
    disp.append( QString::number(ms/60000)+":" );
    ms = ms%60000;
  }else{
    disp.append("0:");
  }
  if(ms>1000){
    if(ms>=10000){ disp.append( QString::number(ms/1000) ); }
    else{ disp.append( "0"+QString::number(ms/1000) ); }
  }else{
    disp.append("00");
  }
  return disp;
}

// =================
//    PRIVATE SLOTS
// =================
void MultimediaWidget::playerStatusChanged(QMediaPlayer::MediaStatus stat){
  //Only use this for end-of-file detection - use the state detection otherwise
  if(stat == QMediaPlayer::EndOfMedia){
    if(!mediaObj->isMuted()){ playerFinished(); } //make sure it is not being seeked right now
  }	  
}

void MultimediaWidget::playerStateChanged(QMediaPlayer::State newstate){
  //This function keeps track of updating the visuals of the player
  bool running = false;
  bool showVideo = false;
  QString msg;
  switch(newstate){
    case QMediaPlayer::PlayingState:
	running=true;
	showVideo = mediaObj->isVideoAvailable();
	msg = "";//mediaObj->metaData(Phonon::TitleMetaData).join(" ");
	if(msg.simplified().isEmpty()){ msg = ui->combo_player_list->currentText(); }
	ui->label_player_novideo->setText(tr("Playing:")+"\n"+msg);
	break;
    case QMediaPlayer::PausedState:
	showVideo=videoDisplay->isVisible(); //don't change the screen
	break;
    case QMediaPlayer::StoppedState:
	ui->label_player_novideo->setText(tr("Stopped"));
        break;
  }
  ui->tool_player_play->setVisible(!running);
  ui->tool_player_pause->setVisible(running);
  ui->tool_player_stop->setEnabled(running);
  ui->label_player_novideo->setVisible(!showVideo);
  videoDisplay->setVisible(showVideo);
}

void MultimediaWidget::playerVideoAvailable(bool showVideo){
  ui->label_player_novideo->setVisible(!showVideo);
  videoDisplay->setVisible(showVideo);	
}

void MultimediaWidget::playerDurationChanged(qint64 dur){
    if(dur < 0){ return; } //not ready yet
    ui->playerSlider->setMaximum(mediaObj->duration());
    playerTTime = msToText(dur);   	
}

void MultimediaWidget::playerTimeChanged(qint64 ctime){
  if(mediaObj->isMuted() || playerTTime.isEmpty() ){ return; } //currently being moved
  ui->playerSlider->setSliderPosition(ctime);
}

void MultimediaWidget::playerError(){
  QString msg = QString(tr("Error Playing File: %1"));
  msg = msg.arg( mediaObj->currentMedia().canonicalUrl().fileName() );
  msg.append("\n"+mediaObj->errorString());
  ui->label_player_novideo->setText(msg);
}

void MultimediaWidget::playerFinished(){
  if(ui->combo_player_list->currentIndex()<(ui->combo_player_list->count()-1) && ui->check_player_gotonext->isChecked()){
    ui->combo_player_list->setCurrentIndex( ui->combo_player_list->currentIndex()+1 );
    QTimer::singleShot(0,this,SLOT(playerStart()));
  }else{
    ui->label_player_novideo->setText(tr("Finished"));
  }
}

void MultimediaWidget::on_tool_player_play_clicked(){
  if(mediaObj->state()==QMediaPlayer::PausedState \
      && mediaObj->currentMedia().canonicalUrl().fileName()==ui->combo_player_list->currentText() ){
    mediaObj->play();
  }else{
    mediaObj->stop();
    //Get the selected file path
    QString filePath = ui->combo_player_list->currentData().toString();
      mediaObj->setMedia( QUrl::fromLocalFile(filePath) );
      playerTTime.clear();
      ui->playerSlider->setEnabled(mediaObj->isSeekable());
      mediaObj->play();
  }
}

void MultimediaWidget::on_combo_player_list_currentIndexChanged(int index){
  ui->tool_player_next->setEnabled( ui->combo_player_list->count() > (index+1) );	
  ui->tool_player_prev->setEnabled( (index-1) >= 0 );
}

void MultimediaWidget::on_tool_player_next_clicked(){
  ui->combo_player_list->setCurrentIndex( ui->combo_player_list->currentIndex()+1);
  if(mediaObj->state()!=QMediaPlayer::StoppedState){ on_tool_player_play_clicked(); }
}

void MultimediaWidget::on_tool_player_prev_clicked(){
  ui->combo_player_list->setCurrentIndex( ui->combo_player_list->currentIndex()-1);	
  if(mediaObj->state()!=QMediaPlayer::StoppedState){ on_tool_player_play_clicked(); }
}

void MultimediaWidget::on_tool_player_pause_clicked(){ 
  mediaObj->pause(); 
}

void MultimediaWidget::on_tool_player_stop_clicked(){ 
  mediaObj->stop(); 
}

//Slider controls
void MultimediaWidget::on_playerSlider_sliderPressed(){
  mediaObj->setMuted(true);
  mediaObj->pause();
}
void MultimediaWidget::on_playerSlider_sliderReleased(){
  if(mediaObj->state()==QMediaPlayer::PausedState){ mediaObj->play(); }
  mediaObj->setMuted(false);
}
void MultimediaWidget::on_playerSlider_valueChanged(int val){
  ui->label_player_runstats->setText( msToText(val)+"/"+playerTTime );
  if(mediaObj->isMuted()){ mediaObj->setPosition(val); } //currently seeking
}
	
bgstack15