Files
Portable-Computing-Toolkit-…/portable_computing_toolkit_installer/ui/downloader.py
2019-04-23 16:46:25 -04:00

100 lines
4.4 KiB
Python

from pathlib import Path
import webbrowser
from PyQt5 import QtCore, QtGui, QtNetwork, QtWebEngineWidgets, QtWidgets
from PyQt5.uic import loadUi
class Downloader(QtWidgets.QMainWindow):
def __init__(self, tool_name, tool_info, download_directory, parent = None, flags = None):
super().__init__(parent, flags)
loadUi(Path(__file__).parent / "downloader.ui", baseinstance = self)
self.hide()
#create an off-the-record profile, isolated from other Downloader objects
new_page = QtWebEngineWidgets.QtWebEnginePage(QtWebEngineWidgets.QWebEngineProfile(self.web_view))
self.web_view.setPage(new_page)
#set application style and decorate pushbuttons
self._style = QtWidgets.QApplication.instance().style()
self.setWindowIcon(QtWidgets.QApplication.instance().windowIcon())
self.back_button.setIcon(self._style.standardIcon(self._style.SP_ArrowBack))
self.forward_button.setIcon(self._style.standardIcon(self._style.SP_ArrowForward))
self.reload_stop_button.setIcon(self._style.standardIcon(self._style.SP_BrowserReload))
self.home_button.setIcon(self._style.standardIcon(self._style.SP_ArrowUp))
#connect signals to make useable as a browser
self.web_view.urlChanged.connect(lambda url: self.address_bar.setText(url.toString()))
self.web_view.loadStarted.connect(self._reload_becomes_stop)
self.web_view.loadFinished.connect(self._stop_becomes_reload)
self.address_bar.returnPressed.connect(lambda: self.web_view.load(QtCore.QUrl(self.address_bar.text())))
#connect cancel actions to handlers
self.actionCancel.triggered.connect(self._cancel_manual_search)
self.actionCancel_and_Open_Issue_on_Github.triggered.connect(self._cancel_manual_search_open_issue)
#set attributes
self._tool_name = tool_name
self._tool_info = tool_info
self._download_directory = download_directory
#name window from attribute
self.setWindowTitle(f"Find {self._tool_name}")
#get homepage from attributes and set home button to load it
self._home_page = tool_info["homepage"]
self.home_button.clicked.connect(lambda: self.web_view.load(QtCore.QUrl(self._home_page)))
def _reload_becomes_stop(self):
"""Turns the reload button into a stop button.
"""
self._reload_stop_button.clicked.disconnect()
self._reload_stop_button.clicked.connect(self.web_view.stop)
self._reload_stop_button.setToolTip("Stop")
self._reload_stop_button.setIcon(self._style.standardIcon(self._style.SP_BrowserStop))
self._check_history()
def _stop_becomes_reload(self, ok):
"""Turns a temporary stop button back into a reload button
Args:
ok (bool): Received from signal indicating load was stopped without error (True) or because of error (False). Ignored.
"""
self._reload_stop_button.clicked.disconnect()
self._reload_stop_button.clicked.connect(self.web_view.reload)
self._reload_stop_button.setToolTip("Reload")
self._reload_stop_button.setIcon(self._style.standardIcon(self._style.SP_BrowserReload))
self._check_history()
def _check_history(self):
if self.web_view.history().canGoBack():
self.back_button.setEnabled(True)
else:
self.back_button.setEnabled(False)
if self.web_view.history().canGoForward():
self.forward_button.setEnabled(True)
else:
self.forward_button.setEnabled(False)
def _cancel_manual_search(self):
self.close()
QtCore.QThread.currentThread().exit(1)
def _cancel_manual_search_open_issue(self):
self.close()
webbrowser.open_new_tab("https://github.com/norweeg/portable-computing-toolkit-installer/issues/new")
QtCore.QThread.currentThread().exit(1)
def begin_manual_search(self):
self.web_view.history().clear()
self._check_history()
self.show()
self.web_view.load(QtCore.QUrl(self._home_page))
def begin_auto_search(self):
pass
def _download_file(self):
pass
class DownloadWorker(QtCore.QRunnable):
def __init__(self, tool_name, tool_info, download_directory):
super().__init__()
self._downloader = Downloader(tool_name, tool_info, download_directory)
def run(self):
self._downloader.begin_auto_search()