def __init__(self, view, asset_version): super(TreeItem, self).__init__() self._view = view self._asset_version = asset_version self.signal = ItemSignal(self) self.setEditable(False) self._thumnbail_default = QtGui.QImage() default_thumb = os.environ["FTRACK_SERVER"] + "/img/thumbnail2.png" self._thumnbail_default.loadFromData( urllib.urlopen(default_thumb).read()) # initiate data self.setData(None, self.asset_version_role) self.setData(None, self.is_button_role) self.setData(None, self.asset_type_role) self.setData(None, self.name_role) self.setData(None, self.version_nb_role) self.setData(None, self.date_role) self.setData(None, self.is_edited_role) self.setData(None, self.publisher_role) self.setData(None, self.comment_role) self.setData(None, self.is_locked_role) self.setData(None, self.is_available_role) self.setData(None, self.location_role) self.setData(None, self.thumbnail_role)
def set_version(self): # tuple_editor = self._asset_version.editor() # if tuple_editor is not None: # user_edit, date_edit = tuple_editor # self.setData(date_edit, self.date_role) # self.setData(user_edit.getName(), self.publisher_role) # self.setData(True, self.is_edited_role) # else: self.setData(self._asset_version.getDate(), self.date_role) self.setData(self._asset_version.getOwner(), self.publisher_role) self.setData(False, self.is_edited_role) self.setData(self._asset_version.get('comment'), self.comment_role) location = self._asset_version.getComponent('nukescript').getLocation() if location: location = location.getName() else: location = 'unmanaged' self.setData([location], self.location_role) # if not self._asset_version.is_available: # self.setDragEnabled(False) thumbnail = self._asset_version.getThumbnail() if thumbnail: image_path = QtGui.QImage() image_path.loadFromData(urllib.urlopen(thumbnail).read()) else: image_path = self._thumnbail_default self.setData(image_path, self.thumbnail_role) self._emit_asset_regenerated()
def paint(self, painter, option, index): painter.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.TextAntialiasing) padding_left = option.rect.left( ) + self._padding_item["left"] + self._padding_content["left"] padding_right = self._padding_item["right"] + self._padding_content[ "right"] padding_top = option.rect.top() + self._padding_item["top"] is_btn = index.data(TreeItem.is_button_role) if is_btn: text_btn = "show more versions..." padding_top += self._fm_desc.height() padding_left_btn = option.rect.width() - option.rect.width() * 0.5 padding_left_btn -= self._fm_desc.width(text_btn) * 0.5 self._button_rect = QtCore.QRect(option.rect.left(), 0, option.rect.width(), option.rect.height()) self.draw_button(painter, text_btn, padding_left_btn, padding_top, index) else: asset_type = index.data(TreeItem.asset_type_role) name = index.data(TreeItem.name_role) version_nb = index.data(TreeItem.version_nb_role) publisher = index.data(TreeItem.publisher_role) date = index.data(TreeItem.date_role) is_edited = index.data(TreeItem.is_edited_role) comment = index.data(TreeItem.comment_role) locked = index.data(TreeItem.is_locked_role) thumbnail = index.data(TreeItem.thumbnail_role) locations = index.data(TreeItem.location_role) available = index.data(TreeItem.is_available_role) main_color = QtGui.QColor() main_color.setNamedColor('#333') legend_color = QtGui.QColor() legend_color.setNamedColor(self._view.asset_color(asset_type)) # Get status if self._is_top_asset_version(index): if available is None: background = self._background_regenerated elif available: background = self._background else: background = self._background_not_available background_comment = self._background_comment size_thumbnail = self._thumnbail_size else: if available is None: background = self._background_regenerated elif available: background = self._background.lighter(111) else: background = self._background_child_not_available background_comment = self._background_comment.lighter(111) size_thumbnail = self._thumnbail_child_size background_comment = main_color background = QtGui.QColor() background.setNamedColor('#222') # Draw background if option.state & QtGui.QStyle.State_Selected: painter.setPen( QtGui.QPen(QtCore.Qt.lightGray, 2, QtCore.Qt.SolidLine)) else: painter.setPen(QtGui.QPen(background, 0, QtCore.Qt.SolidLine)) painter.setBrush(background) adjusted_rect = option.rect.adjusted( self._padding_item["left"], self._padding_item["top"], -self._padding_item["right"], -self._padding_item["top"] - self._padding_item["bottom"]) painter.drawRoundedRect(adjusted_rect, 3, 3) # Draw type type_indicator_rect = QtCore.QRect(adjusted_rect.left() + 3, adjusted_rect.top() + 3, self._type_indicator_width - 6, adjusted_rect.height() - 6) painter.setPen(QtGui.QPen(legend_color, 0, QtCore.Qt.SolidLine)) painter.setBrush(legend_color) # painter.drawRoundedRect(type_indicator_rect, 3, 3) padding_left += self._type_indicator_width padding_top += self._padding_content["top"] # Draw thumbnail if necessary if self._show_thumbnail: if thumbnail is None: pixmap = QtGui.QImage(self._thumnbail_default) else: pixmap = QtGui.QImage(thumbnail) pixmap_scaled = pixmap.scaled(size_thumbnail, QtCore.Qt.KeepAspectRatio) thumbnail_rect = QtCore.QRect(padding_left, padding_top, pixmap_scaled.width(), pixmap_scaled.height()) painter.setPen( QtGui.QPen(self._background.darker(190), 0.5, QtCore.Qt.SolidLine)) painter.setBrushOrigin(padding_left, padding_top) painter.setBrush(pixmap_scaled) painter.drawRoundedRect(thumbnail_rect, 3, 3) # If it's a top asset, we draw the legend if self._is_top_asset_version(index): padding_top_rect = padding_top + thumbnail_rect.height( ) - self._fm_desc.height() type_rect = QtCore.QRect( padding_left, padding_top_rect - 6, self._fm_desc.width(asset_type) + 10, self._fm_desc.height() + 6) painter.setBrush(legend_color) painter.drawRoundedRect(type_rect, 3, 3) if legend_color.lightness() < 127.5: legend_text_color = legend_color.lighter(190) else: legend_text_color = legend_color.darker(190) painter.setPen( QtGui.QPen(legend_text_color, 0, QtCore.Qt.SolidLine)) padding_top_legend = padding_top + thumbnail_rect.height( ) - 6 painter.drawText(padding_left + 2, padding_top_legend, asset_type) padding_left += size_thumbnail.width() + 10 # Draw version number if self._is_top_asset_version(index): color_number = self._name_color else: color_number = self._name_color.darker(130) painter.setPen(QtGui.QPen(color_number, 1, QtCore.Qt.SolidLine)) painter.setFont(self._font_name) version_nb_str = "v%02d" % version_nb painter.drawText( option.rect.right() - padding_right - self._fm_name.width(version_nb_str), padding_top + self._fm_name.height(), version_nb_str) # Draw name asset if necessary if self._is_top_asset_version(index): padding_top += self._fm_name.height() painter.drawText(padding_left, padding_top, name) padding_top += self._inter_text # If publisher is None, that means that the asset is being # regenerated if publisher != None: # Draw publisher painter.setPen( QtGui.QPen(self._desc_color, 1, QtCore.Qt.SolidLine)) painter.setFont(self._font_desc) label = "published by " if not is_edited else "edited by " padding_top += self._fm_desc.height() painter.drawText(padding_left, padding_top, label + publisher.getName()) # Draw Date painter.setPen( QtGui.QPen(self._owner_color, 1, QtCore.Qt.SolidLine)) padding_top += self._fm_desc.height() + self._inter_text self.draw_date(painter, date, padding_left, padding_top, index) self._date_rect = QtCore.QRect( padding_left, padding_top - self._fm_desc.height() - option.rect.top(), option.rect.width() - padding_left - 10, self._fm_desc.height() + 10) # # Draw Location if necessary # if not available: # locations_info = "Locations: " # if len(locations) > 0: # locations_info += ", ".join(locations) # else: # locations_info += "Hmmm... nowhere?" # painter.setPen( # QtGui.QPen(QtCore.Qt.red, 1, QtCore.Qt.SolidLine)) # padding_top += self._fm_desc.height() + self._inter_text # painter.drawText(padding_left, padding_top, locations_info) # Draw comment painter.setPen( QtGui.QPen(background_comment, 0, QtCore.Qt.SolidLine)) painter.setBrush(background_comment) padding_top += self._inter_text * 2 r_comment, r_comment_text = self._bounding_boxes( comment, option.rect.width(), padding_left, padding_top) painter.drawRoundedRect(r_comment, 3, 3) painter.setPen( QtGui.QPen(self._comment_color, 1, QtCore.Qt.SolidLine)) painter.drawText(r_comment_text, self._comment_flags, comment) else: # Draw loading asset... painter.setPen( QtGui.QPen(self._desc_color, 1, QtCore.Qt.SolidLine)) painter.setFont(self._font_desc) padding_top += self._fm_desc.height() painter.drawText(padding_left, padding_top, "Loading...") # Get the width to update the sizeHint self._width = option.rect.width()
def __init__(self, parent, minimum_width=200, show_thumbnail=True): super(TreeDelegateStyle, self).__init__(parent) self._view = parent self._minimum_width = minimum_width self._show_thumbnail = show_thumbnail self._thumnbail_default = QtGui.QImage() default_thumb = os.environ["FTRACK_SERVER"] + "/img/thumbnail2.png" self._thumnbail_default.loadFromData( urllib.urlopen(default_thumb).read()) # Fonts... self._font_name = QtGui.QFont() self._font_name.setPixelSize(12) self._font_name.setWeight(QtGui.QFont.Bold) self._fm_name = QtGui.QFontMetrics(self._font_name) self._font_desc = QtGui.QFont() self._font_desc.setPixelSize(11) self._font_desc.setWeight(QtGui.QFont.Normal) self._fm_desc = QtGui.QFontMetrics(self._font_desc) # Sizes... self._thumnbail_size = QtCore.QSize(150, 100) self._thumnbail_child_size = QtCore.QSize(96, 64) self._type_indicator_width = 12 # Colors... self._background = QtGui.QColor(68, 68, 68) self._background_regenerated = QtGui.QColor(20, 20, 20) self._background_comment = QtGui.QColor(58, 58, 58) self._name_color = QtGui.QColor(195, 207, 164) self._desc_color = QtGui.QColor(240, 240, 240) self._owner_color = QtGui.QColor(200, 200, 200) self._comment_color = QtGui.QColor(240, 240, 240) self._btn_color = QtGui.QColor(255, 255, 255, 80) self._btn_color_hover = QtGui.QColor(255, 255, 255, 200) self._btn_color_pressed = QtGui.QColor(255, 255, 255, 255) # Flags... self._comment_flags = QtCore.Qt.TextWordWrap | QtCore.Qt.AlignLeft | QtCore.Qt.AlignTop # Paddings... self._padding_item = dict(left=3, top=3, right=3, bottom=0) self._padding_content = dict(left=5, top=5, right=5, bottom=10) self._padding_comment = 5 self._inter_text = 5 self._inter_item = 3 self._space_before_btn = 8 # method... self._width = self._minimum_width # Keep the QRect of the "show previous versions" button if necessary self._button_rect = None # Keep the QRect of the "date" to show the real date in hover mode self._date_rect = None # Hover state self._hover_state = dict(no_hover=0, btn_hover=1, btn_pressed=2, date_hover=3) # Keep in memory the index under the mouse self._current_index = None