Improve cut and paste in Qt Quick GUI

This commit is contained in:
Martchus 2018-06-12 22:20:43 +02:00
parent 71652bcab3
commit cc78072ffc
5 changed files with 28 additions and 18 deletions

View File

@ -453,10 +453,9 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
return push(make_unique<EntryModelMoveRowsCommand>(this, sourceParent, sourceRow, count, destinationParent, destinationChild));
}
#endif
// check validation of specified arguments
if (!sourceParent.isValid() || !destinationParent.isValid() || sourceRow < 0 || count <= 0
|| entry(sourceParent)->type() != EntryType::Node // source and destination parent entries
|| entry(destinationParent)->type() != EntryType::Node) { // need to be node entries
// check validation of specified arguments: source and destination parent entries need to be node entries
if (!sourceParent.isValid() || !destinationParent.isValid() || sourceRow < 0 || count <= 0 || entry(sourceParent)->type() != EntryType::Node //
|| entry(destinationParent)->type() != EntryType::Node) {
return false;
}
// determine the source parent entry and dest parent entry as node entries
@ -470,11 +469,12 @@ bool EntryModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int co
}
// do not move a row to one of its own children! -> check before
for (int index = 0; index < count; ++index) {
Entry *toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
if (toMove->type() == EntryType::Node) {
if (destParentEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) {
return false;
}
Entry *const toMove = srcParentEntry->children()[static_cast<size_t>(sourceRow + index)];
if (toMove->type() != EntryType::Node) {
continue;
}
if (toMove == destParentEntry || destParentEntry->isIndirectChildOf(static_cast<NodeEntry *>(toMove))) {
return false;
}
}
// actually perform the move operation

View File

@ -54,7 +54,7 @@ public:
QModelIndex parent(const QModelIndex &child) const;
bool hasChildren(const QModelIndex &parent) const;
Q_INVOKABLE bool isNode(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QMap<int, QVariant> itemData(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role);
bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &roles);

View File

@ -22,7 +22,16 @@ Kirigami.ScrollablePage {
iconName: "edit-paste"
text: qsTr("Paste account")
enabled: nativeInterface.canPaste
onTriggered: nativeInterface.pasteEntries(rootIndex)
onTriggered: {
var pastedEntries = nativeInterface.pasteEntries(rootIndex)
if (pastedEntries.length < 1) {
showPassiveNotification(
qsTr("Unable to paste the entries here"))
return
}
var joinedEntryNames = pastedEntries.join(", ")
showPassiveNotification(qsTr("Pasted ") + joinedEntryNames)
}
}
right: Kirigami.Action {
iconName: "folder-add"

View File

@ -113,28 +113,29 @@ void Controller::save()
}
}
bool Controller::pasteEntries(const QModelIndex &destinationParent, int row)
QStringList Controller::pasteEntries(const QModelIndex &destinationParent, int row)
{
if (m_cutEntries.isEmpty() || !m_entryModel.isNode(destinationParent)) {
return false;
return QStringList();
}
if (row < 0) {
row = m_entryModel.rowCount(destinationParent);
}
bool result = true;
QStringList successfullyMovedEntries;
successfullyMovedEntries.reserve(m_cutEntries.size());
for (const QPersistentModelIndex &cutIndex : m_cutEntries) {
if (m_entryModel.moveRows(cutIndex.parent(), cutIndex.row(), 1, destinationParent, row)) {
successfullyMovedEntries << m_entryModel.data(m_entryModel.index(row, 0, destinationParent)).toString();
++row;
} else {
result = false;
}
}
// clear the cut entries
m_cutEntries.clear();
emit cutEntriesChanged(m_cutEntries);
return true;
return successfullyMovedEntries;
}
void Controller::resetFileStatus()

View File

@ -44,7 +44,7 @@ public:
void setCutEntries(const QList<QPersistentModelIndex> &cutEntries);
QString currentAccountName() const;
Q_INVOKABLE void cutEntry(const QModelIndex &entryIndex);
Q_INVOKABLE bool pasteEntries(const QModelIndex &destinationParent, int row = -1);
Q_INVOKABLE QStringList pasteEntries(const QModelIndex &destinationParent, int row = -1);
bool canPaste() const;
public slots: