1# -*- coding: utf-8 -*-
2
3
4__license__ = 'GPL 3'
5__copyright__ = '2011, John Schember <john@nachtimwald.com>'
6__docformat__ = 'restructuredtext en'
7
8from qt.core import Qt
9
10from calibre.gui2 import gprefs
11from calibre.gui2.convert.heuristics_ui import Ui_Form
12from calibre.gui2.convert import Widget
13from calibre.utils.localization import localize_user_manual_link
14from calibre.ebooks.conversion.config import OPTIONS
15
16
17class HeuristicsWidget(Widget, Ui_Form):
18
19    TITLE = _('Heuristic\nprocessing')
20    HELP  = _('Modify the document text and structure using common patterns.')
21    COMMIT_NAME = 'heuristics'
22    ICON = I('heuristics.png')
23
24    def __init__(self, parent, get_option, get_help, db=None, book_id=None):
25        Widget.__init__(self, parent, OPTIONS['pipe']['heuristics'])
26        self.db, self.book_id = db, book_id
27        self.rssb_defaults = ['', '<hr />', '∗ ∗ ∗', '• • •', '♦ ♦ ♦',
28                '† †', '‡ ‡ ‡', '∞ ∞ ∞', '¤ ¤ ¤', '§']
29        self.initialize_options(get_option, get_help, db, book_id)
30
31        self.load_histories()
32
33        self.opt_enable_heuristics.stateChanged.connect(self.enable_heuristics)
34        self.opt_unwrap_lines.stateChanged.connect(self.enable_unwrap)
35
36        self.enable_heuristics(self.opt_enable_heuristics.checkState())
37        try:
38            self.help_label.setText(self.help_label.text() % localize_user_manual_link(
39                'https://manual.calibre-ebook.com/conversion.html#heuristic-processing'))
40        except TypeError:
41            pass  # link already localized
42
43    def restore_defaults(self, get_option):
44        Widget.restore_defaults(self, get_option)
45
46        self.save_histories()
47        rssb_hist = gprefs['replace_scene_breaks_history']
48        for x in self.rssb_defaults:
49            if x in rssb_hist:
50                del rssb_hist[rssb_hist.index(x)]
51        gprefs['replace_scene_breaks_history'] = self.rssb_defaults + gprefs['replace_scene_breaks_history']
52        self.load_histories()
53
54    def commit_options(self, save_defaults=False):
55        self.save_histories()
56
57        return Widget.commit_options(self, save_defaults)
58
59    def break_cycles(self):
60        Widget.break_cycles(self)
61
62        try:
63            self.opt_enable_heuristics.stateChanged.disconnect()
64            self.opt_unwrap_lines.stateChanged.disconnect()
65        except:
66            pass
67
68    def set_value_handler(self, g, val):
69        if val is None and g is self.opt_html_unwrap_factor:
70            g.setValue(0.0)
71            return True
72        if not val and g is self.opt_replace_scene_breaks:
73            g.lineEdit().setText('')
74            return True
75
76    def load_histories(self):
77        val = str(self.opt_replace_scene_breaks.currentText())
78
79        self.opt_replace_scene_breaks.clear()
80        self.opt_replace_scene_breaks.lineEdit().setText('')
81
82        rssb_hist = gprefs.get('replace_scene_breaks_history', self.rssb_defaults)
83        if val in rssb_hist:
84            del rssb_hist[rssb_hist.index(val)]
85        rssb_hist.insert(0, val)
86        for v in rssb_hist:
87            # Ensure we don't have duplicate items.
88            if self.opt_replace_scene_breaks.findText(v) == -1:
89                self.opt_replace_scene_breaks.addItem(v)
90        self.opt_replace_scene_breaks.setCurrentIndex(0)
91
92    def save_histories(self):
93        rssb_history = []
94        history_pats = [str(self.opt_replace_scene_breaks.lineEdit().text())] + [str(self.opt_replace_scene_breaks.itemText(i))
95                                for i in range(self.opt_replace_scene_breaks.count())]
96        for p in history_pats[:10]:
97            # Ensure we don't have duplicate items.
98            if p not in rssb_history:
99                rssb_history.append(p)
100        gprefs['replace_scene_breaks_history'] = rssb_history
101
102    def enable_heuristics(self, state):
103        state = state == Qt.CheckState.Checked
104        self.heuristic_options.setEnabled(state)
105
106    def enable_unwrap(self, state):
107        if state == Qt.CheckState.Checked:
108            state = True
109        else:
110            state = False
111        self.opt_html_unwrap_factor.setEnabled(state)
112