Commit d6a53df0 authored by Haocheng LIU's avatar Haocheng LIU
Browse files

Add convenience methods for UUID&QVariant conversions

Storing UUID in QVariant as a string for conversion can be expenseive.
Instead, it should be stored as a QByteArray. All future UUID&QVariant
storage should use these functions.
parent 3a99fd93
Pipeline #56251 passed with stage
......@@ -45,6 +45,7 @@
#include "smtk/extension/qt/qtModelPanel.h"
#include "smtk/extension/qt/qtModelOperationWidget.h"
#include "smtk/extension/qt/qtOperatorDockWidget.h"
#include "smtk/extension/qt/qtSMTKUtilities.h"
#include "smtk/extension/qt/qtUIManager.h"
#include "smtk/mesh/Collection.h"
......@@ -935,7 +936,7 @@ void qtModelView::showContextMenu(const QModelIndex &idx, const QPoint& p)
it != sinfo.first.end(); ++it)
{
QAction* act = this->m_ContextMenu->addAction((*it).c_str());
QVariant vdata( QString::fromStdString(sessionString) );
QVariant vdata = qtSMTKUtilities::UUIDToQVariant(brSession.entity());
act->setData(vdata);
QObject::connect(act, SIGNAL(triggered()), this, SLOT(operatorInvoked()));
}
......@@ -944,7 +945,7 @@ void qtModelView::showContextMenu(const QModelIndex &idx, const QPoint& p)
{ // set active model
std::string setAsActiveModel("set as active model");
QAction* act = this->m_ContextMenu->addAction(setAsActiveModel.c_str());
QVariant vdata( QString::fromStdString(sessionString) );
QVariant vdata = qtSMTKUtilities::UUIDToQVariant(brSession.entity());
act->setData(vdata);
QObject::connect(act, SIGNAL(triggered()), this, SLOT(updateActiveModelByModelIndex()));
}
......@@ -1015,8 +1016,8 @@ void qtModelView::operatorInvoked()
return;
}
QVariant var = action->data();
std::string sessionName = var.toString().toStdString();
smtk::common::UUID sessId(sessionName);
smtk::common::UUID sessId = qtSMTKUtilities::QVariantToUUID(var);
std::string sessionName = sessId.toString();
smtk::extension::QEntityItemModel* qmodel = this->getModel();
smtk::model::SessionPtr session =
......
......@@ -10,6 +10,8 @@
#include "smtk/extension/qt/qtSMTKUtilities.h"
#include "smtk/extension/qt/qtUIManager.h"
#include "smtk/common/UUID.h"
#include "smtk/model/EntityRef.h"
SMTKViewConstructorMap qtSMTKUtilities::m_viewConstructors;
......@@ -36,3 +38,37 @@ void qtSMTKUtilities::updateViewConstructors(smtk::extension::qtUIManager* uiMan
uiMan->registerViewConstructor(it->first, it->second);
}
}
QVariant qtSMTKUtilities::UUIDToQVariant(const smtk::common::UUID &uuid)
{
QVariant vdata(QByteArray(reinterpret_cast<const char*>(
uuid.begin()), uuid.size()));
return vdata;
}
QVariant qtSMTKUtilities::entityRefToQVariant(const smtk::model::EntityRef &ent)
{
return qtSMTKUtilities::UUIDToQVariant(ent.entity());
}
smtk::common::UUID qtSMTKUtilities::QVariantToUUID(QVariant variant)
{
QByteArray uuidData = variant.toByteArray();
if (uuidData.size() != static_cast<int>(smtk::common::UUID::size()))
{
return smtk::common::UUID();
}
smtk::common::UUID uuid(
reinterpret_cast<smtk::common::UUID::const_iterator>(uuidData.constData()),
reinterpret_cast<smtk::common::UUID::const_iterator>(uuidData.constData()
+ uuidData.size()));
return uuid;
}
smtk::model::EntityRef qtSMTKUtilities::QVariantToEntityRef(QVariant variant,
smtk::model::ManagerPtr mgr)
{
smtk::common::UUID uuid = qtSMTKUtilities::QVariantToUUID(variant);
return smtk::model::EntityRef(mgr, uuid);
}
......@@ -14,6 +14,7 @@
#include "smtk/extension/qt/qtViewInterface.h" // for qtSMTKViewConstructor definition
#include <map>
#include <QVariant>
typedef std::map<std::string, qtSMTKViewConstructor> SMTKViewConstructorMap;
/// qtSMTKUtilities is a collection of arbitrary utility functions that can be
......@@ -30,6 +31,13 @@ public:
static void updateViewConstructors(smtk::extension::qtUIManager* uiMan);
// convenient method for qvariant - UUID/EntityRef conversion
static QVariant UUIDToQVariant(const smtk::common::UUID& uuid);
static QVariant entityRefToQVariant(const smtk::model::EntityRef& ent);
static smtk::common::UUID QVariantToUUID(QVariant variant);
static smtk::model::EntityRef QVariantToEntityRef(QVariant variant,
smtk::model::ManagerPtr mgr);
private:
static SMTKViewConstructorMap m_viewConstructors;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment