Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
8.1 kB
2
Indexable
Never
from PySide2 import QtCore, QtWidgets, QtGui
import os, time
from PySide2.QtCore import Qt


class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, data):
        super(TableModel, self).__init__()
        self._data = data

    def data(self, index, role):
        if role == Qt.DisplayRole:
            # See below for the nested-list data structure.
            # .row() indexes into the outer list,
            # .column() indexes into the sub-list
            return self._data[index.row()][index.column()]

    def rowCount(self, index):
        # The length of the outer list.
        return len(self._data)

    def columnCount(self, index):
        # The following takes the first sub-list, and returns
        # the length (only works if all rows are an equal length)
        return len(self._data[0])

def getRecentHipFiles():
    userdir = hou.getenv('HOUDINI_USER_PREF_DIR')
    file = os.path.join(userdir, "file.history")
    
    validfiles = []
    with open(file) as f:
        lines = f.readlines()
        for line in lines:
            newline = line.strip()
            if newline == "}":
                break;
                
            if newline != "{" and newline != "HIP":
                if os.path.exists(newline):
                    validfiles.append(newline)
            
        
    validfiles.reverse()
    return validfiles
    


def diagonalFromWidthHeight(width, height):
    return math.sqrt(width * width + height * height)
    
    

class VerticalLineDelegate(QtWidgets.QStyledItemDelegate):
    def paint(self, painter, option, index):
        super(VerticalLineDelegate, self).paint(painter, option, index)
        line = QtCore.QLine(option.rect.topRight(), option.rect.bottomRight())
        painter.save()
        color = QtGui.QColor(50, 50, 50)
        painter.setPen(QtGui.QPen(color, 1))
        painter.drawLine(line)
        painter.restore()



class FileTableWidget(QtWidgets.QTableWidget):
    allFiles = []
    
    def __init__(self, parent=None):
        super(FileTableWidget, self).__init__(parent)
        self.installEventFilter(self)
        
        self.setColumnCount(2)
        self.setMinimumHeight(5)
        self.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
        self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows);
        self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
        self.setAlternatingRowColors(True)
        self.setShowGrid(False)
        delegate = VerticalLineDelegate(self)
        self.setItemDelegate(delegate)
        
        hh = self.horizontalHeader()
        hh.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
        hh.hide()
        
        vh = self.verticalHeader()
        vh.setDefaultSectionSize(hou.ui.scaledSize(10))
        vh.setMinimumSectionSize(hou.ui.scaledSize(20))
        vh.setMaximumWidth(hou.ui.scaledSize(28))
        vh.setSectionResizeMode(QtWidgets.QHeaderView.Fixed)

        files = getRecentHipFiles()
        filedates = [time.ctime(os.path.getmtime(file)) for file in files]

        for i in range(1, 16):
            if i > len(files):
                files.append("")
                filedates.append("")
                
        self.allFiles = files
        for index, file in enumerate(files):
            self.insertRow(index)
            self.setRowHeight(index, hou.ui.scaledSize(10))

            filename = QtWidgets.QTableWidgetItem(file)
            filename.setFlags(filename.flags() & ~QtCore.Qt.ItemIsEditable)
            date = QtWidgets.QTableWidgetItem(filedates[index])
            date.setFlags(date.flags() & ~QtCore.Qt.ItemIsEditable)
            self.setItem(index, 0, filename)
            self.setItem(index, 1, date)
            
            

    def getSelectedFile(self):
        row = self.currentRow()
        return self.allFiles[row]
        
        

    def askLoadingFile(self, file):
        return hou.ui.displayMessage("Do you want to load?\n" + file, buttons=("Yes", "No")) == 0
        

        
    def loadSelectedFile(self):
        file = self.getSelectedFile()
        confirmed = self.askLoadingFile(file)
        if confirmed:
            hou.hipFile.load(file)
            self.parent().close()
        

        
    def loadFileByIndex(self, index):
        file = self.getSelectedFile()
        confirmed = self.askLoadingFile(file)
        if confirmed:
            hou.hipFile.load(file)        
            self.parent().close()
            
            

    def keyPressEvent(self, event):
         key = event.key()
         
         index = -1
         if key == QtCore.Qt.Key_0:
            index = 9
         elif key == QtCore.Qt.Key_1:
            index = 0
         elif key == QtCore.Qt.Key_2:
            index = 1
         elif key == QtCore.Qt.Key_3:
            index = 2
         elif key == QtCore.Qt.Key_4:
            index = 3
         elif key == QtCore.Qt.Key_5:
            index = 4
         elif key == QtCore.Qt.Key_6:
            index = 5
         elif key == QtCore.Qt.Key_7:
            index = 6
         elif key == QtCore.Qt.Key_8:
            index = 7
         elif key == QtCore.Qt.Key_9:
            index = 8
            
         if index != -1:
            self.selectRow(index)
            self.loadFileByIndex(index)
         else:
             if key == QtCore.Qt.Key_Enter:
                 self.loadSelectedFile()
             elif key == QtCore.Qt.Key_Return:
                 self.loadSelectedFile()
             elif key == QtCore.Qt.Key_Escape:
                 self.parent().close()
             else:
                 super(FileTableWidget, self).keyPressEvent(event)
                 
             

    def mousePressEvent(self, event):
        handled = False
        if event.type() == QtCore.QEvent.MouseButtonPress:
            if event.button() == QtCore.Qt.RightButton:
                file = self.getSelectedFile()
                if file:
                    openfile(file)
                    handled = True
                                
        if not handled:
            super(FileTableWidget, self).mousePressEvent(event)
                

            
    def mouseDoubleClickEvent(self, event):
        widget = self.childAt(event.pos())
        if widget is not None and widget.objectName():
            self.loadSelectedFile()
            
            

class FileBrowser(QtWidgets.QWidget):
    def __init__(self):
        super(FileBrowser, self).__init__()
        self.setWindowTitle("Recently Opened Files")
        self.setParent(hou.qt.mainWindow(), QtCore.Qt.Tool)
        #self.setParent(hou.qt.mainWindow(), QtCore.Qt.CustomizeWindowHint)
        
        layout = QtWidgets.QVBoxLayout()
        layout.setMargin(0)
        
        self.table = QtWidgets.QTableView()

        data = [
          [4, 9, 2],
          [1, 0, 0],
          [3, 5, 0],
          [3, 3, 2],
          [7, 8, 9],
        ]

        self.model = TableModel(data)
        self.table.setModel(self.model)
        
        
        layout.addWidget(self.table)
        
        self.setLayout(layout)
        
        self.show()
        
        

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Escape:
            self.close()
        #elif event.key() == QtCore.Qt.Key_Enter:
        #    self.loadSelectedFile()
            
        event.accept()



def showRecentlyOpenedFiles():
    window = FileBrowser()

    size = window.size()
    width = size.width()
    height = size.height()
    window.setFixedSize(width, height)

    desktop = QtWidgets.QDesktopWidget()
    sw = desktop.screen().width()
    sh = desktop.screen().height()

    window.move((sw - width)/2, (sh - height)/2)
    
    
    
    
showRecentlyOpenedFiles()