store expanded status in context

This commit is contained in:
itsmattkc
2021-11-19 17:14:56 -08:00
parent a391e63252
commit a94c7169f2
8 changed files with 143 additions and 85 deletions
+11 -7
View File
@@ -243,12 +243,16 @@ QIcon Node::icon() const
return icon::New;
}
QPointF Node::GetNodePositionInContext(Node *node)
bool Node::SetNodePositionInContext(Node *node, const QPointF &pos)
{
return context_positions_.value(node);
Position p = context_positions_.value(node);
p.position = pos;
return SetNodePositionInContext(node, p);
}
bool Node::SetNodePositionInContext(Node *node, const QPointF &pos)
bool Node::SetNodePositionInContext(Node *node, const Position &pos)
{
bool added = !ContextContainsNode(node);
context_positions_.insert(node, pos);
@@ -257,7 +261,7 @@ bool Node::SetNodePositionInContext(Node *node, const QPointF &pos)
emit NodeAddedToContext(node);
}
emit NodePositionInContextChanged(node, pos);
emit NodePositionInContextChanged(node, pos.position);
return added;
}
@@ -2350,13 +2354,13 @@ Project *Node::ArrayResizeCommand::GetRelevantProject() const
void NodeSetPositionCommand::redo()
{
if (!(added_ = !context_->ContextContainsNode(node_))) {
old_pos_ = context_->GetNodePositionInContext(node_);
old_pos_ = context_->GetNodePositionDataInContext(node_);
}
if (added_) {
context_->SetNodePositionInContext(node_, pos_);
} else {
move(context_, node_, pos_ - old_pos_, move_deps_);
move(context_, node_, pos_.position - old_pos_.position, move_deps_);
}
}
@@ -2365,7 +2369,7 @@ void NodeSetPositionCommand::undo()
if (added_) {
context_->RemoveNodeFromContext(node_);
} else {
move(context_, node_, old_pos_ - pos_, move_deps_);
move(context_, node_, old_pos_.position - pos_.position, move_deps_);
}
}
+61 -10
View File
@@ -208,21 +208,77 @@ public:
return HasInputWithID(id);
}
using PositionMap = QHash<Node*, QPointF>;
struct Position
{
Position(const QPointF &p = QPointF(0, 0), bool e = false)
{
position = p;
expanded = e;
}
QPointF position;
bool expanded;
inline Position &operator+=(const Position &p)
{
position += p.position;
return *this;
}
inline Position &operator-=(const Position &p)
{
position -= p.position;
return *this;
}
friend inline const Position operator+(Position a, const Position &b)
{
a += b;
return a;
}
friend inline const Position operator-(Position a, const Position &b)
{
a -= b;
return a;
}
};
using PositionMap = QHash<Node*, Position>;
const PositionMap &GetContextPositions() const
{
return context_positions_;
}
bool IsNodeExpandedInContext(Node *node) const
{
return context_positions_.value(node).expanded;
}
bool ContextContainsNode(Node *node) const
{
return context_positions_.contains(node);
}
QPointF GetNodePositionInContext(Node *node);
Position GetNodePositionDataInContext(Node *node)
{
return context_positions_.value(node);
}
QPointF GetNodePositionInContext(Node *node)
{
return GetNodePositionDataInContext(node).position;
}
bool SetNodePositionInContext(Node *node, const QPointF &pos);
bool SetNodePositionInContext(Node *node, const Position &pos);
void SetNodeExpandedInContext(Node *node, bool e)
{
context_positions_[node].expanded = e;
}
bool RemoveNodeFromContext(Node *node);
/**
@@ -1006,11 +1062,6 @@ protected:
}
signals:
/**
* @brief Signal emitted whenever the position is set through SetPosition()
*/
void PositionChanged(const QPointF& pos);
/**
* @brief Signal emitted when SetLabel() is called
*/
@@ -1394,7 +1445,7 @@ using NodePtr = std::shared_ptr<Node>;
class NodeSetPositionCommand : public UndoCommand
{
public:
NodeSetPositionCommand(Node* node, Node* context, const QPointF& pos, bool move_dependencies_relatively = false)
NodeSetPositionCommand(Node* node, Node* context, const Node::Position& pos, bool move_dependencies_relatively = false)
{
node_ = node;
context_ = context;
@@ -1417,8 +1468,8 @@ private:
Node* node_;
Node* context_;
QPointF pos_;
QPointF old_pos_;
Node::Position pos_;
Node::Position old_pos_;
bool added_;
bool move_deps_;
+4 -4
View File
@@ -92,7 +92,7 @@ QVector<Node *> NodeCopyPasteService::PasteNodesFromClipboard(NodeGraph *graph,
QVector<Node*> pasted_nodes;
XMLNodeData xml_node_data;
QMap<quintptr, QMap<quintptr, QPointF> > pasted_contexts;
QMap<quintptr, QMap<quintptr, Node::Position> > pasted_contexts;
while (XMLReadNextStartElement(&reader)) {
if (reader.name() == QStringLiteral("olive")) {
@@ -131,7 +131,7 @@ QVector<Node *> NodeCopyPasteService::PasteNodesFromClipboard(NodeGraph *graph,
while (XMLReadNextStartElement(&reader)) {
if (reader.name() == QStringLiteral("context")) {
// Get context ptr
QMap<quintptr, QPointF> map;
QMap<quintptr, Node::Position> map;
quintptr context_ptr = 0;
XMLAttributeLoop((&reader), attr) {
if (attr.name() == QStringLiteral("ptr")) {
@@ -144,7 +144,7 @@ QVector<Node *> NodeCopyPasteService::PasteNodesFromClipboard(NodeGraph *graph,
while (XMLReadNextStartElement(&reader)) {
if (reader.name() == QStringLiteral("node")) {
quintptr node_ptr;
QPointF node_pos;
Node::Position node_pos;
if (Project::LoadPosition(&reader, &node_ptr, &node_pos)) {
map.insert(node_ptr, node_pos);
@@ -216,7 +216,7 @@ QVector<Node *> NodeCopyPasteService::PasteNodesFromClipboard(NodeGraph *graph,
for (auto it=pasted_contexts.cbegin(); it!=pasted_contexts.cend(); it++) {
Node *context = xml_node_data.node_ptrs.value(it.key());
if (context) {
const QMap<quintptr, QPointF> &map = it.value();
auto map = it.value();
for (auto jt=map.cbegin(); jt!=map.cend(); jt++) {
Node *subnode = xml_node_data.node_ptrs.value(jt.key());
if (subnode) {
+10 -7
View File
@@ -158,7 +158,7 @@ void Project::Load(QXmlStreamReader *reader, MainWindowLayoutInfo* layout, uint
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("node")) {
quintptr node_ptr;
QPointF node_pos;
Node::Position node_pos;
if (LoadPosition(reader, &node_ptr, &node_pos)) {
Node *node = xml_node_data.node_ptrs.value(node_ptr);
@@ -350,7 +350,7 @@ void Project::RegenerateUuid()
uuid_ = QUuid::createUuid();
}
bool Project::LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, QPointF *pos)
bool Project::LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, Node::Position *pos)
{
bool got_node_ptr = false;
bool got_pos_x = false;
@@ -366,11 +366,13 @@ bool Project::LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, QPointF
while (XMLReadNextStartElement(reader)) {
if (reader->name() == QStringLiteral("x")) {
pos->setX(reader->readElementText().toDouble());
pos->position.setX(reader->readElementText().toDouble());
got_pos_x = true;
} else if (reader->name() == QStringLiteral("y")) {
pos->setY(reader->readElementText().toDouble());
pos->position.setY(reader->readElementText().toDouble());
got_pos_y = true;
} else if (reader->name() == QStringLiteral("expanded")) {
pos->expanded = reader->readElementText().toInt();
} else {
reader->skipCurrentElement();
}
@@ -379,12 +381,13 @@ bool Project::LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, QPointF
return got_node_ptr && got_pos_x && got_pos_y;
}
void Project::SavePosition(QXmlStreamWriter *writer, Node *node, const QPointF &pos)
void Project::SavePosition(QXmlStreamWriter *writer, Node *node, const Node::Position &pos)
{
writer->writeAttribute(QStringLiteral("ptr"), QString::number(reinterpret_cast<quintptr>(node)));
writer->writeTextElement(QStringLiteral("x"), QString::number(pos.x()));
writer->writeTextElement(QStringLiteral("y"), QString::number(pos.y()));
writer->writeTextElement(QStringLiteral("x"), QString::number(pos.position.x()));
writer->writeTextElement(QStringLiteral("y"), QString::number(pos.position.y()));
writer->writeTextElement(QStringLiteral("expanded"), QString::number(pos.expanded));
}
void Project::ColorManagerValueChanged(const NodeInput &input, const TimeRange &range)
+2 -2
View File
@@ -82,8 +82,8 @@ public:
void RegenerateUuid();
static bool LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, QPointF *pos);
static void SavePosition(QXmlStreamWriter *writer, Node *node, const QPointF &pos);
static bool LoadPosition(QXmlStreamReader *reader, quintptr *node_ptr, Node::Position *pos);
static void SavePosition(QXmlStreamWriter *writer, Node *node, const Node::Position &pos);
signals:
void NameChanged();