Class: Cosmos::OpenGLBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from QtTool

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

Constructor Details

#initialize(options) ⇒ OpenGLBuilder

Constructor



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 33

def initialize (options)
  super(options) # MUST BE FIRST - All code before super is executed twice in RubyQt Based classes
  Cosmos.load_cosmos_icon("opengl_builder.png")

  @stl_scaling_factor = 1.0
  @bounds = GlBounds.new(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0)
  @scene = GlScene.new
  @previous_selection = nil

  initialize_actions()
  initialize_menus()
  initialize_central_widget()
  complete_initialize()

  @earth_scene = GlScene.new
  @earth_scene.append(EarthModel.new(0.0, 0.0, 0.0))

  @moon_scene = GlScene.new
  @moon_scene.append(MoonModel.new(0.0, 0.0, 0.0))

  statusBar.showMessage("")
end

Class Method Details

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

Runs the application



404
405
406
407
408
409
410
411
412
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 404

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 = "OpenGL Builder"
    end
    super(option_parser, options)
  end
end

Instance Method Details

#file_add_shapeObject



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 295

def file_add_shape
  dialog = Qt::Dialog.new(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
  dialog.setWindowTitle('Add Shape...')

  dialog_layout = Qt::VBoxLayout.new
  scaling_chooser = FloatChooser.new(dialog, 'STL Scaling Factor:', @stl_scaling_factor)
  file_chooser = FileChooser.new(dialog, 'STL File:', '', 'Select STL File', File.join(USERPATH, 'config', 'data'), 60, false, "STL Files (*.STL);;All Files (*)")
  dialog_layout.addWidget(scaling_chooser)
  dialog_layout.addWidget(file_chooser)

  button_layout = Qt::HBoxLayout.new
  ok = Qt::PushButton.new("Ok")
  ok.connect(SIGNAL('clicked()')) do
    dialog.accept()
  end
  button_layout.addWidget(ok)
  cancel = Qt::PushButton.new("Cancel")
  cancel.connect(SIGNAL('clicked()')) do
    dialog.reject()
  end
  button_layout.addWidget(cancel)
  dialog_layout.addLayout(button_layout)
  dialog.setLayout(dialog_layout)

  if dialog.exec == Qt::Dialog::Accepted
    filename = file_chooser.filename
    if !filename.to_s.empty?
      shape = StlShape.new(0.0, 0.0, 0.0)
      shape.stl_file = filename
      shape.color = [0.0, 0.7, 0.0, 1.0]
      shape.dragable = true
      shape.show_load_progress = true
      shape.tipText = filename
      @stl_scaling_factor = scaling_chooser.value
      shape.stl_scaling_factor = @stl_scaling_factor
      @scene.append(shape)
      @viewer.update
    end
  end
  dialog.dispose
end

#file_exportObject



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 337

def file_export
  filename = Qt::FileDialog.getSaveFileName(self, "Export Scene to File", File.join(USERPATH, 'config', 'tools', 'opengl_builder', 'scene.txt'), "Scene Files (*.txt);;All Files (*)")
  if !filename.to_s.empty?
    string  = "ZOOM #{@viewer.zoom}\n"
    orientation = @viewer.orientation
    string << "ORIENTATION #{orientation.q0} #{orientation.q1} #{orientation.q2} #{orientation.q3}\n"
    center = @viewer.center
    string << "CENTER #{center[0]} #{center[1]} #{center[2]}\n"
    string << "BOUNDS #{@bounds[0]} #{@bounds[1]} #{@bounds[2]} #{@bounds[3]} #{@bounds[4]} #{@bounds[5]}\n\n"

    @scene.each do |shape|
      string << shape.export
      string << "\n"
    end
    File.open(filename, 'w') do |file|
      file.write(string)
    end
  end
end

#file_openObject

def keyPressEvent



282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 282

def file_open
  filename = Qt::FileDialog.getOpenFileName(self, "Open Scene File:", File.join(USERPATH, 'config', 'tools', 'opengl_builder'), "Scene Files (*.txt);;All Files (*)")
  if !filename.to_s.empty?
    begin
      scene_config = SceneConfig.new(filename)
      @scene = scene_config.scene
      @viewer.scene = @scene
    rescue Exception => error
      ExceptionDialog.new(self, error, "Error Loading Scene", false)
    end
  end
end

#initialize_actionsObject



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
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 56

def initialize_actions
  super()

  # File Menu
  @file_open = Qt::Action.new(tr('&Open Scene'), self)
  @file_open_key_seq = Qt::KeySequence.new(tr('Ctrl+O'))
  @file_open.shortcut = @file_open_key_seq
  @file_open.statusTip = tr('Open Scene File')
  @file_open.connect(SIGNAL('triggered()')) { file_open() }

  @file_add_shape = Qt::Action.new(tr('&Add Shape'), self)
  @file_add_shape_key_seq = Qt::KeySequence.new(tr('Ctrl+A'))
  @file_add_shape.shortcut = @file_add_shape_key_seq
  @file_add_shape.statusTip = tr('Add a Shape to the Scene')
  @file_add_shape.connect(SIGNAL('triggered()')) { file_add_shape() }

  @file_export = Qt::Action.new(tr('&Export Scene'), self)
  @file_export_key_seq = Qt::KeySequence.new(tr('Ctrl+X'))
  @file_export.shortcut = @file_export_key_seq
  @file_export.statusTip = tr('Export Scene to File')
  @file_export.connect(SIGNAL('triggered()')) { file_export() }

  # View Menu
  @view_perspective = Qt::Action.new(tr('&Perspective'), self)
  @view_perspective_key_seq = Qt::KeySequence.new(tr('Ctrl+P'))
  @view_perspective.shortcut = @view_perspective_key_seq
  @view_perspective.statusTip = tr('Perspective View')
  @view_perspective.connect(SIGNAL('triggered()')) { view_perspective() }

  @view_top = Qt::Action.new(tr('&Top'), self)
  @view_top_key_seq = Qt::KeySequence.new(tr('Ctrl+T'))
  @view_top.shortcut = @view_top_key_seq
  @view_top.statusTip = tr('View From Above')
  @view_top.connect(SIGNAL('triggered()')) { view_top() }

  @view_bottom = Qt::Action.new(tr('&Bottom'), self)
  @view_bottom_key_seq = Qt::KeySequence.new(tr('Ctrl+B'))
  @view_bottom.shortcut = @view_bottom_key_seq
  @view_bottom.statusTip = tr('View From Below')
  @view_bottom.connect(SIGNAL('triggered()')) { view_bottom() }

  @view_front = Qt::Action.new(tr('&Front'), self)
  @view_front_key_seq = Qt::KeySequence.new(tr('Ctrl+F'))
  @view_front.shortcut = @view_front_key_seq
  @view_front.statusTip = tr('View From Front')
  @view_front.connect(SIGNAL('triggered()')) { view_front() }

  @view_back = Qt::Action.new(tr('Bac&k'), self)
  @view_back_key_seq = Qt::KeySequence.new(tr('Ctrl+W'))
  @view_back.shortcut = @view_back_key_seq
  @view_back.statusTip = tr('View From Back')
  @view_back.connect(SIGNAL('triggered()')) { view_back() }

  @view_left = Qt::Action.new(tr('&Left'), self)
  @view_left_key_seq = Qt::KeySequence.new(tr('Ctrl+L'))
  @view_left.shortcut = @view_left_key_seq
  @view_left.statusTip = tr('View From Left')
  @view_left.connect(SIGNAL('triggered()')) { view_left() }

  @view_right = Qt::Action.new(tr('&Right'), self)
  @view_right_key_seq = Qt::KeySequence.new(tr('Ctrl+R'))
  @view_right.shortcut = @view_right_key_seq
  @view_right.statusTip = tr('View From Right')
  @view_right.connect(SIGNAL('triggered()')) { view_right() }

  # Show Menu
  @show_scene = Qt::Action.new(tr('Show &Scene'), self)
  @show_scene_key_seq = Qt::KeySequence.new(tr('Ctrl+S'))
  @show_scene.shortcut = @show_scene_key_seq
  @show_scene.statusTip = tr('Show the Normal Scene')
  @show_scene.connect(SIGNAL('triggered()')) { show_scene() }

  @show_earth = Qt::Action.new(tr('Show &Earth'), self)
  @show_earth_key_seq = Qt::KeySequence.new(tr('Ctrl+E'))
  @show_earth.shortcut = @show_earth_key_seq
  @show_earth.statusTip = tr('Show the Earth')
  @show_earth.connect(SIGNAL('triggered()')) { show_earth() }

  @show_moon = Qt::Action.new(tr('Show &Moon'), self)
  @show_moon_key_seq = Qt::KeySequence.new(tr('Ctrl+M'))
  @show_moon.shortcut = @show_moon_key_seq
  @show_moon.statusTip = tr('Show the Moon')
  @show_moon.connect(SIGNAL('triggered()')) { show_moon() }
end

#initialize_central_widgetObject



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

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

  @viewer = GlViewer.new(self)
  @viewer.draw_axis = 15
  @viewer.scene = @scene
  @viewer.selection_callback = method(:selection_callback)
  @top_layout.addWidget(@viewer)
end

#initialize_menusObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 141

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

  # View Menu
  @view_menu = menuBar.addMenu(tr('&View'))
  @view_menu.addAction(@view_perspective)
  @view_menu.addAction(@view_top)
  @view_menu.addAction(@view_bottom)
  @view_menu.addAction(@view_front)
  @view_menu.addAction(@view_back)
  @view_menu.addAction(@view_left)
  @view_menu.addAction(@view_right)

  # Show Menu
  @show_menu = menuBar.addMenu(tr('&Show'))
  @show_menu.addAction(@show_scene)
  @show_menu.addAction(@show_earth)
  @show_menu.addAction(@show_moon)

  # Help Menu
  @about_string = "OpenGL Builder is an example application using OpenGL"
  initialize_help_menu()
end

#keyPressEvent(event) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 185

def keyPressEvent(event)
  selection = @viewer.selection
  if selection
    color = selection.base_color.clone

    case event.text
    when 'x'
      rotation = selection.rotation_x
      rotation = 0.0 unless rotation
      selection.rotation_x = rotation + 1.0

    when 'X'
      rotation = selection.rotation_x
      rotation = 0.0 unless rotation
      selection.rotation_x = rotation - 1.0

    when 'y'
      rotation = selection.rotation_y
      rotation = 0.0 unless rotation
      selection.rotation_y = rotation + 1.0

    when 'Y'
      rotation = selection.rotation_y
      rotation = 0.0 unless rotation
      selection.rotation_y = rotation - 1.0

    when 'z'
      rotation = selection.rotation_z
      rotation = 0.0 unless rotation
      selection.rotation_z = rotation + 1.0

    when 'Z'
      rotation = selection.rotation_z
      rotation = 0.0 unless rotation
      selection.rotation_z = rotation - 1.0

    when *%w(r R g G b B a A)
      index = 0 # r R
      index = 1 if %w(g G).include?(event.text)
      index = 2 if %w(b B).include?(event.text)
      index = 3 if %w(a A).include?(event.text)

      if %w(R G B A).include?(event.text)
        value = 1.0
        increment = 0.05
      else
        value = 0.0
        # Minimum alpha value shouldn't go to 0 or it disappears
        value = 0.05 if event.text == 'a'
        increment = -0.05
      end

      color[index] += increment
      if value == 1.0
        color[index] = value if color[index] > value
      else
        color[index] = value if color[index] < value
      end
      selection.base_color = color.clone
      selection.color = selection.base_color.clone
      statusBar.showMessage("Color: [#{color[0]} #{color[1]} #{color[2]} #{color[3]}]")

    # TODO: Mouseover tip text is currently not supported in QT OpenGL
    #when 't'
    #  dialog = Qt::Dialog.new(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
    #  dialog.setWindowTitle('Set Tip Text...')

    #  dialog_layout = Qt::VBoxLayout.new
    #  string_chooser = StringChooser.new(dialog, 'TipText:', selection.tipText.to_s, 60, true)
    #  dialog_layout.addWidget(string_chooser)

    #  button_layout = Qt::HBoxLayout.new
    #  ok = Qt::PushButton.new("Ok")
    #  ok.connect(SIGNAL('clicked()')) { dialog.accept() }
    #  button_layout.addWidget(ok)
    #  cancel = Qt::PushButton.new("Cancel")
    #  cancel.connect(SIGNAL('clicked()')) { dialog.reject() }
    #  button_layout.addWidget(cancel)
    #  dialog_layout.addLayout(button_layout)
    #  dialog.setLayout(dialog_layout)

    #  if dialog.exec == Qt::Dialog::Accepted
    #    tipText = string_chooser.string
    #    if tipText.to_s.empty?
    #      selection.tipText = nil
    #    else
    #      selection.tipText = tipText
    #    end
    #  end
    #  dialog.dispose

    end # case event.text

  end # if selection

end

#selection_callback(shape) ⇒ Object



397
398
399
400
401
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 397

def selection_callback(shape)
  @previous_selection.color = @previous_selection.base_color.clone if @previous_selection
  shape.color = [1.0, 0.0, 0.0, 1.0] if shape
  @previous_selection = shape
end

#show_earthObject



389
390
391
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 389

def show_earth
  @viewer.scene = @earth_scene
end

#show_moonObject



393
394
395
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 393

def show_moon
  @viewer.scene = @moon_scene
end

#show_sceneObject



385
386
387
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 385

def show_scene
  @viewer.scene = @scene
end

#view_backObject



373
374
375
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 373

def view_back
  @viewer.orientation = Quaternion.new([1.0, 0.0, 0.0], PI)
end

#view_bottomObject



365
366
367
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 365

def view_bottom
  @viewer.orientation = Quaternion.new([1.0, 0.0, 0.0], -PI/2)
end

#view_frontObject



369
370
371
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 369

def view_front
  @viewer.orientation = Quaternion.new([1.0, 0.0, 0.0], 0.0)
end

#view_leftObject



377
378
379
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 377

def view_left
  @viewer.orientation = Quaternion.new([0.0, 1.0, 0.0], PI/2)
end

#view_perspectiveObject



357
358
359
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 357

def view_perspective
  @viewer.orientation = Quaternion.new([1.0, 1.0, 0.0], 0.8)
end

#view_rightObject



381
382
383
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 381

def view_right
  @viewer.orientation = Quaternion.new([0.0, 1.0, 0.0], -PI/2)
end

#view_topObject



361
362
363
# File 'lib/cosmos/tools/opengl_builder/opengl_builder.rb', line 361

def view_top
  @viewer.orientation = Quaternion.new([1.0, 0.0, 0.0], PI/2)
end