Improve file attachments

-Don't show download and save in chatbar
-Add a remove button
-Add missing properties to model
This commit is contained in:
James Graham
2026-02-08 12:29:23 +00:00
parent 13d7f9b322
commit f02366ee48
3 changed files with 36 additions and 1 deletions

View File

@@ -72,12 +72,12 @@ QQC2.Control {
visible: NeoChatConfig.sendMessageWith === 1
}
RowLayout {
spacing: 0
QQC2.ScrollView {
id: chatScrollView
Layout.fillWidth: true
Layout.maximumHeight: Kirigami.Units.gridUnit * 8
contentWidth: availableWidth
clip: true
ColumnLayout {

View File

@@ -36,6 +36,11 @@ ColumnLayout {
*/
required property var fileTransferInfo
/**
* @brief Whether the component should be editable.
*/
required property bool editable
/**
* @brief Whether the media has been downloaded.
*/
@@ -66,6 +71,7 @@ ColumnLayout {
spacing: Kirigami.Units.largeSpacing
RowLayout {
Layout.fillWidth: true
spacing: Kirigami.Units.largeSpacing
states: [
@@ -137,6 +143,7 @@ ColumnLayout {
QQC2.Button {
id: openButton
visible: !root.editable
icon.name: "document-open"
onClicked: {
root.autoOpenFile = true;
@@ -150,6 +157,7 @@ ColumnLayout {
QQC2.Button {
id: downloadButton
visible: !root.editable
icon.name: "download"
onClicked: root.saveFileAs()
@@ -158,6 +166,18 @@ ColumnLayout {
QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
}
QQC2.Button {
id: cancelButton
visible: root.editable
display: QQC2.AbstractButton.IconOnly
text: i18nc("@action:button", "Remove attachment")
icon.name: "dialog-close"
onClicked: root.Message.contentModel?.removeAttachment()
QQC2.ToolTip.text: text
QQC2.ToolTip.visible: hovered
QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
}
Component {
id: fileDialog

View File

@@ -144,6 +144,10 @@ void ChatBarMessageContentModel::initializeFromCache()
}
endResetModel();
if (currentCache->attachmentPath().length() > 0) {
addAttachment(QUrl(currentCache->attachmentPath()));
}
m_currentFocusComponent = QPersistentModelIndex(index(rowCount() - 1));
Q_EMIT focusRowChanged();
}
@@ -365,12 +369,16 @@ void ChatBarMessageContentModel::addAttachment(const QUrl &path)
clearModel();
initializeModel(plainText);
QFileInfo fileInfo(path.isLocalFile() ? path.toLocalFile() : path.toString());
auto mime = QMimeDatabase().mimeTypeForUrl(path);
auto it = insertComponent(m_components.first().type == MessageComponentType::Reply ? 1 : 0,
MessageComponentType::typeForPath(path),
{
{"filename"_L1, path.fileName()},
{"source"_L1, path},
{"animated"_L1, false},
{"mimeIcon"_L1, mime.name()},
{"size"_L1, fileInfo.size()},
});
it->display = path.fileName();
Q_EMIT dataChanged(index(std::distance(m_components.begin(), it)), index(std::distance(m_components.begin(), it)), {DisplayRole});
@@ -662,6 +670,9 @@ std::optional<QString> ChatBarMessageContentModel::getReplyEventId()
void ChatBarMessageContentModel::clearModel()
{
const auto hadAttachment =
hasComponentType({MessageComponentType::File, MessageComponentType::Audio, MessageComponentType::Image, MessageComponentType::Video});
beginResetModel();
for (const auto &component : m_components) {
if (const auto textItem = textItemForComponent(component)) {
@@ -671,6 +682,10 @@ void ChatBarMessageContentModel::clearModel()
}
m_components.clear();
endResetModel();
if (hadAttachment) {
Q_EMIT hasAttachmentChanged();
}
}
#include "moc_chatbarmessagecontentmodel.cpp"