BTSearcher/ux.py

93 lines
2.8 KiB
Python

import sites
import window
from PyQt5.QtGui import QClipboard
from PyQt5.QtCore import QTimer, QThread, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableWidgetItem
class Ux(QMainWindow, window.Ui_MainWindow):
def __init__(self):
super().__init__()
self.clipboard = QApplication.clipboard()
# timer for update status bar
self.timer = QTimer(self)
self.timer.setInterval(1000)
self.timer.timeout.connect(self.update_state)
# setting ui
self.setupUi(self)
self.tb_result.setColumnWidth(0, 70)
self.tb_result.setColumnWidth(1, 90)
self.tb_result.setColumnWidth(2, 480)
for it in sites.lst:
self.cb_source.addItem(it.name)
def update_state(self):
self.process += 1
self.state.showMessage('Searching ' + '.' * (self.process % 10))
def stop_search(self):
self.state.showMessage('Over')
if self.timer.isActive():
self.timer.stop()
if self.th_search.isRunning():
self.th_search.stop()
self.th_search.wait()
def on_cb_source_currentIndexChanged(self, index):
if type(index) == type(1):
self.site = sites.lst[index]
self.stop_search()
def on_btn_search_clicked(self, b=True):
if b:
return
self.stop_search()
self.tb_result.clearContents()
self.tb_result.setRowCount(0)
self.process = 0
self.links = []
self.timer.start()
self.th_search = SearchThread(self, self.ln_key.text())
self.th_search.start()
def on_ln_key_returnPressed(self):
self.btn_search.click()
def on_tb_result_cellActivated(self, row, col):
self.clipboard.setText(self.links[row])
@pyqtSlot()
def do_stop_search(self):
self.timer.stop()
@pyqtSlot(dict)
def do_append_result(self, item):
row = self.tb_result.rowCount()
self.tb_result.insertRow(row)
self.tb_result.setItem(row, 0, QTableWidgetItem(item['hot']))
self.tb_result.setItem(row, 1, QTableWidgetItem(item['size']))
self.tb_result.setItem(row, 2, QTableWidgetItem(item['name']))
self.links.append(item['link'])
class SearchThread(QThread):
to_append_result = pyqtSignal([dict])
to_stop_search = pyqtSignal()
def __init__(self, obj, key_word, parent=None):
super(SearchThread, self).__init__(parent)
self.site = obj.site
self.key = key_word
self.to_append_result.connect(obj.do_append_result)
self.to_stop_search.connect(obj.do_stop_search)
def run(self):
if not self.key:
return
self.search = self.site(self.to_append_result)
self.search.search(self.key)
self.to_stop_search.emit()
def stop(self):
self.search.stop()