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);
VariableManager *vm = VariableManager::instance();
vm->registerVariable(QLatin1String(kCurrentDocumentFilePath),
vm->registerVariable(kCurrentDocumentFilePath,
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."));
vm->registerVariable(QLatin1String(kCurrentDocumentXPos),
vm->registerVariable(kCurrentDocumentXPos,
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."));
connect(vm, SIGNAL(variableUpdateRequested(QString)),
this, SLOT(updateVariable(QString)));
connect(vm, SIGNAL(variableUpdateRequested(QByteArray)),
this, SLOT(updateVariable(QByteArray)));
}
void EditorManager::updateAutoSave()
@@ -2211,29 +2211,28 @@ QString EditorManager::windowTitleAddition() const
return d->m_titleAddition;
}
void EditorManager::updateVariable(const QString &variable)
void EditorManager::updateVariable(const QByteArray &variable)
{
if (variable == QLatin1String(kCurrentDocumentFilePath)
|| variable == QLatin1String(kCurrentDocumentPath)) {
if (variable == kCurrentDocumentFilePath || variable == kCurrentDocumentPath) {
QString value;
IEditor *curEditor = currentEditor();
if (curEditor) {
QString fileName = curEditor->file()->fileName();
if (!fileName.isEmpty()) {
if (variable == QLatin1String(kCurrentDocumentFilePath))
if (variable == kCurrentDocumentFilePath)
value = QFileInfo(fileName).filePath();
else if (variable == QLatin1String(kCurrentDocumentPath))
else if (variable == kCurrentDocumentPath)
value = QFileInfo(fileName).path();
}
}
VariableManager::instance()->insert(variable, value);
} else if (variable == QLatin1String(kCurrentDocumentXPos)) {
} else if (variable == kCurrentDocumentXPos) {
QString value;
IEditor *curEditor = currentEditor();
if (curEditor)
value = QString::number(curEditor->widget()->mapToGlobal(QPoint(0,0)).x());
VariableManager::instance()->insert(variable, value);
} else if (variable == QLatin1String(kCurrentDocumentYPos)) {
} else if (variable == kCurrentDocumentYPos) {
QString value;
IEditor *curEditor = currentEditor();
if (curEditor)

View File

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

View File

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

View File

@@ -51,7 +51,7 @@ public:
virtual bool resolveMacro(const QString &name, QString *ret)
{
bool found;
*ret = Core::VariableManager::instance()->value(name, &found);
*ret = Core::VariableManager::instance()->value(name.toUtf8(), &found);
return found;
}
};
@@ -59,11 +59,21 @@ public:
class VariableManagerPrivate
{
public:
QHash<QString, QString> m_map;
QHash<QByteArray, QString> m_map;
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;
VariableManager::VariableManager() : d(new VariableManagerPrivate)
@@ -77,17 +87,17 @@ VariableManager::~VariableManager()
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);
}
bool VariableManager::remove(const QString &variable)
bool VariableManager::remove(const QByteArray &variable)
{
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);
if (found) {
@@ -96,7 +106,7 @@ QString VariableManager::value(const QString &variable, bool *found)
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);
return d->m_map.value(variable, defaultValue);
@@ -112,17 +122,17 @@ VariableManager *VariableManager::instance()
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);
}
QList<QString> VariableManager::variables() const
QList<QByteArray> VariableManager::variables() const
{
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);
}

View File

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