Untitled
import sys from PySide2.QtCore import ( Qt, QAbstractListModel, QModelIndex, QSize, QRect, QPoint, Signal ) from PySide2.QtGui import ( QPixmap, QPainter, QColor, QFont, QBrush, QPalette, ) from PySide2.QtWidgets import ( QApplication, QMainWindow, QListView, QWidget, QVBoxLayout, QLabel, QHBoxLayout, QStyledItemDelegate, QStyle, QAbstractItemView ) class FileData(): def __init__(self, file_name, file_format, file_path, file_type, last_modified): self.file_name = file_name self.file_format = file_format self.file_path = file_path self.file_type = file_type self.last_modified = last_modified class FileModel(QAbstractListModel): def __init__(self, list_files=[]): super().__init__() self._files = list_files def rowCount(self, parent=QModelIndex()): return len(self._files) def data(self, index, role): if not index.isValid(): return None file_info = self._files[index.row()] if role == Qt.DisplayRole: return file_info.file_path elif role == Qt.UserRole: return file_info return None class FileDelegate(QStyledItemDelegate): cell_clicked = Signal(str, str) alt_clicked = Signal(object) def sizeHint(self, option, index): return QSize(302, 162) def paint(self, painter, option, index): file_info = index.model().data(index, Qt.UserRole) painter.save() # print (option.rect.x(), option.rect.y()) # print (option.rect.width(), option.rect.height()) if option.state & QStyle.State_Selected: file_cell_color = QColor(90, 200, 90) file_cell_pen = Qt.red elif option.state & QStyle.State_MouseOver: file_cell_color = QColor(90, 90, 90) file_cell_pen = Qt.green else: file_cell_color = QColor(63, 63, 63) file_cell_pen = Qt.NoPen file_cell = QRect( option.rect.x(), option.rect.y(), option.rect.width(), option.rect.height() ) asset_type_rect = QRect( option.rect.x() + 8, option.rect.y() + 10, 125, 12 ) file_name_rect = QRect( asset_type_rect.right() + 5, option.rect.y() + 12, 162, 10 ) self.thumb_rect = QRect( option.rect.x() + 8, asset_type_rect.bottom() + 9, 125, 125 ) file_format_rect = QRect( self.thumb_rect.right() + 5, asset_type_rect.bottom() + 14, 162, 10 ) file_last_modified_rect = QRect( self.thumb_rect.right() + 5, asset_type_rect.bottom() + 60, 162, 10 ) file_type_font = QFont("Arial", 11, QFont.Bold) file_font = QFont("Arial", 9) # paint: cell painter.setPen(file_cell_pen) painter.setBrush(file_cell_color) painter.drawRoundRect(file_cell, 10, 10) # paint: asset type painter.setPen(QColor(214, 145,85)) painter.setFont(file_type_font) painter.drawText(asset_type_rect, Qt.AlignRight | Qt.AlignVCenter, file_info.file_type) # paint: file name painter.setPen(Qt.white) painter.setFont(file_font) painter.drawText(file_name_rect, Qt.AlignLeft | Qt.AlignVCenter, file_info.file_name) # paint: thumb painter.setBrush(Qt.gray) painter.drawRect(self.thumb_rect) # paint: file format painter.setPen(Qt.green) painter.setFont(file_type_font) painter.drawText(file_format_rect, Qt.AlignLeft | Qt.AlignVCenter, file_info.file_format) # paint: last modified painter.setPen(Qt.white) painter.setFont(file_font) # painter.drawRect(file_last_modified_rect) painter.drawText(file_last_modified_rect, Qt.AlignLeft | Qt.AlignVCenter, file_info.last_modified) painter.restore() def editorEvent(self, event, model, option, index): if event.type() == event.MouseButtonPress: if self.thumb_rect.contains(event.pos()): file_info = index.model().data(index, Qt.UserRole) self.alt_clicked.emit(file_info) self.cell_clicked.emit(file_info.file_path, file_info.file_name) return super().editorEvent(event, model, option, index) class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() self.populate() def initUI(self): self.setWindowTitle("ListView + Model") self.setGeometry(4000, 100, 800, 500) # QListView self.list_view = QListView() self.list_view.setUniformItemSizes(True) self.list_view.setSelectionMode(QListView.SingleSelection) self.list_view.setResizeMode(QListView.Adjust) self.list_view.setFlow(QListView.LeftToRight) self.list_view.setMovement(QListView.Static) self.list_view.setLayoutMode(QListView.Batched) self.list_view.setWrapping(True) self.list_view.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel) self.list_view.setAutoScroll(False) self.delegate = FileDelegate() self.list_view.setItemDelegate(self.delegate) self.list_view.setViewMode(QListView.IconMode) self.list_view.setSpacing(10) # Signals self.delegate.cell_clicked.connect(self.item_clicked) self.delegate.alt_clicked.connect(self.alt_clicked) self.setCentralWidget(self.list_view) def populate(self): scene_files = [] file_type_list = ["ANIM", "RENDER", "MODEL", "RIG", "ALEMBIC"] for i in range(20): typ = file_type_list[i%len(file_type_list)] fd = FileData("Cena_{:03d}".format(i), ".mb", "C:/Projects/Maya/scenes/{}/Cena_{:03d}".format(typ, i), typ, "2024-03-20 16:00:00") scene_files.append(fd) self.model = FileModel(scene_files) self.list_view.setModel(self.model) def item_clicked(self, file_path, file_name): print (file_path) print (file_name) def alt_clicked(self, obj_): print (obj_.file_format) print(obj_.last_modified) print("") if __name__ == "__main__": app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_())
Leave a Comment