SHARE
TWEET

Untitled

ItsEasy322 Oct 17th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2. import os
  3. from PySide2 import QtCore, QtGui, QtWidgets
  4. from ui import Ui_MainWindow
  5. from datetime import datetime
  6.  
  7.  
  8. class FileManager(Ui_MainWindow):
  9.  
  10.     def __init__(self):
  11.         app = QtWidgets.QApplication(sys.argv)
  12.         main_window = QtWidgets.QMainWindow()
  13.         self.path = ''
  14.         self.history = []
  15.         self.history_index = -1
  16.         self.drives = [chr(x) + ":/" for x in range(65, 90) if os.path.exists(chr(x) + ":")]
  17.         self.setupUi(main_window)
  18.         self.tableWidget.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
  19.         self.tableWidget.horizontalHeader().setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
  20.         self.tableWidget.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
  21.         self.tableWidget.horizontalHeader().setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)
  22.         main_window.setWindowTitle('Файловый менеджер')
  23.         for drive in self.drives:
  24.             self.listWidget.addItem(drive)
  25.         self.tableWidget.itemDoubleClicked.connect(self.choose_dir_or_file)
  26.         self.listWidget.itemDoubleClicked.connect(self.choose_menu)
  27.         self.pushButton.clicked.connect(self.up)
  28.         self.pushButton_2.clicked.connect(self.back)
  29.         self.pushButton_3.clicked.connect(self.forward)
  30.         self.lineEdit.returnPressed.connect(self.line_edit)
  31.         main_window.show()
  32.         sys.exit(app.exec_())
  33.  
  34.     def file_open(self):
  35.         os.startfile(self.path)
  36.  
  37.     def line_edit(self):
  38.         path_prev = self.path
  39.         if len(self.lineEdit.text()) < 1:
  40.             self.lineEdit.setText('')
  41.         else:
  42.             try:
  43.                 self.history_index += 1
  44.                 self.path = self.lineEdit.text()
  45.                 path_slash_fix = self.path[-1:]
  46.                 if path_slash_fix[0] != '/':
  47.                     self.path += '/'
  48.                 self.render_path()
  49.                 self.lineEdit.setText(self.path)
  50.                 self.history.append(self.path)
  51.                 self.history_rewrite()
  52.             except FileNotFoundError:
  53.                 try:
  54.                     self.path = path_prev
  55.                     self.lineEdit.setText(self.history[self.history_index - 1])
  56.                 except IndexError:
  57.                     self.lineEdit.setText('')
  58.                 self.history_index -= 1
  59.             except NotADirectoryError:
  60.                 self.file_open()
  61.                 self.path = path_prev
  62.                 self.lineEdit.setText(self.path)
  63.                 self.history_index -= 1
  64.  
  65.     def history_rewrite(self):
  66.         self.history[self.history_index] = self.path
  67.         history_fixed = []
  68.         for i in range(self.history_index + 1):
  69.             history_fixed.append(self.history[i])
  70.         self.history = history_fixed
  71.  
  72.     def back(self):
  73.         if not self.history_index <= 0:
  74.             try:
  75.                 self.history_index -= 1
  76.                 self.path = self.history[self.history_index]
  77.                 self.render_path()
  78.                 self.lineEdit.setText(self.path)
  79.             except IndexError:
  80.                 self.history_index += 1
  81.  
  82.     def forward(self):
  83.         try:
  84.             self.history_index += 1
  85.             self.path = self.history[self.history_index]
  86.             self.render_path()
  87.             self.lineEdit.setText(self.path)
  88.         except IndexError:
  89.             self.history_index -= 1
  90.  
  91.     def up(self):
  92.         path_check = self.path[:-1]
  93.         if path_check.__contains__('/'):
  94.             count = 1
  95.             for i in path_check[::-1]:
  96.                 if i == '/':
  97.                     break
  98.                 count += 1
  99.             self.path = self.path[:-count]
  100.             self.render_path()
  101.             self.lineEdit.setText(self.path)
  102.  
  103.     def choose_menu(self, item):
  104.         self.path = item.text()
  105.         self.render_path()
  106.         self.lineEdit.setText(self.path)
  107.         self.history.append(self.path)
  108.         self.history_index += 1
  109.         self.history_rewrite()
  110.  
  111.     def choose_dir_or_file(self, item):
  112.         path_prev = self.path
  113.         try:
  114.             self.path = self.path + item.text() + '/'
  115.             self.render_path()
  116.             self.lineEdit.setText(self.path)
  117.             self.history.append(self.path)
  118.             self.history_index += 1
  119.         except FileNotFoundError:
  120.             self.path = path_prev
  121.         except NotADirectoryError:
  122.             self.file_open()
  123.             self.path = path_prev
  124.             self.lineEdit.setText(self.path)
  125.         self.history_rewrite()
  126.  
  127.     def render_path(self):
  128.         count = 0
  129.         for obj in os.scandir(self.path):
  130.             if str(obj.name).startswith('$') or obj.is_file():
  131.                 continue
  132.             item = QtWidgets.QTableWidgetItem(obj.name)
  133.             self.tableWidget.setRowCount(count + 1)
  134.             self.tableWidget.setItem(count, 0, item)
  135.             time_created = datetime.fromtimestamp(os.path.getctime(self.path + obj.name)).strftime('%Y-%m-%d %H:%M:%S')
  136.             item = QtWidgets.QTableWidgetItem(time_created)
  137.             self.tableWidget.setItem(count, 1, item)
  138.             time_modified = datetime.fromtimestamp(os.path.getmtime(self.path + obj.name)).strftime('%Y-%m-%d %H:%M:%S')
  139.             item = QtWidgets.QTableWidgetItem(time_modified)
  140.             self.tableWidget.setItem(count, 2, item)
  141.             item = QtWidgets.QTableWidgetItem('')
  142.             self.tableWidget.setItem(count, 3, item)
  143.             count += 1
  144.         for obj in os.scandir(self.path):
  145.             if str(obj.name).startswith('$') or obj.is_dir():
  146.                 continue
  147.             item = QtWidgets.QTableWidgetItem(obj.name)
  148.             self.tableWidget.setRowCount(count + 1)
  149.             self.tableWidget.setItem(count, 0, item)
  150.             time_created = datetime.fromtimestamp(os.path.getctime(self.path + obj.name)).strftime('%Y-%m-%d %H:%M:%S')
  151.             item = QtWidgets.QTableWidgetItem(time_created)
  152.             self.tableWidget.setItem(count, 1, item)
  153.             time_modified = datetime.fromtimestamp(os.path.getmtime(self.path + obj.name)).strftime('%Y-%m-%d %H:%M:%S')
  154.             item = QtWidgets.QTableWidgetItem(time_modified)
  155.             self.tableWidget.setItem(count, 2, item)
  156.             size = os.path.getsize(obj) / 1024
  157.             size_type = 0
  158.             while True:
  159.                 if size < 1024:
  160.                     break
  161.                 size = size / 1024
  162.                 size_type += 1
  163.             if size_type == 0:
  164.                 size_type = ' Кбайт.'
  165.             elif size_type == 1:
  166.                 size_type = ' Мбайт.'
  167.             elif size_type == 2:
  168.                 size_type = ' Гбайт.'
  169.             elif size_type == 3:
  170.                 size_type = ' Тбайт.'
  171.             size = round(size)
  172.             item = QtWidgets.QTableWidgetItem(str(size) + size_type)
  173.             self.tableWidget.setItem(count, 3, item)
  174.             count += 1
  175.  
  176.  
  177. if __name__ == '__main__':
  178.     FileManager()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top