add table view

This commit is contained in:
Andrei Yankovich 2020-10-31 19:07:28 +03:00
parent 228e31678f
commit 1cf6da31aa
7 changed files with 196 additions and 23 deletions

View File

@ -13,6 +13,7 @@
#include "gamestate.h"
#include <QQmlApplicationEngine>
#include <lvmainmodel.h>
#include <recordlistmodel.h>
constexpr unsigned char currentVersion = 6;
@ -38,10 +39,12 @@ BackEnd::BackEnd(QQmlApplicationEngine *engine):
connect(&_client, &HanoiClient::statusChanged,
this, &BackEnd::handleLogined);
_loginModel = new LoginView::LVMainModel("userLogin");
_loginModel = new LoginView::LVMainModel("userLogin", this);
_loginModel->setComponents(LoginView::Nickname);
_loginModel->init(engine);
_recordsTable = new RecordListModel(this);
connect(_loginModel , &LoginView::LVMainModel::sigLoginRequest,
this, &BackEnd::handleOnlineRequest);
@ -264,7 +267,7 @@ QString BackEnd::profile() const {
return _profile;
}
QStringList BackEnd::profileList() {
QObject* BackEnd::profileList() {
return _profileList.keys();
}

View File

@ -26,6 +26,7 @@ class LVMainModel;
class UserData;
}
class QQmlApplicationEngine;
class RecordListModel;
class BackEnd: public QObject
{
@ -36,9 +37,8 @@ class BackEnd: public QObject
Q_PROPERTY(QObject* gameState READ gameState)
Q_PROPERTY(QObject* client READ client)
Q_PROPERTY(QObject* profileObject READ profileObject NOTIFY profileChanged)
// Q_PROPERTY(QObject* usersListModel READ usersListModel NOTIFY usersListModelChanged)
Q_PROPERTY(QStringList profileList READ profileList NOTIFY profileListChanged)
Q_PROPERTY(QObject* profileList READ profileList NOTIFY profileListChanged)
Q_PROPERTY(QString profile READ profile WRITE setProfile NOTIFY profileChanged)
@ -50,8 +50,8 @@ private:
QuasarAppUtils::Settings *_settings = nullptr;
LoginView::LVMainModel *_loginModel = nullptr;
QHash<QString, ProfileData*> _profileList;
// ViewSolutions::ListViewModel *_usersList = nullptr;
RecordListModel * _recordsTable = nullptr;
QString _profile;
HanoiClient _client;
@ -67,7 +67,7 @@ public:
~BackEnd();
Q_INVOKABLE QString profile() const;
Q_INVOKABLE QStringList profileList();
Q_INVOKABLE QObject* profileList();
Q_INVOKABLE void createProfile(const QString& userName, bool isOnlineuser);
@ -140,15 +140,6 @@ public slots:
*/
QObject* profileObject() const;
// /**
// * @brief usersListModel
// * @return
// */
// QObject *usersListModel() const;
bool isOnline(const QString& name);
int record(const QString& name);
void removeUser(const QString& name);
void setOnline(const QString& name, bool online);

View File

@ -0,0 +1,51 @@
import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Controls.Material 2.15
import "./../base" as Base
Item {
property bool selected: false
signal removedRow();
signal onlineRowChanged(var online);
signal clicked();
Base.Theme{
id: theme;
}
Rectangle {
color: (selected)? "#3700f991" : "#0000f991"
anchors.fill: parent;
Behavior on color {
ColorAnimation {
duration: 500
}
}
}
Switch {
text: qsTr("Online user")
position: online
onPositionChanged: {
onlineRowChanged(Boolean(position));
}
ToolTip.visible: pressed || hovered
ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
ToolTip.timeout: 10000
ToolTip.text: qsTr("If this option is enabled, your saves and the record will be saved on the network.")
visible: online >= 0
}
Text {
text: display
anchors.fill: parent;
}
}

View File

@ -47,7 +47,7 @@ Item {
}
}
ListView {
TableView {
id: listView
Layout.fillWidth: true
@ -56,12 +56,8 @@ Item {
clip: true
model: backEnd.profileList
delegate: UserTableRow {
name: modelData
online: backEnd.isOnline(modelData)
record: backEnd.record(modelData)
width: listView.width
recordLength: button.width - gridLayout.rowSpacing
delegate: UserTableDelegate {
selected: modelData == backEnd.profile;
onRemovedRow: {
backEnd.removeUser(modelData)

View File

@ -22,6 +22,7 @@
<file>menu/UsersTable.qml</file>
<file>menu/UserTableRow.qml</file>
<file>menu/ToolBar.qml</file>
<file>menu/UserTableDelegate.qml</file>
</qresource>
<qresource prefix="/textures">
<file>res/wood.png</file>

View File

@ -0,0 +1,88 @@
#include "recordlistmodel.h"
RecordListModel::RecordListModel(QObject *parent):
QAbstractTableModel(parent) {
}
int RecordListModel::rowCount(const QModelIndex &) const {
return _data.size();
}
int RecordListModel::columnCount(const QModelIndex &) const {
return 3;
}
QVariant RecordListModel::data(const QModelIndex &index, int role) const {
if (rowCount(index) <= index.row()) {
return {};
}
if (columnCount(index) <= index.row()) {
return {};
}
auto iten = _data.begin() + index.row();
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 0: {
return iten.key();
}
case 1: {
return iten.value().record;
}
default:
return "This column not supported of the RecordListModel";
}
}
if (role == RecordListModel::RecordListModelRoles::OnlineRole) {
if (index.column() == 2) {
return iten.value().online;
}
return -1;
}
return QAbstractTableModel::data(index, role);
}
void RecordListModel::setSource(const QMap<QString, userPreview> &data) {
beginResetModel();
_data = data;
endResetModel();
}
void RecordListModel::updateSourceInte(const userPreview &data) {
int row = std::distance(_data.begin(), _data.find(data.UserName));
if (row >= 0) {
emit dataChanged(index(row, 0), index(row, 3));
} else {
beginInsertRows({}, _data.size(), _data.size());
_data.insert(data.UserName, data);
endInsertRows();
}
}
void RecordListModel::removeSourceItem(const QString &name) {
int row = std::distance(_data.begin(), _data.find(name));
if (row < 0)
return;
beginRemoveRows({}, row, row);
_data.remove(name);
endRemoveRows();
}
QHash<int, QByteArray> RecordListModel::roleNames() const {
auto hash = QAbstractTableModel::roleNames();
hash[RecordListModel::RecordListModelRoles::OnlineRole] = "online";
hash[RecordListModel::RecordListModelRoles::RemoveRole] = "remove";
return hash;
}

View File

@ -0,0 +1,43 @@
#ifndef RECORDLISTMODEL_H
#define RECORDLISTMODEL_H
#include <QAbstractTableModel>
#include <QMap>
#include <qqml.h>
struct userPreview {
QString UserName;
int record;
bool online;
};
class RecordListModel: public QAbstractTableModel
{
Q_OBJECT
public:
enum RecordListModelRoles {
OnlineRole = Qt::UserRole,
RemoveRole
};
RecordListModel(QObject * parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
void setSource(const QMap<QString, userPreview> &data);
void updateSourceInte(const userPreview& data);
void removeSourceItem(const QString& name);
private:
QMap<QString, userPreview> _data;
// QAbstractItemModel interface
public:
QHash<int, QByteArray> roleNames() const override;
};
#endif // RECORDLISTMODEL_H