Skip to content

Commit

Permalink
Add filter option to open dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmthomas committed Oct 25, 2017
1 parent d0ff883 commit f568774
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
12 changes: 6 additions & 6 deletions lib/cosmos/gui/utilities/script_module_gui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ def _get_main_thread_gui
return result
end

def save_file_dialog(directory = Cosmos::USERPATH, message = "Save File")
_get_main_thread_gui {|window| Qt::FileDialog.getSaveFileName(window, message, directory) }
def save_file_dialog(directory = Cosmos::USERPATH, message = "Save File", filter = "All Files (*.*)")
_get_main_thread_gui {|window| Qt::FileDialog.getSaveFileName(window, message, directory, filter) }
end

def open_file_dialog(directory = Cosmos::USERPATH, message = "Open File")
_get_main_thread_gui {|window| Qt::FileDialog.getOpenFileName(window, message, directory) }
def open_file_dialog(directory = Cosmos::USERPATH, message = "Open File", filter = "All Files (*.*)")
_get_main_thread_gui {|window| Qt::FileDialog.getOpenFileName(window, message, directory, filter) }
end

def open_files_dialog(directory = Cosmos::USERPATH, message = "Open File(s)")
_get_main_thread_gui {|window| Qt::FileDialog.getOpenFileNames(window, message, directory) }
def open_files_dialog(directory = Cosmos::USERPATH, message = "Open File(s)", filter = "All Files (*.*)")
_get_main_thread_gui {|window| Qt::FileDialog.getOpenFileNames(window, message, directory, filter) }
end

def open_directory_dialog(directory = Cosmos::USERPATH, message = "Open Directory")
Expand Down
18 changes: 9 additions & 9 deletions lib/cosmos/script/scripting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def combo_box(string, *options)
prompt_combo_box(string, options)
end

def _file_dialog(message, directory, select_files = true)
def _file_dialog(message, directory, filter, select_files = true)
answer = ''
files = Dir["#{directory}/*"]
files = Dir["#{directory}/#{filter}"]
if select_files
files.select! {|f| !File.directory? f }
else
Expand All @@ -87,17 +87,17 @@ def _file_dialog(message, directory, select_files = true)
end
return answer
end
def save_file_dialog(directory = Cosmos::USERPATH, message = "Save File")
_file_dialog(message, directory)
def save_file_dialog(directory = Cosmos::USERPATH, message = "Save File", filter = "*")
_file_dialog(message, directory, filter)
end
def open_file_dialog(directory = Cosmos::USERPATH, message = "Open File")
_file_dialog(message, directory)
def open_file_dialog(directory = Cosmos::USERPATH, message = "Open File", filter = "*")
_file_dialog(message, directory, filter)
end
def open_files_dialog(directory = Cosmos::USERPATH, message = "Open File(s)")
_file_dialog(message, directory)
def open_files_dialog(directory = Cosmos::USERPATH, message = "Open File(s)", filter = "*")
_file_dialog(message, directory, filter)
end
def open_directory_dialog(directory = Cosmos::USERPATH, message = "Open Directory")
_file_dialog(message, directory, false)
_file_dialog(message, directory, "*", false)
end

def prompt_for_hazardous(target_name, cmd_name, hazardous_description)
Expand Down
21 changes: 18 additions & 3 deletions spec/script/scripting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,37 @@ class ScriptRunner; end
it "gets file listings" do
capture_io do |stdout|
expect(self).to receive(:gets) { 'file' }
expect(save_file_dialog(Dir.pwd, "Save Something!!!")).to eql 'file'
expect(save_file_dialog()).to eql 'file'
expect(stdout.string).to include "Save File"
stdout.rewind
expect(self).to receive(:gets) { 'file' }
expect(save_file_dialog(Dir.pwd, "Save Something!!!", "*.txt")).to eql 'file'
expect(stdout.string).to include "Save Something!!!"
stdout.rewind
expect(self).to receive(:gets) { 'file' }
expect(open_file_dialog("C:/")).to eql 'file'
expect(open_file_dialog()).to eql 'file'
expect(stdout.string).to include "Open File"
stdout.rewind
expect(self).to receive(:gets) { 'file' }
expect(open_file_dialog(Dir.pwd, "Test Open", "*.txt")).to eql 'file'
expect(stdout.string).to include "Test Open"
stdout.rewind
expect(self).to receive(:gets) { 'file' }
expect(open_files_dialog()).to eql 'file'
expect(stdout.string).to include "Open File(s)"
stdout.rewind
expect(self).to receive(:gets) { 'file' }
expect(open_files_dialog(Dir.pwd, "Test Open Files")).to eql 'file'
expect(stdout.string).to include "Test Open Files"
stdout.rewind
expect(self).to receive(:gets) { 'dir' }
expect(open_directory_dialog()).to eql 'dir'
expect(stdout.string).to include "Open Directory"
stdout.rewind
expect(self).to receive(:gets) { 'dir' }
expect(open_directory_dialog(Dir.pwd, "Test Dir")).to eql 'dir'
expect(stdout.string).to include "Test Dir"
stdout.rewind
end
end
end
Expand Down Expand Up @@ -742,4 +758,3 @@ def self.instrument_script(file_text, path, bool); "#"; end

end
end

0 comments on commit f568774

Please sign in to comment.