Use QByteArray for variable names.

Less conversions, tighter code, less to type.

Change-Id: I38eb27ca17e6f1d98cdbc41fa003cbedf0f0bb34
Reviewed-by: hjk <qthjk@ovi.com>
This commit is contained in:
hjk
2011-12-21 11:29:35 +01:00
committed by hjk
parent 6571e01725
commit 443e77a47d
14 changed files with 86 additions and 78 deletions

View File

@@ -509,16 +509,16 @@ void EditorManager::init()
pluginManager()->addObject(d->m_openEditorsFactory); pluginManager()->addObject(d->m_openEditorsFactory);
VariableManager *vm = VariableManager::instance(); VariableManager *vm = VariableManager::instance();
vm->registerVariable(QLatin1String(kCurrentDocumentFilePath), vm->registerVariable(kCurrentDocumentFilePath,
tr("Full path of the current document including file name.")); tr("Full path of the current document including file name."));
vm->registerVariable(QLatin1String(kCurrentDocumentPath), vm->registerVariable(kCurrentDocumentPath,
tr("Full path of the current document excluding file name.")); tr("Full path of the current document excluding file name."));
vm->registerVariable(QLatin1String(kCurrentDocumentXPos), vm->registerVariable(kCurrentDocumentXPos,
tr("X-coordinate of the current editor's upper left corner, relative to screen.")); tr("X-coordinate of the current editor's upper left corner, relative to screen."));
vm->registerVariable(QLatin1String(kCurrentDocumentYPos), vm->registerVariable(kCurrentDocumentYPos,
tr("Y-coordinate of the current editor's upper left corner, relative to screen.")); tr("Y-coordinate of the current editor's upper left corner, relative to screen."));
connect(vm, SIGNAL(variableUpdateRequested(QString)), connect(vm, SIGNAL(variableUpdateRequested(QByteArray)),
this, SLOT(updateVariable(QString))); this, SLOT(updateVariable(QByteArray)));
} }
void EditorManager::updateAutoSave() void EditorManager::updateAutoSave()
@@ -2211,29 +2211,28 @@ QString EditorManager::windowTitleAddition() const
return d->m_titleAddition; return d->m_titleAddition;
} }
void EditorManager::updateVariable(const QString &variable) void EditorManager::updateVariable(const QByteArray &variable)
{ {
if (variable == QLatin1String(kCurrentDocumentFilePath) if (variable == kCurrentDocumentFilePath || variable == kCurrentDocumentPath) {
|| variable == QLatin1String(kCurrentDocumentPath)) {
QString value; QString value;
IEditor *curEditor = currentEditor(); IEditor *curEditor = currentEditor();
if (curEditor) { if (curEditor) {
QString fileName = curEditor->file()->fileName(); QString fileName = curEditor->file()->fileName();
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
if (variable == QLatin1String(kCurrentDocumentFilePath)) if (variable == kCurrentDocumentFilePath)
value = QFileInfo(fileName).filePath(); value = QFileInfo(fileName).filePath();
else if (variable == QLatin1String(kCurrentDocumentPath)) else if (variable == kCurrentDocumentPath)
value = QFileInfo(fileName).path(); value = QFileInfo(fileName).path();
} }
} }
VariableManager::instance()->insert(variable, value); VariableManager::instance()->insert(variable, value);
} else if (variable == QLatin1String(kCurrentDocumentXPos)) { } else if (variable == kCurrentDocumentXPos) {
QString value; QString value;
IEditor *curEditor = currentEditor(); IEditor *curEditor = currentEditor();
if (curEditor) if (curEditor)
value = QString::number(curEditor->widget()->mapToGlobal(QPoint(0,0)).x()); value = QString::number(curEditor->widget()->mapToGlobal(QPoint(0,0)).x());
VariableManager::instance()->insert(variable, value); VariableManager::instance()->insert(variable, value);
} else if (variable == QLatin1String(kCurrentDocumentYPos)) { } else if (variable == kCurrentDocumentYPos) {
QString value; QString value;
IEditor *curEditor = currentEditor(); IEditor *curEditor = currentEditor();
if (curEditor) if (curEditor)

View File

@@ -218,7 +218,7 @@ private slots:
void vcsOpenCurrentEditor(); void vcsOpenCurrentEditor();
void updateWindowTitle(); void updateWindowTitle();
void handleEditorStateChange(); void handleEditorStateChange();
void updateVariable(const QString &variable); void updateVariable(const QByteArray &variable);
void autoSave(); void autoSave();
void closeEditorFromContextMenu(); void closeEditorFromContextMenu();

View File

@@ -62,9 +62,8 @@ VariableChooser::VariableChooser(QWidget *parent) :
setFocusProxy(ui->variableList); setFocusProxy(ui->variableList);
VariableManager *vm = VariableManager::instance(); VariableManager *vm = VariableManager::instance();
foreach (const QString &variable, vm->variables()) { foreach (const QByteArray &variable, vm->variables())
ui->variableList->addItem(variable); ui->variableList->addItem(variable);
}
connect(ui->variableList, SIGNAL(currentTextChanged(QString)), connect(ui->variableList, SIGNAL(currentTextChanged(QString)),
this, SLOT(updateDescription(QString))); this, SLOT(updateDescription(QString)));
@@ -86,7 +85,7 @@ void VariableChooser::updateDescription(const QString &variable)
if (variable.isNull()) if (variable.isNull())
ui->variableDescription->setText(m_defaultDescription); ui->variableDescription->setText(m_defaultDescription);
else else
ui->variableDescription->setText(VariableManager::instance()->variableDescription(variable)); ui->variableDescription->setText(VariableManager::instance()->variableDescription(variable.toUtf8()));
} }
void VariableChooser::updateCurrentEditor(QWidget *old, QWidget *widget) void VariableChooser::updateCurrentEditor(QWidget *old, QWidget *widget)

View File

@@ -51,7 +51,7 @@ public:
virtual bool resolveMacro(const QString &name, QString *ret) virtual bool resolveMacro(const QString &name, QString *ret)
{ {
bool found; bool found;
*ret = Core::VariableManager::instance()->value(name, &found); *ret = Core::VariableManager::instance()->value(name.toUtf8(), &found);
return found; return found;
} }
}; };
@@ -59,11 +59,21 @@ public:
class VariableManagerPrivate class VariableManagerPrivate
{ {
public: public:
QHash<QString, QString> m_map; QHash<QByteArray, QString> m_map;
VMMapExpander m_macroExpander; VMMapExpander m_macroExpander;
QMap<QString, QString> m_descriptions; QMap<QByteArray, QString> m_descriptions;
}; };
/*!
\class Core::VariableManager
\brief The VaraiableManager class holds a generic map from variable names
to string values.
The names of the variables are stored as QByteArray. They are typically
7-bit-clean. In cases where this is not possible, Latin-1 encoding is
assumed.
*/
static VariableManager *variableManagerInstance = 0; static VariableManager *variableManagerInstance = 0;
VariableManager::VariableManager() : d(new VariableManagerPrivate) VariableManager::VariableManager() : d(new VariableManagerPrivate)
@@ -77,17 +87,17 @@ VariableManager::~VariableManager()
delete d; delete d;
} }
void VariableManager::insert(const QString &variable, const QString &value) void VariableManager::insert(const QByteArray &variable, const QString &value)
{ {
d->m_map.insert(variable, value); d->m_map.insert(variable, value);
} }
bool VariableManager::remove(const QString &variable) bool VariableManager::remove(const QByteArray &variable)
{ {
return d->m_map.remove(variable) > 0; return d->m_map.remove(variable) > 0;
} }
QString VariableManager::value(const QString &variable, bool *found) QString VariableManager::value(const QByteArray &variable, bool *found)
{ {
emit variableUpdateRequested(variable); emit variableUpdateRequested(variable);
if (found) { if (found) {
@@ -96,7 +106,7 @@ QString VariableManager::value(const QString &variable, bool *found)
return d->m_map.value(variable); return d->m_map.value(variable);
} }
QString VariableManager::value(const QString &variable, const QString &defaultValue) QString VariableManager::value(const QByteArray &variable, const QString &defaultValue)
{ {
emit variableUpdateRequested(variable); emit variableUpdateRequested(variable);
return d->m_map.value(variable, defaultValue); return d->m_map.value(variable, defaultValue);
@@ -112,17 +122,17 @@ VariableManager *VariableManager::instance()
return variableManagerInstance; return variableManagerInstance;
} }
void VariableManager::registerVariable(const QString &variable, const QString &description) void VariableManager::registerVariable(const QByteArray &variable, const QString &description)
{ {
d->m_descriptions.insert(variable, description); d->m_descriptions.insert(variable, description);
} }
QList<QString> VariableManager::variables() const QList<QByteArray> VariableManager::variables() const
{ {
return d->m_descriptions.keys(); return d->m_descriptions.keys();
} }
QString VariableManager::variableDescription(const QString &variable) const QString VariableManager::variableDescription(const QByteArray &variable) const
{ {
return d->m_descriptions.value(variable); return d->m_descriptions.value(variable);
} }

View File

@@ -56,19 +56,19 @@ public:
static VariableManager *instance(); static VariableManager *instance();
void insert(const QString &variable, const QString &value); void insert(const QByteArray &variable, const QString &value);
bool remove(const QString &variable); bool remove(const QByteArray &variable);
QString value(const QString &variable, bool *found = 0); QString value(const QByteArray &variable, bool *found = 0);
QString value(const QString &variable, const QString &defaultValue); QString value(const QByteArray &variable, const QString &defaultValue);
Utils::AbstractMacroExpander *macroExpander(); Utils::AbstractMacroExpander *macroExpander();
void registerVariable(const QString &variable, void registerVariable(const QByteArray &variable,
const QString &description); const QString &description);
QList<QString> variables() const; QList<QByteArray> variables() const;
QString variableDescription(const QString &variable) const; QString variableDescription(const QByteArray &variable) const;
signals: signals:
void variableUpdateRequested(const QString &variable); void variableUpdateRequested(const QByteArray &variable);
private: private:
VariableManagerPrivate *d; VariableManagerPrivate *d;

View File

@@ -92,9 +92,9 @@ void ExecuteFilter::accept(FilterEntry selection) const
VariableManager *vm = Core::VariableManager::instance(); VariableManager *vm = Core::VariableManager::instance();
bool found; bool found;
QString workingDirectory = vm->value(QLatin1String("CurrentDocument:Path"), &found); QString workingDirectory = vm->value("CurrentDocument:Path", &found);
if (!found || workingDirectory.isEmpty()) if (!found || workingDirectory.isEmpty())
workingDirectory = vm->value(QLatin1String("CurrentProject:Path"), &found); workingDirectory = vm->value("CurrentProject:Path", &found);
ExecuteData d; ExecuteData d;
d.workingDirectory = workingDirectory; d.workingDirectory = workingDirectory;

View File

@@ -61,7 +61,7 @@ class VarManMacroExpander : public Utils::AbstractQtcMacroExpander {
public: public:
virtual bool resolveMacro(const QString &name, QString *ret) virtual bool resolveMacro(const QString &name, QString *ret)
{ {
*ret = Core::VariableManager::instance()->value(name); *ret = Core::VariableManager::instance()->value(name.toUtf8());
return !ret->isEmpty(); return !ret->isEmpty();
} }
}; };

View File

@@ -74,7 +74,7 @@ bool BuildConfigMacroExpander::resolveMacro(const QString &name, QString *ret)
*ret = QDir::toNativeSeparators(m_bc->buildDirectory()); *ret = QDir::toNativeSeparators(m_bc->buildDirectory());
return true; return true;
} }
*ret = Core::VariableManager::instance()->value(name); *ret = Core::VariableManager::instance()->value(name.toUtf8());
return !ret->isEmpty(); return !ret->isEmpty();
} }
} // namespace Internal } // namespace Internal

View File

@@ -1005,12 +1005,12 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er
updateWelcomePage(); updateWelcomePage();
Core::VariableManager *vm = Core::VariableManager::instance(); Core::VariableManager *vm = Core::VariableManager::instance();
vm->registerVariable(QLatin1String(kCurrentProjectFilePath), vm->registerVariable(kCurrentProjectFilePath,
tr("Full path of the current project's main file, including file name.")); tr("Full path of the current project's main file, including file name."));
vm->registerVariable(QLatin1String(kCurrentProjectPath), vm->registerVariable(kCurrentProjectPath,
tr("Full path of the current project's main file, excluding file name.")); tr("Full path of the current project's main file, excluding file name."));
connect(vm, SIGNAL(variableUpdateRequested(QString)), connect(vm, SIGNAL(variableUpdateRequested(QByteArray)),
this, SLOT(updateVariable(QString))); this, SLOT(updateVariable(QByteArray)));
return true; return true;
} }
@@ -1129,16 +1129,16 @@ void ProjectExplorerPlugin::loadCustomWizards()
} }
} }
void ProjectExplorerPlugin::updateVariable(const QString &variable) void ProjectExplorerPlugin::updateVariable(const QByteArray &variable)
{ {
if (variable == QLatin1String(kCurrentProjectFilePath)) { if (variable == kCurrentProjectFilePath) {
if (currentProject() && currentProject()->file()) { if (currentProject() && currentProject()->file()) {
Core::VariableManager::instance()->insert(variable, Core::VariableManager::instance()->insert(variable,
currentProject()->file()->fileName()); currentProject()->file()->fileName());
} else { } else {
Core::VariableManager::instance()->remove(variable); Core::VariableManager::instance()->remove(variable);
} }
} else if (variable == QLatin1String(kCurrentProjectPath)) { } else if (variable == kCurrentProjectPath) {
if (currentProject() && currentProject()->file()) { if (currentProject() && currentProject()->file()) {
Core::VariableManager::instance()->insert(variable, Core::VariableManager::instance()->insert(variable,
QFileInfo(currentProject()->file()->fileName()).path()); QFileInfo(currentProject()->file()->fileName()).path());

View File

@@ -223,7 +223,7 @@ private slots:
void currentModeChanged(Core::IMode *mode, Core::IMode *oldMode); void currentModeChanged(Core::IMode *mode, Core::IMode *oldMode);
void updateActions(); void updateActions();
void loadCustomWizards(); void loadCustomWizards();
void updateVariable(const QString &variable); void updateVariable(const QByteArray &variable);
void updateRunWithoutDeployMenu(); void updateRunWithoutDeployMenu();
void publishProject(); void publishProject();

View File

@@ -137,10 +137,10 @@ void Qt4Manager::init()
this, SLOT(editorChanged(Core::IEditor*))); this, SLOT(editorChanged(Core::IEditor*)));
Core::VariableManager *vm = Core::VariableManager::instance(); Core::VariableManager *vm = Core::VariableManager::instance();
vm->registerVariable(QLatin1String(kInstallBins), vm->registerVariable(kInstallBins,
tr("Full path to the bin/ install directory of the current project's Qt version.")); tr("Full path to the bin/ install directory of the current project's Qt version."));
connect(vm, SIGNAL(variableUpdateRequested(QString)), connect(vm, SIGNAL(variableUpdateRequested(QByteArray)),
this, SLOT(updateVariable(QString))); this, SLOT(updateVariable(QByteArray)));
} }
void Qt4Manager::editorChanged(Core::IEditor *editor) void Qt4Manager::editorChanged(Core::IEditor *editor)
@@ -182,19 +182,19 @@ void Qt4Manager::editorAboutToClose(Core::IEditor *editor)
} }
} }
void Qt4Manager::updateVariable(const QString &variable) void Qt4Manager::updateVariable(const QByteArray &variable)
{ {
if (variable == QLatin1String(kInstallBins)) { if (variable == kInstallBins) {
Qt4Project *qt4pro = qobject_cast<Qt4Project *>(projectExplorer()->currentProject()); Qt4Project *qt4pro = qobject_cast<Qt4Project *>(projectExplorer()->currentProject());
if (!qt4pro) { if (!qt4pro) {
Core::VariableManager::instance()->remove(QLatin1String(kInstallBins)); Core::VariableManager::instance()->remove(kInstallBins);
return; return;
} }
QString value; QString value;
QtSupport::BaseQtVersion *qtv = qt4pro->activeTarget()->activeQt4BuildConfiguration()->qtVersion(); QtSupport::BaseQtVersion *qtv = qt4pro->activeTarget()->activeQt4BuildConfiguration()->qtVersion();
if (qtv) if (qtv)
value = qtv->versionInfo().value(QLatin1String("QT_INSTALL_BINS")); value = qtv->versionInfo().value(QLatin1String("QT_INSTALL_BINS"));
Core::VariableManager::instance()->insert(QLatin1String(kInstallBins), value); Core::VariableManager::instance()->insert(kInstallBins, value);
} }
} }

View File

@@ -109,7 +109,7 @@ private slots:
void editorAboutToClose(Core::IEditor *editor); void editorAboutToClose(Core::IEditor *editor);
void uiEditorContentsChanged(); void uiEditorContentsChanged();
void editorChanged(Core::IEditor*); void editorChanged(Core::IEditor*);
void updateVariable(const QString &variable); void updateVariable(const QByteArray &variable);
private: private:
QList<Qt4Project *> m_projects; QList<Qt4Project *> m_projects;

View File

@@ -185,20 +185,20 @@ void TextEditorPlugin::extensionsInitialized()
addAutoReleasedObject(new FindInCurrentFile); addAutoReleasedObject(new FindInCurrentFile);
Core::VariableManager *vm = Core::VariableManager::instance(); Core::VariableManager *vm = Core::VariableManager::instance();
vm->registerVariable(QLatin1String(kCurrentDocumentSelection), vm->registerVariable(kCurrentDocumentSelection,
tr("Selected text within the current document.")); tr("Selected text within the current document."));
vm->registerVariable(QLatin1String(kCurrentDocumentRow), vm->registerVariable(kCurrentDocumentRow,
tr("Line number of the text cursor position in current document (starts with 1).")); tr("Line number of the text cursor position in current document (starts with 1)."));
vm->registerVariable(QLatin1String(kCurrentDocumentColumn), vm->registerVariable(kCurrentDocumentColumn,
tr("Column number of the text cursor position in current document (starts with 0).")); tr("Column number of the text cursor position in current document (starts with 0)."));
vm->registerVariable(QLatin1String(kCurrentDocumentRowCount), vm->registerVariable(kCurrentDocumentRowCount,
tr("Number of lines visible in current document.")); tr("Number of lines visible in current document."));
vm->registerVariable(QLatin1String(kCurrentDocumentColumnCount), vm->registerVariable(kCurrentDocumentColumnCount,
tr("Number of columns visible in current document.")); tr("Number of columns visible in current document."));
vm->registerVariable(QLatin1String(kCurrentDocumentFontSize), vm->registerVariable(kCurrentDocumentFontSize,
tr("Current document's font size in points.")); tr("Current document's font size in points."));
connect(vm, SIGNAL(variableUpdateRequested(QString)), connect(vm, SIGNAL(variableUpdateRequested(QByteArray)),
this, SLOT(updateVariable(QString))); this, SLOT(updateVariable(QByteArray)));
connect(Core::ExternalToolManager::instance(), SIGNAL(replaceSelectionRequested(QString)), connect(Core::ExternalToolManager::instance(), SIGNAL(replaceSelectionRequested(QString)),
this, SLOT(updateCurrentSelection(QString))); this, SLOT(updateCurrentSelection(QString)));
} }
@@ -232,32 +232,32 @@ void TextEditorPlugin::updateSearchResultsFont(const FontSettings &settings)
settings.fontSize() * settings.fontZoom() / 100)); settings.fontSize() * settings.fontZoom() / 100));
} }
void TextEditorPlugin::updateVariable(const QString &variable) void TextEditorPlugin::updateVariable(const QByteArray &variable)
{ {
static QSet<QString> variables = QSet<QString>() static QSet<QByteArray> variables = QSet<QByteArray>()
<< QString::fromLatin1(kCurrentDocumentSelection) << kCurrentDocumentSelection
<< QString::fromLatin1(kCurrentDocumentRow) << kCurrentDocumentRow
<< QString::fromLatin1(kCurrentDocumentColumn) << kCurrentDocumentColumn
<< QString::fromLatin1(kCurrentDocumentRowCount) << kCurrentDocumentRowCount
<< QString::fromLatin1(kCurrentDocumentColumnCount) << kCurrentDocumentColumnCount
<< QString::fromLatin1(kCurrentDocumentFontSize); << kCurrentDocumentFontSize;
if (variables.contains(variable)) { if (variables.contains(variable)) {
QString value; QString value;
Core::IEditor *iface = Core::EditorManager::instance()->currentEditor(); Core::IEditor *iface = Core::EditorManager::instance()->currentEditor();
ITextEditor *editor = qobject_cast<ITextEditor *>(iface); ITextEditor *editor = qobject_cast<ITextEditor *>(iface);
if (editor) { if (editor) {
if (variable == QLatin1String(kCurrentDocumentSelection)) { if (variable == kCurrentDocumentSelection) {
value = editor->selectedText(); value = editor->selectedText();
value.replace(QChar::ParagraphSeparator, QLatin1String("\n")); value.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
} else if (variable == QLatin1String(kCurrentDocumentRow)) { } else if (variable == kCurrentDocumentRow) {
value = QString::number(editor->currentLine()); value = QString::number(editor->currentLine());
} else if (variable == QLatin1String(kCurrentDocumentColumn)) { } else if (variable == kCurrentDocumentColumn) {
value = QString::number(editor->currentColumn()); value = QString::number(editor->currentColumn());
} else if (variable == QLatin1String(kCurrentDocumentRowCount)) { } else if (variable == kCurrentDocumentRowCount) {
value = QString::number(editor->rowCount()); value = QString::number(editor->rowCount());
} else if (variable == QLatin1String(kCurrentDocumentColumnCount)) { } else if (variable == kCurrentDocumentColumnCount) {
value = QString::number(editor->columnCount()); value = QString::number(editor->columnCount());
} else if (variable == QLatin1String(kCurrentDocumentFontSize)) { } else if (variable == kCurrentDocumentFontSize) {
value = QString::number(editor->widget()->font().pointSize()); value = QString::number(editor->widget()->font().pointSize());
} }
} }

View File

@@ -75,7 +75,7 @@ private slots:
void invokeCompletion(); void invokeCompletion();
void invokeQuickFix(); void invokeQuickFix();
void updateSearchResultsFont(const TextEditor::FontSettings &); void updateSearchResultsFont(const TextEditor::FontSettings &);
void updateVariable(const QString &variable); void updateVariable(const QByteArray &variable);
void updateCurrentSelection(const QString &text); void updateCurrentSelection(const QString &text);
private: private: