Blame view

PlayerControls.cpp 5.47 KB
3f2992b2c   bostx   V1.0
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
  #include <QTime>
  #include <QStyle>
  #include <QHBoxLayout>
  #include <QVBoxLayout>
  
  #include "PlayerControls.h"
  
  /////////////////
  // constructor //
  /////////////////
  
  PlayerControls::PlayerControls(QWidget *parent)
    : QWidget(parent),
      playerState(QMediaPlayer::StoppedState),
      m_isMuted(false)
  {
    m_durationSlider = new QSlider(Qt::Horizontal);
    connect(m_durationSlider, SIGNAL(sliderMoved(int)), this, SLOT(sliderMoved(int)));
  
    m_currentTimeLabel = new QLabel("--:--");
    QLabel *separatorLabel = new QLabel("/");
    m_durationLabel = new QLabel("--:--");
  
    QToolButton *previousButton = new QToolButton(this);
    previousButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipBackward));
  
    m_playButton = new QToolButton;
    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    connect(m_playButton, SIGNAL(clicked()), this, SLOT(playClicked()));
  
    QToolButton *nextButton = new QToolButton(this);
    nextButton->setIcon(style()->standardIcon(QStyle::SP_MediaSkipForward));
  
    QToolButton *stopButton = new QToolButton;
    stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
    connect(stopButton, SIGNAL(clicked()), this, SLOT(stopClicked()));
  
    m_muteButton = new QToolButton;
    m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
    connect(m_muteButton, SIGNAL(clicked()), this, SLOT(switchMuted()));
  
    QLabel *rateMult = new QLabel("x");
    m_rateBox = new QComboBox;
    m_rateBox->addItem("0.25");
    m_rateBox->addItem("0.5");
    m_rateBox->addItem("1.0");
    m_rateBox->addItem("2.0");
    m_rateBox->addItem("4.0");
    m_rateBox->setCurrentIndex(2);
    connect(m_rateBox, SIGNAL(currentTextChanged(const QString&)), this, SLOT(rateChanged(const QString&)));
  
    QHBoxLayout *durationLayout = new QHBoxLayout;
    durationLayout->addWidget(m_durationSlider);
    durationLayout->addWidget(m_currentTimeLabel);
    durationLayout->addWidget(separatorLabel);
    durationLayout->addWidget(m_durationLabel);
  
    QHBoxLayout *buttonBarLayout = new QHBoxLayout;
    buttonBarLayout->addWidget(previousButton);
    buttonBarLayout->addWidget(m_playButton);
    buttonBarLayout->addWidget(nextButton);
    buttonBarLayout->addWidget(stopButton);
    buttonBarLayout->addWidget(m_muteButton);
    buttonBarLayout->addWidget(m_durationSlider);
    buttonBarLayout->addWidget(m_durationLabel);
    buttonBarLayout->addWidget(m_rateBox);
    buttonBarLayout->addWidget(rateMult);
  
    QVBoxLayout *controlsLayout = new QVBoxLayout;
    controlsLayout->addLayout(durationLayout);
    controlsLayout->addLayout(buttonBarLayout);
      
    setLayout(controlsLayout);
  }
  
  ////////////////
  // destructor //
  ////////////////
  
  PlayerControls::~PlayerControls()
  {
  }
  
  void PlayerControls::reset()
  {
    m_currentTimeLabel->setText("--:--");
    m_durationLabel->setText("--:--");
    setEnabled(false);
  }
  
  /////////////////////////////////////////
  // public slots in reaction to signals //
  //        emitted by the player        //
  /////////////////////////////////////////
  
  void PlayerControls::durationChanged(qint64 duration)
  {
    QString tStr;
  
    m_durationSlider->setMaximum(duration);
  
    duration /= 1000;
    QTime totalTime(duration / 3600, (duration / 60) % 60, duration % 60);
  
    if (duration > 3600)
      m_timeFormat = "hh:mm:ss";
    else
      m_timeFormat = "mm:ss";
  
    tStr = totalTime.toString(m_timeFormat);
    m_durationLabel->setText(tStr);
  }
  
  void PlayerControls::positionChanged(qint64 position)
  {
    QString tStr;
    
    m_durationSlider->setSliderPosition(position);
  
    position /= 1000;
    QTime currentTime((position / 3600) % 60, (position / 60) % 60, position % 60);
    tStr = currentTime.toString(m_timeFormat);
    m_currentTimeLabel->setText(tStr);
  }
  
  void PlayerControls::mediaStatusChanged(QMediaPlayer::MediaStatus status)
  {
    if (status == QMediaPlayer::EndOfMedia)
      stopClicked();
  }
  
  ////////////////////////////////
  // private slots used to emit //
  //    signals to the player   //
  ////////////////////////////////
  
  void PlayerControls::playClicked()
  {
    if (playerState == QMediaPlayer::StoppedState || playerState == QMediaPlayer::PausedState) {
      emit play();
      m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPause));
      playerState = QMediaPlayer::PlayingState;
    }
    else {
      emit pause();
      m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
      playerState = QMediaPlayer::PausedState;
    }
  }
  
  void PlayerControls::stopClicked()
  {
    m_playButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
    playerState = QMediaPlayer::StoppedState;
    emit stop();
  
    sliderMoved(0);
    
    m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
    m_muteButton->setEnabled(true);
    emit setMuted(false);
  
    m_rateBox->setCurrentIndex(2);
    emit playbackRateChanged(1.0);    
  }
  
  void PlayerControls::switchMuted()
  {
    m_isMuted = !m_isMuted;
  
    emit setMuted(m_isMuted);
  
    if (m_isMuted)
      m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolumeMuted));
    else
      m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
  }
  
  void PlayerControls::sliderMoved(int value)
  {
    positionChanged(value);
    emit positionManuallyChanged(value);
  }
  
  void PlayerControls::rateChanged(const QString &rate)
  {
    qreal doubleRate = rate.toDouble();
    emit playbackRateChanged(doubleRate);
  
    if (doubleRate != 1.0) {
      emit setMuted(true);
      m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolumeMuted));
      m_muteButton->setEnabled(false);
    }
    else {
      emit setMuted(false);
      m_muteButton->setIcon(style()->standardIcon(QStyle::SP_MediaVolume));
      m_muteButton->setEnabled(true);
    }
  }