Skip to content
Snippets Groups Projects
Commit 70c34e16 authored by Nicolas Werner's avatar Nicolas Werner
Browse files

Differentiate RoomList and Room

parent ad388ea7
No related branches found
No related tags found
No related merge requests found
......@@ -34,7 +34,8 @@ moc_files = qt5.preprocess(moc_headers :
'src/login.h',
'src/settings.h',
'src/sync.h',
'src/models/roommodel.h'
'src/models/roommodel.h',
'src/models/roomlistmodel.h'
],
moc_extra_arguments: defines, include_directories: inc,
dependencies: deps)
......@@ -46,7 +47,8 @@ sources = [
'src/login.cpp',
'src/settings.cpp',
'src/sync.cpp',
'src/models/roommodel.cpp'
'src/models/roommodel.cpp',
'src/models/roomlistmodel.cpp'
]
translations = qt5.compile_translations(ts_files : ['translations/harbour-spoon.ts', 'translations/harbour-spoon-de.ts'], build_by_default : true)
......
......@@ -8,7 +8,7 @@
#include "settings.h"
#include "sync.h"
#include "models/roommodel.h"
#include "models/roomlistmodel.h"
QDataStream &operator<<(QDataStream &out, const std::string &s) {
out << QString::fromStdString(s);
......@@ -45,11 +45,11 @@ int main(int argc, char *argv[]) {
Login login;
v->rootContext()->setContextProperty("login", &login);
RoomModel rooms;
RoomListModel rooms;
Sync sync(rooms, &login);
QObject::connect(&login, &Login::loginSuccess, &sync, &Sync::startSync, Qt::QueuedConnection);
QObject::connect(&sync, &Sync::newRoom, &rooms, &RoomModel::addRoom, Qt::QueuedConnection);
QObject::connect(&sync, &Sync::newInvite, &rooms, &RoomModel::addInvite, Qt::QueuedConnection);
QObject::connect(&sync, &Sync::newRoom, &rooms, &RoomListModel::addRoom, Qt::QueuedConnection);
QObject::connect(&sync, &Sync::newInvite, &rooms, &RoomListModel::addInvite, Qt::QueuedConnection);
QObject::connect(app.data(), &QGuiApplication::aboutToQuit, &sync, &Sync::stopSync, Qt::QueuedConnection);
QSharedPointer<Room> a = QSharedPointer<Room>(new Room);
......
#include "roomlistmodel.h"
#include <algorithm>
static auto byTag = [](QSharedPointer<Room> a, QSharedPointer<Room> b) { return a->tag < b->tag; };
void RoomListModel::addRoom(QSharedPointer<Room> room) {
auto pos = std::upper_bound(std::begin(joinedRooms), std::end(joinedRooms), room, byTag);
int posIdx = (int)invites.size() + std::distance(joinedRooms.begin(), pos);
beginInsertRows(QModelIndex(), posIdx, posIdx);
joinedRooms.insert(pos, room);
endInsertRows();
}
void RoomListModel::addInvite(QSharedPointer<Room> room) {
beginInsertRows(QModelIndex(), (int)invites.size(), (int)invites.size());
invites.push_back(room);
endInsertRows();
}
QHash<int, QByteArray> RoomListModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[RoomNameRole] = "roomName";
roles[UnreadNotificationsRole] = "unreadNotifications";
roles[LastMessageRole] = "lastMessage";
roles[RoomPictureRole] = "roomPicture";
roles[TagRole] = "tag";
return roles;
}
int RoomListModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return (int)invites.size() + joinedRooms.size();
}
QVariant RoomListModel::data(const QModelIndex &index, int role) const {
if (index.row() < 0 || index.row() >= rowCount())
return QVariant();
const auto &room = [this, index]() {
if (index.row() < (int)invites.size())
return invites[index.row()];
else
return joinedRooms[index.row() - invites.size()];
}();
switch (role) {
case RoomNameRole:
return QString::fromStdString(room->name);
case UnreadNotificationsRole:
return room->unreadNotifications;
case LastMessageRole:
return QString::fromStdString(room->lastMessage);
case RoomPictureRole:
return QString::fromStdString(room->picture);
case TagRole:
return QString::fromStdString(room->tag);
default:
return QVariant();
}
}
RoomListModel::RoomListModel(QObject *parent) { Q_UNUSED(parent); }
#ifndef ROOMMODEL_H
#define ROOMMODEL_H
#include <QAbstractListModel>
#include <QSharedPointer>
#include <map>
#include <string>
#include <vector>
#include "models/roommodel.h"
class RoomListModel : public QAbstractListModel {
Q_OBJECT
public:
enum RoomRoles {
RoomNameRole,
UnreadNotificationsRole,
LastMessageRole,
RoomPictureRole,
TagRole,
};
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
explicit RoomListModel(QObject *parent = 0);
public slots:
void addRoom(QSharedPointer<Room> room);
void addInvite(QSharedPointer<Room> room);
private:
std::vector<QSharedPointer<Room>> invites;
std::vector<QSharedPointer<Room>> joinedRooms;
};
#endif // ROOMMODEL_H
#include "roommodel.h"
#include <algorithm>
#include <QMetaType>
#include <QSharedPointer>
Q_DECLARE_METATYPE(Room);
Q_DECLARE_METATYPE(QSharedPointer<Room>);
static auto byTag = [](QSharedPointer<Room> a, QSharedPointer<Room> b) { return a->tag < b->tag; };
void RoomModel::addRoom(QSharedPointer<Room> room) {
auto pos = std::upper_bound(std::begin(joinedRooms), std::end(joinedRooms), room, byTag);
int posIdx = (int)invites.size() + std::distance(joinedRooms.begin(), pos);
beginInsertRows(QModelIndex(), posIdx, posIdx);
joinedRooms.insert(pos, room);
endInsertRows();
}
void RoomModel::addInvite(QSharedPointer<Room> room) {
beginInsertRows(QModelIndex(), (int)invites.size(), (int)invites.size());
invites.push_back(room);
endInsertRows();
}
QHash<int, QByteArray> RoomModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[RoomNameRole] = "roomName";
roles[UnreadNotificationsRole] = "unreadNotifications";
roles[LastMessageRole] = "lastMessage";
roles[RoomPictureRole] = "roomPicture";
roles[TagRole] = "tag";
return roles;
}
int RoomModel::rowCount(const QModelIndex &parent) const {
Q_UNUSED(parent);
return (int)invites.size() + joinedRooms.size();
}
QVariant RoomModel::data(const QModelIndex &index, int role) const {
if (index.row() < 0 || index.row() >= rowCount())
return QVariant();
const auto &room = [this, index]() {
if (index.row() < (int)invites.size())
return invites[index.row()];
else
return joinedRooms[index.row() - invites.size()];
}();
switch (role) {
case RoomNameRole:
return QString::fromStdString(room->name);
case UnreadNotificationsRole:
return room->unreadNotifications;
case LastMessageRole:
return QString::fromStdString(room->lastMessage);
case RoomPictureRole:
return QString::fromStdString(room->picture);
case TagRole:
return QString::fromStdString(room->tag);
default:
return QVariant();
}
}
RoomModel::RoomModel(QObject *parent) { Q_UNUSED(parent); }
#ifndef ROOMMODEL_H
#define ROOMMODEL_H
#pragma once
#include <QAbstractListModel>
#include <QSharedPointer>
#include <map>
#include <string>
#include <vector>
......@@ -12,31 +7,3 @@ struct Room {
std::string name, lastMessage, picture, tag;
int unreadNotifications = 0;
};
class RoomModel : public QAbstractListModel {
Q_OBJECT
public:
enum RoomRoles {
RoomNameRole,
UnreadNotificationsRole,
LastMessageRole,
RoomPictureRole,
TagRole,
};
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
RoomModel(QObject *parent = 0);
public slots:
void addRoom(QSharedPointer<Room> room);
void addInvite(QSharedPointer<Room> room);
private:
std::vector<QSharedPointer<Room>> invites;
std::vector<QSharedPointer<Room>> joinedRooms;
};
#endif // ROOMMODEL_H
......@@ -7,13 +7,13 @@
#include <QWaitCondition>
#include "login.h"
#include "models/roommodel.h"
#include "models/roomlistmodel.h"
class Sync : public QThread {
Q_OBJECT
public:
Sync(RoomModel &rooms, Login *login, QObject *parent = 0)
Sync(RoomListModel &rooms, Login *login, QObject *parent = 0)
: QThread(parent), rooms(rooms), stop(false), exit(false) {
connect(this, &Sync::nextSync, this, &Sync::sync);
}
......@@ -33,7 +33,7 @@ class Sync : public QThread {
void nextSync();
private:
RoomModel &rooms;
RoomListModel &rooms;
QWaitCondition condition;
QMutex mutex;
std::atomic<bool> stop, exit;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment