|Home | Tutorial | Classes | Functions | QSA Workbench | Language | Qt API | QSA Articles | Qt Script for Applications |

# QSProject Class Reference

The QSProject class provides project management over scripts written in Qt Script for Applications. More...

`#include <qsproject.h>`

List of all member functions.

## Public Members

- enum StorageMode { Bundle, TextFiles }
- QSProject ( QObject * parent = 0, const char * name = 0 )
- bool editorsModified () const
- bool scriptsModified () const
- QSInterpreter * interpreter () const
- QStringList scriptNames () const
- QPtrList<QSScript> scripts () const
- QSScript * script ( const QString & name ) const
- QSScript * script ( QObject * context ) const
- QObject * object ( const QString & name ) const
- QObjectList objects () const
- QSScript * createScript ( QObject * context, const QString & code = QString::null )
- QSScript * createScript ( const QString & name, const QString & code = QString::null )
- void addSignalHandler ( QObject * sender, const char * signal, QObject * receiver, const char * qtscriptFunction )
- void addSignalHandler ( QObject * sender, const char * signal, const char * qtscriptFunction )
- void removeSignalHandler ( QObject * sender, const char * signal, QObject * receiver, const char * qtscriptFunction )
- void removeSignalHandler ( QObject * sender, const char * signal, const char * qtscriptFunction )
- QSEditor * activeEditor () const
- QPtrList<QSEditor> editors () const
- QSEditor * editor ( QSScript * script ) const
- QSEditor * createEditor ( QSScript * script, QWidget * parent = 0, const char * name = 0 )
- void setStorageMode ( StorageMode mode )
- StorageMode storageMode () const

## Public Slots

- bool load ( const QString & projectFile )
- bool save ( const QString & projectFile = QString::null )
- bool loadFromData ( QByteArray projectData )
- bool saveToData ( QByteArray data )
- void clearObjects ()
- void addObject ( QObject * object )
- void removeObject ( const QObject * object )
- void commitEditorContents ()
- void revertEditorContents ()

## Signals

- void editorTextChanged ()
- void projectChanged ()
- void projectEvaluated ()

## Detailed Description

The QSProject class provides project management over scripts written in Qt Script for Applications.

The project's interpreter can be accessed through the function interpreter().

Scripts are encapsulated as instances of the class QSScript. QSScript objects are created by using the createScript() functions. It is possible to query the project for scripts using the functions scripts(), scriptNames(), and script().

It is possible to associate a QSScript with a context object, meaning that the script will be evaluated in the context of that object. If a script and an object have the same name, they are grouped together.

Objects are added to the project with addObject() and removed from the project with removeObject(). To query the project's objects, use the object() and objects() functions. Note that objects added to the project become persistant, meaning that when the interpreter is cleared, they will still be scriptable. This is in contrast to objects added to the interpreter using the QSInterpreter::addTransientObject() function, which will not be available after the interpreter is cleared.

A project can be stored on disk and loaded again using the load() and save() functions. It is also possible to write the project to a data buffer that can be used in conjunction with other applications by using the loadFromData() and saveToData() functions.

The project also manages editors for the scripts it holds. Editors can be created using createEditor(), and can be queried using the editors(), editor(), and activeEditor() functions. When text changes in the interpreter, the editorsModified() signal is emitted. Before the changes are reflected in the script, the commitEditorContents() function must be called. To revert the editors to the code in the scripts use revertEditorContents().

When the editors are committed, the scripts will be out of sync with the state of the interpreter. The scriptsModified() signal is emitted to indicate this situation.

Several actions in the interpreter will trigger re-evaluation of the project; consequently, the interpreter will be cleared and the scripts re-evaluated. Such actions include modifying scripts and removing objects. For this reason it is unwise to have executing script code (such as calling application functionality) outside of functions when using a QSProject. Non-executing script code (such as declaration of global variables) is safe.

The addSignalHandler() and removeSignalHandler() functions can be used to connect and disconnect signals and slots from C++ to Qt Script.

Note on threading; The QSProject class cannot be used outside the GUI thread.

See the Manual for more explanations and examples.

See also QSScript, QSEditor, and QSInterpreter.

## Member Type Documentation

### QSProject::StorageMode

The StorageMode enum describes the format of the project files when they are stored with QSProject::save()

- `QSProject::Bundle` - The project file is saved as one binary bundle. This format is easily portable, but the scripts cannot be edited in an external editor (This was the default for QSA 1.0).
- `QSProject::TextFiles` - The project file stores only the script file name, and saves the script file as a separate file. This allows editing of script files with an external editor. This is the default for QSA 1.1 and later.

## Member Function Documentation

### QSProject::QSProject ( QObject * parent = 0, const char * name = 0 )

Constructs a QSProject with parent parent and name being passed to the QObject constructor.

### QSEditor * QSProject::activeEditor () const

Returns the editor which is currently editing the project; if no editor is active returns 0.

### void QSProject::addObject ( QObject * object )`[slot]`

Makes the QObject object available to the scripting engine. All child objects of object are also made available, recursivly.

If no object in the parent hierarchy of object has been added via addObject(), object will be made available as a toplevel object to the programmer and will be accessible via `Application.object_name` (where `object_name` is the value of object's QObject::name() property).

If an object in the parent hierarchy of object has been made available via addObject(), object will not be made available as a toplevel object. It is then accessible through `parent1.parent2.object_name` in the scripting language, assuming that `parent1` has previously been made available via addObject(). The reason to make an object available this way even though it is not made available as a toplevel object, is so that code can be added in the context of that object.

Objects added with this function are persistent. This means that when the interpreter is cleared, either by calling QSInterpreter::clear() or by modifying the scripts, these objects will remain available to the scripting engine.

If a script exists in the project that has the same name as object, the script will be evaluated with object as context.

Note on threading; If the interpreter is running in the non-GUI thread, object cannot be a QWidget subclass.

Warning: Every object passed to this function must have a unique name.

See also QSInterpreter::addTransientObject(), removeObject(), clearObjects(), and object().

### void QSProject::addSignalHandler ( QObject * sender, const char * signal, QObject * receiver, const char * qtscriptFunction )

Adds the Qt Script function qtscriptFunction in the context of receiver as a signal handler for the C++ signal signal of the object sender.

Example:

      project->addSignalHandler(myButton, SIGNAL(clicked()), document, "startCalculation");

See also removeSignalHandler().

### void QSProject::addSignalHandler ( QObject * sender, const char * signal, const char * qtscriptFunction )

Adds the Qt Script function qtscriptFunction (fully qualified) as a signal handler for the C++ signal signal of the object sender.

Example:

      project->addSignalHandler(myButton, SIGNAL(clicked()), "a.b.startCalculation");

See also removeSignalHandler().

### void QSProject::clearObjects ()`[slot]`

Removes all application objects from the list of available objects.

QSProject does not take ownership of the objects, so the objects are not deleted.

See also addObject(), removeObject(), object(), and QSInterpreter::addTransientObject().

### void QSProject::commitEditorContents ()`[slot]`

Updates the scripts with the content of the editors.

For each editor that is changed, commit() is called, propagating the changes in the editor into the script it is currently editing.

After a call to commitEditorContents, the project will no longer be modified.

See also editorsModified() and revertEditorContents().

### QSEditor * QSProject::createEditor ( QSScript * script, QWidget * parent = 0, const char * name = 0 )

Creates a new editor for the script script with the parent parent and name name.

If script does not belong to the current project, 0 is returned.

If an editor already exists for the given script, the existing editor is returned.

### QSScript * QSProject::createScript ( QObject * context, const QString & code = QString::null )

Creates a script with context context and source code code.

The context is added to this project's list of objects.

If a script already exists for this context, 0 is returned.

### QSScript * QSProject::createScript ( const QString & name, const QString & code = QString::null )

Creates a script called name and source code code without a context.

If an object exists in the project with the same name as name, the object is associated with the created script and the script will be evaluated in the context of that object.

If a script already exists with this name, 0 is returned.

### QSEditor * QSProject::editor ( QSScript * script ) const

Returns the editor for the script script if the editor exists; otherwise returns 0.

### void QSProject::editorTextChanged ()`[signal]`

This signal is emitted whenever text changes in one of the editors that the project currently manages.

### QPtrList<QSEditor> QSProject::editors () const

Returns a list of all the editors for the scripts in this project.

### bool QSProject::editorsModified () const

Returns TRUE if the project is modified; otherwise returns FALSE.

A project enters the modified state when the editors and scripts become out of sync. This will occur, for example, when a user edits a script in an editor. Before the interpreter is used, this value should be checked and the application should decide to either commit or revert the changes in the editor before the project is evaluated.

See also commitEditorContents() and revertEditorContents().

### QSInterpreter * QSProject::interpreter () const

Returns the interpreter which is used to evaluate code for this project.

### bool QSProject::load ( const QString & projectFile )`[slot]`

Reads and parses the Qt Script for Applications project contained in the file, projectFile.

If an error occurs while running or parsing the script, actions according to the errorMode() are taken and the error() signal is emitted.

After load() is called, functions and classes defined in this project can be called and used through the evaluate() and call() functions.

If the project has signal/slot connections, for example, a form that connects widget signals to Qt Script slots, then the connections are established as part of the open() process, if the objects are already known to the interpreter via QSProject::addObject() or QSInterpreter::addTransientObject().

### bool QSProject::loadFromData ( QByteArray projectData )`[slot]`

Reads and parses the Qt Script for Applications project from projectData.

If an error occurs while running or parsing the script, actions according to the errorMode() are taken and the error() signal is emitted.

After loadFromData() is called, functions and classes defined in this project can be called and used through the evaluate() and call() functions.

If the project has signal/slot connections, for example, a form that connects widget signals to Qt Script slots, then the connections are established as part of the process.

### QObject * QSProject::object ( const QString & name ) const

Returns the persistent object in the project called name.

### QObjectList QSProject::objects () const

Returns the list of persistent objects in the project.

### void QSProject::projectChanged ()`[signal]`

This signal is emitted whenever the project has changed in such a way that it is out of sync with the interpreter and needs to be re-evaluated before the interpreter is used.

### void QSProject::projectEvaluated ()`[signal]`

This signal is emitted whenever the project has been evaluated and the interpreter back in sync with the scripts in the project.

### void QSProject::removeObject ( const QObject * object )`[slot]`

Removes the persistent QObject object from the list of available objects for scripting.

Note that only persistent objects can be removed with this function. Objects added to the interpreter using QSInterpreter::addTransientObject can not be removed with this function.

If object is the context of a script, the script will also be removed from the project.

See also addObject(), clearObjects(), and object().

### void QSProject::removeSignalHandler ( QObject * sender, const char * signal, QObject * receiver, const char * qtscriptFunction )

Removes the connection between the signal signal of the object sender and the signal handler qtscriptFunction in the context receiver.

See also addSignalHandler().

### void QSProject::removeSignalHandler ( QObject * sender, const char * signal, const char * qtscriptFunction )

Removes the connection between the signal signal of the object sender and the fully qualified signal handler qtscriptFunction.

See also addSignalHandler().

### void QSProject::revertEditorContents ()`[slot]`

Reverts the text in the editors to the state of the scripts.

After a call to revertEditorContents, the project will no longer be modified.

See also editorsModified() and commitEditorContents().

### bool QSProject::save ( const QString & projectFile = QString::null )`[slot]`

Saves the scripts project to the file projectFile

The default for projectFile is QString::null, in which case the filename used during load will be used.

See also load().

### bool QSProject::saveToData ( QByteArray data )`[slot]`

Writes the the whole project to the data block data.

This is useful for keeping the project data with a document. For example, you can take the data returned by this function and store it in a document's file. Later, when the document is opened, you can extract the data from the document and pass it to the overload of openFromData() that takes a QByteArray to reconstruct the project.

See also loadFromData().

### QSScript * QSProject::script ( const QString & name ) const

Returns the script called name if it exists in the project; otherwise returns 0.

### QSScript * QSProject::script ( QObject * context ) const

Returns the script that has context as context if it exists in the project; otherwise returns 0.

### QStringList QSProject::scriptNames () const

Returns the names of all the scripts in this project.

### QPtrList<QSScript> QSProject::scripts () const

Returns a list of all the scripts in this project.

### bool QSProject::scriptsModified () const

Returns TRUE if the script has been modified and is out of sync with the state in the interpreter; otherwise returns FALSE.

### void QSProject::setStorageMode ( StorageMode mode )

Sets the mode for storing projects to mode.

See also StorageMode.

### StorageMode QSProject::storageMode () const

Returns the storage mode used for this project.

See also StorageMode.

This file is part of Qt Script for Applications, copyright © 2001-2004 Trolltech. All Rights Reserved.