| 1 | /* |
| 2 | Copyright (C) 2011 Mario Stephan <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU Lesser General Public License as published |
| 6 | by the Free Software Foundation; either version 2.1 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include "playerwidget.h" |
| 19 | #include "player.h" |
| 20 | #include "trackanalyser.h" |
| 21 | #include "ui_playerwidget.h" |
| 22 | #include "vumeter.h" |
| 23 | |
| 24 | #include <QDragEnterEvent> |
| 25 | |
| 26 | struct PlayerWidgetPrivate { |
| 27 | bool isEndAnnounced; |
| 28 | }; |
| 29 | |
| 30 | PlayerWidget::PlayerWidget(QWidget* parent) |
| 31 | : QWidget(parent) |
| 32 | , ui(new Ui::PlayerWidget) |
| 33 | , songTime(0) |
| 34 | , mTrackFinishEmitTime(12000) |
| 35 | , m_CurrentTrack(nullptr) |
| 36 | , remainCueTime(0) |
| 37 | , m_isStarted(false) |
| 38 | , m_isHanging(false) |
| 39 | , p(new PlayerWidgetPrivate) |
| 40 | { |
| 41 | ui->setupUi(this); |
| 42 | |
| 43 | p->isEndAnnounced = false; |
| 44 | |
| 45 | //create the player |
| 46 | player = new Player(this); |
| 47 | player->prepare(); |
| 48 | |
| 49 | ui->butFwd->setIcon(QIcon(":forward.png")); |
| 50 | ui->butRew->setIcon(QIcon(":backward.png")); |
| 51 | ui->butPlay->setIcon(QIcon(":play.png")); |
| 52 | ui->butPlay->setChecked(false); |
| 53 | ui->butFwd->setIconSize(QSize(26, 26)); |
| 54 | ui->butRew->setIconSize(QSize(26, 26)); |
| 55 | ui->butPlay->setIconSize(QSize(26, 26)); |
| 56 | ui->butCue->setChecked(false); |
| 57 | |
| 58 | vuMeter = ui->vuMeter; |
| 59 | vuMeter->setOrientation(Qt::Horizontal); |
| 60 | vuMeter->LevelColorNormal.setRgb(112, 146, 190); |
| 61 | vuMeter->LevelColorHigh.setRgb(218, 59, 9); |
| 62 | vuMeter->LevelColorOff.setRgb(31, 45, 65); |
| 63 | vuMeter->setLinesPerSegment(2); |
| 64 | vuMeter->setSpacesBetweenSegments(1); |
| 65 | vuMeter->setSegmentsPerPeak(2); |
| 66 | |
| 67 | timerLevel = new QTimer(this); |
| 68 | connect(timerLevel, SIGNAL(timeout()), SLOT(timerLevel_timeOut())); |
| 69 | |
| 70 | timerPosition = new QTimer(this); |
| 71 | connect(timerPosition, SIGNAL(timeout()), SLOT(timerPosition_timeOut())); |
| 72 | |
| 73 | connect(player, SIGNAL(finish()), this, SLOT(playerFinished())); |
| 74 | connect(player, SIGNAL(error()), this, SLOT(playerError())); |
| 75 | connect(player, SIGNAL(loadFinished()), this, SLOT(playerLoaded())); |
| 76 | |
| 77 | ui->lblTitle->setText(""); |
| 78 | ui->lblInfo->setText(""); |
| 79 | |
| 80 | QFont font = ui->lblInfo->font(); |
| 81 | QFont fonttime = ui->lblTime->font(); |
| 82 | #if defined(Q_OS_DARWIN) |
| 83 | int newSize = font.pointSize() - 4; |
| 84 | fonttime.setPointSize(fonttime.pointSize() + 2); |
| 85 | #else |
| 86 | int newSize = font.pointSize() - 1; |
| 87 | #endif |
| 88 | font.setPointSize(newSize); |
| 89 | ui->lblInfo->setFont(font); |
| 90 | ui->lblTime->setFont(fonttime); |
| 91 | ui->lblTimeRemain->setFont(fonttime); |
| 92 | |
| 93 | m_isStarted = false; |
| 94 | setAcceptDrops(true); |
| 95 | this->stop(); |
| 96 | |
| 97 | trackanalyser = new TrackAnalyser(this); |
| 98 | connect(trackanalyser, SIGNAL(finishGain()), this, SLOT(analyseGainFinished())); |
| 99 | } |
| 100 | |
| 101 | PlayerWidget::~PlayerWidget() |
| 102 | { |
| 103 | delete player; |
| 104 | delete timerPosition; |
| 105 | delete timerLevel; |
| 106 | delete trackanalyser; |
| 107 | trackanalyser = nullptr; |
| 108 | delete p; |
| 109 | } |
| 110 | |
| 111 | void PlayerWidget::setVolume(double volume) |
| 112 | { |
| 113 | player->setVolume(volume); |
| 114 | } |
| 115 | |
| 116 | void PlayerWidget::setGain(double gain) |
| 117 | { |
| 118 | player->setGain(gain); |
| 119 | } |
| 120 | |
| 121 | void PlayerWidget::setInfo(QPair<int, int> info) |
| 122 | { |
| 123 | QString strTrack = (info.first > 1) ? tr("Tracks") : tr("Track"); |
| 124 | ui->lblInfo->setText(QString("%1 %2 %3 %4") |
| 125 | .arg(info.first) |
| 126 | .arg(strTrack) |
| 127 | .arg(Track::prettyTime(info.second)) |
| 128 | .arg(tr("Hours"))); |
| 129 | } |
| 130 | |
| 131 | void PlayerWidget::setEqualizer(EqBand band, int value) |
| 132 | { |
| 133 | //ranging from -24.0 to +12.0. |
| 134 | player->setEqualizer("band" + QString::number(band), (value - 240) / 10.0); |
| 135 | } |
| 136 | |
| 137 | void PlayerWidget::setPositionMarkers() |
| 138 | { |
| 139 | if (trackanalyser->finished()) { |
| 140 | if (m_skipSilentEnd && trackanalyser->endPosition() > QTime(0, 0)) { |
| 141 | qDebug() << Q_FUNC_INFO << "endPosition:" << trackanalyser->endPosition(); |
| 142 | qDebug() << Q_FUNC_INFO << "length:" << trackanalyser->length(); |
| 143 | remainCueTime = trackanalyser->endPosition().msecsTo(trackanalyser->length()); |
| 144 | } else |
| 145 | remainCueTime = 0; |
| 146 | |
| 147 | ui->txtCue->setText("-" + QString::number(remainCueTime / 1000)); |
| 148 | } |
| 149 | |
| 150 | if (!m_isStarted && m_skipSilentBegin && trackanalyser->finished()) { |
| 151 | player->setPosition(trackanalyser->startPosition()); |
| 152 | ui->butCue->setChecked(true); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void PlayerWidget::play() |
| 157 | { |
| 158 | m_isStarted = true; |
| 159 | if (m_CurrentTrack) { |
| 160 | ui->butPlay->setIcon(QIcon(":pause.png")); |
| 161 | ui->butPlay->setChecked(true); |
| 162 | player->play(); |
| 163 | ui->butCue->setChecked(false); |
| 164 | timerLevel->start(50); |
| 165 | timerPosition->start(100); |
| 166 | Q_EMIT statusChanged(m_isStarted); |
| 167 | } else |
| 168 | m_isHanging = true; |
| 169 | } |
| 170 | |
| 171 | void PlayerWidget::pause() |
| 172 | { |
| 173 | ui->butPlay->setIcon(QIcon(":play.png")); |
| 174 | ui->butPlay->setChecked(false); |
| 175 | m_isStarted = false; |
| 176 | player->pause(); |
| 177 | timerLevel->stop(); |
| 178 | timerPosition->stop(); |
| 179 | vuMeter->reset(); |
| 180 | Q_EMIT statusChanged(m_isStarted); |
| 181 | Q_EMIT levelChanged(0, 0); |
| 182 | } |
| 183 | |
| 184 | void PlayerWidget::stop() |
| 185 | { |
| 186 | ui->butPlay->setIcon(QIcon(":play.png")); |
| 187 | ui->butPlay->setChecked(false); |
| 188 | m_isStarted = false; |
| 189 | m_isHanging = false; |
| 190 | player->stop(); |
| 191 | timerLevel->stop(); |
| 192 | timerPosition->stop(); |
| 193 | vuMeter->reset(); |
| 194 | Q_EMIT statusChanged(m_isStarted); |
| 195 | Q_EMIT levelChanged(0, 0); |
| 196 | } |
| 197 | |
| 198 | void PlayerWidget::on_butPlay_clicked() |
| 199 | { |
| 200 | if (m_isStarted) { |
| 201 | this->pause(); |
| 202 | } else { |
| 203 | this->play(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | void PlayerWidget::analyseGainFinished() |
| 208 | { |
| 209 | qDebug() << Q_FUNC_INFO << ":" << objectName(); |
| 210 | // got gain factor -> emit |
| 211 | if (trackanalyser->gainDB() != TrackAnalyser::GAIN_INVALID) { |
| 212 | Q_EMIT gainChanged(trackanalyser->gainFactor()); |
| 213 | } |
| 214 | if (m_CurrentTrack) { |
| 215 | setPositionMarkers(); |
| 216 | updateTimeAndPositionDisplay(); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void PlayerWidget::timerLevel_timeOut() |
| 221 | { |
| 222 | vuMeter->setValueLeft(player->levelLeft()); |
| 223 | vuMeter->setValueRight(player->levelRight()); |
| 224 | Q_EMIT levelChanged(player->levelOutLeft(), player->levelOutRight()); |
| 225 | } |
| 226 | |
| 227 | void PlayerWidget::timerPosition_timeOut() |
| 228 | { |
| 229 | updateTimeAndPositionDisplay(); |
| 230 | } |
| 231 | |
| 232 | void PlayerWidget::dragEnterEvent(QDragEnterEvent* event) |
| 233 | { |
| 234 | //ToDo: remove forein classname tracklist" |
| 235 | if (!event->source()) |
| 236 | return; |
| 237 | event->setDropAction(Qt::CopyAction); |
| 238 | QString sourceSite = event->source()->objectName(); |
| 239 | QString dropSite = this->objectName(); |
| 240 | qDebug() << "PlayerWidget: dragEnterEvent: sourceSite=" << sourceSite << " dropSite=" << dropSite; |
| 241 | if (sourceSite.left(4) == dropSite.left(4) || sourceSite.left(9) == "tracklist") { |
| 242 | qDebug() << "PlayerWidget: dragEnterEvent: acceptProposedAction"; |
| 243 | event->acceptProposedAction(); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void PlayerWidget::dragMoveEvent(QDragMoveEvent* event) |
| 248 | { |
| 249 | event->acceptProposedAction(); |
| 250 | } |
| 251 | |
| 252 | void PlayerWidget::dropEvent(QDropEvent* event) |
| 253 | { |
| 254 | qDebug() << "PlayerWidget: dragEnterEvent: " << event->mimeData(); |
| 255 | if (event->mimeData()->hasUrls()) { |
| 256 | QList<QUrl> urlList = event->mimeData()->urls(); // returns list of QUrls |
| 257 | event->ignore(); |
| 258 | |
| 259 | if (urlList.size() > 0) // if at least one QUrl is present in list |
| 260 | { |
| 261 | //load first |
| 262 | loadFile(urlList.at(1)); |
| 263 | } |
| 264 | } else if (event->mimeData()->hasFormat("text/playlistitem")) { |
| 265 | |
| 266 | //decode playlistitem |
| 267 | QByteArray itemData = event->mimeData()->data("text/playlistitem"); |
| 268 | QDataStream stream(&itemData, QIODevice::ReadOnly); |
| 269 | QVector<QStringList> tags; |
| 270 | |
| 271 | stream >> tags; |
| 272 | event->setDropAction(Qt::MoveAction); |
| 273 | event->accept(); |
| 274 | |
| 275 | //publish dropped Tracks to connected playlist |
| 276 | foreach (QStringList tag, tags) { |
| 277 | Track* track = new Track(tag); |
| 278 | Q_EMIT trackDropped(track); |
| 279 | } |
| 280 | |
| 281 | } else |
| 282 | event->ignore(); |
| 283 | } |
| 284 | |
| 285 | void PlayerWidget::loadFile(QUrl file) |
| 286 | { |
| 287 | qDebug() << Q_FUNC_INFO << "url=" << file; |
| 288 | loadTrack(new Track(file)); |
| 289 | } |
| 290 | |
| 291 | void PlayerWidget::loadTrack(Track* track) |
| 292 | { |
| 293 | if (track) |
| 294 | qDebug() << Q_FUNC_INFO << ":" << objectName() << " track=" << track->url(); |
| 295 | |
| 296 | m_CurrentTrack = track; |
| 297 | |
| 298 | if (track != nullptr) { |
| 299 | |
| 300 | drawTitle(); |
| 301 | |
| 302 | bool doPlay = m_isStarted; |
| 303 | player->stop(); |
| 304 | |
| 305 | QUrl url = track->url(); |
| 306 | player->open(url); |
| 307 | |
| 308 | trackanalyser->setMode(TrackAnalyser::STANDARD); |
| 309 | trackanalyser->open(url); |
| 310 | |
| 311 | if (doPlay) |
| 312 | player->play(); |
| 313 | |
| 314 | } else { |
| 315 | if (player->lastError != "") |
| 316 | ui->lblTitle->setText(player->lastError); |
| 317 | else |
| 318 | ui->lblTitle->setText("no track"); |
| 319 | |
| 320 | ui->lblTime->setText("-:-"); |
| 321 | ui->lblTimeMs->setText(".-"); |
| 322 | ui->lblTimeRemain->setText("-:-"); |
| 323 | ui->lblTimeRemainMs->setText(".-"); |
| 324 | stop(); |
| 325 | } |
| 326 | |
| 327 | remainCueTime = 0; |
| 328 | ui->sliPosition->setValue(0); |
| 329 | ui->txtCue->setText("-"); |
| 330 | ui->butCue->setChecked(false); |
| 331 | } |
| 332 | |
| 333 | void PlayerWidget::resizeEvent(QResizeEvent* e) |
| 334 | { |
| 335 | QWidget::resizeEvent(e); |
| 336 | |
| 337 | drawTitle(); |
| 338 | } |
| 339 | |
| 340 | void PlayerWidget::drawTitle() |
| 341 | { |
| 342 | int width = ui->lblTitle->width() - 2; |
| 343 | if (width < 300) |
| 344 | ui->lblTitle->setStyleSheet("* { font-size: 13pt; }"); |
| 345 | else if (width < 400) |
| 346 | ui->lblTitle->setStyleSheet("* { font-size: 14pt; }"); |
| 347 | else |
| 348 | ui->lblTitle->setStyleSheet("* { font-size: 16pt; }"); |
| 349 | |
| 350 | QFontMetrics metrix(ui->lblTitle->font()); |
| 351 | |
| 352 | QString clippedText = tr("No track"); |
| 353 | if (m_CurrentTrack) |
| 354 | clippedText = metrix.elidedText(m_CurrentTrack->prettyTitle(), Qt::ElideRight, width); |
| 355 | |
| 356 | ui->lblTitle->setText(clippedText); |
| 357 | } |
| 358 | |
| 359 | float PlayerWidget::currentLevelLeft() |
| 360 | { |
| 361 | return player->levelOutLeft(); |
| 362 | } |
| 363 | |
| 364 | float PlayerWidget::currentLevelRight() |
| 365 | { |
| 366 | return player->levelOutRight(); |
| 367 | } |
| 368 | |
| 369 | void PlayerWidget::updateTimeAndPositionDisplay(bool isPassive) |
| 370 | { |
| 371 | |
| 372 | QTime length = player->length(); |
| 373 | QTime curpos = player->position(); |
| 374 | QTime remain(0, 0, 0); |
| 375 | long remainMs; |
| 376 | |
| 377 | //Some tracks deliver no length in state pause |
| 378 | if (length == QTime(0, 0) && m_CurrentTrack) |
| 379 | length = QTime(0, 0, 0).addSecs(m_CurrentTrack->length()); |
| 380 | |
| 381 | remainMs = curpos.msecsTo(length); |
| 382 | remain = QTime(0, 0, 0).addMSecs(remainMs); |
| 383 | |
| 384 | //qDebug()<<remainMs << " :" <<remain; |
| 385 | |
| 386 | ui->lblTime->setText(curpos.toString("mm:ss")); |
| 387 | ui->lblTimeMs->setText("." + curpos.toString("zzz").left(1)); |
| 388 | ui->lblTimeRemain->setText("-" + remain.toString("mm:ss")); |
| 389 | ui->lblTimeRemainMs->setText("." + remain.toString("zzz").left(1)); |
| 390 | |
| 391 | //Signal end of track or media error |
| 392 | //ToDo: better recognition of media error (play pressed but player is not running) |
| 393 | |
| 394 | if ((remainMs - remainCueTime - mTrackFinishEmitTime <= 0 |
| 395 | && 0 < remainMs) |
| 396 | || m_isHanging) { |
| 397 | if (!p->isEndAnnounced) { |
| 398 | qDebug() << Q_FUNC_INFO << ":" << objectName() << " EMIT aboutFinished"; |
| 399 | qDebug() << Q_FUNC_INFO << ": curpos:" << curpos; |
| 400 | qDebug() << Q_FUNC_INFO << ": remainMs:" << remainMs; |
| 401 | qDebug() << Q_FUNC_INFO << ": remainCueTime:" << remainCueTime; |
| 402 | qDebug() << Q_FUNC_INFO << ": mTrackFinishEmitTime:" << mTrackFinishEmitTime; |
| 403 | qDebug() << Q_FUNC_INFO << ": m_isHanging:" << m_isHanging; |
| 404 | |
| 405 | //send signals only once |
| 406 | p->isEndAnnounced = true; |
| 407 | Q_EMIT aboutFinished(); |
| 408 | Q_EMIT trackPlayed(m_CurrentTrack); |
| 409 | } |
| 410 | } else |
| 411 | p->isEndAnnounced = false; |
| 412 | |
| 413 | //update position slider only if triggerd by timer |
| 414 | if (isPassive) { |
| 415 | if (length != QTime(0, 0, 0)) |
| 416 | ui->sliPosition->setValue(curpos.msecsTo(QTime(0, 0, 0)) * 1000 / length.msecsTo(QTime(0, 0, 0))); |
| 417 | else |
| 418 | ui->sliPosition->setValue(0); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void PlayerWidget::playerError() |
| 423 | { |
| 424 | Q_EMIT trackFinished(); |
| 425 | } |
| 426 | |
| 427 | void PlayerWidget::playerFinished() |
| 428 | { |
| 429 | Q_EMIT trackFinished(); |
| 430 | } |
| 431 | |
| 432 | void PlayerWidget::playerLoaded() |
| 433 | { |
| 434 | updateTimeAndPositionDisplay(); |
| 435 | } |
| 436 | |
| 437 | void PlayerWidget::on_butRew_clicked() |
| 438 | { |
| 439 | if (player->position() < QTime(0, 0, 3)) |
| 440 | Q_EMIT rewindPressed(); |
| 441 | else |
| 442 | player->setPosition(QTime(0, 0, 0)); |
| 443 | } |
| 444 | |
| 445 | void PlayerWidget::on_butFwd_clicked() |
| 446 | { |
| 447 | Q_EMIT forwardPressed(); |
| 448 | } |
| 449 | |
| 450 | void PlayerWidget::setTrackFinishEmitTime(const int sec) |
| 451 | { |
| 452 | if (sec >= 0 && sec < 60) |
| 453 | mTrackFinishEmitTime = sec * 1000; |
| 454 | } |
| 455 | |
| 456 | void PlayerWidget::on_sliPosition_sliderMoved(int value) |
| 457 | { |
| 458 | uint length = -player->length().msecsTo(QTime(0, 0, 0)); |
| 459 | if (length != 0 && value > 0) { |
| 460 | QTime pos = QTime(0, 0, 0); |
| 461 | pos = pos.addMSecs(length * (value / 1000.0)); |
| 462 | qDebug() << "pos:" << pos; |
| 463 | player->setPosition(pos); |
| 464 | } |
| 465 | updateTimeAndPositionDisplay(false); |
| 466 | } |
| 467 | |
| 468 | void PlayerWidget::on_sliPosition_actionTriggered(int action) |
| 469 | { |
| 470 | //a workaround for page moving |
| 471 | int posi; |
| 472 | switch (action) { |
| 473 | case 3: |
| 474 | posi = ui->sliPosition->value() + 100; |
| 475 | break; |
| 476 | case 4: |
| 477 | posi = ui->sliPosition->value() - 100; |
| 478 | if (posi < 100) |
| 479 | posi = 1; |
| 480 | break; |
| 481 | case 1: |
| 482 | posi = ui->sliPosition->value() + 10; |
| 483 | break; |
| 484 | case 2: |
| 485 | posi = ui->sliPosition->value() - 10; |
| 486 | break; |
| 487 | default: |
| 488 | return; |
| 489 | break; |
| 490 | } |
| 491 | |
| 492 | this->on_sliPosition_sliderMoved(posi); |
| 493 | ui->butCue->setChecked(false); |
| 494 | } |
| 495 | |
| 496 | void PlayerWidget::on_butCue_clicked() |
| 497 | { |
| 498 | //ToDo: Visualize skipped silent at start and at the end (color bar) |
| 499 | this->pause(); |
| 500 | player->setPosition(trackanalyser->startPosition()); |
| 501 | updateTimeAndPositionDisplay(); |
| 502 | } |