Init UserListModel.

Another model from Quaternion.
Also UI tweaks in RoomListModel.
This commit is contained in:
Black Hat 2018-08-26 13:17:12 +08:00
parent 8c4ef9ad01
commit 0ad9ed7b5d
9 changed files with 287 additions and 50 deletions

View File

@ -26,7 +26,8 @@ SOURCES += src/main.cpp \
src/messageeventmodel.cpp \ src/messageeventmodel.cpp \
src/imageproviderconnection.cpp \ src/imageproviderconnection.cpp \
src/emojimodel.cpp \ src/emojimodel.cpp \
src/matriqueroom.cpp src/matriqueroom.cpp \
src/userlistmodel.cpp
RESOURCES += \ RESOURCES += \
res.qrc res.qrc
@ -86,4 +87,5 @@ HEADERS += \
src/messageeventmodel.h \ src/messageeventmodel.h \
src/imageproviderconnection.h \ src/imageproviderconnection.h \
src/emojimodel.h \ src/emojimodel.h \
src/matriqueroom.h src/matriqueroom.h \
src/userlistmodel.h

View File

@ -6,7 +6,7 @@ import QtQuick.Controls.Material 2.2
import Qt.labs.settings 1.0 import Qt.labs.settings 1.0
import Matrique.Settings 0.1 import Matrique.Settings 0.1
import "qrc:/qml/component" import "component"
Page { Page {
property var controller property var controller

View File

@ -4,7 +4,7 @@ import QtQuick.Layouts 1.3
import Matrique 0.1 import Matrique 0.1
import Matrique.Settings 0.1 import Matrique.Settings 0.1
import "qrc:/qml/form" import "form"
Page { Page {
property alias connection: roomListModel.connection property alias connection: roomListModel.connection

View File

@ -56,7 +56,9 @@ Item {
function getInitials(text) { function getInitials(text) {
if (!text) return "N" if (!text) return "N"
return text.toUpperCase().replace(/[^a-zA-Z- ]/g, "").match(/\b\w/g); var initial = text.toUpperCase().replace(/[^a-zA-Z- ]/g, "").match(/\b\w/g);
if (!initial) return "N"
return initial
} }
function stringToColor(str) { function stringToColor(str) {

View File

@ -7,7 +7,7 @@ import QtGraphicalEffects 1.0
import Matrique 0.1 import Matrique 0.1
import Matrique.Settings 0.1 import Matrique.Settings 0.1
import "qrc:/qml/component" import "../component"
import "qrc:/js/md.js" as Markdown import "qrc:/js/md.js" as Markdown
Item { Item {
@ -15,6 +15,12 @@ Item {
id: item id: item
UserListModel {
id: userListModel
room: currentRoom
}
Drawer { Drawer {
id: roomDrawer id: roomDrawer
@ -30,34 +36,34 @@ Item {
onClicked: roomDrawer.close() onClicked: roomDrawer.close()
} }
Column { ColumnLayout {
anchors.fill: parent anchors.fill: parent
anchors.margins: 32 anchors.margins: 32
spacing: 16 spacing: 16
ImageStatus { ImageStatus {
width: 64 Layout.preferredWidth: 64
height: 64 Layout.preferredHeight: 64
anchors.horizontalCenter: parent.horizontalCenter Layout.alignment: Qt.AlignHCenter
source: currentRoom && currentRoom.avatarUrl != "" ? "image://mxc/" + currentRoom.avatarUrl : null source: currentRoom && currentRoom.avatarUrl != "" ? "image://mxc/" + currentRoom.avatarUrl : null
displayText: currentRoom ? currentRoom.displayName : "" displayText: currentRoom ? currentRoom.displayName : ""
} }
Label { Label {
width: parent.width Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
text: currentRoom && currentRoom.id ? currentRoom.id : "" text: currentRoom && currentRoom.id ? currentRoom.id : ""
} }
Label { Label {
width: parent.width Layout.fillWidth: true
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
text: currentRoom && currentRoom.canonicalAlias ? currentRoom.canonicalAlias : "No Canonical Alias" text: currentRoom && currentRoom.canonicalAlias ? currentRoom.canonicalAlias : "No Canonical Alias"
} }
RowLayout { RowLayout {
width: parent.width Layout.fillWidth: true
TextField { TextField {
id: roomNameField id: roomNameField
@ -67,7 +73,7 @@ Item {
ItemDelegate { ItemDelegate {
Layout.preferredWidth: height Layout.preferredWidth: height
Layout.fillHeight: true Layout.preferredHeight: parent.height
contentItem: MaterialIcon { icon: "\ue5ca" } contentItem: MaterialIcon { icon: "\ue5ca" }
@ -76,7 +82,7 @@ Item {
} }
RowLayout { RowLayout {
width: parent.width Layout.fillWidth: true
TextField { TextField {
id: roomTopicField id: roomTopicField
@ -87,13 +93,51 @@ Item {
ItemDelegate { ItemDelegate {
Layout.preferredWidth: height Layout.preferredWidth: height
Layout.fillHeight: true Layout.preferredHeight: parent.height
contentItem: MaterialIcon { icon: "\ue5ca" } contentItem: MaterialIcon { icon: "\ue5ca" }
onClicked: currentRoom.setTopic(roomTopicField.text) onClicked: currentRoom.setTopic(roomTopicField.text)
} }
} }
ListView {
Layout.fillWidth: true
Layout.fillHeight: true
clip: true
boundsBehavior: Flickable.DragOverBounds
delegate: ItemDelegate {
width: parent.width
height: 48
RowLayout {
anchors.fill: parent
anchors.margins: 8
spacing: 16
ImageStatus {
Layout.preferredWidth: height
Layout.fillHeight: true
source: avatar != "" ? "image://mxc/" + avatar : ""
displayText: name
}
Label {
Layout.fillWidth: true
text: name
}
}
}
model: userListModel
ScrollBar.vertical: ScrollBar {}
}
} }
} }
@ -119,11 +163,10 @@ Item {
color: MSettings.darkTheme ? "#242424" : "#eaeaea" color: MSettings.darkTheme ? "#242424" : "#eaeaea"
MouseArea { ItemDelegate {
anchors.fill: parent anchors.fill: parent
onClicked: roomDrawer.open() onClicked: roomDrawer.open()
}
RowLayout { RowLayout {
anchors.fill: parent anchors.fill: parent
@ -166,6 +209,7 @@ Item {
} }
} }
} }
}
RowLayout { RowLayout {
Layout.fillWidth: true Layout.fillWidth: true

View File

@ -8,7 +8,7 @@ import Matrique 0.1
import SortFilterProxyModel 0.2 import SortFilterProxyModel 0.2
import Matrique.Settings 0.1 import Matrique.Settings 0.1
import "qrc:/qml/component" import "../component"
Item { Item {
property alias listModel: roomListProxyModel.sourceModel property alias listModel: roomListProxyModel.sourceModel
@ -115,11 +115,13 @@ Item {
ScrollBar.vertical: ScrollBar { id: scrollBar } ScrollBar.vertical: ScrollBar { id: scrollBar }
delegate: Rectangle { delegate: Rectangle {
readonly property bool highlighted: currentRoom === enteredRoom
id: swipeDelegate id: swipeDelegate
width: parent.width width: parent.width
height: 80 height: 80
color: currentRoom === enteredRoom ? Material.background : "transparent" color: highlighted ? Material.background : "transparent"
AutoMouseArea { AutoMouseArea {
anchors.fill: parent anchors.fill: parent
@ -137,7 +139,7 @@ Item {
width: 4 width: 4
height: parent.height height: parent.height
color: Qt.tint(Material.accent, "#20FFFFFF") color: Qt.tint(Material.accent, "#20FFFFFF")
visible: unreadCount > 0 visible: unreadCount > 0 || highlighted
} }
RowLayout { RowLayout {

View File

@ -10,6 +10,7 @@
#include "messageeventmodel.h" #include "messageeventmodel.h"
#include "room.h" #include "room.h"
#include "roomlistmodel.h" #include "roomlistmodel.h"
#include "userlistmodel.h"
#include "csapi/joining.h" #include "csapi/joining.h"
#include "csapi/leaving.h" #include "csapi/leaving.h"
@ -36,6 +37,7 @@ int main(int argc, char *argv[]) {
qmlRegisterType<Controller>("Matrique", 0, 1, "Controller"); qmlRegisterType<Controller>("Matrique", 0, 1, "Controller");
qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel"); qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel");
qmlRegisterType<UserListModel>("Matrique", 0, 1, "UserListModel");
qmlRegisterType<MessageEventModel>("Matrique", 0, 1, "MessageEventModel"); qmlRegisterType<MessageEventModel>("Matrique", 0, 1, "MessageEventModel");
qmlRegisterType<EmojiModel>("Matrique", 0, 1, "EmojiModel"); qmlRegisterType<EmojiModel>("Matrique", 0, 1, "EmojiModel");
qmlRegisterUncreatableType<RoomMessageEvent>("Matrique", 0, 1, qmlRegisterUncreatableType<RoomMessageEvent>("Matrique", 0, 1,

129
src/userlistmodel.cpp Normal file
View File

@ -0,0 +1,129 @@
#include "userlistmodel.h"
#include <QElapsedTimer>
#include <QtCore/QDebug>
#include <QtGui/QPixmap>
#include <connection.h>
#include <room.h>
#include <user.h>
UserListModel::UserListModel(QObject* parent)
: QAbstractListModel(parent), m_currentRoom(nullptr) {}
void UserListModel::setRoom(QMatrixClient::Room* room) {
if (m_currentRoom == room) return;
using namespace QMatrixClient;
beginResetModel();
if (m_currentRoom) {
m_currentRoom->connection()->disconnect(this);
m_currentRoom->disconnect(this);
for (User* user : m_users) user->disconnect(this);
m_users.clear();
}
m_currentRoom = room;
if (m_currentRoom) {
connect(m_currentRoom, &Room::userAdded, this, &UserListModel::userAdded);
connect(m_currentRoom, &Room::userRemoved, this,
&UserListModel::userRemoved);
connect(m_currentRoom, &Room::memberAboutToRename, this,
&UserListModel::userRemoved);
connect(m_currentRoom, &Room::memberRenamed, this,
&UserListModel::userAdded);
{
QElapsedTimer et;
et.start();
m_users = m_currentRoom->users();
std::sort(m_users.begin(), m_users.end(), room->memberSorter());
qDebug() << "Sorting" << m_users.size() << "user(s) in"
<< m_currentRoom->displayName() << "took" << et;
}
for (User* user : m_users) {
connect(user, &User::avatarChanged, this, &UserListModel::avatarChanged);
}
connect(m_currentRoom->connection(), &Connection::loggedOut, this,
[=] { setRoom(nullptr); });
qDebug() << m_users.count() << "user(s) in the room";
}
endResetModel();
emit roomChanged();
}
QMatrixClient::User* UserListModel::userAt(QModelIndex index) {
if (index.row() < 0 || index.row() >= m_users.size()) return nullptr;
return m_users.at(index.row());
}
QVariant UserListModel::data(const QModelIndex& index, int role) const {
if (!index.isValid()) return QVariant();
if (index.row() >= m_users.count()) {
qDebug()
<< "UserListModel, something's wrong: index.row() >= m_users.count()";
return QVariant();
}
auto user = m_users.at(index.row());
if (role == NameRole) {
return user->displayname(m_currentRoom);
}
if (role == AvatarRole) {
return user->avatarUrl(m_currentRoom);
}
return QVariant();
}
int UserListModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) return 0;
return m_users.count();
}
void UserListModel::userAdded(QMatrixClient::User* user) {
auto pos = findUserPos(user);
beginInsertRows(QModelIndex(), pos, pos);
m_users.insert(pos, user);
endInsertRows();
connect(user, &QMatrixClient::User::avatarChanged, this,
&UserListModel::avatarChanged);
}
void UserListModel::userRemoved(QMatrixClient::User* user) {
auto pos = findUserPos(user);
if (pos != m_users.size()) {
beginRemoveRows(QModelIndex(), pos, pos);
m_users.removeAt(pos);
endRemoveRows();
user->disconnect(this);
} else
qWarning() << "Trying to remove a room member not in the user list";
}
void UserListModel::refresh(QMatrixClient::User* user, QVector<int> roles) {
auto pos = findUserPos(user);
if (pos != m_users.size())
emit dataChanged(index(pos), index(pos), roles);
else
qWarning() << "Trying to access a room member not in the user list";
}
void UserListModel::avatarChanged(QMatrixClient::User* user,
const QMatrixClient::Room* context) {
if (context == m_currentRoom) refresh(user, {Qt::DecorationRole});
}
int UserListModel::findUserPos(User* user) const {
return findUserPos(m_currentRoom->roomMembername(user));
}
int UserListModel::findUserPos(const QString& username) const {
return m_currentRoom->memberSorter().lowerBoundIndex(m_users, username);
}
QHash<int, QByteArray> UserListModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[AvatarRole] = "avatar";
return roles;
}

56
src/userlistmodel.h Normal file
View File

@ -0,0 +1,56 @@
#ifndef USERLISTMODEL_H
#define USERLISTMODEL_H
#include "room.h"
#include <QObject>
#include <QtCore/QAbstractListModel>
namespace QMatrixClient {
class Connection;
class Room;
class User;
} // namespace QMatrixClient
class UserListModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(
QMatrixClient::Room* room READ room WRITE setRoom NOTIFY roomChanged)
public:
enum EventRoles {
NameRole = Qt::UserRole + 1,
AvatarRole
};
using User = QMatrixClient::User;
UserListModel(QObject* parent = nullptr);
QMatrixClient::Room* room() { return m_currentRoom; }
void setRoom(QMatrixClient::Room* room);
User* userAt(QModelIndex index);
QVariant data(const QModelIndex& index,
int role = NameRole) const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QHash<int, QByteArray> roleNames() const override;
signals:
void roomChanged();
private slots:
void userAdded(User* user);
void userRemoved(User* user);
void refresh(User* user, QVector<int> roles = {});
void avatarChanged(User* user, const QMatrixClient::Room* context);
private:
QMatrixClient::Room* m_currentRoom;
QList<User*> m_users;
int findUserPos(User* user) const;
int findUserPos(const QString& username) const;
};
#endif // USERLISTMODEL_H