diff --git a/app/panel/project/project.cpp b/app/panel/project/project.cpp index 130acdcbc..db24eed39 100644 --- a/app/panel/project/project.cpp +++ b/app/panel/project/project.cpp @@ -133,6 +133,11 @@ void ProjectPanel::Overwrite() } } +void ProjectPanel::DeleteSelected() +{ + explorer_->DeleteSelected(); +} + void ProjectPanel::Edit(Item* item) { explorer_->Edit(item); diff --git a/app/panel/project/project.h b/app/panel/project/project.h index 2ec79f07c..23da1aea3 100644 --- a/app/panel/project/project.h +++ b/app/panel/project/project.h @@ -52,6 +52,8 @@ public: virtual void Insert() override; virtual void Overwrite() override; + virtual void DeleteSelected() override; + public slots: void Edit(Item *item); diff --git a/app/project/projectviewmodel.cpp b/app/project/projectviewmodel.cpp index fa92470cf..cd5902f8a 100644 --- a/app/project/projectviewmodel.cpp +++ b/app/project/projectviewmodel.cpp @@ -475,7 +475,7 @@ void ProjectViewModel::MoveItemInternal(Item *item, Item *destination) beginMoveRows(item_index.parent(), item_index.row(), item_index.row(), destination_index, destination->child_count()); - ItemPtr item_ptr = item->parent()->shared_ptr_from_raw(item); + ItemPtr item_ptr = item->get_shared_ptr(); destination->add_child(item_ptr); @@ -541,10 +541,6 @@ ProjectViewModel::AddItemCommand::AddItemCommand(ProjectViewModel* model, Item* { } -ProjectViewModel::AddItemCommand::~AddItemCommand() -{ -} - void ProjectViewModel::AddItemCommand::redo_internal() { model_->AddChild(parent_, child_); @@ -558,3 +554,21 @@ void ProjectViewModel::AddItemCommand::undo_internal() done_ = false; } + +ProjectViewModel::RemoveItemCommand::RemoveItemCommand(ProjectViewModel *model, ItemPtr item, QUndoCommand *parent) : + UndoCommand(parent), + model_(model), + item_(item) +{ +} + +void ProjectViewModel::RemoveItemCommand::redo_internal() +{ + parent_ = item_->parent(); + model_->RemoveChild(parent_, item_.get()); +} + +void ProjectViewModel::RemoveItemCommand::undo_internal() +{ + model_->AddChild(parent_, item_); +} diff --git a/app/project/projectviewmodel.h b/app/project/projectviewmodel.h index 50057ecdb..124fa30fc 100644 --- a/app/project/projectviewmodel.h +++ b/app/project/projectviewmodel.h @@ -151,8 +151,6 @@ public: public: AddItemCommand(ProjectViewModel* model, Item* folder, ItemPtr child, QUndoCommand* parent = nullptr); - virtual ~AddItemCommand() override; - protected: virtual void redo_internal() override; @@ -164,6 +162,28 @@ public: ItemPtr child_; bool done_; }; + + /** + * @brief An undo command for removing an item + */ + class RemoveItemCommand : public UndoCommand { + public: + RemoveItemCommand(ProjectViewModel* model, ItemPtr item, QUndoCommand* parent = nullptr); + + protected: + virtual void redo_internal() override; + + virtual void undo_internal() override; + + private: + ProjectViewModel* model_; + + ItemPtr item_; + + Item* parent_; + + }; + private: /** * @brief Retrieve the index of `item` in its parent diff --git a/app/widget/projectexplorer/projectexplorer.cpp b/app/widget/projectexplorer/projectexplorer.cpp index 1f7468a10..b2a9694c8 100644 --- a/app/widget/projectexplorer/projectexplorer.cpp +++ b/app/widget/projectexplorer/projectexplorer.cpp @@ -406,3 +406,22 @@ void ProjectExplorer::DeselectAll() { CurrentView()->selectionModel()->clearSelection(); } + +void ProjectExplorer::DeleteSelected() +{ + QList selected = SelectedItems(); + + if (selected.isEmpty()) { + return; + } + + QUndoCommand* command = new QUndoCommand(); + + foreach (Item* item, selected) { + ItemPtr item_ptr = item->get_shared_ptr(); + + new ProjectViewModel::RemoveItemCommand(&model_, item_ptr, command); + } + + Core::instance()->undo_stack()->pushIfHasChildren(command); +} diff --git a/app/widget/projectexplorer/projectexplorer.h b/app/widget/projectexplorer/projectexplorer.h index 84caca476..9285e76ab 100644 --- a/app/widget/projectexplorer/projectexplorer.h +++ b/app/widget/projectexplorer/projectexplorer.h @@ -77,6 +77,8 @@ public: void DeselectAll(); + void DeleteSelected(); + public slots: void set_view_type(ProjectToolbar::ViewType type);