Ejemplo n.º 1
0
    def paint(self, painter, option, index):
        top_level = not index.parent().isValid()
        hover = option.state & QStyle.StateFlag.State_MouseOver
        cc = current_container()

        def safe_size(index):
            try:
                return cc.filesize(str(index.data(NAME_ROLE) or ''))
            except OSError:
                return 0

        if hover:
            if top_level:
                count = index.model().rowCount(index)
                total_size = human_readable(sum(safe_size(index.child(r, 0)) for r in range(count)))
                suffix = f'{NBSP}{count}@{total_size}'
            else:
                suffix = NBSP + human_readable(safe_size(index))
            br = painter.boundingRect(option.rect, Qt.AlignmentFlag.AlignRight|Qt.AlignmentFlag.AlignVCenter, suffix)
        if top_level and index.row() > 0:
            option.rect.adjust(0, 5, 0, 0)
            painter.drawLine(option.rect.topLeft(), option.rect.topRight())
            option.rect.adjust(0, 1, 0, 0)
        if hover:
            option.rect.adjust(0, 0, -br.width(), 0)
        QStyledItemDelegate.paint(self, painter, option, index)
        if hover:
            option.rect.adjust(0, 0, br.width(), 0)
            painter.drawText(option.rect, Qt.AlignmentFlag.AlignRight|Qt.AlignmentFlag.AlignVCenter, suffix)
Ejemplo n.º 2
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     text, positions = index.data(Qt.ItemDataRole.UserRole)
     self.initStyleOption(option, index)
     painter.save()
     painter.setFont(option.font)
     p = option.palette
     c = QPalette.ColorRole.HighlightedText if option.state & QStyle.StateFlag.State_Selected else QPalette.ColorRole.Text
     group = (QPalette.ColorGroup.Active if option.state & QStyle.StateFlag.State_Active else QPalette.ColorGroup.Inactive)
     c = p.color(group, c)
     painter.setClipRect(option.rect)
     if positions is None or -1 in positions:
         painter.setPen(c)
         painter.drawText(option.rect, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter | Qt.TextFlag.TextSingleLine, text)
     else:
         to = QTextOption()
         to.setWrapMode(QTextOption.WrapMode.NoWrap)
         to.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
         positions = sorted(set(positions) - {-1}, reverse=True)
         text = '<body>%s</body>' % make_highlighted_text(emphasis_style(), text, positions)
         doc = QTextDocument()
         c = 'rgb(%d, %d, %d)'%c.getRgb()[:3]
         doc.setDefaultStyleSheet(' body { color: %s }'%c)
         doc.setHtml(text)
         doc.setDefaultFont(option.font)
         doc.setDocumentMargin(0.0)
         doc.setDefaultTextOption(to)
         height = doc.size().height()
         painter.translate(option.rect.left(), option.rect.top() + (max(0, option.rect.height() - height) // 2))
         doc.drawContents(painter)
     painter.restore()
Ejemplo n.º 3
0
 def paint(self, painter, option, index):
     name = index.data(Qt.ItemDataRole.DisplayRole)
     sz = human_readable(index.data(Qt.ItemDataRole.UserRole))
     pmap = index.data(Qt.ItemDataRole.UserRole + 1)
     irect = option.rect.adjusted(0, 5, 0, -5)
     irect.setRight(irect.left() + 70)
     if pmap is None:
         pmap = QPixmap(
             current_container().get_file_path_for_processing(name))
         scaled, nwidth, nheight = fit_image(pmap.width(), pmap.height(),
                                             irect.width(), irect.height())
         if scaled:
             pmap = pmap.scaled(
                 nwidth,
                 nheight,
                 transformMode=Qt.TransformationMode.SmoothTransformation)
         index.model().setData(index, pmap, Qt.ItemDataRole.UserRole + 1)
     x, y = (irect.width() - pmap.width()) // 2, (irect.height() -
                                                  pmap.height()) // 2
     r = irect.adjusted(x, y, -x, -y)
     QStyledItemDelegate.paint(self, painter, option, empty_index)
     painter.drawPixmap(r, pmap)
     trect = irect.adjusted(irect.width() + 10, 0, 0, 0)
     trect.setRight(option.rect.right())
     painter.save()
     if option.state & QStyle.StateFlag.State_Selected:
         painter.setPen(
             QPen(option.palette.color(QPalette.ColorRole.HighlightedText)))
     painter.drawText(
         trect, Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft,
         name + '\n' + sz)
     painter.restore()
Ejemplo n.º 4
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, self.dummy_index)
     painter.save()
     painter.setClipRect(QRectF(option.rect))
     painter.translate(option.rect.topLeft())
     self.to_doc(index, option).drawContents(painter)
     painter.restore()
Ejemplo n.º 5
0
    def paint(self, painter, option, index):
        QStyledItemDelegate.paint(
            self, painter, option,
            empty_index)  # draw the hover and selection highlights
        name = str(index.data(Qt.ItemDataRole.DisplayRole) or '')
        cover = self.cover_cache.get(name, None)
        if cover is None:
            cover = self.cover_cache[name] = QPixmap()
            try:
                raw = current_container().raw_data(name, decode=False)
            except:
                pass
            else:
                try:
                    dpr = painter.device().devicePixelRatioF()
                except AttributeError:
                    dpr = painter.device().devicePixelRatio()
                cover.loadFromData(raw)
                cover.setDevicePixelRatio(dpr)
                if not cover.isNull():
                    scaled, width, height = fit_image(cover.width(),
                                                      cover.height(),
                                                      self.cover_size.width(),
                                                      self.cover_size.height())
                    if scaled:
                        cover = self.cover_cache[name] = cover.scaled(
                            int(dpr * width),
                            int(dpr * height),
                            transformMode=Qt.TransformationMode.
                            SmoothTransformation)

        painter.save()
        try:
            rect = option.rect
            rect.adjust(self.MARGIN, self.MARGIN, -self.MARGIN, -self.MARGIN)
            trect = QRect(rect)
            rect.setBottom(rect.bottom() - self.title_height)
            if not cover.isNull():
                dx = max(
                    0,
                    int((rect.width() -
                         int(cover.width() / cover.devicePixelRatio())) / 2.0))
                dy = max(
                    0,
                    rect.height() -
                    int(cover.height() / cover.devicePixelRatio()))
                rect.adjust(dx, dy, -dx, 0)
                painter.drawPixmap(rect, cover)
            rect = trect
            rect.setTop(rect.bottom() - self.title_height + 5)
            painter.setRenderHint(QPainter.RenderHint.TextAntialiasing, True)
            metrics = painter.fontMetrics()
            painter.drawText(
                rect,
                Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextSingleLine,
                metrics.elidedText(name, Qt.TextElideMode.ElideLeft,
                                   rect.width()))
        finally:
            painter.restore()
Ejemplo n.º 6
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     style = QApplication.style()
     # Ensure the cover is rendered over any selection rect
     style.drawItemPixmap(
         painter, option.rect,
         Qt.AlignmentFlag.AlignTop | Qt.AlignmentFlag.AlignHCenter,
         QPixmap(index.data(Qt.ItemDataRole.DecorationRole)))
Ejemplo n.º 7
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, empty_index)
     painter.save()
     try:
         self.do_paint(painter, option, index)
     except:
         pass
     painter.restore()
Ejemplo n.º 8
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     pal = option.palette
     color = pal.color(QPalette.ColorRole.HighlightedText if option.state & QStyle.StateFlag.State_Selected else QPalette.ColorRole.Text).name()
     text = '<div style="color:%s">%s</div>' % (color, index.data(RENDER_ROLE))
     st = QStaticText(text)
     st.setTextWidth(option.rect.width())
     painter.drawStaticText(option.rect.left() + self.MARGIN // 2, option.rect.top() + self.MARGIN // 2, st)
Ejemplo n.º 9
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, empty_index)
     painter.save()
     try:
         self.do_paint(painter, option, index)
     except:
         import traceback
         traceback.print_exc()
     painter.restore()
Ejemplo n.º 10
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     result = index.data(Qt.ItemDataRole.UserRole)
     is_hidden, result_before, result_text, result_after, show_leading_dot = self.result_data(
         result)
     if result_text is None:
         return
     painter.save()
     try:
         p = option.palette
         c = QPalette.ColorRole.HighlightedText if option.state & QStyle.StateFlag.State_Selected else QPalette.ColorRole.Text
         group = (QPalette.ColorGroup.Active if option.state
                  & QStyle.StateFlag.State_Active else
                  QPalette.ColorGroup.Inactive)
         c = p.color(group, c)
         painter.setPen(c)
         font = option.font
         if self.emphasize_text:
             emphasis_font = QFont(font)
             emphasis_font.setBold(True)
         else:
             emphasis_font = font
         flags = Qt.AlignmentFlag.AlignTop | Qt.TextFlag.TextSingleLine | Qt.TextFlag.TextIncludeTrailingSpaces
         rect = option.rect.adjusted(
             option.decorationSize.width() + 4 if is_hidden else 0, 0, 0, 0)
         painter.setClipRect(rect)
         before = re.sub(r'\s+', ' ', result_before)
         if show_leading_dot:
             before = '•' + before
         before_width = 0
         if before:
             before_width = painter.boundingRect(rect, flags,
                                                 before).width()
         after = re.sub(r'\s+', ' ', result_after.rstrip())
         after_width = 0
         if after:
             after_width = painter.boundingRect(rect, flags, after).width()
         ellipsis_width = painter.boundingRect(rect, flags, '...').width()
         painter.setFont(emphasis_font)
         text = re.sub(r'\s+', ' ', result_text)
         match_width = painter.boundingRect(rect, flags, text).width()
         if match_width >= rect.width() - 3 * ellipsis_width:
             efm = QFontMetrics(emphasis_font)
             if show_leading_dot:
                 text = '•' + text
             text = efm.elidedText(text, Qt.TextElideMode.ElideRight,
                                   rect.width())
             painter.drawText(rect, flags, text)
         else:
             self.draw_match(painter, flags, before, text, after, rect,
                             before_width, match_width, after_width,
                             ellipsis_width, emphasis_font, font)
     except Exception:
         import traceback
         traceback.print_exc()
     painter.restore()
Ejemplo n.º 11
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     style = QApplication.style()
     waiting = self.animator.is_running() and bool(index.data(Qt.ItemDataRole.UserRole))
     if waiting:
         rect = QRect(0, 0, self.spinner_width, self.spinner_width)
         rect.moveCenter(option.rect.center())
         self.animator.draw(painter, rect, self.color)
     else:
         # Ensure the cover is rendered over any selection rect
         style.drawItemPixmap(painter, option.rect, Qt.AlignmentFlag.AlignTop|Qt.AlignmentFlag.AlignHCenter,
             QPixmap(index.data(Qt.ItemDataRole.DecorationRole)))
Ejemplo n.º 12
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, index)
     try:
         charcode = int(index.data(Qt.ItemDataRole.UserRole))
     except (TypeError, ValueError):
         return
     painter.save()
     try:
         if charcode in non_printing:
             self.paint_non_printing(painter, option, charcode)
         else:
             self.paint_normal(painter, option, charcode)
     finally:
         painter.restore()
Ejemplo n.º 13
0
 def paint(self, painter, option, index):
     icon = index.data(Qt.ItemDataRole.DecorationRole)
     if icon and not icon.isNull():
         QStyledItemDelegate.paint(self, painter, option, QModelIndex())
         pw, ph = option.rect.width(), option.rect.height()
         scaled, w, h = fit_image(option.decorationSize.width(),
                                  option.decorationSize.height(), pw, ph)
         r = option.rect
         if pw > w:
             x = (pw - w) // 2
             r = r.adjusted(x, 0, -x, 0)
         if ph > h:
             y = (ph - h) // 2
             r = r.adjusted(0, y, 0, -y)
         painter.drawPixmap(r, icon.pixmap(w, h))
     else:
         QStyledItemDelegate.paint(self, painter, option, index)
Ejemplo n.º 14
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, empty_index)
     img = index.data(Qt.ItemDataRole.DecorationRole)
     if img:
         h = option.rect.height() - 4
         w = option.rect.width()
         if isinstance(img, QIcon):
             img = img.pixmap(h - 4, h - 4)
             dpr = img.devicePixelRatio()
         else:
             dpr = img.devicePixelRatio()
             scaled, nw, nh = fit_image(img.width(), img.height(), w, h)
             if scaled:
                 img = img.scaled(int(nw*dpr), int(nh*dpr), Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
         iw, ih = int(img.width()/dpr), int(img.height()/dpr)
         dx, dy = (option.rect.width() - iw) // 2, (option.rect.height() - ih) // 2
         painter.drawPixmap(option.rect.adjusted(dx, dy, -dx, -dy), img)
Ejemplo n.º 15
0
 def paint(self, painter, option, index):
     QStyledItemDelegate.paint(self, painter, option, empty_index)
     theme = index.data(Qt.ItemDataRole.UserRole)
     if not theme:
         return
     painter.save()
     pixmap = index.data(Qt.ItemDataRole.DecorationRole)
     if pixmap and not pixmap.isNull():
         rect = option.rect.adjusted(0, self.SPACING,
                                     COVER_SIZE[0] - option.rect.width(),
                                     -self.SPACING)
         painter.drawPixmap(rect, pixmap)
     if option.state & QStyle.StateFlag.State_Selected:
         painter.setPen(
             QPen(QApplication.instance().palette().highlightedText().color(
             )))
     bottom = option.rect.bottom() - 2
     painter.drawLine(0, bottom, option.rect.right(), bottom)
     if 'static-text' not in theme:
         theme['static-text'] = QStaticText(
             _('''
         <h1>{title}</h1>
         <p>by <i>{author}</i> with <b>{number}</b> icons [{size}]</p>
         <p>{description}</p>
         <p>Version: {version} Number of users: {usage}</p>
         <p><i>Right click to visit theme homepage</i></p>
         '''.format(
                 title=theme.get('title', _('Unknown')),
                 author=theme.get('author', _('Unknown')),
                 number=theme.get('number', 0),
                 description=theme.get('description', ''),
                 size=human_readable(theme.get('compressed-size', 0)),
                 version=theme.get('version', 1),
                 usage=theme.get('usage', 0),
             )))
     painter.drawStaticText(COVER_SIZE[0] + self.SPACING,
                            option.rect.top() + self.SPACING,
                            theme['static-text'])
     painter.restore()
Ejemplo n.º 16
0
 def paint(self, painter, option, index):
     option.font = self.rf
     option.textElideMode = self.em
     return QStyledItemDelegate.paint(self, painter, option, index)
Ejemplo n.º 17
0
    def paint(self, painter, option, index):
        QStyledItemDelegate.paint(
            self, painter, option,
            empty_index)  # draw the hover and selection highlights
        m = index.model()
        db = m.db
        try:
            book_id = db.id(index.row())
        except (ValueError, IndexError, KeyError):
            return
        if book_id in m.ids_to_highlight_set:
            painter.save()
            try:
                painter.setPen(self.highlight_color)
                painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
                painter.drawRoundedRect(option.rect, 10, 10,
                                        Qt.SizeMode.RelativeSize)
            finally:
                painter.restore()
        marked = db.data.get_marked(book_id)
        db = db.new_api
        cdata = self.cover_cache[book_id]
        device_connected = self.parent().gui.device_connected is not None
        on_device = device_connected and db.field_for('ondevice', book_id)

        emblem_rules = db.pref('cover_grid_icon_rules', default=())
        emblems = []
        if self.emblem_size > 0:
            mi = None
            for i, (kind, column, rule) in enumerate(emblem_rules):
                icon_name, mi = self.render_emblem(book_id, rule, i,
                                                   m.cover_grid_emblem_cache,
                                                   mi, db, m.formatter,
                                                   m.cover_grid_template_cache)
                if icon_name is not None:
                    for one_icon in filter(None,
                                           (i.strip()
                                            for i in icon_name.split(':'))):
                        pixmap = self.cached_emblem(m.cover_grid_bitmap_cache,
                                                    one_icon)
                        if pixmap is not None:
                            emblems.append(pixmap)
            if marked:
                emblems.insert(
                    0,
                    self.cached_emblem(m.cover_grid_bitmap_cache, ':marked',
                                       m.marked_icon))
            if on_device:
                emblems.insert(
                    0,
                    self.cached_emblem(m.cover_grid_bitmap_cache, ':ondevice'))

        painter.save()
        right_adjust = 0
        try:
            rect = option.rect
            rect.adjust(self.MARGIN, self.MARGIN, -self.MARGIN, -self.MARGIN)
            if self.emblem_size > 0:
                self.paint_emblems(painter, rect, emblems)
            orect = QRect(rect)
            trect = QRect(rect)
            if self.title_height != 0:
                rect.setBottom(rect.bottom() - self.title_height)
                trect.setTop(trect.bottom() - self.title_height + 5)
            if cdata is None or cdata is False:
                title = db.field_for('title', book_id, default_value='')
                authors = ' & '.join(
                    db.field_for('authors', book_id, default_value=()))
                painter.setRenderHint(QPainter.RenderHint.TextAntialiasing,
                                      True)
                painter.drawText(
                    rect,
                    Qt.AlignmentFlag.AlignCenter | Qt.TextFlag.TextWordWrap,
                    '%s\n\n%s' % (title, authors))
                if cdata is False:
                    self.render_queue.put(book_id)
                if self.title_height != 0:
                    self.paint_title(painter, trect, db, book_id)
            else:
                if self.animating is not None and self.animating.row(
                ) == index.row():
                    cdata = cdata.scaled(cdata.size() * self._animated_size)
                dpr = cdata.devicePixelRatio()
                cw, ch = int(cdata.width() / dpr), int(cdata.height() / dpr)
                dx = max(0, int((rect.width() - cw) / 2.0))
                dy = max(0, int((rect.height() - ch) / 2.0))
                right_adjust = dx
                rect.adjust(dx, dy, -dx, -dy)
                painter.drawPixmap(rect, cdata)
                if self.title_height != 0:
                    self.paint_title(painter, trect, db, book_id)
            if self.emblem_size > 0:
                return  # We dont draw embossed emblems as the ondevice/marked emblems are drawn in the gutter
            if marked:
                try:
                    p = self.marked_emblem
                except AttributeError:
                    p = self.marked_emblem = m.marked_icon.pixmap(48, 48)
                self.paint_embossed_emblem(p, painter, orect, right_adjust)

            if on_device:
                try:
                    p = self.on_device_emblem
                except AttributeError:
                    p = self.on_device_emblem = QIcon(I('ok.png')).pixmap(
                        48, 48)
                self.paint_embossed_emblem(p,
                                           painter,
                                           orect,
                                           right_adjust,
                                           left=False)
        finally:
            painter.restore()
Ejemplo n.º 18
0
 def paint(self, p, opt, idx):
     copy = QStyleOptionViewItem(opt)
     copy.showDecorationSelected = True
     if self.view.currentIndex() == idx:
         copy.state |= QStyle.StateFlag.State_HasFocus
     QStyledItemDelegate.paint(self, p, copy, idx)