Switch to one-shot timers in search_box. Fix preference migration in database2

This commit is contained in:
Charles Haley 2010-07-20 19:52:38 +01:00
parent 2ec97d2b89
commit 7443d8f8c5
2 changed files with 15 additions and 24 deletions

View File

@ -10,7 +10,7 @@ import re
from PyQt4.Qt import QComboBox, Qt, QLineEdit, QStringList, pyqtSlot, \ from PyQt4.Qt import QComboBox, Qt, QLineEdit, QStringList, pyqtSlot, \
pyqtSignal, SIGNAL, QObject, QDialog, QCompleter, \ pyqtSignal, SIGNAL, QObject, QDialog, QCompleter, \
QAction, QKeySequence QAction, QKeySequence, QTimer
from calibre.gui2 import config from calibre.gui2 import config
from calibre.gui2.dialogs.confirm_delete import confirm from calibre.gui2.dialogs.confirm_delete import confirm
@ -82,7 +82,9 @@ class SearchBox2(QComboBox):
self.help_state = False self.help_state = False
self.as_you_type = True self.as_you_type = True
self.prev_search = '' self.prev_search = ''
self.timer = None self.timer = QTimer()
self.timer.setSingleShot(True)
self.timer.timeout.connect(self.timer_event)
self.setInsertPolicy(self.NoInsert) self.setInsertPolicy(self.NoInsert)
self.setMaxCount(self.MAX_COUNT) self.setMaxCount(self.MAX_COUNT)
self.setSizeAdjustPolicy(self.AdjustToMinimumContentsLengthWithIcon) self.setSizeAdjustPolicy(self.AdjustToMinimumContentsLengthWithIcon)
@ -116,9 +118,6 @@ class SearchBox2(QComboBox):
self.search.emit('') self.search.emit('')
self._in_a_search = False self._in_a_search = False
self.setEditText(self.help_text) self.setEditText(self.help_text)
if self.timer is not None: # Turn off any timers that got started in setEditText
self.killTimer(self.timer)
self.timer = None
self.line_edit.home(False) self.line_edit.home(False)
self.line_edit.setStyleSheet( self.line_edit.setStyleSheet(
'QLineEdit { color: gray; background-color: %s; }' % 'QLineEdit { color: gray; background-color: %s; }' %
@ -147,18 +146,15 @@ class SearchBox2(QComboBox):
self._in_a_search = False self._in_a_search = False
if event.key() in (Qt.Key_Return, Qt.Key_Enter): if event.key() in (Qt.Key_Return, Qt.Key_Enter):
self.do_search() self.do_search()
self.timer = self.startTimer(self.__class__.INTERVAL) self.timer.start(1500)
def mouse_released(self, event): def mouse_released(self, event):
self.normalize_state() self.normalize_state()
if self.as_you_type: if self.as_you_type:
self.timer = self.startTimer(self.__class__.INTERVAL) self.timer.start(1500)
def timerEvent(self, event): def timer_event(self):
self.killTimer(event.timerId()) self.do_search()
if event.timerId() == self.timer:
self.timer = None
self.do_search()
def history_selected(self, text): def history_selected(self, text):
self.emit(SIGNAL('changed()')) self.emit(SIGNAL('changed()'))
@ -212,9 +208,6 @@ class SearchBox2(QComboBox):
return return
self.normalize_state() self.normalize_state()
self.setEditText(txt) self.setEditText(txt)
if self.timer is not None: # Turn off any timers that got started in setEditText
self.killTimer(self.timer)
self.timer = None
self.search.emit(txt) self.search.emit(txt)
self.line_edit.end(False) self.line_edit.end(False)
self.initial_state = False self.initial_state = False

View File

@ -144,15 +144,13 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
self.prefs = DBPrefs(self) self.prefs = DBPrefs(self)
# Migrate saved search and user categories to db preference scheme # Migrate saved search and user categories to db preference scheme
def migrate_preference(name, default): def migrate_preference(key, default):
obsolete = '###OBSOLETE--DON\'T USE ME###' oldval = prefs[key]
ans = self.prefs.get(name, None) if oldval != default:
if ans is None: self.prefs[key] = oldval
ans = prefs[name] prefs[key] = default
if ans in (None, obsolete): if key not in self.prefs:
ans = default self.prefs[key] = default
prefs[name] = obsolete
self.prefs[name] = ans
migrate_preference('user_categories', {}) migrate_preference('user_categories', {})
migrate_preference('saved_searches', {}) migrate_preference('saved_searches', {})