mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 11:01:29 -07:00
do not save a const reference to the user data in the info dialog (#6974)
* do not save a const reference to the user data in the info dialog * cmake format
This commit is contained in:
@@ -85,24 +85,15 @@ void UserInfoBox::retranslateUi()
|
|||||||
avatarButton.setText(tr("Change avatar"));
|
avatarButton.setText(tr("Change avatar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates the default profile pic that is used when the user doesn't have a custom pic
|
|
||||||
*/
|
|
||||||
static QPixmap createDefaultAvatar(int height, const ServerInfo_User &user)
|
|
||||||
{
|
|
||||||
return UserLevelPixmapGenerator::generatePixmap(height, UserLevelFlags(user.user_level()), user.pawn_colors(),
|
|
||||||
false, QString::fromStdString(user.privlevel()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void UserInfoBox::updateInfo(const ServerInfo_User &user)
|
void UserInfoBox::updateInfo(const ServerInfo_User &user)
|
||||||
{
|
{
|
||||||
currentUserInfo = &user;
|
userLevel = UserLevelFlags(user.user_level());
|
||||||
|
pawnColors = user.pawn_colors();
|
||||||
const UserLevelFlags userLevel(user.user_level());
|
privLevel = QString::fromStdString(user.privlevel());
|
||||||
|
|
||||||
const std::string &bmp = user.avatar_bmp();
|
const std::string &bmp = user.avatar_bmp();
|
||||||
if (!avatarPixmap.loadFromData((const uchar *)bmp.data(), static_cast<uint>(bmp.size()))) {
|
if (!avatarPixmap.loadFromData((const uchar *)bmp.data(), static_cast<uint>(bmp.size()))) {
|
||||||
avatarPixmap = createDefaultAvatar(64, user);
|
avatarPixmap = UserLevelPixmapGenerator::generatePixmap(64, userLevel, pawnColors, false, privLevel);
|
||||||
hasAvatar = false;
|
hasAvatar = false;
|
||||||
} else {
|
} else {
|
||||||
hasAvatar = true;
|
hasAvatar = true;
|
||||||
@@ -120,8 +111,7 @@ void UserInfoBox::updateInfo(const ServerInfo_User &user)
|
|||||||
countryLabel3.setText("");
|
countryLabel3.setText("");
|
||||||
}
|
}
|
||||||
|
|
||||||
userLevelIcon.setPixmap(UserLevelPixmapGenerator::generatePixmap(15, userLevel, user.pawn_colors(), false,
|
userLevelIcon.setPixmap(UserLevelPixmapGenerator::generatePixmap(15, userLevel, pawnColors, false, privLevel));
|
||||||
QString::fromStdString(user.privlevel())));
|
|
||||||
QString userLevelText;
|
QString userLevelText;
|
||||||
if (userLevel.testFlag(ServerInfo_User::IsAdmin)) {
|
if (userLevel.testFlag(ServerInfo_User::IsAdmin)) {
|
||||||
userLevelText = tr("Administrator");
|
userLevelText = tr("Administrator");
|
||||||
@@ -373,7 +363,7 @@ void UserInfoBox::processAvatarResponse(const Response &r)
|
|||||||
break;
|
break;
|
||||||
case Response::RespInternalError:
|
case Response::RespInternalError:
|
||||||
default:
|
default:
|
||||||
QMessageBox::critical(this, tr("Error"), tr("An error occured while trying to updater your avatar."));
|
QMessageBox::critical(this, tr("Error"), tr("An error occured while trying to update your avatar."));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -385,7 +375,7 @@ void UserInfoBox::resizeEvent(QResizeEvent *event)
|
|||||||
resizedPixmap = avatarPixmap.scaled(avatarPic.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
resizedPixmap = avatarPixmap.scaled(avatarPic.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
||||||
} else {
|
} else {
|
||||||
int height = qMin(avatarPic.size().width(), avatarPic.size().height());
|
int height = qMin(avatarPic.size().width(), avatarPic.size().height());
|
||||||
resizedPixmap = createDefaultAvatar(height, *currentUserInfo);
|
resizedPixmap = UserLevelPixmapGenerator::generatePixmap(height, userLevel, pawnColors, false, privLevel);
|
||||||
}
|
}
|
||||||
avatarPic.setPixmap(resizedPixmap);
|
avatarPic.setPixmap(resizedPixmap);
|
||||||
|
|
||||||
|
|||||||
@@ -11,8 +11,9 @@
|
|||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include <libcockatrice/network/server/remote/user_level.h>
|
||||||
|
#include <libcockatrice/utility/days_years_between.h>
|
||||||
|
|
||||||
class ServerInfo_User;
|
|
||||||
class AbstractClient;
|
class AbstractClient;
|
||||||
class Response;
|
class Response;
|
||||||
|
|
||||||
@@ -27,20 +28,15 @@ private:
|
|||||||
QPushButton editButton, passwordButton, avatarButton;
|
QPushButton editButton, passwordButton, avatarButton;
|
||||||
QPixmap avatarPixmap;
|
QPixmap avatarPixmap;
|
||||||
bool hasAvatar;
|
bool hasAvatar;
|
||||||
const ServerInfo_User *currentUserInfo;
|
UserLevelFlags userLevel;
|
||||||
|
ServerInfo_User::PawnColorsOverride pawnColors;
|
||||||
|
QString privLevel;
|
||||||
|
|
||||||
static QString getAgeString(int ageSeconds);
|
static QString getAgeString(int ageSeconds);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
UserInfoBox(AbstractClient *_client, bool editable, QWidget *parent = nullptr, Qt::WindowFlags flags = {});
|
UserInfoBox(AbstractClient *_client, bool editable, QWidget *parent = nullptr, Qt::WindowFlags flags = {});
|
||||||
void retranslateUi();
|
void retranslateUi();
|
||||||
|
|
||||||
inline static QPair<int, int> getDaysAndYearsBetween(const QDate &then, const QDate &now)
|
|
||||||
{
|
|
||||||
int years = now.addDays(1 - then.dayOfYear()).year() - then.year(); // there is no yearsTo
|
|
||||||
int days = then.addYears(years).daysTo(now);
|
|
||||||
return {days, years};
|
|
||||||
}
|
|
||||||
private slots:
|
private slots:
|
||||||
void processResponse(const Response &r);
|
void processResponse(const Response &r);
|
||||||
void processEditResponse(const Response &r);
|
void processEditResponse(const Response &r);
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
|
inline static QPair<int, int> getDaysAndYearsBetween(const QDate &then, const QDate &now)
|
||||||
|
{
|
||||||
|
int years = now.addDays(1 - then.dayOfYear()).year() - then.year(); // there is no yearsTo
|
||||||
|
int days = then.addYears(years).daysTo(now);
|
||||||
|
return {days, years};
|
||||||
|
}
|
||||||
@@ -59,7 +59,9 @@ endif()
|
|||||||
include_directories(${GTEST_INCLUDE_DIRS})
|
include_directories(${GTEST_INCLUDE_DIRS})
|
||||||
target_link_libraries(dummy_test Threads::Threads ${GTEST_BOTH_LIBRARIES})
|
target_link_libraries(dummy_test Threads::Threads ${GTEST_BOTH_LIBRARIES})
|
||||||
target_link_libraries(expression_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES})
|
target_link_libraries(expression_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES})
|
||||||
target_link_libraries(test_age_formatting Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES})
|
target_link_libraries(
|
||||||
|
test_age_formatting libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
|
||||||
|
)
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
password_hash_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
|
password_hash_test libcockatrice_utility Threads::Threads ${GTEST_BOTH_LIBRARIES} ${TEST_QT_MODULES}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "../cockatrice/src/interface/widgets/server/user/user_info_box.h"
|
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
#include <libcockatrice/utility/days_years_between.h>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
@@ -8,31 +7,31 @@ using dayyear = QPair<int, int>;
|
|||||||
|
|
||||||
TEST(AgeFormatting, Zero)
|
TEST(AgeFormatting, Zero)
|
||||||
{
|
{
|
||||||
auto got = UserInfoBox::getDaysAndYearsBetween(QDate(2000, 1, 1), QDate(2000, 1, 1));
|
auto got = getDaysAndYearsBetween(QDate(2000, 1, 1), QDate(2000, 1, 1));
|
||||||
ASSERT_EQ(got, dayyear(0, 0)) << "these are the same day";
|
ASSERT_EQ(got, dayyear(0, 0)) << "these are the same day";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AgeFormatting, LeapDay)
|
TEST(AgeFormatting, LeapDay)
|
||||||
{
|
{
|
||||||
auto got = UserInfoBox::getDaysAndYearsBetween(QDate(2000, 2, 28), QDate(2000, 3, 1));
|
auto got = getDaysAndYearsBetween(QDate(2000, 2, 28), QDate(2000, 3, 1));
|
||||||
ASSERT_EQ(got, dayyear(2, 0)) << "there is a leap day in between these days";
|
ASSERT_EQ(got, dayyear(2, 0)) << "there is a leap day in between these days";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AgeFormatting, LeapYear)
|
TEST(AgeFormatting, LeapYear)
|
||||||
{
|
{
|
||||||
auto got = UserInfoBox::getDaysAndYearsBetween(QDate(2000, 1, 1), QDate(2001, 1, 1));
|
auto got = getDaysAndYearsBetween(QDate(2000, 1, 1), QDate(2001, 1, 1));
|
||||||
ASSERT_EQ(got, dayyear(0, 1)) << "there is a leap day in between these dates, but that's fine";
|
ASSERT_EQ(got, dayyear(0, 1)) << "there is a leap day in between these dates, but that's fine";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AgeFormatting, LeapDayWithYear)
|
TEST(AgeFormatting, LeapDayWithYear)
|
||||||
{
|
{
|
||||||
auto got = UserInfoBox::getDaysAndYearsBetween(QDate(2000, 2, 28), QDate(2001, 3, 1));
|
auto got = getDaysAndYearsBetween(QDate(2000, 2, 28), QDate(2001, 3, 1));
|
||||||
ASSERT_EQ(got, dayyear(1, 1)) << "there is a leap day in between these days but not in the last year";
|
ASSERT_EQ(got, dayyear(1, 1)) << "there is a leap day in between these days but not in the last year";
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AgeFormatting, LeapDayThisYear)
|
TEST(AgeFormatting, LeapDayThisYear)
|
||||||
{
|
{
|
||||||
auto got = UserInfoBox::getDaysAndYearsBetween(QDate(2003, 2, 28), QDate(2004, 3, 1));
|
auto got = getDaysAndYearsBetween(QDate(2003, 2, 28), QDate(2004, 3, 1));
|
||||||
ASSERT_EQ(got, dayyear(2, 1)) << "there is a leap day in between these days this year";
|
ASSERT_EQ(got, dayyear(2, 1)) << "there is a leap day in between these days this year";
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user