def on_file_click(self): """ Add a file to the list widget to make it available for showing """ file_paths, selected_filter = FileDialog.getOpenFileNames( self, self.on_new_prompt, Settings().value(self.settings_section + '/last directory'), self.on_new_file_masks) log.info('New file(s) {file_paths}'.format(file_paths=file_paths)) if file_paths: self.application.set_busy_cursor() self.validate_and_load(file_paths) self.application.set_normal_cursor()
def test_get_open_file_names_selected_filter(self): """ Test that `getOpenFileNames` does not modify the selectedFilter as returned by `QFileDialog.getOpenFileNames` """ # GIVEN: FileDialog with a mocked QDialog.getOpenFileNames method # WHEN: Calling FileDialog.getOpenFileNames, and `QFileDialog.getOpenFileNames` returns a known # `selectedFilter` with patch('PyQt5.QtWidgets.QFileDialog.getOpenFileNames', return_value=([], 'selected filter')): result = FileDialog.getOpenFileNames() # THEN: getOpenFileNames() should return a tuple with the second value set to a the selected filter assert result[1] == 'selected filter'
def test_get_open_file_names_user_abort(self): """ Test that `getOpenFileNames` handles the case when the user cancels the dialog """ # GIVEN: FileDialog with a mocked QDialog.getOpenFileNames method # WHEN: Calling FileDialog.getOpenFileNames and the user cancels the dialog (it returns a tuple with the first # value set as an empty list) with patch('PyQt5.QtWidgets.QFileDialog.getOpenFileNames', return_value=([], '')): result = FileDialog.getOpenFileNames() # THEN: First value should be an empty list assert result[0] == []
def test_get_open_file_names_user_accepts(self): """ Test that `getOpenFileNames` handles the case when the user accepts the dialog """ # GIVEN: FileDialog with a mocked QDialog.getOpenFileNames method # WHEN: Calling FileDialog.getOpenFileNames, the user chooses some files and accepts the dialog (it returns a # tuple with the first value set as a list of strings pointing to the file) with patch('PyQt5.QtWidgets.QFileDialog.getOpenFileNames', return_value=([ os.path.join('test', 'chosen.file1'), os.path.join('test', 'chosen.file2') ], '')): result = FileDialog.getOpenFileNames() # THEN: getOpenFileNames() should return a tuple with the first value set to a list of Path objects pointing # to the chosen file assert result[0] == [ Path('test', 'chosen.file1'), Path('test', 'chosen.file2') ]
def on_import_theme(self, checked=None): """ Opens a file dialog to select the theme file(s) to import before attempting to extract OpenLP themes from those files. This process will only load version 2 themes. :param bool checked: Sent by the QAction.triggered signal. It's not used in this method. :rtype: None """ file_paths, filter_used = FileDialog.getOpenFileNames( self, translate('OpenLP.ThemeManager', 'Select Theme Import File'), Settings().value(self.settings_section + '/last directory import'), translate('OpenLP.ThemeManager', 'OpenLP Themes (*.otz)')) self.log_info('New Themes {file_paths}'.format(file_paths=file_paths)) if not file_paths: return self.application.set_busy_cursor() for file_path in file_paths: self.unzip_theme(file_path, self.theme_path) Settings().setValue(self.settings_section + '/last directory import', file_path.parent) self.load_themes() self.application.set_normal_cursor()
def get_files(self, title, listbox, filters=''): """ Opens a QFileDialog and writes the filenames to the given listbox. :param title: The title of the dialog (str). :param listbox: A listbox (QListWidget). :param filters: The file extension filters. It should contain the file descriptions as well as the file extensions. For example:: 'SongBeamer Files (*.sng)' """ if filters: filters += ';;' filters += '{text} (*)'.format(text=UiStrings().AllFiles) file_paths, filter_used = FileDialog.getOpenFileNames( self, title, self.settings.value(self.plugin.settings_section + '/last directory import'), filters) for file_path in file_paths: list_item = QtWidgets.QListWidgetItem(str(file_path)) list_item.setData(QtCore.Qt.UserRole, file_path) listbox.addItem(list_item) if file_paths: self.settings.setValue(self.plugin.settings_section + '/last directory import', file_paths[0].parent)