def updateUIvk(self): userName = unicode(self.settings.value("userVK/username").toString()) cookie = unicode(self.settings.value("userVK/cookie").toString()) if userName and cookie: self.ui.userNameLabel.setText(userName) self.ui.logoffVkButton.setEnabled(True) self.ui.loginVkButton.setEnabled(False) else: self.ui.loginVkButton.setEnabled(True) self.ui.logoffVkButton.setEnabled(False) if not self.vkHandle.check_connection(): self.ui.statusLabel.setText(u"Невозможно установить соединение") else: self.thread = GenericThread(self.getUserInfo) self.thread.start()
def mainthread_wrapper(self): self.thread = GenericThread(self.thread_func) self.thread.start()
class SettingsFormVK(QWidget): updateUIuserSignal = pyqtSignal(bool, dict) setPreloaderStateSignal = pyqtSignal(bool) def __init__(self): QWidget.__init__(self) self.ui = Ui_settingsFormVK() self.ui.setupUi(self) self.updateUIuserSignal.connect(self.updateUIuser) self.setPreloaderStateSignal.connect(self.setPreloaderState) self.settings = QSettings() self.settings.beginGroup("app") self.vkHandle = VK() cookie = unicode(self.settings.value("userVK/cookie").toString()) if cookie: self.vkHandle.set_cookie(cookie) self.preloader = PreloaderWidget(self) self.updateUI() def updateUI(self): self.updateUIvk() def updateUIvk(self): userName = unicode(self.settings.value("userVK/username").toString()) cookie = unicode(self.settings.value("userVK/cookie").toString()) if userName and cookie: self.ui.userNameLabel.setText(userName) self.ui.logoffVkButton.setEnabled(True) self.ui.loginVkButton.setEnabled(False) else: self.ui.loginVkButton.setEnabled(True) self.ui.logoffVkButton.setEnabled(False) if not self.vkHandle.check_connection(): self.ui.statusLabel.setText(u"Невозможно установить соединение") else: self.thread = GenericThread(self.getUserInfo) self.thread.start() @pyqtSlot(bool) def setPreloaderState(self, state): if state: self.preloader.start() else: self.preloader.stop() def getUserInfo(self): self.setPreloaderStateSignal.emit(True) try: if self.checkCookies(): vkuser = self.vkHandle.getUser() if vkuser: self.updateUIuserSignal.emit(True, vkuser) self.setPreloaderStateSignal.emit(False) else: self.updateUIuserSignal.emit(False, {}) self.setPreloaderStateSignal.emit(False) finally: self.setPreloaderStateSignal.emit(False) @pyqtSlot(bool, dict) def updateUIuser(self, authorized, userInfo): if authorized: userName = userInfo.get("username", u"") self.settings.setValue("userVK/username", userName) self.ui.userNameLabel.setEnabled(True) self.ui.userNameLabel.setText(userName) self.ui.logoffVkButton.setEnabled(True) self.ui.loginVkButton.setEnabled(False) self.ui.statusLabel.setText(u"Авторизован") else: self.ui.userNameLabel.setEnabled(False) self.ui.userNameLabel.setText(u"") self.ui.statusLabel.setText(u"Не авторизован") self.ui.logoffVkButton.setEnabled(False) self.ui.loginVkButton.setEnabled(True) def checkCookies(self): if self.vkHandle.check_connection(): if self.vkHandle.is_logged(): return True else: self.flushUserInfo() return False def flushUserInfo(self): self.settings.setValue("userVK/username", "") self.settings.setValue("userVK/cookie", "") self.vkHandle.set_cookie("") @pyqtSlot(str) def accept_cookie(self, cookie): self.settings.setValue("userVK/cookie", unicode(cookie)) self.vkHandle.set_cookie(cookie) self.updateUI() @pyqtSlot() def on_loginVkButton_clicked(self): self.loginVkWindow = LoginVk() self.loginVkWindow.acceptCookieSignal.connect(self.accept_cookie) self.loginVkWindow.show() @pyqtSlot() def on_logoffVkButton_clicked(self): self.flushUserInfo() self.updateUI()
class MainWindowForm(QMainWindow): startAnimationSignal = pyqtSignal() stopAnimationSignal = pyqtSignal() setStateDownloadButtonSignal = pyqtSignal(bool) setStateAlreadyLabelSignal = pyqtSignal(bool) setProgressBarValueSignal = pyqtSignal(int) setStatusLabelTextSignal = pyqtSignal(unicode) setPlayNowLabelTextSignal = pyqtSignal(unicode) abortAndShowErrorSignal = pyqtSignal(unicode) setUpdatePlayNowIntervalSignal = pyqtSignal(int) updatePlayNowInterval = 15000 def __init__(self): QMainWindow.__init__(self) self.ui = Ui_MainForm() self.ui.setupUi(self) self.lastFMHandle = UserLastApi() self.musicLibHandle = MusicLib() self.singletonDownloader = SingletonDownload(self) self.settings = QSettings() self.settings.beginGroup('app') self.lastFMUser = '' self.current_track = {} self.scrollLabelTimer = QTimer() self.scrollLabelTimer.setInterval(50) self.updatePlayNowTimer = QTimer() self.updatePlayNowTimer.setInterval(self.updatePlayNowInterval) self.loadingAnimGif = QMovie(":/animations/loading.gif") self.loadingAnimGif.setScaledSize(QSize(24, 24)) self.startAnimationSignal.connect(self.start_animation) self.stopAnimationSignal.connect(self.stop_animation) self.setStateDownloadButtonSignal.connect(self.setStateDownloadButton) self.setProgressBarValueSignal.connect(self.setProgressBarValue) self.setStatusLabelTextSignal.connect(self.setStatusLabelText) self.setStateAlreadyLabelSignal.connect(self.setStateAlreadyLabel) self.setPlayNowLabelTextSignal.connect(self.setPlayNowLabelText) self.abortAndShowErrorSignal.connect(self.abortAndShowError) self.scrollLabelTimer.timeout.connect(self.on_timer_timeout) self.updatePlayNowTimer.timeout.connect(self.mainthread_wrapper) self.setUpdatePlayNowIntervalSignal.connect(self.setUpdatePlayNowInterval) self.updatePlayNowTimer.timeout.emit() self.updatePlayNowTimer.start() QObject.installEventFilter(self, self) @pyqtSlot() def mainthread_wrapper(self): self.thread = GenericThread(self.thread_func) self.thread.start() def thread_func(self): self.startAnimationSignal.emit() self.setStatusLabelTextSignal.emit(u' ') self.setStateAlreadyLabelSignal.emit(False) self.current_track = {} self.setStateDownloadButtonSignal.emit(False) self.check_current_lastfm_track() def check_current_lastfm_track(self): if not self.settings.contains('userLastFM/sessionkey'): self.abortAndShowErrorSignal.emit(u'Не авторизован в Last.FM, проверьте настройки') return try: if not self.lastFMUser: session_key = unicode(self.settings.value('userLastFM/sessionkey').toString()) self.lastFMUser = self.lastFMHandle.auth(session_key) except LastApiWSError: self.abortAndShowErrorSignal.emit(u'Не удалось авторизоваться в Last.FM') return except LastApiNetworkError: self.abortAndShowErrorSignal.emit(u'Не удалось установить соединение с сервером') return _current_track = self.lastFMHandle.get_playnow() #print _current_track if _current_track: self.current_track = _current_track song_title = self.current_track['artist'] + ' - ' + self.current_track['title'] self.stopAnimationSignal.emit() self.setPlayNowLabelTextSignal.emit(song_title) self.setUpdatePlayNowIntervalSignal.emit(self.updatePlayNowInterval) if self.musicLibHandle.isTrackExists(self.current_track['artist'], self.current_track['title']): self.setStateAlreadyLabelSignal.emit(True) else: self.setStateDownloadButtonSignal.emit(True) else: self.setUpdatePlayNowIntervalSignal.emit(self.updatePlayNowInterval / 4) self.stopAnimationSignal.emit() self.setStatusLabelTextSignal.emit(u'В данный момент нет проигрываемых треков') @pyqtSlot(unicode) def abortAndShowError(self, text): self.stopAnimationSignal.emit() self.current_track = {} self.setStatusLabelTextSignal.emit(text) @pyqtSlot() def start_animation(self): self.resetScrollArea() self.ui.playNowLabel.setMovie(self.loadingAnimGif) self.loadingAnimGif.start() @pyqtSlot() def stop_animation(self): self.loadingAnimGif.stop() self.loadingAnimGif.jumpToFrame(0) self.setPlayNowLabelTextSignal.emit(u' ') @pyqtSlot(unicode) def setPlayNowLabelText(self, text): self.ui.playNowLabel.setText(text) _font = self.ui.playNowLabel.font() _fm = QFontMetrics(_font) _str_width = _fm.width(self.ui.playNowLabel.text()) scrollAreaWidth = self.ui.scrollArea.width() if _str_width > scrollAreaWidth: self.startScrollArea(_str_width + 10) @pyqtSlot(unicode) def setStatusLabelText(self, text): self.ui.statusLabel.setText(text) @pyqtSlot(int) def setProgressBarValue(self, num): self.ui.downloadProgressBar.setValue(num) @pyqtSlot(bool) def setStateDownloadButton(self, state): self.ui.downloadButton.setEnabled(state) @pyqtSlot(bool) def setStateAlreadyLabel(self, state): if state: pixmap = QPixmap(':/icons/label/ok.png') self.ui.alreadyLabel.setPixmap(pixmap.scaled(24, 24)) self.ui.alreadyLabel.setToolTip(u'Трек уже есть в музыкальной библиотеке') else: pixmap = QPixmap(':/icons/label/ok_off.png') self.ui.alreadyLabel.setPixmap(pixmap.scaled(24, 24)) @pyqtSlot() def on_settingsButton_clicked(self): self.settingsForm = SettingsFormMain() self.settingsForm.show() @pyqtSlot() def on_actionPreferences_triggered(self): self.on_settingsButton_clicked() @pyqtSlot() def on_downloadButton_clicked(self): self.setStateDownloadButtonSignal.emit(False) self.singletonDownloader.start() @pyqtSlot() def on_exitButton_clicked(self): confirm_exit = QMessageBox.question(self, u'Подтверждение завершения LastVK', u"Вы действительно хотите завершить приложение LastVK?", QMessageBox.Ok | QMessageBox.Cancel ) if confirm_exit == QMessageBox.Ok: qApp.quit() @pyqtSlot() def on_timer_timeout(self): _value = self.ui.scrollArea.horizontalScrollBar().value() if _value >= self.__maxscroll: _value = -self.__scrollWidth self.ui.scrollArea.horizontalScrollBar().setValue(_value + 3) @pyqtSlot(int) def setUpdatePlayNowInterval(self, interval): self.updatePlayNowTimer.setInterval(interval) def resetScrollArea(self): self.scrollLabelTimer.stop() _scrollWidth = self.ui.scrollArea.width() self.ui.playNowLabel.setFixedWidth(_scrollWidth) self.ui.playNowLabel.setAlignment(Qt.AlignHCenter) self.ui.scrollArea.horizontalScrollBar().setValue(0) def startScrollArea(self, width): self.__scrollWidth = self.ui.scrollArea.width() self.__maxscroll = width self.ui.playNowLabel.setFixedWidth(width) self.ui.scrollArea.horizontalScrollBar().setMinimum(-self.__scrollWidth) self.ui.scrollArea.horizontalScrollBar().setMaximum(width) self.scrollLabelTimer.start() def eventFilter(self, obj, event): if event.type() == QEvent.WindowDeactivate: if QApplication.focusWidget() == self: self.close() return True else: return QObject.eventFilter(self, obj, event) def closeEvent(self, event): self.updatePlayNowTimer.stop() self.scrollLabelTimer.stop()