mirror of
https://github.com/QuasarApp/Hanoi-Towers.git
synced 2025-05-14 02:19:33 +00:00
fix Avatars view (meaby need to refactoring)
This commit is contained in:
parent
7ae768fdc4
commit
a1e13b2c1a
@ -1,3 +1,10 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2020 QuasarApp.
|
||||
* Distributed under the lgplv3 software license, see the accompanying
|
||||
* Everyone is permitted to copy and distribute verbatim copies
|
||||
* of this license document, but changing it is not allowed.
|
||||
*/
|
||||
|
||||
#include "useravatar.h"
|
||||
|
||||
UserAvatar::UserAvatar(): QH::PKG::DBObject("Avatars") {
|
||||
|
@ -1,3 +1,10 @@
|
||||
/*
|
||||
* Copyright (C) 2018-2020 QuasarApp.
|
||||
* Distributed under the lgplv3 software license, see the accompanying
|
||||
* Everyone is permitted to copy and distribute verbatim copies
|
||||
* of this license document, but changing it is not allowed.
|
||||
*/
|
||||
|
||||
#ifndef USERAVATAR_H
|
||||
#define USERAVATAR_H
|
||||
|
||||
|
@ -219,14 +219,23 @@ void BackEnd::setShowHelp(bool state) {
|
||||
_settings->setValue(FIRST_RUN_KEY, state);
|
||||
}
|
||||
|
||||
void BackEnd::setNewAvatar(const QString &pathToAvatar) {
|
||||
QImage image;
|
||||
void BackEnd::setNewAvatar(QString pathToAvatar) {
|
||||
if (pathToAvatar.contains("file://")) {
|
||||
image = QImage(pathToAvatar.right(pathToAvatar.size() - 7));
|
||||
} else {
|
||||
image = QImage(pathToAvatar);
|
||||
pathToAvatar = pathToAvatar.right(pathToAvatar.size() - 7);
|
||||
}
|
||||
_client->setNewAvatar(_profile.getId().toString(), image);
|
||||
|
||||
int id = _profile.avatarHash();
|
||||
_profile.setAvatarHash(-1);
|
||||
|
||||
QFile file(pathToAvatar);
|
||||
if (file.open(QIODevice::ReadOnly)) {
|
||||
auto userId = _profile.getId().toString();
|
||||
_client->setNewAvatar(userId, file.readAll());
|
||||
file.close();
|
||||
|
||||
}
|
||||
_profile.setAvatarHash(id);
|
||||
|
||||
}
|
||||
|
||||
bool BackEnd::fog() const {
|
||||
|
@ -44,6 +44,8 @@ class BackEnd: public QObject
|
||||
Q_PROPERTY(QObject* client READ client)
|
||||
|
||||
Q_PROPERTY(QObject* profileList READ profileList NOTIFY profileListChanged)
|
||||
Q_PROPERTY(QObject* profileObject READ profileObject NOTIFY profileChanged)
|
||||
|
||||
Q_PROPERTY(QString profile READ profile WRITE setProfile NOTIFY profileChanged)
|
||||
|
||||
|
||||
@ -68,7 +70,7 @@ public:
|
||||
* @param state - a new state of show help message
|
||||
*/
|
||||
Q_INVOKABLE void setShowHelp(bool state);
|
||||
Q_INVOKABLE void setNewAvatar(const QString& pathToAvatar);
|
||||
Q_INVOKABLE void setNewAvatar(QString pathToAvatar);
|
||||
|
||||
bool fog() const;
|
||||
|
||||
|
@ -17,6 +17,10 @@
|
||||
#include <useravatar.h>
|
||||
#include <sqldb.h>
|
||||
#include <sqldbwriter.h>
|
||||
#include <cacheddbobjectsrequest.h>
|
||||
#include <getmaxintegerid.h>
|
||||
#include <QBuffer>
|
||||
#include <getsinglevalue.h>
|
||||
#include "hanoierrorcodes.h"
|
||||
#include "localrecordstable.h"
|
||||
|
||||
@ -144,33 +148,41 @@ void HanoiClient::setStatus(const Status &status) {
|
||||
}
|
||||
}
|
||||
|
||||
bool HanoiClient::setNewAvatar(const QString &userId, const QImage &image) {
|
||||
auto avatarData = QSharedPointer<UserAvatar>::create();
|
||||
avatarData->setId(userId);
|
||||
|
||||
QByteArray array;
|
||||
QDataStream stram(&array, QIODevice::WriteOnly);
|
||||
|
||||
stram << image;
|
||||
avatarData->setImage(array);
|
||||
|
||||
if (!db()->updateObject(avatarData)) {
|
||||
return false;
|
||||
}
|
||||
bool HanoiClient::setNewAvatar(const QString &userId, const QByteArray &image) {
|
||||
|
||||
auto profile = getLocalUser(userId);
|
||||
|
||||
if (profile->online()) {
|
||||
return sendData(avatarData.data(), _serverAddress);
|
||||
|
||||
auto avatarData = UserAvatar();
|
||||
avatarData.setId(profile->avatarHash());
|
||||
|
||||
auto updateAction = [image](const QSharedPointer<QH::PKG::DBObject>& object) {
|
||||
|
||||
auto obj = object.dynamicCast<UserAvatar>();
|
||||
|
||||
if (!obj) {
|
||||
return false;
|
||||
}
|
||||
|
||||
obj->setImage(image);
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!db()->changeObjects(avatarData, updateAction)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (profile->online()) {
|
||||
return sendData(&avatarData, _serverAddress);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QImage HanoiClient::userAvatar(const QString &userId) const {
|
||||
QImage HanoiClient::userAvatar(int avatarId) const {
|
||||
UserAvatar avatarData;
|
||||
avatarData.setId(userId);
|
||||
avatarData.setId(avatarId);
|
||||
|
||||
auto result = db()->getObject(avatarData);
|
||||
|
||||
@ -181,6 +193,19 @@ QImage HanoiClient::userAvatar(const QString &userId) const {
|
||||
return {};
|
||||
}
|
||||
|
||||
QImage HanoiClient::userAvatar(const QString &userId) const {
|
||||
using RequestType = QH::PKG::CachedDbObjectsRequest<UserAvatar>;
|
||||
const RequestType request = QString("id=(select userAvatar from Users where id='%0')").arg(userId);
|
||||
|
||||
auto result = db()->getObject<UserAvatar>(request);
|
||||
|
||||
if (result && result->isValid()) {
|
||||
return QImage::fromData(result->image());
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
QString HanoiClient::currentUserId() const {
|
||||
return _currentUserId;
|
||||
}
|
||||
@ -200,13 +225,12 @@ QSharedPointer<LocalUser> HanoiClient::createLocalUser(const QString &login) {
|
||||
QSharedPointer<UserAvatar> HanoiClient::getDefaultAvatar(const QString& userId) const {
|
||||
auto avatar = QSharedPointer<UserAvatar>::create();
|
||||
|
||||
QByteArray array;
|
||||
QDataStream stram(&array, QIODevice::WriteOnly);
|
||||
QFile image(":/img/DefaultAvatar");
|
||||
if (image.open(QIODevice::ReadOnly)) {
|
||||
avatar->setImage(image.readAll());
|
||||
image.close();
|
||||
}
|
||||
|
||||
QImage image(":/img/DefaultAvatar");
|
||||
|
||||
stram << image;
|
||||
avatar->setImage(array);
|
||||
avatar->setUserId(userId);
|
||||
|
||||
return avatar;
|
||||
@ -214,12 +238,18 @@ QSharedPointer<UserAvatar> HanoiClient::getDefaultAvatar(const QString& userId)
|
||||
|
||||
bool HanoiClient::addProfile(const LocalUser& user) {
|
||||
|
||||
if (!db())
|
||||
return false;
|
||||
|
||||
auto maxId = db()->getObject(QH::PKG::GetMaxIntegerId("Avatars", "id"));
|
||||
if (!maxId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto localUser = QSharedPointer<LocalUser>::create();
|
||||
localUser->copyFrom(&user);
|
||||
localUser->setUpdateTime(time(nullptr));
|
||||
|
||||
if (!db())
|
||||
return false;
|
||||
localUser->setAvatarHash(maxId->value() + 1);
|
||||
|
||||
if (!db()->insertObject(localUser)) {
|
||||
return false;
|
||||
|
@ -18,6 +18,7 @@
|
||||
#include <databasenode.h>
|
||||
#include <profiledata.h>
|
||||
#include <useravatar.h>
|
||||
#include <useravatar.h>
|
||||
#include <userpreview.h>
|
||||
|
||||
class LocalUser;
|
||||
@ -66,9 +67,11 @@ public:
|
||||
Status getStatus() const;
|
||||
void setStatus(const Status &status);
|
||||
|
||||
bool setNewAvatar(const QString &userId, const QImage& image);
|
||||
bool setNewAvatar(const QString &userId, const QByteArray& image);
|
||||
QImage userAvatar(int avatarId) const;
|
||||
QImage userAvatar(const QString &userId) const;
|
||||
|
||||
|
||||
protected:
|
||||
void nodeConfirmend(const QH::HostAddress &node) override;
|
||||
void nodeConnected(const QH::HostAddress &node) override;
|
||||
|
@ -49,7 +49,7 @@ void AsyncImageResponse::run() {
|
||||
return;
|
||||
}
|
||||
|
||||
QImage image = _client->userAvatar(m_id.toLocal8Bit());;
|
||||
QImage image = _client->userAvatar(m_id.toInt());
|
||||
|
||||
if (m_requestedSize.isValid())
|
||||
image = image.scaled(m_requestedSize);
|
||||
|
@ -21,7 +21,7 @@ GridLayout {
|
||||
Layout.rowSpan: 1
|
||||
Image {
|
||||
id: userAvatar
|
||||
source: "image://HanoiImages/" + ((userModel)? userModel.userId: "")
|
||||
source: "image://HanoiImages/" + ((userModel)? userModel.avatarHash: "")
|
||||
anchors.fill: parent
|
||||
|
||||
fillMode: Image.PreserveAspectFit
|
||||
|
@ -29,7 +29,7 @@ Item {
|
||||
|
||||
UserView {
|
||||
Layout.rowSpan: 2
|
||||
userModel: (backEnd)? backEnd.profileObject(): null
|
||||
userModel: (backEnd)? backEnd.profileObject: null
|
||||
|
||||
onNewAvatar: {
|
||||
backEnd.setNewAvatar(path);
|
||||
|
2
Heart
2
Heart
@ -1 +1 @@
|
||||
Subproject commit d74f4c5752b45f908a254777fdc64abaab3e4bd9
|
||||
Subproject commit ebe0b5a4843d7bb28c25c87639eb5740ac48cb23
|
Loading…
x
Reference in New Issue
Block a user