-
Notifications
You must be signed in to change notification settings - Fork 0
/
antigo.py
498 lines (392 loc) · 18.4 KB
/
antigo.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import sys
import requests
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QGridLayout, QLabel, QLineEdit
from PyQt5.QtWidgets import QTextEdit, QWidget, QDialog, QApplication, QMainWindow, QTableWidgetItem, QStyle, QMenu, QAction
from PyQt5 import *
import json
import main_ui1
import login_ui
import create_user_ui
import mwindow3_ui
import addplaylist_ui
from config import config
from config import themes
from lib import session_lib, request_lib, server
from gi.repository import Gst, GLib
from random import randint
import time
######################## CLASSE QUE TRATA DO LOGIN #############################
class Login(QDialog, login_ui.Ui_Dialog):
def __init__(self, parent=None):
super(Login, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.logincheck)
self.pushButton_2.clicked.connect(self.criarContaWindow)
def criarContaWindow(self):
self.hide()
self.criarConta = criarConta()
self.criarConta.show()
def teste(self):
self.username = str(self.lineEdit.text())
self.accept()
def logincheck(self):
self.username = str(self.lineEdit.text())
self.password = str(self.lineEdit_2.text())
if (self.username == "" or self.password == ""):
messages = "Preencha todos os campos."
self.label.setStyleSheet("color: red")
self.label.setText("%s"%messages)
else:
response, ok = server.postServer(
'auth/login',
{'user_name' : self.username,'password' : self.password}
)
if response['success'] == True:
# QtWidgets.QMessageBox.information(
# self, "Sucesso", "Bem vindo/a: %s"% self.username
# )
self.label.setText("Loggin in...")
session_lib.set_token(response['payload']['token'])
self.label.setStyleSheet("color: green")
self.accept()
else:
messages = "Login Inválido"
self.label.setStyleSheet("color: red")
self.label.setText("%s"%messages)
#http://stackoverflow.com/questions/31843262/pass-a-variable-from-one-class-to-another-in-pyqt
#http://bytes.com/topic/python/answers/462303-pyqt-qdialog-qmainwindow-interacting-each-other
def accept(self):
self.mainwindow = Pytunes(self.username)
self.mainwindow.show()
QDialog.accept(self)
######################## CLASSE PARA CRIAR CONTA ###############################
class criarConta(QDialog, create_user_ui.Ui_Dialog):
#http://stackoverflow.com/questions/30470433/how-to-put-a-child-window-inside-a-main-windowpyqt
def __init__(self, parent=None):
super(criarConta, self).__init__(parent)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.setupUi(self)
self.buttonBox.accepted.connect(self.testarConta)
self.buttonBox.rejected.connect(self.cancelCriarConta)
def testarConta(self):
response, ok = server.postServer(
'auth/create_account',
{'name':self.newUser.text(),'password':self.newPassword.text(),'password_confirmation':self.confPassword.text(),'email':self.eMail.text()}
)
#response = requests.post(config.server_host+'/auth/create_account', {'name':self.newUser.text(),'password':self.newPassword.text(),'password_confirmation':self.confPassword.text(),'email':self.eMail.text()}, verify=False)
if response['success'] == True:
QtWidgets.QMessageBox.information(self, "Sucesso", "Utilizador %s criado"% self.newUser.text())
self.accept()
else:
fail = response['error_message']
messages = "\n".join(fail)
QtWidgets.QMessageBox.information(self, "Erro", "%s"%messages)
def accept(self):
QDialog.accept(self)
self.login = Login()
self.login.show()
def cancelCriarConta(self):
self.close()
self.login = Login()
self.login.show()
#http://stackoverflow.com/questions/13768012/to-show-pyqt-main-window-inside-a-function
class Pytunes(QMainWindow, main_ui1.Ui_MainWindow):
def __init__(self, username, parent=None):
super(Pytunes, self).__init__(parent)
self.setupUi(self)
#self.setWindowFlags(Qt.FramelessWindowHint);
self.loggeduser = username
self.setWindowTitle("Pytunes - CET02 Project")
#http://stackoverflow.com/questions/21334518/i-need-help-making-a-menu-bar-in-pyqt5
homeLogOut = self.menuHome.addAction('Logout')
homeExit = self.menuHome.addAction('Exit')
homeExit.triggered.connect(self.exit)
homeLogOut.triggered.connect(self.logout)
self.play_list = user_interface(username)
self.setCentralWidget(self.play_list)
def logout(self):
session_lib.del_token()
self.close()
self.login = Login()
self.login.show()
def exit(self):
self.close()
class user_interface(QWidget, mwindow3_ui.Ui_Form):
def __init__(self, username, parent=None):
super(user_interface, self).__init__(parent)
self.setupUi(self)
self.user_playlist()
self.playbutton = self.pushButton_2
self.playbutton.setText("")
self.is_playing = False
self.currently_playingID = None
self.path_musica = None
self.playbutton.clicked.connect(self.change_state)
self.playbutton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.rewindbutton = self.pushButton
self.rewindbutton.setText("")
self.rewindbutton.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipBackward))
self.rewindbutton.clicked.connect(self.on_previous)
self.forwardbutton = self.pushButton_3
self.forwardbutton.setText("")
self.forwardbutton.setIcon(self.style().standardIcon(QStyle.SP_MediaSkipForward))
self.forwardbutton.clicked.connect(self.on_next)
self.horizontalSlider.setTickInterval(10)
self.horizontalSlider.setSingleStep(1)
self.horizontalSlider.valueChanged.connect(self.setVolume)
self.label_4.setText("Welcome %s"%username)
self.playbin = Gst.ElementFactory.make("playbin", "playbin")
self.addplaylist_button = self.pushButton_4
self.addplaylist_button.clicked.connect(self.add_playlist)
self.bus = self.playbin.get_bus()
self.bus.add_signal_watch()
self.bus.connect('message', self.debugging)
self.w = None
#http://notes.brooks.nu/2011/01/python-gstreamer-controller/
#Usamos esta função para fazer debugging à aplicação e para controlar quando a
#música chega ao fim dado que sempre que isso acontece é enviada uma mensagem
#GST_MESSAGE_EOS
def on_slider_seek(self):
seek_time_secs = self.horizontalSlider_2.value()
self.playbin.seek_simple(Gst.Format.TIME, Gst.SeekFlags.FLUSH | Gst.SeekFlags.KEY_UNIT, seek_time_secs * Gst.SECOND)
def add_playlist(self):
self.w = addplaylist(self)
self.w.show()
def debugging(self, bus, message):
debug = message.type
print(debug)
if "GST_MESSAGE_EOS" in str(debug):
self.on_next()
def setVolume(self, value):
self.horizontalSlider.setValue(value)
self.playbin.set_property('volume', self.horizontalSlider.value()/100)
def user_playlist(self):
self.listWidget.clear()
from PyQt5.QtCore import QVariant
from PyQt5.QtWidgets import QListWidgetItem
r = server.getServer('user/playlists', {'token':session_lib.get_token()})
playlists = r['payload']['playlists']
self.listWidget.itemDoubleClicked.connect(self.playlist_changed)
my_item2 = QListWidgetItem()
my_item2.setText('Browse Music')
my_item2.setIcon(QIcon("images/browse-music.png"))
my_item2.setData(Qt.UserRole, 999)
self.listWidget.addItem(my_item2)
#http://stackoverflow.com/questions/13536839/qt-how-to-improve-my-contextmenu
self.listWidget.setContextMenuPolicy(Qt.CustomContextMenu)
self.listWidget.customContextMenuRequested.connect(self.handleContext)
for p in playlists:
my_item=QListWidgetItem()
my_item.setText(p['name'])
self.listWidget.addItem(my_item)
myObject=str(p['id'])
my_item.setIcon(QIcon("images/playlist-icon.png"))
my_item.setData(Qt.UserRole, myObject)
#http://stackoverflow.com/questions/13536839/qt-how-to-improve-my-contextmenu
def handleContext(self, pos):
item = self.listWidget.itemAt(pos)
itemrow = self.listWidget.row(self.listWidget.itemAt(pos))
if item is not None and itemrow is not 0:
itemid = item.data(Qt.UserRole)
itemname = str(item.text())
menu = QMenu("Context menu", self)
#http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot
#http://www.thehackeruniversity.com/2014/02/20/pyqt5-beginner-tutorial-part-3/
delete = QAction(QIcon("images/delete-playlist.png"), "Delete: '%s'"%itemname, self)
menu.addAction(delete)
print("#############itemid", itemid)
delete.triggered.connect(lambda: self.delete_playlist(itemid))
menu.exec_(self.listWidget.mapToGlobal(pos))
def delete_playlist(self, pid):
print("DDDDDDDDDDDpid", pid)
r = server.postServer('playlists/delete', {'playlist_id':int(pid),'token':session_lib.get_token()})
print(r)
#if r['success'] is True:
#time.sleep(2)
self.user_playlist()
#else:
# print('Erro a apagar a playlist..')
def get_library(self):
self.clean_table()
r = server.getServer('musics/', {'token':session_lib.get_token()})
if len(r['payload']['musics']) == 0:
self.clean_table()
else:
self.tableWidget.setRowCount(len(r['payload']['musics']))
self.tableWidget.setColumnCount(1)
self.tableWidget.setHorizontalHeaderLabels(['Musica'])
self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.cellDoubleClicked.connect(self.select_music)
i = 0
self.musics_in_current_playlist_by_index = {}
for music in r['payload']['musics']:
self.musics_in_current_playlist_by_index[i] = {
'id': music['id'], 'file_name': music['file_name'],
'name': music['name']
}
musica = QTableWidgetItem(music['name'])
file_path = QTableWidgetItem(music['file_name'])
musica.setFlags(musica.flags() & ~Qt.ItemIsEditable)
self.tableWidget.setItem(i, 0, musica)
self.tableWidget.setItem(i, 1, file_path)
i+=1
def playlist_changed(self, arg):
playlist_id = arg.data(Qt.UserRole)
if playlist_id == 999:
self.get_library()
elif playlist_id is None:
pass
else:
self.show_musics(playlist_id)
def show_musics(self, playmid):
self.clean_table()
r = server.getServer('playlists/'+playmid, {'id':playmid})
#http://nullege.com/codes/show/src@p@y@pyqt5-HEAD@examples@dialogs@findfiles.py/193/PyQt5.QtWidgets.QTableWidget.setSelectionBehavior
if len(r['payload']['musics']) == 0:
self.clean_table()
else:
self.tableWidget.setRowCount(len(r['payload']['musics']))
self.tableWidget.setColumnCount(1)
self.tableWidget.setHorizontalHeaderLabels(['Musica'])
self.tableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.cellDoubleClicked.connect(self.select_music)
i = 0
self.musics_in_current_playlist_by_index = {}
for music in r['payload']['musics']:
self.musics_in_current_playlist_by_index[i] = {
'id': music['id'], 'file_name': music['file_name'],
'name': music['name']
}
musica = QTableWidgetItem(music['name'])
file_path = QTableWidgetItem(music['file_name'])
musica.setFlags(musica.flags() & ~Qt.ItemIsEditable)
self.tableWidget.setItem(i, 0, musica)
self.tableWidget.setItem(i, 1, file_path)
i+=1
def clean_table(self):
self.tableWidget.setColumnCount(0)
self.tableWidget.hideColumn(0)
self.tableWidget.setRowCount(0)
def change_state(self):
if self.is_playing == False:
self.on_play()
elif self.is_playing == True:
self.on_pause()
def select_music(self, i):
self.playbin.set_state(Gst.State.NULL)
self.path_musica = self.musics_in_current_playlist_by_index[i]['file_name']
self.currently_playingID = i
self.currently_playing = self.musics_in_current_playlist_by_index[self.currently_playingID]['name']
#self.currently_playingID = i
self.on_play()
self.label.setText(self.currently_playing)
def on_play(self):
#self.playbin.set_property('uri',config.server_host+'/musics/'+self.path_musica)
if self.path_musica is None:
pass
else:
self.playbin.set_property('uri',config.server_host+'/musics/%s'%self.path_musica)
self.is_playing = True
self.playbin.set_state(Gst.State.PLAYING)
self.playbutton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))
#GLib.timeout_add(1000, self.update_slider)
#self.label.setText("Form","%"% )
def on_pause(self):
self.is_playing = False
self.playbin.set_state(Gst.State.PAUSED)
self.playbutton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
def on_next(self):
if self.currently_playingID is None:
pass
elif self.currently_playingID < len(self.musics_in_current_playlist_by_index)-1:
self.is_playing = False
self.playbin.set_state(Gst.State.PAUSED)
self.currently_playingID += 1
next_music = self.musics_in_current_playlist_by_index[self.currently_playingID]['file_name']
self.tableWidget.setCurrentCell(self.currently_playingID, 0)
self.label.setText(self.musics_in_current_playlist_by_index[self.currently_playingID]['name'])
self.next(next_music)
else:
self.currently_playingID = 0
self.tableWidget.setCurrentCell(self.currently_playingID, 0)
next_music = self.musics_in_current_playlist_by_index[self.currently_playingID]['file_name']
self.label.setText(self.musics_in_current_playlist_by_index[self.currently_playingID]['name'])
self.next(next_music)
def next(self, next_music):
self.playbin.set_state(Gst.State.READY)
self.playbin.set_property('uri',config.server_host+'/musics/'+next_music)
self.playbin.set_state(Gst.State.PLAYING)
def on_previous(self, widget):
if self.currently_playingID is None:
pass
elif self.currently_playingID > 0:
self.is_playing = False
self.playbin.set_state(Gst.State.PAUSED)
self.currently_playingID -= 1
next_music = self.musics_in_current_playlist_by_index[self.currently_playingID]['file_name']
self.tableWidget.setCurrentCell(self.currently_playingID, 0)
self.label.setText(self.musics_in_current_playlist_by_index[self.currently_playingID]['name'])
self.next(next_music)
else:
self.currently_playingID = 0
self.tableWidget.setCurrentCell(self.currently_playingID, 0)
next_music = self.musics_in_current_playlist_by_index[self.currently_playingID]['file_name']
self.next(next_music)
def previous(self, next_music):
self.playbin.set_state(Gst.State.READY)
self.playbin.set_property('uri',config.server_host+'/musics/'+next_music)
self.playbin.set_state(Gst.State.PLAYING)
#starting up a timer to check on the current playback value
#GLib.timeout_add(1000, self.update_slider)
class addplaylist(QWidget, addplaylist_ui.Ui_Form):
def __init__(self, m, parent=None):
#QWidget.__init__(self, parent)
super(addplaylist, self).__init__(parent)
self.user_interface = m
self.setupUi(self)
self.pushButton.clicked.connect(self.addbutton)
def addbutton(self):
playlist_name = self.lineEdit.text()
print(playlist_name)
response, ok = server.postServer(
'playlists/add',
{'playlist_name':playlist_name, 'token':session_lib.get_token()}
)
print(response)
if response['success'] == True:
self.close()
time.sleep(0.05)
self.user_interface.user_playlist()
#http://stackoverflow.com/questions/26603540/how-to-avoid-the-child-qmainwindow-disappearing
#UTIL SE O UTILIZADOR JÁ TIVER O COOKIE DE SESSÃO VÁLIDO PASSA LOGO PARA A MAINWINDOW
#TIVE QUE FAZER SUBCLASS DA APPLICATION()
class Application(QApplication):
def __init__(self):
QApplication.__init__(self, sys.argv)
self._window = None
def window(self):
if self._window is None:
if session_lib.get_token() == "":
self._window = Login()
return self._window
else:
response, ok = server.postServer(
'auth/check_token',
{'token' : session_lib.get_token()}
)
if response['success'] == True:
username = response['payload']['user']['name']
self._window = Pytunes(username)
else:
print('fail')
return self._window
if __name__ == '__main__':
app = QApplication = Application()
app.setStyleSheet(themes.default_theme)
Gst.init()
app.window().show()
sys.exit(app.exec_())