Skip to content

Commit

Permalink
Add ability to rename attachments
Browse files Browse the repository at this point in the history
* Closes #4758
  • Loading branch information
fpohtmeh-github authored and droidmonkey committed Sep 1, 2020
1 parent e538506 commit 42f290a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/core/EntryAttachments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/core/EntryAttachments.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class EntryAttachments : public QObject
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);
Expand Down
27 changes: 27 additions & 0 deletions src/gui/entry/EntryAttachmentsModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -150,3 +172,8 @@ void EntryAttachmentsModel::reset()
{
endResetModel();
}

void EntryAttachmentsModel::setReadOnly(bool readOnly)
{
m_readOnly = readOnly;
}
4 changes: 4 additions & 0 deletions src/gui/entry/EntryAttachmentsModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class EntryAttachmentsModel : public QAbstractListModel
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:
Expand All @@ -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
8 changes: 8 additions & 0 deletions src/gui/entry/EntryAttachmentsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/gui/entry/EntryAttachmentsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public slots:
private slots:
void insertAttachments();
void removeSelectedAttachments();
void renameSelectedAttachments();
void saveSelectedAttachments();
void openAttachment(const QModelIndex& index);
void openSelectedAttachments();
Expand Down
16 changes: 16 additions & 0 deletions src/gui/entry/EntryAttachmentsWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
<property name="accessibleName">
<string>Attachments</string>
</property>
<property name="editTriggers">
<set>QAbstractItemView::EditKeyPressed|QAbstractItemView::SelectedClicked</set>
</property>
</widget>
</item>
<item>
Expand Down Expand Up @@ -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">
Expand Down

0 comments on commit 42f290a

Please sign in to comment.