Class: Ruber::ProjectList

Inherits:
Qt::Object
  • Object
show all
Includes:
Enumerable, PluginLike
Defined in:
lib/ruber/projects/project_list.rb

Overview

List of all open global projects

It allows to obtain a list of the open projects, to know when a project is closed and keeps trace of which is the current project. The most common usage is the following:

  • use the #project method to retrieve an open project basing on the name of its file, or to open it if it isn’t already open, or to create a new project. This will also cause the project to be added to the list of open projects, if needed

  • use #current_project= to set the current project

  • use one of the methods to iterate or work with one of the projects

  • close one of the projects with the project’s close method, or use the #close_current_project method to close the current project.

Instance Attribute Summary collapse

Attributes included from PluginLike

#plugin_description

Instance Method Summary collapse

Methods included from Enumerable

#find!

Methods included from PluginLike

#add_extensions_to_project, #add_options_to_project, #add_widgets_to_project, #plugin_name, #register_with_project, #remove_extensions_from_project, #remove_from_project, #remove_options_from_project, #remove_widgets_from_project, #restore_session, #session_data, #shutdown, #unload, #update_project

Constructor Details

#initialize(_manager, psf) ⇒ ProjectList

the component

Parameters:



91
92
93
94
95
96
# File 'lib/ruber/projects/project_list.rb', line 91

def initialize _manager, psf
  super Ruber[:app]
  initialize_plugin psf
  @current_project = nil
  @projects = {}
end

Instance Attribute Details

#current_projectRuber::Project? Also known as: current

Returns the current project or nil if there’s no open project.

Returns:

  • (Ruber::Project, nil)

    the current project or nil if there’s no open project



83
84
85
# File 'lib/ruber/projects/project_list.rb', line 83

def current_project
  @current_project
end

Instance Method Details

#[](name) ⇒ Ruber::Project? #[](file) ⇒ Ruber::Project?

Returns the project corresponding to a given file or with a given name

Overloads:

  • #[](name) ⇒ Ruber::Project?

    Returns the project with the given name project with that name exists. If more than one project with that name exist, which will be returned is arbitrary

    Parameters:

    • name (String)

      the name of the project. It *must not* start with a slash (@/@)

    Returns:

    • (Ruber::Project, nil)

      the project with name name or nil if no open

  • #[](file) ⇒ Ruber::Project?

    Returns the project corresponding to the given project file or nil if no open projects correspond to that file. Note that, unlike #project, this method doesn’t attempt to open the project corresponding to file if it isn’t already open

    Parameters:

    • file (String)

      the absolute path of the project file

    Returns:

    • (Ruber::Project, nil)

      the project corresponding to the project file file



166
167
168
169
170
171
# File 'lib/ruber/projects/project_list.rb', line 166

def [] arg
  if arg[0,1] == '/' then @projects[arg]
  else
    find{|prj| prj.project_name == arg}
  end
end

#add_project(prj) ⇒ Ruber::Project

Adds a project to the list

The #project_added signal is emitted after adding the project.

Since this method is automatically called by both #project and #new_project, you usually don’t need to call it, unless you need to create the project using @Project.new@ rather than using one of the above methods.

already in the list

Parameters:

Returns:

Raises:

  • @RuntimeError@ if a project corresponding to the same file as prj is



225
226
227
228
229
230
231
232
233
# File 'lib/ruber/projects/project_list.rb', line 225

def add_project prj
  if @projects[prj.project_file]
    raise "A project with project file #{prj.project_file} is already open"
  end
  @projects[prj.project_file] = prj
  connect prj, SIGNAL('closing(QObject*)'), self, SLOT('close_project(QObject*)')
  emit project_added(prj)
  prj
end

#close_current_projectnil

Closes the current project

If there’s not a current project, nothing is done. Otherwise, it simply calls the #close_project method passing the current project as argument.

Returns:

  • (nil)


243
244
245
246
# File 'lib/ruber/projects/project_list.rb', line 243

def close_current_project
  @current_project.close if @current_project
  nil
end

#close_project(prj) ⇒ nil

Closes a project

If the project is current, sets the current project to nil before closing it. In all cases, emits the #closing_project signal before closing the project.

Parameters:

Returns:

  • (nil)


258
259
260
261
262
263
# File 'lib/ruber/projects/project_list.rb', line 258

def close_project prj
  self.current_project = nil if @current_project == prj
  emit closing_project(prj)
  @projects.delete prj.project_file
  nil
end

#each_project {|prj| ... } ⇒ Ruber::ProjectList #each_projectEnumerator Also known as: each

Iterates on all the projects

In both versions of the method, the order in which the projects are passed to the block is arbitrary

Overloads:

  • #each_project {|prj| ... } ⇒ Ruber::ProjectList

    Passes each open project to the block in turn

    Yields:

    • (prj)

      one of the projects

    Returns:

  • #each_projectEnumerator

    in turn

    Returns:

    • (Enumerator)

      an enumerator whose @each@ method yields all the projects



112
113
114
115
# File 'lib/ruber/projects/project_list.rb', line 112

def each_project &blk #:yields: project
  res = @projects.each_value &blk
  res.same?(@projects) ? self : res
end

#new_project(file, name) ⇒ Ruber::Project

Creates a new empty project

After being created, the new project is added to the list. You almost always should use this method, rather than calling AbstractProject.new to create a new empty project.

new project

Parameters:

  • file (String)

    the absolue path of the project file to associate with the

  • name (String)

    the name of the new project

Returns:

Raises:

  • @RuntimeError@ if a file corresponding to the path file already exists



186
187
188
# File 'lib/ruber/projects/project_list.rb', line 186

def new_project file, name
  add_project Project.new( file, name )
end

#project(file) ⇒ Ruber::Project

Returns the project associated with a project file, opening it if necessary

This is one of the core methods of this class. It searchs the list of open projects and return the one corresponding to the given project file. If a project for that file isn’t open, it will be opened and added to the list of open projects.

If, for any reason, you create a project using @Project.new@ instead of using this method, you’ll need to add it to the project list yourself using #add_project.

or can’t be opened

Parameters:

  • file (String)

    the absolute path of the project file

Returns:

  • (Ruber::Project)

    the project corresponding to the project file file

Raises:



145
146
147
# File 'lib/ruber/projects/project_list.rb', line 145

def project file
  @projects[file] || add_project(Project.new(file))
end

#project_for_file(file, which = :active_only) ⇒ Ruber::Project?

Returns the project associated with a given project file

be returned only if it’s active. If it is @:all@ then it will be returned even if it’s inactive. Any other value will cause this method to always return nil if the list doesn’t contain any such file or if it doesn’t respect the value of which.

Parameters:

  • file (String)

    the path of the project file

  • which (Symbol) (defaults to: :active_only)

    if @:active_only@, the project corresponding to file will

Returns:

  • (Ruber::Project, nil)

    the project associated with the file file or nil



301
302
303
304
305
306
307
308
309
# File 'lib/ruber/projects/project_list.rb', line 301

def project_for_file file, which = :active_only
  current_prj = current
  return nil unless current_prj
  if current_prj.project_files.file_in_project?(file) then current_prj
  elsif which == :all
    find{|prj| prj.project_files.file_in_project?(file)}
  else nil
  end
end

#projectsArray<Ruber::Project>

The existing projects

wont affect the @ProjectList@

Returns:



124
125
126
# File 'lib/ruber/projects/project_list.rb', line 124

def projects
  @projects.values
end

#query_closeBoolean

Tells whether it’s all right for the projects to close the application

It calls the @query_close@ method for each project, returning false if any of them returns false and true if all return true

and false if at least one of them say the application can’t be closed

Returns:

  • (Boolean)

    true if it’s all right for the projects to close the application



285
286
287
288
# File 'lib/ruber/projects/project_list.rb', line 285

def query_close
  @projects.values.each{|pr| return false unless pr.query_close}
  true
end

#save_settingsnil

Saves each open project

Returns:

  • (nil)


271
272
273
274
# File 'lib/ruber/projects/project_list.rb', line 271

def save_settings
  @projects.values.each{|pr| pr.save}
  nil
end