Add New/Preview Entry Attachments dialog and functionality (#11637)

Closes #11506
Closes #3383

* This change adds a new opportunity to add attachments that don’t require a real file in the file system.
* Add a new dialog window to add and preview attachments and integrate it into the EntryAttachmentsWidget.
* Attachment preview support for images and plain text files.

Additional enhancements:
* Fix sizing of attachment columns
* Add padding to attachment table items
* Fix targeting of preview widget styling to not impact unintended children
This commit is contained in:
Kuznetsov Oleg authored and GitHub committed 2025-01-11 23:08:19 -05:00
1 parent 40ee047ef0
commit dce34de875
16 files changed
+717 -47

No files matched your search

+82 -19
View File
@@ -16,16 +16,19 @@
*/
#include "EntryAttachmentsWidget.h"
#include "EntryAttachmentsModel.h"
#include "NewEntryAttachmentsDialog.h"
#include "PreviewEntryAttachmentsDialog.h"
#include "ui_EntryAttachmentsWidget.h"
#include <QDir>
#include <QDebug>
#include <QDropEvent>
#include <QMimeData>
#include <QStandardPaths>
#include <QTemporaryFile>
#include "EntryAttachmentsModel.h"
#include "core/Config.h"
#include "core/EntryAttachments.h"
#include "core/Tools.h"
#include "gui/FileDialog.h"
@@ -46,12 +49,12 @@ EntryAttachmentsWidget::EntryAttachmentsWidget(QWidget* parent)
m_ui->attachmentsView->viewport()->installEventFilter(this);
m_ui->attachmentsView->setModel(m_attachmentsModel);
m_ui->attachmentsView->verticalHeader()->hide();
m_ui->attachmentsView->horizontalHeader()->setStretchLastSection(true);
m_ui->attachmentsView->horizontalHeader()->resizeSection(EntryAttachmentsModel::NameColumn, 400);
m_ui->attachmentsView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_ui->attachmentsView->setSelectionMode(QAbstractItemView::ExtendedSelection);
m_ui->attachmentsView->setEditTriggers(QAbstractItemView::SelectedClicked);
m_ui->attachmentsView->horizontalHeader()->setMinimumSectionSize(70);
m_ui->attachmentsView->horizontalHeader()->setSectionResizeMode(EntryAttachmentsModel::NameColumn,
QHeaderView::Stretch);
m_ui->attachmentsView->horizontalHeader()->setSectionResizeMode(EntryAttachmentsModel::SizeColumn,
QHeaderView::ResizeToContents);
m_ui->attachmentsView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
connect(this, SIGNAL(buttonsVisibleChanged(bool)), this, SLOT(updateButtonsVisible()));
connect(this, SIGNAL(readOnlyChanged(bool)), SLOT(updateButtonsEnabled()));
@@ -64,12 +67,13 @@ EntryAttachmentsWidget::EntryAttachmentsWidget(QWidget* parent)
// 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->attachmentsView, SIGNAL(doubleClicked(QModelIndex)), SLOT(previewSelectedAttachment()));
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->newAttachmentButton, SIGNAL(clicked()), SLOT(newAttachments()));
connect(m_ui->previewAttachmentButton, SIGNAL(clicked()), SLOT(previewSelectedAttachment()));
connect(m_ui->removeAttachmentButton, SIGNAL(clicked()), SLOT(removeSelectedAttachments()));
connect(m_ui->renameAttachmentButton, SIGNAL(clicked()), SLOT(renameSelectedAttachments()));
updateButtonsVisible();
updateButtonsEnabled();
@@ -163,6 +167,57 @@ void EntryAttachmentsWidget::insertAttachments()
emit widgetUpdated();
}
void EntryAttachmentsWidget::newAttachments()
{
Q_ASSERT(m_entryAttachments);
Q_ASSERT(!isReadOnly());
if (isReadOnly()) {
return;
}
NewEntryAttachmentsDialog newEntryDialog(m_entryAttachments, this);
if (newEntryDialog.exec() == QDialog::Accepted) {
emit widgetUpdated();
}
}
void EntryAttachmentsWidget::previewSelectedAttachment()
{
Q_ASSERT(m_entryAttachments);
const auto index = m_ui->attachmentsView->selectionModel()->selectedIndexes().first();
if (!index.isValid()) {
qWarning() << tr("Failed to preview an attachment: Attachment not found");
return;
}
// Set selection to the first
m_ui->attachmentsView->setCurrentIndex(index);
auto name = m_attachmentsModel->keyByIndex(index);
auto data = m_entryAttachments->value(name);
PreviewEntryAttachmentsDialog previewDialog(this);
previewDialog.setAttachment(name, data);
connect(&previewDialog, SIGNAL(openAttachment(QString)), SLOT(openSelectedAttachments()));
connect(&previewDialog, SIGNAL(saveAttachment(QString)), SLOT(saveSelectedAttachments()));
// Refresh the preview if the attachment changes
connect(m_entryAttachments,
&EntryAttachments::keyModified,
&previewDialog,
[&previewDialog, &name, this](const QString& key) {
if (key == name) {
previewDialog.setAttachment(name, m_entryAttachments->value(name));
}
});
previewDialog.exec();
// Set focus back to the widget to allow keyboard navigation
setFocus();
}
void EntryAttachmentsWidget::removeSelectedAttachments()
{
Q_ASSERT(m_entryAttachments);
@@ -192,12 +247,6 @@ void EntryAttachmentsWidget::removeSelectedAttachments()
}
}
void EntryAttachmentsWidget::renameSelectedAttachments()
{
Q_ASSERT(m_entryAttachments);
m_ui->attachmentsView->edit(m_ui->attachmentsView->selectionModel()->selectedIndexes().first());
}
void EntryAttachmentsWidget::saveSelectedAttachments()
{
Q_ASSERT(m_entryAttachments);
@@ -287,7 +336,7 @@ void EntryAttachmentsWidget::openSelectedAttachments()
if (!m_entryAttachments->openAttachment(m_attachmentsModel->keyByIndex(index), &errorMessage)) {
const QString filename = m_attachmentsModel->keyByIndex(index);
errors.append(QString("%1 - %2").arg(filename, errorMessage));
};
}
}
if (!errors.isEmpty()) {
@@ -300,18 +349,32 @@ void EntryAttachmentsWidget::updateButtonsEnabled()
const bool hasSelection = m_ui->attachmentsView->selectionModel()->hasSelection();
m_ui->addAttachmentButton->setEnabled(!m_readOnly);
m_ui->newAttachmentButton->setEnabled(!m_readOnly);
m_ui->removeAttachmentButton->setEnabled(hasSelection && !m_readOnly);
m_ui->renameAttachmentButton->setEnabled(hasSelection && !m_readOnly);
m_ui->saveAttachmentButton->setEnabled(hasSelection);
m_ui->previewAttachmentButton->setEnabled(hasSelection);
m_ui->openAttachmentButton->setEnabled(hasSelection);
updateSpacers();
}
void EntryAttachmentsWidget::updateSpacers()
{
if (m_buttonsVisible && !m_readOnly) {
m_ui->previewVSpacer->changeSize(20, 40, QSizePolicy::Fixed, QSizePolicy::Expanding);
} else {
m_ui->previewVSpacer->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
}
}
void EntryAttachmentsWidget::updateButtonsVisible()
{
m_ui->addAttachmentButton->setVisible(m_buttonsVisible && !m_readOnly);
m_ui->newAttachmentButton->setVisible(m_buttonsVisible && !m_readOnly);
m_ui->removeAttachmentButton->setVisible(m_buttonsVisible && !m_readOnly);
m_ui->renameAttachmentButton->setVisible(m_buttonsVisible && !m_readOnly);
updateSpacers();
}
bool EntryAttachmentsWidget::insertAttachments(const QStringList& filenames, QString& errorMessage)