def __init__(self): super().__init__() self.setAcceptDrops(True) self.setDragEnabled(True) self.directory = "" self.icon_provider = QtWidgets.QFileIconProvider()
def create_child_items_from_data(self, parent_item, data): for item_data in data: child_item = TreeItem() icon = QtGui.QPixmap(icon_path+"/blank.svg") if item_data.get("data").get("category") in ["node", "deleted_node"]: icon = hou.ui.createQtIcon(hou.nodeType(item_data.get("data").get("icon_type")).icon()).pixmap(50,50) elif item_data.get("data").get("category") == "webUrl": icon = QtGui.QPixmap(icon_path+"/internet.svg") elif item_data.get("data").get("category") == "folder": icon = QtGui.QPixmap(icon_path+"/folder.svg") elif item_data.get("data").get("category") == "file": file_path = item_data.get("data").get("path").replace("file:///", "") if os.path.exists(file_path): file_info = QtCore.QFileInfo(file_path) icon = QtWidgets.QFileIconProvider().icon(file_info) size = QtCore.QSize(1,1) icon = icon.pixmap(icon.actualSize(size)) child_item.setIcon(QtGui.QPixmap(icon)) child_item.setData(item_data.get("data")) child_item.setText(item_data.get("text")) color = item_data.get("data").get("color") if item_data.get("data").get("category") != "folder": child_item.setDropEnabled(False) brush = QtGui.QBrush() brush.setColor(QtGui.QColor(color[0]*255,color[1]*255,color[2]*255)) child_item.setForeground(brush) parent_item.appendRow(child_item) if len(item_data["children"]) != 0: self.create_child_items_from_data(child_item, item_data["children"][0])
def __init__(self, path): super().__init__() self.path = path self.file_name = os.path.split(path)[-1] self._full_path = path self.fileInfo = QtCore.QFileInfo(path) self.setText(self.fileInfo.fileName()) icon_provider = QtWidgets.QFileIconProvider() icon = icon_provider.icon(self.fileInfo) self.setIcon(icon)
def load_project_structure_(file_dict, tree): """ Load Project structure tree :param startpath: :param tree: :return: """ icon_provider = QtWidgets.QFileIconProvider() file_icon = icon_provider.icon(QtWidgets.QFileIconProvider.File) folder_icon = icon_provider.icon(QtWidgets.QFileIconProvider.Folder) for key, value in file_dict.items(): if isinstance(value, dict): parent_itm = QtWidgets.QTreeWidgetItem(tree, [key]) load_project_structure_(value, parent_itm) parent_itm.setIcon(0, folder_icon) else: for file in value: parent_itm = QtWidgets.QTreeWidgetItem(tree, [file]) parent_itm.setIcon(0, file_icon)
def add_file(self, filename): """ Add a file or directory to this widget. """ filenames = [] for index in range(self.count()): filenames.append(self.item(index).filename) if filename not in filenames: if not os.access(filename, os.R_OK): Alert(self.common, strings._("not_a_readable_file").format(filename)) return fileinfo = QtCore.QFileInfo(filename) ip = QtWidgets.QFileIconProvider() icon = ip.icon(fileinfo) if os.path.isfile(filename): size_bytes = fileinfo.size() size_readable = self.common.human_readable_filesize(size_bytes) else: size_bytes = self.common.dir_size(filename) size_readable = self.common.human_readable_filesize(size_bytes) # Create a new item item = QtWidgets.QListWidgetItem() item.setIcon(icon) item.size_bytes = size_bytes # Item's filename attribute and size labels item.filename = filename item_size = QtWidgets.QLabel(size_readable) item_size.setStyleSheet( self.common.gui.css["share_file_list_item_size"]) item.basename = os.path.basename(filename.rstrip("/")) # Use the basename as the method with which to sort the list metrics = QtGui.QFontMetrics(item.font()) elided = metrics.elidedText(item.basename, QtCore.Qt.ElideRight, self.sizeHint().width()) item.setData(QtCore.Qt.DisplayRole, elided) # Item's delete button def delete_item(): itemrow = self.row(item) self.takeItem(itemrow) self.files_updated.emit() item.item_button = QtWidgets.QPushButton() item.item_button.setDefault(False) item.item_button.setFlat(True) item.item_button.setIcon( QtGui.QIcon( GuiCommon.get_resource_path("images/file_delete.png"))) item.item_button.clicked.connect(delete_item) item.item_button.setSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) # Item info widget, with a white background item_info_layout = QtWidgets.QHBoxLayout() item_info_layout.setContentsMargins(0, 0, 0, 0) item_info_layout.addWidget(item_size) item_info_layout.addWidget(item.item_button) item_info = QtWidgets.QWidget() item_info.setObjectName("item-info") item_info.setLayout(item_info_layout) # Create the item's widget and layouts item_hlayout = QtWidgets.QHBoxLayout() item_hlayout.addStretch() item_hlayout.addWidget(item_info) widget = QtWidgets.QWidget() widget.setLayout(item_hlayout) item.setSizeHint(widget.sizeHint()) self.addItem(item) self.setItemWidget(item, widget) self.files_updated.emit()
def dropEvent(self, event): if(event.source() == self): super(TreeView, self).dropEvent(event) else: data_texts = list() if event.mimeData().hasUrls(): data_texts = QtCore.QUrl.toStringList(event.mimeData().urls()) else: data_texts = event.mimeData().text().split() try: for path in data_texts: text = path item = TreeItem() item.setDropEnabled(False) category = "node" note = "" session_id = "" color = (0.8, 0.8, 0.8) icon_type = "" icon = self.icons["blank"] if "file://" in text and event.mimeData().hasUrls(): text = QtCore.QUrl(path).fileName() file_info = QtCore.QFileInfo(text) icon = QtWidgets.QFileIconProvider().icon(file_info) size = QtCore.QSize(1,1) icon = icon.pixmap(icon.actualSize(size)) category = "file" elif event.mimeData().hasUrls(): category = "webUrl" icon = self.icons["internet"] else: h_item = hou.item(text) h_session_id = h_item.sessionId() h_item_type = h_item.networkItemType() session_id = str(h_session_id) valid_types = [hou.networkItemType.Node, hou.networkItemType.StickyNote, hou.networkItemType.NetworkBox] if h_item_type == valid_types[0]: category = "node" node = hou.node(text) self.connect_node(node) note = node.comment() color = node.color().rgb() try: icon = hou.ui.createQtIcon(node.type().icon()) icon_type = node.type().nameWithCategory() except: icon_type = hou.sopNodeTypeCategory().nodeTypes()["null"].nameWithCategory() icon = hou.ui.createQtIcon(hou.sopNodeTypeCategory().nodeTypes()["null"].icon()) elif h_item_type == valid_types[1]: category = "stickynote" stickynote = hou.itemBySessionId(h_item_type, h_session_id) note = stickynote.text() color = stickynote.color().rgb() elif h_item_type == valid_types[2]: continue else: continue item.setIcon(icon) brush = QtGui.QBrush() brush.setColor(QtGui.QColor(color[0]*255,color[1]*255,color[2]*255)) item.setForeground(brush) data = {"note": note, "path": path, "category": category, "icon_type": icon_type, "session_id": session_id, "color": color} item.setData(data, QtCore.Qt.UserRole + 1) item.setText(text) self.model().appendRow(item) except: return False event.accept()
import abc import json from json import JSONEncoder import logging import os import shutil import subprocess from functools import partial from PySide2 import QtWidgets, QtCore, QtGui from PySide2.QtCore import Signal from libs.consts import * ICON_PROVIDER = QtWidgets.QFileIconProvider() log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) _drop_message = None class FileItem: """ This is the (Model) class of storing information about all file paths in the file browsers and favwidgets. QTableWidgetItem, QTreeWidgetItem and QListWidgetItems (View) are only used for displaying this data. When dragging an item from the Browser or FavWidget, we pass the FileItem object and it is up to the receiving browser on how to display data from that object. A reference to the file item is stored in the (View) object's data property.
autoCheckForUpdates=settings["autoCheckForUpdates"]) def openHelp() -> None: webbrowser.open_new( "https://github.com/martinet101/SomePythonThings-Zip-Manager/wiki") def getExtension(file) -> str: if (file.split('.') == 1): return 'none' else: return (file.split('.'))[-1] fileIconProvider = QtWidgets.QFileIconProvider() cachedIcons = {} def getFileIcon(file: str, skipCacheOnExe: bool = False) -> QtGui.QIcon: ext = getExtension(file).lower() if (ext[-1] == "/"): ext = "folder" if ext in cachedIcons and (ext != "exe" or skipCacheOnExe): return cachedIcons[ext] else: fileInfo = QtCore.QFileInfo(file) icon = fileIconProvider.icon(fileInfo) if icon.isNull(): icon = QtGui.QIcon( fileIconProvider.icon(fileInfo).pixmap(48, 48).scaledToHeight(