From cbd5974d551cc5746963341798185e4f89d3a5c1 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Thu, 15 Mar 2018 17:10:27 +0800 Subject: [PATCH] Add ChatRoom.qml and clean up code. --- main.cpp | 2 + matrix/controller.h | 1 + matrix/imageprovider.cpp | 7 ++-- matrix/matriqueroom.cpp | 3 +- matrix/messageeventmodel.cpp | 7 ++-- matrix/roomlistmodel.cpp | 7 +++- qml/component/ChatRoom.qml | 76 ++++++++++++++++++++++++++++++++++++ res.qrc | 1 + 8 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 qml/component/ChatRoom.qml diff --git a/main.cpp b/main.cpp index 767e0de..f923d86 100644 --- a/main.cpp +++ b/main.cpp @@ -6,6 +6,7 @@ #include "matrix/controller.h" #include "matrix/roomlistmodel.h" #include "matrix/imageprovider.h" +#include "matrix/messageeventmodel.h" using namespace QMatrixClient; @@ -26,6 +27,7 @@ int main(int argc, char *argv[]) qmlRegisterType("Matrique", 0, 1, "Controller"); qmlRegisterType("Matrique", 0, 1, "RoomListModel"); + qmlRegisterType("Matrique", 0, 1, "MessageEventModel"); QQmlApplicationEngine engine; diff --git a/matrix/controller.h b/matrix/controller.h index d6e6389..a1da594 100644 --- a/matrix/controller.h +++ b/matrix/controller.h @@ -4,6 +4,7 @@ #include #include "libqmatrixclient/connection.h" + #include "roomlistmodel.h" namespace QMatrixClient { diff --git a/matrix/imageprovider.cpp b/matrix/imageprovider.cpp index 09a02bd..350a014 100644 --- a/matrix/imageprovider.cpp +++ b/matrix/imageprovider.cpp @@ -1,12 +1,13 @@ #include "imageprovider.h" -#include "connection.h" -#include "jobs/mediathumbnailjob.h" - #include #include #include +#include "jobs/mediathumbnailjob.h" + +#include "connection.h" + using QMatrixClient::MediaThumbnailJob; ImageProvider::ImageProvider(QObject *parent) diff --git a/matrix/matriqueroom.cpp b/matrix/matriqueroom.cpp index 168f798..0672de2 100644 --- a/matrix/matriqueroom.cpp +++ b/matrix/matriqueroom.cpp @@ -1,8 +1,9 @@ #include "matriqueroom.h" -#include "user.h" #include "events/roommessageevent.h" +#include "user.h" + using namespace QMatrixClient; MatriqueRoom::MatriqueRoom(Connection* connection, QString roomId, diff --git a/matrix/messageeventmodel.cpp b/matrix/messageeventmodel.cpp index 48294e2..522b77d 100644 --- a/matrix/messageeventmodel.cpp +++ b/matrix/messageeventmodel.cpp @@ -4,13 +4,14 @@ #include #include // for qmlRegisterType() -#include "connection.h" -#include "user.h" -#include "settings.h" #include "events/roommemberevent.h" #include "events/simplestateevents.h" #include "events/redactionevent.h" +#include "connection.h" +#include "user.h" +#include "settings.h" + QHash MessageEventModel::roleNames() const { QHash roles = QAbstractItemModel::roleNames(); diff --git a/matrix/roomlistmodel.cpp b/matrix/roomlistmodel.cpp index dc511a1..ccdb993 100644 --- a/matrix/roomlistmodel.cpp +++ b/matrix/roomlistmodel.cpp @@ -1,10 +1,11 @@ #include "roomlistmodel.h" + +#include + #include "matriqueroom.h" #include "connection.h" #include "user.h" -#include - RoomListModel::RoomListModel(QObject* parent) : QAbstractListModel(parent) { } @@ -165,9 +166,11 @@ QVariant RoomListModel::data(const QModelIndex& index, int role) const // return QIcon(":/irc-channel-parted.svg"); // } if(room->avatarUrl().toString() != "") { + qInfo() << "Room avatar:" << room->avatarUrl(); return room->avatarUrl(); } else if(room->users().length() == 2) { QMatrixClient::User* user = room->users().at(0); + qInfo() << "User avatar:" << user->avatarUrl(); return user->avatarUrl(); } } diff --git a/qml/component/ChatRoom.qml b/qml/component/ChatRoom.qml new file mode 100644 index 0000000..8bb038f --- /dev/null +++ b/qml/component/ChatRoom.qml @@ -0,0 +1,76 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +Rectangle { + id: root + + property Connection currentConnection: null + property var currentRoom: null + + function setRoom(room) { + currentRoom = room + messageModel.changeRoom(room) + } + + function setConnection(conn) { + currentConnection = conn + messageModel.setConnection(conn) + } + + function sendLine(text) { + if(!currentRoom || !currentConnection) return + currentConnection.postMessage(currentRoom, "m.text", text) + } + + ListView { + id: chatView + anchors.fill: parent + flickableDirection: Flickable.VerticalFlick + verticalLayoutDirection: ListView.BottomToTop + model: MessageEventModel { id: messageModel } + + delegate: Row { + id: message + width: parent.width + spacing: 8 + + Label { + id: timelabel + text: time.toLocaleTimeString("hh:mm:ss") + color: "grey" + } + Label { + width: 64 + elide: Text.ElideRight + text: eventType == "message" ? author : "***" + color: eventType == "message" ? "grey" : "lightgrey" + horizontalAlignment: Text.AlignRight + } + Label { + text: content + wrapMode: Text.Wrap + width: parent.width - (x - parent.x) - spacing + color: eventType == "message" ? "black" : "lightgrey" + } + } + + section { + property: "date" + labelPositioning: ViewSection.CurrentLabelAtStart + delegate: Rectangle { + width: parent.width + height: childrenRect.height + Label { + width: parent.width + text: section.toLocaleString(Qt.locale()) + color: "grey" + horizontalAlignment: Text.AlignRight + } + } + } + + onAtYBeginningChanged: { + if(currentRoom && atYBeginning) currentRoom.getPreviousContent() + } + } +} diff --git a/res.qrc b/res.qrc index 3101981..95b3e9c 100644 --- a/res.qrc +++ b/res.qrc @@ -15,5 +15,6 @@ qml/form/DetailForm.qml qml/form/ListForm.qml qml/Contact.qml + qml/component/ChatRoom.qml