added undoable renaming of project items

This commit is contained in:
itsmattkc
2019-07-04 18:15:00 -05:00
parent ac9f673665
commit d8b3eb74a9
2 changed files with 83 additions and 6 deletions
+61 -5
View File
@@ -118,11 +118,12 @@ QVariant ProjectViewModel::data(const QModelIndex &index, int role) const
{ {
Item* internal_item = GetItemObjectFromIndex(index); Item* internal_item = GetItemObjectFromIndex(index);
ColumnType column_type = columns_.at(index.column());
switch (role) { switch (role) {
case Qt::DisplayRole: case Qt::DisplayRole:
{ {
// Standard text role // Standard text role
ColumnType column_type = columns_.at(index.column());
switch (column_type) { switch (column_type) {
case kName: case kName:
@@ -138,7 +139,7 @@ QVariant ProjectViewModel::data(const QModelIndex &index, int role) const
break; break;
case Qt::DecorationRole: case Qt::DecorationRole:
// If this is the first column, return the Item's icon // If this is the first column, return the Item's icon
if (index.column() == 0) { if (column_type == kName) {
return internal_item->icon(); return internal_item->icon();
} }
break; break;
@@ -188,9 +189,36 @@ bool ProjectViewModel::hasChildren(const QModelIndex &parent) const
return QAbstractItemModel::hasChildren(parent); return QAbstractItemModel::hasChildren(parent);
} }
bool ProjectViewModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
// The name is editable
if (index.isValid() && columns_.at(index.column()) == kName && role == Qt::EditRole) {
Item* item = GetItemObjectFromIndex(index);
RenameItemCommand* ric = new RenameItemCommand(this, item, value.toString());
olive::undo_stack.push(ric);
return true;
}
return false;
}
Qt::ItemFlags ProjectViewModel::flags(const QModelIndex &index) const Qt::ItemFlags ProjectViewModel::flags(const QModelIndex &index) const
{ {
return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | QAbstractItemModel::flags(index); if (!index.isValid()) {
return Qt::NoItemFlags;
}
Qt::ItemFlags f = Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | QAbstractItemModel::flags(index);
// If the column is the kName column, that means it's editable
if (columns_.at(index.column()) == kName) {
f |= Qt::ItemIsEditable;
}
return f;
} }
QStringList ProjectViewModel::mimeTypes() const QStringList ProjectViewModel::mimeTypes() const
@@ -356,9 +384,9 @@ void ProjectViewModel::MoveItemInternal(Item *item, Item *destination)
endMoveRows(); endMoveRows();
} }
QModelIndex ProjectViewModel::CreateIndexFromItem(Item *item) QModelIndex ProjectViewModel::CreateIndexFromItem(Item *item, int column)
{ {
return createIndex(IndexOfChild(item), 0, item); return createIndex(IndexOfChild(item), column, item);
} }
ProjectViewModel::MoveItemCommand::MoveItemCommand(ProjectViewModel *model, ProjectViewModel::MoveItemCommand::MoveItemCommand(ProjectViewModel *model,
@@ -384,3 +412,31 @@ void ProjectViewModel::MoveItemCommand::undo()
{ {
model_->MoveItemInternal(item_, source_); model_->MoveItemInternal(item_, source_);
} }
ProjectViewModel::RenameItemCommand::RenameItemCommand(ProjectViewModel* model, Item *item, const QString &name, QUndoCommand *parent) :
QUndoCommand(parent),
model_(model),
item_(item),
new_name_(name)
{
old_name_ = item->name();
}
void ProjectViewModel::RenameItemCommand::redo()
{
set_name(new_name_);
}
void ProjectViewModel::RenameItemCommand::undo()
{
set_name(old_name_);
}
void ProjectViewModel::RenameItemCommand::set_name(const QString &n)
{
item_->set_name(n);
QModelIndex index = model_->CreateIndexFromItem(item_, model_->columns_.indexOf(kName));
emit model_->dataChanged(index, index, {Qt::DisplayRole, Qt::EditRole});
}
+22 -1
View File
@@ -83,6 +83,7 @@ public:
/** Optional Qt QAbstractItemModel overrides */ /** Optional Qt QAbstractItemModel overrides */
virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; 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 hasChildren(const QModelIndex &parent = QModelIndex()) const override;
virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
/** Drag and drop support */ /** Drag and drop support */
virtual Qt::ItemFlags flags(const QModelIndex &index) const override; virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
@@ -142,7 +143,7 @@ private:
/** /**
* @brief Convenience function for creating QModelIndexes from an Item object * @brief Convenience function for creating QModelIndexes from an Item object
*/ */
QModelIndex CreateIndexFromItem(Item* item); QModelIndex CreateIndexFromItem(Item* item, int column = 0);
Project* project_; Project* project_;
@@ -166,6 +167,26 @@ private:
Folder* destination_; 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 #endif // VIEWMODEL_H