Add ability to rename attachments

* Closes #4758
This commit is contained in:
fpohtmeh 2020-07-11 19:12:23 +03:00 committed by Jonathan White
parent e53850627f
commit bbdfbe64da
7 changed files with 64 additions and 0 deletions

View File

@ -112,6 +112,13 @@ void EntryAttachments::remove(const QStringList& keys)
}
}
void EntryAttachments::rename(const QString& key, const QString& newKey)
{
const QByteArray val = value(key);
remove(key);
set(newKey, val);
}
bool EntryAttachments::isEmpty() const
{
return m_attachments.isEmpty();

View File

@ -36,6 +36,7 @@ public:
void set(const QString& key, const QByteArray& value);
void remove(const QString& key);
void remove(const QStringList& keys);
void rename(const QString& key, const QString& newKey);
bool isEmpty() const;
void clear();
void copyDataFrom(const EntryAttachments* other);

View File

@ -101,6 +101,28 @@ QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const
return QVariant();
}
bool EntryAttachmentsModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
if (!m_readOnly && index.column() == Columns::NameColumn) {
const QString key = value.toString().trimmed();
if (key.isEmpty() || m_entryAttachments->hasKey(key)) {
return false;
}
m_entryAttachments->rename(keyByIndex(index), key);
return true;
}
return QAbstractListModel::setData(index, value, role);
}
Qt::ItemFlags EntryAttachmentsModel::flags(const QModelIndex& index) const
{
Qt::ItemFlags flags = QAbstractListModel::flags(index);
if (!m_readOnly && index.column() == Columns::NameColumn) {
flags = flags | Qt::ItemIsEditable;
}
return flags;
}
QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const
{
if (!index.isValid()) {
@ -150,3 +172,8 @@ void EntryAttachmentsModel::reset()
{
endResetModel();
}
void EntryAttachmentsModel::setReadOnly(bool readOnly)
{
m_readOnly = readOnly;
}

View File

@ -40,6 +40,8 @@ public:
int columnCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
QString keyByIndex(const QModelIndex& index) const;
private slots:
@ -50,10 +52,12 @@ private slots:
void attachmentRemove();
void aboutToReset();
void reset();
void setReadOnly(bool readOnly);
private:
EntryAttachments* m_entryAttachments;
QStringList m_headers;
bool m_readOnly = false;
};
#endif // KEEPASSX_ENTRYATTACHMENTSMODEL_H

View File

@ -49,12 +49,14 @@ EntryAttachmentsWidget::EntryAttachmentsWidget(QWidget* parent)
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
SLOT(updateButtonsEnabled()));
// clang-format on
connect(this, SIGNAL(readOnlyChanged(bool)), m_attachmentsModel, SLOT(setReadOnly(bool)));
connect(m_ui->attachmentsView, SIGNAL(doubleClicked(QModelIndex)), SLOT(openAttachment(QModelIndex)));
connect(m_ui->saveAttachmentButton, SIGNAL(clicked()), SLOT(saveSelectedAttachments()));
connect(m_ui->openAttachmentButton, SIGNAL(clicked()), SLOT(openSelectedAttachments()));
connect(m_ui->addAttachmentButton, SIGNAL(clicked()), SLOT(insertAttachments()));
connect(m_ui->removeAttachmentButton, SIGNAL(clicked()), SLOT(removeSelectedAttachments()));
connect(m_ui->renameAttachmentButton, SIGNAL(clicked()), SLOT(renameSelectedAttachments()));
updateButtonsVisible();
updateButtonsEnabled();
@ -184,6 +186,11 @@ void EntryAttachmentsWidget::removeSelectedAttachments()
}
}
void EntryAttachmentsWidget::renameSelectedAttachments()
{
m_ui->attachmentsView->edit(m_ui->attachmentsView->selectionModel()->selectedIndexes().first());
}
void EntryAttachmentsWidget::saveSelectedAttachments()
{
const QModelIndexList indexes = m_ui->attachmentsView->selectionModel()->selectedRows(0);
@ -289,6 +296,7 @@ void EntryAttachmentsWidget::updateButtonsEnabled()
m_ui->addAttachmentButton->setEnabled(!m_readOnly);
m_ui->removeAttachmentButton->setEnabled(hasSelection && !m_readOnly);
m_ui->renameAttachmentButton->setEnabled(hasSelection && !m_readOnly);
m_ui->saveAttachmentButton->setEnabled(hasSelection);
m_ui->openAttachmentButton->setEnabled(hasSelection);

View File

@ -45,6 +45,7 @@ signals:
private slots:
void insertAttachments();
void removeSelectedAttachments();
void renameSelectedAttachments();
void saveSelectedAttachments();
void openAttachment(const QModelIndex& index);
void openSelectedAttachments();

View File

@ -31,6 +31,9 @@
<property name="accessibleName">
<string>Attachments</string>
</property>
<property name="editTriggers">
<set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property>
</widget>
</item>
<item>
@ -74,6 +77,19 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="renameAttachmentButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="accessibleName">
<string>Rename selected attachment</string>
</property>
<property name="text">
<string>Rename</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="openAttachmentButton">
<property name="enabled">