Files
qt-creator/src/plugins/coreplugin/variablemanager.cpp
T

307 lines
12 KiB
C++
Raw Normal View History

2012-10-02 09:12:39 +02:00
/****************************************************************************
2008-12-02 12:01:29 +01:00
**
2014-01-07 13:27:11 +01:00
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
2012-10-02 09:12:39 +02:00
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
2012-10-02 09:12:39 +02:00
** This file is part of Qt Creator.
2008-12-02 12:01:29 +01:00
**
2012-10-02 09:12:39 +02:00
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
2012-10-02 09:12:39 +02:00
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
2012-10-02 09:12:39 +02:00
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2012-10-02 09:12:39 +02:00
****************************************************************************/
2008-12-02 15:08:31 +01:00
2008-12-02 12:01:29 +01:00
#include "variablemanager.h"
2010-09-16 14:59:05 +02:00
2010-11-08 19:54:19 +01:00
#include <utils/stringutils.h>
2009-10-01 16:38:08 +02:00
#include <QCoreApplication>
#include <QFileInfo>
#include <QMap>
#include <QDebug>
2008-12-02 12:01:29 +01:00
2013-03-11 11:05:06 +01:00
static const char kFilePathPostfix[] = ":FilePath";
static const char kPathPostfix[] = ":Path";
static const char kFileNamePostfix[] = ":FileName";
static const char kFileBaseNamePostfix[] = ":FileBaseName";
2013-03-11 11:05:06 +01:00
2010-09-16 14:59:05 +02:00
namespace Core {
2008-12-02 12:01:29 +01:00
2011-07-06 19:02:18 +02:00
class VMMapExpander : public Utils::AbstractQtcMacroExpander
{
2010-11-08 19:54:19 +01:00
public:
virtual bool resolveMacro(const QString &name, QString *ret)
{
bool found;
2013-03-11 11:52:05 +01:00
*ret = Core::VariableManager::value(name.toUtf8(), &found);
return found;
2010-11-08 19:54:19 +01:00
}
};
2011-07-06 19:02:18 +02:00
class VariableManagerPrivate
2008-12-02 12:01:29 +01:00
{
2010-09-16 14:59:05 +02:00
public:
QHash<QByteArray, VariableManager::StringFunction> m_map;
2010-11-08 19:54:19 +01:00
VMMapExpander m_macroExpander;
2011-12-21 11:29:35 +01:00
QMap<QByteArray, QString> m_descriptions;
2010-09-16 14:59:05 +02:00
};
2011-12-21 11:29:35 +01:00
/*!
\class Core::VariableManager
2013-03-13 17:06:50 +01:00
\brief The VariableManager class manages \QC wide variables, that a user
can enter into many string settings. The variables are replaced by an actual value when the string
is used, similar to how environment variables are expanded by a shell.
2011-12-21 11:29:35 +01:00
2013-03-13 17:06:50 +01:00
\section1 Variables
Variable names can be basically any string without dollar sign and braces,
though it is recommended to only use 7-bit ASCII without special characters and whitespace.
If there are several variables that contain different aspects of the same object,
it is convention to give them the same prefix, followed by a colon and a postfix
that describes the aspect.
Examples of this are \c{CurrentDocument:FilePath} and \c{CurrentDocument:Selection}.
When the variable manager is requested to replace variables in a string, it looks for
2013-09-30 17:40:10 +03:00
variable names enclosed in %{ and }, like %{CurrentDocument:FilePath}.
2013-03-13 17:06:50 +01:00
\note The names of the variables are stored as QByteArray. They are typically
7-bit-clean. In cases where this is not possible, UTF-8 encoding is
2011-12-21 11:29:35 +01:00
assumed.
2013-03-13 17:06:50 +01:00
\section1 Providing Variable Values
Plugins can register variables together with a description through registerVariable(),
and then need to connect to the variableUpdateRequested() signal to actually give
the variable its value when requested. A typical setup is to register
variables in the Plugin::initialize() function.
2013-03-13 17:06:50 +01:00
\code
bool MyPlugin::initialize(const QStringList &arguments, QString *errorString)
{
2013-09-25 18:19:45 +02:00
[...]
VariableManager::registerVariable(
"MyVariable",
tr("The current value of whatever I want."));
[]() -> QString {
2013-09-25 18:19:45 +02:00
QString value;
// do whatever is necessary to retrieve the value
[...]
return value;
2013-09-25 18:19:45 +02:00
}
);
[...]
}
\endcode
2013-03-13 17:06:50 +01:00
For variables that refer to a file, you should use the convenience function
VariableManager::registerFileVariables().
The functions take a variable prefix, like \c MyFileVariable,
2013-03-13 17:06:50 +01:00
and automatically handle standardized postfixes like \c{:FilePath},
\c{:Path} and \c{:FileBaseName}, resulting in the combined variables, such as
\c{MyFileVariable:FilePath}.
\section1 Providing and Expanding Parametrized Strings
Though it is possible to just ask the variable manager for the value of some variable in your
code, the preferred use case is to give the user the possibility to parametrize strings, for
example for settings.
(If you ever think about doing the former, think twice. It is much more efficient
to just ask the plugin that provides the variable value directly, without going through
string conversions, and through the variable manager which will do a large scale poll. To be
more concrete, using the example from the Providing Variable Values section: instead of
calling \c{VariableManager::value("MyVariable")}, it is much more efficient to just ask directly
with \c{MyPlugin::variableValue()}.)
\section2 User Interface
If the string that you want to parametrize is settable by the user, through a QLineEdit or
QTextEdit derived class, you should add a variable chooser to your UI, which allows adding
variables to the string by browsing through a list. See Core::VariableChooser for more
details.
\section2 Expanding Strings
Expanding variable values in strings is done by "macro expanders".
Utils::AbstractMacroExpander is the base class for these, and the variable manager
provides an implementation that expands \QC variables through
VariableManager::macroExpander().
There are several different ways to expand a string, covering the different use cases,
listed here sorted by relevance:
\list
2013-09-25 18:19:45 +02:00
\li Using VariableManager::expandedString(). This is the most comfortable way to get a string
with variable values expanded, but also the least flexible one. If this is sufficient for
you, use it.
2013-10-07 13:34:40 +02:00
\li Using the Utils::expandMacros() functions. These take a string and a macro expander (for which
2013-09-25 18:19:45 +02:00
you would use the one provided by the variable manager). Mostly the same as
VariableManager::expandedString(), but also has a variant that does the replacement inline
instead of returning a new string.
\li Using Utils::QtcProcess::expandMacros(). This expands the string while conforming to the
2013-10-07 13:34:40 +02:00
quoting rules of the platform it is run on. Use this function with the variable manager's
2013-09-25 18:19:45 +02:00
macro expander if your string will be passed as a command line parameter string to an
external command.
\li Writing your own macro expander that nests the variable manager's macro expander. And then
doing one of the above. This allows you to expand additional "local" variables/macros,
that do not come from the variable manager.
2013-03-13 17:06:50 +01:00
\endlist
2011-12-21 11:29:35 +01:00
*/
2013-03-13 17:06:50 +01:00
/*!
* \fn void VariableManager::variableUpdateRequested(const QByteArray &variable)
* Signals that the value of the \a variable should be updated because someone requests its value.
* Handlers of this signal should call insert() and return as fast as possible.
*/
2011-07-06 19:02:18 +02:00
static VariableManager *variableManagerInstance = 0;
2013-03-11 11:52:05 +01:00
static VariableManagerPrivate *d;
2010-09-16 14:59:05 +02:00
2013-03-13 17:06:50 +01:00
/*!
* \internal
*/
2013-03-11 11:52:05 +01:00
VariableManager::VariableManager()
{
2013-03-11 11:52:05 +01:00
d = new VariableManagerPrivate;
2011-07-06 19:02:18 +02:00
variableManagerInstance = this;
2010-09-16 14:59:05 +02:00
}
2013-03-13 17:06:50 +01:00
/*!
* \internal
*/
2010-09-16 14:59:05 +02:00
VariableManager::~VariableManager()
{
2011-07-06 19:02:18 +02:00
variableManagerInstance = 0;
delete d;
2010-09-16 14:59:05 +02:00
}
2013-03-13 17:06:50 +01:00
/*!
* Returns the value of the given \a variable. If \a found is given, it is
* set to true if the variable has a value at all, false if not.
2013-03-13 17:06:50 +01:00
*/
2011-12-21 11:29:35 +01:00
QString VariableManager::value(const QByteArray &variable, bool *found)
{
if (found)
*found = d->m_map.contains(variable);
StringFunction f = d->m_map.value(variable);
return f ? f() : QString();
}
2013-03-13 17:06:50 +01:00
/*!
* Returns \a stringWithVariables with all variables replaced by their values.
* See the VariableManager overview documentation for other ways to expand variables.
*
* \sa VariableManager
* \sa macroExpander()
*/
QString VariableManager::expandedString(const QString &stringWithVariables)
{
return Utils::expandMacros(stringWithVariables, macroExpander());
}
2013-03-13 17:06:50 +01:00
/*!
* Returns a macro expander that is used to expand all variables from the variable manager
* in a string.
* See the VariableManager overview documentation for other ways to expand variables.
*
* \sa VariableManager
* \sa expandedString()
*/
2010-11-08 19:54:19 +01:00
Utils::AbstractMacroExpander *VariableManager::macroExpander()
{
return &d->m_macroExpander;
}
2013-03-13 17:06:50 +01:00
/*!
* Makes the given string-valued \a variable known to the variable manager,
* together with a localized \a description.
*
* \sa registerFileVariables(), registerIntVariable()
2013-03-13 17:06:50 +01:00
*/
void VariableManager::registerVariable(const QByteArray &variable,
const QString &description, const StringFunction &value)
2010-09-16 14:59:05 +02:00
{
d->m_descriptions.insert(variable, description);
d->m_map.insert(variable, value);
2010-09-16 14:59:05 +02:00
}
2013-03-13 17:06:50 +01:00
/*!
* Makes the given integral-valued \a variable known to the variable manager,
* together with a localized \a description.
2013-03-13 17:06:50 +01:00
*
* \sa registerVariable(), registerFileVariables()
2013-03-13 17:06:50 +01:00
*/
void VariableManager::registerIntVariable(const QByteArray &variable,
const QString &description, const VariableManager::IntFunction &value)
2011-02-10 20:58:17 +01:00
{
registerVariable(variable, description,
[=]() -> QString { return QString::number(value ? value() : 0); });
2011-02-10 20:58:17 +01:00
}
2013-03-13 17:06:50 +01:00
/*!
2013-10-07 13:34:40 +02:00
* Convenience function to register several variables with the same \a prefix, that have a file
2013-03-13 17:06:50 +01:00
* as a value. Takes the prefix and registers variables like \c{prefix:FilePath} and
* \c{prefix:Path}, with descriptions that start with the given \a heading.
* For example \c{registerFileVariables("CurrentDocument", tr("Current Document"))} registers
* variables such as \c{CurrentDocument:FilePath} with description
* "Current Document: Full path including file name."
*/
void VariableManager::registerFileVariables(const QByteArray &prefix,
const QString &heading, const StringFunction &base)
2013-03-11 11:05:06 +01:00
{
registerVariable(prefix + kFilePathPostfix,
QCoreApplication::translate("Core::VariableManager", "%1: Full path including file name.").arg(heading),
[=]() -> QString { return QFileInfo(base()).filePath(); });
2013-03-11 11:05:06 +01:00
registerVariable(prefix + kPathPostfix,
QCoreApplication::translate("Core::VariableManager", "%1: Full path excluding file name.").arg(heading),
[=]() -> QString { return QFileInfo(base()).path(); });
2013-03-11 11:05:06 +01:00
registerVariable(prefix + kFileNamePostfix,
QCoreApplication::translate("Core::VariableManager", "%1: File name without path.").arg(heading),
[=]() -> QString { return QFileInfo(base()).fileName(); });
2013-03-11 11:05:06 +01:00
registerVariable(prefix + kFileBaseNamePostfix,
QCoreApplication::translate("Core::VariableManager", "%1: File base name without path and suffix.").arg(heading),
[=]() -> QString { return QFileInfo(base()).baseName(); });
2013-03-11 11:05:06 +01:00
}
2013-03-13 17:06:50 +01:00
/*!
* Returns all registered variable names.
*
* \sa registerVariable()
* \sa registerFileVariables()
*/
2013-03-11 11:52:05 +01:00
QList<QByteArray> VariableManager::variables()
2011-02-10 20:58:17 +01:00
{
return d->m_descriptions.keys();
}
2013-03-13 17:06:50 +01:00
/*!
* Returns the description that was registered for the \a variable.
*/
2013-03-11 11:52:05 +01:00
QString VariableManager::variableDescription(const QByteArray &variable)
2011-02-10 20:58:17 +01:00
{
return d->m_descriptions.value(variable);
}
2010-09-16 14:59:05 +02:00
} // namespace Core