def __init__(self, toolbox): """ Args: toolbox (ToolboxUI): QMainWindow instance """ from ..ui import open_project_dialog super().__init__(parent=toolbox, f=Qt.Dialog) # Setting the parent inherits the stylesheet self._toolbox = toolbox # Set up the user interface from Designer file self.ui = open_project_dialog.Ui_Dialog() self.ui.setupUi(self) self.combobox_context_menu = None # Ensure this dialog is garbage-collected when closed self.setAttribute(Qt.WA_DeleteOnClose) # QActions for keyboard shortcuts self.go_root_action = QAction(self) self.go_home_action = QAction(self) self.go_documents_action = QAction(self) self.go_desktop_action = QAction(self) self.set_keyboard_shortcuts() self.selected_path = "" self.cb_ss = self.ui.comboBox_current_path.styleSheet() self.file_model = CustomQFileSystemModel() self.file_model.setFilter(QDir.AllDirs | QDir.NoDotAndDotDot) self.icon_provider = ProjectDirectoryIconProvider() self.file_model.setIconProvider(self.icon_provider) self.file_model.setRootPath(QDir.rootPath()) self.ui.treeView_file_system.setModel(self.file_model) self.file_model.sort(0, Qt.AscendingOrder) # Enable validator (experimental, not very useful here) # Validator prevents typing Invalid strings to combobox. (not in use) # When text in combobox is Intermediate, the validator prevents emitting # currentIndexChanged signal when enter is pressed. # Pressing enter still triggers the done() slot of the QDialog. self.validator = DirValidator() self.ui.comboBox_current_path.setValidator(self.validator) self.ui.comboBox_current_path.setInsertPolicy(QComboBox.NoInsert) # Override QCombobox keyPressEvent to catch the Enter key press self.ui.comboBox_current_path.keyPressEvent = self.combobox_key_press_event # Read recent project directories and populate combobox recents = self._toolbox.qsettings().value("appSettings/recentProjectStorages", defaultValue=None) if recents: recents_lst = str(recents).split("\n") self.ui.comboBox_current_path.insertItems(0, recents_lst) # Set start index to most recent project storage or to root if it does not exist p = self.ui.comboBox_current_path.itemText(0) if os.path.isdir(p): start_index = self.file_model.index(p) else: start_index = self.file_model.index(QDir.rootPath()) else: start_index = self.file_model.index(QDir.rootPath()) self.ui.comboBox_current_path.setCurrentIndex(-1) self.file_model.directoryLoaded.connect(self.expand_and_resize) # Start browsing to start index immediately when dialog is shown self.ui.treeView_file_system.setCurrentIndex(start_index) self.connect_signals()
def go_root(self, checked=False): """Slot for the 'Root' button. Scrolls the treeview to show and select the user's root directory. Note: We need to expand and scroll the tree view here after setCurrentIndex just in case the directory has been loaded already. """ self.ui.comboBox_current_path.setCurrentIndex(-1) root_index = self.file_model.index(QDir.rootPath()) self.ui.treeView_file_system.collapseAll() self.ui.treeView_file_system.setCurrentIndex(root_index) self.ui.treeView_file_system.expand(root_index) self.ui.treeView_file_system.scrollTo(root_index, hint=QAbstractItemView.PositionAtTop)
def create_itemview_tabwidget(self): result = QTabWidget() init_widget(result, "bottomLeftTabWidget") result.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Ignored) tree_view = QTreeView() init_widget(tree_view, "treeView") filesystem_model = QFileSystemModel(tree_view) filesystem_model.setRootPath(QDir.rootPath()) tree_view.setModel(filesystem_model) table_widget = QTableWidget() init_widget(table_widget, "tableWidget") table_widget.setRowCount(10) table_widget.setColumnCount(10) list_model = QStandardItemModel(0, 1, result) list_model.appendRow(QStandardItem(QIcon(DIR_OPEN_ICON), "Directory")) list_model.appendRow(QStandardItem(QIcon(COMPUTER_ICON), "Computer")) list_view = QListView() init_widget(list_view, "listView") list_view.setModel(list_model) icon_mode_listview = QListView() init_widget(icon_mode_listview, "iconModeListView") icon_mode_listview.setViewMode(QListView.IconMode) icon_mode_listview.setModel(list_model) result.addTab(embed_into_hbox_layout(tree_view), "Tree View") result.addTab(embed_into_hbox_layout(table_widget), "Table") result.addTab(embed_into_hbox_layout(list_view), "List") result.addTab(embed_into_hbox_layout(icon_mode_listview), "Icon Mode List") return result