view src/pngplayer.cpp @ 8:ac4db84f1d9d

Fix date format in player window
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 25 Mar 2015 14:25:21 +0100
parents c11fe56aa4a2
children f290e8c4e097
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 <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);

    QPushButton *firstBtn = new QPushButton("First");
    controlBtns->addWidget(firstBtn);
    connect(firstBtn, &QPushButton::clicked, this, &PNGPlayer::firstClicked);

    QPushButton *prevBtn = new QPushButton("Prev");
    controlBtns->addWidget(prevBtn);
    connect(prevBtn, &QPushButton::clicked, this, &PNGPlayer::back);

    mPlayBtn = new QPushButton("Play");
    mPlayBtn->setCheckable(true);
    controlBtns->addWidget(mPlayBtn);
    connect(mPlayBtn, &QPushButton::clicked, this, &PNGPlayer::togglePlay);

    QPushButton *nextBtn = new QPushButton("Next");
    connect(nextBtn, &QPushButton::clicked, this, &PNGPlayer::advance);
    controlBtns->addWidget(nextBtn);

    QPushButton *lastBtn = new QPushButton("Last");
    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()) {
//        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->setText("Play");
        mPlayBtn->setChecked(false);
    } else {
        mAdvanceTimer.start();
        mPlayBtn->setText("Pause");
        mPlayBtn->setChecked(true);
        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)