diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index db35b2f8f..944471c7f 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -140,9 +140,14 @@ void EditEntryWidget::setupAdvanced() m_attachmentsModel->setEntryAttachments(m_entryAttachments); m_advancedUi->attachmentsView->setModel(m_attachmentsModel); + m_advancedUi->attachmentsView->horizontalHeader()->setStretchLastSection(true); + m_advancedUi->attachmentsView->horizontalHeader()->resizeSection(0, 600); + m_advancedUi->attachmentsView->setSelectionBehavior(QAbstractItemView::SelectRows); + m_advancedUi->attachmentsView->setSelectionMode(QAbstractItemView::MultiSelection); + m_advancedUi->attachmentsView->setSelectionMode(QAbstractItemView::ExtendedSelection); - connect(m_advancedUi->attachmentsView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), - SLOT(updateAttachmentButtonsEnabled(QModelIndex))); + connect(m_advancedUi->attachmentsView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), + SLOT(updateAttachmentButtonsEnabled())); connect(m_advancedUi->attachmentsView, SIGNAL(doubleClicked(QModelIndex)), SLOT(openAttachment(QModelIndex))); connect(m_advancedUi->saveAttachmentButton, SIGNAL(clicked()), SLOT(saveSelectedAttachments())); connect(m_advancedUi->openAttachmentButton, SIGNAL(clicked()), SLOT(openSelectedAttachments())); @@ -510,15 +515,6 @@ void EditEntryWidget::useExpiryPreset(QAction* action) m_mainUi->expireDatePicker->setDateTime(expiryDateTime); } -void EditEntryWidget::updateAttachmentButtonsEnabled(const QModelIndex& current) -{ - bool enable = current.isValid(); - - m_advancedUi->saveAttachmentButton->setEnabled(enable); - m_advancedUi->openAttachmentButton->setEnabled(enable); - m_advancedUi->removeAttachmentButton->setEnabled(enable && !m_history); -} - void EditEntryWidget::toggleHideNotes(bool visible) { m_mainUi->notesEdit->setVisible(visible); @@ -581,7 +577,7 @@ void EditEntryWidget::setForms(const Entry* entry, bool restore) m_mainUi->togglePasswordGeneratorButton->setDisabled(m_history); m_mainUi->passwordGenerator->reset(); m_advancedUi->addAttachmentButton->setEnabled(!m_history); - updateAttachmentButtonsEnabled(m_advancedUi->attachmentsView->currentIndex()); + updateAttachmentButtonsEnabled(); m_advancedUi->addAttributeButton->setEnabled(!m_history); m_advancedUi->editAttributeButton->setEnabled(false); m_advancedUi->removeAttributeButton->setEnabled(false); @@ -1069,7 +1065,7 @@ void EditEntryWidget::saveSelectedAttachment() void EditEntryWidget::saveSelectedAttachments() { - const QModelIndexList indexes = m_advancedUi->attachmentsView->selectionModel()->selectedIndexes(); + const QModelIndexList indexes = m_advancedUi->attachmentsView->selectionModel()->selectedRows(0); if (indexes.isEmpty()) { return; } else if (indexes.count() == 1) { @@ -1140,7 +1136,7 @@ void EditEntryWidget::openAttachment(const QModelIndex& index) void EditEntryWidget::openSelectedAttachments() { - const QModelIndexList indexes = m_advancedUi->attachmentsView->selectionModel()->selectedIndexes(); + const QModelIndexList indexes = m_advancedUi->attachmentsView->selectionModel()->selectedRows(0); if (indexes.isEmpty()) { return; } @@ -1163,7 +1159,7 @@ void EditEntryWidget::removeSelectedAttachments() { Q_ASSERT(!m_history); - const QModelIndexList indexes = m_advancedUi->attachmentsView->selectionModel()->selectedIndexes(); + const QModelIndexList indexes = m_advancedUi->attachmentsView->selectionModel()->selectedRows(0); if (indexes.isEmpty()) { return; } @@ -1180,6 +1176,15 @@ void EditEntryWidget::removeSelectedAttachments() } } +void EditEntryWidget::updateAttachmentButtonsEnabled() +{ + const bool hasSelection = m_advancedUi->attachmentsView->selectionModel()->hasSelection(); + + m_advancedUi->saveAttachmentButton->setEnabled(hasSelection); + m_advancedUi->openAttachmentButton->setEnabled(hasSelection); + m_advancedUi->removeAttachmentButton->setEnabled(hasSelection && !m_history); +} + void EditEntryWidget::updateAutoTypeEnabled() { bool autoTypeEnabled = m_autoTypeUi->enableButton->isChecked(); diff --git a/src/gui/entry/EditEntryWidget.h b/src/gui/entry/EditEntryWidget.h index 883e7e7fb..887e562d6 100644 --- a/src/gui/entry/EditEntryWidget.h +++ b/src/gui/entry/EditEntryWidget.h @@ -92,6 +92,7 @@ private slots: void openAttachment(const QModelIndex& index); void openSelectedAttachments(); void removeSelectedAttachments(); + void updateAttachmentButtonsEnabled(); void updateAutoTypeEnabled(); void insertAutoTypeAssoc(); void removeAutoTypeAssoc(); @@ -106,7 +107,6 @@ private slots: void histEntryActivated(const QModelIndex& index); void updateHistoryButtons(const QModelIndex& current, const QModelIndex& previous); void useExpiryPreset(QAction* action); - void updateAttachmentButtonsEnabled(const QModelIndex& current); void toggleHideNotes(bool visible); #ifdef WITH_XC_SSHAGENT void updateSSHAgent(); diff --git a/src/gui/entry/EditEntryWidgetAdvanced.ui b/src/gui/entry/EditEntryWidgetAdvanced.ui index 2c7f95dde..5085d164e 100644 --- a/src/gui/entry/EditEntryWidgetAdvanced.ui +++ b/src/gui/entry/EditEntryWidgetAdvanced.ui @@ -153,14 +153,7 @@ - - - QListView::LeftToRight - - - true - - + @@ -234,7 +227,6 @@ addAttributeButton removeAttributeButton editAttributeButton - attachmentsView addAttachmentButton removeAttachmentButton openAttachmentButton diff --git a/src/gui/entry/EntryAttachmentsModel.cpp b/src/gui/entry/EntryAttachmentsModel.cpp index 082641380..332ed754c 100644 --- a/src/gui/entry/EntryAttachmentsModel.cpp +++ b/src/gui/entry/EntryAttachmentsModel.cpp @@ -26,6 +26,8 @@ EntryAttachmentsModel::EntryAttachmentsModel(QObject* parent) : QAbstractListModel(parent) , m_entryAttachments(nullptr) { + m_headers << tr("Name") + << tr("Size"); } void EntryAttachmentsModel::setEntryAttachments(EntryAttachments* entryAttachments) @@ -65,7 +67,17 @@ int EntryAttachmentsModel::columnCount(const QModelIndex& parent) const { Q_UNUSED(parent); - return 1; + return Columns::ColumnsCount; +} + +QVariant EntryAttachmentsModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { + Q_ASSERT(m_headers.size() == columnCount()); + return m_headers[section]; + } + + return QAbstractListModel::headerData(section, orientation, role); } QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const @@ -74,15 +86,18 @@ QVariant EntryAttachmentsModel::data(const QModelIndex& index, int role) const return QVariant(); } - if (role == Qt::DisplayRole && index.column() == 0) { + if (role == Qt::DisplayRole) { QString key = keyByIndex(index); - - return QString("%1 (%2)").arg(key, - Tools::humanReadableFileSize(m_entryAttachments->value(key).size())); - } - else { - return QVariant(); + switch (index.column()) { + case Columns::NameColumn: + return key; + case Columns::SizeColumn: + return Tools::humanReadableFileSize(m_entryAttachments->value(key).size()); + default: + break; + } } + return QVariant(); } QString EntryAttachmentsModel::keyByIndex(const QModelIndex& index) const diff --git a/src/gui/entry/EntryAttachmentsModel.h b/src/gui/entry/EntryAttachmentsModel.h index 6abcdc2e2..fa8d35548 100644 --- a/src/gui/entry/EntryAttachmentsModel.h +++ b/src/gui/entry/EntryAttachmentsModel.h @@ -27,10 +27,17 @@ class EntryAttachmentsModel : public QAbstractListModel Q_OBJECT public: + enum Columns { + NameColumn, + SizeColumn, + ColumnsCount + }; + explicit EntryAttachmentsModel(QObject* parent = nullptr); void setEntryAttachments(EntryAttachments* entry); int rowCount(const QModelIndex& parent = QModelIndex()) const override; 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; QString keyByIndex(const QModelIndex& index) const; @@ -45,6 +52,7 @@ private slots: private: EntryAttachments* m_entryAttachments; + QStringList m_headers; }; #endif // KEEPASSX_ENTRYATTACHMENTSMODEL_H