class MainWindow(QMainWindow): def __init__(self): super().__init__() # SET => App Icon self.icon = QIcon("img/iconXLNK.png") # End self.tree_view = None self.file_system_model = None # SET => Window Icon self.setWindowIcon(self.icon) # End # WSET => Window Title self.setWindowTitle("XLNK | Data Manager") # End # Menus self.menu = self.menuBar() self.file_menu = self.menu.addMenu("&File") self.edit_menu = self.menu.addMenu("&Edit") self.view_menu = self.menu.addMenu("&View") self.help_menu = self.menu.addMenu("&Help") # End # =================== # Menu Button Actions # Exit QAction exit_action = QAction("Exit", self) exit_action.setShortcut(QKeySequence.Quit) exit_action.triggered.connect(self.close) self.file_menu.addAction(exit_action) # End # End # Tool Bar toolbar = QToolBar(self) self.addToolBar(toolbar) # delete action on toolbar delete_action_tb = QAction("DELETE TABLE ROW", self) delete_action_tb.setStatusTip("Obrisi Red U Tabeli") delete_action_tb.triggered.connect(self.delete_table_row_tb) toolbar.addAction(delete_action_tb) # Dock Widget dock_widget = QDockWidget("EXPLORER", self) # File System Model self.file_system_model = QFileSystemModel() self.file_system_model.setRootPath(QDir.currentPath()) # SET => Tree View MOdel self.tree_view = QTreeView() self.tree_view.setModel(self.file_system_model) self.tree_view.setRootIndex( self.file_system_model.index(QDir.currentPath() + "/data")) self.tree_view.clicked.connect(self.file_clicked_handler) dock_widget.setWidget(self.tree_view) dock_widget.setFloating(False) self.addDockWidget(Qt.LeftDockWidgetArea, dock_widget) # QLabel qlabel = QLabel(self) qlabel.setText("Welcome to XLNK.") # Central Widget self.clicked_file = None self.setCentralWidget(qlabel) self.showMaximized() # ToolBar Functions # TODO def delete_table_row_tb(self): print("Ugraditi funkciju za brisanje reda iz tabele.") def file_clicked_handler(self, index): index = self.tree_view.currentIndex() file_clicked_param = os.path.basename( self.file_system_model.filePath(index)) self.clicked_file = file_clicked_param self.status_bar = QStatusBar(self) self.status_bar.showMessage( "File Selected: {}".format(file_clicked_param), 3000) self.setStatusBar(self.status_bar) self.workspace_widget = WorkSpaceWidget(self, self.clicked_file) self.setCentralWidget(self.workspace_widget)
class BrowserWidget(QWidget): """ This class puts together a TabCompletionLineEdit and a list view of teh FolderListModel in one single widget. """ activated = Signal(str) # emitted when the user selects a file def __init__(self, parent=None): super().__init__(parent) self._recursiveActivated = False self._model = FolderListModel(self) self._completer = QCompleter(self._model, self) self._completer.setCompletionColumn(3) self._lineedit = TabCompletionLineEdit(self._completer, self) self._view = QTreeView(self) self._view.setModel(self._model) self._model.folderChanged.connect(self._lineedit.setText) self._model.setFolder(".") self._view.header().setSectionResizeMode(0, QHeaderView.Interactive) self._view.header().setSectionResizeMode(1, QHeaderView.ResizeToContents) self._view.header().setSectionResizeMode(2, QHeaderView.ResizeToContents) self._view.header().resizeSection(0, 500) self._view.header().setSectionHidden(3, True) layout = QVBoxLayout(self) layout.addWidget(self._lineedit) layout.addWidget(self._view) self.setLayout(layout) self._view.activated.connect(self._activated) self.activated.connect(self._lineedit.setText) self._lineedit.returnPressed.connect(self._leActivated, Qt.QueuedConnection) self._lineedit.textEdited.connect(self._leTextEdited) def setActive(self, activeFile): """ set the activated file :param activeFile: a string or Path instance :return: """ activeFile = Path(activeFile) assert activeFile.is_file() self._model.setFolder(activeFile.parent) idx = self._model.fileToIndex(activeFile) self._view.setCurrentIndex(idx) self._view.scrollTo(idx) self.activated.emit(str(activeFile)) def active(self): """ The currently activated file :return: a string instance """ cidx = self._view.currentIndex() c = self._model.data(cidx, Qt.UserRole) return str(c) if c is not None else None def current(self): """ A synonym for active() :return: a string instance """ return self.active() def setFilter(self, flt): """ Set the name filter of the file browser :param flt: a string instance or a list of strings :return: """ self._model.setFilter(flt) def scrollTo(self, item): """ Scrolls to the given item. :param item: a string instance :return: """ cidx = self._model.fileToIndex(item) self._view.scrollTo(cidx) def folder(self): """ Returns the current folder :return: a Path instance """ return self._model.folder() def setFolder(self, folder): """ Sets the current folder :param folder: a string or a Path instance :return: """ self._model.setFolder(folder) def _leActivated(self): idx = self._model.fileToIndex(self._lineedit.text()) self._activated(idx) def _leTextEdited(self, text): p = Path(text) if p.is_dir() and len(text) > 0 and text[-1] in ["/", "\\"]: self.setFolder(p) def _activated(self, idx): c = self._model.data(idx, Qt.UserRole) if c is None: return logger.debug("activate %s", c) if c.is_file(): self.activated.emit(str(c)) else: self._model.setFolder(c)