def sizeHint(self): ''' @return: QSize ''' boldFont = QFont(self.font()) boldFont.setPointSizeF(styleHelper.sidebarFontSize()) boldFont.setBold(True) fm = QFontMetrics(boldFont) spacing = 8 width = 60 + spacing + 2 iconHeight = 32 ret = QSize(width, iconHeight + spacing + fm.height()) return ret
def _tabSizeHint(self, minimum=False): ''' @return: QSize ''' boldFont = QFont(self.font()) boldFont.setPointSizeF(styleHelper.sidebarFontSize()) boldFont.setBold(True) fm = QFontMetrics(boldFont) spacing = 8 width = 60 + spacing + 2 if minimum: iconHeight = 0 else: iconHeight = 32 return QSize(width, iconHeight + spacing + fm.height())
def populate(self, phrase, ts, process_space=True): phrase_pos = 0 processed = False matches = self.__class__.whitespace.finditer(phrase) font = QFont(ts.font) if self.valign is not None: font.setPixelSize(font.pixelSize() / 1.5) fm = QFontMetrics(font) single_space_width = fm.width(' ') height, descent = fm.height(), fm.descent() for match in matches: processed = True left, right = match.span() if not process_space: right = left space_width = single_space_width * (right - left) word = phrase[phrase_pos:left] width = fm.width(word) if self.current_width + width < self.line_length: self.commit(word, width, height, descent, ts, font) if space_width > 0 and self.current_width + space_width < self.line_length: self.add_space(space_width) phrase_pos = right continue # Word doesn't fit on line if self.hyphenate and len(word) > 3: tokens = hyphenate_word(word) for i in range(len(tokens) - 2, -1, -1): word = ''.join(tokens[0:i + 1]) + '-' width = fm.width(word) if self.current_width + width < self.line_length: self.commit(word, width, height, descent, ts, font) return phrase_pos + len(word) - 1, True if self.current_width < 5: # Force hyphenation as word is longer than line for i in range(len(word) - 5, 0, -5): part = word[:i] + '-' width = fm.width(part) if self.current_width + width < self.line_length: self.commit(part, width, height, descent, ts, font) return phrase_pos + len(part) - 1, True # Failed to add word. return phrase_pos, True if not processed: return self.populate(phrase + ' ', ts, False) return phrase_pos, False
def populate(self, phrase, ts, process_space=True): phrase_pos = 0 processed = False matches = self.__class__.whitespace.finditer(phrase) font = QFont(ts.font) if self.valign is not None: font.setPixelSize(font.pixelSize()/1.5) fm = QFontMetrics(font) single_space_width = fm.width(' ') height, descent = fm.height(), fm.descent() for match in matches: processed = True left, right = match.span() if not process_space: right = left space_width = single_space_width * (right-left) word = phrase[phrase_pos:left] width = fm.width(word) if self.current_width + width < self.line_length: self.commit(word, width, height, descent, ts, font) if space_width > 0 and self.current_width + space_width < self.line_length: self.add_space(space_width) phrase_pos = right continue # Word doesn't fit on line if self.hyphenate and len(word) > 3: tokens = hyphenate_word(word) for i in range(len(tokens)-2, -1, -1): word = ''.join(tokens[0:i+1])+'-' width = fm.width(word) if self.current_width + width < self.line_length: self.commit(word, width, height, descent, ts, font) return phrase_pos + len(word)-1, True if self.current_width < 5: # Force hyphenation as word is longer than line for i in range(len(word)-5, 0, -5): part = word[:i] + '-' width = fm.width(part) if self.current_width + width < self.line_length: self.commit(part, width, height, descent, ts, font) return phrase_pos + len(part)-1, True # Failed to add word. return phrase_pos, True if not processed: return self.populate(phrase+' ', ts, False) return phrase_pos, False
def do_size_hint(self, option, index): text = index.data(Qt.DisplayRole) or '' font = QFont(option.font) font.setPointSize(QFontInfo(font).pointSize() * 1.5) m = QFontMetrics(font) return QSize(m.width(text), m.height())
def __init__(self, parent, book_list, get_annotations_as_HTML, source): self.opts = parent.opts self.parent = parent self.get_annotations_as_HTML = get_annotations_as_HTML self.show_confidence_colors = self.opts.prefs.get( 'annotated_books_dialog_show_confidence_as_bg_color', True) self.source = source # QDialog.__init__(self, parent=self.opts.gui) SizePersistedDialog.__init__( self, self.opts.gui, 'Annotations plugin:import annotations dialog') self.setWindowTitle(_('Import Annotations')) self.setWindowIcon(self.opts.icon) self.l = QVBoxLayout(self) self.setLayout(self.l) self.perfect_width = 0 from calibre_plugins.annotations.appearance import default_timestamp friendly_timestamp_format = plugin_prefs.get( 'appearance_timestamp_format', default_timestamp) # Are we collecting News clippings? collect_news_clippings = self.opts.prefs.get( 'cfg_news_clippings_checkbox', False) news_clippings_destination = self.opts.prefs.get( 'cfg_news_clippings_lineEdit', None) # Populate the table data self.tabledata = [] for book_data in book_list: enabled = QCheckBox() enabled.setChecked(True) # last_annotation sorts by timestamp last_annotation = SortableTableWidgetItem( strftime(friendly_timestamp_format, localtime(book_data['last_update'])), book_data['last_update']) # reader_app sorts case-insensitive reader_app = SortableTableWidgetItem( book_data['reader_app'], book_data['reader_app'].upper()) # title, author sort by title_sort, author_sort if not book_data['title_sort']: book_data['title_sort'] = book_data['title'] title = SortableTableWidgetItem(book_data['title'], book_data['title_sort'].upper()) if not book_data['author_sort']: book_data['author_sort'] = book_data['author'] author = SortableTableWidgetItem(book_data['author'], book_data['author_sort'].upper()) genres = book_data['genre'].split(', ') if 'News' in genres and collect_news_clippings: cid = get_clippings_cid(self, news_clippings_destination) confidence = 5 else: cid, confidence = parent.generate_confidence(book_data) # List order matches self.annotations_header this_book = [ book_data['uuid'], book_data['book_id'], book_data['genre'], enabled, reader_app, title, author, last_annotation, book_data['annotations'], confidence ] self.tabledata.append(this_book) self.tv = QTableView(self) self.l.addWidget(self.tv) self.annotations_header = [ 'uuid', 'book_id', 'genre', '', _('Reader App'), _('Title'), _('Author'), _('Last Annotation'), _('Annotations'), _('Confidence') ] self.ENABLED_COL = 3 self.READER_APP_COL = 4 self.TITLE_COL = 5 self.AUTHOR_COL = 6 self.LAST_ANNOTATION_COL = 7 self.CONFIDENCE_COL = 9 columns_to_center = [8] self.tm = MarkupTableModel(self, columns_to_center=columns_to_center) self.tv.setModel(self.tm) self.tv.setShowGrid(False) self.tv.setFont(self.FONT) self.tvSelectionModel = self.tv.selectionModel() self.tv.setAlternatingRowColors(not self.show_confidence_colors) self.tv.setShowGrid(False) self.tv.setWordWrap(False) self.tv.setSelectionBehavior(self.tv.SelectRows) # Connect signals self.tv.doubleClicked.connect(self.getTableRowDoubleClick) self.tv.horizontalHeader().sectionClicked.connect( self.capture_sort_column) # Hide the vertical self.header self.tv.verticalHeader().setVisible(False) # Hide uuid, book_id, genre, confidence self.tv.hideColumn(self.annotations_header.index('uuid')) self.tv.hideColumn(self.annotations_header.index('book_id')) self.tv.hideColumn(self.annotations_header.index('genre')) # self.tv.hideColumn(self.annotations_header.index(_('Confidence'))) self.tv.hideColumn(self.CONFIDENCE_COL) # Set horizontal self.header props self.tv.horizontalHeader().setStretchLastSection(True) narrow_columns = [ _('Last Annotation'), _('Reader App'), _('Annotations') ] extra_width = 10 breathing_space = 20 # Set column width to fit contents self.tv.resizeColumnsToContents() perfect_width = 10 + (len(narrow_columns) * extra_width) for i in range(3, 8): perfect_width += self.tv.columnWidth(i) + breathing_space self.tv.setMinimumSize(perfect_width, 100) self.perfect_width = perfect_width # Add some width to narrow columns for nc in narrow_columns: cw = self.tv.columnWidth(self.annotations_header.index(nc)) self.tv.setColumnWidth(self.annotations_header.index(nc), cw + extra_width) # Set row height fm = QFontMetrics(self.FONT) nrows = len(self.tabledata) for row in xrange(nrows): self.tv.setRowHeight(row, fm.height() + 4) self.tv.setSortingEnabled(True) sort_column = self.opts.prefs.get('annotated_books_dialog_sort_column', self.CONFIDENCE_COL) sort_order = self.opts.prefs.get('annotated_books_dialog_sort_order', Qt.DescendingOrder) self.tv.sortByColumn(sort_column, sort_order) # ~~~~~~~~ Create the ButtonBox ~~~~~~~~ self.dialogButtonBox = QDialogButtonBox(QDialogButtonBox.Cancel | QDialogButtonBox.Help) self.dialogButtonBox.setOrientation(Qt.Horizontal) self.import_button = self.dialogButtonBox.addButton( self.dialogButtonBox.Ok) self.import_button.setText(_('Import Annotations')) # Action buttons self.toggle_checkmarks_button = self.dialogButtonBox.addButton( _('Clear All'), QDialogButtonBox.ActionRole) self.toggle_checkmarks_button.setObjectName('toggle_checkmarks_button') scb_text = _('Show match status') if self.show_confidence_colors: scb_text = _("Hide match status") self.show_confidence_button = self.dialogButtonBox.addButton( scb_text, QDialogButtonBox.ActionRole) self.show_confidence_button.setObjectName('confidence_button') if self.show_confidence_colors: self.show_confidence_button.setIcon( get_icon('images/matches_hide.png')) else: self.show_confidence_button.setIcon( get_icon('images/matches_show.png')) self.preview_button = self.dialogButtonBox.addButton( _('Preview'), QDialogButtonBox.ActionRole) self.preview_button.setObjectName('preview_button') self.dialogButtonBox.clicked.connect( self.show_annotated_books_dialog_clicked) self.l.addWidget(self.dialogButtonBox) # Cause our dialog size to be restored from prefs or created on first usage self.resize_dialog()
def __init__(self, parent, book_list, get_annotations_as_HTML, source): self.opts = parent.opts self.parent = parent self.get_annotations_as_HTML = get_annotations_as_HTML self.show_confidence_colors = self.opts.prefs.get('annotated_books_dialog_show_confidence_as_bg_color', True) self.source = source # QDialog.__init__(self, parent=self.opts.gui) SizePersistedDialog.__init__(self, self.opts.gui, 'Annotations plugin:import annotations dialog') self.setWindowTitle(u'Import Annotations') self.setWindowIcon(self.opts.icon) self.l = QVBoxLayout(self) self.setLayout(self.l) self.perfect_width = 0 from calibre_plugins.annotations.appearance import default_timestamp friendly_timestamp_format = plugin_prefs.get('appearance_timestamp_format', default_timestamp) # Are we collecting News clippings? collect_news_clippings = self.opts.prefs.get('cfg_news_clippings_checkbox', False) news_clippings_destination = self.opts.prefs.get('cfg_news_clippings_lineEdit', None) # Populate the table data self.tabledata = [] for book_data in book_list: enabled = QCheckBox() enabled.setChecked(True) # last_annotation sorts by timestamp last_annotation = SortableTableWidgetItem( strftime(friendly_timestamp_format, localtime(book_data['last_update'])), book_data['last_update']) # reader_app sorts case-insensitive reader_app = SortableTableWidgetItem( book_data['reader_app'], book_data['reader_app'].upper()) # title, author sort by title_sort, author_sort if not book_data['title_sort']: book_data['title_sort'] = book_data['title'] title = SortableTableWidgetItem( book_data['title'], book_data['title_sort'].upper()) if not book_data['author_sort']: book_data['author_sort'] = book_data['author'] author = SortableTableWidgetItem( book_data['author'], book_data['author_sort'].upper()) genres = book_data['genre'].split(', ') if 'News' in genres and collect_news_clippings: cid = get_clippings_cid(self, news_clippings_destination) confidence = 5 else: cid, confidence = parent.generate_confidence(book_data) # List order matches self.annotations_header this_book = [ book_data['uuid'], book_data['book_id'], book_data['genre'], enabled, reader_app, title, author, last_annotation, book_data['annotations'], confidence] self.tabledata.append(this_book) self.tv = QTableView(self) self.l.addWidget(self.tv) self.annotations_header = ['uuid', 'book_id', 'genre', '', 'Reader App', 'Title', 'Author', 'Last Annotation', 'Annotations', 'Confidence'] self.ENABLED_COL = 3 self.READER_APP_COL = 4 self.TITLE_COL = 5 self.AUTHOR_COL = 6 self.LAST_ANNOTATION_COL = 7 self.CONFIDENCE_COL = 9 columns_to_center = [8] self.tm = MarkupTableModel(self, columns_to_center=columns_to_center) self.tv.setModel(self.tm) self.tv.setShowGrid(False) self.tv.setFont(self.FONT) self.tvSelectionModel = self.tv.selectionModel() self.tv.setAlternatingRowColors(not self.show_confidence_colors) self.tv.setShowGrid(False) self.tv.setWordWrap(False) self.tv.setSelectionBehavior(self.tv.SelectRows) # Connect signals self.tv.doubleClicked.connect(self.getTableRowDoubleClick) self.tv.horizontalHeader().sectionClicked.connect(self.capture_sort_column) # Hide the vertical self.header self.tv.verticalHeader().setVisible(False) # Hide uuid, book_id, genre, confidence self.tv.hideColumn(self.annotations_header.index('uuid')) self.tv.hideColumn(self.annotations_header.index('book_id')) self.tv.hideColumn(self.annotations_header.index('genre')) self.tv.hideColumn(self.annotations_header.index('Confidence')) # Set horizontal self.header props self.tv.horizontalHeader().setStretchLastSection(True) narrow_columns = ['Last Annotation', 'Reader App', 'Annotations'] extra_width = 10 breathing_space = 20 # Set column width to fit contents self.tv.resizeColumnsToContents() perfect_width = 10 + (len(narrow_columns) * extra_width) for i in range(3, 8): perfect_width += self.tv.columnWidth(i) + breathing_space self.tv.setMinimumSize(perfect_width, 100) self.perfect_width = perfect_width # Add some width to narrow columns for nc in narrow_columns: cw = self.tv.columnWidth(self.annotations_header.index(nc)) self.tv.setColumnWidth(self.annotations_header.index(nc), cw + extra_width) # Set row height fm = QFontMetrics(self.FONT) nrows = len(self.tabledata) for row in xrange(nrows): self.tv.setRowHeight(row, fm.height() + 4) self.tv.setSortingEnabled(True) sort_column = self.opts.prefs.get('annotated_books_dialog_sort_column', self.annotations_header.index('Confidence')) sort_order = self.opts.prefs.get('annotated_books_dialog_sort_order', Qt.DescendingOrder) self.tv.sortByColumn(sort_column, sort_order) # ~~~~~~~~ Create the ButtonBox ~~~~~~~~ self.dialogButtonBox = QDialogButtonBox(QDialogButtonBox.Cancel | QDialogButtonBox.Help) self.dialogButtonBox.setOrientation(Qt.Horizontal) self.import_button = self.dialogButtonBox.addButton(self.dialogButtonBox.Ok) self.import_button.setText('Import Annotations') # Action buttons self.toggle_checkmarks_button = self.dialogButtonBox.addButton('Clear All', QDialogButtonBox.ActionRole) self.toggle_checkmarks_button.setObjectName('toggle_checkmarks_button') scb_text = 'Show match status' if self.show_confidence_colors: scb_text = "Hide match status" self.show_confidence_button = self.dialogButtonBox.addButton(scb_text, QDialogButtonBox.ActionRole) self.show_confidence_button.setObjectName('confidence_button') if self.show_confidence_colors: self.show_confidence_button.setIcon(get_icon('images/matches_hide.png')) else: self.show_confidence_button.setIcon(get_icon('images/matches_show.png')) self.preview_button = self.dialogButtonBox.addButton('Preview', QDialogButtonBox.ActionRole) self.preview_button.setObjectName('preview_button') self.dialogButtonBox.clicked.connect(self.show_annotated_books_dialog_clicked) self.l.addWidget(self.dialogButtonBox) # Cause our dialog size to be restored from prefs or created on first usage self.resize_dialog()
def paint(self, painter, option, index): # noqa C901 ''' @param painter QPainter @param option index QStyleOptionViewItem @param option index QModelIndex ''' from ..LocationBar import LocationBar opt = QStyleOptionViewItem(option) self.initStyleOption(opt, index) w = opt.widget if w: style = w.style() else: style = QApplication.style() height = opt.rect.height() center = height / 2 + opt.rect.top() # Prepare link font # QFont linkFont = opt.font linkFont.setPointSize(linkFont.pointSize() - 1) linkMetrics = QFontMetrics(linkFont) leftPosition = self._padding * 2 rightPosition = opt.rect.right() - self._padding opt.state |= QStyle.State_Active if opt.state & QStyle.State_Selected: iconMode = QIcon.Selected colorRole = QPalette.HighlightedText colorLinkRole = QPalette.HighlightedText else: iconMode = QIcon.Normal colorRole = QPalette.Text colorLinkRole = QPalette.Link if const.OS_WIN: opt.palette.setColor(QPalette.All, QPalette.HighlightedText, opt.palette.color(QPalette.Active, QPalette.Text)) opt.palette.setColor(QPalette.All, QPalette.Highlight, opt.palette.base().color().darker(108)) textPalette = QPalette(opt.palette) if opt.state & QStyle.State_Enabled: textPalette.setCurrentColorGroup(QPalette.Normal) else: textPalette.setCurrentColorGroup(QPalette.Disabled) # Draw background style.drawPrimitive(QStyle.PE_PanelItemViewItem, opt, painter, w) isVisitSearchItem = index.data(LocationCompleterModel.VisitSearchItemRole) isSearchSuggestion = index.data(LocationCompleterModel.SearchSuggestionRole) loadAction = LocationBar.LoadAction() isWebSearch = isSearchSuggestion # BookmarkItem bookmark = index.data(LocationCompleterModel.BookmarkItemRole) if isVisitSearchItem: text = index.data(LocationCompleterModel.SearchStringRole) loadAction = LocationBar.loadAction(text) isWebSearch = loadAction.type == LocationBar.LoadAction.Search if not self._forceVisitItem: bookmark = loadAction.bookmark # Draw icon iconSize = 16 iconYPos = center - iconSize / 2 iconRect = QRect(leftPosition, iconYPos, iconSize, iconSize) icon = index.data(Qt.DecorationRole) if not icon: icon = QIcon() pixmap = icon.pixmap(iconSize) if isSearchSuggestion or (isVisitSearchItem and isWebSearch): pixmap = QIcon.fromTheme('edit-find', QIcon(':/icons/menu/search-icon.svg')).pixmap(iconSize, iconMode) if isVisitSearchItem and bookmark: pixmap = bookmark.icon().pixmap(iconSize) elif loadAction.type == LocationBar.LoadAction.Search: if loadAction.searchEngine.name != LocationBar.searchEngine().name: pixmap = loadAction.searchEngine.icon.pixmap(iconSize) painter.drawPixmap(iconRect, pixmap) leftPosition = iconRect.right() + self._padding * 2 # Draw star to bookmark items starPixmapWidth = 0 if bookmark: icon = IconProvider.instance().bookmarkIcon starSize = QSize(16, 16) starPixmapWidth = starSize.width() pos = QPoint(rightPosition - starPixmapWidth, center - starSize.height() / 2) starRect = QRect(pos, starSize) painter.drawPixmap(starRect, icon.pixmap(starSize, iconMode)) searchText = index.data(LocationCompleterModel.SearchStringRole) # Draw title leftPosition += 2 titleRect = QRect(leftPosition, center - opt.fontMetrics.height() / 2, opt.rect.width() * 0.6, opt.fontMetrics.height()) title = index.data(LocationCompleterModel.TitleRole) painter.setFont(opt.font) if isVisitSearchItem: if bookmark: title = bookmark.title() else: title = index.data(LocationCompleterModel.SearchStringRole) searchText = '' leftPosition += self.viewItemDrawText(painter, opt, titleRect, title, textPalette.color(colorRole), searchText) leftPosition += self._padding * 2 # Trim link to maximum number characters that can be visible, # otherwise there may be perf issue with huge URLs maxChars = int((opt.rect.width() - leftPosition) / opt.fontMetrics.width('i')) link = index.data(Qt.DisplayRole) if not link.startswith('data') and not link.startswith('javascript'): link = unquote(link)[:maxChars] else: link = link[:maxChars] if isVisitSearchItem or isSearchSuggestion: if not (opt.state & QStyle.State_Selected) and not (opt.state & QStyle.State_MouseOver): link = '' elif isVisitSearchItem and (not isWebSearch or self._forceVisitItem): link = _('Visit') else: searchEngineName = loadAction.searchEngine.name if not searchEngineName: searchEngineName = LocationBar.searchEngine().name link = _('Search with %s') % searchEngineName if bookmark: link = bookmark.url().toString() # Draw separator if link: separator = '-' separatorRect = QRect(leftPosition, center - linkMetrics.height() / 2, linkMetrics.width(separator), linkMetrics.height()) style.drawItemText(painter, separatorRect, Qt.AlignCenter, textPalette, True, separator, colorRole) leftPosition += separatorRect.width() + self._padding * 2 # Draw link leftLinkEdge = leftPosition rightLinkEdge = rightPosition - self._padding - starPixmapWidth linkRect = QRect(leftLinkEdge, center - linkMetrics.height() / 2, rightLinkEdge - leftLinkEdge, linkMetrics.height()) painter.setFont(linkFont) # Darw url (or switch to tab) tabPos = index.data(LocationCompleterModel.TabPositionTabRole) if gVar.appSettings.showSwitchTab and not self._forceVisitItem and tabPos != -1: tabIcon = QIcon(':/icons/menu/tab.svg') iconRect = QRect(linkRect) iconRect.setX(iconRect.x()) iconRect.setWidth(16) painter.drawPixmap(iconRect, tabIcon.pixmap(iconRect.size(), iconMode)) textRect = QRect(linkRect) textRect.setX(textRect.x() + self._padding + 16 + self._padding) self.viewItemDrawText(painter, opt, textRect, _('Switch to tab'), textPalette.color(colorLinkRole)) elif isVisitSearchItem or isSearchSuggestion: self.viewItemDrawText(painter, opt, linkRect, link, textPalette.color(colorLinkRole)) else: self.viewItemDrawText(painter, opt, linkRect, link, textPalette.color(colorLinkRole), searchText)