From d3341b996cac995f7f14b117f4c6f85f45422ef5 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Mon, 11 Mar 2013 11:05:06 +0100 Subject: [PATCH] Centralize handling of file-kind variables. Change-Id: I400e28ae7d1d4f0250519dcd3c85746da1ea1e93 Reviewed-by: Tobias Hunger --- .../editormanager/editormanager.cpp | 19 ++++-------- src/plugins/coreplugin/variablemanager.cpp | 31 +++++++++++++++++++ src/plugins/coreplugin/variablemanager.h | 10 ++++++ .../projectexplorer/projectexplorer.cpp | 5 +-- .../projectexplorerconstants.h | 3 +- .../projectexplorer/projectmacroexpander.cpp | 12 +++---- 6 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 779c60772f5..b066e54cd29 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -86,8 +86,7 @@ enum { debugEditorManager=0 }; -static const char kCurrentDocumentFilePath[] = "CurrentDocument:FilePath"; -static const char kCurrentDocumentPath[] = "CurrentDocument:Path"; +static const char kCurrentDocumentPrefix[] = "CurrentDocument"; static const char kCurrentDocumentXPos[] = "CurrentDocument:XPos"; static const char kCurrentDocumentYPos[] = "CurrentDocument:YPos"; static const char kMakeWritableWarning[] = "Core.EditorManager.MakeWritable"; @@ -467,10 +466,7 @@ void EditorManager::init() ExtensionSystem::PluginManager::addObject(d->m_openEditorsFactory); VariableManager *vm = VariableManager::instance(); - vm->registerVariable(kCurrentDocumentFilePath, - tr("Full path of the current document including file name.")); - vm->registerVariable(kCurrentDocumentPath, - tr("Full path of the current document excluding file name.")); + vm->registerFileVariables(kCurrentDocumentPrefix, tr("Current document")); vm->registerVariable(kCurrentDocumentXPos, tr("X-coordinate of the current editor's upper left corner, relative to screen.")); vm->registerVariable(kCurrentDocumentYPos, @@ -2291,17 +2287,14 @@ QString EditorManager::windowTitleAddition() const void EditorManager::updateVariable(const QByteArray &variable) { - if (variable == kCurrentDocumentFilePath || variable == kCurrentDocumentPath) { + if (VariableManager::instance()->isFileVariable(variable, kCurrentDocumentPrefix)) { QString value; IEditor *curEditor = currentEditor(); if (curEditor) { QString fileName = curEditor->document()->fileName(); - if (!fileName.isEmpty()) { - if (variable == kCurrentDocumentFilePath) - value = QFileInfo(fileName).filePath(); - else if (variable == kCurrentDocumentPath) - value = QFileInfo(fileName).path(); - } + if (!fileName.isEmpty()) + value = VariableManager::instance()->fileVariableValue(variable, kCurrentDocumentPrefix, + fileName); } VariableManager::instance()->insert(variable, value); } else if (variable == kCurrentDocumentXPos) { diff --git a/src/plugins/coreplugin/variablemanager.cpp b/src/plugins/coreplugin/variablemanager.cpp index 59c379ed336..a14bd8c53ce 100644 --- a/src/plugins/coreplugin/variablemanager.cpp +++ b/src/plugins/coreplugin/variablemanager.cpp @@ -40,6 +40,9 @@ #include #include +static const char kFilePathPostfix[] = ":FilePath"; +static const char kPathPostfix[] = ":Path"; + namespace Core { class VMMapExpander : public Utils::AbstractQtcMacroExpander @@ -117,6 +120,34 @@ void VariableManager::registerVariable(const QByteArray &variable, const QString d->m_descriptions.insert(variable, description); } +void VariableManager::registerFileVariables(const QByteArray &prefix, const QString &heading) +{ + registerVariable(prefix + kFilePathPostfix, tr("%1: Full path including file name.").arg(heading)); + registerVariable(prefix + kPathPostfix, tr("%1: Full path excluding file name.").arg(heading)); +} + +bool VariableManager::isFileVariable(const QByteArray &variable, const QByteArray &prefix) +{ + return variable == prefix + kFilePathPostfix + || variable == prefix + kPathPostfix; +} + +QString VariableManager::fileVariableValue(const QByteArray &variable, const QByteArray &prefix, + const QString &fileName) +{ + return fileVariableValue(variable, prefix, QFileInfo(fileName)); +} + +QString VariableManager::fileVariableValue(const QByteArray &variable, const QByteArray &prefix, + const QFileInfo &fileInfo) +{ + if (variable == prefix + kFilePathPostfix) + return fileInfo.filePath(); + else if (variable == prefix + kPathPostfix) + return fileInfo.path(); + return QString(); +} + QList VariableManager::variables() const { return d->m_descriptions.keys(); diff --git a/src/plugins/coreplugin/variablemanager.h b/src/plugins/coreplugin/variablemanager.h index cddf0b688fc..d156f10da84 100644 --- a/src/plugins/coreplugin/variablemanager.h +++ b/src/plugins/coreplugin/variablemanager.h @@ -32,6 +32,7 @@ #include "core_global.h" +#include #include #include @@ -60,6 +61,15 @@ public: void registerVariable(const QByteArray &variable, const QString &description); + + void registerFileVariables(const QByteArray &prefix, + const QString &heading); + bool isFileVariable(const QByteArray &variable, const QByteArray &prefix); + QString fileVariableValue(const QByteArray &variable, const QByteArray &prefix, + const QString &fileName); + QString fileVariableValue(const QByteArray &variable, const QByteArray &prefix, + const QFileInfo &fileInfo); + QList variables() const; QString variableDescription(const QByteArray &variable) const; diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 6aa5d51b40a..553f4da8d0e 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -1005,10 +1005,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er updateWelcomePage(); Core::VariableManager *vm = Core::VariableManager::instance(); - vm->registerVariable(Constants::VAR_CURRENTPROJECT_FILEPATH, - tr("Full path of the current project's main file, including file name.")); - vm->registerVariable(Constants::VAR_CURRENTPROJECT_PATH, - tr("Full path of the current project's main file, excluding file name.")); + vm->registerFileVariables(Constants::VAR_CURRENTPROJECT_PREFIX, tr("Current project's main file")); vm->registerVariable(Constants::VAR_CURRENTPROJECT_BUILDPATH, tr("Full build path of the current project's active build configuration.")); vm->registerVariable(Constants::VAR_CURRENTPROJECT_NAME, tr("The current project's name.")); diff --git a/src/plugins/projectexplorer/projectexplorerconstants.h b/src/plugins/projectexplorer/projectexplorerconstants.h index 76bea57d358..9ab4d47e8e6 100644 --- a/src/plugins/projectexplorer/projectexplorerconstants.h +++ b/src/plugins/projectexplorer/projectexplorerconstants.h @@ -237,8 +237,7 @@ const char DESKTOP_DEVICE_ID[] = "Desktop Device"; const char DESKTOP_DEVICE_TYPE[] = "Desktop"; // Variable Names: -const char VAR_CURRENTPROJECT_PATH[] = "CurrentProject:Path"; -const char VAR_CURRENTPROJECT_FILEPATH[] = "CurrentProject:FilePath"; +const char VAR_CURRENTPROJECT_PREFIX[] = "CurrentProject"; const char VAR_CURRENTPROJECT_BUILDPATH[] = "CurrentProject:BuildPath"; const char VAR_CURRENTPROJECT_NAME[] = "CurrentProject:Name"; const char VAR_CURRENTKIT_NAME[] = "CurrentKit:Name"; diff --git a/src/plugins/projectexplorer/projectmacroexpander.cpp b/src/plugins/projectexplorer/projectmacroexpander.cpp index 978b7678111..439792f1030 100644 --- a/src/plugins/projectexplorer/projectmacroexpander.cpp +++ b/src/plugins/projectexplorer/projectmacroexpander.cpp @@ -49,14 +49,12 @@ bool ProjectExpander::resolveProjectMacro(const QString &name, QString *ret) result = m_projectName; found = true; } - } else if (name == QLatin1String(ProjectExplorer::Constants::VAR_CURRENTPROJECT_PATH)) { + } else if (Core::VariableManager::instance()->isFileVariable( + name.toUtf8(), ProjectExplorer::Constants::VAR_CURRENTPROJECT_PREFIX)) { if (!m_projectFile.filePath().isEmpty()) { - result = m_projectFile.absolutePath(); - found = true; - } - } else if (name == QLatin1String(ProjectExplorer::Constants::VAR_CURRENTPROJECT_FILEPATH)) { - if (!m_projectFile.filePath().isEmpty()) { - result = m_projectFile.absoluteFilePath(); + result = Core::VariableManager::instance()->fileVariableValue(name.toUtf8(), + ProjectExplorer::Constants::VAR_CURRENTPROJECT_PREFIX, + m_projectFile); found = true; } } else if (m_kit && name == QLatin1String(ProjectExplorer::Constants::VAR_CURRENTKIT_NAME)) {