[Qhandjoob] improve qhandjoob

This commit is contained in:
Bezleputh 2013-10-16 20:58:56 +02:00
commit 8a774daaee

View file

@ -17,8 +17,8 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from PyQt4.QtGui import QListWidgetItem from PyQt4.QtGui import QListWidgetItem, QApplication, QCompleter
from PyQt4.QtCore import SIGNAL from PyQt4.QtCore import SIGNAL, Qt, QStringList
from weboob.tools.application.qt import QtMainWindow, QtDo from weboob.tools.application.qt import QtMainWindow, QtDo
from weboob.tools.application.qt.backendcfg import BackendCfg from weboob.tools.application.qt.backendcfg import BackendCfg
@ -26,6 +26,9 @@ from weboob.capabilities.job import ICapJob
from .ui.main_window_ui import Ui_MainWindow from .ui.main_window_ui import Ui_MainWindow
import os
import codecs
class JobListWidgetItem(QListWidgetItem): class JobListWidgetItem(QListWidgetItem):
def __init__(self, job, *args, **kwargs): def __init__(self, job, *args, **kwargs):
@ -33,7 +36,10 @@ class JobListWidgetItem(QListWidgetItem):
self.job = job self.job = job
def __lt__(self, other): def __lt__(self, other):
return self.job.publication_date < other.job.publication_date if self.job.publication_date and other.job.publication_date:
return self.job.publication_date < other.job.publication_date
else:
return False
def setAttrs(self, storage): def setAttrs(self, storage):
text = u'%s - %s' % (self.job.backend, self.job.title) text = u'%s - %s' % (self.job.backend, self.job.title)
@ -54,6 +60,10 @@ class MainWindow(QtMainWindow):
self.process_photo = {} self.process_photo = {}
self.process_bookmarks = {} self.process_bookmarks = {}
# search history is a list of patterns which have been searched
self.search_history = self.loadSearchHistory()
self.updateCompletion()
self.ui.jobFrame.hide() self.ui.jobFrame.hide()
self.connect(self.ui.actionBackends, SIGNAL("triggered()"), self.backendsConfig) self.connect(self.ui.actionBackends, SIGNAL("triggered()"), self.backendsConfig)
@ -71,17 +81,55 @@ class MainWindow(QtMainWindow):
if self.weboob.count_backends() == 0: if self.weboob.count_backends() == 0:
self.backendsConfig() self.backendsConfig()
def loadSearchHistory(self):
''' Return search string history list loaded from history file
'''
result = []
history_path = os.path.join(self.weboob.workdir, 'qhandjoob_history')
if os.path.exists(history_path):
f = codecs.open(history_path, 'r', 'utf-8')
conf_hist = f.read()
f.close()
if conf_hist is not None and conf_hist.strip() != '':
result = conf_hist.strip().split('\n')
return result
def saveSearchHistory(self):
''' Save search history in history file
'''
if len(self.search_history) > 0:
history_path = os.path.join(self.weboob.workdir, 'qhandjoob_history')
f = codecs.open(history_path, 'w', 'utf-8')
f.write('\n'.join(self.search_history))
f.close()
def updateCompletion(self):
qc = QCompleter(QStringList(self.search_history), self)
qc.setCaseSensitivity(Qt.CaseInsensitive)
self.ui.searchEdit.setCompleter(qc)
def tabChange(self, index): def tabChange(self, index):
if index == 1: if index == 1:
self.doAdvancedSearch() self.doAdvancedSearch()
def doAdvancedSearch(self): def doAdvancedSearch(self):
QApplication.setOverrideCursor(Qt.WaitCursor)
self.ui.jobListAdvancedSearch.clear() self.ui.jobListAdvancedSearch.clear()
self.process = QtDo(self.weboob, self.addJobAdvancedSearch) self.process = QtDo(self.weboob, self.addJobAdvancedSearch)
self.process.do('advanced_search_job') self.process.do('advanced_search_job')
def doSearch(self): def doSearch(self):
QApplication.setOverrideCursor(Qt.WaitCursor)
pattern = unicode(self.ui.searchEdit.text()) pattern = unicode(self.ui.searchEdit.text())
# arbitrary max number of completion word
if pattern:
if len(self.search_history) > 50:
self.search_history.pop(0)
if pattern not in self.search_history:
self.search_history.append(pattern)
self.updateCompletion()
self.ui.jobList.clear() self.ui.jobList.clear()
self.process = QtDo(self.weboob, self.addJobSearch) self.process = QtDo(self.weboob, self.addJobSearch)
self.process.do('search_job', pattern) self.process.do('search_job', pattern)
@ -91,11 +139,17 @@ class MainWindow(QtMainWindow):
if item: if item:
self.ui.jobList.addItem(item) self.ui.jobList.addItem(item)
if not backend:
QApplication.restoreOverrideCursor()
def addJobAdvancedSearch(self, backend, job): def addJobAdvancedSearch(self, backend, job):
item = self.addJob(backend, job) item = self.addJob(backend, job)
if item: if item:
self.ui.jobListAdvancedSearch.addItem(item) self.ui.jobListAdvancedSearch.addItem(item)
if not backend:
QApplication.restoreOverrideCursor()
def addJob(self, backend, job): def addJob(self, backend, job):
if not backend: if not backend:
self.process = None self.process = None
@ -109,6 +163,7 @@ class MainWindow(QtMainWindow):
return item return item
def closeEvent(self, event): def closeEvent(self, event):
self.saveSearchHistory()
QtMainWindow.closeEvent(self, event) QtMainWindow.closeEvent(self, event)
def backendsConfig(self): def backendsConfig(self):
@ -117,6 +172,7 @@ class MainWindow(QtMainWindow):
pass pass
def jobSelected(self, item, prev): def jobSelected(self, item, prev):
QApplication.setOverrideCursor(Qt.WaitCursor)
if item is not None: if item is not None:
job = item.job job = item.job
self.ui.queriesTabWidget.setEnabled(False) self.ui.queriesTabWidget.setEnabled(False)
@ -133,6 +189,7 @@ class MainWindow(QtMainWindow):
prev.setAttrs(self.storage) prev.setAttrs(self.storage)
def openJob(self): def openJob(self):
QApplication.setOverrideCursor(Qt.WaitCursor)
url = unicode(self.ui.idEdit.text()) url = unicode(self.ui.idEdit.text())
if not url: if not url:
return return
@ -146,6 +203,7 @@ class MainWindow(QtMainWindow):
self.setJob(job) self.setJob(job)
self.ui.idEdit.clear() self.ui.idEdit.clear()
QApplication.restoreOverrideCursor()
def gotJob(self, backend, job): def gotJob(self, backend, job):
if not backend: if not backend:
@ -172,3 +230,5 @@ class MainWindow(QtMainWindow):
self.ui.jobFrame.show() self.ui.jobFrame.show()
else: else:
self.ui.jobFrame.hide() self.ui.jobFrame.hide()
QApplication.restoreOverrideCursor()