Ejemplo n.º 1
0
    def __init__(self, title, html, parent=None, unique_name=None):
        QDialog.__init__(self, parent)
        self.l = l = QVBoxLayout()
        self.setLayout(l)

        self.tb = QTextBrowser(self)
        self.tb.setHtml('<pre style="font-family: monospace">%s</pre>' % html)
        l.addWidget(self.tb)

        self.bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
        self.bb.accepted.connect(self.accept)
        self.bb.rejected.connect(self.reject)
        self.copy_button = self.bb.addButton(
            _('Copy to clipboard'), QDialogButtonBox.ButtonRole.ActionRole)
        self.copy_button.setIcon(QIcon(I('edit-copy.png')))
        self.copy_button.clicked.connect(self.copy_to_clipboard)
        l.addWidget(self.bb)

        self.unique_name = unique_name or 'view-log-dialog'
        self.finished.connect(self.dialog_closing)
        self.resize(QSize(700, 500))
        geom = gprefs.get(self.unique_name, None)
        if geom is not None:
            QApplication.instance().safe_restore_geometry(self, geom)

        self.setModal(False)
        self.setWindowTitle(title)
        self.setWindowIcon(QIcon(I('debug.png')))
        self.show()
Ejemplo n.º 2
0
    def setup_ui(self):
        self.pb = pb = QProgressBar(self)
        pb.setTextVisible(True)
        pb.setSizePolicy(QSizePolicy.Policy.Expanding,
                         QSizePolicy.Policy.Fixed)
        pb.setRange(0, 0)
        self.w = w = QWidget(self)
        self.w.l = l = QVBoxLayout(w)
        l.addStretch(), l.addWidget(pb)
        self.w.la = la = QLabel(_('Checking external links, please wait...'))
        la.setStyleSheet('QLabel { font-size: 20px; font-weight: bold }')
        l.addWidget(la, 0, Qt.AlignmentFlag.AlignCenter), l.addStretch()

        self.l = l = QVBoxLayout(self)
        self.results = QTextBrowser(self)
        self.results.setOpenLinks(False)
        self.results.anchorClicked.connect(self.anchor_clicked)
        self.stack = s = QStackedWidget(self)
        s.addWidget(w), s.addWidget(self.results)
        l.addWidget(s)
        self.bh = h = QHBoxLayout()
        self.check_anchors = ca = QCheckBox(_('Check &anchors'))
        ca.setToolTip(
            _('Check HTML anchors in links (the part after the #).\n'
              ' This can be a little slow, since it requires downloading and parsing all the HTML pages.'
              ))
        ca.setChecked(tprefs.get('check_external_link_anchors', True))
        ca.stateChanged.connect(self.anchors_changed)
        h.addWidget(ca), h.addStretch(100), h.addWidget(self.bb)
        l.addLayout(h)
        self.bb.setStandardButtons(QDialogButtonBox.StandardButton.Close)
        self.rb = b = self.bb.addButton(_('&Refresh'),
                                        QDialogButtonBox.ButtonRole.ActionRole)
        b.setIcon(QIcon(I('view-refresh.png')))
        b.clicked.connect(self.refresh)
Ejemplo n.º 3
0
    def __init__(self, log, parent=None):
        QDialog.__init__(self, parent)
        self.log = log
        self.l = l = QVBoxLayout()
        self.setLayout(l)

        self.tb = QTextBrowser(self)
        l.addWidget(self.tb)

        self.bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
        l.addWidget(self.bb)
        self.copy_button = self.bb.addButton(
            _('Copy to clipboard'), QDialogButtonBox.ButtonRole.ActionRole)
        self.copy_button.clicked.connect(self.copy_to_clipboard)
        self.copy_button.setIcon(QIcon(I('edit-copy.png')))
        self.bb.rejected.connect(self.reject)
        self.bb.accepted.connect(self.accept)

        self.setWindowTitle(_('Download log'))
        self.setWindowIcon(QIcon(I('debug.png')))
        self.resize(QSize(800, 400))

        self.keep_updating = True
        self.last_html = None
        self.finished.connect(self.stop)
        QTimer.singleShot(100, self.update_log)

        self.show()
Ejemplo n.º 4
0
    def __init__(self, mi, parent=None):
        QTextBrowser.__init__(self, parent)
        series = ''
        fm = field_metadata
        if mi.series:
            series = _('{num} of {series}').format(
                num=mi.format_series_index(), series='<i>%s</i>' % mi.series)
        self.setHtml('''
<h3 style="text-align:center">{mb}</h3>
<p><b>{title}</b> - <i>{authors}</i><br></p>
<table>
<tr><td>{fm[timestamp][name]}:</td><td>{date}</td></tr>
<tr><td>{fm[pubdate][name]}:</td><td>{published}</td></tr>
<tr><td>{fm[formats][name]}:</td><td>{formats}</td></tr>
<tr><td>{fm[series][name]}:</td><td>{series}</td></tr>
</table>
        '''.format(mb=_('Target book'),
                   title=mi.title,
                   authors=authors_to_string(mi.authors),
                   date=format_date(mi.timestamp,
                                    tweaks['gui_timestamp_display_format']),
                   fm=fm,
                   published=(format_date(mi.pubdate,
                                          tweaks['gui_pubdate_display_format'])
                              if mi.pubdate else ''),
                   formats=', '.join(mi.formats or ()),
                   series=series))
Ejemplo n.º 5
0
 def __init__(self, parent):
     QTextBrowser.__init__(self, parent)
     self.setFrameShape(QFrame.Shape.NoFrame)
     self.setOpenLinks(False)
     self.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent, False)
     palette = self.palette()
     palette.setBrush(QPalette.ColorRole.Base, Qt.GlobalColor.transparent)
     self.setPalette(palette)
     self.setAcceptDrops(False)
Ejemplo n.º 6
0
def show_report(changed, title, report, parent, show_current_diff):
    report = format_report(title, report)
    d = QDialog(parent)
    d.setWindowTitle(_('Action report'))
    d.l = QVBoxLayout()
    d.setLayout(d.l)
    d.e = QTextBrowser(d)
    d.l.addWidget(d.e)
    d.e.setHtml(report)
    d.bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
    d.show_changes = False
    if changed:
        b = d.b = d.bb.addButton(_('See what &changed'), QDialogButtonBox.ButtonRole.AcceptRole)
        b.setIcon(QIcon(I('diff.png'))), b.setAutoDefault(False)
        connect_lambda(b.clicked, d, lambda d: setattr(d, 'show_changes', True))
    b = d.bb.addButton(_('&Copy to clipboard'), QDialogButtonBox.ButtonRole.ActionRole)
    b.setIcon(QIcon(I('edit-copy.png'))), b.setAutoDefault(False)

    def copy_report():
        text = re.sub(r'</.+?>', '\n', report)
        text = re.sub(r'<.+?>', '', text)
        cp = QApplication.instance().clipboard()
        cp.setText(text)

    b.clicked.connect(copy_report)
    d.bb.button(QDialogButtonBox.StandardButton.Close).setDefault(True)
    d.l.addWidget(d.bb)
    d.bb.rejected.connect(d.reject)
    d.bb.accepted.connect(d.accept)
    d.resize(600, 400)
    d.exec_()
    b.clicked.disconnect()
    if d.show_changes:
        show_current_diff(allow_revert=True)
Ejemplo n.º 7
0
 def setup_ui(self):
     self.setObjectName("Dialog")
     self.resize(497, 235)
     self.gridLayout = l = QGridLayout(self)
     l.setObjectName("gridLayout")
     self.icon_widget = Icon(self)
     l.addWidget(self.icon_widget)
     self.msg = la = QLabel(self)
     la.setWordWrap(True), la.setMinimumWidth(400)
     la.setOpenExternalLinks(True)
     la.setObjectName("msg")
     l.addWidget(la, 0, 1, 1, 1)
     self.det_msg = dm = QTextBrowser(self)
     dm.setReadOnly(True)
     dm.setObjectName("det_msg")
     l.addWidget(dm, 1, 0, 1, 2)
     self.bb = bb = QDialogButtonBox(self)
     bb.setStandardButtons(QDialogButtonBox.StandardButton.Ok)
     bb.setObjectName("bb")
     bb.accepted.connect(self.accept)
     bb.rejected.connect(self.reject)
     l.addWidget(bb, 3, 0, 1, 2)
     self.toggle_checkbox = tc = QCheckBox(self)
     tc.setObjectName("toggle_checkbox")
     l.addWidget(tc, 2, 0, 1, 2)
Ejemplo n.º 8
0
    def __init__(self, parent=None):
        QSplitter.__init__(self, parent)
        self.setChildrenCollapsible(False)

        self.items = i = QListWidget(self)
        i.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
        i.customContextMenuRequested.connect(self.context_menu)
        self.items.setSpacing(3)
        self.items.itemDoubleClicked.connect(self.current_item_activated)
        self.items.currentItemChanged.connect(self.current_item_changed)
        self.items.setSelectionMode(
            QAbstractItemView.SelectionMode.NoSelection)
        self.delegate = Delegate(self.items)
        self.items.setItemDelegate(self.delegate)
        self.addWidget(i)
        self.help = h = QTextBrowser(self)
        h.anchorClicked.connect(self.link_clicked)
        h.setOpenLinks(False)
        self.addWidget(h)
        self.setStretchFactor(0, 100)
        self.setStretchFactor(1, 50)
        self.clear_at_startup()

        state = tprefs.get('check-book-splitter-state', None)
        if state is not None:
            self.restoreState(state)
Ejemplo n.º 9
0
class LogViewer(QDialog):  # {{{
    def __init__(self, log, parent=None):
        QDialog.__init__(self, parent)
        self.log = log
        self.l = l = QVBoxLayout()
        self.setLayout(l)

        self.tb = QTextBrowser(self)
        l.addWidget(self.tb)

        self.bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Close)
        l.addWidget(self.bb)
        self.copy_button = self.bb.addButton(
            _('Copy to clipboard'), QDialogButtonBox.ButtonRole.ActionRole)
        self.copy_button.clicked.connect(self.copy_to_clipboard)
        self.copy_button.setIcon(QIcon(I('edit-copy.png')))
        self.bb.rejected.connect(self.reject)
        self.bb.accepted.connect(self.accept)

        self.setWindowTitle(_('Download log'))
        self.setWindowIcon(QIcon(I('debug.png')))
        self.resize(QSize(800, 400))

        self.keep_updating = True
        self.last_html = None
        self.finished.connect(self.stop)
        QTimer.singleShot(100, self.update_log)

        self.show()

    def copy_to_clipboard(self):
        QApplication.clipboard().setText(''.join(self.log.plain_text))

    def stop(self, *args):
        self.keep_updating = False

    def update_log(self):
        if not self.keep_updating:
            return
        html = self.log.html
        if html != self.last_html:
            self.last_html = html
            self.tb.setHtml('<pre style="font-family:monospace">%s</pre>' %
                            html)
        QTimer.singleShot(1000, self.update_log)
Ejemplo n.º 10
0
 def __init__(self, parent=None):
     QTextBrowser.__init__(self, parent)
     self.last_set_html = ''
     self.default_css = self.external_css = ''
     app = QApplication.instance()
     app.palette_changed.connect(self.palette_changed)
     self.palette_changed()
     font = self.font()
     f = QFontInfo(font)
     delta = tweaks['change_book_details_font_size_by'] + 1
     if delta:
         font.setPixelSize(f.pixelSize() + delta)
         self.setFont(font)
     self.setFrameShape(QFrame.Shape.NoFrame)
     self.setOpenLinks(False)
     self.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent, False)
     palette = self.palette()
     palette.setBrush(QPalette.ColorRole.Base, Qt.GlobalColor.transparent)
     self.setPalette(palette)
     self.setAcceptDrops(False)
     self.anchorClicked.connect(self.on_anchor_clicked)
Ejemplo n.º 11
0
 def loadResource(self, rtype, qurl):
     if qurl.isLocalFile():
         path = qurl.toLocalFile()
         try:
             with lopen(path, 'rb') as f:
                 data = f.read()
         except OSError:
             if path.rpartition('.')[-1].lower() in {'jpg', 'jpeg', 'gif', 'png', 'bmp', 'webp'}:
                 return QByteArray(bytearray.fromhex(
                     '89504e470d0a1a0a0000000d49484452'
                     '000000010000000108060000001f15c4'
                     '890000000a49444154789c6300010000'
                     '0500010d0a2db40000000049454e44ae'
                     '426082'))
         else:
             return QByteArray(data)
     else:
         return QTextBrowser.loadResource(self, rtype, qurl)
Ejemplo n.º 12
0
 def setup_ui(self):
     self.l = l = QVBoxLayout(self)
     if self.html_view:
         self.tb = w = QTextBrowser(self)
     else:
         self.log = w = QPlainTextEdit(self)
         w.setReadOnly(True), w.setLineWrapMode(QPlainTextEdit.LineWrapMode.NoWrap)
     l.addWidget(w)
     l.addWidget(self.bb)
     self.bb.clear(), self.bb.setStandardButtons(QDialogButtonBox.StandardButton.Close)
     self.copy_button = b = self.bb.addButton(_('&Copy to clipboard'), QDialogButtonBox.ButtonRole.ActionRole)
     b.setIcon(QIcon(I('edit-copy.png')))
     b.clicked.connect(self.copy_to_clipboard)
     self.next_pos = 0
     self.update()
     self.timer = QTimer(self)
     self.timer.timeout.connect(self.update)
     self.timer.start(1000)
     if not self.html_view:
         v = self.log.verticalScrollBar()
         v.setValue(v.maximum())
Ejemplo n.º 13
0
    def __init__(self, gui, existing_names, editing=None):
        QDialog.__init__(self, gui)

        self.gui = gui
        self.existing_names = existing_names

        if editing:
            self.setWindowTitle(_('Edit Virtual library'))
        else:
            self.setWindowTitle(_('Create Virtual library'))
        self.setWindowIcon(QIcon(I('lt.png')))

        gl = QGridLayout()
        self.setLayout(gl)
        self.la1 = la1 = QLabel(_('Virtual library &name:'))
        gl.addWidget(la1, 0, 0)
        self.vl_name = QComboBox()
        self.vl_name.setEditable(True)
        self.vl_name.lineEdit().setMaxLength(MAX_VIRTUAL_LIBRARY_NAME_LENGTH)
        la1.setBuddy(self.vl_name)
        gl.addWidget(self.vl_name, 0, 1)
        self.editing = editing

        self.saved_searches_label = sl = QTextBrowser(self)
        sl.viewport().setAutoFillBackground(False)
        gl.addWidget(sl, 2, 0, 1, 2)

        self.la2 = la2 = QLabel(_('&Search expression:'))
        gl.addWidget(la2, 1, 0)
        self.vl_text = QLineEdit()
        self.vl_text.textChanged.connect(self.search_text_changed)
        la2.setBuddy(self.vl_text)
        gl.addWidget(self.vl_text, 1, 1)
        # Trigger the textChanged signal to initialize the saved searches box
        self.vl_text.setText(' ')
        self.vl_text.setText(_build_full_search_string(self.gui))

        self.sl = sl = QLabel('<p>'+_('Create a Virtual library based on: ')+
            ('<a href="author.{0}">{0}</a>, '
            '<a href="tag.{1}">{1}</a>, '
            '<a href="publisher.{2}">{2}</a>, '
            '<a href="series.{3}">{3}</a>, '
            '<a href="search.{4}">{4}</a>.').format(_('Authors'), _('Tags'),
                                            _('Publishers'), ngettext('Series', 'Series', 2), _('Saved searches')))
        sl.setWordWrap(True)
        sl.setTextInteractionFlags(Qt.TextInteractionFlag.LinksAccessibleByMouse)
        sl.linkActivated.connect(self.link_activated)
        gl.addWidget(sl, 3, 0, 1, 2)
        gl.setRowStretch(3,10)

        self.hl = hl = QLabel(_('''
            <h2>Virtual libraries</h2>

            <p>With <i>Virtual libraries</i>, you can restrict calibre to only show
            you books that match a search. When a Virtual library is in effect, calibre
            behaves as though the library contains only the matched books. The Tag browser
            display only the tags/authors/series/etc. that belong to the matched books and any searches
            you do will only search within the books in the Virtual library. This
            is a good way to partition your large library into smaller and easier to work with subsets.</p>

            <p>For example you can use a Virtual library to only show you books with the tag <i>Unread</i>
            or only books by <i>My favorite author</i> or only books in a particular series.</p>

            <p>More information and examples are available in the
            <a href="%s">User Manual</a>.</p>
            ''') % localize_user_manual_link('https://manual.calibre-ebook.com/virtual_libraries.html'))
        hl.setWordWrap(True)
        hl.setOpenExternalLinks(True)
        hl.setFrameStyle(QFrame.Shape.StyledPanel)
        gl.addWidget(hl, 0, 3, 4, 1)

        bb = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
        bb.accepted.connect(self.accept)
        bb.rejected.connect(self.reject)
        gl.addWidget(bb, 4, 0, 1, 0)

        if editing:
            db = self.gui.current_db
            virt_libs = db.new_api.pref('virtual_libraries', {})
            for dex,vl in enumerate(sorted(virt_libs.keys(), key=sort_key)):
                self.vl_name.addItem(vl, virt_libs.get(vl, ''))
                if vl == editing:
                    self.vl_name.setCurrentIndex(dex)
                    self.original_index = dex
            self.original_search = virt_libs.get(editing, '')
            self.vl_text.setText(self.original_search)
            self.new_name = editing
            self.vl_name.currentIndexChanged[int].connect(self.name_index_changed)
            self.vl_name.lineEdit().textEdited.connect(self.name_text_edited)

        self.resize(self.sizeHint()+QSize(150, 25))
Ejemplo n.º 14
0
 def setHtml(self, html):
     self.last_set_html = html
     QTextBrowser.setHtml(self, html)
Ejemplo n.º 15
0
class CheckExternalLinks(Dialog):

    progress_made = pyqtSignal(object, object)

    def __init__(self, parent=None):
        Dialog.__init__(self, _('Check external links'),
                        'check-external-links-dialog', parent)
        self.progress_made.connect(self.on_progress_made,
                                   type=Qt.ConnectionType.QueuedConnection)

    def show(self):
        if self.rb.isEnabled():
            self.refresh()
        return Dialog.show(self)

    def refresh(self):
        self.stack.setCurrentIndex(0)
        self.rb.setEnabled(False)
        t = Thread(name='CheckLinksMaster', target=self.run)
        t.daemon = True
        t.start()

    def setup_ui(self):
        self.pb = pb = QProgressBar(self)
        pb.setTextVisible(True)
        pb.setSizePolicy(QSizePolicy.Policy.Expanding,
                         QSizePolicy.Policy.Fixed)
        pb.setRange(0, 0)
        self.w = w = QWidget(self)
        self.w.l = l = QVBoxLayout(w)
        l.addStretch(), l.addWidget(pb)
        self.w.la = la = QLabel(_('Checking external links, please wait...'))
        la.setStyleSheet('QLabel { font-size: 20px; font-weight: bold }')
        l.addWidget(la, 0, Qt.AlignmentFlag.AlignCenter), l.addStretch()

        self.l = l = QVBoxLayout(self)
        self.results = QTextBrowser(self)
        self.results.setOpenLinks(False)
        self.results.anchorClicked.connect(self.anchor_clicked)
        self.stack = s = QStackedWidget(self)
        s.addWidget(w), s.addWidget(self.results)
        l.addWidget(s)
        self.bh = h = QHBoxLayout()
        self.check_anchors = ca = QCheckBox(_('Check &anchors'))
        ca.setToolTip(
            _('Check HTML anchors in links (the part after the #).\n'
              ' This can be a little slow, since it requires downloading and parsing all the HTML pages.'
              ))
        ca.setChecked(tprefs.get('check_external_link_anchors', True))
        ca.stateChanged.connect(self.anchors_changed)
        h.addWidget(ca), h.addStretch(100), h.addWidget(self.bb)
        l.addLayout(h)
        self.bb.setStandardButtons(QDialogButtonBox.StandardButton.Close)
        self.rb = b = self.bb.addButton(_('&Refresh'),
                                        QDialogButtonBox.ButtonRole.ActionRole)
        b.setIcon(QIcon(I('view-refresh.png')))
        b.clicked.connect(self.refresh)

    def anchors_changed(self):
        tprefs.set('check_external_link_anchors',
                   self.check_anchors.isChecked())

    def sizeHint(self):
        ans = Dialog.sizeHint(self)
        ans.setHeight(600)
        ans.setWidth(max(ans.width(), 800))
        return ans

    def run(self):
        from calibre.ebooks.oeb.polish.check.links import check_external_links
        self.tb = None
        self.errors = []
        try:
            self.errors = check_external_links(
                current_container(),
                self.progress_made.emit,
                check_anchors=self.check_anchors.isChecked())
        except Exception:
            import traceback
            self.tb = traceback.format_exc()
        self.progress_made.emit(None, None)

    def on_progress_made(self, curr, total):
        if curr is None:
            self.results.setText('')
            self.stack.setCurrentIndex(1)
            self.fixed_errors = set()
            self.rb.setEnabled(True)
            if self.tb is not None:
                return error_dialog(
                    self,
                    _('Checking failed'),
                    _('There was an error while checking links, click "Show details" for more information'
                      ),
                    det_msg=self.tb,
                    show=True)
            if not self.errors:
                self.results.setText(_('No broken links found'))
            else:
                self.populate_results()
        else:
            self.pb.setMaximum(total), self.pb.setValue(curr)

    def populate_results(self, preserve_pos=False):
        num = len(self.errors) - len(self.fixed_errors)
        text = '<h3>%s</h3><ol>' % (ngettext(
            'Found a broken link', 'Found {} broken links', num).format(num))
        for i, (locations, err, url) in enumerate(self.errors):
            if i in self.fixed_errors:
                continue
            text += '<li><b>%s</b> \xa0<a href="err:%d">[%s]</a><br>%s<br><ul>' % (
                url, i, _('Fix this link'), err)
            for name, href, lnum, col in locations:
                text += '<li>{name} \xa0<a href="loc:{lnum},{name}">[{line}: {lnum}]</a></li>'.format(
                    name=name, lnum=lnum, line=_('line number'))
            text += '</ul></li><hr>'
        self.results.setHtml(text)

    def anchor_clicked(self, qurl):
        url = qurl.toString()
        if url.startswith('err:'):
            errnum = int(url[4:])
            err = self.errors[errnum]
            newurl, ok = QInputDialog.getText(self,
                                              _('Fix URL'),
                                              _('Enter the corrected URL:') +
                                              '\xa0' * 40,
                                              text=err[2])
            if not ok:
                return
            nmap = defaultdict(set)
            for name, href in {(l[0], l[1]) for l in err[0]}:
                nmap[name].add(href)

            for name, hrefs in iteritems(nmap):
                raw = oraw = get_data(name)
                for href in hrefs:
                    raw = raw.replace(href, newurl)
                if raw != oraw:
                    set_data(name, raw)
            self.fixed_errors.add(errnum)
            self.populate_results()
        elif url.startswith('loc:'):
            lnum, name = url[4:].partition(',')[::2]
            lnum = int(lnum or 1)
            editor = get_boss().edit_file(name)
            if lnum and editor is not None and editor.has_line_numbers:
                editor.current_line = lnum