def test_get_existing_directory_param_order(self): """ Test that `getExistingDirectory` passes the parameters to `QFileDialog.getExistingDirectory` in the correct order """ # GIVEN: FileDialog with patch('openlp.core.widgets.dialogs.QtWidgets.QFileDialog.getExistingDirectory', return_value='') \ as mocked_get_existing_directory: # WHEN: Calling the getExistingDirectory method with all parameters set FileDialog.getExistingDirectory('Parent', 'Caption', Path('test', 'dir'), 'Options') # THEN: The `QFileDialog.getExistingDirectory` should have been called with the parameters in the correct # order mocked_get_existing_directory.assert_called_once_with( 'Parent', 'Caption', os.path.join('test', 'dir'), 'Options')
def test_get_existing_directory_user_abort(self): """ Test that `getExistingDirectory` handles the case when the user cancels the dialog """ # GIVEN: FileDialog with a mocked QDialog.getExistingDirectory method # WHEN: Calling FileDialog.getExistingDirectory and the user cancels the dialog returns a empty string with patch('PyQt5.QtWidgets.QFileDialog.getExistingDirectory', return_value=''): result = FileDialog.getExistingDirectory() # THEN: The result should be None assert result is None
def test_get_existing_directory_user_accepts(self): """ Test that `getExistingDirectory` handles the case when the user accepts the dialog """ # GIVEN: FileDialog with a mocked QDialog.getExistingDirectory method # WHEN: Calling FileDialog.getExistingDirectory, the user chooses a file and accepts the dialog (it returns a # string pointing to the directory) with patch('PyQt5.QtWidgets.QFileDialog.getExistingDirectory', return_value=os.path.join('test', 'dir')): result = FileDialog.getExistingDirectory() # THEN: getExistingDirectory() should return a Path object pointing to the chosen file assert result == Path('test', 'dir')
def get_folder(self, title, editbox, setting_name): """ Opens a FileDialog and saves the selected folder to the given editbox. :param str title: The title of the dialog. :param QtWidgets.QLineEdit editbox: An QLineEditbox. :param str setting_name: The place where to save the last opened directory. :rtype: None """ folder_path = FileDialog.getExistingDirectory( self, title, Settings().value(self.plugin.settings_section + '/' + setting_name), FileDialog.ShowDirsOnly) if folder_path: editbox.setText(str(folder_path)) Settings().setValue(self.plugin.settings_section + '/' + setting_name, folder_path)
def on_browse_button_clicked(self): """ A handler to handle a click on the browse button. Show the QFileDialog and process the input from the user :rtype: None """ caption = self.dialog_caption path = None if self._path_type == PathEditType.Directories: if not caption: caption = translate('OpenLP.PathEdit', 'Select Directory') path = FileDialog.getExistingDirectory(self, caption, self._path, FileDialog.ShowDirsOnly) elif self._path_type == PathEditType.Files: if not caption: caption = self.dialog_caption = translate('OpenLP.PathEdit', 'Select File') path, filter_used = FileDialog.getOpenFileName(self, caption, self._path, self.filters) if path: self.on_new_path(path)