more work on project management

This commit is contained in:
itsmattkc
2019-07-06 09:46:55 -05:00
parent bc631c495d
commit a6971b3fde
15 changed files with 290 additions and 114 deletions
-18
View File
@@ -23,24 +23,6 @@
Project::Project()
{
name_ = tr("(untitled)");
// FIXME: Test code
Folder* f1 = new Folder();
f1->set_name("F1");
f1->set_parent(&root_);
Folder* f2 = new Folder();
f2->set_name("F2");
f2->set_parent(f1);
Folder* f3 = new Folder();
f3->set_name("F3");
f3->set_parent(&root_);
Folder* f4 = new Folder();
f4->set_name("F4");
f4->set_parent(f1);
Folder* f5 = new Folder();
f5->set_name("F5");
f5->set_parent(f3);
// End Test code
}
Folder *Project::root()
+55 -7
View File
@@ -370,7 +370,7 @@ bool ProjectViewModel::dropMimeData(const QMimeData *data, Qt::DropAction action
}
// Trigger an import
olive::core.ImportFiles(urls, static_cast<Folder*>(drop_item));
olive::core.ImportFiles(urls, this, static_cast<Folder*>(drop_item));
}
return false;
@@ -391,6 +391,32 @@ void ProjectViewModel::AddChild(Item *parent, Item *child)
endInsertRows();
}
void ProjectViewModel::RemoveChild(Item *parent, Item *child)
{
QModelIndex parent_index;
if (parent != project_->root()) {
parent_index = CreateIndexFromItem(parent);
}
int child_row = IndexOfChild(child);
beginRemoveRows(parent_index, child_row, child_row);
parent->remove_child(child);
endRemoveRows();
}
void ProjectViewModel::RenameChild(Item *item, const QString &name)
{
item->set_name(name);
QModelIndex index = CreateIndexFromItem(item, columns_.indexOf(kName));
emit dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
}
int ProjectViewModel::IndexOfChild(Item *item) const
{
// Find parent's index within its own parent
@@ -498,19 +524,41 @@ ProjectViewModel::RenameItemCommand::RenameItemCommand(ProjectViewModel* model,
void ProjectViewModel::RenameItemCommand::redo()
{
set_name(new_name_);
model_->RenameChild(item_, new_name_);
}
void ProjectViewModel::RenameItemCommand::undo()
{
set_name(old_name_);
model_->RenameChild(item_, old_name_);
}
void ProjectViewModel::RenameItemCommand::set_name(const QString &n)
ProjectViewModel::AddItemCommand::AddItemCommand(ProjectViewModel* model, Item* folder, Item* child, QUndoCommand* parent) :
QUndoCommand(parent),
model_(model),
parent_(folder),
child_(child),
done_(false)
{
item_->set_name(n);
}
QModelIndex index = model_->CreateIndexFromItem(item_, model_->columns_.indexOf(kName));
ProjectViewModel::AddItemCommand::~AddItemCommand()
{
// FIXME Use smart pointers. I hate having to do this.
if (!done_) {
delete child_;
}
}
emit model_->dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
void ProjectViewModel::AddItemCommand::redo()
{
model_->AddChild(parent_, child_);
done_ = true;
}
void ProjectViewModel::AddItemCommand::undo()
{
model_->RemoveChild(parent_, child_);
done_ = false;
}
+64 -44
View File
@@ -94,6 +94,70 @@ public:
/** Other model functions */
void AddChild(Item* parent, Item* child);
void RemoveChild(Item* parent, Item* child);
void RenameChild(Item* item, const QString& name);
/**
* @brief Convenience function for creating QModelIndexes from an Item object
*/
QModelIndex CreateIndexFromItem(Item* item, int column = 0);
/**
* @brief A QUndoCommand for moving an item from one folder to another folder
*/
class MoveItemCommand : public QUndoCommand {
public:
MoveItemCommand(ProjectViewModel* model, Item* item, Folder* destination, QUndoCommand* parent = nullptr);
virtual void redo() override;
virtual void undo() override;
private:
ProjectViewModel* model_;
Item* item_;
Folder* source_;
Folder* destination_;
};
/**
* @brief A QUndoCommand for renaming an item
*/
class RenameItemCommand : public QUndoCommand {
public:
RenameItemCommand(ProjectViewModel* model, Item* item, const QString& name, QUndoCommand* parent = nullptr);
virtual void redo() override;
virtual void undo() override;
private:
ProjectViewModel* model_;
Item* item_;
QString old_name_;
QString new_name_;
};
/**
* @brief A QUndoCommand for adding an item
*/
class AddItemCommand : public QUndoCommand {
public:
AddItemCommand(ProjectViewModel* model, Item* folder, Item* child, QUndoCommand* parent = nullptr);
virtual ~AddItemCommand() override;
virtual void redo() override;
virtual void undo() override;
private:
ProjectViewModel* model_;
Item* parent_;
Item* child_;
bool done_;
};
private:
/**
* @brief Retrieve the index of `item` in its parent
@@ -144,53 +208,9 @@ private:
*/
void MoveItemInternal(Item* item, Item* destination);
/**
* @brief Convenience function for creating QModelIndexes from an Item object
*/
QModelIndex CreateIndexFromItem(Item* item, int column = 0);
Project* project_;
QVector<ColumnType> columns_;
/**
* @brief A QUndoCommand for moving an item from one folder to another folder
*/
class MoveItemCommand : public QUndoCommand {
public:
MoveItemCommand(ProjectViewModel* model, Item* item, Folder* destination, QUndoCommand* parent = nullptr);
virtual void redo() override;
virtual void undo() override;
private:
ProjectViewModel* model_;
Item* item_;
Folder* source_;
Folder* destination_;
};
/**
* @brief A QUndoCommand for renaming an item
*/
class RenameItemCommand : public QUndoCommand {
public:
RenameItemCommand(ProjectViewModel* model, Item* item, const QString& name, QUndoCommand* parent = nullptr);
virtual void redo() override;
virtual void undo() override;
private:
void set_name(const QString& n);
ProjectViewModel* model_;
Item* item_;
QString old_name_;
QString new_name_;
};
};
#endif // VIEWMODEL_H