Class: Cosmos::DataViewerComponent

Inherits:
Qt::Widget show all
Defined in:
lib/cosmos/tools/data_viewer/data_viewer_component.rb

Direct Known Subclasses

DumpComponent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, tab_name) ⇒ DataViewerComponent

Create a component to go inside the DataViewer

Parameters:

  • parent (Qt::Widget)

    Parent widget

  • tab_name (String)

    Name of the tab which displays this widget



24
25
26
27
28
29
30
31
32
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 24

def initialize(parent, tab_name)
  super(parent)
  @tab_name = tab_name
  @packets = []
  @processed_queue = Queue.new
  @log_file_directory = System.paths['LOGS']
  @max_block_count = 1000
  @timer = nil
end

Instance Attribute Details

#packetsObject (readonly)

Returns the value of attribute packets.



17
18
19
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 17

def packets
  @packets
end

#tab_nameObject (readonly)

Returns the value of attribute tab_name.



16
17
18
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 16

def tab_name
  @tab_name
end

#textObject (readonly)

Returns the value of attribute text.



18
19
20
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 18

def text
  @text
end

Instance Method Details

#add_packet(target_name, packet_name) ⇒ Object

Adds a packet to the list of packets this components processes

Parameters:

  • target_name (String)

    Name of the target

  • packet_name (String)

    Name of the packet



38
39
40
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 38

def add_packet(target_name, packet_name)
  @packets << [target_name, packet_name]
end

#initialize_guiObject

Builds the gui for this component



43
44
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
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 43

def initialize_gui
  @top_layout = Qt::VBoxLayout.new

  @text = Qt::PlainTextEdit.new
  @text.setReadOnly(true)
  @text.setMaximumBlockCount(@max_block_count)
  @text.font = Cosmos.get_default_small_font
  @text.setWordWrapMode(Qt::TextOption::NoWrap)
  @top_layout.addWidget(@text)

  @button = Qt::PushButton.new('Save Text to File')
  @button.connect(SIGNAL('clicked()')) do
    Qt.execute_in_main_thread(true) do
      filename = File.join(@log_file_directory, File.build_timestamped_filename(['dataviewer']))

      # Prompt user for filename
      filename = Qt::FileDialog::getSaveFileName(self, "Save As...", filename, "Text Files (*.txt);;All Files (*)")
      if not filename.nil? and not filename.empty?
        @log_file_directory = File.dirname(filename)
        @log_file_directory << '/' unless @log_file_directory[-1..-1] == '\\'

        File.open(filename, 'w') do |file|
          file.write(@text.toPlainText)
        end
      end
    end
  end
  @top_layout.addWidget(@button)

  @timer = Qt::Timer.new(self)
  @timer.connect(SIGNAL('timeout()')) { scroll_to_bottom() }
  @timer.setSingleShot(true)

  setLayout(@top_layout)
end

#process_packet(packet) ⇒ Object

Processes the given packet. No gui interaction should be done in this method. Override this method for other components.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 114

def process_packet(packet)
  processed_text = ''
  processed_text << '*' * 80 << "\n"
  processed_text << "* #{packet.target_name} #{packet.packet_name}\n"
  processed_text << "* Received Time: #{packet.received_time.formatted}\n" if packet.received_time
  processed_text << "* Received Count: #{packet.received_count}\n"
  processed_text << '*' * 80 << "\n"
  processed_text << packet.formatted(:WITH_UNITS) << "\n"
  # Ensure that queue does not grow infinitely while paused
  if @processed_queue.length < 1000
    @processed_queue << processed_text
  end
end

#resetObject

Resets the gui and any intermediate processing



95
96
97
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 95

def reset
  @text.setPlainText("")
end

#scroll_to_bottomObject



108
109
110
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 108

def scroll_to_bottom
  @text.verticalScrollBar.value = @text.verticalScrollBar.maximum
end

#showEvent(event) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 99

def showEvent(event)
  # When the tab is shown we want to ensure the scroll bar is at the
  # maximum to allow the PlainTextArea to automatically hold the scroll
  # at the bottom of the display while appending things.
  # If this is not done, switching tabs will cause the scroll bar
  # to "stick" and not stay at the bottom with the newest text.
  @timer.start(100)
end

#shutdownObject

Shutdown the Data Viewer Component. Called when program is closed.



129
130
131
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 129

def shutdown
  # Do nothing by default
end

#update_guiObject

Updates the gui with any changes if needed



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 80

def update_gui
  begin
    loop do
      # Get new processed text
      processed_text = @processed_queue.pop(true)

      # Add text to text widget
      @text.appendPlainText(processed_text)
    end
  rescue ThreadError
    # Nothing to do
  end
end