1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #!/usr/bin/env python
from pathlib import Path
import sys
try:
from PySide6 import QtCore, QtGui, QtWidgets
except ImportError:
print("ERREUR: installez pyside 6 !")
exit(13)
class WinMain(QtWidgets.QMainWindow):
""" Application simple """
def __init__(self, directory):
super(WinMain, self).__init__()
self.model = QtWidgets.QFileSystemModel()
self.model.setFilter(QtCore.QDir.Files | QtCore.QDir.NoDotAndDotDot)
self.resize(600, 520)
if layout := QtWidgets.QVBoxLayout(): # décalage juste pour mon visuel
splitter = QtWidgets.QSplitter()
tmp = QtWidgets.QHBoxLayout(splitter)
self.content = QtWidgets.QLabel()
self.content.setText("Voir image")
self.content.setGeometry(QtCore.QRect(0, 0, 300, 300))
tmp.addWidget(self.content)
self.content.setMaximumWidth(600)
self.list = QtWidgets.QListView(splitter)
self.list.setModel(self.model)
self.list.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.list.customContextMenuRequested.connect(self.onContextMenu)
self.list.doubleClicked.connect(self.afficher)
layout.addWidget(splitter)
widget = QtWidgets.QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
self.statusBar()
self.directory = directory
@property
def directory(self):
return self._directory
@directory.setter
def directory(self, value):
"""on change de répertoire"""
self._directory = value
self.content.clear()
self.content.setText("Voir image/fichier texte")
self.content.setWordWrap(True)
self.model.setRootPath(self.directory)
self.list.setRootIndex(self.model.index(self.directory))
self.setWindowTitle(self.directory)
self.statusBar().showMessage(self.directory)
print("root dir:", self.model.rootDirectory())
def changeDir(self):
choix=QtWidgets.QFileDialog.getExistingDirectory(
self,
"Choisissez votre dossier",
self.directory,
QtWidgets.QFileDialog.Option.ShowDirsOnly | QtWidgets.QFileDialog.Option.ReadOnly,
)
if choix:
self.directory = choix
def afficher(self, item):
#file_ = self.model.filePath(item) # même chose que ligne suivante
file_ =item.data(QtWidgets.QFileSystemModel.Roles.FilePathRole)
print(file_)
pix = QtGui.QPixmap(file_)
# ("png", "jpeg", "jpg", "svg", "bmp", "gif", 'ico', "pdf")
if not pix.isNull():
self.content.setPixmap(pix)
sizem = min(self.content.width(), self.content.height())
size = QtCore.QSize(sizem, sizem)
myScaledPixmap = pix.scaled(size, QtCore.Qt.KeepAspectRatio)
self.content.setPixmap(myScaledPixmap)
else:
try:
# que appercu car pas de scrolling
self.content.setText(Path(file_).read_text()[0:412])
except UnicodeDecodeError:
self.content.setText("fichier incompatible `_´")
self.statusBar().showMessage("")
return
self.statusBar().showMessage(file_)
def onContextMenu(self, point):
menu = QtWidgets.QMenu()
notUsed = menu.addAction('-')
notUsed.setEnabled(False)
menu.addSeparator()
aInfo = menu.addAction("Changer de dossier...")
aRefresh = menu.addAction("Actualiser")
action = menu.exec(self.list.mapToGlobal(point))
if action == aInfo:
self.changeDir()
if action == aRefresh:
self.directory = self.directory
def run(directory):
app = QtWidgets.QApplication(sys.argv)
ico = QtGui.QIcon.fromTheme("folder")
app.setWindowIcon(ico)
trans = QtCore.QTranslator()
trans.load('qt_fr', QtCore.QLibraryInfo.path(QtCore.QLibraryInfo.LibraryPath.TranslationsPath))
QtCore.QCoreApplication.installTranslator(trans) # pour avoir les dialogues en fr
win = WinMain(directory)
win.show()
sys.exit(app.exec())
if __name__ == "__main__":
run(QtCore.QDir.currentPath()) |
Partager