Class: Ruber::OutputWidget::Model

Inherits:
Qt::StandardItemModel show all
Defined in:
lib/ruber/output_widget.rb

Overview

Convenience class to use instead of Qt::StandardItem as model for OutputWidget.

It provides three methods which make easier to insert items in the widget: insert, insert_lines and set. Besides, it allows to set the item flags globally, using the global_flags attribute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Qt::StandardItemModel

#each, #each_row

Constructor Details

#initialize(widget, parent = nil) ⇒ Model

Creates a new instance. widget is the output widget which will use the model. parent is the parent object



928
929
930
931
932
# File 'lib/ruber/output_widget.rb', line 928

def initialize widget, parent = nil
  super parent
  @output_widget = widget
  @global_flags = (Qt::ItemIsEnabled | Qt::ItemIsSelectable).to_i
end

Instance Attribute Details

#global_flagsObject

The flags to use for all valid indexes (always converted to an integer). If this is nil, then flags will revert to Qt::StandardModel behaviour. The default value is Qt::ItemIsEnabled|Qt::ItemIsSelectable



922
923
924
# File 'lib/ruber/output_widget.rb', line 922

def global_flags
  @global_flags
end

Instance Method Details

#insert(text, type, row, opts = {}) ⇒ Object

Inserts a new row in the model and sets the output type of its elements.

opts can contain two keys:

:parent

the Qt::StandardItem the new row should be child of. If not given, the new row will be a top-level row

:col

the column where to put the text (default: 0). It has effect only if text is a string (see below)

text represents the contents of the new row and can be either a string or an array containing strings and nils.

If text is an array, each entry of the array will become a column in the new row. with a text, while nil entries will produce empty items (that is items without text. Calling item.text on these items will give nil. The associated indexes, however, are valid).

If text is a string, the resulting row will have all the elements from column 0 to the one before the :col entry set to empty element (as described above). The column :col has text text. Of course, if :col is 0 (or is missing) no empty items are created.

type is the output type to give to the items in the new row. It can be either a symbol or an array of symbols. If it is a symbol, it will be the output type of all elements in the new row. If it is an array, each entry will be the type of the corresponding non-empty item (that is of the item in text which has the same index after removing all nil elements from text). If type is longer than the text array, the entries in excess are ignored (if text is a string, it behaves as an array of size 1 in this regard). If type is shorter than the text array, the entries in excess won’t have their output type set.

row is the index where the new row should be put. If nil, the new row will be appended to the model.

If row or :col are negative, they’re counted from the end. That is, the actual row index if row is negative is row_count+row. The same happens with :col.

If row is greater than row_count (or negative and its absolute value is greater than row_count), IndexError is raised. This is because Qt::StandardItemModel doesn’t allow for a row to be inserted after the last one. This doesn’t happen for the columns, which are added automatically.

This method returns an array containing all the non-empty items of the new row.



1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
# File 'lib/ruber/output_widget.rb', line 1036

def insert text, type, row, opts = {}
  parent = opts[:parent] || self
  rc = parent.row_count
  row ||= rc
  row =  rc + row if row < 0
  col = opts[:col] || 0
  cc = parent.column_count
  col = cc + col if col < 0
  if row < 0 or row > rc 
    raise IndexError, "Row index #{row} is out of range. The allowed values are from 0 to #{rc}"
  end
  text = Array.new(col) << text unless text.is_a? Array
  items = text.map do |i| 
    it = i ? Qt::StandardItem.new(i) : Qt::StandardItem.new
    it.flags = @global_flags if @global_flags
    it
  end
  parent.insert_row row, items
  items.delete_if{|i| i.text.nil?}
  type = Array.new(items.size, type) unless type.is_a? Array
  items.each_with_index do |it, i|
    @output_widget.set_output_type it.index, type[i]
  end
  items
end

#insert_lines(text, type, row, opts = {}) ⇒ Object

Similar to insert, but inserts each line of text in a different item, one below the other, starting at the position given by the row and opts argument.

row and opts have the same meaning as in insert.

text can be either a string or an array of strings. If it is a string, it will be split into lines, while if it is an array, each entry of the array will be considered a single line (even if it contains newlines). In both cases, a single string is passed to insert for each line.

type is the output type to assign to each item and should be a symbol. The same type is used for all lines.



1076
1077
1078
1079
1080
1081
1082
# File 'lib/ruber/output_widget.rb', line 1076

def insert_lines text, type, row, opts = {}
  lines = text.is_a?(Array) ? text : text.split("\n")
  lines.each do |l|
    insert l, type, row, opts
    row += 1 if row
  end
end

#set(text, type, row, opts = {}) ⇒ Object

Changes content of the given element.

It creates a new Qt::StandardItem containing the text text, inserts it in the model, sets the output type of the corresponding index to type and changes its flags to make it enabled and selectabled.

row is an integer corresponding to the row where the item should be put. If opts contains the :col entry, it represents the colun of the new item (if this option is missing, the column is 0). If opts contains the :parent entry, it is the parent item (not index) of the new one. If row and/or the :col entry are negative, they’re counted from backwards.

Note that, if an item with the given row, column and parent already exist, it is replaced by the new item.

Returns the new item.



977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/ruber/output_widget.rb', line 977

def set text, type, row, opts = {}
  col = opts[:col] || 0
  parent = opts[:parent]
  it = Qt::StandardItem.new(text)
  it.flags = @global_flags if @global_flags
  row = (parent || self).row_count + row if row < 0
  col = (parent || self).column_count + col if col < 0
  if parent then parent.set_child row, col, it
  else set_item row, col, it
  end
  @output_widget.set_output_type it.index, type
  it
end