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

Initial work on message event model.

parent a23e7f72
...@@ -18,6 +18,7 @@ func main() { ...@@ -18,6 +18,7 @@ func main() {
ui.Controller_QmlRegisterType2("Matrique", 0, 1, "Controller") ui.Controller_QmlRegisterType2("Matrique", 0, 1, "Controller")
ui.RoomListModel_QmlRegisterType2("Matrique", 0, 1, "RoomListModel") ui.RoomListModel_QmlRegisterType2("Matrique", 0, 1, "RoomListModel")
ui.MessageEventModel_QmlRegisterType2("Matrique", 0, 1, "MessageEventModel")
var app = qml.NewQQmlApplicationEngine(nil) var app = qml.NewQQmlApplicationEngine(nil)
......
...@@ -6,9 +6,9 @@ import ( ...@@ -6,9 +6,9 @@ import (
) )
type Message struct { type Message struct {
eventID string EventID string
event *gomatrix.Event Event *gomatrix.Event
authorID string AuthorID string
text string Text string
timestamp *time.Time Timestamp *time.Time
} }
...@@ -3,9 +3,11 @@ import QtQuick.Controls 2.3 ...@@ -3,9 +3,11 @@ import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3 import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.3 import QtQuick.Controls.Material 2.3
import QtGraphicalEffects 1.0 import QtGraphicalEffects 1.0
import Matrique 0.1
import "qrc:/qml/component" import "qrc:/qml/component"
Item { Item {
id: item
property var currentRoom property var currentRoom
Pane { Pane {
...@@ -86,7 +88,7 @@ Item { ...@@ -86,7 +88,7 @@ Item {
displayMarginEnd: 40 displayMarginEnd: 40
verticalLayoutDirection: ListView.BottomToTop verticalLayoutDirection: ListView.BottomToTop
spacing: 12 spacing: 12
model: 10 model: MessageEventModel{ currentRoom: item.currentRoom }
delegate: Row { delegate: Row {
readonly property bool sentByMe: index % 2 == 0 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 package ui
//import "github.com/therecipe/qt/core" import (
// "github.com/therecipe/qt/core"
//type MessageEventModel struct { "gitlab.com/b0/matrique-go/matrix"
// core.QAbstractListModel "log"
// )
// _ *Controller `property:"controller"`
// type MessageEventModel struct {
// _ func() `constructor:"init"` core.QAbstractListModel
//
// _ *Room `property:"currentRoom"` _ *Controller `property:"controller"`
//
// _ map[int]*core.QByteArray `property:"roles"` _ func() `constructor:"init"`
// _ []*Message `property:"message"`
// _ *Room `property:"currentRoom"`
// _ func(*[]*Message) `signal:"doResetModel"`
// _ map[int]*core.QByteArray `property:"roles"`
// _ func(int) *Message `slot:"messageAt"` _ []*Message `property:"message"`
// _ func(*Message) `slot:"addMessage"`
// _ func(row int) `slot:"removeMessage"` _ func(interface{}) `signal:"doResetModel"`
//}
// _ func(int) *Message `slot:"messageAt"`
//func (m *MessageEventModel) init() { _ func(*Message) `slot:"addMessage"`
// m.SetRoles(map[int]*core.QByteArray{ _ func(row int) `slot:"removeMessage"`
// ID: core.NewQByteArray2("id", len("id")), }
// Name: core.NewQByteArray2("name", len("name")),
// Alias: core.NewQByteArray2("alias", len("alias")), func (m *MessageEventModel) init() {
// Topic: core.NewQByteArray2("topic", len("topic")), m.SetRoles(map[int]*core.QByteArray{
// Avatar: core.NewQByteArray2("avatar", len("avatar")), 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 ( ...@@ -10,6 +10,11 @@ const (
Avatar Avatar
) )
const (
Text = int(core.Qt__DisplayRole) + 1<<iota
AuthorID
)
func checkErr(err error) { func checkErr(err error) {
if err != nil { if err != nil {
panic(err) 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