main.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import logging, pathlib
  2. from PyQt5.QtWidgets import *
  3. from PyQt5.QtGui import *
  4. from PyQt5.QtCore import *
  5. from protolib.gui_utils.pyqt5_messageboxes import show_critical_messagebox
  6. from windows.to_protolib.console import ConsoleWindow
  7. from windows.configure import ConfigureWindow
  8. from widgets.file_browser import FileBrowserWidget
  9. from config import load_config
  10. logger = logging.getLogger(__name__)
  11. class MainWindow(QMainWindow):
  12. def __init__(self, args):
  13. super().__init__()
  14. self.setWindowIcon(QIcon('icon.png'))
  15. self.setWindowTitle("File Browser")
  16. self.setMinimumWidth(700)
  17. self.setMinimumHeight(400)
  18. self.console = ConsoleWindow.new_window('Console - File Browser')
  19. #region
  20. menuBar = self.menuBar()
  21. #region
  22. self.fileMenu = QMenu("&File", self)
  23. menuBar.addMenu(self.fileMenu)
  24. #region
  25. refreshAction = QAction("&Refresh", self)
  26. refreshAction.setShortcut("Ctrl+R")
  27. refreshAction.setStatusTip('Update the directory listing')
  28. refreshAction.triggered.connect(lambda: self.get_tab().update_treeview())
  29. self.fileMenu.addAction(refreshAction)
  30. #endregion
  31. #region
  32. refreshAction = QAction("&Go To Parent", self)
  33. refreshAction.setShortcut("Ctrl+Up")
  34. refreshAction.setStatusTip('Go to the parent directory')
  35. refreshAction.triggered.connect(lambda: self.get_tab().goto_dir_parent())
  36. self.fileMenu.addAction(refreshAction)
  37. #endregion
  38. self.fileMenu.addSeparator()
  39. #region
  40. quitAction = QAction("&Quit", self)
  41. quitAction.setShortcut("Ctrl+Q")
  42. quitAction.setStatusTip('Leave the app')
  43. quitAction.triggered.connect(self.close)
  44. self.fileMenu.addAction(quitAction)
  45. #endregion
  46. #endregion
  47. #region
  48. self.editMenu = QMenu("&Edit", self)
  49. menuBar.addMenu(self.editMenu)
  50. #region
  51. copyAction = QAction("&Copy", self)
  52. copyAction.setStatusTip('Copy the selected file')
  53. copyAction.triggered.connect(lambda: self.get_tab().copy_path())
  54. copyAction.setShortcut('Ctrl+C')
  55. self.editMenu.addAction(copyAction)
  56. #endregion
  57. #region
  58. cutAction = QAction("&Cut", self)
  59. cutAction.setStatusTip('Cut the selected file')
  60. cutAction.triggered.connect(lambda: self.get_tab().copy_path())
  61. cutAction.setShortcut('Ctrl+X')
  62. self.editMenu.addAction(cutAction)
  63. #endregion
  64. #region
  65. pasteAction = QAction("&Paste", self)
  66. pasteAction.setStatusTip('Paste the file in your clipboard')
  67. pasteAction.triggered.connect(lambda: self.get_tab().copy_path())
  68. pasteAction.setShortcut('Ctrl+V')
  69. self.editMenu.addAction(pasteAction)
  70. #endregion
  71. self.editMenu.addSeparator()
  72. #region
  73. configureAction = QAction("&Config", self)
  74. configureAction.setStatusTip('Open the config menu')
  75. configureAction.triggered.connect(self.open_configure_window)
  76. self.editMenu.addAction(configureAction)
  77. #endregion
  78. #region
  79. self.hiddenFilesAction = QAction("Hidden Files", self, checkable=True)
  80. self.hiddenFilesAction.setStatusTip('Enable or disable removing hidden files from the directory listing')
  81. self.editMenu.addAction(self.hiddenFilesAction)
  82. #endregion
  83. #endregion
  84. #region
  85. self.consoleMenu = QMenu("&Console", self)
  86. menuBar.addMenu(self.consoleMenu)
  87. #region
  88. showConsoleAction = QAction("&Show", self)
  89. showConsoleAction.setStatusTip('Show the console window')
  90. showConsoleAction.triggered.connect(self.console.show)
  91. self.consoleMenu.addAction(showConsoleAction)
  92. #endregion
  93. #region
  94. hideConsoleAction = QAction("&Hide", self)
  95. hideConsoleAction.setStatusTip('Hide the console window')
  96. hideConsoleAction.triggered.connect(self.console.hide)
  97. self.consoleMenu.addAction(hideConsoleAction)
  98. #endregion
  99. #endregion
  100. #endregion
  101. #region
  102. layout = QVBoxLayout()
  103. widget = QWidget()
  104. widget.setLayout(layout)
  105. self.new_tab_button = QPushButton('New Tab')
  106. self.new_tab_button.clicked.connect(self.new_tab)
  107. self.tab_widget = QTabWidget()
  108. self.tab_widget.setCornerWidget(self.new_tab_button)
  109. layout.addWidget(self.tab_widget)
  110. self.new_tab()
  111. self.setCentralWidget(widget)
  112. #endregion
  113. logger.info('Main window initialized')
  114. def open_configure_window(self):
  115. tab = self.get_tab()
  116. window = ConfigureWindow(tab)
  117. window.exec_()
  118. tab.update_treeview()
  119. self.sync_browser_configs()
  120. def new_tab(self):
  121. file_browser = FileBrowserWidget()
  122. load_config(file_browser)
  123. self.hiddenFilesAction.toggled.connect(file_browser.update_hide_files)
  124. tab = self.tab_widget.addTab(file_browser, 'Placeholder')
  125. remove_tab_button = QPushButton('Close Tab')
  126. remove_tab_button.clicked.connect(lambda: self.close_tab(tab))
  127. self.tab_widget.tabBar().setTabButton(tab, 1, remove_tab_button)
  128. file_browser.directory_listing_refreshed.connect(lambda: self.update_tab_name(tab))
  129. file_browser.update_treeview()
  130. self.update_tab_name(tab)
  131. def update_tab_name(self, index):
  132. widget = self.tab_widget.widget(index)
  133. path = widget.get_current_path()
  134. if path.name:
  135. self.tab_widget.setTabText(index, path.name)
  136. else:
  137. self.tab_widget.setTabText(index, str(path))
  138. def get_tab(self) -> FileBrowserWidget:
  139. index = self.tab_widget.currentIndex()
  140. return self.tab_widget.widget(index)
  141. def close_tab(self, index):
  142. self.tab_widget.removeTab(index)
  143. if self.tab_widget.count() == 0:
  144. self.new_tab()
  145. def sync_browser_configs(self):
  146. cur_tab = self.get_tab()
  147. index = self.tab_widget.currentIndex()
  148. for i in range(self.tab_widget.count()):
  149. if i == index:
  150. continue
  151. tab = self.tab_widget.widget(i)
  152. tab.pattern_assoc = cur_tab.pattern_assoc
  153. tab.name_assoc = cur_tab.name_assoc
  154. tab.viewer_assoc = cur_tab.viewer_assoc
  155. tab.mimetype_assoc = cur_tab.mimetype_assoc
  156. def closeEvent(self, event):
  157. self.console.close()
  158. QMainWindow.closeEvent(self, event)
  159. logger.info('Main window closed')