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