view src/pngplayer.cpp @ 23:d9626e84b93d

Added tag 0.0.2 for changeset 0a08c1c2531a
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 14 Apr 2015 18:58:49 +0200
parents 6b5e7d6bf4a2
children 64a51a42c01f
line wrap: on
line source
/* Copyright (C) 2014 by Intevation GmbH
 *
 * This file is Free Software under the GNU GPL (v>=2)
 * and comes with ABSOLUTELY NO WARRANTY!
 * See LICENSE.txt for details.
 */
#include "pngplayer.h"
#include "constants.h"
#include "imagelabel.h"
#include <QVBoxLayout>
#include <QPixmap>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QSlider>
#include <QSettings>
#include <QApplication>
#include <QStyle>

#include <QDebug>

PNGPlayer::PNGPlayer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) {
    setupGUI();
    QSettings settings;
    mSpeedSlider->setValue(settings.value(REPLAY_SPEED_KEY, REPLAY_SPEED_DEFAULT).toInt());
    speedChanged();
}

void PNGPlayer::setupGUI() {
    QVBoxLayout *baseLayout = new QVBoxLayout;
    mPNGLabel = new ImageLabel;
    mPNGLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QHBoxLayout *controlArea = new QHBoxLayout;
    QHBoxLayout *controlBtns = new QHBoxLayout;
    controlArea->addLayout(controlBtns);
    controlArea->addStretch(-1);

    QPushButton *firstBtn = new QPushButton;
    firstBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaSkipBackward));
    controlBtns->addWidget(firstBtn);
    connect(firstBtn, &QPushButton::clicked, this, &PNGPlayer::firstClicked);

    QPushButton *prevBtn = new QPushButton;
    prevBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaSeekBackward));
    controlBtns->addWidget(prevBtn);
    connect(prevBtn, &QPushButton::clicked, this, &PNGPlayer::back);

    mPlayBtn = new QPushButton;
    mPlayBtn->setCheckable(true);
    mPlayBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaPlay));
    controlBtns->addWidget(mPlayBtn);
    connect(mPlayBtn, &QPushButton::clicked, this, &PNGPlayer::togglePlay);

    QPushButton *nextBtn = new QPushButton;
    nextBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaSeekForward));
    connect(nextBtn, &QPushButton::clicked, this, &PNGPlayer::advance);
    controlBtns->addWidget(nextBtn);

    QPushButton *lastBtn = new QPushButton;
    lastBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaSkipForward));
    controlBtns->addWidget(lastBtn);
    connect(lastBtn, &QPushButton::clicked, this, &PNGPlayer::lastClicked);

    mSlider = new QSlider(Qt::Horizontal);
    mSlider->setTickPosition(QSlider::TicksBelow);
    connect(mSlider, &QSlider::valueChanged, this, &PNGPlayer::sliderChanged);
    connect(mSlider, &QSlider::sliderPressed, this, &PNGPlayer::sliderPressed);
    connect(mSlider, &QSlider::sliderReleased, this, &PNGPlayer::sliderReleased);

    QVBoxLayout *speedInfoArea = new QVBoxLayout;
    QHBoxLayout *speedArea = new QHBoxLayout;
    speedInfoArea->addLayout(speedArea);

    QLabel *speedLabel = new QLabel(tr("Speed:"));
    speedArea->addWidget(speedLabel);

    mSpeedSlider = new QSlider(Qt::Horizontal);
    mSpeedSlider->setMaximum(10);
    mSpeedSlider->setTickInterval(1);
    mSpeedSlider->setTickPosition(QSlider::TicksBelow);
    mSpeedSlider->setTracking(false);
    mSpeedSlider->setInvertedAppearance(true);

    speedArea->addWidget(mSpeedSlider);
    connect(mSpeedSlider, &QSlider::valueChanged, this, &PNGPlayer::speedChanged);

    mCurSpeedLabel = new QLabel;
    speedArea->addWidget(mCurSpeedLabel);
    speedArea->addStretch(-1);

    mPositionLabel = new QLabel;
    speedInfoArea->addWidget(mPositionLabel);

    controlArea->addLayout(speedInfoArea);

    baseLayout->addWidget(mPNGLabel);
    baseLayout->addLayout(controlArea);
    baseLayout->addWidget(mSlider);

    connect(&mAdvanceTimer, SIGNAL(timeout()), this, SIGNAL(advance()));
    setLayout(baseLayout);
}

void PNGPlayer::showPicture(const QString& fileName, int current, int max,
        const QDateTime& timestamp) {
    QPixmap pic(mBaseDir.filePath(fileName));
    /* If this is too slow we could use a pixmap cache here and do
     * some intelligent preloading */
    if (pic.isNull()) {
        qWarning() << "Failed to load picture: " << fileName;
//        emit error(tr("Failed to load picture: '%1'").arg(fileName));
        return;
    }
    mPNGLabel->setPixmap(pic);
    updatePositions(current, max, timestamp);
}

void PNGPlayer::updatePositions(int current, int max, const QDateTime& timestamp) {
    mMax = max;
    mSlider->blockSignals(true); /* We only want user generated changes */
    mSlider->setValue(current);
    mSlider->blockSignals(false);
    mSlider->setMaximum(max);
    mPositionLabel->setText("<b>" + tr("Screenshot Nr.:") + " </b>" +
            QString("%1 (%2)").arg(current).arg(max) + " / <b>" +
        tr("Timestamp:") + " </b>" + (timestamp.isValid() ?
            timestamp.toString(LONG_DATE_FORMAT) : tr("Unknown")));

    if (mMax == current && mAdvanceTimer.isActive()) {
        togglePlay();
    }
}

void PNGPlayer::setSpeed(int mSecsPerPicture) {
    if (mSecsPerPicture == 1000) {
        mCurSpeedLabel->setText(tr("%1 second per Picture").
                                arg(mSecsPerPicture / 1000.));
    } else {
        mCurSpeedLabel->setText(tr("%1 seconds per Picture").
                                arg(mSecsPerPicture / 1000.));
    }
    mAdvanceTimer.setInterval(mSecsPerPicture);
}

void PNGPlayer::togglePlay() {
    if (mAdvanceTimer.isActive()) {
        mAdvanceTimer.stop();
        mPlayBtn->setChecked(false);
        mPlayBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaPlay));
    } else {
        mAdvanceTimer.start();
        mPlayBtn->setChecked(true);
        mPlayBtn->setIcon(QApplication::style()->standardIcon(QStyle::SP_MediaPause));
        emit advance();
    }
}

void PNGPlayer::sliderChanged() {
    qDebug() << "Slider moved. ";
    emit jumpToFrame(mSlider->value());
}

void PNGPlayer::sliderPressed() {
    qDebug() << "Slider pressed. Deactivating timer. ";
    mAdvanceTimer.stop();
}

void PNGPlayer::sliderReleased() {
    if (mPlayBtn->isChecked()) {
        mAdvanceTimer.start();
    }
}

void PNGPlayer::firstClicked() {
    emit jumpToFrame(0);
}

void PNGPlayer::lastClicked() {
    emit jumpToFrame(mMax);
}

void PNGPlayer::speedChanged() {
    if (mSpeedSlider->value()) {
        setSpeed(mSpeedSlider->value() * REPLAY_SPEED_STEP_MS);
    } else {
        setSpeed(REPLAY_SPEED_STEP_MS / 2);
    }
    QSettings settings;
    settings.setValue(REPLAY_SPEED_KEY, mSpeedSlider->value());
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)