home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1312 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  17.4 KB  |  434 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import re
  9. from PyQt4.Qt import QComboBox, Qt, QLineEdit, QStringList, pyqtSlot, pyqtSignal, SIGNAL, QObject, QDialog, QCompleter, QAction, QKeySequence, QTimer
  10. from calibre.gui2 import config
  11. from calibre.gui2.dialogs.confirm_delete import confirm
  12. from calibre.gui2.dialogs.saved_search_editor import SavedSearchEditor
  13. from calibre.gui2.dialogs.search import SearchDialog
  14. from calibre.utils.search_query_parser import saved_searches
  15.  
  16. class SearchLineEdit(QLineEdit):
  17.     key_pressed = pyqtSignal(object)
  18.     mouse_released = pyqtSignal(object)
  19.     focus_out = pyqtSignal(object)
  20.     
  21.     def keyPressEvent(self, event):
  22.         self.key_pressed.emit(event)
  23.         QLineEdit.keyPressEvent(self, event)
  24.  
  25.     
  26.     def mouseReleaseEvent(self, event):
  27.         self.mouse_released.emit(event)
  28.         QLineEdit.mouseReleaseEvent(self, event)
  29.  
  30.     
  31.     def focusOutEvent(self, event):
  32.         self.focus_out.emit(event)
  33.         QLineEdit.focusOutEvent(self, event)
  34.  
  35.     
  36.     def dropEvent(self, ev):
  37.         if self.parent().help_state:
  38.             self.parent().normalize_state()
  39.         
  40.         return QLineEdit.dropEvent(self, ev)
  41.  
  42.     
  43.     def contextMenuEvent(self, ev):
  44.         if self.parent().help_state:
  45.             self.parent().normalize_state()
  46.         
  47.         return QLineEdit.contextMenuEvent(self, ev)
  48.  
  49.     
  50.     def paste(self, *args):
  51.         if self.parent().help_state:
  52.             self.parent().normalize_state()
  53.         
  54.         return QLineEdit.paste(self)
  55.  
  56.     paste = pyqtSlot()(paste)
  57.  
  58.  
  59. class SearchBox2(QComboBox):
  60.     INTERVAL = 1500
  61.     MAX_COUNT = 25
  62.     search = pyqtSignal(object)
  63.     
  64.     def __init__(self, parent = None):
  65.         QComboBox.__init__(self, parent)
  66.         self.normal_background = 'rgb(255, 255, 255, 0%)'
  67.         self.line_edit = SearchLineEdit(self)
  68.         self.setLineEdit(self.line_edit)
  69.         self.line_edit.key_pressed.connect(self.key_pressed, type = Qt.DirectConnection)
  70.         self.line_edit.mouse_released.connect(self.mouse_released, type = Qt.DirectConnection)
  71.         self.activated.connect(self.history_selected)
  72.         self.setEditable(True)
  73.         self.help_state = False
  74.         self.as_you_type = True
  75.         self.prev_search = ''
  76.         self.timer = QTimer()
  77.         self.timer.setSingleShot(True)
  78.         self.timer.timeout.connect(self.timer_event, type = Qt.QueuedConnection)
  79.         self.setInsertPolicy(self.NoInsert)
  80.         self.setMaxCount(self.MAX_COUNT)
  81.         self.setSizeAdjustPolicy(self.AdjustToMinimumContentsLengthWithIcon)
  82.         self.setMinimumContentsLength(25)
  83.         self._in_a_search = False
  84.  
  85.     
  86.     def initialize(self, opt_name, colorize = False, help_text = _('Search')):
  87.         self.as_you_type = config['search_as_you_type']
  88.         self.opt_name = opt_name
  89.         self.addItems(QStringList(list(set(config[opt_name]))))
  90.         self.help_text = help_text
  91.         self.colorize = colorize
  92.         self.clear_to_help()
  93.  
  94.     
  95.     def normalize_state(self):
  96.         if self.help_state:
  97.             self.setEditText('')
  98.             self.line_edit.setStyleSheet('QLineEdit { color: black; background-color: %s; }' % self.normal_background)
  99.             self.help_state = False
  100.         else:
  101.             self.line_edit.setStyleSheet('QLineEdit { color: black; background-color: %s; }' % self.normal_background)
  102.  
  103.     
  104.     def clear_to_help(self):
  105.         if self.help_state:
  106.             return None
  107.         self.help_state = True
  108.         self.search.emit('')
  109.         self._in_a_search = False
  110.         self.setEditText(self.help_text)
  111.         self.line_edit.home(False)
  112.         self.line_edit.setStyleSheet('QLineEdit { color: gray; background-color: %s; }' % self.normal_background)
  113.         self.emit(SIGNAL('cleared()'))
  114.  
  115.     
  116.     def text(self):
  117.         return self.currentText()
  118.  
  119.     
  120.     def clear(self):
  121.         self.clear_to_help()
  122.  
  123.     
  124.     def search_done(self, ok):
  125.         if not unicode(self.currentText()).strip():
  126.             return self.clear_to_help()
  127.         self._in_a_search = ok
  128.         col = unicode(self.currentText()).strip() if ok else 'rgb(255,0,0,20%)'
  129.         if not self.colorize:
  130.             col = self.normal_background
  131.         
  132.         self.line_edit.setStyleSheet('QLineEdit { color: black; background-color: %s; }' % col)
  133.  
  134.     
  135.     def key_pressed(self, event):
  136.         self.normalize_state()
  137.         if self._in_a_search:
  138.             self.emit(SIGNAL('changed()'))
  139.             self._in_a_search = False
  140.         
  141.         if event.key() in (Qt.Key_Return, Qt.Key_Enter):
  142.             self.do_search()
  143.         
  144.         self.timer.start(1500)
  145.  
  146.     
  147.     def mouse_released(self, event):
  148.         self.normalize_state()
  149.         if self.as_you_type:
  150.             self.timer.start(1500)
  151.         
  152.  
  153.     
  154.     def timer_event(self):
  155.         self.do_search()
  156.  
  157.     
  158.     def history_selected(self, text):
  159.         self.emit(SIGNAL('changed()'))
  160.         self.do_search()
  161.  
  162.     
  163.     def smart_text(self):
  164.         text = unicode(self.currentText()).strip()
  165.         if not text or text == self.help_text:
  166.             return ''
  167.         return text
  168.  
  169.     smart_text = property(smart_text)
  170.     
  171.     def do_search(self):
  172.         text = unicode(self.currentText()).strip()
  173.         if not text or text == self.help_text:
  174.             return self.clear()
  175.         self.help_state = False
  176.         self.prev_search = text
  177.         self.search.emit(text)
  178.         idx = self.findText(text, Qt.MatchFixedString)
  179.         self.block_signals(True)
  180.         if idx < 0:
  181.             self.insertItem(0, text)
  182.         else:
  183.             t = self.itemText(idx)
  184.             self.removeItem(idx)
  185.             self.insertItem(0, t)
  186.             self.setCurrentIndex(0)
  187.         self.block_signals(False)
  188.         config[self.opt_name] = [ unicode(self.itemText(i)) for i in range(self.count()) ]
  189.  
  190.     
  191.     def block_signals(self, yes):
  192.         self.blockSignals(yes)
  193.         self.line_edit.blockSignals(yes)
  194.  
  195.     
  196.     def search_from_tokens(self, tokens, all):
  197.         ans = []([ u'%s:%s' % x for x in tokens ])
  198.         self.set_search_string(ans)
  199.  
  200.     
  201.     def search_from_tags(self, tags, all):
  202.         joiner = None if all else ' or '
  203.         self.set_search_string(joiner.join(tags))
  204.  
  205.     
  206.     def set_search_string(self, txt):
  207.         if not txt:
  208.             self.clear_to_help()
  209.             return None
  210.         self.normalize_state()
  211.         self.setEditText(txt)
  212.         self.search.emit(txt)
  213.         self.line_edit.end(False)
  214.         self.initial_state = False
  215.  
  216.     
  217.     def search_as_you_type(self, enabled):
  218.         self.as_you_type = enabled
  219.  
  220.     
  221.     def in_a_search(self):
  222.         return self._in_a_search
  223.  
  224.  
  225.  
  226. class SavedSearchBox(QComboBox):
  227.     
  228.     def __init__(self, parent = None):
  229.         QComboBox.__init__(self, parent)
  230.         self.normal_background = 'rgb(255, 255, 255, 0%)'
  231.         self.line_edit = SearchLineEdit(self)
  232.         self.setLineEdit(self.line_edit)
  233.         self.line_edit.key_pressed.connect(self.key_pressed, type = Qt.DirectConnection)
  234.         self.line_edit.mouse_released.connect(self.mouse_released, type = Qt.DirectConnection)
  235.         self.line_edit.focus_out.connect(self.focus_out, type = Qt.DirectConnection)
  236.         self.activated[str].connect(self.saved_search_selected)
  237.         completer = QCompleter(self)
  238.         self.setCompleter(completer)
  239.         self.setEditable(True)
  240.         self.help_state = True
  241.         self.prev_search = ''
  242.         self.setInsertPolicy(self.NoInsert)
  243.         self.setSizeAdjustPolicy(self.AdjustToMinimumContentsLengthWithIcon)
  244.         self.setMinimumContentsLength(10)
  245.         self.tool_tip_text = self.toolTip()
  246.  
  247.     
  248.     def initialize(self, _search_box, colorize = False, help_text = _('Search')):
  249.         self.search_box = _search_box
  250.         self.help_text = help_text
  251.         self.colorize = colorize
  252.         self.clear_to_help()
  253.  
  254.     
  255.     def normalize_state(self):
  256.         self.setEditText('')
  257.         self.line_edit.setStyleSheet('QLineEdit { color: black; background-color: %s; }' % self.normal_background)
  258.         self.help_state = False
  259.  
  260.     
  261.     def clear_to_help(self):
  262.         self.setToolTip(self.tool_tip_text)
  263.         self.initialize_saved_search_names()
  264.         self.setEditText(self.help_text)
  265.         self.line_edit.home(False)
  266.         self.help_state = True
  267.         self.line_edit.setStyleSheet('QLineEdit { color: gray; background-color: %s; }' % self.normal_background)
  268.  
  269.     
  270.     def focus_out(self, event):
  271.         if self.currentText() == '':
  272.             self.clear_to_help()
  273.         
  274.  
  275.     
  276.     def key_pressed(self, event):
  277.         if self.help_state:
  278.             self.normalize_state()
  279.         
  280.  
  281.     
  282.     def mouse_released(self, event):
  283.         if self.help_state:
  284.             self.normalize_state()
  285.         
  286.  
  287.     
  288.     def saved_search_selected(self, qname):
  289.         qname = unicode(qname)
  290.         if qname is None or not qname.strip():
  291.             return None
  292.         self.normalize_state()
  293.         self.search_box.set_search_string(u'search:"%s"' % qname)
  294.         self.setEditText(qname)
  295.         self.setToolTip(saved_searches().lookup(qname))
  296.  
  297.     
  298.     def initialize_saved_search_names(self):
  299.         self.clear()
  300.         qnames = saved_searches().names()
  301.         self.addItems(qnames)
  302.         self.setCurrentIndex(-1)
  303.  
  304.     
  305.     def delete_search_button_clicked(self):
  306.         if not confirm('<p>' + _('The selected search will be <b>permanently deleted</b>. Are you sure?') + '</p>', 'saved_search_delete', self):
  307.             return None
  308.         idx = self.currentIndex
  309.         if idx < 0:
  310.             return None
  311.         ss = saved_searches().lookup(unicode(self.currentText()))
  312.         if ss is None:
  313.             return None
  314.         saved_searches().delete(unicode(self.currentText()))
  315.         self.clear_to_help()
  316.         self.search_box.clear_to_help()
  317.         self.emit(SIGNAL('changed()'))
  318.  
  319.     
  320.     def save_search_button_clicked(self):
  321.         name = unicode(self.currentText())
  322.         if self.help_state or not name.strip():
  323.             name = unicode(self.search_box.text()).replace('"', '')
  324.         
  325.         saved_searches().delete(name)
  326.         saved_searches().add(name, unicode(self.search_box.text()))
  327.         self.clear_to_help()
  328.         self.normalize_state()
  329.         self.setCurrentIndex(self.findText(name))
  330.         self.saved_search_selected(name)
  331.         self.emit(SIGNAL('changed()'))
  332.  
  333.     
  334.     def copy_search_button_clicked(self):
  335.         idx = self.currentIndex()
  336.         if idx < 0:
  337.             return None
  338.         self.search_box.set_search_string(saved_searches().lookup(unicode(self.currentText())))
  339.  
  340.  
  341.  
  342. class SearchBoxMixin(object):
  343.     
  344.     def __init__(self):
  345.         self.search.initialize('main_search_history', colorize = True, help_text = _('Search (For Advanced Search click the button to the left)'))
  346.         self.connect(self.search, SIGNAL('cleared()'), self.search_box_cleared)
  347.         self.connect(self.search, SIGNAL('changed()'), self.search_box_changed)
  348.         self.connect(self.clear_button, SIGNAL('clicked()'), self.search.clear)
  349.         QObject.connect(self.advanced_search_button, SIGNAL('clicked(bool)'), self.do_advanced_search)
  350.         self.search.clear()
  351.         self.search.setMaximumWidth(self.width() - 150)
  352.         self.action_focus_search = QAction(self)
  353.         shortcuts = QKeySequence.keyBindings(QKeySequence.Find)
  354.         shortcuts = list(shortcuts) + [
  355.             QKeySequence('/')]
  356.         self.action_focus_search.setShortcuts(shortcuts)
  357.         (self.action_focus_search.triggered.connect,)((lambda x: self.search.setFocus(Qt.OtherFocusReason)))
  358.         self.addAction(self.action_focus_search)
  359.         self.search.setStatusTip(re.sub('<\\w+>', ' ', unicode(self.search.toolTip())))
  360.         self.advanced_search_button.setStatusTip(self.advanced_search_button.toolTip())
  361.         self.clear_button.setStatusTip(self.clear_button.toolTip())
  362.  
  363.     
  364.     def search_box_cleared(self):
  365.         self.tags_view.clear()
  366.         self.saved_search.clear_to_help()
  367.         self.set_number_of_books_shown()
  368.  
  369.     
  370.     def search_box_changed(self):
  371.         self.tags_view.clear()
  372.  
  373.     
  374.     def do_advanced_search(self, *args):
  375.         d = SearchDialog(self)
  376.         if d.exec_() == QDialog.Accepted:
  377.             self.search.set_search_string(d.search_string())
  378.         
  379.  
  380.  
  381.  
  382. class SavedSearchBoxMixin(object):
  383.     
  384.     def __init__(self, db):
  385.         self.db = db
  386.         self.connect(self.saved_search, SIGNAL('changed()'), self.saved_searches_changed)
  387.         self.saved_searches_changed()
  388.         self.connect(self.clear_button, SIGNAL('clicked()'), self.saved_search.clear_to_help)
  389.         self.saved_search.initialize(self.search, colorize = True, help_text = _('Saved Searches'))
  390.         self.connect(self.save_search_button, SIGNAL('clicked()'), self.saved_search.save_search_button_clicked)
  391.         self.connect(self.delete_search_button, SIGNAL('clicked()'), self.saved_search.delete_search_button_clicked)
  392.         self.connect(self.copy_search_button, SIGNAL('clicked()'), self.saved_search.copy_search_button_clicked)
  393.         self.saved_search.setToolTip(_('Choose saved search or enter name for new saved search'))
  394.         self.saved_search.setStatusTip(self.saved_search.toolTip())
  395.         for x in ('copy', 'save', 'delete'):
  396.             b = getattr(self, x + '_search_button')
  397.             b.setStatusTip(b.toolTip())
  398.         
  399.  
  400.     
  401.     def set_database(self, db):
  402.         self.db = db
  403.         self.saved_searches_changed()
  404.  
  405.     
  406.     def saved_searches_changed(self):
  407.         p = saved_searches().names()
  408.         p.sort()
  409.         t = unicode(self.search_restriction.currentText())
  410.         self.search_restriction.clear()
  411.         self.search_restriction.addItem('')
  412.         self.tags_view.recount()
  413.         for s in p:
  414.             self.search_restriction.addItem(s)
  415.         
  416.         if t:
  417.             if t in p:
  418.                 self.search_restriction.setCurrentIndex(self.search_restriction.findText(t))
  419.             else:
  420.                 self.search_restriction.setCurrentIndex(0)
  421.                 self.apply_search_restriction('')
  422.         
  423.  
  424.     
  425.     def do_saved_search_edit(self, search):
  426.         d = SavedSearchEditor(self, search)
  427.         d.exec_()
  428.         if d.result() == d.Accepted:
  429.             self.saved_searches_changed()
  430.             self.saved_search.clear_to_help()
  431.         
  432.  
  433.  
  434.