dtkVisualizationWidgetsVideoControls.cpp 7.71 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
#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(); } ;
};


49 50 51 52
class dtkVisualizationWidgetsVideoControlsPrivate: public QObject
{
    Q_OBJECT

53 54 55
public:
    dtkVisualizationWidgetsVideoControlsItem *status_item = nullptr;
    dtkVisualizationWidgetsVideoControlsItem *rewind      = nullptr;
56
    dtkVisualizationWidgetsVideoControlsItem *fullscreen  = nullptr;
57 58 59

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

60 61 62
public slots:
    void updateDurationInfo(qint64 currentInfo);

63 64
public:
    QProgressBar *progress_bar = nullptr;
65 66
    QLabel *label_duration = nullptr;
    bool is_fullscreen = false;
67 68
};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
void dtkVisualizationWidgetsVideoControlsPrivate::updateDurationInfo(qint64 currentInfo)
{
    QString tStr;
    int duration = this->progress_bar->maximum() / 1000;
    if (currentInfo || duration) {
        QTime currentTime((currentInfo / 3600) % 60, (currentInfo / 60) % 60,
            currentInfo % 60, (currentInfo * 1000) % 1000);
        QTime totalTime((duration / 3600) % 60, (duration / 60) % 60,
            duration % 60, (duration * 1000) % 1000);
        QString format = "mm:ss";
        if (duration > 3600)
            format = "hh:mm:ss";
        tStr = currentTime.toString(format) + " / " + totalTime.toString(format);
    }
    this->label_duration->setText(tStr);
    this->label_duration->setStyleSheet("font: 14pt;");
}

87 88
dtkVisualizationWidgetsVideoControls::dtkVisualizationWidgetsVideoControls(QWidget *parent): QFrame(parent), d(new dtkVisualizationWidgetsVideoControlsPrivate)
{
89 90 91
    this->setAttribute(Qt::WA_NoSystemBackground);
    this->setAttribute(Qt::WA_TranslucentBackground);

92
    dtkFontAwesome::instance()->initFontAwesome();
93
    dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
94

95 96 97
    d->status_item = new dtkVisualizationWidgetsVideoControlsItem(fa::play, 24, parent);
    d->rewind      = new dtkVisualizationWidgetsVideoControlsItem(fa::stepbackward, 24, parent);
    d->fullscreen  = new dtkVisualizationWidgetsVideoControlsItem(fa::arrowsalt, 24, parent);
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
98
    connect(d->status_item, &dtkVisualizationWidgetsVideoControlsItem::clicked, this, &dtkVisualizationWidgetsVideoControls::toggle);
99 100 101 102 103 104 105 106 107
    connect(d->fullscreen, &dtkVisualizationWidgetsVideoControlsItem::clicked, [=]() {
            if (d->is_fullscreen) // was fullscreen
                d->fullscreen->setIcon(fa::arrowsalt);
            else
                d->fullscreen->setIcon(fa::compress);

            d->is_fullscreen = !d->is_fullscreen;
            emit fullscreen(d->is_fullscreen);
        });
108
    connect(d->rewind, &dtkVisualizationWidgetsVideoControlsItem::clicked, [=]() {
109
            d->state = QMediaPlayer::State::StoppedState;
110 111 112 113 114 115 116 117
            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);
118 119
    d->progress_bar->setFixedHeight(10);
    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(253,26,30,100); }";
120 121 122

    d->progress_bar->setStyleSheet(style_bar);

123 124 125 126 127 128 129 130 131

    d->label_duration = new QLabel(this);
    d->label_duration->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    QVBoxLayout *vlayout =  new QVBoxLayout(this);
    vlayout->setContentsMargins(32, 0, 32, 0);
    vlayout->setAlignment(Qt::AlignLeft);

    QHBoxLayout *layout =  new QHBoxLayout;
132 133 134
    layout->setContentsMargins(0, 0, 0, 0);

    layout->addWidget(d->status_item);
135 136 137 138
    layout->addWidget(d->rewind);
    layout->addWidget(d->label_duration);
    layout->addWidget(d->fullscreen);

139
    this->setMouseTracking(true);
140 141 142 143

     vlayout->addWidget(d->progress_bar);
     vlayout->addLayout(layout);
     this->setLayout(vlayout);
144 145
}

146

147 148 149
void dtkVisualizationWidgetsVideoControls::reset(void)
{
    d->state = QMediaPlayer::State::StoppedState;
150
    dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    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();
}

166 167 168 169 170 171 172 173
void dtkVisualizationWidgetsVideoControls::setDuration(qlonglong duration)
{
    d->progress_bar->setRange(0, duration);
}

void dtkVisualizationWidgetsVideoControls::setPosition(qlonglong pos)
{
    d->progress_bar->setValue(pos);
174
    d->updateDurationInfo(pos / 1000);
175 176
}

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
177 178 179 180
void dtkVisualizationWidgetsVideoControls::toggle(void)
{
    if (d->state == QMediaPlayer::State::StoppedState || d->state == QMediaPlayer::State::PausedState) {
        d->state = QMediaPlayer::State::PlayingState;
181
        dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
182 183 184 185
        d->status_item->setIcon(fa::pause);
        emit play();
    } else {
        d->state = QMediaPlayer::State::PausedState;
186
        dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
187 188 189 190 191
        d->status_item->setIcon(fa::play);
        emit pause();
    }
}

192 193 194 195 196 197 198
void dtkVisualizationWidgetsVideoControls::setCurrentFrame(qlonglong count)
{
    d->progress_bar->setValue(count);
}

void dtkVisualizationWidgetsVideoControls::mouseMoveEvent(QMouseEvent *event)
{
199 200 201 202 203 204 205 206 207 208 209 210
    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() / 1000.0 ;

    QTime currentTime((frame_index / 3600) % 60, (frame_index / 60) % 60,
                      frame_index % 60, (frame_index * 1000) % 1000);

    QString format = "mm:ss";
    if (frame_index > 3600)
        format = "hh:mm:ss";

    d->progress_bar->setToolTip(currentTime.toString(format));
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
    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"