dtkVisualizationWidgetsVideoControls.cpp 8.6 KB
Newer Older
1 2
#include "dtkVisualizationWidgetsVideoControls.h"

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
3
#include <dtkFonts/dtkFontAwesome>
4

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
5
#include <QtWidgets>
6

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
7 8 9 10 11
// ///////////////////////////////////////////////////////////////////
// dtkVisualizationWidgetsVideoControlsItem
// ///////////////////////////////////////////////////////////////////

class dtkVisualizationWidgetsVideoControlsItem : public QFrame
12 13 14 15
{
    Q_OBJECT

public:
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
16 17 18
    dtkVisualizationWidgetsVideoControlsItem(fa::icon icon, int icon_size, QWidget *parent = nullptr);

    void setIcon(fa::icon icon);
19 20 21 22 23 24 25 26 27

signals:
    void clicked(void);

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

protected:
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
28
    void mousePressEvent(QMouseEvent *);
29 30
};

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
31 32 33 34 35 36 37 38 39 40 41 42
// ///////////////////////////////////////////////////////////////////

dtkVisualizationWidgetsVideoControlsItem::dtkVisualizationWidgetsVideoControlsItem(fa::icon icon, int icon_size, QWidget *parent) : 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);
43

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    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);
};

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

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

// ///////////////////////////////////////////////////////////////////
// dtkVisualizationWidgetsVideoControlsPrivate
// ///////////////////////////////////////////////////////////////////

class dtkVisualizationWidgetsVideoControlsPrivate : public QObject
67 68 69
{
    Q_OBJECT

70 71 72
public:
    dtkVisualizationWidgetsVideoControlsItem *status_item = nullptr;
    dtkVisualizationWidgetsVideoControlsItem *rewind      = nullptr;
73
    dtkVisualizationWidgetsVideoControlsItem *fullscreen  = nullptr;
74

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
75
    dtkVisualizationWidgetsVideoControls::State state = dtkVisualizationWidgetsVideoControls::State::StoppedState;
76

77 78 79
public slots:
    void updateDurationInfo(qint64 currentInfo);

80 81
public:
    QProgressBar *progress_bar = nullptr;
82 83
    QLabel *label_duration = nullptr;
    bool is_fullscreen = false;
84 85
};

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
86 87
// ///////////////////////////////////////////////////////////////////

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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;");
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
106 107 108 109 110
// ///////////////////////////////////////////////////////////////////
// dtkVisualizationWidgetsVideoControls
// ///////////////////////////////////////////////////////////////////

dtkVisualizationWidgetsVideoControls::dtkVisualizationWidgetsVideoControls(QWidget *parent) : QFrame(parent), d(new dtkVisualizationWidgetsVideoControlsPrivate)
111
{
112 113 114
    this->setAttribute(Qt::WA_NoSystemBackground);
    this->setAttribute(Qt::WA_TranslucentBackground);

115
    dtkFontAwesome::instance()->initFontAwesome();
116
    dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
117

118 119 120
    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
121
    connect(d->status_item, &dtkVisualizationWidgetsVideoControlsItem::clicked, this, &dtkVisualizationWidgetsVideoControls::toggle);
122 123 124 125 126 127 128 129 130
    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);
        });
131
    connect(d->rewind, &dtkVisualizationWidgetsVideoControlsItem::clicked, [=]() {
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
132
            d->state = State::StoppedState;
133 134 135 136 137 138 139 140
            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);
141
    d->progress_bar->setFixedHeight(10);
142

143
    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); }";
144 145 146

    d->progress_bar->setStyleSheet(style_bar);

147 148 149 150 151 152 153 154
    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;
155 156 157
    layout->setContentsMargins(0, 0, 0, 0);

    layout->addWidget(d->status_item);
158 159 160 161
    layout->addWidget(d->rewind);
    layout->addWidget(d->label_duration);
    layout->addWidget(d->fullscreen);

162
    this->setMouseTracking(true);
163

164 165 166 167
    vlayout->addWidget(d->progress_bar);
    vlayout->addLayout(layout);

    this->setLayout(vlayout);
168 169
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
170 171 172 173
dtkVisualizationWidgetsVideoControls::~dtkVisualizationWidgetsVideoControls(void)
{
    delete d;
}
174

175 176
void dtkVisualizationWidgetsVideoControls::reset(void)
{
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
177
    d->state = State::StoppedState;
178
    dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
179 180 181
    d->status_item->setIcon(fa::play);
}

KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
182
dtkVisualizationWidgetsVideoControls::State dtkVisualizationWidgetsVideoControls::state(void) const
183 184 185 186 187 188 189 190 191 192 193
{
    return d->state;
}

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

194 195 196 197 198 199 200 201
void dtkVisualizationWidgetsVideoControls::setDuration(qlonglong duration)
{
    d->progress_bar->setRange(0, duration);
}

void dtkVisualizationWidgetsVideoControls::setPosition(qlonglong pos)
{
    d->progress_bar->setValue(pos);
202
    d->updateDurationInfo(pos / 1000);
203 204
}

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
205 206
void dtkVisualizationWidgetsVideoControls::toggle(void)
{
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
207 208
    if (d->state == State::StoppedState || d->state == State::PausedState) {
        d->state = State::PlayingState;
209
        dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
210 211
        d->status_item->setIcon(fa::pause);
        emit play();
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
212

NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
213
    } else {
KLOCZKO Thibaud's avatar
KLOCZKO Thibaud committed
214
        d->state = State::PausedState;
215
        dtkFontAwesome::instance()->setDefaultOption("color", QColor(Qt::white));
NICLAUSSE Nicolas's avatar
NICLAUSSE Nicolas committed
216 217 218 219 220
        d->status_item->setIcon(fa::play);
        emit pause();
    }
}

221 222 223 224 225 226 227
void dtkVisualizationWidgetsVideoControls::setCurrentFrame(qlonglong count)
{
    d->progress_bar->setValue(count);
}

void dtkVisualizationWidgetsVideoControls::mouseMoveEvent(QMouseEvent *event)
{
228 229 230 231 232 233 234 235 236 237 238 239
    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));
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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"