Class: Ruber::ProjectFilesRuleChooser

Inherits:
Qt::Widget
  • Object
show all
Defined in:
lib/ruber/application/project_files_widget.rb,
lib/ruber/world/project_files_widget.rb

Overview

Widget which displays and allows to modify the rules which determine whether a file belongs to a project or not.

It contains a Qt::TreeView where the rules are displayed and some buttons to add and remove rules. The tree view has two columns: the first displays the contents of the rules, while the second contains the rules’ type (whether they’re regexp rules or file rules).

See Also:

Defined Under Namespace

Classes: RegexpRuleValidator

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Signal Summary

Constructor Details

- (ProjectFilesRuleChooser) initialize(parent = nil)

A new instance of ProjectFilesRuleChooser

Parameters:

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

    the parent widget



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruber/application/project_files_widget.rb', line 83

def initialize parent = nil
  super 
  @project = nil
  @ui = Ui::ProjectFilesRuleChooser.new
  @ui.setupUi self
  model = Qt::StandardItemModel.new @ui.rules_widget
  @ui.rules_widget.model = model
  model.horizontal_header_labels = %w[Pattern Type]
  connect @ui.add_regexp_btn, SIGNAL('clicked()'), self, SLOT('add_regexp_rule()')
  connect @ui.add_path_btn, SIGNAL('clicked()'), self, SLOT('add_path_rule()')
  connect @ui.remove_rule_btn, SIGNAL('clicked()'), self, SLOT('remove_rule()')
  connect @ui.rules_widget.selection_model, SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self, SLOT('change_button_state()')
  @ui.remove_rule_btn.enabled = false
end

Instance Attribute Details

- (Ruber::Project) project=(value) (writeonly)

The project the widget displays rules for

Returns:



49
50
51
# File 'lib/ruber/application/project_files_widget.rb', line 49

def project=(value)
  @project = value
end

Instance Method Details

- (nil) add_path_rule (private)

Adds a file rule to the list

A dialog is shown to the user to select the file to add to the project

Returns:

  • (nil)


192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ruber/application/project_files_widget.rb', line 192

def add_path_rule
  rule = KDE::FileDialog.get_open_file_name KDE::Url.from_path(@project.project_dir),
      '', self, "Add files to project"
  return unless rule
  path = Pathname.new(rule)
  if path.child_of?(@project.project_dir)
    prj_dir = Pathname.new(@project.project_dir)
    rule = path.relative_path_from(prj_dir).to_s
  end
  add_rule rule, 'Path'
  nil
end

Slot Signature:

add_path_rule()

- (nil) add_regexp_rule (private)

Adds a regexp rule to the list

The user is shown a dialog to choose the regexp

Returns:

  • (nil)


212
213
214
215
216
217
218
# File 'lib/ruber/application/project_files_widget.rb', line 212

def add_regexp_rule
  rule = KDE::InputDialog.get_text "Add regexp rule", "Regexp", 
      :validator => RegexpRuleValidator.new(self)
  return unless rule
  add_rule rule, 'Regexp'
  nil
end

Slot Signature:

add_regexp_rule()

- ((Qt::StandardItem, Qt::StandardItem)) add_rule(text, type) (private)

Adds a rule to the tree view

The #rules_edited signal will be emitted after the rule has been inserted

be the path of the file for a file rule and the source of the regexp for a regexp rule Regexp or File model

Parameters:

  • text (String)

    the text to insert in the first column of the view. It must

  • the (String)

    name of the type of rule. Currently, this should be either

Returns:



162
163
164
165
166
167
168
169
170
# File 'lib/ruber/application/project_files_widget.rb', line 162

def add_rule text, type
  row = [Qt::StandardItem.new(text), Qt::StandardItem.new(type)]
  @ui.rules_widget.model.append_row row
  @ui.rules_widget.selection_model.select row[0].index, 
        Qt::ItemSelectionModel::SelectCurrent| Qt::ItemSelectionModel::Rows |
        Qt::ItemSelectionModel::Clear
  emit rules_edited
  row
end

- (nil) change_button_state (private)

Enables or disables the the Remove button

The button is enabled if a rule is selected and disabled otherwise

Returns:

  • (nil)


179
180
181
182
183
# File 'lib/ruber/application/project_files_widget.rb', line 179

def change_button_state
  @ui.remove_rule_btn.enabled = 
      !@ui.rules_widget.selection_model.selected_rows.first.nil?
  nil
end

Slot Signature:

change_button_state()

- (nil) remove_rule (private)

Removes the selected rule from the list

The #rules_edited signal is emitted after removing the rule

Note: this method doesn’t check whether there’s an item selected and will fail if it isn’t so

Returns:

  • (nil)


229
230
231
232
233
234
# File 'lib/ruber/application/project_files_widget.rb', line 229

def remove_rule
  row = @ui.rules_widget.selection_model.selected_rows.first
  @ui.rules_widget.model.remove_row row.row
  emit rules_edited
  nil
end

Slot Signature:

remove_rule()

- (Array<String,Regexp>) rules

The rules chosen by the user

String entries correspond to file rules, while regexp entries correspond to regexp rules

Returns:

  • (Array<String,Regexp>)

    an array containing the rules the user has chosen.



114
115
116
117
118
119
120
# File 'lib/ruber/application/project_files_widget.rb', line 114

def rules
  @ui.rules_widget.model.enum_for(:each_row).map do |rule, type|
    if type.text == 'Path' then rule.text
    else Regexp.new rule.text
    end
  end
end

- (nil) rules=(list)

Fills the tree view

Note: calling this method won’t trigger the #rules_edited signal

entries will be considered as file rules, while regexp entries will be considered regexp rules

Parameters:

  • list (Array<String,Regexp>)

    a list of the rules to insert in the view. String

Returns:

  • (nil)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ruber/application/project_files_widget.rb', line 132

def rules= list
  model = @ui.rules_widget.model
  model.clear
  model.horizontal_header_labels = %w[Pattern Type]
  list.each do |rule|
    type = 'Path'
    if rule.is_a? Regexp
      type = 'Regexp'
      rule = rule.source
    end
    model.append_row [Qt::StandardItem.new(rule), Qt::StandardItem.new(type)]
  end
  nil
end

- (Qt::Size) sizeHint

Override of Qt::Widget#sizeHint

Returns:

  • (Qt::Size)

    the optimal size to use (350×200)



103
104
105
# File 'lib/ruber/application/project_files_widget.rb', line 103

def sizeHint
  Qt::Size.new(350,200)
end

Signal Details

- rules_edited

Signal emitted when the rules have been changed