def sizeHint(self, option, index): k = "{}x{}".format(index.row(), index.column()) if "{}x{}".format(index.row(), index.column()) in self.last_bbox: s = self.last_bbox[k] # print "reusing ndx={} {}x{}".format(k,s.width(),s.height()) del self.last_bbox[k] return QSize(s.width(), s.height()) s = self.get_displayed_data(index) if s == None: return QSize(-1, -1) fm = QFontMetrics(option.font) w = self.table.columnWidth(index.column()) # if option.rect.width() > 0: # w = option.rect.width() size = fm.boundingRect(0, 0, w, 30000, Qt.AlignTop | Qt.AlignLeft | Qt.TextWordWrap, s, 0, []) # if s and len(s) > 50: # print("[{}] w={} / s.w={} s.h={} txt:{}".format(self.i,w,size.width(),size.height(),s)) # h = max( size.height(), self.last_bbox.height()) # print "using ndx={} {}x{}".format(k, size.width(),size.height()) return QSize(size.width(), size.height()) # FIXME hardcoded value
def refs_size(self, option, refs, skip_head): if not refs: return 0, 0 metrics = QFontMetrics(option.font) width = 0 height = 0 for ref in refs: if skip_head and posixpath.basename(ref) == 'HEAD': continue ref_text = git_api.parse_ref(ref)[0] text_rect = metrics.boundingRect(0, 0, 0, 0, Qt.AlignLeft | Qt.AlignTop, ref_text) text_rect.setWidth(text_rect.width() + self.ref_padding_x) text_rect.setHeight(text_rect.height() + self.ref_padding_y) width += text_rect.width() width += text_rect.height() * self.ref_arrow_ratio width += self.ref_spacing height = max(height, text_rect.height()) return width, height
def add_labels_internal(self, gl, render_state, draw_to_canvas, labels): ''' call to add a list of labels ''' text_paint = QPainter() if draw_to_canvas: text_paint.begin(self.bitmap) text_paint.setRenderHints(QPainter.Antialiasing) u = 0 v = 0 line_height = 0 for label in labels: ascent = 0 descent = 0 measured_text_width = 0 height = 0 width = 0 font_size = label.font_size while True: metrics = None if draw_to_canvas: mask = 0x000000FF b = (label.color >> 16) & mask g = (label.color >> 8) & mask r = label.color & mask ######################################################################## LINE CHANGED text_paint.setPen(QColor(0, 0, 0)) #text_paint.setPen(QColor(r, g, b)) # The value 0.75 is hard coded representing phone pixel density text_paint.setFont(QFont('Veranda', font_size * 0.75)) # Paint.ascent is negative, so negate it. metrics = text_paint.fontMetrics() else: # The value 0.75 is hard coded representing phone pixel density metrics = QFontMetrics(QFont('Veranda', font_size * 0.75)) ascent = math.ceil(metrics.ascent()) descent = math.ceil(metrics.descent()) measured_text_width = math.ceil(metrics.boundingRect(label.string).width()) height = int(ascent) + int(descent) width = int(measured_text_width) # If it's wider than the screen, try it again with a font size of 1 # smaller. font_size -= 1 if font_size < 0 or width < render_state.screen_width: break next_u = 0 # Is there room for this string on the current line? if u + width > self.strike_width: # No room, go to the next line: u = 0 next_u = width v += line_height line_height = 0 else: next_u = u + width line_height = max(line_height, height) if (v + line_height > self.strike_height) and draw_to_canvas: raise Exception("out of texture space.") v_base = v + ascent if draw_to_canvas: text_paint.drawText(int(u), int(v_base), label.string) label.set_texture_data(width, height, u, v + height, width, -height, self.texel_width, self.texel_height) u = next_u if draw_to_canvas: text_paint.end() return v + line_height
class DefaultCustomDelegate(QStyledItemDelegate): '''Delegate to do custom draw of the items''' memoized_size = {} def __init__(self, parent): '''Initialization''' QStyledItemDelegate.__init__(self, parent) self.bg_color = QColor('#000000') self.bg_alternate_color = QColor('#333333') self.new_bg_color = QColor('#0044dd') self.new_bg_alternate_color = QColor('#223399') self.user_color = QColor('#7AB4F5') self.time_color = QColor('#7AB4F5') self.replyto_color = QColor('#7AB4F5') self.text_color = QColor('#FFFFFF') self.separator_color = QColor('#000000') self.fsize = 1.0 self.fm = None self.minifm = None self.normFont = None self.miniFont = None # print os.path.join(os.path.dirname(__file__), # 'icons', 'reply.png') self.reply_icon = QPixmap(os.path.join(os.path.dirname(__file__), 'icons', 'reply.png')) # print dir(self.reply_icon) self.retweet_icon = QPixmap(os.path.join(os.path.dirname(__file__), 'icons', 'retweet.png')) self.geoloc_icon = QPixmap(os.path.join(os.path.dirname(__file__), 'icons', 'geoloc.png')) def doZoomRefresh(self): self.memoized_size.clear() self.fm = None self.minifm = None self.normFont = None self.miniFont = None def sizeHint(self, option, index): '''Custom size calculation of our items''' uid = to_str(index.data(role=IDROLE)) + 'x' + str(option.rect.width()) #Fix Bug #967 (sometime uid have some strange unicode chars ... ?) try: return self.memoized_size[uid] except: tweet = to_str(index.data(Qt.DisplayRole)) # One time is enought sizeHint need to be fast if not self.fm: self.normFont = QFont(option.font) self.normFont.setPointSizeF(option.font.pointSizeF() * self.fsize) self.fm = QFontMetrics(self.normFont) if not self.minifm: self.miniFont = QFont(option.font) self.miniFont.setPointSizeF(option.font.pointSizeF() * 0.8 * self.fsize) self.minifm = QFontMetrics(self.miniFont) height = self.fm.boundingRect( 0, 0, option.rect.width() - 75, 800, int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), tweet, ).height() reply_text = to_str(index.data(role=REPLYTEXTROLE)) if reply_text: height += self.minifm.boundingRect( 0, 0, option.rect.width() - 75, 800, int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), reply_text, ).height() + 5 height += self.minifm.boundingRect( 0, 0, option.rect.width() - 75, 800, int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), 'LpqAT', ).height() height += 10 # Spacer if height < 70: height = 70 self.memoized_size[uid] = QSize(option.rect.width(), height) return self.memoized_size[uid] def paint( self, painter, option, index, ): '''Paint our tweet''' (x1, y1, x2, y2) = option.rect.getCoords() # Ugly hack ? if y1 < 0 and y2 < 0: return # Init Font : One time is enough if not self.fm: self.normFont = QFont(option.font) self.normFont.setPointSizeF(option.font.pointSizeF() * self.fsize) if not self.minifm: self.miniFont = QFont(option.font) self.miniFont.setPointSizeF(option.font.pointSizeF() * 0.8 * self.fsize) # Query data tweet = to_str(index.data(Qt.DisplayRole)) screenname = to_str(index.data(SCREENNAMEROLE)) retweet_of = index.data(RETWEETOFROLE) timestamp = to_str(index.data(role=TIMESTAMPROLE)) reply_name = to_str(index.data(role=REPLYTOSCREENNAMEROLE)) reply_text = to_str(index.data(role=REPLYTEXTROLE)) is_new = index.data(role=ISNEWROLE) painter.save() # Draw alternate ? if index.row() % 2 == 0: color = self.bg_color else: color = self.bg_alternate_color painter.fillRect(option.rect, color) # highlight selected items if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.highlight()) # Draw icon icon = index.data(Qt.DecorationRole) if type(icon) == QPixmap: try: painter.drawPixmap(x1 + 10, y1 + 10, 50, 50, icon) except Exception: logging.exception("Drawing icon") # Draw screenname painter.setFont(self.miniFont) painter.setPen(self.user_color) nrect = painter.drawText(option.rect.adjusted(70, 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft), screenname) # Reply icon if reply_name: painter.drawPixmap(x1 + 74 + nrect.width(), y1, 26, 26, self.reply_icon) painter.setFont(self.miniFont) painter.setPen(self.replyto_color) painter.drawText(option.rect.adjusted(109 + nrect.width(), 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft), reply_name) # Retweet icon if retweet_of: painter.drawPixmap(x1 + 74 + nrect.width(), y1, 32, 32, self.retweet_icon) painter.setFont(self.miniFont) painter.setPen(self.replyto_color) painter.drawText(option.rect.adjusted(110 + nrect.width(), 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft), retweet_of.user.screen_name) # Draw tweet painter.setFont(self.normFont) painter.setPen(self.text_color) new_rect = painter.drawText(option.rect.adjusted(70, nrect.height() + 5, -4, 0), int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), tweet) # Draw Timeline painter.setFont(self.miniFont) painter.setPen(self.time_color) painter.drawText(option.rect.adjusted(70, 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignRight), timestamp) # Draw reply if reply_text: painter.setFont(self.miniFont) painter.setPen(self.replyto_color) painter.drawText(option.rect.adjusted(70, nrect.height() + new_rect.height() + 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), reply_text) # Draw line separator painter.setPen(self.separator_color) painter.drawLine(x1, y2, x2, y2) #Use a little tips to say that's a new tweet if is_new: painter.fillRect(x1,y1,8,y2, self.new_bg_alternate_color) # restore painter painter.restore()
class DefaultCustomDelegate(QStyledItemDelegate): '''Delegate to do custom draw of the items''' memoized_size = {} def __init__(self, parent): '''Initialization''' QStyledItemDelegate.__init__(self, parent) self.bg_color = QColor('#000000') self.bg_alternate_color = QColor('#333333') self.new_bg_color = QColor('#0044dd') self.new_bg_alternate_color = QColor('#223399') self.user_color = QColor('#7AB4F5') self.time_color = QColor('#7AB4F5') self.replyto_color = QColor('#7AB4F5') self.text_color = QColor('#FFFFFF') self.separator_color = QColor('#000000') self.fsize = 1.0 self.fm = None self.minifm = None self.normFont = None self.miniFont = None # print os.path.join(os.path.dirname(__file__), # 'icons', 'reply.png') self.reply_icon = QPixmap( os.path.join(os.path.dirname(__file__), 'icons', 'reply.png')) # print dir(self.reply_icon) self.retweet_icon = QPixmap( os.path.join(os.path.dirname(__file__), 'icons', 'retweet.png')) self.geoloc_icon = QPixmap( os.path.join(os.path.dirname(__file__), 'icons', 'geoloc.png')) def doZoomRefresh(self): self.memoized_size.clear() self.fm = None self.minifm = None self.normFont = None self.miniFont = None def sizeHint(self, option, index): '''Custom size calculation of our items''' uid = to_str(index.data(role=IDROLE)) + 'x' + str(option.rect.width( )) #Fix Bug #967 (sometime uid have some strange unicode chars ... ?) try: return self.memoized_size[uid] except: tweet = to_str(index.data(Qt.DisplayRole)) # One time is enought sizeHint need to be fast if not self.fm: self.normFont = QFont(option.font) self.normFont.setPointSizeF(option.font.pointSizeF() * self.fsize) self.fm = QFontMetrics(self.normFont) if not self.minifm: self.miniFont = QFont(option.font) self.miniFont.setPointSizeF(option.font.pointSizeF() * 0.8 * self.fsize) self.minifm = QFontMetrics(self.miniFont) height = self.fm.boundingRect( 0, 0, option.rect.width() - 75, 800, int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), tweet, ).height() reply_text = to_str(index.data(role=REPLYTEXTROLE)) if reply_text: height += self.minifm.boundingRect( 0, 0, option.rect.width() - 75, 800, int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), reply_text, ).height() + 5 height += self.minifm.boundingRect( 0, 0, option.rect.width() - 75, 800, int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), 'LpqAT', ).height() height += 10 # Spacer if height < 70: height = 70 self.memoized_size[uid] = QSize(option.rect.width(), height) return self.memoized_size[uid] def paint( self, painter, option, index, ): '''Paint our tweet''' (x1, y1, x2, y2) = option.rect.getCoords() # Ugly hack ? if y1 < 0 and y2 < 0: return # Init Font : One time is enough if not self.fm: self.normFont = QFont(option.font) self.normFont.setPointSizeF(option.font.pointSizeF() * self.fsize) if not self.minifm: self.miniFont = QFont(option.font) self.miniFont.setPointSizeF(option.font.pointSizeF() * 0.8 * self.fsize) # Query data tweet = to_str(index.data(Qt.DisplayRole)) screenname = to_str(index.data(SCREENNAMEROLE)) retweet_of = index.data(RETWEETOFROLE) timestamp = to_str(index.data(role=TIMESTAMPROLE)) reply_name = to_str(index.data(role=REPLYTOSCREENNAMEROLE)) reply_text = to_str(index.data(role=REPLYTEXTROLE)) is_new = index.data(role=ISNEWROLE) painter.save() # Draw alternate ? if index.row() % 2 == 0: color = self.bg_color else: color = self.bg_alternate_color painter.fillRect(option.rect, color) # highlight selected items if option.state & QStyle.State_Selected: painter.fillRect(option.rect, option.palette.highlight()) # Draw icon icon = index.data(Qt.DecorationRole) if type(icon) == QPixmap: try: painter.drawPixmap(x1 + 10, y1 + 10, 50, 50, icon) except Exception: logging.exception("Drawing icon") # Draw screenname painter.setFont(self.miniFont) painter.setPen(self.user_color) nrect = painter.drawText(option.rect.adjusted(70, 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft), screenname) # Reply icon if reply_name: painter.drawPixmap(x1 + 74 + nrect.width(), y1, 26, 26, self.reply_icon) painter.setFont(self.miniFont) painter.setPen(self.replyto_color) painter.drawText( option.rect.adjusted(109 + nrect.width(), 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft), reply_name) # Retweet icon if retweet_of: painter.drawPixmap(x1 + 74 + nrect.width(), y1, 32, 32, self.retweet_icon) painter.setFont(self.miniFont) painter.setPen(self.replyto_color) painter.drawText( option.rect.adjusted(110 + nrect.width(), 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft), retweet_of.user.screen_name) # Draw tweet painter.setFont(self.normFont) painter.setPen(self.text_color) new_rect = painter.drawText( option.rect.adjusted(70, nrect.height() + 5, -4, 0), int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), tweet) # Draw Timeline painter.setFont(self.miniFont) painter.setPen(self.time_color) painter.drawText(option.rect.adjusted(70, 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignRight), timestamp) # Draw reply if reply_text: painter.setFont(self.miniFont) painter.setPen(self.replyto_color) painter.drawText( option.rect.adjusted(70, nrect.height() + new_rect.height() + 5, -4, -9), int(Qt.AlignTop) | int(Qt.AlignLeft) | int(Qt.TextWordWrap), reply_text) # Draw line separator painter.setPen(self.separator_color) painter.drawLine(x1, y2, x2, y2) #Use a little tips to say that's a new tweet if is_new: painter.fillRect(x1, y1, 8, y2, self.new_bg_alternate_color) # restore painter painter.restore()