dtkVisualizationWidgetsVideoControls.cpp 5.47 KB
Newer Older
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
#include "dtkVisualizationWidgetsVideoControls.h"

#include <dtkFonts>

#include <dtkWidgets/dtkWidgetsHUD>
#include <dtkWidgets/dtkWidgetsHUDItem>

class dtkVisualizationWidgetsVideoControlsItem: public QFrame
{
    Q_OBJECT

public:
    dtkVisualizationWidgetsVideoControlsItem(fa::icon icon, int icon_size, QWidget *parent = nullptr): QFrame(parent) {
        m_icon_size = icon_size;

        label = new QLabel(this);
        label->setFixedSize(QSize(icon_size,icon_size));
        label->setPixmap(dtkFontAwesome::instance()->icon(icon).pixmap(icon_size, icon_size));
        label->setAttribute(Qt::WA_TranslucentBackground);
        label->setAttribute(Qt::WA_NoSystemBackground);
        label->setVisible(true);

        QHBoxLayout *layout = new QHBoxLayout;
        layout->setContentsMargins(0, 0, 0, 0);
        layout->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        layout->addWidget(label);
        this->setLayout(layout);
        this->setMouseTracking(true);
    };

    virtual ~dtkVisualizationWidgetsVideoControlsItem(void) { };

    void setIcon(fa::icon icon) {
        label->setPixmap(dtkFontAwesome::instance()->icon(icon).pixmap(m_icon_size, m_icon_size));
    };

signals:
    void clicked(void);

protected:
    QLabel *label = nullptr;
    int m_icon_size = 0;

protected:
    void mousePressEvent(QMouseEvent *event) { emit clicked(); } ;
};


class dtkVisualizationWidgetsVideoControlsPrivate {
public:
    dtkVisualizationWidgetsVideoControlsItem *status_item = nullptr;
    dtkVisualizationWidgetsVideoControlsItem *rewind      = nullptr;

    QMediaPlayer::State state = QMediaPlayer::State::StoppedState;

public:
    QProgressBar *progress_bar = nullptr;

};

dtkVisualizationWidgetsVideoControls::dtkVisualizationWidgetsVideoControls(QWidget *parent): QFrame(parent), d(new dtkVisualizationWidgetsVideoControlsPrivate)
{
63 64 65 66
    this->setAttribute(Qt::WA_NoSystemBackground);
    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setAttribute(Qt::WA_TransparentForMouseEvents, true);

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    dtkFontAwesome::instance()->initFontAwesome();
    dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::gray));

    d->status_item   = new dtkVisualizationWidgetsVideoControlsItem(fa::play, 30, parent);
    d->rewind = new dtkVisualizationWidgetsVideoControlsItem(fa::stepbackward, 24, parent);

    connect(d->status_item, &dtkVisualizationWidgetsVideoControlsItem::clicked, [=] () {
            if (d->state == QMediaPlayer::State::StoppedState || QMediaPlayer::State::PausedState) {
                d->state = QMediaPlayer::State::PlayingState;
                dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::gray));
                d->status_item->setIcon(fa::pause);
                qDebug() << "emitting play";
                emit play();
            } else {
                d->state = QMediaPlayer::State::PausedState;
                dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::gray));
                d->status_item->setIcon(fa::play);
                qDebug() << "emitting pause";
                emit pause();
            }
        });
    connect(d->rewind, &dtkVisualizationWidgetsVideoControlsItem::clicked, [=]() {
89
            d->state = QMediaPlayer::State::StoppedState;
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
            d->status_item->setIcon(fa::play);
            d->progress_bar->reset();
            emit restart();
        });

    d->progress_bar = new QProgressBar(parent);
    d->progress_bar->setOrientation(Qt::Horizontal);
    d->progress_bar->setTextVisible(true);
    QString style_bar = "QProgressBar{ border: 1px solid transparent; text-align: center; color:rgba(0,0,0,100); border-radius: 5px; background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(182, 182, 182, 100), stop:1 rgba(209, 209, 209, 100)); } QProgressBar::chunk{ background-color: rgba(0,255,0,100); }";

    d->progress_bar->setStyleSheet(style_bar);

    QHBoxLayout *layout =  new QHBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);

    layout->addWidget(d->rewind);
    layout->addWidget(d->status_item);
    layout->addWidget(d->progress_bar);
    this->setMouseTracking(true);
}

void dtkVisualizationWidgetsVideoControls::reset(void)
{
    d->state = QMediaPlayer::State::StoppedState;
    dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::gray));
    d->status_item->setIcon(fa::play);
}

QMediaPlayer::State dtkVisualizationWidgetsVideoControls::state(void) const
{
    return d->state;
}

void dtkVisualizationWidgetsVideoControls::setFrameNumber(qlonglong count)
{
    d->progress_bar->setRange(1, count);
    d->progress_bar->setValue(1);
    d->progress_bar->reset();
}

130 131 132 133 134 135 136 137 138 139
void dtkVisualizationWidgetsVideoControls::setDuration(qlonglong duration)
{
    d->progress_bar->setRange(0, duration);
}

void dtkVisualizationWidgetsVideoControls::setPosition(qlonglong pos)
{
    d->progress_bar->setValue(pos);
}

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
void dtkVisualizationWidgetsVideoControls::setCurrentFrame(qlonglong count)
{
    d->progress_bar->setValue(count);
}

void dtkVisualizationWidgetsVideoControls::mouseMoveEvent(QMouseEvent *event)
{
    event->ignore();

    QFrame::mouseMoveEvent(event);
}

void dtkVisualizationWidgetsVideoControls::mousePressEvent(QMouseEvent *event)
{
    int size = d->progress_bar->width();
    double percent = (event->pos().x() - d->progress_bar->x())/ (double)size ;
    int frame_index = percent * d->progress_bar->maximum();
    emit seekTo(frame_index);
    event->ignore();

    QFrame::mousePressEvent(event);
}

#include "dtkVisualizationWidgetsVideoControls.moc"