def openFile(self, near = None): fileDialog = Q.QFileDialog(self) fileDialog.setAcceptMode(Q.QFileDialog.AcceptOpen) fileDialog.setFileMode(Q.QFileDialog.ExistingFiles) fileDialog.setFilter(Q.QDir.Hidden|Q.QDir.AllEntries|Q.QDir.System) fileDialog.setViewMode(Q.QFileDialog.Detail) if near is not None: if isinstance(near,str): near = Q.QFileInfo(near) if isinstance(near,Q.QFileInfo): if near.isDir(): near = Q.QDir(near.filePath()) else: near = near.dir() if not isinstance(near,Q.QDir): near = Q.QDir.home() fileDialog.setDirectory(near.canonicalPath()) if fileDialog.exec(): if fileDialog.selectedFiles(): if not self.player: # return self.player = self._parent.getPlayerAt(-1) for filePath in fileDialog.selectedFiles(): print("\n"+filePath+"\n") self.player.command("loadfile",str(filePath),"append-play") w = self.player.widget if w:w.sized_once = False
def save_annotations(self): self.record_labeled_positions() # Grab the latest annotations file_dialog = Qt.QFileDialog() file_dialog.setAcceptMode(Qt.QFileDialog.AcceptSave) if file_dialog.exec_(): # Run dialog box and check for a good exit save_path = pathlib.Path(file_dialog.selectedFiles()[0]) self.worm_info.to_csv(save_path.open('w'), sep='\t') print('file written to ' + str(save_path))
def load_annotations(self): file_dialog = Qt.QFileDialog() file_dialog.setAcceptMode(Qt.QFileDialog.AcceptOpen) if file_dialog.exec_(): # Run dialog box and check for a good exit load_path = pathlib.Path(file_dialog.selectedFiles()[0]) loaded_info = pd.read_csv(load_path.open(), sep='\t', index_col=0) if (set(loaded_info.columns.values) != set( self.worm_info.columns.values)) or (set( loaded_info.index) != set(self.worm_info.index)): print(loaded_info) print(self.worm_info) raise Exception('Bad annotation file') self.worm_info = loaded_info self.labels = list(self.worm_info.columns.values) print('annotations read from ' + str(load_path))
def get_file_name(mode, parent, title, url, filters, suffix=None, dflt_filter=None): """ Show standard file dialog, to select a file to open or save. Arguments: mode (int): File mode: 0 for Save; 1 for Open. parent (QWidget): Parent widget. title (str): Dialog's title. url (str): Initial file path. filters (list[str] or str): File patterns by list or one pattern by string. suffix (Optional[str]): Default extension to be automatically appended to the file name. Defaults to *None*. dflt_filter (Optional[str]): File pattern that will be set as current file filter. Defaults to *None*: first pattern. Returns: str: Selected file or *None* if operation is cancelled. """ dlg = Q.QFileDialog(parent) urls = [] urls.append(Q.QDir.homePath()) urls.append(osp.dirname(Q.QApplication.arguments()[0])) dlg.setSidebarUrls([Q.QUrl.fromLocalFile(i) for i in urls]) acc_mode = Q.QFileDialog.AcceptOpen if mode else Q.QFileDialog.AcceptSave fmode = Q.QFileDialog.ExistingFile if mode else Q.QFileDialog.AnyFile dlg.setAcceptMode(acc_mode) dlg.setFileMode(fmode) # Uncomment below to disable using the system dialog # That would make ExistingFile mode effective #option = Q.QFileDialog.DontUseNativeDialog #dlg.setOption(option) dlg.setWindowTitle(title) dlg.setNameFilters(to_list(filters)) if dflt_filter is None: dflt_filter = common_filters()[-1] dlg.selectNameFilter(dflt_filter) if suffix: dlg.setDefaultSuffix(suffix) dlg.selectFile(url) return dlg.selectedFiles()[0] if dlg.exec_() else None
def fileSaveAs(self): filename = self.filename if filename == None: filename = "" filedialog = Qt.QFileDialog(self, self.tr("Save Kodos File"), filename, "Kodos file (*.kds);;All (*)") filedialog.setAcceptMode(Qt.QFileDialog.AcceptSave) filedialog.setDefaultSuffix("kds") ok = filedialog.exec_() if ok == Qt.QDialog.Rejected: self.updateStatus(self.tr("No file selected to save"), -1, 5, True) return filename = os.path.normcase(str(filedialog.selectedFiles()[0])) self.filename = filename self.fileSave()