summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMahmoud Badri <mahmoud.badri@qt.io>2019-02-15 15:57:37 +0200
committerMahmoud Badri <mahmoud.badri@qt.io>2019-02-18 18:32:33 +0000
commit1fa0d29b1b6f11b58b0cbf2b5fc64d59235f098c (patch)
tree32a0a851341ba65c276bb9d0a647b92e21153e79
parent6acfb03a5a3bcc8e49b24ba1611b78df2ba4981a (diff)
Variants: use QHash instead of QVector
To make lookup fast. Also neater. Task-number: QT3DS-3047 Change-Id: I3ce05252b9eb97a436ea66d4148fb5fa406898ff Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
-rw-r--r--src/Authoring/Studio/Application/ProjectFile.cpp94
-rw-r--r--src/Authoring/Studio/Application/ProjectFile.h6
-rw-r--r--src/Authoring/Studio/Palettes/Inspector/InspectorControlView.cpp9
-rw-r--r--src/Authoring/Studio/Palettes/Inspector/VariantsGroupModel.cpp22
4 files changed, 39 insertions, 92 deletions
diff --git a/src/Authoring/Studio/Application/ProjectFile.cpp b/src/Authoring/Studio/Application/ProjectFile.cpp
index c4a09b8e..21b26b8c 100644
--- a/src/Authoring/Studio/Application/ProjectFile.cpp
+++ b/src/Authoring/Studio/Application/ProjectFile.cpp
@@ -962,19 +962,7 @@ void ProjectFile::loadVariants(const QString &filePath)
if (reader.name() == QLatin1String("variantgroup")) {
QString groupId = reader.attributes().value(QLatin1String("id")).toString();
QString groupColor = reader.attributes().value(QLatin1String("color")).toString();
- auto it = std::find_if(m_variantsDef.begin(), m_variantsDef.end(),
- [&](const VariantGroup &vg) -> bool {
- return vg.m_title == groupId;
- });
-
- if (it != m_variantsDef.end()) { // group exists, override it
- currentGroup = it;
- } else {
- VariantGroup g;
- m_variantsDef.append(g);
- currentGroup = &m_variantsDef.last();
- }
- currentGroup->m_title = groupId;
+ currentGroup = &m_variantsDef[groupId];
currentGroup->m_color = groupColor;
} else if (reader.name() == QLatin1String("variant")) {
if (currentGroup) {
@@ -1004,13 +992,14 @@ void ProjectFile::loadVariants(const QString &filePath)
vElem = domDoc.createElement(QStringLiteral("variants"));
domDoc.documentElement().appendChild(vElem);
- for (auto &g : qAsConst(m_variantsDef)) {
+ const auto keys = m_variantsDef.keys();
+ for (auto &g : keys) {
QDomElement gElem = domDoc.createElement(QStringLiteral("variantgroup"));
- gElem.setAttribute(QStringLiteral("id"), g.m_title);
- gElem.setAttribute(QStringLiteral("color"), g.m_color);
+ gElem.setAttribute(QStringLiteral("id"), g);
+ gElem.setAttribute(QStringLiteral("color"), m_variantsDef[g].m_color);
vElem.appendChild(gElem);
- for (auto &t : qAsConst(g.m_tags)) {
+ for (auto &t : qAsConst(m_variantsDef[g].m_tags)) {
QDomElement tElem = domDoc.createElement(QStringLiteral("variant"));
tElem.setAttribute(QStringLiteral("id"), t);
gElem.appendChild(tElem);
@@ -1047,12 +1036,7 @@ void ProjectFile::addVariantTag(const QString &group, const QString &newTag)
}
// update m_variantsDef
- for (auto &g : m_variantsDef) {
- if (g.m_title == group) {
- g.m_tags.append(newTag);
- break;
- }
- }
+ m_variantsDef[group].m_tags.append(newTag);
}
// Add a new group, it is assumes that the new group name is unique
@@ -1084,9 +1068,8 @@ void ProjectFile::addVariantGroup(const QString &newGroup)
// update m_variantsDef
VariantGroup g;
- g.m_title = newGroup;
g.m_color = newColor;
- m_variantsDef.append(g);
+ m_variantsDef[newGroup] = g;
}
void ProjectFile::renameVariantTag(const QString &group, const QString &oldTag,
@@ -1153,18 +1136,11 @@ void ProjectFile::renameVariantTag(const QString &group, const QString &oldTag,
}
// update m_variantsDef
- renamed = false;
- for (auto &g : m_variantsDef) {
- if (g.m_title == group) {
- for (auto &t : g.m_tags) {
- if (t == oldTag) {
- t = newTag;
- renamed = true;
- break;
- }
- }
- if (renamed)
- break;
+ for (auto &t : m_variantsDef[group].m_tags) {
+ if (t == oldTag) {
+ t = newTag;
+ renamed = true;
+ break;
}
}
}
@@ -1223,12 +1199,8 @@ void ProjectFile::renameVariantGroup(const QString &oldGroup, const QString &new
}
// update m_variantsDef
- for (auto &g : m_variantsDef) {
- if (g.m_title == oldGroup) {
- g.m_title = newGroup;
- break;
- }
- }
+ m_variantsDef[newGroup] = m_variantsDef[oldGroup];
+ m_variantsDef.remove(oldGroup);
}
void ProjectFile::deleteVariantGroup(const QString &group)
@@ -1322,12 +1294,7 @@ void ProjectFile::deleteVariantGroup(const QString &group)
}
// update m_variantsDef
- for (auto &g : m_variantsDef) {
- if (g.m_title == group) {
- m_variantsDef.erase(&g);
- break;
- }
- }
+ m_variantsDef.remove(group);
}
void ProjectFile::changeVariantGroupColor(const QString &group, const QString &newColor)
@@ -1352,12 +1319,7 @@ void ProjectFile::changeVariantGroupColor(const QString &group, const QString &n
}
// update m_variantsDef
- for (auto &g : m_variantsDef) {
- if (g.m_title == group) {
- g.m_color = newColor;
- break;
- }
- }
+ m_variantsDef[group].m_color = newColor;
}
bool ProjectFile::tagExistsInUip(const QString &src, const QString &group, const QString &tag) const
@@ -1531,22 +1493,15 @@ void ProjectFile::deleteGroupFromUip(const QString &src, const QString &group)
bool ProjectFile::isVariantGroupUnique(const QString &group) const
{
- for (auto &g : qAsConst(m_variantsDef)) {
- if (g.m_title == group)
- return false;
- }
-
- return true;
+ return !m_variantsDef.contains(group);
}
bool ProjectFile::isVariantTagUnique(const QString &group, const QString &tag) const
{
- for (auto &g : qAsConst(m_variantsDef)) {
- if (g.m_title == group)
- return !g.m_tags.contains(tag);
- }
+ if (!m_variantsDef.contains(group))
+ return true;
- return true;
+ return !m_variantsDef[group].m_tags.contains(tag);
}
void ProjectFile::deleteVariantTag(const QString &group, const QString &tag)
@@ -1623,10 +1578,5 @@ void ProjectFile::deleteVariantTag(const QString &group, const QString &tag)
}
// update m_variantsDef
- for (auto &g : m_variantsDef) {
- if (g.m_title == group) {
- g.m_tags.removeOne(tag);
- break;
- }
- }
+ m_variantsDef[group].m_tags.removeOne(tag);
}
diff --git a/src/Authoring/Studio/Application/ProjectFile.h b/src/Authoring/Studio/Application/ProjectFile.h
index 6b138666..78ecc6d6 100644
--- a/src/Authoring/Studio/Application/ProjectFile.h
+++ b/src/Authoring/Studio/Application/ProjectFile.h
@@ -47,7 +47,6 @@ public:
ProjectFile();
struct VariantGroup {
- QString m_title;
QString m_color;
QStringList m_tags;
};
@@ -98,8 +97,7 @@ public:
bool isVariantGroupUnique(const QString &group) const;
bool isVariantTagUnique(const QString &group, const QString &tag) const;
- QVector<VariantGroup> variantsDef() const { return m_variantsDef; }
-
+ QHash<QString, VariantGroup> variantsDef() const { return m_variantsDef; }
Q_SIGNALS:
void presentationIdChanged(const QString &path, const QString &id);
@@ -117,7 +115,7 @@ private:
QFileInfo m_fileInfo; // uia file info
QString m_initialPresentation;
- QVector<VariantGroup> m_variantsDef; // definition of variants
+ QHash<QString, VariantGroup> m_variantsDef; // definition of variants
};
#endif // PROJECTFILE_H
diff --git a/src/Authoring/Studio/Palettes/Inspector/InspectorControlView.cpp b/src/Authoring/Studio/Palettes/Inspector/InspectorControlView.cpp
index 10222fcd..230c0d0e 100644
--- a/src/Authoring/Studio/Palettes/Inspector/InspectorControlView.cpp
+++ b/src/Authoring/Studio/Palettes/Inspector/InspectorControlView.cpp
@@ -499,13 +499,8 @@ void InspectorControlView::showGroupContextMenu(int x, int y, const QString &gro
auto actionColor = theContextMenu.addAction(QObject::tr("Change Group Color"));
connect(actionColor, &QAction::triggered, this, [&]() {
const auto variantsDef = g_StudioApp.GetCore()->getProjectFile().variantsDef();
- for (auto &g : variantsDef) {
- if (g.m_title == group) {
- QColor newColor = this->showColorDialog(g.m_color);
- projectFile.changeVariantGroupColor(group, newColor.name());
- break;
- }
- }
+ QColor newColor = this->showColorDialog(variantsDef[group].m_color);
+ projectFile.changeVariantGroupColor(group, newColor.name());
});
auto actionDelete = theContextMenu.addAction(QObject::tr("Delete Group"));
diff --git a/src/Authoring/Studio/Palettes/Inspector/VariantsGroupModel.cpp b/src/Authoring/Studio/Palettes/Inspector/VariantsGroupModel.cpp
index 87219554..37814b56 100644
--- a/src/Authoring/Studio/Palettes/Inspector/VariantsGroupModel.cpp
+++ b/src/Authoring/Studio/Palettes/Inspector/VariantsGroupModel.cpp
@@ -82,15 +82,17 @@ void VariantsGroupModel::refresh()
// build the variants data model
const auto variantsDef = g_StudioApp.GetCore()->getProjectFile().variantsDef();
- for (auto &group : variantsDef) {
+ const auto keys = variantsDef.keys();
+ for (auto &group : keys) {
TagGroupData g;
- g.m_title = group.m_title;
- g.m_color = group.m_color;
+ g.m_title = group;
+ g.m_color = variantsDef[group].m_color;
VariantsTagModel *m = new VariantsTagModel(this);
QVector<std::pair<QString, bool> > tags;
- for (int i = 0; i < group.m_tags.length(); ++i)
- tags.append({group.m_tags[i], propTags[group.m_title].contains(group.m_tags[i])});
+ for (int i = 0; i < variantsDef[group].m_tags.length(); ++i)
+ tags.append({variantsDef[group].m_tags[i],
+ propTags[group].contains(variantsDef[group].m_tags[i])});
m->init(tags);
g.m_tagsModel = m;
@@ -187,15 +189,17 @@ void VariantsGroupModel::exportVariants()
" encoding=\"utf-8\"")));
const auto variantsDef = g_StudioApp.GetCore()->getProjectFile().variantsDef();
+ const auto keys = variantsDef.keys();
QDomElement vElem = domDoc.createElement(QStringLiteral("variants"));
domDoc.appendChild(vElem);
- for (auto &g : variantsDef) {
+ for (auto &g : keys) {
+ const auto group = variantsDef[g];
QDomElement gElem = domDoc.createElement(QStringLiteral("variantgroup"));
- gElem.setAttribute(QStringLiteral("id"), g.m_title);
- gElem.setAttribute(QStringLiteral("color"), g.m_color);
+ gElem.setAttribute(QStringLiteral("id"), g);
+ gElem.setAttribute(QStringLiteral("color"), group.m_color);
vElem.appendChild(gElem);
- for (auto &t : qAsConst(g.m_tags)) {
+ for (auto &t : qAsConst(group.m_tags)) {
QDomElement tElem = domDoc.createElement(QStringLiteral("variant"));;
tElem.setAttribute(QStringLiteral("id"), t);
gElem.appendChild(tElem);