Skip to content

Commit

Permalink
Add utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasKroes committed Nov 26, 2024
1 parent eeacd1a commit 3236baf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
2 changes: 0 additions & 2 deletions ManiVault/src/actions/ToggleAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ bool ToggleAction::CheckBoxWidget::eventFilter(QObject* target, QEvent* event)

if (_toggleAction->isEnabled() && mouseEvent->button() == Qt::LeftButton && !_toggleAction->getDrag().isDragging())
{
qDebug() << "Toggle!";
_toggleAction->setChecked(!_toggleAction->isChecked());
}


return true;
}
Expand Down
72 changes: 72 additions & 0 deletions ManiVault/src/util/Miscellaneous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QStringList>
#include <QTcpSocket>
#include <QUrl>
#include <QVariant>
#include <QString>

namespace mv::util
{
Expand Down Expand Up @@ -180,4 +182,74 @@ QIcon getAlignmentIcon(const Qt::Alignment& alignment)
return gui::createIcon(pixmap);
}

void setValueByPath(QVariant& root, const QString& path, const QVariant& value)
{
QStringList components = path.split('/', Qt::SkipEmptyParts);

if (components.isEmpty())
return;

QVariant* current = &root;

for (int i = 0; i < components.size(); ++i) {
const QString& key = components[i];

// If this is the last component, set the value
if (i == components.size() - 1) {
if (current->type() == QMetaType::QVariantMap || current->isNull()) {
QVariantMap map = current->toMap();
map[key] = value;
*current = map;
}
else {
qWarning() << "Cannot set value at path:" << path << "as current is not a map.";
}

return;
}

// Ensure the current level is a map
if (current->typeId() == QMetaType::QVariantMap || current->isNull()) {
QVariantMap map = current->toMap();

// If the key doesn't exist, initialize it with an empty map
if (!map.contains(key)) {
map[key] = QVariantMap();
}

*current = map;
current = &((*current).toMap()[key]);
}
else {
qWarning() << "Cannot create intermediate path:" << key << "as current is not a map.";
return;
}
}
}

QVariant getValueByPath(const QVariant& root, const QString& path, const QVariant& valueIfNotFound /*= QVariant()*/)
{
QStringList components = path.split('/', Qt::SkipEmptyParts);
const QVariant* current = &root;

QVariant foundValue;

for (const QString& key : components) {
if (current->typeId() == QMetaType::QVariantMap) {
const auto map = current->toMap();

if (map.contains(key)) {
foundValue = map[key];
}
else {
return valueIfNotFound; // Return invalid QVariant if the key doesn't exist
}
}
else {
return valueIfNotFound; // Return invalid QVariant if the current node isn't a map
}
}

return foundValue; // Return the found value
}
}
17 changes: 16 additions & 1 deletion ManiVault/src/util/Miscellaneous.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <QString>
#include <QWidget>
#include <QPointer>
#include <QDebug>

#include <algorithm>
#include <qtcpsocket.h>
Expand Down Expand Up @@ -107,4 +106,20 @@ CORE_EXPORT void clearLayout(QLayout* layout, bool removeWidgets = false);
CORE_EXPORT void setLayoutContentsMargins(QLayout* layout, std::int32_t margin);
CORE_EXPORT QIcon getAlignmentIcon(const Qt::Alignment& alignment);

/**
* Set child \p value of \root by \p path
* @param root Root element
* @param path Path to the value
* @param value Value to set
*/
CORE_EXPORT void setValueByPath(QVariant& root, const QString& path, const QVariant& value);

/**
* Get child value of \root by \p path
* @param root Root element
* @param path Path to the value
* @param valueIfNotFound Value if not found
* @return Value, invalid when no value was found
*/
CORE_EXPORT QVariant getValueByPath(const QVariant& root, const QString& path, const QVariant& valueIfNotFound = QVariant());
}

0 comments on commit 3236baf

Please sign in to comment.