Use enum instead of strings for message type

This commit is contained in:
Tobias Fella
2022-11-01 21:15:44 +01:00
committed by Carl Schwan
parent bba947e508
commit 92ec441594
9 changed files with 69 additions and 53 deletions

View File

@@ -10,9 +10,9 @@ bool CollapseStateProxyModel::filterAcceptsRow(int source_row, const QModelIndex
{ {
Q_UNUSED(source_parent); Q_UNUSED(source_parent);
return sourceModel()->data(sourceModel()->index(source_row, 0), MessageEventModel::EventTypeRole) return sourceModel()->data(sourceModel()->index(source_row, 0), MessageEventModel::EventTypeRole)
!= QLatin1String("state") // If this is not a state, show it != MessageEventModel::DelegateType::State // If this is not a state, show it
|| sourceModel()->data(sourceModel()->index(source_row + 1, 0), MessageEventModel::EventTypeRole) || sourceModel()->data(sourceModel()->index(source_row + 1, 0), MessageEventModel::EventTypeRole)
!= QLatin1String("state") // If this is the first state in a block, show it. TODO hidden events? != MessageEventModel::DelegateType::State // If this is the first state in a block, show it. TODO hidden events?
|| sourceModel()->data(sourceModel()->index(source_row, 0), MessageEventModel::ShowSectionRole).toBool() // If it's a new day, show it || sourceModel()->data(sourceModel()->index(source_row, 0), MessageEventModel::ShowSectionRole).toBool() // If it's a new day, show it
|| sourceModel()->data(sourceModel()->index(source_row, 0), MessageEventModel::EventResolvedTypeRole) || sourceModel()->data(sourceModel()->index(source_row, 0), MessageEventModel::EventResolvedTypeRole)
!= sourceModel()->data(sourceModel()->index(source_row + 1, 0), != sourceModel()->data(sourceModel()->index(source_row + 1, 0),
@@ -41,7 +41,8 @@ QString CollapseStateProxyModel::aggregateEventToString(int sourceRow) const
QStringList parts; QStringList parts;
for (int i = sourceRow; i >= 0; i--) { for (int i = sourceRow; i >= 0; i--) {
parts += sourceModel()->data(sourceModel()->index(i, 0), Qt::DisplayRole).toString(); parts += sourceModel()->data(sourceModel()->index(i, 0), Qt::DisplayRole).toString();
if (sourceModel()->data(sourceModel()->index(i, 0), MessageEventModel::EventTypeRole) != QLatin1String("state") // If it's not a state event if (sourceModel()->data(sourceModel()->index(i, 0), MessageEventModel::EventTypeRole)
!= MessageEventModel::DelegateType::State // If it's not a state event
|| (i > 0 || (i > 0
&& sourceModel()->data(sourceModel()->index(i, 0), MessageEventModel::EventResolvedTypeRole) && sourceModel()->data(sourceModel()->index(i, 0), MessageEventModel::EventResolvedTypeRole)
!= sourceModel()->data(sourceModel()->index(i - 1, 0), MessageEventModel::EventResolvedTypeRole)) // or of a different type != sourceModel()->data(sourceModel()->index(i - 1, 0), MessageEventModel::EventResolvedTypeRole)) // or of a different type

View File

@@ -420,7 +420,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const
if (m_lastReadEventIndex.row() == row) { if (m_lastReadEventIndex.row() == row) {
switch (role) { switch (role) {
case EventTypeRole: case EventTypeRole:
return QStringLiteral("readMarker"); return DelegateType::ReadMarker;
case TimeRole: { case TimeRole: {
const QDateTime eventDate = data(index(m_lastReadEventIndex.row() + 1, 0), TimeRole).toDateTime(); const QDateTime eventDate = data(index(m_lastReadEventIndex.row() + 1, 0), TimeRole).toDateTime();
const KFormat format; const KFormat format;
@@ -467,35 +467,34 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const
if (auto e = eventCast<const RoomMessageEvent>(&evt)) { if (auto e = eventCast<const RoomMessageEvent>(&evt)) {
switch (e->msgtype()) { switch (e->msgtype()) {
case MessageEventType::Emote: case MessageEventType::Emote:
return "emote"; return DelegateType::Emote;
case MessageEventType::Notice: case MessageEventType::Notice:
return "notice"; return DelegateType::Notice;
case MessageEventType::Image: case MessageEventType::Image:
return "image"; return DelegateType::Image;
case MessageEventType::Audio: case MessageEventType::Audio:
return "audio"; return DelegateType::Audio;
case MessageEventType::Video: case MessageEventType::Video:
return "video"; return DelegateType::Video;
default: default:
break; break;
} }
if (e->hasFileContent()) { if (e->hasFileContent()) {
return "file"; return DelegateType::File;
} }
return "message"; return DelegateType::Message;
} }
if (is<const StickerEvent>(evt)) { if (is<const StickerEvent>(evt)) {
return "sticker"; return DelegateType::Sticker;
} }
if (evt.isStateEvent()) { if (evt.isStateEvent()) {
return "state"; return DelegateType::State;
} }
if (is<const EncryptedEvent>(evt)) { if (is<const EncryptedEvent>(evt)) {
return "encrypted"; return DelegateType::Encrypted;
} }
return DelegateType::Other;
return "other";
} }
if (role == EventResolvedTypeRole) { if (role == EventResolvedTypeRole) {
@@ -671,36 +670,36 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const
return {}; return {};
} }
QString type; DelegateType type;
if (auto e = eventCast<const RoomMessageEvent>(replyPtr)) { if (auto e = eventCast<const RoomMessageEvent>(replyPtr)) {
switch (e->msgtype()) { switch (e->msgtype()) {
case MessageEventType::Emote: case MessageEventType::Emote:
type = "emote"; type = DelegateType::Emote;
break; break;
case MessageEventType::Notice: case MessageEventType::Notice:
type = "notice"; type = DelegateType::Notice;
break; break;
case MessageEventType::Image: case MessageEventType::Image:
type = "image"; type = DelegateType::Image;
break; break;
case MessageEventType::Audio: case MessageEventType::Audio:
type = "audio"; type = DelegateType::Audio;
break; break;
case MessageEventType::Video: case MessageEventType::Video:
type = "video"; type = DelegateType::Video;
break; break;
default: default:
if (e->hasFileContent()) { if (e->hasFileContent()) {
type = "file"; type = DelegateType::File;
break; break;
} }
type = "message"; type = DelegateType::Message;
} }
} else if (is<const StickerEvent>(*replyPtr)) { } else if (is<const StickerEvent>(*replyPtr)) {
type = "sticker"; type = DelegateType::Sticker;
} else { } else {
type = "other"; type = DelegateType::Other;
} }
QVariant content; QVariant content;
@@ -729,7 +728,7 @@ QVariant MessageEventModel::data(const QModelIndex &idx, int role) const
// While the row is removed the subsequent row indexes are not changed so we need to skip over the removed index. // While the row is removed the subsequent row indexes are not changed so we need to skip over the removed index.
// See - https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows // See - https://doc.qt.io/qt-5/qabstractitemmodel.html#beginRemoveRows
if (data(i, SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) { if (data(i, SpecialMarksRole) != EventStatus::Hidden && !itemData(i).empty()) {
return data(i, AuthorRole) != data(idx, AuthorRole) || data(i, EventTypeRole) == "state" return data(i, AuthorRole) != data(idx, AuthorRole) || data(i, EventTypeRole) == MessageEventModel::State
|| data(i, TimeRole).toDateTime().msecsTo(data(idx, TimeRole).toDateTime()) > 600000 || data(i, TimeRole).toDateTime().msecsTo(data(idx, TimeRole).toDateTime()) > 600000
|| data(i, TimeRole).toDateTime().toLocalTime().date().day() != data(idx, TimeRole).toDateTime().toLocalTime().date().day(); || data(i, TimeRole).toDateTime().toLocalTime().date().day() != data(idx, TimeRole).toDateTime().toLocalTime().date().day();
} }

View File

@@ -13,6 +13,22 @@ class MessageEventModel : public QAbstractListModel
Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged) Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
public: public:
enum DelegateType {
Emote,
Notice,
Image,
Audio,
Video,
File,
Message,
Sticker,
State,
Encrypted,
ReadMarker,
Other,
};
Q_ENUM(DelegateType);
enum EventRoles { enum EventRoles {
EventTypeRole = Qt::UserRole + 1, EventTypeRole = Qt::UserRole + 1,
MessageRole, MessageRole,

View File

@@ -29,11 +29,11 @@ bool MessageFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sour
const QString eventType = index.data(MessageEventModel::EventTypeRole).toString(); const QString eventType = index.data(MessageEventModel::EventTypeRole).toString();
if (eventType == QLatin1String("other")) { if (eventType == MessageEventModel::Other) {
return false; return false;
} }
if (!NeoChatConfig::self()->showLeaveJoinEvent() && eventType == QLatin1String("state")) { if (!NeoChatConfig::self()->showLeaveJoinEvent() && eventType == MessageEventModel::State) {
return false; return false;
} }

View File

@@ -14,64 +14,64 @@ DelegateChooser {
role: "eventType" role: "eventType"
DelegateChoice { DelegateChoice {
roleValue: "state" roleValue: MessageEventModel.State
delegate: StateDelegate {} delegate: StateDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "emote" roleValue: MessageEventModel.Emote
delegate: MessageDelegate { delegate: MessageDelegate {
isEmote: true isEmote: true
} }
} }
DelegateChoice { DelegateChoice {
roleValue: "message" roleValue: MessageEventModel.Message
delegate: MessageDelegate {} delegate: MessageDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "notice" roleValue: MessageEventModel.Notice
delegate: MessageDelegate {} delegate: MessageDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "image" roleValue: MessageEventModel.Image
delegate: ImageDelegate {} delegate: ImageDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "sticker" roleValue: MessageEventModel.Sticker
delegate: ImageDelegate {} delegate: ImageDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "audio" roleValue: MessageEventModel.Audio
delegate: AudioDelegate {} delegate: AudioDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "video" roleValue: MessageEventModel.Video
delegate: VideoDelegate {} delegate: VideoDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "file" roleValue: MessageEventModel.File
delegate: FileDelegate {} delegate: FileDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "encrypted" roleValue: MessageEventModel.Encrypted
delegate: EncryptedDelegate {} delegate: EncryptedDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "readMarker" roleValue: MessageEventModel.ReadMarker
delegate: ReadMarkerDelegate {} delegate: ReadMarkerDelegate {}
} }
DelegateChoice { DelegateChoice {
roleValue: "other" roleValue: MessageEventModel.Other
delegate: Item {} delegate: Item {}
} }
} }

View File

@@ -65,17 +65,17 @@ Item {
sourceComponent: { sourceComponent: {
switch (reply.type) { switch (reply.type) {
case "image": case MessageEventModel.Image:
case "sticker": case MessageEventModel.Sticker:
return imageComponent; return imageComponent;
case "message": case MessageEventModel.Message:
case "notice": case MessageEventModel.Notice:
return textComponent; return textComponent;
case "file": case MessageEventModel.File:
case "video": case MessageEventModel.Video:
case "audio": case MessageEventModel.Audio:
return mimeComponent; return mimeComponent;
case "encrypted": case MessageEventModel.Encrypted:
return encryptedComponent; return encryptedComponent;
default: default:
return textComponent; return textComponent;
@@ -114,7 +114,7 @@ Item {
MimeComponent { MimeComponent {
mimeIconSource: reply.content.info.mimetype.replace("/", "-") mimeIconSource: reply.content.info.mimetype.replace("/", "-")
label: reply.display label: reply.display
subLabel: reply.type === "file" ? Controller.formatByteSize(reply.content.info ? reply.content.info.size : 0) : Controller.formatDuration(reply.content.info.duration) subLabel: reply.type === MessageEventModel.File ? Controller.formatByteSize(reply.content.info ? reply.content.info.size : 0) : Controller.formatDuration(reply.content.info.duration)
} }
} }
Component { Component {

View File

@@ -308,7 +308,7 @@ QQC2.ItemDelegate {
} }
height: active ? item.implicitHeight : 0 height: active ? item.implicitHeight : 0
//Layout.bottomMargin: readMarker ? Kirigami.Units.smallSpacing : 0 //Layout.bottomMargin: readMarker ? Kirigami.Units.smallSpacing : 0
active: eventType !== "state" && eventType !== "notice" && reaction != undefined && reaction.length > 0 active: eventType !== MessageEventModel.State && eventType !== MessageEventModel.Notice && reaction != undefined && reaction.length > 0
visible: active visible: active
sourceComponent: ReactionDelegate { } sourceComponent: ReactionDelegate { }
} }

View File

@@ -31,7 +31,7 @@ Loader {
currentRoom.chatBoxEditId = eventId; currentRoom.chatBoxEditId = eventId;
currentRoom.chatBoxReplyId = ""; currentRoom.chatBoxReplyId = "";
} }
visible: eventType.length > 0 && author.id === Controller.activeConnection.localUserId && (eventType === "emote" || eventType === "message") visible: eventType.length > 0 && author.id === Controller.activeConnection.localUserId && (eventType === MessageEventModel.Emote || eventType === MessageEventModel.Message)
}, },
Kirigami.Action { Kirigami.Action {
text: i18n("Reply") text: i18n("Reply")

View File

@@ -420,7 +420,7 @@ Kirigami.ScrollablePage {
id: hoverActions id: hoverActions
property var event: null property var event: null
property bool userMsg: event && event.author.id === Controller.activeConnection.localUserId property bool userMsg: event && event.author.id === Controller.activeConnection.localUserId
property bool showEdit: event && (userMsg && (event.eventType === "emote" || event.eventType === "message")) property bool showEdit: event && (userMsg && (event.eventType === MessageEventType.Emote || event.eventType === MessageEventModel.Message))
property var delegate: null property var delegate: null
property var bubble: null property var bubble: null
property var hovered: bubble && bubble.hovered property var hovered: bubble && bubble.hovered