Method: Ruber::OutputWidget::Model#insert
- Defined in:
- lib/ruber/output_widget.rb
#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::StandardItemthe 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 |