package main import ( "time" humanize "github.com/dustin/go-humanize" "github.com/therecipe/qt/core" ) // maxMessageCount defines the max amount of messages stored in a model const ( maxMessageCount = 150 ) // Model Roles const ( Name = int(core.Qt__UserRole) + 1<= len(m.Messages()) { return core.NewQVariant() } var p = m.Messages()[len(m.Messages())-1-index.Row()] switch role { case Name: { return core.NewQVariant14(p.Name) } case MessageID: { return core.NewQVariant14(p.MessageID) } case PostURL: { return core.NewQVariant14(p.PostURL) } case Author: { return core.NewQVariant14(p.Author) } case AuthorURL: { return core.NewQVariant14(p.AuthorURL) } case AuthorID: { return core.NewQVariant14(p.AuthorID) } case Avatar: { return core.NewQVariant14(p.Avatar) } case Body: { return core.NewQVariant14(p.Body) } case CreatedAt: { return core.NewQVariant14(humanize.Time(p.CreatedAt)) } case Actor: { return core.NewQVariant14(p.Actor) } case ActorName: { return core.NewQVariant14(p.ActorName) } case ActorID: { return core.NewQVariant14(p.ActorID) } case Reply: { return core.NewQVariant11(p.Reply) } case ReplyToID: { return core.NewQVariant14(p.ReplyToID) } case ReplyToAuthor: { return core.NewQVariant14(p.ReplyToAuthor) } case Forward: { return core.NewQVariant11(p.Forward) } case Mention: { return core.NewQVariant11(p.Mention) } case Like: { return core.NewQVariant11(p.Like) } case Followed: { return core.NewQVariant11(p.Followed) } case Following: { return core.NewQVariant11(p.Following) } case FollowedBy: { return core.NewQVariant11(p.FollowedBy) } case MediaPreview: { return core.NewQVariant14(p.MediaPreview) } case MediaURL: { return core.NewQVariant14(p.MediaURL) } case Editing: { return core.NewQVariant11(p.Editing) } case Liked: { return core.NewQVariant11(p.Liked) } case Shared: { return core.NewQVariant11(p.Shared) } default: { return core.NewQVariant() } } } func (m *MessageModel) rowCount(parent *core.QModelIndex) int { return len(m.Messages()) } func (m *MessageModel) columnCount(parent *core.QModelIndex) int { return 1 } func (m *MessageModel) roleNames() map[int]*core.QByteArray { return m.Roles() } func (m *MessageModel) clear() { m.BeginResetModel() m.SetMessages([]*Message{}) m.EndResetModel() } func (m *MessageModel) addMessage(p *Message) { m.BeginInsertRows(core.NewQModelIndex(), 0, 0) m.SetMessages(append(m.Messages(), p)) m.EndInsertRows() if len(m.Messages()) > maxMessageCount { m.removeMessage(len(m.Messages()) - 1) } } func (m *MessageModel) appendMessage(p *Message) { m.BeginInsertRows(core.NewQModelIndex(), len(m.Messages()), len(m.Messages())) m.SetMessages(append([]*Message{p}, m.Messages()...)) m.EndInsertRows() } func (m *MessageModel) removeMessage(row int) { trow := len(m.Messages()) - 1 - row m.BeginRemoveRows(core.NewQModelIndex(), row, row) m.SetMessages(append(m.Messages()[:trow], m.Messages()[trow+1:]...)) m.EndRemoveRows() } func (m *MessageModel) updateMessageTime() { if len(m.Messages()) > 0 { var fIndex = m.Index(0, 0, core.NewQModelIndex()) var lIndex = m.Index(len(m.Messages())-1, 0, core.NewQModelIndex()) m.DataChanged(fIndex, lIndex, []int{CreatedAt}) } } /* func (m *MessageModel) editMessage(row int, param string) { var p = m.Messages()[row] var pIndex = m.Index(row, 0, core.NewQModelIndex()) m.DataChanged(pIndex, pIndex, []int{roles}) } */ func init() { MessageModel_QRegisterMetaType() Message_QRegisterMetaType() }