summaryrefslogtreecommitdiffstats
path: root/videoviewer.cpp
blob: 8c7c23367b327cf75f51034422bbbc0891903eeb (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QKeyEvent>
#include <QCloseEvent>
#include <QSettings>
#include <QApplication>
#include <QDesktopWidget>

#include "videoviewer.h"
#include "smglobals.h"

VideoViewer::VideoViewer(QWidget *parent) : QVideoWidget(parent) {
    resize(1280,1024);
    move(qApp->desktop()->availableGeometry(this).center() - rect().center());
    mPlayer = new QMediaPlayer(this);
    mPlayer->setVideoOutput(this);
    QMediaPlaylist *playList = new QMediaPlaylist;
    mPlayer->setPlaylist(playList);
    readSettings();
}

void VideoViewer::readSettings(){
    QSettings s;
    QRect winSize = s.value("vw/size").toRect();
    setGeometry(winSize);
    bool fullScreen = s.value("vw/fullscreen").toBool();
    setFullScreen(fullScreen);
}

void VideoViewer::writeSettings(){
    QSettings s;
    s.setValue("vw/size", geometry());
    s.setValue("vw/fullscreen", isFullScreen());
}

void VideoViewer::keyPressEvent(QKeyEvent *e){
    int keyNum = e->key();
    if(keyNum == Qt::Key_Q){
        player()->stop();
        setHidden(true);
    }else if(keyNum == Qt::Key_F){
        bool fs = isFullScreen();
        mLastFullScreen = !fs;
        setFullScreen(mLastFullScreen);
    }else if(keyNum == Qt::Key_Right){
        mPlayer->setPosition(mPlayer->position() + 5000);
    }else if(keyNum == Qt::Key_Left){
        qint64 pos = mPlayer->position();
        mPlayer->setPosition(pos - 5000 < 0 ? 0 : pos - 5000);
    }else if(keyNum == Qt::Key_Up){
        mPlayer->setPosition(mPlayer->position() + 60000);
    }else if(keyNum == Qt::Key_Down){
        qint64 pos = mPlayer->position();
        mPlayer->setPosition(pos - 60000 < 0 ? 0 : pos - 60000);
    }else if(keyNum == Qt::Key_N){
        int mediaCount = mPlayer->playlist()->mediaCount();
        int nextIndex = mPlayer->playlist()->currentIndex() + 1;
        if(nextIndex >= mediaCount){
            nextIndex = 0;
        }
        mPlayer->playlist()->setCurrentIndex(nextIndex);
    }else if(keyNum == Qt::Key_P){
        int mediaCount = mPlayer->playlist()->mediaCount();
        int prevIndex = mPlayer->playlist()->currentIndex() - 1;
        if(prevIndex < 0){
            prevIndex = mediaCount - 1;
        }
        mPlayer->playlist()->setCurrentIndex(prevIndex);
    }else if(keyNum == Qt::Key_M){
        mPlayer->setMuted(mPlayer->isMuted() ? false : true);
    }else if(keyNum == Qt::Key_Space){
        QMediaPlayer::State state = mPlayer->state();
        if(state == QMediaPlayer::PausedState){
            mPlayer->play();
        }else if(state == QMediaPlayer::PlayingState){
            mPlayer->pause();
        }
    }else if(keyNum == Qt::Key_1){
        int curVol = mPlayer->volume() - 5;
        if(curVol < 0){
            curVol = 0;
        }
        mPlayer->setVolume(curVol);
    }else if(keyNum == Qt::Key_2){
        int curVol = mPlayer->volume() + 5;
        if(curVol > 100){
            curVol = 100;
        }
        mPlayer->setVolume(curVol);
    }
}

void VideoViewer::closeEvent(QCloseEvent *e){
    mPlayer->stop();
    writeSettings();
    hide();
    e->ignore();
}