Class: Cosmos::LauncherTool

Inherits:
Qt::Object
  • Object
show all
Defined in:
lib/cosmos/tools/launcher/launcher_tool.rb

Instance Method Summary collapse

Constructor Details

#initialize(parent, button_text, shell_command, capture_io, variable_parameters) ⇒ LauncherTool

Returns a new instance of LauncherTool.



18
19
20
21
22
23
24
# File 'lib/cosmos/tools/launcher/launcher_tool.rb', line 18

def initialize(parent, button_text, shell_command, capture_io, variable_parameters)
  super(parent)
  @button_text = button_text
  @shell_command = shell_command
  @capture_io = capture_io
  @variable_parameters = variable_parameters
end

Instance Method Details

#button_clickedObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cosmos/tools/launcher/launcher_tool.rb', line 26

def button_clicked
  if @variable_parameters
    parameters = parameters_dialog
    if parameters
      if @capture_io
        Cosmos.run_process_check_output(@shell_command + ' ' + parameters)
      else
        Cosmos.run_process(@shell_command + ' ' + parameters)
      end
    end
  else
    if @capture_io
      Cosmos.run_process_check_output(@shell_command)
    else
      Cosmos.run_process(@shell_command)
    end
  end
end

#parameters_dialogObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cosmos/tools/launcher/launcher_tool.rb', line 45

def parameters_dialog
  dialog = Qt::Dialog.new(parent)
  dialog.window_title = "#{@button_text} Options"
  layout = Qt::VBoxLayout.new
  dialog.layout = layout

  widgets = []
  @variable_parameters.each do |parameter_name, parameter_value|
    hlayout = Qt::HBoxLayout.new
    hlayout.addWidget(Qt::Label.new(parameter_name))
    parameter_options = parameter_value.split('|')
    if parameter_options.length > 1
      combo_box = Qt::ComboBox.new
      combo_box.setEditable(true)
      idx = 0
      parameter_options.each do |option|
        combo_box.insertItem(idx, option)
        idx += 1
      end
      combo_box.setCurrentIndex(0) # Default Option is the first one
      hlayout.addWidget(combo_box)
      widgets << combo_box
    else
      line_edit = Qt::LineEdit.new
      line_edit.setText(parameter_value)
      hlayout.addWidget(line_edit)
      widgets << line_edit
    end
    layout.addLayout(hlayout)
  end

  ok_button = Qt::PushButton.new('Ok')
  connect(ok_button, SIGNAL('clicked()'), dialog, SLOT('accept()'))
  cancel_button = Qt::PushButton.new('Cancel')
  connect(cancel_button, SIGNAL('clicked()'), dialog, SLOT('reject()'))

  hlayout = Qt::HBoxLayout.new
  hlayout.addWidget(ok_button, 0, Qt::AlignLeft)
  hlayout.addWidget(cancel_button, 0, Qt::AlignRight)
  layout.addLayout(hlayout)

  dialog.resize(400, 0)
  cursor_pos = Qt::Cursor.pos
  x_pos = cursor_pos.x - 200
  y_pos = cursor_pos.y - 50
  if x_pos < 0
    x_pos = 0
  elsif x_pos > (Qt::Application.desktop.width - dialog.frameGeometry.width)
    x_pos = Qt::Application.desktop.width - dialog.frameGeometry.width
  end
  dialog.move(x_pos, y_pos)

  result = dialog.exec
  if result == Qt::Dialog::Accepted
    parameters = ''
    index = 0
    @variable_parameters.each do |parameter_name, _parameter_value|
      parameters << parameter_name
      parameters << ' '
      parameters << widgets[index].text
      parameters << ' '
      index += 1
    end
    dialog.dispose
    return parameters
  else
    dialog.dispose
    return nil
  end
end