Commit fca8319c authored by Nicolas Carion's avatar Nicolas Carion

Rework folder renaming

parent 9cdc8fbb
......@@ -1477,18 +1477,6 @@ void Bin::doAddFolder(const QString &id, const QString &name, const QString &par
emit storeFolder(id, parentId, QString(), name);
}
void Bin::renameFolder(const QString &id, const QString &name)
{
std::shared_ptr<ProjectFolder> folder = m_itemModel->getFolderByBinId(id);
if ((folder == nullptr) || (folder->parent() == nullptr)) {
qCDebug(KDENLIVE_LOG) << " / / ERROR IN PARENT FOLDER";
return;
}
folder->setName(name);
emit itemUpdated(folder);
emit storeFolder(id, folder->parent()->clipId(), QString(), name);
}
void Bin::slotLoadFolders(const QMap<QString, QString> &foldersData)
{
// Folder parent is saved in folderId, separated by a dot. for example "1.3" means parent folder id is "1" and new folder id is "3".
......@@ -2855,11 +2843,6 @@ void Bin::slotItemEdited(const QModelIndex &ix, const QModelIndex &, const QVect
}
}
void Bin::renameFolderCommand(const QString &id, const QString &newName, const QString &oldName)
{
auto *command = new RenameBinFolderCommand(this, id, newName, oldName);
m_doc->commandStack()->push(command);
}
void Bin::renameSubClipCommand(const QString &id, const QString &newName, const QString &oldName, int in, int out)
{
......
......@@ -261,10 +261,6 @@ public:
void focusBinView() const;
/** @brief Get a string list of all clip ids that are inside a folder defined by id. */
QStringList getBinFolderClipIds(const QString &id) const;
/** @brief Build a rename folder command. */
void renameFolderCommand(const QString &id, const QString &newName, const QString &oldName);
/** @brief Rename a folder and store new name in MLT. */
void renameFolder(const QString &id, const QString &name);
/** @brief Build a rename subclip command. */
void renameSubClipCommand(const QString &id, const QString &newName, const QString &oldName, int in, int out);
/** @brief Rename a clip zone (subclip). */
......
......@@ -96,25 +96,6 @@ void MoveBinFolderCommand::redo()
m_bin->doMoveFolder(m_clipId, m_newParentId);
}
RenameBinFolderCommand::RenameBinFolderCommand(Bin *bin, const QString &folderId, const QString &newName, const QString &oldName, QUndoCommand *parent)
: QUndoCommand(parent)
, m_bin(bin)
, m_clipId(folderId)
, m_oldName(oldName)
, m_newName(newName)
{
setText(i18n("Rename Folder"));
}
// virtual
void RenameBinFolderCommand::undo()
{
m_bin->renameFolder(m_clipId, m_oldName);
}
// virtual
void RenameBinFolderCommand::redo()
{
m_bin->renameFolder(m_clipId, m_newName);
}
RenameBinSubClipCommand::RenameBinSubClipCommand(Bin *bin, const QString &clipId, const QString &newName, const QString &oldName, int in, int out,
QUndoCommand *parent)
......
......@@ -70,20 +70,6 @@ private:
QString m_newParentId;
};
class RenameBinFolderCommand : public QUndoCommand
{
public:
explicit RenameBinFolderCommand(Bin *bin, const QString &folderId, const QString &newName, const QString &oldName, QUndoCommand *parent = nullptr);
void undo() override;
void redo() override;
private:
Bin *m_bin;
QString m_clipId;
QString m_oldName;
QString m_newName;
};
class RenameBinSubClipCommand : public QUndoCommand
{
public:
......
......@@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "projectfolder.h"
#include "bin.h"
#include "projectclip.h"
#include "core.h"
#include "projectitemmodel.h"
#include "utils/KoIconUtils.h"
......@@ -147,6 +148,11 @@ bool ProjectFolder::rename(const QString &name, int column)
return false;
}
// Rename folder
if (auto ptr = m_model.lock()) std::static_pointer_cast<ProjectItemModel>(ptr)->bin()->renameFolderCommand(m_binId, name, m_name);
return true;
if (auto ptr = m_model.lock()) {
auto self = std::static_pointer_cast<ProjectFolder>(shared_from_this());
return std::static_pointer_cast<ProjectItemModel>(ptr)->requestRenameFolder(self, name);
}
qDebug() << "ERROR: Impossible to rename folder because model is not available";
Q_ASSERT(false);
return false;
}
......@@ -409,31 +409,23 @@ bool ProjectItemModel::requestBinClipDeletion(std::shared_ptr<AbstractProjectIte
Fun reverse = [this, clip, parentId]() {
/* To undo deletion, we reregister the clip */
std::shared_ptr<TreeItem> parent;
qDebug() << "reversing deletion. Parentid =" << parentId;
if (parentId != -1) {
qDebug() << "parent found";
parent = getItemById(parentId);
}
clip->changeParent(parent);
return true;
};
bool res = operation();
qDebug() << "operation succes" << res;
if (res) {
UPDATE_UNDO_REDO(operation, reverse, undo, redo);
res = clip->selfSoftDelete(undo, redo);
qDebug() << "soltDelete succes" << res;
}
if (res) {
}
qDebug() << "Deletion success" << res;
return res;
}
void ProjectItemModel::registerItem(const std::shared_ptr<TreeItem> &item)
{
auto clip = std::static_pointer_cast<AbstractProjectItem>(item);
qDebug() << "registering" <<clip->clipId();
manageBinClipInsertion(clip);
AbstractTreeModel::registerItem(item);
}
......@@ -500,10 +492,10 @@ int ProjectItemModel::getFreeClipId()
{
return m_clipCounter++;
}
bool ProjectItemModel::requestAddFolder(QString &id, const QString &name, const QString &parentId, Fun &undo, Fun &redo)
{
QWriteLocker locker(&m_lock);
qDebug() << "request Add. parent="<<parentId;
std::shared_ptr<ProjectFolder> parentFolder = getFolderByBinId(parentId);
if (!parentFolder) {
qCDebug(KDENLIVE_LOG) << " / / ERROR IN PARENT FOLDER";
......@@ -513,18 +505,14 @@ bool ProjectItemModel::requestAddFolder(QString &id, const QString &name, const
id = QString::number(getFreeFolderId());
}
std::shared_ptr<ProjectFolder> new_folder = ProjectFolder::construct(id, name, std::static_pointer_cast<ProjectItemModel>(shared_from_this()), parentFolder);
qDebug() << "newfolder created:"<<new_folder->clipId()<<(void*)new_folder.get();
parentFolder->appendChild(new_folder);
int folderId = new_folder->getId();
qDebug() << "creating op. parent="<<parentId<<(void*)parentFolder.get();
Fun operation = [this, new_folder, parentId]() {
/* Insertion is simply setting the parent of the folder.*/
qDebug() << "executing op. parent="<<parentId;
std::shared_ptr<ProjectFolder> parent = getFolderByBinId(parentId);
if (!parent) {
return false;
}
qDebug() << "executing op2. parent="<<parent->clipId()<<(void*)parent.get();
new_folder->changeParent(parent);
return true;
};
......@@ -538,16 +526,49 @@ bool ProjectItemModel::requestAddFolder(QString &id, const QString &name, const
parent->removeChild(folder);
return true;
};
qDebug()<<"Folder list";
for (const auto &clip : m_allItems) {
auto c = std::static_pointer_cast<AbstractProjectItem>(clip.second.lock());
if (c->itemType() == AbstractProjectItem::FolderItem) {
qDebug() <<"folder"<<c->clipId()<<(void*)c.get();
}
}
bool res = operation();
if (res) {
UPDATE_UNDO_REDO(operation, reverse, undo, redo);
}
return res;
}
Fun ProjectItemModel::requestRenameFolder_lambda(std::shared_ptr<AbstractProjectItem> folder, const QString &newName)
{
int id = folder->getId();
return [this, id, newName](){
auto currentFolder = std::static_pointer_cast<AbstractProjectItem>(m_allItems[id].lock());
if (!currentFolder) {
return false;
}
auto parent = currentFolder->parent();
parent->removeChild(currentFolder);
currentFolder->setName(newName);
parent->appendChild(currentFolder);
return true;
};
}
bool ProjectItemModel::requestRenameFolder(std::shared_ptr<AbstractProjectItem> folder, const QString &name, Fun &undo, Fun &redo)
{
QWriteLocker locker(&m_lock);
QString oldName = folder->name();
auto operation = requestRenameFolder_lambda(folder, name);
if (operation()) {
auto reverse = requestRenameFolder_lambda(folder, oldName);
UPDATE_UNDO_REDO(operation, reverse, undo, redo);
return true;
}
return false;
}
bool ProjectItemModel::requestRenameFolder(std::shared_ptr<AbstractProjectItem> folder, const QString &name)
{
Fun undo = []() { return true; };
Fun redo = []() { return true; };
bool res = requestRenameFolder(folder, name, undo, redo);
if (res) {
pCore->pushUndo(undo, redo, i18n("Rename Folder"));
}
return res;
}
......@@ -104,7 +104,7 @@ public:
Qt::DropActions supportedDropActions() const override;
/* @brief Request deletion of a bin clip from the project bin
@param binId : id of the bin clip to deleted
@param clip : pointer to the clip to delete
@param undo,redo: lambdas that are updated to accumulate operation.
*/
bool requestBinClipDeletion(std::shared_ptr<AbstractProjectItem> clip, Fun &undo, Fun &redo);
......@@ -116,6 +116,16 @@ public:
@param undo,redo: lambdas that are updated to accumulate operation.
*/
bool requestAddFolder(QString &id, const QString &name, const QString &parentId, Fun &undo, Fun &redo);
/* @brief Request that a folder's name is changed
@param clip : pointer to the folder to rename
@param name: new name of the folder
@param undo,redo: lambdas that are updated to accumulate operation.
*/
bool requestRenameFolder(std::shared_ptr<AbstractProjectItem> folder, const QString &name, Fun &undo, Fun &redo);
/* Same functions but pushes the undo object directly */
bool requestRenameFolder(std::shared_ptr<AbstractProjectItem> folder, const QString &name);
/* @brief Manage insertion in the tree hierarchy.
Note that the element has normally already been registered through registerItem,
this function is called when its parent is defined.
......@@ -150,6 +160,10 @@ protected:
/* @brief Retrieves the next id available for attribution to a clip */
int getFreeClipId();
/* @brief Helper function to generate a lambda that rename a folder */
Fun requestRenameFolder_lambda(std::shared_ptr<AbstractProjectItem> folder, const QString &newName);
public slots:
/** @brief An item in the list was modified, notify */
void onItemUpdated(std::shared_ptr<AbstractProjectItem> item);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment