/
MovieLayout.py
264 lines (199 loc) · 9.77 KB
/
MovieLayout.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QWidget, QFrame, QSlider, QHBoxLayout, QPushButton, \
QVBoxLayout, QApplication, QPlainTextEdit, QLabel, QTextEdit
import vlc
import pafy
from MovieController import MovieController
from Professor import Professor
#layout for presenting a movie with all navigation features
class MovieLayout(QWidget):
def __init__(self, url, movieID, user):
super().__init__()
self.controller = MovieController(url)
self.url = url
self.user = user
self.setWindowTitle("Movie Player")
self.movieID = movieID
# creating a basic vlc instance
self.instance = vlc.Instance()
# creating an empty vlc media player
self.mediaplayer = self.instance.media_player_new()
self.isPaused = True
# self.moviebox = QVBoxLayout()
# self.moviebox.addWidget(self.widget)
# self.widget.setLayout(self.moviebox)
# print("here")
self.videoframe = QFrame()
self.palette = self.videoframe.palette()
self.palette.setColor (QPalette.Window,
QColor(0,0,0))
self.videoframe.setPalette(self.palette)
self.videoframe.setAutoFillBackground(True)
self.positionslider = QSlider(Qt.Horizontal, self)
self.positionslider.setToolTip("Position")
self.positionslider.setMaximum(1000)
self.positionslider.sliderMoved.connect(self.setPosition)
self.hbuttonbox = QHBoxLayout()
self.playbutton = QPushButton()
self.playbutton.setObjectName("playbutton")
self.hbuttonbox.addWidget(self.playbutton)
result = self.controller.isMovieForUser(self.movieID, self.user.id)
self.stopbutton = QPushButton()
self.stopbutton.setObjectName("stopbutton")
self.hbuttonbox.addWidget(self.stopbutton)
if(isinstance(self.user, Professor) == True and len(result) > 0):
self.numberofViews = result[0][1]
self.description = result[0][0]
self.numofLikes = result[0][2]
self.infoButton = QPushButton("Statistics")
self.hbuttonbox.addWidget(self.infoButton)
self.infoButton.clicked.connect(lambda: self.openStats())
self.stopbutton.clicked.connect(self.Stop)
self.hbuttonbox.addStretch(1)
self.volumeslider = QSlider(Qt.Horizontal, self)
self.volumeslider.setMaximum(100)
self.volumeslider.setValue(self.mediaplayer.audio_get_volume())
self.volumeslider.setToolTip("Volume")
self.hbuttonbox.addWidget(self.volumeslider)
self.volumeslider.valueChanged.connect(self.setVolume)
self.vboxlayout = QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.vboxlayout.addWidget(self.positionslider)
self.vboxlayout.addLayout(self.hbuttonbox)
status = self.controller.getLikeStatus(self.movieID, self.user.id)
if(status == 0):
self.likeButton = QPushButton("Like")
else:
self.likeButton = QPushButton("DisLike")
self.likeButton.clicked.connect(lambda: self.LikeDislike())
self.comment = QPlainTextEdit()
self.likehbox = QHBoxLayout()
self.comment.setPlaceholderText("Type Comment Here ...")
self.comment.setObjectName("comment")
self.comment.setFixedHeight(50)
self.commentbox = QVBoxLayout()
self.commentSubmit = QPushButton("Comment")
self.commentSubmit.setObjectName("commentSubmit")
self.commentSubmit.setFixedWidth(200)
self.commentSubmit.clicked.connect(lambda: self.submitComment())
self.commentbox.addWidget(self.comment)
self.likehbox.addWidget(self.commentSubmit)
self.likehbox.addWidget(self.likeButton)
self.commentbox.addLayout(self.likehbox)
self.commentSection = QLabel("Comments:")
self.commentSection.setFixedHeight(50)
self.commentSection.setObjectName("commentSection")
self.commentSection.setFixedHeight(30)
self.commentbox.addWidget(self.commentSection)
self.commentSection_comments = QTextEdit("No Comments Yet")
self.commentSection_comments.setObjectName("commentSection_comments")
self.commentSection_comments.setReadOnly(True)
self.commentSection_comments.setFixedHeight(50)
self.commentbox.addWidget(self.commentSection_comments)
self.vboxlayout.addLayout(self.commentbox)
self.setLayout(self.vboxlayout)
self.playbutton.clicked.connect(lambda: self.PlayPause())
self.stopbutton.clicked.connect(lambda: self.Stop())
self.resize(640,480)
self.timer = QTimer(self)
self.timer.setInterval(500)
self.timer.timeout.connect(self.updateUI)
self.stats = None
self.loaded = False
self.video = pafy.new(self.url)
self.best = self.video.getbest()
self.media = self.instance.media_new(self.best.url)
self.mediaplayer.set_media(self.media)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.mediaplayer.set_xwindow(self.videoframe.winId())
elif sys.platform == "win32": # for Windows
self.mediaplayer.set_hwnd(self.videoframe.winId())
elif sys.platform == "darwin": # for MacOS
self.mediaplayer.set_nsobject(int(self.videoframe.winId()))
self.LoadComments()
def openStats(self):
if(self.stats != None):
del self.stats
result = self.controller.isMovieForUser(self.movieID, self.user.id)
if(isinstance(self.user, Professor) == True and len(result) > 0):
self.numberofViews = result[0][1]
self.description = result[0][0]
self.numofLikes = result[0][2]
self.stats = self.controller.openStatsLayout(self.numberofViews, self.description, self.numofLikes)
self.stats.show()
def LikeDislike(self):
if(self.likeButton.text() == "DisLike"):
self.controller.removieLike(self.movieID, self.user.id)
self.likeButton.setText("Like")
elif(self.likeButton.text() == "Like"):
self.controller.LikeMovie(self.movieID, self.user.id)
self.likeButton.setText("DisLike")
def submitComment(self):
if(self.comment.toPlainText() != ""):
result = self.controller.submitComment(self.movieID, self.comment.toPlainText(), self.user.username)
if(result == 1):
self.commentSection_comments.setPlainText(self.user.username + ": " + self.comment.toPlainText() + "\n" + self.commentSection_comments.toPlainText())
self.comment.setPlainText("")
def LoadComments(self):
comments = self.controller.getComments(self.movieID)
comment = ""
for i in comments:
comment += i[1] + ": " + i[0] + "\n"
self.commentSection_comments.setPlainText(comment)
#Plays and pauses the movie, same button changes status from play to pause
def PlayPause(self):
print("Play Pause")
if(self.loaded == False):
self.loaded = True
if self.mediaplayer.is_playing():
self.mediaplayer.pause()
#self.playbutton.setText("Play")
self.isPaused = True
else:
self.mediaplayer.play()
#self.playbutton.setText("Pause")
self.timer.start()
self.isPaused = False
#stops the movie, unloads it, pressing this requires loading the movie again
def Stop(self):
self.mediaplayer.stop()
#self.playbutton.setText("Play")
#Opens the movie from youtube and sets it in the appropriate layout item to be showcased
#TODO: add and use param (string youtube_url)
def OpenFile(self, filename=None):
self.video = pafy.new(self.url)
self.best = self.video.getbest()
self.media = self.instance.media_new(self.best.url)
self.mediaplayer.set_media(self.media)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.mediaplayer.set_xwindow(self.videoframe.winId())
elif sys.platform == "win32": # for Windows
self.mediaplayer.set_hwnd(self.videoframe.winId())
elif sys.platform == "darwin": # for MacOS
self.mediaplayer.set_nsobject(int(self.videoframe.winId()))
#Changes the volume of the media player
def setVolume(self, Volume):
#Set the volume
self.mediaplayer.audio_set_volume(Volume)
#Sets the position of the video slider, responsible for going to different times in the movie
def setPosition(self, position):
# setting the position to where the slider was dragged
self.mediaplayer.set_position(position / 1000.0)
# the vlc MediaPlayer needs a float value between 0 and 1, Qt
# uses integer variables, so you need a factor; the higher the
# factor, the more precise are the results
# (1000 should be enough)
#function used by timer to keep the movie's time slider indicator in the right position
def updateUI(self):
# setting the slider to the desired position
self.positionslider.setValue(self.mediaplayer.get_position() * 1000)
#Initiator
def run():
app = QApplication(sys.argv)
welcome_widget = MovieLayout("https://www.youtube.com/watch?v=LeYsRMZFUq0&t=6s",1)
welcome_widget.show()
sys.exit(app.exec_())
#if __name__ == '__main__':
# run()