Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What JS objects are available in Qt Creator JS: variable expansion?

Tags:

qt

qt-creator

Qt Creator's wizards and configuration settings supports variable expansion, including evaluation of JavaScript expressions, e.g. the C++ class wizard file template contains:

%{JS: Cpp.openNamespaces('%{Class}')}

Cpp seems to be a global object. Where in the Qt Creator sources are those defined, and what's available?

These are not documented anywhere.

like image 455
Kuba hasn't forgotten Monica Avatar asked May 03 '17 14:05

Kuba hasn't forgotten Monica


Video Answer


2 Answers

The JavaScript variable expansion is performed by the JsExpander in the core plugin. The expander can register QObject instances and expose them as properties of the global JS object.

To find all of those global objects, search for registerQObjectForJs method invocations. See the github search results for this method.

As of Qt Creator 4.2.1, and until at least 4.6, the following are the only objects registered:

  • Util - exposing Internal::UtilsJsExtension,
  • Cpp - exposing CppTools::Internal::CppToolsJsExtension,
  • Modeling - exposing ModelEditor::Internal::JsExtension,
  • QtSupport - exposing QtSupport::CodeGenerator,
  • Vcs - exposing VcsBase::Internal::VcsJsExtension.

The method parameter types are mapped to JavaScript types by QJSEngine. E.g. to obtain the Qt includes, one could have the following substitution:

%{JS: QtSupport.qtIncludes([ '%{Base}' ], [ '%{Base}' ])}

given the signature

QString qtIncludes(const QStringList &qt4, const QStringList &qt5)

The method list follows.

Util

QString toNativeSeparators(const QString &in) const;
QString fromNativeSeparators(const QString &in) const;

QString baseName(const QString &in) const;
QString fileName(const QString &in) const;
QString completeBaseName(const QString &in) const;
QString suffix(const QString &in) const;
QString completeSuffix(const QString &in) const;
QString path(const QString &in) const;
QString absoluteFilePath(const QString &in) const;

QString relativeFilePath(const QString &path, const QString &base) const;

// File checks:
bool exists(const QString &in) const;
bool isDirectory(const QString &in) const;
bool isFile(const QString &in) const;

// MimeDB:
QString preferredSuffix(const QString &mimetype) const;

// Generate filename:
QString fileName(const QString &path,
                             const QString &extension) const;

// Generate temporary file:
QString mktemp(const QString &pattern) const;

// Generate a ascii-only string:
QString asciify(const QString &input) const;

Cpp

// Generate header guard:
QString headerGuard(const QString &in) const;

// Fix the filename casing as configured in C++/File Naming:
QString fileName(const QString &path, const QString &extension) const;

// Work with classes:
QStringList namespaces(const QString &klass) const;
QString className(const QString &klass) const;
QString classToFileName(const QString &klass,
                                    const QString &extension) const;
QString classToHeaderGuard(const QString &klass, const QString &extension) const;
QString openNamespaces(const QString &klass) const;
QString closeNamespaces(const QString &klass) const;

Modeling

QString fileNameToElementName(const QString &file);
QString elementNameToFileName(const QString &element);

QtSupport

// Ui file related:
// Change the class name in a UI XML form
QString changeUiClassName(const QString &uiXml, const QString &newUiClassName);

QString uiClassName(const QString &uiXml);

// Generic Qt:
QString qtIncludes(const QStringList &qt4, const QStringList &qt5);

Vcs

bool isConfigured(const QString &vcsId) const;
QString displayName(const QString &vcsId) const;
like image 137
Kuba hasn't forgotten Monica Avatar answered Oct 19 '22 00:10

Kuba hasn't forgotten Monica


It is also useful to note that several JS variables exposed via the macroexpander see: https://github.com/qt-creator/qt-creator/blob/master/src/plugins/coreplugin/coreplugin.cpp#L169

like image 1
Miklos Marton Avatar answered Oct 19 '22 00:10

Miklos Marton