nodes: remove graph and merge into project

This commit is contained in:
itsmattkc
2023-02-17 14:13:03 -08:00
parent 491fbacfbc
commit 9745b536f6
62 changed files with 266 additions and 344 deletions
+2 -2
View File
@@ -36,8 +36,6 @@ set(OLIVE_SOURCES
node/factory.h
node/globals.cpp
node/globals.h
node/graph.cpp
node/graph.h
node/inputdragger.cpp
node/inputdragger.h
node/inputimmediate.cpp
@@ -48,6 +46,8 @@ set(OLIVE_SOURCES
node/node.h
node/param.cpp
node/param.h
node/project.cpp
node/project.h
node/splitvalue.h
node/traverser.cpp
node/traverser.h
+5 -7
View File
@@ -21,7 +21,7 @@
#include "ociobase.h"
#include "node/color/colormanager/colormanager.h"
#include "node/project/project.h"
#include "node/project.h"
namespace olive {
@@ -41,13 +41,11 @@ OCIOBaseNode::OCIOBaseNode() :
SetFlags(kVideoEffect);
}
void OCIOBaseNode::AddedToGraph(NodeGraph *graph)
void OCIOBaseNode::AddedToGraph(Project *p)
{
if (Project *p = dynamic_cast<Project*>(graph)) {
manager_ = p->color_manager();
connect(manager_, &ColorManager::ConfigChanged, this, &OCIOBaseNode::ConfigChanged);
ConfigChanged();
}
manager_ = p->color_manager();
connect(manager_, &ColorManager::ConfigChanged, this, &OCIOBaseNode::ConfigChanged);
ConfigChanged();
}
void OCIOBaseNode::RemovedFromGraph()
+1 -1
View File
@@ -51,7 +51,7 @@ private:
ColorProcessorPtr processor_;
private slots:
void AddedToGraph(NodeGraph *graph);
void AddedToGraph(Project *p);
void RemovedFromGraph();
@@ -23,7 +23,7 @@
#include <iostream>
#include "common/ocioutils.h"
#include "node/project/project.h"
#include "node/project.h"
#include "render/colorprocessor.h"
#include "widget/slider/floatslider.h"
+1 -1
View File
@@ -26,7 +26,7 @@
#include "common/html.h"
#include "core.h"
#include "node/project/project.h"
#include "node/project.h"
#include "widget/nodeparamview/nodeparamviewundo.h"
namespace olive {
-131
View File
@@ -1,131 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "graph.h"
#include <QChildEvent>
namespace olive {
#define super QObject
NodeGraph::NodeGraph()
{
}
NodeGraph::~NodeGraph()
{
Clear();
}
void NodeGraph::Clear()
{
// By deleting the last nodes first, we assume that nodes that are most important are deleted last
// (e.g. Project's ColorManager or ProjectSettingsNode.
for (auto it=node_children_.cbegin(); it!=node_children_.cend(); it++) {
(*it)->SetCachesEnabled(false);
}
while (!node_children_.isEmpty()) {
delete node_children_.last();
}
}
int NodeGraph::GetNumberOfContextsNodeIsIn(Node *node, bool except_itself) const
{
int count = 0;
foreach (Node *ctx, node_children_) {
if (ctx->ContextContainsNode(node) && (!except_itself || ctx != node)) {
count++;
}
}
return count;
}
void NodeGraph::childEvent(QChildEvent *event)
{
super::childEvent(event);
Node* node = dynamic_cast<Node*>(event->child());
if (node) {
if (event->type() == QEvent::ChildAdded) {
node_children_.append(node);
// Connect signals
connect(node, &Node::InputConnected, this, &NodeGraph::InputConnected, Qt::DirectConnection);
connect(node, &Node::InputDisconnected, this, &NodeGraph::InputDisconnected, Qt::DirectConnection);
connect(node, &Node::ValueChanged, this, &NodeGraph::ValueChanged, Qt::DirectConnection);
connect(node, &Node::InputValueHintChanged, this, &NodeGraph::InputValueHintChanged, Qt::DirectConnection);
if (NodeGroup *group = dynamic_cast<NodeGroup*>(node)) {
connect(group, &NodeGroup::InputPassthroughAdded, this, &NodeGraph::GroupAddedInputPassthrough, Qt::DirectConnection);
connect(group, &NodeGroup::InputPassthroughRemoved, this, &NodeGraph::GroupRemovedInputPassthrough, Qt::DirectConnection);
connect(group, &NodeGroup::OutputPassthroughChanged, this, &NodeGraph::GroupChangedOutputPassthrough, Qt::DirectConnection);
}
emit NodeAdded(node);
emit node->AddedToGraph(this);
// Emit input connections
for (auto it=node->input_connections().cbegin(); it!=node->input_connections().cend(); it++) {
if (nodes().contains(it->second)) {
emit InputConnected(it->second, it->first);
}
}
// Emit output connections
for (auto it=node->output_connections().cbegin(); it!=node->output_connections().cend(); it++) {
if (nodes().contains(it->second.node())) {
emit InputConnected(it->first, it->second);
}
}
} else if (event->type() == QEvent::ChildRemoved) {
node_children_.removeOne(node);
// Disconnect signals
disconnect(node, &Node::InputConnected, this, &NodeGraph::InputConnected);
disconnect(node, &Node::InputDisconnected, this, &NodeGraph::InputDisconnected);
disconnect(node, &Node::ValueChanged, this, &NodeGraph::ValueChanged);
disconnect(node, &Node::InputValueHintChanged, this, &NodeGraph::InputValueHintChanged);
if (NodeGroup *group = dynamic_cast<NodeGroup*>(node)) {
disconnect(group, &NodeGroup::InputPassthroughAdded, this, &NodeGraph::GroupAddedInputPassthrough);
disconnect(group, &NodeGroup::InputPassthroughRemoved, this, &NodeGraph::GroupRemovedInputPassthrough);
disconnect(group, &NodeGroup::OutputPassthroughChanged, this, &NodeGraph::GroupChangedOutputPassthrough);
}
emit NodeRemoved(node);
emit node->RemovedFromGraph(this);
// Remove from any contexts
foreach (Node *context, node_children_) {
context->RemoveNodeFromContext(node);
}
}
}
}
}
-111
View File
@@ -1,111 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef NODEGRAPH_H
#define NODEGRAPH_H
#include "node/group/group.h"
#include "node/node.h"
namespace olive {
/**
* @brief A collection of nodes
*
* This doesn't technically need to be a derivative of Item, but since both Item and NodeGraph need
* to be QObject derivatives, this simplifies Sequence.
*/
class NodeGraph : public QObject
{
Q_OBJECT
public:
/**
* @brief NodeGraph Constructor
*/
NodeGraph();
/**
* @brief NodeGraph Destructor
*/
virtual ~NodeGraph() override;
/**
* @brief Destructively destroys all nodes in the graph
*/
void Clear();
/**
* @brief Retrieve a complete list of the nodes belonging to this graph
*/
const QVector<Node*>& nodes() const
{
return node_children_;
}
const QVector<Node*>& default_nodes() const
{
return default_nodes_;
}
int GetNumberOfContextsNodeIsIn(Node *node, bool except_itself = false) const;
signals:
/**
* @brief Signal emitted when a Node is added to the graph
*/
void NodeAdded(Node* node);
/**
* @brief Signal emitted when a Node is removed from the graph
*/
void NodeRemoved(Node* node);
void InputConnected(Node *output, const NodeInput& input);
void InputDisconnected(Node *output, const NodeInput& input);
void ValueChanged(const NodeInput& input);
void InputValueHintChanged(const NodeInput& input);
void GroupAddedInputPassthrough(NodeGroup *group, const NodeInput &input);
void GroupRemovedInputPassthrough(NodeGroup *group, const NodeInput &input);
void GroupChangedOutputPassthrough(NodeGroup *group, Node *output);
protected:
void AddDefaultNode(Node* n)
{
default_nodes_.append(n);
}
virtual void childEvent(QChildEvent* event) override;
private:
QVector<Node*> node_children_;
QVector<Node*> default_nodes_;
};
}
#endif // NODEGRAPH_H
+1 -1
View File
@@ -20,7 +20,7 @@
#include "group.h"
#include "node/graph.h"
#include "node/project.h"
namespace olive {
+1 -1
View File
@@ -15,7 +15,7 @@
#include "despill.h"
#include "node/project/project.h"
#include "node/project.h"
namespace olive {
+7 -6
View File
@@ -28,7 +28,8 @@
#include "common/lerp.h"
#include "core.h"
#include "config/config.h"
#include "project/project.h"
#include "node/group/group.h"
#include "project.h"
#include "ui/colorcoding.h"
#include "ui/icons/icons.h"
#include "widget/nodeparamview/nodeparamviewundo.h"
@@ -76,9 +77,9 @@ Node::~Node()
}
}
NodeGraph *Node::parent() const
Project *Node::parent() const
{
return static_cast<NodeGraph*>(QObject::parent());
return static_cast<Project*>(QObject::parent());
}
Project *Node::project() const
@@ -982,7 +983,7 @@ QVector<Node *> Node::CopyDependencyGraph(const QVector<Node *> &nodes, MultiUnd
Node::CopyInputs(nodes.at(i), c, false);
// Add to graph
NodeGraph* graph = static_cast<NodeGraph*>(nodes.at(i)->parent());
Project* graph = nodes.at(i)->parent();
if (command) {
command->add_child(new NodeAddCommand(graph, c));
} else {
@@ -1113,7 +1114,7 @@ Node *Node::CopyNodeInGraph(Node *node, MultiUndoCommand *command)
} else {
copy = node->copy();
command->add_child(new NodeAddCommand(static_cast<NodeGraph*>(node->parent()), copy));
command->add_child(new NodeAddCommand(node->parent(), copy));
CopyInputs(node, copy, true, command);
@@ -2010,7 +2011,7 @@ void NodeRemovePositionFromContextCommand::undo()
void NodeRemovePositionFromAllContextsCommand::redo()
{
NodeGraph *graph = node_->parent();
Project *graph = node_->parent();
foreach (Node* context, graph->nodes()) {
if (context->ContextContainsNode(node_)) {
+4 -4
View File
@@ -56,7 +56,7 @@ namespace olive {
#define NODE_COPY_FUNCTION(x) \
virtual Node *copy() const override {return new x();}
class NodeGraph;
class Project;
class Folder;
/**
@@ -123,7 +123,7 @@ public:
/**
* @brief Convenience function - assumes parent is a NodeGraph
*/
NodeGraph* parent() const;
Project *parent() const;
Project* project() const;
@@ -1275,9 +1275,9 @@ signals:
void InputDataTypeChanged(const QString& id, NodeValue::Type type);
void AddedToGraph(NodeGraph* graph);
void AddedToGraph(Project* graph);
void RemovedFromGraph(NodeGraph* graph);
void RemovedFromGraph(Project* graph);
void NodeAddedToContext(Node *node);
+1 -1
View File
@@ -142,7 +142,7 @@ void TrackList::UpdateTrackIndexesFrom(int index)
}
}
NodeGraph *TrackList::GetParentGraph() const
Project *TrackList::GetParentGraph() const
{
return parent()->parent();
}
+1 -2
View File
@@ -23,7 +23,6 @@
#include <QObject>
#include "node/graph.h"
#include "node/output/track/track.h"
#include "timeline/timelinecommon.h"
@@ -59,7 +58,7 @@ public:
return track_cache_.size();
}
NodeGraph* GetParentGraph() const;
Project* GetParentGraph() const;
const QString &track_input() const;
NodeInput track_input(int element) const;
@@ -33,6 +33,8 @@
namespace olive {
#define super QObject
const QString Project::kItemMimeType = QStringLiteral("application/x-oliveprojectitemdata");
Project::Project() :
@@ -65,6 +67,104 @@ Project::Project() :
this, &Project::ColorManagerValueChanged);
}
Project::~Project()
{
Clear();
}
void Project::Clear()
{
// By deleting the last nodes first, we assume that nodes that are most important are deleted last
// (e.g. Project's ColorManager or ProjectSettingsNode.
for (auto it=node_children_.cbegin(); it!=node_children_.cend(); it++) {
(*it)->SetCachesEnabled(false);
}
while (!node_children_.isEmpty()) {
delete node_children_.last();
}
}
int Project::GetNumberOfContextsNodeIsIn(Node *node, bool except_itself) const
{
int count = 0;
foreach (Node *ctx, node_children_) {
if (ctx->ContextContainsNode(node) && (!except_itself || ctx != node)) {
count++;
}
}
return count;
}
void Project::childEvent(QChildEvent *event)
{
super::childEvent(event);
Node* node = dynamic_cast<Node*>(event->child());
if (node) {
if (event->type() == QEvent::ChildAdded) {
node_children_.append(node);
// Connect signals
connect(node, &Node::InputConnected, this, &Project::InputConnected, Qt::DirectConnection);
connect(node, &Node::InputDisconnected, this, &Project::InputDisconnected, Qt::DirectConnection);
connect(node, &Node::ValueChanged, this, &Project::ValueChanged, Qt::DirectConnection);
connect(node, &Node::InputValueHintChanged, this, &Project::InputValueHintChanged, Qt::DirectConnection);
if (NodeGroup *group = dynamic_cast<NodeGroup*>(node)) {
connect(group, &NodeGroup::InputPassthroughAdded, this, &Project::GroupAddedInputPassthrough, Qt::DirectConnection);
connect(group, &NodeGroup::InputPassthroughRemoved, this, &Project::GroupRemovedInputPassthrough, Qt::DirectConnection);
connect(group, &NodeGroup::OutputPassthroughChanged, this, &Project::GroupChangedOutputPassthrough, Qt::DirectConnection);
}
emit NodeAdded(node);
emit node->AddedToGraph(this);
// Emit input connections
for (auto it=node->input_connections().cbegin(); it!=node->input_connections().cend(); it++) {
if (nodes().contains(it->second)) {
emit InputConnected(it->second, it->first);
}
}
// Emit output connections
for (auto it=node->output_connections().cbegin(); it!=node->output_connections().cend(); it++) {
if (nodes().contains(it->second.node())) {
emit InputConnected(it->first, it->second);
}
}
} else if (event->type() == QEvent::ChildRemoved) {
node_children_.removeOne(node);
// Disconnect signals
disconnect(node, &Node::InputConnected, this, &Project::InputConnected);
disconnect(node, &Node::InputDisconnected, this, &Project::InputDisconnected);
disconnect(node, &Node::ValueChanged, this, &Project::ValueChanged);
disconnect(node, &Node::InputValueHintChanged, this, &Project::InputValueHintChanged);
if (NodeGroup *group = dynamic_cast<NodeGroup*>(node)) {
disconnect(group, &NodeGroup::InputPassthroughAdded, this, &Project::GroupAddedInputPassthrough);
disconnect(group, &NodeGroup::InputPassthroughRemoved, this, &Project::GroupRemovedInputPassthrough);
disconnect(group, &NodeGroup::OutputPassthroughChanged, this, &Project::GroupChangedOutputPassthrough);
}
emit NodeRemoved(node);
emit node->RemovedFromGraph(this);
// Remove from any contexts
foreach (Node *context, node_children_) {
context->RemoveNodeFromContext(node);
}
}
}
}
Folder *Project::root()
{
return root_;
@@ -44,12 +44,34 @@ namespace olive {
* * Project Settings
* * Window Layout
*/
class Project : public NodeGraph
class Project : public QObject
{
Q_OBJECT
public:
Project();
virtual ~Project() override;
/**
* @brief Destructively destroys all nodes in the graph
*/
void Clear();
/**
* @brief Retrieve a complete list of the nodes belonging to this graph
*/
const QVector<Node*>& nodes() const
{
return node_children_;
}
const QVector<Node*>& default_nodes() const
{
return default_nodes_;
}
int GetNumberOfContextsNodeIsIn(Node *node, bool except_itself = false) const;
Folder* root();
QString name() const;
@@ -124,6 +146,38 @@ signals:
void ModifiedChanged(bool e);
/**
* @brief Signal emitted when a Node is added to the graph
*/
void NodeAdded(Node* node);
/**
* @brief Signal emitted when a Node is removed from the graph
*/
void NodeRemoved(Node* node);
void InputConnected(Node *output, const NodeInput& input);
void InputDisconnected(Node *output, const NodeInput& input);
void ValueChanged(const NodeInput& input);
void InputValueHintChanged(const NodeInput& input);
void GroupAddedInputPassthrough(NodeGroup *group, const NodeInput &input);
void GroupRemovedInputPassthrough(NodeGroup *group, const NodeInput &input);
void GroupChangedOutputPassthrough(NodeGroup *group, Node *output);
protected:
void AddDefaultNode(Node* n)
{
default_nodes_.append(n);
}
virtual void childEvent(QChildEvent* event) override;
private:
QUuid uuid_;
@@ -143,6 +197,10 @@ private:
MainWindowLayoutInfo layout_info_;
QVector<Node*> node_children_;
QVector<Node*> default_nodes_;
private slots:
void ColorManagerValueChanged(const NodeInput& input, const TimeRange& range);
-4
View File
@@ -22,9 +22,5 @@ add_subdirectory(serializer)
set(OLIVE_SOURCES
${OLIVE_SOURCES}
node/project/project.h
node/project/project.cpp
node/project/projectviewmodel.h
node/project/projectviewmodel.cpp
PARENT_SCOPE
)
-544
View File
@@ -1,544 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "projectviewmodel.h"
#include <QDebug>
#include <QMimeData>
#include <QUrl>
#include "common/qtutils.h"
#include "core.h"
#include "widget/nodeview/nodeviewundo.h"
#include "widget/nodeparamview/nodeparamviewundo.h"
namespace olive {
ProjectViewModel::ProjectViewModel(QObject *parent) :
QAbstractItemModel(parent),
project_(nullptr)
{
}
Project *ProjectViewModel::project() const
{
return project_;
}
void ProjectViewModel::set_project(Project *p)
{
beginResetModel();
if (project_) {
DisconnectItem(project_->root());
}
project_ = p;
if (project_) {
ConnectItem(project_->root());
}
endResetModel();
}
QModelIndex ProjectViewModel::index(int row, int column, const QModelIndex &parent) const
{
// I'm actually not 100% sure what this does, but it seems logical and was in the earlier code
if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
// Get the parent object, we assume it's a folder since only folders can have children
Folder* item_parent = static_cast<Folder*>(GetItemObjectFromIndex(parent));
// Return an index to this object
return createIndex(row, column, item_parent->item_child(row));
}
QModelIndex ProjectViewModel::parent(const QModelIndex &child) const
{
// Get the Item object from the index
Node* item = GetItemObjectFromIndex(child);
// Get Item's parent object
Folder* par = item->folder();
// If the parent is the root, return an empty index
if (par == project_->root()) {
return QModelIndex();
}
// Otherwise return a true index to its parent
int parent_index = IndexOfChild(par);
// Make sure the index is valid (there's no reason it shouldn't be)
Q_ASSERT(parent_index > -1);
// Return an index to the parent
return createIndex(parent_index, 0, par);
}
int ProjectViewModel::rowCount(const QModelIndex &parent) const
{
// If there's no project, there are obviously no items to show
if (project_ == nullptr) {
return 0;
}
// If the index is the root, return the root child count
if (parent == QModelIndex()) {
return project_->root()->item_child_count();
}
// Otherwise, the index must contain a valid pointer, so we just return its child count
return static_cast<Folder*>(GetItemObjectFromIndex(parent))->item_child_count();
}
int ProjectViewModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
// Not strictly necessary, but a decent visual cue that there's no project currently active
if (project_ == nullptr) {
return 0;
}
return kColumnCount;
}
QVariant ProjectViewModel::data(const QModelIndex &index, int role) const
{
Node* internal_item = GetItemObjectFromIndex(index);
ColumnType column_type = static_cast<ColumnType>(index.column());
switch (role) {
case Qt::DisplayRole:
case kInnerTextRole:
{
// Standard text role
switch (column_type) {
case kName:
return internal_item->GetLabel();
case kDuration:
return internal_item->duration();
case kRate:
return internal_item->rate();
case kLastModified:
case kCreatedTime:
{
qint64 using_time = (column_type == kLastModified) ? internal_item->mod_time() : internal_item->creation_time();
if (using_time == 0) {
// 0 is the null value, return nothing
break;
}
QVariant ret;
if (role == kInnerTextRole) {
// Use time value directly for correct sorting
ret = using_time;
} else {
// Display role, format to a human readable string
ret = QtUtils::GetFormattedDateTime(QDateTime::fromSecsSinceEpoch(using_time));
}
return ret;
}
case kColumnCount:
break;
}
}
break;
case Qt::EditRole:
if (column_type == kName) {
return internal_item->GetLabel();
}
break;
case Qt::DecorationRole:
// If this is the first column, return the Item's icon
if (column_type == kName) {
return internal_item->icon();
}
break;
case Qt::ToolTipRole:
return internal_item->ToolTip();
}
return QVariant();
}
QVariant ProjectViewModel::headerData(int section, Qt::Orientation orientation, int role) const
{
// Check if we need text data (DisplayRole) and orientation is horizontal
// FIXME I'm not 100% sure what happens if the orientation is vertical/if that check is necessary
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
ColumnType column_type = static_cast<ColumnType>(section);
// Return the name based on the column's current type
switch (column_type) {
case kName:
return tr("Name");
case kDuration:
return tr("Duration");
case kRate:
return tr("Rate");
case kLastModified:
return tr("Modified");
case kCreatedTime:
return tr("Created");
case kColumnCount:
break;
}
}
return QAbstractItemModel::headerData(section, orientation, role);
}
bool ProjectViewModel::hasChildren(const QModelIndex &parent) const
{
// If it's a folder, we always return TRUE in order to always show the "expand triangle" icon,
// even when there are no "physical" children
Node* item = GetItemObjectFromIndex(parent);
return dynamic_cast<Folder*>(item);
}
bool ProjectViewModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
// The name is editable
if (index.isValid() && index.column() == kName && role == Qt::EditRole) {
Node* item = GetItemObjectFromIndex(index);
QString new_name = value.toString();
if (!new_name.isEmpty()) {
NodeRenameCommand* nrc = new NodeRenameCommand();
nrc->AddNode(item, value.toString());
Core::instance()->undo_stack()->push(nrc);
return true;
}
}
return false;
}
bool ProjectViewModel::canFetchMore(const QModelIndex &parent) const
{
// Use the same hack that always returns true with folders so the expand triangle is always visible
return hasChildren(parent);
}
Qt::ItemFlags ProjectViewModel::flags(const QModelIndex &index) const
{
if (!index.isValid()) {
// Allow dropping files from external sources
return Qt::ItemIsDropEnabled;
}
Qt::ItemFlags f = Qt::ItemIsDragEnabled | QAbstractItemModel::flags(index);
if (dynamic_cast<Folder*>(GetItemObjectFromIndex(index))) {
f |= Qt::ItemIsDropEnabled;
}
// If the column is the kName column, that means it's editable
if (index.column() == kName) {
f |= Qt::ItemIsEditable;
}
return f;
}
QStringList ProjectViewModel::mimeTypes() const
{
// Allow data from this model and a file list from external sources
return {Project::kItemMimeType, QStringLiteral("text/uri-list")};
}
QMimeData *ProjectViewModel::mimeData(const QModelIndexList &indexes) const
{
// Compliance with Qt standard
if (indexes.isEmpty()) {
return nullptr;
}
// Encode mime data for the rows/items that were dragged
QMimeData* data = new QMimeData();
// Use QDataStream to stream the item data into a byte array
QByteArray encoded_data;
QDataStream stream(&encoded_data, QIODevice::WriteOnly);
// The indexes list includes indexes for each column which we don't use. To make sure each row only gets sent *once*,
// we keep a list of dragged items
QVector<void*> dragged_items;
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
// Check if we've dragged this item before
if (!dragged_items.contains(index.internalPointer())) {
// If not, add it to the stream (and also keep track of it in the vector)
Node *item = static_cast<Node*>(index.internalPointer());
QVector<Track::Reference> streams;
if (ViewerOutput* footage = dynamic_cast<ViewerOutput*>(item)) {
streams = footage->GetEnabledStreamsAsReferences();
}
stream << streams << reinterpret_cast<quintptr>(item);
dragged_items.append(item);
}
}
}
// Set byte array as the mime data and return the mime data
data->setData(Project::kItemMimeType, encoded_data);
return data;
}
bool ProjectViewModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &drop)
{
// Default recommended checks from https://doc.qt.io/qt-5/model-view-programming.html#using-drag-and-drop-with-item-views
if (!canDropMimeData(data, action, row, column, drop)) {
return false;
}
if (action == Qt::IgnoreAction) {
return true;
}
// Probe mime data for its format
QStringList mime_formats = data->formats();
if (mime_formats.contains(Project::kItemMimeType)) {
// Data is drag/drop data from this model
QByteArray model_data = data->data(Project::kItemMimeType);
// Use QDataStream to deserialize the data
QDataStream stream(&model_data, QIODevice::ReadOnly);
// Get the Item object that the items were dropped on
Folder* drop_location = dynamic_cast<Folder*>(GetItemObjectFromIndex(drop));
// If this is not a folder, we cannot drop these items here
if (!drop_location) {
return false;
}
// Variables to deserialize into
quintptr item_ptr;
QList<Track::Reference> streams;
// Loop through all data
MultiUndoCommand* move_command = new MultiUndoCommand();
move_command->set_name(tr("Move Items"));
while (!stream.atEnd()) {
stream >> streams >> item_ptr;
Node* item = reinterpret_cast<Node*>(item_ptr);
// Check if Item is already the drop location or if its parent is the drop location, in which case this is a
// no-op
if (item != drop_location && item->folder() != drop_location
&& (!dynamic_cast<Folder*>(item) || !ItemIsParentOfChild(static_cast<Folder*>(item), drop_location))) {
move_command->add_child(new NodeEdgeRemoveCommand(item, NodeInput(item->folder(), Folder::kChildInput, item->folder()->index_of_child_in_array(item))));
move_command->add_child(new FolderAddChild(drop_location, item));
}
}
Core::instance()->undo_stack()->pushIfHasChildren(move_command);
return true;
} else if (mime_formats.contains(QStringLiteral("text/uri-list"))) {
// We received a list of files
QByteArray file_data = data->data(QStringLiteral("text/uri-list"));
// Use text stream to parse (just an easy way of sifting through line breaks
QTextStream stream(&file_data);
// Convert QByteArray to QStringList (which Core takes for importing)
QStringList urls;
while (!stream.atEnd()) {
QUrl url = stream.readLine();
if (!url.isEmpty()) {
urls.append(url.toLocalFile());
}
}
// Get folder dropped onto
Node* drop_item = GetItemObjectFromIndex(drop);
// If we didn't drop onto an item, find the nearest parent folder (should eventually terminate at root either way)
if (!dynamic_cast<Folder*>(drop_item)) {
drop_item = drop_item->folder();
if (!drop_item) {
// Failed to find folder to place this in
return false;
}
}
// Trigger an import
Core::instance()->ImportFiles(urls, static_cast<Folder*>(drop_item));
return true;
}
return false;
}
int ProjectViewModel::IndexOfChild(Node *item) const
{
// Find parent's index within its own parent
Folder* parent = item->folder();
if (parent) {
return parent->index_of_child(item);
}
return -1;
}
Node *ProjectViewModel::GetItemObjectFromIndex(const QModelIndex &index) const
{
if (index.isValid()) {
return static_cast<Node*>(index.internalPointer());
}
return project_ ? project_->root() : nullptr;
}
bool ProjectViewModel::ItemIsParentOfChild(Folder *parent, Node *child) const
{
// Loop through parent hierarchy checking if `parent` is one of its parents
do {
child = child->folder();
if (parent == child) {
return true;
}
} while (child != nullptr);
return false;
}
void ProjectViewModel::ConnectItem(Node *n)
{
connect(n, &Node::LabelChanged, this, &ProjectViewModel::ItemRenamed);
Folder* f = dynamic_cast<Folder*>(n);
if (f) {
connect(f, &Folder::BeginInsertItem, this, &ProjectViewModel::FolderBeginInsertItem);
connect(f, &Folder::EndInsertItem, this, &ProjectViewModel::FolderEndInsertItem);
connect(f, &Folder::BeginRemoveItem, this, &ProjectViewModel::FolderBeginRemoveItem);
connect(f, &Folder::EndRemoveItem, this, &ProjectViewModel::FolderEndRemoveItem);
foreach (Node* c, f->children()) {
ConnectItem(c);
}
}
}
void ProjectViewModel::DisconnectItem(Node *n)
{
disconnect(n, &Node::LabelChanged, this, &ProjectViewModel::ItemRenamed);
Folder* f = dynamic_cast<Folder*>(n);
if (f) {
disconnect(f, &Folder::BeginInsertItem, this, &ProjectViewModel::FolderBeginInsertItem);
disconnect(f, &Folder::EndInsertItem, this, &ProjectViewModel::FolderEndInsertItem);
disconnect(f, &Folder::BeginRemoveItem, this, &ProjectViewModel::FolderBeginRemoveItem);
disconnect(f, &Folder::EndRemoveItem, this, &ProjectViewModel::FolderEndRemoveItem);
foreach (Node* c, f->children()) {
DisconnectItem(c);
}
}
}
void ProjectViewModel::FolderBeginInsertItem(Node *n, int insert_index)
{
Folder* folder = static_cast<Folder*>(sender());
ConnectItem(n);
QModelIndex index;
if (folder != project_->root()) {
index = CreateIndexFromItem(folder);
}
beginInsertRows(index, insert_index, insert_index);
}
void ProjectViewModel::FolderEndInsertItem()
{
endInsertRows();
}
void ProjectViewModel::FolderBeginRemoveItem(Node *n, int child_index)
{
Folder* folder = static_cast<Folder*>(sender());
DisconnectItem(n);
QModelIndex index;
if (folder != project_->root()) {
index = CreateIndexFromItem(folder);
}
beginRemoveRows(index, child_index, child_index);
}
void ProjectViewModel::FolderEndRemoveItem()
{
endRemoveRows();
}
void ProjectViewModel::ItemRenamed()
{
Node* item = static_cast<Node*>(sender());
QModelIndex index = CreateIndexFromItem(item);
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
}
QModelIndex ProjectViewModel::CreateIndexFromItem(Node *item, int column)
{
return createIndex(IndexOfChild(item), column, item);
}
}
-166
View File
@@ -1,166 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef VIEWMODEL_H
#define VIEWMODEL_H
#include <QAbstractItemModel>
#include "project.h"
#include "undo/undocommand.h"
#include "node/block/block.h"
namespace olive {
/**
* @brief An adapter that interprets the data in a Project into a Qt item model for usage in ViewModel Views.
*
* Assuming a Project is currently "open" (i.e. the Project is connected to a ProjectExplorer/ProjectPanel through
* a ProjectViewModel), it may be better to make modifications (e.g. additions/removals/renames) through the
* ProjectViewModel so that the views can be efficiently and correctly updated. ProjectViewModel contains several
* "wrapper" functions for Project and Item functions that also signal any connected views to update accordingly.
*/
class ProjectViewModel : public QAbstractItemModel
{
Q_OBJECT
public:
enum ColumnType {
/// Media name
kName,
/// Media duration
kDuration,
/// Media rate (frame rate for video, sample rate for audio)
kRate,
/// Last modified time (for footage/files)
kLastModified,
/// Creation time (for footage/files)
kCreatedTime,
/// Count
kColumnCount
};
static const int kInnerTextRole = Qt::UserRole + 1;
/**
* @brief ProjectViewModel Constructor
*
* @param parent
* Parent object for memory handling
*/
ProjectViewModel(QObject* parent);
/**
* @brief Get currently active project
*
* @return
*
* Currently active project or nullptr if there is none
*/
Project* project() const;
/**
* @brief Set the project to adapt
*
* Any views attached to this model will get updated by this function.
*
* @param p
*
* Project to adapt, can be set to nullptr to "close" the project (will show an empty model that cannot be modified)
*/
void set_project(Project* p);
/** Compulsory Qt QAbstractItemModel overrides */
virtual QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex &child) const override;
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override;
virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
/** Optional Qt QAbstractItemModel overrides */
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
virtual bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
virtual bool canFetchMore(const QModelIndex &parent) const override;
/** Drag and drop support */
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
virtual QStringList mimeTypes() const override;
virtual QMimeData * mimeData(const QModelIndexList &indexes) const override;
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
/**
* @brief Convenience function for creating QModelIndexes from an Item object
*/
QModelIndex CreateIndexFromItem(Node *item, int column = 0);
private:
/**
* @brief Retrieve the index of `item` in its parent
*
* This function will return the index of a specified item in its parent according to whichever sorting algorithm
* is currently active.
*
* @return
*
* Index of the specified item, or -1 if the item is root (in which case it has no parent).
*/
int IndexOfChild(Node* item) const;
/**
* @brief Retrieves the Item object from a given index
*
* A convenience function for retrieving Item objects. If the index is not valid, this returns the root Item.
*/
Node* GetItemObjectFromIndex(const QModelIndex& index) const;
/**
* @brief Check if an Item is a parent of a Child
*
* Checks entire "parent hierarchy" of `child` to see if `parent` is one of its parents.
*/
bool ItemIsParentOfChild(Folder *parent, Node* child) const;
void ConnectItem(Node* n);
void DisconnectItem(Node *n);
Project* project_;
private slots:
void FolderBeginInsertItem(Node *n, int insert_index);
void FolderEndInsertItem();
void FolderBeginRemoveItem(Node* n, int child_index);
void FolderEndRemoveItem();
void ItemRenamed();
};
}
#endif // VIEWMODEL_H
@@ -26,6 +26,7 @@
#include "common/xmlutils.h"
#include "core.h"
#include "node/group/group.h"
#include "serializer190219.h"
#include "serializer210528.h"
#include "serializer210907.h"
+1 -1
View File
@@ -24,7 +24,7 @@
#include <vector>
#include "common/define.h"
#include "node/project/project.h"
#include "node/project.h"
#include "typeserializer.h"
namespace olive {
@@ -22,6 +22,7 @@
#include "config/config.h"
#include "node/factory.h"
#include "node/group/group.h"
namespace olive {
@@ -22,6 +22,7 @@
#include "config/config.h"
#include "node/factory.h"
#include "node/group/group.h"
namespace olive {
@@ -22,6 +22,7 @@
#include "config/config.h"
#include "node/factory.h"
#include "node/group/group.h"
namespace olive {
@@ -22,6 +22,7 @@
#include "config/config.h"
#include "node/factory.h"
#include "node/group/group.h"
namespace olive {