Class: Ruber::ExceptionDialog::ExceptionBacktraceWidget

Inherits:
KDE::TextEdit
  • Object
show all
Defined in:
lib/ruber/exception_widgets.rb

Overview

Subclass of KDE::TextEdit specialized in displaying the backtrace of an exception.

The differences with a standard KDE::TextEdit are:

  • automatically uses a fixed width font
  • doesn’t use word wrapping
  • is always read-only
  • its #size_hint method computes (or at least tries to compute) width and height so that each line of the backtrace fits on a single line.
  • automatically extracts the message and the backtrace from the exception

Instance Method Summary (collapse)

Constructor Details

- (ExceptionBacktraceWidget) initialize(ex, parent = nil)

A new instance of ExceptionBacktraceWidget

Parameters:

  • ex (Exception)

    the exception whose backtrace should be displayed

  • parent (Qt::Widget, nil) (defaults to: nil)

    the parent widget



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruber/exception_widgets.rb', line 50

def initialize ex, parent = nil
  super parent
  self.line_wrap_mode = NoWrap
  self.read_only = true
  #Replace < and > with the corresponding HTML entities in the message
  msg = ex.message.gsub(/[<>]/){|s| s == '<' ? '&lt;' : '&gt;'}
  #Replace < and > with the corresponding HTML entities in the backtrace
  backtrace = ex.backtrace.map{|l| l.gsub(/[<>]/){|s| s == '<' ? '&lt;' : '&gt;'}}
  text = "<tt>#{msg}<br/>#{backtrace.join '<br/>'}</tt>"
  self.html = text
end

Instance Method Details

- (Qt::Size) sizeHint

Override of KDE::TextEdit#sizeHint

The width is computed as the size (using the fixed width font) of the longest line, while the height is computed as the height of a single line multiplied by the number of lines.

Both width and height are computed using Qt::FontMetrics

Returns:

  • (Qt::Size)

    the suggested size of the widget



73
74
75
76
77
78
79
# File 'lib/ruber/exception_widgets.rb', line 73

def sizeHint
  font = Qt::TextCursor.new(document).char_format.font
  fm = Qt::FontMetrics.new font
  lines = to_plain_text.split_lines
  longest = lines.sort_by{|l| l.size}[-1]
  Qt::Size.new fm.width(longest), fm.height * lines.size
end