view src/pngplayer.cpp @ 3:248d5d1cdb38

Add functionalty to control buttons and make picture resizable
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 23 Mar 2015 19:10:01 +0100
parents 97d2c8869c39
children 107e435cb569
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 <QDebug>

PNGPlayer::PNGPlayer(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) {
    setupGUI();
}

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(10000);
    mSpeedSlider->setTickInterval(500);
    mSpeedSlider->setTickPosition(QSlider::TicksBelow);
    speedArea->addWidget(mSpeedSlider);

    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.toString(LONG_DATE_FORMAT));

    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));
    }
    mSpeedSlider->setValue(mSecsPerPicture);
    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);
}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)