def process_duplicates(self, db, duplicates): ta = _('%(title)s by %(author)s') bf = QFont(self.dup_list.font()) bf.setBold(True) itf = QFont(self.dup_list.font()) itf.setItalic(True) for mi, cover, formats in duplicates: item = QTreeWidgetItem([ta%dict( title=mi.title, author=mi.format_field('authors')[1])] , 0) item.setCheckState(0, Qt.Checked) item.setFlags(Qt.ItemIsEnabled|Qt.ItemIsUserCheckable) item.setData(0, Qt.FontRole, bf) item.setData(0, Qt.UserRole, (mi, cover, formats)) matching_books = db.books_with_same_title(mi) def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemIsEnabled) item.addChild(c) return c add_child(_('Already in calibre:')).setData(0, Qt.FontRole, itf) for book_id in matching_books: aut = [a.replace('|', ',') for a in (db.authors(book_id, index_is_id=True) or '').split(',')] add_child(ta%dict( title=db.title(book_id, index_is_id=True), author=authors_to_string(aut))) add_child('') yield item
def create_item(name, linear=None): imt = container.mime_map.get(name, guess_type(name)) icat = get_category(name, imt) category = "text" if linear is not None else ({"text": "misc"}.get(icat, icat)) item = QTreeWidgetItem(self.categories["text" if linear is not None else category], 1) flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable if category == "text": flags |= Qt.ItemIsDragEnabled if name not in cannot_be_renamed: flags |= Qt.ItemIsEditable item.setFlags(flags) item.setStatusTip(0, _("Full path: ") + name) item.setData(0, NAME_ROLE, name) item.setData(0, CATEGORY_ROLE, category) item.setData(0, LINEAR_ROLE, bool(linear)) item.setData(0, MIME_ROLE, imt) set_display_name(name, item) # TODO: Add appropriate tooltips based on the emblems emblems = [] if name in {cover_page_name, cover_image_name}: emblems.append("default_cover.png") if name not in manifested_names and name not in ok_to_be_unmanifested: emblems.append("dialog_question.png") if linear is False: emblems.append("arrow-down.png") if linear is None and icat == "text": # Text item outside spine emblems.append("dialog_warning.png") if category == "text" and name in processed: # Duplicate entry in spine emblems.append("dialog_warning.png") render_emblems(item, emblems) return item
def create_item(self, f, parent): name = f.name ans = QTreeWidgetItem(parent, [name]) ans.setData(0, Qt.UserRole, '/'.join(f.full_path[1:])) ans.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) ans.setCheckState(0, Qt.Unchecked if self.dev.is_folder_ignored(f.storage_id, f.full_path[1:]) else Qt.Checked) ans.setData(0, Qt.DecorationRole, file_icon_provider().icon_from_ext('dir')) return ans
def create_item(self, f, parent): name = f.name ans = QTreeWidgetItem(parent, [name]) ans.setData(0, Qt.UserRole, '/'.join(f.full_path[1:])) ans.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) ans.setCheckState( 0, Qt.Unchecked if self.dev.is_folder_ignored( f.storage_id, f.full_path[1:]) else Qt.Checked) ans.setData(0, Qt.DecorationRole, file_icon_provider().icon_from_ext('dir')) return ans
def create_item(name, linear=None): imt = container.mime_map.get(name, guess_type(name)) icat = get_category(name, imt) category = "text" if linear is not None else ({"text": "misc"}.get(icat, icat)) item = QTreeWidgetItem(self.categories["text" if linear is not None else category], 1) flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable if category == "text": flags |= Qt.ItemIsDragEnabled if name not in cannot_be_renamed: flags |= Qt.ItemIsEditable item.setFlags(flags) item.setStatusTip(0, _("Full path: ") + name) item.setData(0, NAME_ROLE, name) item.setData(0, CATEGORY_ROLE, category) item.setData(0, LINEAR_ROLE, bool(linear)) item.setData(0, MIME_ROLE, imt) set_display_name(name, item) tooltips = [] emblems = [] if name in {cover_page_name, cover_image_name}: emblems.append("default_cover.png") tooltips.append( _("This file is the cover %s for this book") % (_("image") if name == cover_image_name else _("page")) ) if name in container.opf_name: emblems.append("metadata.png") tooltips.append(_("This file contains all the metadata and book structure information")) if imt == ncx_mime: emblems.append("toc.png") tooltips.append(_("This file contains the metadata table of contents")) if name not in manifested_names and name not in ok_to_be_unmanifested: emblems.append("dialog_question.png") tooltips.append(_("This file is not listed in the book manifest")) if linear is False: emblems.append("arrow-down.png") tooltips.append( _("This file is marked as non-linear in the spine\nDrag it to the top to make it linear") ) if linear is None and icat == "text": # Text item outside spine emblems.append("dialog_warning.png") tooltips.append(_("This file is a text file that is not referenced in the spine")) if category == "text" and name in processed: # Duplicate entry in spine emblems.append("dialog_error.png") tooltips.append(_("This file occurs more than once in the spine")) render_emblems(item, emblems) if tooltips: item.setData(0, Qt.ToolTipRole, "\n".join(tooltips)) return item
def process_duplicates(self, db, duplicates): ta = _('%(title)s by %(author)s [%(formats)s]') bf = QFont(self.dup_list.font()) bf.setBold(True) itf = QFont(self.dup_list.font()) itf.setItalic(True) for mi, cover, formats in duplicates: # formats is a list of file paths # Grab just the extension and display to the user # Based only off the file name, no file type tests are done. incoming_formats = ', '.join( os.path.splitext(path)[-1].replace('.', '').upper() for path in formats) item = QTreeWidgetItem([ ta % dict(title=mi.title, author=mi.format_field('authors')[1], formats=incoming_formats) ], 0) item.setCheckState(0, Qt.Checked) item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsUserCheckable) item.setData(0, Qt.FontRole, bf) item.setData(0, Qt.UserRole, (mi, cover, formats)) matching_books = db.books_with_same_title(mi) def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemIsEnabled) item.addChild(c) return c add_child(_('Already in calibre:')).setData(0, Qt.FontRole, itf) for book_id in matching_books: aut = [ a.replace('|', ',') for a in ( db.authors(book_id, index_is_id=True) or '').split(',') ] add_child( ta % dict(title=db.title(book_id, index_is_id=True), author=authors_to_string(aut), formats=db.formats( book_id, index_is_id=True, verify_formats=False))) add_child('') yield item
def create_item(name, linear=None): imt = container.mime_map.get(name, guess_type(name)) icat = get_category(name, imt) category = 'text' if linear is not None else ({ 'text': 'misc' }.get(icat, icat)) item = QTreeWidgetItem( self.categories['text' if linear is not None else category], 1) flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable if category == 'text': flags |= Qt.ItemIsDragEnabled if name not in cannot_be_renamed: flags |= Qt.ItemIsEditable item.setFlags(flags) item.setStatusTip(0, _('Full path: ') + name) item.setData(0, NAME_ROLE, name) item.setData(0, CATEGORY_ROLE, category) item.setData(0, LINEAR_ROLE, bool(linear)) item.setData(0, MIME_ROLE, imt) set_display_name(name, item) # TODO: Add appropriate tooltips based on the emblems emblems = [] if name in {cover_page_name, cover_image_name}: emblems.append('default_cover.png') if name in container.opf_name: emblems.append('metadata.png') if imt == ncx_mime: emblems.append('toc.png') if name not in manifested_names and name not in ok_to_be_unmanifested: emblems.append('dialog_question.png') if linear is False: emblems.append('arrow-down.png') if linear is None and icat == 'text': # Text item outside spine emblems.append('dialog_warning.png') if category == 'text' and name in processed: # Duplicate entry in spine emblems.append('dialog_warning.png') render_emblems(item, emblems) return item
def process_duplicates(self, db, duplicates): ta = _('%(title)s by %(author)s [%(formats)s]') bf = QFont(self.dup_list.font()) bf.setBold(True) itf = QFont(self.dup_list.font()) itf.setItalic(True) for mi, cover, formats in duplicates: # formats is a list of file paths # Grab just the extension and display to the user # Based only off the file name, no file type tests are done. incoming_formats = ', '.join(os.path.splitext(path)[-1].replace('.', '').upper() for path in formats) item = QTreeWidgetItem([ta%dict( title=mi.title, author=mi.format_field('authors')[1], formats=incoming_formats)] , 0) item.setCheckState(0, Qt.Checked) item.setFlags(Qt.ItemIsEnabled|Qt.ItemIsUserCheckable) item.setData(0, Qt.FontRole, bf) item.setData(0, Qt.UserRole, (mi, cover, formats)) matching_books = db.books_with_same_title(mi) def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemIsEnabled) item.addChild(c) return c add_child(_('Already in calibre:')).setData(0, Qt.FontRole, itf) for book_id in matching_books: aut = [a.replace('|', ',') for a in (db.authors(book_id, index_is_id=True) or '').split(',')] add_child(ta%dict( title=db.title(book_id, index_is_id=True), author=authors_to_string(aut), formats=db.formats(book_id, index_is_id=True, verify_formats=False))) add_child('') yield item
def build_dictionaries(self, reread=False): all_dictionaries = builtin_dictionaries() | custom_dictionaries(reread=reread) languages = defaultdict(lambda : defaultdict(set)) for d in all_dictionaries: for locale in d.locales | {d.primary_locale}: languages[locale.langcode][locale.countrycode].add(d) bf = QFont(self.dictionaries.font()) bf.setBold(True) itf = QFont(self.dictionaries.font()) itf.setItalic(True) self.dictionaries.clear() for lc in sorted(languages, key=lambda x:sort_key(calibre_langcode_to_name(x))): i = QTreeWidgetItem(self.dictionaries, LANG) i.setText(0, calibre_langcode_to_name(lc)) i.setData(0, Qt.UserRole, lc) best_country = getattr(best_locale_for_language(lc), 'countrycode', None) for countrycode in sorted(languages[lc], key=lambda x: country_map()['names'].get(x, x)): j = QTreeWidgetItem(i, COUNTRY) j.setText(0, country_map()['names'].get(countrycode, countrycode)) j.setData(0, Qt.UserRole, countrycode) if countrycode == best_country: j.setData(0, Qt.FontRole, bf) pd = get_dictionary(DictionaryLocale(lc, countrycode)) for dictionary in sorted(languages[lc][countrycode], key=lambda d:d.name): k = QTreeWidgetItem(j, DICTIONARY) pl = calibre_langcode_to_name(dictionary.primary_locale.langcode) if dictionary.primary_locale.countrycode: pl += '-' + dictionary.primary_locale.countrycode.upper() k.setText(0, dictionary.name or (_('<Builtin dictionary for {0}>').format(pl))) k.setData(0, Qt.UserRole, dictionary) if dictionary.name: k.setFlags(k.flags() | Qt.ItemIsEditable) if pd == dictionary: k.setData(0, Qt.FontRole, itf) self.dictionaries.expandAll()
def create_item(name, linear=None): imt = container.mime_map.get(name, guess_type(name)) icat = get_category(name, imt) category = 'text' if linear is not None else ({ 'text': 'misc' }.get(icat, icat)) item = QTreeWidgetItem( self.categories['text' if linear is not None else category], 1) flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable if category == 'text': flags |= Qt.ItemIsDragEnabled if name not in cannot_be_renamed: flags |= Qt.ItemIsEditable item.setFlags(flags) item.setStatusTip(0, _('Full path: ') + name) item.setData(0, NAME_ROLE, name) item.setData(0, CATEGORY_ROLE, category) item.setData(0, LINEAR_ROLE, bool(linear)) item.setData(0, MIME_ROLE, imt) set_display_name(name, item) tooltips = [] emblems = [] if name in {cover_page_name, cover_image_name}: emblems.append('default_cover.png') tooltips.append( _('This file is the cover %s for this book') % (_('image') if name == cover_image_name else _('page'))) if name in container.opf_name: emblems.append('metadata.png') tooltips.append( _('This file contains all the metadata and book structure information' )) if imt == ncx_mime: emblems.append('toc.png') tooltips.append( _('This file contains the metadata table of contents')) if name not in manifested_names and not container.ok_to_be_unmanifested( name): emblems.append('dialog_question.png') tooltips.append( _('This file is not listed in the book manifest')) if linear is False: emblems.append('arrow-down.png') tooltips.append( _('This file is marked as non-linear in the spine\nDrag it to the top to make it linear' )) if linear is None and icat == 'text': # Text item outside spine emblems.append('dialog_warning.png') tooltips.append( _('This file is a text file that is not referenced in the spine' )) if category == 'text' and name in processed: # Duplicate entry in spine emblems.append('dialog_error.png') tooltips.append( _('This file occurs more than once in the spine')) render_emblems(item, emblems) if tooltips: item.setData(0, Qt.ToolTipRole, '\n'.join(tooltips)) return item
def add_child(text): c = QTreeWidgetItem([text], 1) c.setFlags(Qt.ItemIsEnabled) item.addChild(c) return c
def build(self, container): self.clear() self.root = self.invisibleRootItem() self.root.setFlags(Qt.ItemIsDragEnabled) self.categories = {} for category, text, icon in ( ('text', _('Text'), 'keyboard-prefs.png'), ('styles', _('Styles'), 'lookfeel.png'), ('images', _('Images'), 'view-image.png'), ('fonts', _('Fonts'), 'font.png'), ('misc', _('Miscellaneous'), 'mimetypes/dir.png'), ): self.categories[category] = i = QTreeWidgetItem(self.root, 0) i.setText(0, text) i.setIcon(0, QIcon(I(icon))) f = i.font(0) f.setBold(True) i.setFont(0, f) i.setData(0, NAME_ROLE, category) flags = Qt.ItemIsEnabled if category == 'text': flags |= Qt.ItemIsDropEnabled i.setFlags(flags) processed, seen = set(), {} def get_display_name(name, item): parts = name.split('/') text = parts[-1] while text in seen and parts: text = parts.pop() + '/' + text seen[text] = item return text for name, linear in container.spine_names: processed.add(name) i = QTreeWidgetItem(self.categories['text'], 1) prefix = '' if linear else '[nl] ' if not linear: i.setIcon(self.non_linear_icon) i.setText(0, prefix + get_display_name(name, i)) i.setStatusTip(0, _('Full path: ') + name) i.setFlags(Qt.ItemIsEnabled | Qt.ItemIsDragEnabled | Qt.ItemIsSelectable) i.setData(0, NAME_ROLE, name) font_types = {guess_type('a.'+x)[0] for x in ('ttf', 'otf', 'woff')} def get_category(mt): category = 'misc' if mt.startswith('image/'): category = 'images' elif mt in font_types: category = 'fonts' elif mt in OEB_STYLES: category = 'styles' return category all_files = list(container.manifest_type_map.iteritems()) all_files.append((guess_type('a.opf')[0], [container.opf_name])) for name in container.name_path_map: if name in processed: continue processed.add(name) imt = container.mime_map.get(name, guess_type(name)[0]) icat = get_category(imt) i = QTreeWidgetItem(self.categories[icat], 1) i.setText(0, get_display_name(name, i)) i.setStatusTip(0, _('Full path: ') + name) i.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable) i.setData(0, NAME_ROLE, name) for c in self.categories.itervalues(): self.expandItem(c)