def store_passwd(ok, during_shutdown): if ok == 'never': exclude = gprefs.get('never-store-password-for', {}) exclude[key] = True password_exclusions.set(key, True) elif ok and not during_shutdown: QApplication.instance().store_password(url, username, password)
def show_data(self, html): def color_to_string(col): ans = '#000000' if col.isValid(): col = col.toRgb() if col.isValid(): ans = unicode_type(col.name()) return ans fi = QFontInfo(QApplication.font(self.parent())) f = fi.pixelSize()+1+int(tweaks['change_book_details_font_size_by']) fam = unicode_type(fi.family()).strip().replace('"', '') if not fam: fam = 'sans-serif' c = color_to_string(QApplication.palette().color(QPalette.Normal, QPalette.WindowText)) templ = '''\ <html> <head> <style type="text/css"> body, td {background-color: transparent; font-family: "%s"; font-size: %dpx; color: %s } a { text-decoration: none; color: blue } div.description { margin-top: 0; padding-top: 0; text-indent: 0 } table { margin-bottom: 0; padding-bottom: 0; } </style> </head> <body> <div class="description"> %%s </div> </body> <html> '''%(fam, f, c) self.setHtml(templ%html)
def main(args=sys.argv): # Ensure viewer can continue to function if GUI is closed os.environ.pop('CALIBRE_WORKER_TEMP_DIR', None) reset_base_dir() parser = option_parser() opts, args = parser.parse_args(args) open_at = float(opts.open_at.replace(',', '.')) if opts.open_at else None override = 'calibre-ebook-viewer' if islinux else None app = Application(args, override_program_name=override, color_prefs=vprefs) app.load_builtin_fonts() app.setWindowIcon(QIcon(I('viewer.png'))) QApplication.setOrganizationName(ORG_NAME) QApplication.setApplicationName(APP_UID) main = EbookViewer(args[1] if len(args) > 1 else None, debug_javascript=opts.debug_javascript, open_at=open_at, start_in_fullscreen=opts.full_screen) app.installEventFilter(main) # This is needed for paged mode. Without it, the first document that is # loaded will have extra blank space at the bottom, as # turn_off_internal_scrollbars does not take effect for the first # rendered document main.view.load_path(P('viewer/blank.html', allow_user_override=False)) sys.excepthook = main.unhandled_exception main.show() if opts.raise_window: main.raise_() with main: return app.exec_() return 0
def render_html(self, ok): try: if not ok: return cwidth, cheight = self.page.mainFrame().contentsSize().width(), self.page.mainFrame().contentsSize().height() self.page.setViewportSize(QSize(cwidth, cheight)) factor = float(self.width)/cwidth if cwidth > self.width else 1 cutoff_height = int(self.height/factor)-3 image = QImage(self.page.viewportSize(), QImage.Format_ARGB32) image.setDotsPerMeterX(self.dpi*(100/2.54)) image.setDotsPerMeterY(self.dpi*(100/2.54)) painter = QPainter(image) self.page.mainFrame().render(painter) painter.end() cheight = image.height() cwidth = image.width() pos = 0 while pos < cheight: img = image.copy(0, pos, cwidth, min(cheight-pos, cutoff_height)) pos += cutoff_height-20 if cwidth > self.width: img = img.scaledToWidth(self.width, Qt.SmoothTransform) f = os.path.join(self.tdir, '%d.png'%pos) img.save(f) self.images.append((f, img.width(), img.height())) finally: QApplication.quit()
def copy_cover(self): idx = self.currentIndex() pmap = self.model().cover_pixmap(idx) if pmap is None and idx.row() == 0: pmap = self.model().cc if pmap is not None: QApplication.clipboard().setPixmap(pmap)
def add_image_diff(self, left_data, right_data): def load(data): p = QPixmap() p.loadFromData(bytes(data)) if data and p.isNull(): p = self.failed_img return p left_img, right_img = load(left_data), load(right_data) change = [] # Let any initial resizing of the window finish in case this is the # first diff, to avoid the expensive resize calculation later QApplication.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers) for v, img, size in ((self.left, left_img, len(left_data)), (self.right, right_img, len(right_data))): c = v.textCursor() c.movePosition(c.End) start = c.block().blockNumber() lines, w = self.get_lines_for_image(img, v) c.movePosition(c.StartOfBlock) if size > 0: c.beginEditBlock() c.insertText(_('Size: {0} Resolution: {1}x{2}').format(human_readable(size), img.width(), img.height())) for i in xrange(lines + 1): c.insertBlock() change.extend((start, c.block().blockNumber())) c.insertBlock() c.endEditBlock() v.images[start] = (img, w, lines) change.append('replace' if left_data and right_data else 'delete' if left_data else 'insert') self.left.changes.append((change[0], change[1], change[-1])) self.right.changes.append((change[2], change[3], change[-1])) QApplication.processEvents(QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers)
def ensure_app(headless=True): global _store_app with _ea_lock: if _store_app is None and QApplication.instance() is None: args = sys.argv[:1] if headless and (islinux or isbsd): args += ['-platformpluginpath', sys.extensions_location, '-platform', 'headless'] _store_app = QApplication(args) if headless and (islinux or isbsd): _store_app.headless = True import traceback # This is needed because as of PyQt 5.4 if sys.execpthook == # sys.__excepthook__ PyQt will abort the application on an # unhandled python exception in a slot or virtual method. Since ensure_app() # is used in worker processes for background work like rendering html # or running a headless browser, we circumvent this as I really # dont feel like going through all the code and making sure no # unhandled exceptions ever occur. All the actual GUI apps already # override sys.except_hook with a proper error handler. def eh(t, v, tb): try: traceback.print_exception(t, v, tb, file=sys.stderr) except: pass sys.excepthook = eh
def fix_errors(self, container, errors): with BusyCursor(): self.show_busy(_('Running fixers, please wait...')) QApplication.processEvents() changed = fix_errors(container, errors) self.run_checks(container) return changed
def __init__(self, title, html, parent=None): ''' :param title: Caption for window title :param html: HTML string log/report ''' QDialog.__init__(self, parent) self.l = l = QVBoxLayout() self.setLayout(l) self.tb = QTextBrowser(self) QApplication.setOverrideCursor(Qt.WaitCursor) # Rather than formatting the text in <pre> blocks like the calibre # ViewLog does, instead just format it inside divs to keep style formatting html = html.replace('\t',' ')#.replace('\n', '<br/>') html = html.replace('> ','> ') self.tb.setHtml('<div>{0}</div>'.format(html)) QApplication.restoreOverrideCursor() l.addWidget(self.tb) self.bb = QDialogButtonBox(QDialogButtonBox.Ok) self.bb.accepted.connect(self.accept) self.bb.rejected.connect(self.reject) self.copy_button = self.bb.addButton(_('Copy to clipboard'), self.bb.ActionRole) self.copy_button.setIcon(QIcon(I('edit-copy.png'))) self.copy_button.clicked.connect(self.copy_to_clipboard) l.addWidget(self.bb) self.setModal(False) self.resize(QSize(700, 500)) self.setWindowTitle(title) self.setWindowIcon(QIcon(I('dialog_information.png'))) self.show()
def do_search(self): query = unicode(self.search_text.text()) if not query: d = error_dialog(self.gui, _('Match books'), _('You must enter a search expression into the search box')) d.exec_() return try: self.search_button.setEnabled(False) QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) books = self.library_db.data.search(query, return_matches=True) self.books_table.setRowCount(len(books)) self.books_table.setSortingEnabled(False) for row, b in enumerate(books): mi = self.library_db.get_metadata(b, index_is_id=True, get_user_categories=False) a = TableItem(mi.title, mi.title_sort) a.setData(Qt.UserRole, b) self.books_table.setItem(row, 0, a) a = TableItem(' & '.join(mi.authors), mi.author_sort) self.books_table.setItem(row, 1, a) series = mi.format_field('series')[1] if series is None: series = '' a = TableItem(series, mi.series, mi.series_index) self.books_table.setItem(row, 2, a) self.books_table.setRowHeight(row, self.books_table_row_height) self.books_table.setSortingEnabled(True) finally: self.search_button.setEnabled(True) QApplication.restoreOverrideCursor()
def eventFilter(self, obj, ev): if ev.type() == ev.MouseMove: if self.cursor_hidden: self.cursor_hidden = False QApplication.instance().restoreOverrideCursor() self.hide_cursor_timer.start() return False
def copy_to_clipboard(self, *args): QApplication.clipboard().setText( 'calibre, version %s\n%s: %s\n\n%s' % (__version__, unicode_type(self.windowTitle()), unicode_type(self.msg_label.text()), unicode_type(self.det_msg.toPlainText()))) self.copy_button.setText(_('Copied'))
def test(scale=0.25): from PyQt5.Qt import QLabel, QApplication, QPixmap, QMainWindow, QWidget, QScrollArea, QGridLayout app = QApplication([]) mi = Metadata('xxx', ['Kovid Goyal', 'John & Doe', 'Author']) mi.series = 'A series of styles' m = QMainWindow() sa = QScrollArea(m) w = QWidget(m) sa.setWidget(w) l = QGridLayout(w) w.setLayout(l), l.setSpacing(30) labels = [] for r, color in enumerate(sorted(default_color_themes)): for c, style in enumerate(sorted(all_styles())): mi.series_index = c + 1 mi.title = 'An algorithmic cover [%s]' % color prefs = override_prefs(cprefs, override_color_theme=color, override_style=style) scale_cover(prefs, scale) img = generate_cover(mi, prefs=prefs, as_qimage=True) la = QLabel() la.setPixmap(QPixmap.fromImage(img)) l.addWidget(la, r, c) labels.append(la) m.setCentralWidget(sa) w.resize(w.sizeHint()) m.show() app.exec_()
def launch_editor(path_to_edit, path_is_raw=False, syntax='html', callback=None): from calibre.gui2.tweak_book import dictionaries from calibre.gui2.tweak_book.main import option_parser from calibre.gui2.tweak_book.ui import Main from calibre.gui2.tweak_book.editor.syntax.html import refresh_spell_check_status dictionaries.initialize() refresh_spell_check_status() opts = option_parser().parse_args([]) app = QApplication([]) # Create the actions that are placed into the editors toolbars main = Main(opts) # noqa if path_is_raw: raw = path_to_edit else: with open(path_to_edit, 'rb') as f: raw = f.read().decode('utf-8') ext = path_to_edit.rpartition('.')[-1].lower() if ext in ('html', 'htm', 'xhtml', 'xhtm'): syntax = 'html' elif ext in ('css',): syntax = 'css' t = Editor(syntax) t.data = raw if callback is not None: callback(t) t.show() app.exec_()
def formatValueLike(self, type_, v, upper): res = "IS NULL" if type_ == "bool": s = str(v[0]).upper() if s == str(QApplication.tr("Sí")[0]).upper(): res = "=1" elif str(QApplication.tr("No")[0]).upper(): res = "=0" elif type_ == "date": util = FLUtil() res = "LIKE '%%" + util.dateDMAtoAMD(str(v)) + "'" elif type_ == "time": t = v.toTime() res = "LIKE '" + t.toString(QtCore.Qt.ISODate) + "%%'" else: res = str(v) if upper: res = "%s" % res.upper() res = "LIKE '" + res + "%%'" return res
def finalize_apply(self): db = self.gui.current_db db.commit() if self.apply_pd is not None: self.apply_pd.hide() if self.apply_failures: msg = [] for i, tb in self.apply_failures: title = db.title(i, index_is_id=True) authors = db.authors(i, index_is_id=True) if authors: authors = [x.replace('|', ',') for x in authors.split(',')] title += ' - ' + authors_to_string(authors) msg.append(title+'\n\n'+tb+'\n'+('*'*80)) error_dialog(self.gui, _('Some failures'), _('Failed to apply updated metadata for some books' ' in your library. Click "Show Details" to see ' 'details.'), det_msg='\n\n'.join(msg), show=True) changed_books = len(self.applied_ids or ()) self.refresh_gui(self.applied_ids) self.apply_id_map = [] self.apply_pd = None try: if callable(self.apply_callback): self.apply_callback(list(self.applied_ids)) finally: self.apply_callback = None if changed_books: QApplication.alert(self.gui, 2000)
def copy_to_clipboard(self, *args): QApplication.clipboard().setText( '%s, version %s\n%s: %s\n\n%s' % (appname, str_version, self.windowTitle(), self.msg.text(), self.det_msg.toPlainText())) if hasattr(self, 'ctc_button'): self.ctc_button.setText(_('Copied'))
def copy_url(window, *args, **kwargs): if window.current_tab is not None: qurl = window.current_tab.url() if not qurl.isEmpty(): QApplication.clipboard().setText(qurl.toString()) window.show_status_message(_('Copied: ') + qurl.toString(), 2, 'success') window.statusBar() return True
def no_replace(prefix=''): QApplication.restoreOverrideCursor() if prefix: prefix += ' ' error_dialog( gui_parent, _('Cannot replace'), prefix + _( 'You must first click Find, before trying to replace'), show=True) return False
def handle_tap_hold(self, action, tp): etype = {'start':QEvent.MouseButtonPress, 'update':QEvent.MouseMove, 'end':QEvent.MouseButtonRelease}[action] ev = QMouseEvent(etype, self.current_position(tp), tp.current_screen_position.toPoint(), Qt.LeftButton, Qt.LeftButton, Qt.NoModifier) QApplication.sendEvent(self.parent(), ev) if action == 'end': ev = QContextMenuEvent(QContextMenuEvent.Other, self.current_position(tp), tp.current_screen_position.toPoint()) # We have to use post event otherwise the popup remains an alien widget and does not receive events QApplication.postEvent(self.parent(), ev)
def copy_to_clipboard(self, *args): QApplication.clipboard().setText( 'calibre, version %s\n%s: %s\n\n%s' % (__version__, unicode(self.windowTitle()), unicode(self.msg.text()), unicode(self.det_msg.toPlainText()))) if hasattr(self, 'ctc_button'): self.ctc_button.setText(_('Copied'))
def copy_to_clipboard(self): items = [] for item in (self.items.item(i) for i in xrange(self.items.count())): msg = unicode(item.text()) msg = prefix_for_level(item.data(Qt.UserRole).level) + msg items.append(msg) if items: QApplication.clipboard().setText('\n'.join(items))
def copy(self): # Workaround Qt replacing nbsp with normal spaces on copy c = self.textCursor() if not c.hasSelection(): return md = QMimeData() md.setText(self.selected_text) QApplication.clipboard().setMimeData(md)
def show_splash_screen(self): self.splash_pixmap = QPixmap() self.splash_pixmap.load(I('library.png')) self.splash_screen = QSplashScreen(self.splash_pixmap) self.splash_screen.showMessage(_('Starting %s: Loading books...') % __appname__) self.splash_screen.show() QApplication.instance().processEvents()
def main(): app = QApplication(sys.argv) ui = MainWindow() ui.setupUi() ui.show() sys.exit(app.exec_())
def render_html(mi, css, vertical, widget, all_fields=False, render_data_func=None): # {{{ table, comment_fields = (render_data_func or render_data)( mi, all_fields=all_fields, use_roman_numbers=config["use_roman_numerals_for_series_number"] ) def color_to_string(col): ans = "#000000" if col.isValid(): col = col.toRgb() if col.isValid(): ans = unicode(col.name()) return ans fi = QFontInfo(QApplication.font(widget)) f = fi.pixelSize() + 1 + int(tweaks["change_book_details_font_size_by"]) fam = unicode(fi.family()).strip().replace('"', "") if not fam: fam = "sans-serif" c = color_to_string(QApplication.palette().color(QPalette.Normal, QPalette.WindowText)) templ = u"""\ <html> <head> <style type="text/css"> body, td { background-color: transparent; font-size: %dpx; font-family: "%s",sans-serif; color: %s } </style> <style type="text/css"> %s </style> </head> <body> %%s </body> <html> """ % ( f, fam, c, css, ) comments = u"" if comment_fields: comments = "\n".join(u"<div>%s</div>" % x for x in comment_fields) right_pane = u'<div id="comments" class="comments">%s</div>' % comments if vertical: ans = templ % (table + right_pane) else: ans = templ % ( u'<table><tr><td valign="top" ' 'style="padding-right:2em; width:40%%">%s</td><td valign="top">%s</td></tr></table>' % (table, right_pane) ) return ans
def start(self): self.setGeometry(0, 0, self.parent().width(), self.parent().height()) self.setVisible(True) # Prevent any actions from being triggerred by key presses self.parent().setEnabled(False) self.raise_() self.setFocus(Qt.OtherFocusReason) self.pi.startAnimation() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
def no_match(): QApplication.restoreOverrideCursor() msg = '<p>' + _('No matches were found for %s') % ('<pre style="font-style:italic">' + prepare_string_for_xml(errfind) + '</pre>') if not wrap: msg += '<p>' + _('You have turned off search wrapping, so all text might not have been searched.' ' Try the search again, with wrapping enabled. Wrapping is enabled via the' ' "Wrap" checkbox at the bottom of the search panel.') return error_dialog( gui_parent, _('Not found'), msg, show=True)
def run_wizard(self, *args): if self.confirm_quit(): self.run_wizard_b4_shutdown = True self.restart_after_quit = True try: self.shutdown(write_settings=False) except: pass QApplication.instance().quit()
def stop(self): QApplication.restoreOverrideCursor() self.pi.stopAnimation() self.setVisible(False) self.parent().setEnabled(True) # The following line is needed on OS X, because of this bug: # https://bugreports.qt-project.org/browse/QTBUG-34371 it causes # keyboard events to no longer work self.parent().setFocus(Qt.OtherFocusReason)
def updateEditorGeometry(self, editor, option, index): if editor is None: return fm = editor.fontMetrics() # get the original size of the edit widget opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) opt.showDecorationSelected = True opt.decorationSize = QSize(0, 0) # We want the editor to cover the decoration style = QApplication.style() initial_geometry = style.subElementRect(style.SE_ItemViewItemText, opt, None) orig_width = initial_geometry.width() # Compute the required width: the width that can show all of the current value if hasattr(self, 'get_required_width'): new_width = self.get_required_width(editor, style, fm) else: # The line edit box seems to extend by the space consumed by an 'M'. # So add that to the text text = self.displayText(index.data(Qt.DisplayRole), QLocale()) + u'M' srect = style.itemTextRect(fm, editor.geometry(), Qt.AlignLeft, False, text) new_width = srect.width() # Now get the size of the combo/spinner arrows and add them to the needed width if isinstance(editor, (QComboBox, QDateTimeEdit)): r = style.subControlRect(QStyle.CC_ComboBox, QStyleOptionComboBox(), QStyle.SC_ComboBoxArrow, editor) new_width += r.width() elif isinstance(editor, (QSpinBox, QDoubleSpinBox)): r = style.subControlRect(QStyle.CC_SpinBox, QStyleOptionSpinBox(), QStyle.SC_SpinBoxUp, editor) new_width += r.width() # Compute the maximum we can show if we consume the entire viewport max_width = (self.table_widget.horizontalScrollBar().geometry().width() - self.table_widget.verticalHeader().width()) # What we have to display might not fit. If so, adjust down new_width = new_width if new_width < max_width else max_width # See if we need to change the editor's geometry if new_width <= orig_width: delta_x = 0 delta_width = 0 else: # Compute the space available from the left edge of the widget to # the right edge of the displayed table (the viewport) and the left # edge of the widget to the left edge of the viewport. These are # used to position the edit box space_left = initial_geometry.x() space_right = max_width - space_left if editor.layoutDirection() == Qt.RightToLeft: # If language is RtL, align to the cell's right edge if possible cw = initial_geometry.width() consume_on_left = min(space_left, new_width - cw) consume_on_right = max(0, new_width - (consume_on_left + cw)) delta_x = -consume_on_left delta_width = consume_on_right else: # If language is LtR, align to the left if possible consume_on_right = min(space_right, new_width) consume_on_left = max(0, new_width - consume_on_right) delta_x = -consume_on_left delta_width = consume_on_right - initial_geometry.width() initial_geometry.adjust(delta_x, 0, delta_width, 0) editor.setGeometry(initial_geometry)
SHOULDER_RIGHT = "RightShoulder" SHOULDER_LEFT = "LeftShoulder" ARM_RIGHT = "RightArm" ARM_LEFT = "LeftArm" HAND_RIGHT = "RightHand" HAND_LEFT = "LeftHand" HIPS = "Hips" LEG_LEFT = "LeftLeg" LEG_RIGHT = "RightLeg" FOOT_RIGHT = "RightFoot" FOOT_LEFT = "LeftFoot" TORSO_CHILDREN = [NECK, SHOULDER_RIGHT, SHOULDER_LEFT, HIPS] HIPS_CHILDREN = [LEG_RIGHT, LEG_LEFT] app = QApplication(sys.argv) class Test(unittest.TestCase): def setUp(self): self.dataNavigator = XMCDDataNavigator() self.spyDataSelectionChanged = QSignalSpy( self.dataNavigator.dataSelectionChanged) def tearDown(self): pass def createNodesWithParents(self): self.torso = DataNode(TORSO) self.neck = DataNode(NECK, self.torso) self.head = DataNode(HEAD, self.neck)
def sizeHint(self): desktop = QApplication.instance().desktop() h = desktop.availableGeometry(self).height() return QSize(900, h - 75)
def hide_cursor(self): if get_session_pref('auto_hide_mouse', True): self.cursor_hidden = True QApplication.instance().setOverrideCursor( Qt.CursorShape.BlankCursor)
def copy_to_clipboard(self): QApplication.clipboard().setText(self.as_text)
def auto_height(widget): # On some broken systems, availableGeometry() returns tiny values, we need # a value of at least 1000 for 200 DPI systems. return max(1000, QApplication.instance().desktop().availableGeometry(widget).height()) / 5.0
def create_downloads(*args): di = FakeDownloadItem() app = QApplication.instance() app.downloads.download_created(di) app.downloads.download_created(FakeDownloadItem(size=100 * 1024))
def evaljs_callback(self, result): self.evaljs_result = result QApplication.instance().exit(0)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'ipetrash' from PyQt5.Qt import QApplication, QLabel, QVBoxLayout, QWidget app = QApplication([]) label_1 = QLabel() label_1.setText('Hello World!!!') label_1.setStyleSheet(""" font: bold italic; color: green; background-color: black; """) label_2 = QLabel() label_2.setText('nothing...') layout = QVBoxLayout() layout.addWidget(label_1) layout.addWidget(label_2) mw = QWidget() mw.setLayout(layout) mw.show() app.exec()
def __init__(self, field_metadata, parent=None, revert_tooltip=None, datetime_fmt='MMMM yyyy', blank_as_equal=True, fields=('title', 'authors', 'series', 'tags', 'rating', 'publisher', 'pubdate', 'identifiers', 'languages', 'comments', 'cover'), db=None): QWidget.__init__(self, parent) self.l = l = QGridLayout() l.setContentsMargins(0, 0, 0, 0) self.setLayout(l) revert_tooltip = revert_tooltip or _('Revert %s') self.current_mi = None self.changed_font = QFont(QApplication.font()) self.changed_font.setBold(True) self.changed_font.setItalic(True) self.blank_as_equal = blank_as_equal self.widgets = OrderedDict() row = 0 for field in fields: m = field_metadata[field] dt = m['datatype'] extra = None if 'series' in {field, dt}: cls = SeriesEdit elif field == 'identifiers': cls = IdentifiersEdit elif field == 'languages': cls = LanguagesEdit elif 'comments' in {field, dt}: cls = CommentsEdit elif 'rating' in {field, dt}: cls = RatingsEdit elif dt == 'datetime': extra = datetime_fmt cls = DateEdit elif field == 'cover': cls = CoverView elif dt in {'text', 'enum'}: cls = LineEdit else: continue neww = cls(field, True, self, m, extra) neww.changed.connect(partial(self.changed, field)) if isinstance(neww, EditWithComplete): try: neww.update_items_cache(db.new_api.all_field_names(field)) except ValueError: pass # A one-one field like title if isinstance(neww, SeriesEdit): neww.set_db(db.new_api) oldw = cls(field, False, self, m, extra) newl = QLabel('&%s:' % m['name']) newl.setBuddy(neww) button = RightClickButton(self) button.setIcon(QIcon(I('back.png'))) button.clicked.connect(partial(self.revert, field)) button.setToolTip(revert_tooltip % m['name']) if field == 'identifiers': button.m = m = QMenu(button) button.setMenu(m) button.setPopupMode(QToolButton.DelayedPopup) m.addAction(button.toolTip()).triggered.connect(button.click) m.actions()[0].setIcon(button.icon()) m.addAction(_('Merge identifiers')).triggered.connect( self.merge_identifiers) m.actions()[1].setIcon(QIcon(I('merge.png'))) elif field == 'tags': button.m = m = QMenu(button) button.setMenu(m) button.setPopupMode(QToolButton.DelayedPopup) m.addAction(button.toolTip()).triggered.connect(button.click) m.actions()[0].setIcon(button.icon()) m.addAction(_('Merge tags')).triggered.connect(self.merge_tags) m.actions()[1].setIcon(QIcon(I('merge.png'))) self.widgets[field] = Widgets(neww, oldw, newl, button) for i, w in enumerate((newl, neww, button, oldw)): c = i if i < 2 else i + 1 if w is oldw: c += 1 l.addWidget(w, row, c) row += 1 self.sep = f = QFrame(self) f.setFrameShape(f.VLine) l.addWidget(f, 0, 2, row, 1) self.sep2 = f = QFrame(self) f.setFrameShape(f.VLine) l.addWidget(f, 0, 4, row, 1) if 'comments' in self.widgets and not gprefs.get( 'diff_widget_show_comments_controls', True): self.widgets['comments'].new.hide_toolbars()
def __init__(self, parent, view, row, link_delegate): QDialog.__init__(self, parent) self.normal_brush = QBrush(Qt.GlobalColor.white) self.marked_brush = QBrush(Qt.GlobalColor.lightGray) self.marked = None self.gui = parent self.splitter = QSplitter(self) self._l = l = QVBoxLayout(self) self.setLayout(l) l.addWidget(self.splitter) self.cover = Cover(self, show_size=gprefs['bd_overlay_cover_size']) self.cover.resizeEvent = self.cover_view_resized self.cover.cover_changed.connect(self.cover_changed) self.cover.open_with_requested.connect(self.open_with) self.cover.choose_open_with_requested.connect(self.choose_open_with) self.cover_pixmap = None self.cover.sizeHint = self.details_size_hint self.splitter.addWidget(self.cover) self.details = Details(parent.book_details.book_info, self) self.details.anchor_clicked.connect(self.on_link_clicked) self.link_delegate = link_delegate self.details.setAttribute(Qt.WidgetAttribute.WA_OpaquePaintEvent, False) palette = self.details.palette() self.details.setAcceptDrops(False) palette.setBrush(QPalette.ColorRole.Base, Qt.GlobalColor.transparent) self.details.setPalette(palette) self.c = QWidget(self) self.c.l = l2 = QGridLayout(self.c) l2.setContentsMargins(0, 0, 0, 0) self.c.setLayout(l2) l2.addWidget(self.details, 0, 0, 1, -1) self.splitter.addWidget(self.c) self.fit_cover = QCheckBox(_('Fit &cover within view'), self) self.fit_cover.setChecked( gprefs.get('book_info_dialog_fit_cover', True)) self.hl = hl = QHBoxLayout() hl.setContentsMargins(0, 0, 0, 0) l2.addLayout(hl, l2.rowCount(), 0, 1, -1) hl.addWidget(self.fit_cover), hl.addStretch() self.clabel = QLabel( '<div style="text-align: right"><a href="calibre:conf" title="{}" style="text-decoration: none">{}</a>' .format(_('Configure this view'), _('Configure'))) self.clabel.linkActivated.connect(self.configure) hl.addWidget(self.clabel) self.previous_button = QPushButton(QIcon(I('previous.png')), _('&Previous'), self) self.previous_button.clicked.connect(self.previous) l2.addWidget(self.previous_button, l2.rowCount(), 0) self.next_button = QPushButton(QIcon(I('next.png')), _('&Next'), self) self.next_button.clicked.connect(self.next) l2.addWidget(self.next_button, l2.rowCount() - 1, 1) self.view = view self.path_to_book = None self.current_row = None self.refresh(row) self.view.model().new_bookdisplay_data.connect(self.slave) self.fit_cover.stateChanged.connect(self.toggle_cover_fit) self.ns = QShortcut(QKeySequence('Alt+Right'), self) self.ns.activated.connect(self.next) self.ps = QShortcut(QKeySequence('Alt+Left'), self) self.ps.activated.connect(self.previous) self.next_button.setToolTip( _('Next [%s]') % unicode_type(self.ns.key().toString( QKeySequence.SequenceFormat.NativeText))) self.previous_button.setToolTip( _('Previous [%s]') % unicode_type(self.ps.key().toString( QKeySequence.SequenceFormat.NativeText))) geom = QCoreApplication.instance().desktop().availableGeometry(self) screen_height = geom.height() - 100 screen_width = geom.width() - 100 self.resize(max(int(screen_width / 2), 700), screen_height) saved_layout = gprefs.get('book_info_dialog_layout', None) if saved_layout is not None: try: QApplication.instance().safe_restore_geometry( self, saved_layout[0]) self.splitter.restoreState(saved_layout[1]) except Exception: pass from calibre.gui2.ui import get_gui ema = get_gui().iactions['Edit Metadata'].menuless_qaction a = self.ema = QAction('edit metadata', self) a.setShortcut(ema.shortcut()) self.addAction(a) a.triggered.connect(self.edit_metadata)
def __init__(self, ids, get_metadata, field_metadata, parent=None, window_title=None, reject_button_tooltip=None, accept_all_tooltip=None, reject_all_tooltip=None, revert_tooltip=None, intro_msg=None, action_button=None, **kwargs): QDialog.__init__(self, parent) self.l = l = QVBoxLayout() self.setLayout(l) self.setWindowIcon(QIcon(I('auto_author_sort.png'))) self.get_metadata = get_metadata self.ids = list(ids) self.total = len(self.ids) self.accepted = OrderedDict() self.rejected_ids = set() self.window_title = window_title or _('Compare metadata') if intro_msg: self.la = la = QLabel(intro_msg) la.setWordWrap(True) l.addWidget(la) self.compare_widget = CompareSingle(field_metadata, parent=parent, revert_tooltip=revert_tooltip, **kwargs) self.sa = sa = QScrollArea() l.addWidget(sa) sa.setWidget(self.compare_widget) sa.setWidgetResizable(True) self.bb = bb = QDialogButtonBox(QDialogButtonBox.Cancel) bb.button(bb.Cancel).setAutoDefault(False) bb.rejected.connect(self.reject) if self.total > 1: self.aarb = b = bb.addButton(_('&Accept all remaining'), bb.YesRole) b.setIcon(QIcon(I('ok.png'))), b.setAutoDefault(False) if accept_all_tooltip: b.setToolTip(accept_all_tooltip) b.clicked.connect(self.accept_all_remaining) self.rarb = b = bb.addButton(_('Re&ject all remaining'), bb.NoRole) b.setIcon(QIcon(I('minus.png'))), b.setAutoDefault(False) if reject_all_tooltip: b.setToolTip(reject_all_tooltip) b.clicked.connect(self.reject_all_remaining) self.sb = b = bb.addButton(_('&Reject'), bb.ActionRole) b.clicked.connect(partial(self.next_item, False)) b.setIcon(QIcon(I('minus.png'))), b.setAutoDefault(False) if reject_button_tooltip: b.setToolTip(reject_button_tooltip) self.next_action = ac = QAction(self) ac.setShortcut(QKeySequence(Qt.ALT | Qt.Key_Right)) self.addAction(ac) if action_button is not None: self.acb = b = bb.addButton(action_button[0], bb.ActionRole) b.setIcon(QIcon(action_button[1])) self.action_button_action = action_button[2] b.clicked.connect(self.action_button_clicked) self.nb = b = bb.addButton( _('&Next') if self.total > 1 else _('&OK'), bb.ActionRole) if self.total > 1: b.setToolTip( _('Move to next [%s]') % self.next_action.shortcut().toString(QKeySequence.NativeText)) self.next_action.triggered.connect(b.click) b.setIcon(QIcon(I('forward.png' if self.total > 1 else 'ok.png'))) b.clicked.connect(partial(self.next_item, True)) b.setDefault(True), b.setAutoDefault(True) self.bbh = h = QHBoxLayout() h.setContentsMargins(0, 0, 0, 0) l.addLayout(h) self.markq = m = QCheckBox(_('&Mark rejected books')) m.setChecked(gprefs['metadata_diff_mark_rejected']) m.stateChanged[int].connect( lambda: gprefs.set('metadata_diff_mark_rejected', m.isChecked())) m.setToolTip( _('Mark rejected books in the book list after this dialog is closed' )) h.addWidget(m), h.addWidget(bb) self.next_item(True) desktop = QApplication.instance().desktop() geom = desktop.availableGeometry(parent or self) width = max(700, min(950, geom.width() - 50)) height = max(650, min(1000, geom.height() - 100)) self.resize(QSize(width, height)) geom = gprefs.get('diff_dialog_geom', None) if geom is not None: self.restoreGeometry(geom) b.setFocus(Qt.OtherFocusReason)
def data(self, index, role): row, col = index.row(), index.column() if row >= len(self.matches): return None result = self.matches[row] if role == Qt.DisplayRole: if col == 1: t = result.title if result.title else _('Unknown') a = result.author if result.author else '' return ('<b>%s</b><br><i>%s</i>' % (t, a)) elif col == 2: return (result.price) elif col == 4: return ('<span>%s<br>%s</span>' % (result.store_name, result.formats)) return None elif role == Qt.DecorationRole: if col == 0 and result.cover_data: p = QPixmap() p.loadFromData(result.cover_data) p.setDevicePixelRatio( QApplication.instance().devicePixelRatio()) return p if col == 3: if result.drm == SearchResult.DRM_LOCKED: return (self.DRM_LOCKED_ICON) elif result.drm == SearchResult.DRM_UNLOCKED: return (self.DRM_UNLOCKED_ICON) elif result.drm == SearchResult.DRM_UNKNOWN: return (self.DRM_UNKNOWN_ICON) if col == 5: if result.downloads: return (self.DOWNLOAD_ICON) if col == 6: if result.affiliate: return (self.DONATE_ICON) elif role == Qt.ToolTipRole: if col == 1: return ('<p>%s</p>' % result.title) elif col == 2: if result.price: return ('<p>' + _( 'Detected price as: %s. Check with the store before making a purchase' ' to verify this price is correct. This price often does not include' ' promotions the store may be running.') % result.price + '</p>') return '<p>' + _('No price was found') elif col == 3: if result.drm == SearchResult.DRM_LOCKED: return ('<p>' + _( 'This book as been detected as having DRM restrictions. This book may not work with your reader and you will have limitations placed upon you as to what you can do with this book. Check with the store before making any purchases to ensure you can actually read this book.' ) + '</p>') # noqa elif result.drm == SearchResult.DRM_UNLOCKED: return ('<p>' + _( 'This book has been detected as being DRM Free. You should be able to use this book on any device provided it is in a format calibre supports for conversion. However, before making a purchase double check the DRM status with the store. The store may not be disclosing the use of DRM.' ) + '</p>') # noqa else: return ('<p>' + _( 'The DRM status of this book could not be determined. There is a very high likelihood that this book is actually DRM restricted.' ) + '</p>') # noqa elif col == 4: return ('<p>%s</p>' % result.formats) elif col == 5: if result.downloads: return ('<p>' + _( 'The following formats can be downloaded directly: %s.' ) % ', '.join(result.downloads.keys()) + '</p>') elif col == 6: if result.affiliate: return ('<p>' + _( 'Buying from this store supports the calibre developer: %s.' ) % result.plugin_author + '</p>') elif role == Qt.SizeHintRole: return QSize(64, 64) return None
def bd_copy_link(self, url): if url: QApplication.clipboard().setText(url)
def check_key_modifier(which_modifier): v = int(QApplication.keyboardModifiers() & (Qt.ControlModifier + Qt.ShiftModifier)) return v == which_modifier
def copy_to_clipboard(self): QApplication.clipboard().setText(''.join(self.log.plain_text))
def copy_to_clipboard(self): QApplication.instance().clipboard().setPixmap(self.pixmap)
def copy_to_clipboard(self): QApplication.clipboard().setText(self.text_results)
def event_has_mods(self, event=None): mods = event.modifiers() if event is not None else \ QApplication.keyboardModifiers() return mods & Qt.ControlModifier or mods & Qt.ShiftModifier
def render_html(mi, css, vertical, widget, all_fields=False, render_data_func=None, pref_name='book_display_fields'): # {{{ func = render_data_func or render_data try: table, comment_fields = func( mi, all_fields=all_fields, use_roman_numbers=config['use_roman_numerals_for_series_number'], pref_name=pref_name) except TypeError: table, comment_fields = func( mi, all_fields=all_fields, use_roman_numbers=config['use_roman_numerals_for_series_number']) def color_to_string(col): ans = '#000000' if col.isValid(): col = col.toRgb() if col.isValid(): ans = unicode_type(col.name()) return ans fi = QFontInfo(QApplication.font(widget)) f = fi.pixelSize() + 1 + int(tweaks['change_book_details_font_size_by']) fam = unicode_type(fi.family()).strip().replace('"', '') if not fam: fam = 'sans-serif' c = color_to_string(QApplication.palette().color(QPalette.Normal, QPalette.WindowText)) templ = '''\ <html> <head> <style type="text/css"> body, td { background-color: transparent; font-size: %dpx; font-family: "%s",sans-serif; color: %s } </style> <style type="text/css"> %s </style> </head> <body> %%s </body> <html> ''' % (f, fam, c, css) comments = '' if comment_fields: comments = '\n'.join(u'<div>%s</div>' % x for x in comment_fields) right_pane = '<div id="comments" class="comments">%s</div>' % comments if vertical: ans = templ % (table + right_pane) else: if gprefs['book_details_narrow_comments_layout'] == 'columns': ans = templ % ( '<table><tr><td valign="top" ' 'style="padding-right:2em; width:40%%">%s</td><td valign="top">%s</td></tr></table>' % (table, right_pane)) else: ans = templ % ( '<div style="float: left; margin-right: 1em; margin-bottom: 1em; max-width: 40%">{}</div><div>{}</div>' .format(table, right_pane)) return ans
# SOURCE: http://www.pngall.com/target-png/download/12907 FILE_NAME = str(Path(__file__).resolve().parent / 'target.png') def move_window_to_cursor(widget: QWidget): width, height = widget.width(), widget.height() pos = QCursor.pos() pos.setX(pos.x() - width / 2) pos.setY(pos.y() - height / 2) widget.move(pos) if __name__ == '__main__': app = QApplication(sys.argv) lifetime_ms = 3000 if len(sys.argv) == 2: lifetime_ms = int(sys.argv[1]) pix = QPixmap(FILE_NAME) mw = QLabel() mw.setWindowFlag(Qt.WindowStaysOnTopHint) mw.setWindowFlag(Qt.FramelessWindowHint) mw.setPixmap(pix) mw.setMask(pix.mask()) mw.show()
self.det_msg.setPlainText(det_msg) self.det_msg.setVisible(False) self.det_msg_toggle.setText(self.show_det_msg) self.det_msg_toggle.setVisible(True) self.suppress.setChecked(False) self.update_suppress_state() if not det_msg: self.det_msg_toggle.setVisible(False) self.retry_button.setVisible(retry_func is not None) self.retry_func = retry_func self.do_resize() self.show() def done(self, r): if self.suppress.isChecked(): self.queue = [] QDialog.done(self, r) self.do_pop.emit() # }}} if __name__ == '__main__': app = QApplication([]) from calibre.gui2 import question_dialog print question_dialog(None, 'title', 'msg <a href="http://google.com">goog</a> ', det_msg='det ' * 1000, show_copy_button=True)
def __enter__(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
def run_loop(self): self.block_signal_handlers() try: return QApplication.exec_() finally: self.restore_signal_handlers()
import ui_converter import qrc_converter from PyQt5.Qt import QApplication from core.core import Core from data.database import Database def launch_gui(core): from gui import gui gui = gui.Gui(core) gui.init_gui() return gui if __name__ == '__main__': import sys app = QApplication(sys.argv) data = Database(app) core = Core(app, data) gui = launch_gui(core) sys.exit(app.exec_())
def __exit__(self, *args): QApplication.restoreOverrideCursor()
def set_clipboard(data): QApplication.clipboard().setText(data)
def load_pixmap(self, data): pmap = QPixmap() pmap.loadFromData(data) pmap.setDevicePixelRatio(QApplication.instance().devicePixelRatio()) return pmap
def copy_to_clipboard(self): txt = self.tb.toPlainText() QApplication.clipboard().setText(txt)
def do_paste_and_match_style(self): text = QApplication.instance().clipboard().text() if text: self.setText(text)