Use a table view to display the list of attachments in EditEntryWidget

This commit is contained in:
frostasm 2017-12-10 20:55:18 +02:00
parent 0a9b250d73
commit bf5b96362f
5 changed files with 53 additions and 33 deletions

View File

@ -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();

View File

@ -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();

View File

@ -153,14 +153,7 @@
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QListView" name="attachmentsView">
<property name="flow">
<enum>QListView::LeftToRight</enum>
</property>
<property name="isWrapping" stdset="0">
<bool>true</bool>
</property>
</widget>
<widget class="QTableView" name="attachmentsView"/>
</item>
<item>
<layout class="QVBoxLayout" name="attachmentsButtonLayout">
@ -234,7 +227,6 @@
<tabstop>addAttributeButton</tabstop>
<tabstop>removeAttributeButton</tabstop>
<tabstop>editAttributeButton</tabstop>
<tabstop>attachmentsView</tabstop>
<tabstop>addAttachmentButton</tabstop>
<tabstop>removeAttachmentButton</tabstop>
<tabstop>openAttachmentButton</tabstop>

View File

@ -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

View File

@ -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