Skip to content
Snippets Groups Projects

Store task UI geometry in project file

Merged John Tourtellott requested to merge john.tourtellott/smtk:john/task-ui-geometry into task-node
3 unresolved threads
Files
10
@@ -33,6 +33,14 @@
#include <QVBoxLayout>
#include <QWidget>
#include <string>
namespace
{
// String constant used to designate UI features in serialized task manager.
const std::string JSON_LABEL = "qtTaskEditor";
Please register or sign in to reply
}
namespace smtk
{
namespace extension
@@ -72,18 +80,61 @@ public:
QTimer::singleShot(0, [this]()
{
// TODO: If the user has already manually edited node
// placement, we don't want to do this. How
// do we detect?
this->computeNodeLayout();
bool modified = this->computeNodeLayout();
m_widget->ensureVisible(m_scene->sceneRect());
// After layout complete, connect nodeMoved signal
for (const auto& entry : m_taskIndex)
{
qtTaskNode* taskNode = entry.second;
QObject::connect(
taskNode, &qtTaskNode::nodeMoved, m_self, &qtTaskEditor::onNodeGeometryChanged);
if (modified)
{
Q_EMIT taskNode->nodeMoved();
}
}
}
);
}
void computeNodeLayout()
// Returns true if UI configuration was modified
bool computeNodeLayout()
{
if (m_taskIndex.empty())
{
return false;
}
// Check if taskManager has UI config objects first
bool configured = false;
auto* firstTask = m_taskIndex.begin()->first;
auto* taskManager = firstTask->manager();
taskManager->setUiLabel(JSON_LABEL);
Please register or sign in to reply
for (const auto& entry : m_taskIndex)
{
nlohmann::json uiConfig = taskManager->uiConfig(entry.first->id());
if (uiConfig.contains("position"))
{
auto jPosition = uiConfig["position"];
double x = jPosition[0].get<double>();
double y = jPosition[1].get<double>();
auto* node = entry.second;
node->setPos(x, y);
configured = true;
} // if
} // for
if (configured)
{
return false; // not modified
}
// If geometry not configured, call the scenes method to layout nodes
std::unordered_set<qtTaskNode*> nodes;
std::unordered_set<qtTaskArc*> arcs;
for (const auto& entry : m_taskIndex)
@@ -97,7 +148,13 @@ public:
arcs.insert(arc);
}
}
m_scene->computeLayout(nodes, arcs);
if (m_scene->computeLayout(nodes, arcs) == 1)
{
return true;
}
// (else)
return false;
}
void removeObservers()
@@ -367,6 +424,7 @@ void qtTaskEditor::displayProject(const std::shared_ptr<smtk::project::Project>&
if (project)
{
this->displayTaskManager(&project->taskManager());
project->taskManager().setUiLabel(JSON_LABEL);
// TODO: Observe project's manager and clear this view if a different project is loaded.
}
else
@@ -379,6 +437,7 @@ void qtTaskEditor::displayProject(const std::shared_ptr<smtk::project::Project>&
void qtTaskEditor::displayTaskManager(smtk::task::Manager* taskManager)
{
m_p->displayTaskManager(taskManager);
taskManager->setUiLabel(JSON_LABEL);
}
void qtTaskEditor::switchToTask(const std::shared_ptr<smtk::task::Task>& task)
@@ -405,5 +464,20 @@ std::shared_ptr<smtk::view::Configuration> qtTaskEditor::defaultConfiguration()
return result;
}
void qtTaskEditor::onNodeGeometryChanged()
{
auto* qtNode = dynamic_cast<qtTaskNode*>(this->sender());
if (qtNode == nullptr)
{
return;
}
// Pass node's geometry to task manager
QPointF pos = qtNode->scenePos();
nlohmann::json uiGeom = nlohmann::json::object();
uiGeom["position"] = { pos.x(), pos.y() };
m_p->m_taskManager->uiGeometryChanged(qtNode->task()->id(), uiGeom);
}
} // namespace extension
} // namespace smtk
Loading