Skip to content

Commit

Permalink
Adding the possibility to filter on property.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Feustel committed Apr 26, 2024
1 parent ef80122 commit d918a14
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 28 deletions.
8 changes: 8 additions & 0 deletions lib/src/Data/ItemPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ ItemPath::ItemPath(const std::string& path)
component = '\"' + component + '\"';
m_components.push_back(std::move(component));
}

if (pathss.peek() == '(') {
getline(pathss, component, '(');
getline(pathss, component, ')');
component = '(' + component + ')';
m_components.push_back(std::move(component));
}

getline(pathss, component, '/');
if (component.length() > 0) {
m_components.push_back(std::move(component));
Expand Down
28 changes: 14 additions & 14 deletions lib/src/Scene/Qt/QtItemTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ QString GetObjectName(QObject* object)
return object->objectName();
}

QString TextPropertyByObject(QObject* object)
QString PropertValueByObject(QObject* object, QString propertyName)
{
if (object == nullptr) {
return "";
}

auto objectText = object->property("text");
auto property = object->property(propertyName.toStdString().c_str());
auto objectVisible = object->property("visible");

if (objectText.isNull() || objectVisible.isNull()) {
if (property.isNull() || objectVisible.isNull()) {
return "";
}

if (objectText.isValid() && objectVisible.toBool()) {
return objectText.toString();
if (property.isValid() && objectVisible.toBool()) {
return property.toString();
}

return "";
Expand All @@ -95,8 +95,8 @@ QString TypeByObject(QObject* object)
return typeName;
}

QObject* FindChildItem(QObject* object, const QString& name, const std::optional<QString>& propertyText = {},
const std::optional<QString>& type = {})
QObject* FindChildItem(QObject* object, const QString& name, const std::optional<QString>& propertyName = {},
const std::optional<QString>& propertyValue = {}, const std::optional<QString>& type = {})
{
if (object == nullptr) {
return nullptr;
Expand All @@ -109,20 +109,20 @@ QObject* FindChildItem(QObject* object, const QString& name, const std::optional
if (GetObjectName(child) == name) {
return child;
}
if (propertyText.has_value()) {
if (TextPropertyByObject(child) == propertyText.value()) {
if (propertyName.has_value() && propertyValue.has_value()) {
if (PropertValueByObject(child, propertyName.value()) == propertyValue.value()) {
return child;
}

if (auto item = FindChildItem(child, name, propertyText, {})) {
if (auto item = FindChildItem(child, name, propertyName.value(), propertyValue.value(), {})) {
return item;
}
} else if (type.has_value()) {
if (TypeByObject(child) == type.value()) {
return child;
}

if (auto item = FindChildItem(child, name, {}, type)) {
if (auto item = FindChildItem(child, name, {}, {}, type)) {
return item;
}

Expand All @@ -139,12 +139,12 @@ QObject* FindChildItem(QObject* object, const QString& name, const std::optional
return child;
}

if (propertyText.has_value()) {
if (auto item = FindChildItem(child, name, propertyText, {})) {
if (propertyValue.has_value() && propertyName.has_value()) {
if (auto item = FindChildItem(child, name, propertyName.value(), propertyValue.value(), {})) {
return item;
}
} else if (type.has_value()) {
if (auto item = FindChildItem(child, name, {}, type)) {
if (auto item = FindChildItem(child, name, {}, {}, type)) {
return item;
}
} else {
Expand Down
14 changes: 7 additions & 7 deletions lib/src/Scene/Qt/QtItemTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ QQuickItem* RepeaterChildAtIndex(QQuickItem* repeater, int index);
QQuickItem* RepeaterChildWithName(QQuickItem* repeater, const QString& name);

QString GetObjectName(QObject* object);
QString TextPropertyByObject(QObject* object);
QString PropertValueByObject(QObject* object, QString propertyName);
QString TypeByObject(QObject* object);
/**
* @brief Find a child object with the given name.
Expand All @@ -35,21 +35,21 @@ QString TypeByObject(QObject* object);
* encounters a `QQuickItem`, it no longer iterates over the object's
* `children()`, but rather its `childItems()`.
*/
QObject* FindChildItem(QObject* object, const QString& name, const std::optional<QString>& propertyText,
const std::optional<QString>& type);
QObject* FindChildItem(QObject* object, const QString& name, const std::optional<QString>& propertyName,
const std::optional<QString>& propertyValue, const std::optional<QString>& type);
QVector<QObject*> FindChildItems(QObject* object, const std::optional<QString>& type);

template <typename T>
T FindChildItem(QObject* object, const QString& name, const std::optional<QString>& propertyText,
const std::optional<QString>& type)
T FindChildItem(QObject* object, const QString& name, const std::optional<QString>& propertyName,
const std::optional<QString>& propertyValue, const std::optional<QString>& type)
{
return qobject_cast<T>(FindChildItem(object, name, propertyText, type));
return qobject_cast<T>(FindChildItem(object, name, propertyName, propertyValue, type));
}

template <typename T>
T FindChildItem(QObject* object, const QString& name)
{
return qobject_cast<T>(FindChildItem(object, name, {}, {}));
return qobject_cast<T>(FindChildItem(object, name, {}, {}, {}));
}

using QMLReturnVariant = std::variant<std::nullptr_t, bool, int, float, double, QString, QDateTime, QVariant>;
Expand Down
27 changes: 20 additions & 7 deletions lib/src/Scene/Qt/QtScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ namespace {
QString getNameForObject(QObject* object)
{
QString name;
if (spix::qt::TextPropertyByObject(object) != "") {
name = "\"" + spix::qt::TextPropertyByObject(object) + "\"";
if (spix::qt::PropertValueByObject(object, QString::fromStdString("text")) != "") {
name = "\"" + spix::qt::PropertValueByObject(object, QString::fromStdString("text")) + "\"";
} else if (spix::qt::PropertValueByObject(object, QString::fromStdString("source")) != "") {
name = "(source=" + spix::qt::PropertValueByObject(object, QString::fromStdString("source")) + ")";
} else if (spix::qt::GetObjectName(object) != "") {
name = spix::qt::GetObjectName(object);
} else {
Expand Down Expand Up @@ -70,14 +72,15 @@ QQuickItem* getQQuickItemWithRoot(const spix::ItemPath& path, QObject* root)
}

} else if (itemName.compare(0, 1, "\"") == 0) {
auto propertyName = itemName.substr(1);
QVariant propertyValue = root->property(propertyName.c_str());

// remove ""
size_t found = itemName.find("\"");
auto searchText = itemName.substr(found + 1, itemName.length() - 2);
subItem = spix::qt::FindChildItem<QQuickItem*>(root, itemName.c_str(), QString::fromStdString(searchText), {});

subItem = spix::qt::FindChildItem<QQuickItem*>(
root, itemName.c_str(), QString::fromStdString("text"), QString::fromStdString(searchText), {});

} else if (itemName.compare(0, 1, "#") == 0) {
// remove #
size_t found = itemName.find("#");
auto type = QString::fromStdString(itemName.substr(found + 1));

Expand All @@ -91,10 +94,20 @@ QQuickItem* getQQuickItemWithRoot(const spix::ItemPath& path, QObject* root)
}
}
} else {
subItem = spix::qt::FindChildItem<QQuickItem*>(root, itemName.c_str(), {}, type);
subItem = spix::qt::FindChildItem<QQuickItem*>(root, itemName.c_str());
return subItem;
}
} else if (itemName.compare(0, 1, "(") == 0) {
// remove ()
size_t foundBracketSign = itemName.find('(');
auto searchText = itemName.substr(foundBracketSign + 1, itemName.length() - 2);

// Split in to property and value
size_t foundEqualSign = searchText.find('=');
auto property = QString::fromStdString(searchText.substr(0, foundEqualSign));
auto value = QString::fromStdString(searchText.substr(foundEqualSign + 1));

subItem = spix::qt::FindChildItem<QQuickItem*>(root, itemName.c_str(), property, value, {});
} else if (rootClassName == spix::qt::repeater_class_name) {
QQuickItem* repeater = static_cast<QQuickItem*>(root);
subItem = spix::qt::RepeaterChildWithName(repeater, QString::fromStdString(itemName));
Expand Down

0 comments on commit d918a14

Please sign in to comment.