Commit 5e424268 authored by Black Hat's avatar Black Hat

Initial work on message event model.

parent a23e7f72
......@@ -18,6 +18,7 @@ func main() {
ui.Controller_QmlRegisterType2("Matrique", 0, 1, "Controller")
ui.RoomListModel_QmlRegisterType2("Matrique", 0, 1, "RoomListModel")
ui.MessageEventModel_QmlRegisterType2("Matrique", 0, 1, "MessageEventModel")
var app = qml.NewQQmlApplicationEngine(nil)
......
......@@ -6,9 +6,9 @@ import (
)
type Message struct {
eventID string
event *gomatrix.Event
authorID string
text string
timestamp *time.Time
EventID string
Event *gomatrix.Event
AuthorID string
Text string
Timestamp *time.Time
}
......@@ -3,9 +3,11 @@ import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.3
import QtGraphicalEffects 1.0
import Matrique 0.1
import "qrc:/qml/component"
Item {
id: item
property var currentRoom
Pane {
......@@ -86,7 +88,7 @@ Item {
displayMarginEnd: 40
verticalLayoutDirection: ListView.BottomToTop
spacing: 12
model: 10
model: MessageEventModel{ currentRoom: item.currentRoom }
delegate: Row {
readonly property bool sentByMe: index % 2 == 0
......
package ui
import (
"github.com/therecipe/qt/core"
"gitlab.com/b0/matrique-go/matrix"
)
type Message struct {
core.QObject
matrix.Message
_ string `property:"authorID"`
_ string `property:"text"`
}
func (m *Message) Init() {
m.ConnectText(func() string { return m.Message.Text })
m.ConnectSetText(func(text string) { m.Message.Text = text })
m.ConnectAuthorID(func() string { return m.Message.AuthorID })
m.ConnectSetAuthorID(func(authorID string) { m.Message.AuthorID = authorID })
}
func (m *Message) FromMessage(msg *matrix.Message) {
m.SetText(msg.Text)
m.SetAuthorID(msg.AuthorID)
}
package ui
//import "github.com/therecipe/qt/core"
//
//type MessageEventModel struct {
// core.QAbstractListModel
//
// _ *Controller `property:"controller"`
//
// _ func() `constructor:"init"`
//
// _ *Room `property:"currentRoom"`
//
// _ map[int]*core.QByteArray `property:"roles"`
// _ []*Message `property:"message"`
//
// _ func(*[]*Message) `signal:"doResetModel"`
//
// _ func(int) *Message `slot:"messageAt"`
// _ func(*Message) `slot:"addMessage"`
// _ func(row int) `slot:"removeMessage"`
//}
//
//func (m *MessageEventModel) init() {
// m.SetRoles(map[int]*core.QByteArray{
// ID: core.NewQByteArray2("id", len("id")),
// Name: core.NewQByteArray2("name", len("name")),
// Alias: core.NewQByteArray2("alias", len("alias")),
// Topic: core.NewQByteArray2("topic", len("topic")),
// Avatar: core.NewQByteArray2("avatar", len("avatar")),
// })
//}
//
import (
"github.com/therecipe/qt/core"
"gitlab.com/b0/matrique-go/matrix"
"log"
)
type MessageEventModel struct {
core.QAbstractListModel
_ *Controller `property:"controller"`
_ func() `constructor:"init"`
_ *Room `property:"currentRoom"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*Message `property:"message"`
_ func(interface{}) `signal:"doResetModel"`
_ func(int) *Message `slot:"messageAt"`
_ func(*Message) `slot:"addMessage"`
_ func(row int) `slot:"removeMessage"`
}
func (m *MessageEventModel) init() {
m.SetRoles(map[int]*core.QByteArray{
Text: core.NewQByteArray2("text", len("text")),
AuthorID: core.NewQByteArray2("authorID", len("authorID")),
})
m.ConnectData(m.data)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectRoleNames(m.roleNames)
m.ConnectMessageAt(m.messageAt)
m.ConnectDoResetModel(m.resetModel)
m.ConnectAddMessage(m.addMessage)
m.ConnectRemoveMessage(m.removeMessage)
m.ConnectCurrentRoomChanged(func(currentRoom *Room) {
log.Println("GO: room changed.")
})
}
func (m *MessageEventModel) resetModel(msgsInterface interface{}) {
msgs := msgsInterface.([]*matrix.Message)
log.Println("Begin resetting roomlistmodel.")
m.BeginResetModel()
var qmlMsgs []*Message
for _, msg := range msgs {
qmlMsg := NewMessage(nil)
qmlMsg.FromMessage(msg)
qmlMsgs = append(qmlMsgs, qmlMsg)
}
m.SetMessage(qmlMsgs)
m.EndResetModel()
log.Println("Reset complete.")
}
func (m *MessageEventModel) messageAt(row int) *Message {
return m.Message()[row]
}
func (m *MessageEventModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(m.Message()) {
return core.NewQVariant()
}
var r = m.Message()[index.Row()]
switch role {
case Text:
return core.NewQVariant14(r.Text())
case Name:
return core.NewQVariant14(r.AuthorID())
default:
return core.NewQVariant()
}
}
func (m *MessageEventModel) rowCount(parent *core.QModelIndex) int {
return len(m.Message())
}
func (m *MessageEventModel) columnCount(parent *core.QModelIndex) int {
return 1
}
func (m *MessageEventModel) roleNames() map[int]*core.QByteArray {
return m.Roles()
}
func (m *MessageEventModel) addMessage(msg *Message) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.Message()), len(m.Message()))
m.SetMessage(append(m.Message(), msg))
m.EndInsertRows()
}
func (m *MessageEventModel) removeMessage(row int) {
m.BeginRemoveRows(core.NewQModelIndex(), row, row)
m.SetMessage(append(m.Message()[:row], m.Message()[row+1:]...))
m.EndRemoveRows()
}
......@@ -10,6 +10,11 @@ const (
Avatar
)
const (
Text = int(core.Qt__DisplayRole) + 1<<iota
AuthorID
)
func checkErr(err error) {
if err != nil {
panic(err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment