xref: /OK3568_Linux_fs/app/multivideoplayer/multivideoplayer.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /****************************************************************************
2 **
3 ** Copyright (C) 2017 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 #include "multivideoplayer.h"
52 #include <QtWidgets>
53 #include <QVideoSurfaceFormat>
54 
MultiVideoPlayer(QStringList list)55 MultiVideoPlayer::MultiVideoPlayer(QStringList list)
56 {
57     const QRect screenGeometry = QApplication::desktop()->screenGeometry(this);
58     int i, num = 0;
59     resize(screenGeometry.width(), screenGeometry.height());
60 
61     playerList = new QList<QMediaPlayer*>();
62     videoList = new QList<QVideoWidget*>();
63     hLayoutList = new QList<QHBoxLayout*>();
64     multiPlayList = new QList<QMediaPlaylist*>();
65     urlList = list;
66 
67     exitButton = new QPushButton(tr("Exit"));
68     connect(exitButton, &QAbstractButton::clicked, this, &MultiVideoPlayer::exit);
69 
70     if(urlList.count() > 0){
71         num = qSqrt(urlList.count());
72         if(qreal(num) < qSqrt(list.count())){
73             num++;
74         }
75     }
76 
77     for(i=0; i<num; i++){
78         QHBoxLayout *layout = new QHBoxLayout;
79         hLayoutList->append(layout);
80     }
81 
82     for(i=0; i<urlList.count(); i++){
83         QMediaPlayer *player = new QMediaPlayer(this, QMediaPlayer::VideoSurface);
84         playerList->append(player);
85         QVideoWidget *video = new QVideoWidget();
86         videoList->append(video);
87         QMediaPlaylist *playlist = new QMediaPlaylist(this);
88         multiPlayList->append(playlist);
89         QString str = list.at(i);
90         QFile f(urlList.at(i));
91         QUrl u(str);
92 
93         if(f.exists()){
94             playlist->addMedia(QUrl::fromLocalFile(str));
95         }else if(u.isValid()){
96             playlist->addMedia(u);
97         }
98         playlist->setCurrentIndex(1);
99         playlist->setPlaybackMode(QMediaPlaylist::Loop);
100         player->setPlaylist(playlist);
101         QHBoxLayout *layout = hLayoutList->value(i/num);
102         if(layout){
103             layout->addWidget(video);
104             layout->setMargin(0);
105             layout->setSpacing(0);
106         }
107     }
108 
109     QBoxLayout *mainLayout = new QVBoxLayout();
110     mainLayout->setMargin(0);
111     mainLayout->setSpacing(0);
112     for(i=0; i<hLayoutList->count(); i++){
113         QWidget *widget = new QWidget();
114         widget->setLayout(hLayoutList->value(i));
115         mainLayout->addWidget(widget);
116     }
117     mainLayout->addWidget(exitButton);
118 
119     QWidget *widget = new QWidget();
120     widget->setLayout(mainLayout);
121 
122     setCentralWidget(widget);
123     setWindowState(Qt::WindowMaximized);
124     setWindowFlags(Qt::FramelessWindowHint);
125 
126     QPalette palette = QWidget::palette();
127     palette.setColor(QPalette::Window, Qt::black);
128     setPalette(palette);
129 }
130 
~MultiVideoPlayer()131 MultiVideoPlayer::~MultiVideoPlayer()
132 {
133 }
134 
play()135 void MultiVideoPlayer::play()
136 {
137     for(int i=0; i<urlList.count(); i++){
138         QVideoWidget *video = videoList->value(i);
139         QMediaPlayer *player = playerList->value(i);
140         if(video && player){
141             player->setVideoOutput(video);
142             video->show();
143             player->play();
144         }
145     }
146 }
147 
mouseDoubleClickEvent(QMouseEvent * e)148 void MultiVideoPlayer::mouseDoubleClickEvent(QMouseEvent *e)
149 {
150     if(e->button() == Qt::LeftButton){
151         if(isFullScreen()){
152             showNormal();
153         }else {
154             showFullScreen();
155         }
156     }
157 }
158 
mouseReleaseEvent(QMouseEvent * e)159 void MultiVideoPlayer::mouseReleaseEvent(QMouseEvent *e)
160 {
161     if(e->button() == Qt::LeftButton){
162     }
163 }
164 
keyReleaseEvent(QKeyEvent * e)165 void MultiVideoPlayer::keyReleaseEvent(QKeyEvent *e)
166 {
167     switch(e->key())
168     {
169         case Qt::Key_VolumeUp:
170         case Qt::Key_Up:
171             break;
172         case Qt::Key_VolumeDown:
173         case Qt::Key_Down:
174             break;
175     }
176 }
177 
exit()178 void MultiVideoPlayer::exit()
179 {
180     qApp->exit(0);
181 }
182 
183