changed items to shared ptrs

This commit is contained in:
itsmattkc
2019-07-06 12:50:00 -05:00
parent a6971b3fde
commit b62ec30015
12 changed files with 98 additions and 42 deletions
+2
View File
@@ -231,4 +231,6 @@ private:
};
using FootagePtr = std::shared_ptr<Footage>;
#endif // FOOTAGE_H
+22 -17
View File
@@ -27,20 +27,16 @@ Item::Item() :
Item::~Item()
{
// Delete all children
for (int i=0;i<children_.size();i++) {
delete children_.at(i);
}
}
void Item::add_child(Item* c)
void Item::add_child(ItemPtr c)
{
if (c->parent_ == this) {
return;
}
if (c->parent_ != nullptr) {
c->parent_->remove_child(c);
c->parent_->remove_child(c.get());
}
children_.append(c);
@@ -53,7 +49,14 @@ void Item::remove_child(Item *c)
return;
}
children_.removeAll(c);
// Remove all instances of this child in the list
for (int i=0;i<children_.size();i++) {
if (children_.at(i).get() == c) {
children_.removeAt(i);
i--;
}
}
c->parent_ = nullptr;
}
@@ -64,7 +67,18 @@ int Item::child_count()
Item *Item::child(int i)
{
return children_.at(i);
return children_.at(i).get();
}
ItemPtr Item::shared_ptr_from_raw(Item *item)
{
for (int i=0;i<children_.size();i++) {
if (children_.at(i).get() == item) {
return children_.at(i);
}
}
return nullptr;
}
const QString &Item::name() const
@@ -101,12 +115,3 @@ Item *Item::parent() const
{
return parent_;
}
void Item::set_parent(Item *p)
{
if (parent_ != nullptr) {
parent_->remove_child(this);
}
p->add_child(this);
}
+9 -4
View File
@@ -21,10 +21,14 @@
#ifndef ITEM_H
#define ITEM_H
#include <memory>
#include <QIcon>
#include <QList>
#include <QString>
class Item;
using ItemPtr = std::shared_ptr<Item>;
class Item
{
public:
@@ -40,7 +44,7 @@ public:
Item();
/**
* @brief Item destructor, deletes all children
* @brief Required virtual Item destructor
*/
virtual ~Item();
@@ -66,11 +70,13 @@ public:
virtual Type type() const = 0;
void add_child(Item *c);
void add_child(ItemPtr c);
void remove_child(Item* c);
int child_count();
Item* child(int i);
ItemPtr shared_ptr_from_raw(Item* item);
const QString& name() const;
void set_name(const QString& n);
@@ -81,10 +87,9 @@ public:
void set_icon(const QIcon& icon);
Item *parent() const;
void set_parent(Item *p);
private:
QList<Item*> children_;
QList<ItemPtr> children_;
Item* parent_;
+6 -8
View File
@@ -376,7 +376,7 @@ bool ProjectViewModel::dropMimeData(const QMimeData *data, Qt::DropAction action
return false;
}
void ProjectViewModel::AddChild(Item *parent, Item *child)
void ProjectViewModel::AddChild(Item *parent, ItemPtr child)
{
QModelIndex parent_index;
@@ -477,7 +477,9 @@ void ProjectViewModel::MoveItemInternal(Item *item, Item *destination)
beginMoveRows(item_index.parent(), item_index.row(), item_index.row(), destination_index, destination->child_count());
item->set_parent(destination);
ItemPtr item_ptr = item->parent()->shared_ptr_from_raw(item);
destination->add_child(item_ptr);
endMoveRows();
}
@@ -532,7 +534,7 @@ void ProjectViewModel::RenameItemCommand::undo()
model_->RenameChild(item_, old_name_);
}
ProjectViewModel::AddItemCommand::AddItemCommand(ProjectViewModel* model, Item* folder, Item* child, QUndoCommand* parent) :
ProjectViewModel::AddItemCommand::AddItemCommand(ProjectViewModel* model, Item* folder, ItemPtr child, QUndoCommand* parent) :
QUndoCommand(parent),
model_(model),
parent_(folder),
@@ -543,10 +545,6 @@ ProjectViewModel::AddItemCommand::AddItemCommand(ProjectViewModel* model, Item*
ProjectViewModel::AddItemCommand::~AddItemCommand()
{
// FIXME Use smart pointers. I hate having to do this.
if (!done_) {
delete child_;
}
}
void ProjectViewModel::AddItemCommand::redo()
@@ -558,7 +556,7 @@ void ProjectViewModel::AddItemCommand::redo()
void ProjectViewModel::AddItemCommand::undo()
{
model_->RemoveChild(parent_, child_);
model_->RemoveChild(parent_, child_.get());
done_ = false;
}
+3 -3
View File
@@ -93,7 +93,7 @@ public:
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
/** Other model functions */
void AddChild(Item* parent, Item* child);
void AddChild(Item* parent, ItemPtr child);
void RemoveChild(Item* parent, Item* child);
void RenameChild(Item* item, const QString& name);
@@ -144,7 +144,7 @@ public:
*/
class AddItemCommand : public QUndoCommand {
public:
AddItemCommand(ProjectViewModel* model, Item* folder, Item* child, QUndoCommand* parent = nullptr);
AddItemCommand(ProjectViewModel* model, Item* folder, ItemPtr child, QUndoCommand* parent = nullptr);
virtual ~AddItemCommand() override;
@@ -155,7 +155,7 @@ public:
private:
ProjectViewModel* model_;
Item* parent_;
Item* child_;
ItemPtr child_;
bool done_;
};
private: