Class: Cosmos::Launcher

Inherits:
QtTool show all
Defined in:
lib/cosmos/tools/launcher/launcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from QtTool

#about, #closeEvent, #complete_initialize, create_default_options, graceful_kill, #initialize_actions, #initialize_help_menu, post_options_parsed_hook, redirect_io, restore_io

Constructor Details

#initialize(options) ⇒ Launcher

Returns a new instance of Launcher.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cosmos/tools/launcher/launcher.rb', line 24

def initialize(options)
  super(options) # MUST BE FIRST - All code before super is executed twice in RubyQt Based classes

  # Set environment variable of COSMOS_USERPATH so that all launched apps know where to find the configuration
  ENV['COSMOS_USERPATH'] = Cosmos::USERPATH

  Cosmos.load_cosmos_icon("launcher.png")
  layout.setSizeConstraint(Qt::Layout::SetFixedSize)

  @about_string = "Launcher provides a list of applications to launch at the click of a button. It can also launch multiple applications and configure their exact placement on the screen."
  initialize_actions()
  initialize_menus()
  initialize_central_widget()
  complete_initialize()
end

Class Method Details

.pre_window_new_hook(options) ⇒ Object



165
166
167
168
# File 'lib/cosmos/tools/launcher/launcher.rb', line 165

def self.pre_window_new_hook(options)
  # Show legal dialog
  LegalDialog.new
end

.run(option_parser = nil, options = nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/cosmos/tools/launcher/launcher.rb', line 170

def self.run(option_parser = nil, options = nil)
  Cosmos.catch_fatal_exception do
    unless option_parser and options
      option_parser, options = create_default_options()
      options.title = 'Launcher'
      options.config_file = 'launcher.txt'
      option_parser.separator "Launcher Specific Options:"
      option_parser.on("-c", "--config FILE", "Use the specified configuration file") do |arg|
        options.config_file = arg
      end
    end

    super(option_parser, options)
  end
end

Instance Method Details

#initialize_central_widgetObject



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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cosmos/tools/launcher/launcher.rb', line 49

def initialize_central_widget
  # Create the central widget
  central_widget = Qt::Widget.new
  setCentralWidget(central_widget)

  # Read the configuration file
  config_file = File.join(Cosmos::USERPATH, 'config', 'tools', 'launcher', @options.config_file)
  begin
    config = LauncherConfig.new(config_file)
  rescue => error
    ExceptionDialog.new(self, error, "Error parsing #{config_file}")
  end

  # Set the title
  self.window_title = config.title

  # Create each button or divider
  default_icon_filename = 'COSMOS_64x64.png'
  widgets = []
  config.items.each do |item_type, text, shell_command_or_settings, capture_io, icon_filename, variable_parameters|
    case item_type
    when :TOOL, :MULTITOOL
      layout = Qt::VBoxLayout.new
      if icon_filename
        icon = Cosmos.get_icon(icon_filename)
      else
        icon = Cosmos.get_icon(default_icon_filename)
      end
      button = Qt::PushButton.new('')
      button.setIcon(icon)
      button.setIconSize(Qt::Size.new(64,64))
      if item_type == :TOOL
        connect(button,
                SIGNAL('clicked()'),
                LauncherTool.new(self, text, shell_command_or_settings, capture_io, variable_parameters),
                SLOT('button_clicked()'))
      else
        connect(button,
                SIGNAL('clicked()'),
                LauncherMultitool.new(self, shell_command_or_settings),
                SLOT('button_clicked()'))
      end
      if Kernel.is_mac?
        button.setFixedSize(84,84)
      else
        stylesheet = "padding:4px; text-align:center; " \
          "font-family:#{config.tool_font_settings[0]}; " \
          "font-size:#{config.tool_font_settings[1]}px"
        button.setStyleSheet(stylesheet)
        button.setFixedSize(74,74)
      end
      label = Qt::Label.new(text)
      stylesheet = "text-align:center; " \
        "font-family:#{config.tool_font_settings[0]}; " \
        "font-size:#{config.tool_font_settings[1]}px"
      label.setStyleSheet(stylesheet)
      label.wordWrap = true
      label.setFixedWidth(70)
      label.setSizePolicy(Qt::SizePolicy::Fixed, Qt::SizePolicy::Fixed)
      label.setMinimumSize(label.sizeHint)
      label.setAlignment(Qt::AlignHCenter)
      layout.addWidget(button)
      layout.addWidget(label)
      layout.setAlignment(button, Qt::AlignHCenter)
      layout.setAlignment(label, Qt::AlignHCenter)
      widgets << layout

    when :DIVIDER
      divider = Qt::Frame.new
      divider.setFrameStyle(Qt::Frame::HLine | Qt::Frame::Raised)
      divider.setLineWidth(1)
      divider.setMidLineWidth(1)
      widgets << divider

    when :LABEL
      label = Qt::Label.new(text)
      stylesheet = "text-align:center; " \
        "font-family:#{config.label_font_settings[0]}; " \
        "font-size:#{config.label_font_settings[1]}px"
      label.setStyleSheet(stylesheet)
      widgets << label

    else
      raise "Unhandled item type from LauncherConfig: #{item_type}"
    end
  end

  # Add widgets to layout
  col = 0
  h_layout = nil
  v_layout = Qt::VBoxLayout.new
  central_widget.layout = v_layout
  widgets.each do |widget|
    if Qt::VBoxLayout === widget
      unless h_layout
        h_layout = Qt::HBoxLayout.new
        v_layout.addLayout(h_layout)
        col = 0
      end
      widget.setAlignment(Qt::AlignTop | Qt::AlignLeft)
      h_layout.addLayout(widget)
      col += 1
      if col >= config.num_columns
        col = 0
        h_layout = nil
      end
    else
      # Divider or Label
      h_layout.addStretch(0) if h_layout
      v_layout.addWidget(widget)
      h_layout = nil
    end
  end
  h_layout.addStretch(0) if h_layout
end

#initialize_menusObject



40
41
42
43
44
45
46
47
# File 'lib/cosmos/tools/launcher/launcher.rb', line 40

def initialize_menus
  # File Menu
  @file_menu = menuBar().addMenu(tr('&File'))
  @file_menu.addAction(@exit_action)

  # Help Menu
  initialize_help_menu()
end