Class: Cosmos::DataViewerComponent

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

Direct Known Subclasses

DumpComponent, TextComponent

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



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

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
end

Instance Attribute Details

#packetsObject (readonly)

Returns the value of attribute packets.



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

def packets
  @packets
end

#tab_nameObject (readonly)

Returns the value of attribute tab_name.



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

def tab_name
  @tab_name
end

#textObject (readonly)

Returns the value of attribute text.



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

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



36
37
38
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 36

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

#initialize_guiObject

Builds the gui for this component



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

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)
  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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 101

def process_packet(packet)
  processed_text = ''
  processed_text << '*' * 80 << "\n"
  processed_text << "* #{packet.target_name} #{packet.packet_name}\n"
  processed_text << "* Packet Time: #{packet.packet_time.formatted}\n" if packet.packet_time
  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



85
86
87
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 85

def reset
  @text.setPlainText("")
end

#showEvent(event) ⇒ Object

QT method called when this widget is displayed



90
91
92
93
94
95
96
97
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 90

def showEvent(event)
  # When the tab is shown we want to ensure the cursor is visible
  # 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.
  @text.ensureCursorVisible()
end

#shutdownObject

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



117
118
119
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 117

def shutdown
  # Do nothing by default, this should be overridden by subclasses
end

#update_guiObject

Updates the gui with any changes if needed



73
74
75
76
77
78
79
80
81
82
# File 'lib/cosmos/tools/data_viewer/data_viewer_component.rb', line 73

def update_gui
  begin
    loop do
      # Get new processed text and append to text
      @text.appendPlainText(@processed_queue.pop(true))
    end
  rescue ThreadError
    # Nothing to do
  end
end