Address macOS issue where right-clicking a username in the main chat (#4523)

* Address macOS issue where right-clicking a username in the main chat (or game chat) areas would pop up a seemingly empty user profile. This is because the resize event is overridden and doesn't actually attempt to resize based on the size hint of the dialog. Now that we're explicit with the call, this resize should be forced and have comparable results to popping up user profile from the user list.

* use datetime for calculating account age (#4526)

* use datetime for calculating account age

make translating easier by using tr multiples
automatically account for leap days

Co-authored-by: ebbit1q <ebbit1q@gmail.com>
This commit is contained in:
Zach H 2022-01-16 16:51:13 -05:00 committed by GitHub
parent baaf22d0c4
commit d61c604bf4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 30 deletions

View file

@ -11,20 +11,15 @@
#include <QDateTime>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QMessageBox>
const qint64 SIXTY = 60;
const qint64 HOURS_IN_A_DAY = 24;
const qint64 DAYS_IN_A_YEAR = 365;
UserInfoBox::UserInfoBox(AbstractClient *_client, bool _editable, QWidget *parent, Qt::WindowFlags flags)
: QWidget(parent, flags), client(_client), editable(_editable)
{
QFont nameFont = nameLabel.font();
nameFont.setBold(true);
nameFont.setPointSize(nameFont.pointSize() * 1.5);
nameFont.setPointSizeF(nameFont.pointSizeF() * 1.5);
nameLabel.setFont(nameFont);
avatarLabel.setMinimumSize(200, 200);
@ -90,7 +85,6 @@ void UserInfoBox::updateInfo(const ServerInfo_User &user)
avatarPixmap =
UserLevelPixmapGenerator::generatePixmap(64, userLevel, false, QString::fromStdString(user.privlevel()));
}
avatarLabel.setPixmap(avatarPixmap.scaled(400, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));
nameLabel.setText(QString::fromStdString(user.name()));
realNameLabel2.setText(QString::fromStdString(user.real_name()));
@ -129,32 +123,37 @@ void UserInfoBox::updateInfo(const ServerInfo_User &user)
QString accountAgeString = tr("Unregistered user");
if (userLevel.testFlag(ServerInfo_User::IsAdmin) || userLevel.testFlag(ServerInfo_User::IsModerator) ||
userLevel.testFlag(ServerInfo_User::IsRegistered)) {
if (user.accountage_secs() == 0)
accountAgeString = tr("Unknown");
else {
qint64 seconds = user.accountage_secs();
qint64 minutes = seconds / SIXTY;
qint64 hours = minutes / SIXTY;
qint64 days = hours / HOURS_IN_A_DAY;
qint64 years = days / DAYS_IN_A_YEAR;
qint64 daysMinusYears = days - (years * DAYS_IN_A_YEAR);
accountAgeString = "";
if (years >= 1) {
accountAgeString = QString::number(years);
accountAgeString.append(" ");
accountAgeString.append(years == 1 ? tr("Year") : tr("Years"));
accountAgeString.append(" ");
}
accountAgeString.append(QString::number(daysMinusYears));
accountAgeString.append(" ");
accountAgeString.append(days == 1 ? tr("Day") : tr("Days"));
}
accountAgeString = getAgeString(user.accountage_secs());
}
accountAgeLabel2.setText(accountAgeString);
}
QString UserInfoBox::getAgeString(int ageSeconds)
{
QString accountAgeString = tr("Unknown");
if (ageSeconds == 0)
return accountAgeString;
auto date = QDateTime::fromTime_t(QDateTime::currentSecsSinceEpoch() - ageSeconds).date();
if (!date.isValid())
return accountAgeString;
QString dateString = QLocale().toString(date, QLocale::ShortFormat);
auto now = QDate::currentDate();
auto daysAndYears = getDaysAndYearsBetween(date, now);
QString yearString;
if (daysAndYears.second > 0) {
yearString = tr("%n Year(s), ", "amount of years (only shown if more than 0)", daysAndYears.second);
}
accountAgeString =
tr("%10%n Day(s) %20", "amount of years (if more than 0), amount of days, date in local short format",
daysAndYears.first)
.arg(yearString)
.arg(dateString);
return accountAgeString;
}
void UserInfoBox::updateInfo(const QString &userName)
{
Command_GetUserInfo cmd;
@ -171,6 +170,7 @@ void UserInfoBox::processResponse(const Response &r)
{
const Response_GetUserInfo &response = r.GetExtension(Response_GetUserInfo::ext);
updateInfo(response.user_info());
resize(sizeHint());
show();
}
@ -257,7 +257,7 @@ void UserInfoBox::processEditResponse(const Response &r)
case Response::RespInternalError:
default:
QMessageBox::critical(this, tr("Error"),
tr("An error occured while trying to update your user informations."));
tr("An error occurred while trying to update your user information."));
break;
}
}