gta5view/SnapmaticEditor.cpp

428 lines
14 KiB
C++
Raw Permalink Normal View History

2017-10-09 08:35:48 +02:00
/*****************************************************************************
* gta5view Grand Theft Auto V Profile Viewer
* Copyright (C) 2016-2021 Syping
2017-10-09 08:35:48 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "SnapmaticEditor.h"
#include "ui_SnapmaticEditor.h"
#include "SnapmaticPicture.h"
2017-12-12 04:45:46 +01:00
#include "PlayerListDialog.h"
2017-10-09 08:35:48 +02:00
#include "StringParser.h"
2021-03-26 10:59:34 +01:00
#include "wrapper.h"
2017-10-09 08:35:48 +02:00
#include "AppEnv.h"
#include "config.h"
2017-10-09 08:35:48 +02:00
#include <QStringBuilder>
#include <QTextDocument>
#include <QInputDialog>
#include <QMessageBox>
#include <QDebug>
#include <QFile>
#ifdef GTA5SYNC_TELEMETRY
#include "TelemetryClass.h"
#include <QJsonDocument>
#include <QJsonObject>
#endif
2017-12-12 04:45:46 +01:00
SnapmaticEditor::SnapmaticEditor(CrewDatabase *crewDB, ProfileDatabase *profileDB, QWidget *parent) :
QDialog(parent), crewDB(crewDB), profileDB(profileDB),
2017-10-09 08:35:48 +02:00
ui(new Ui::SnapmaticEditor)
{
// Set Window Flags
2021-02-15 23:12:44 +01:00
#if QT_VERSION >= 0x050900
setWindowFlag(Qt::WindowContextHelpButtonHint, false);
#else
setWindowFlags(windowFlags()^Qt::WindowContextHelpButtonHint);
2021-02-15 23:12:44 +01:00
#endif
2017-10-09 08:35:48 +02:00
ui->setupUi(this);
ui->cmdCancel->setDefault(true);
2018-01-11 08:41:00 +01:00
ui->cmdCancel->setFocus();
2017-10-09 08:35:48 +02:00
2018-01-11 08:41:00 +01:00
// Set Icon for Apply Button
2021-02-15 23:12:44 +01:00
if (QIcon::hasThemeIcon("dialog-ok-apply")) {
ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok-apply"));
}
2021-02-15 23:12:44 +01:00
else if (QIcon::hasThemeIcon("dialog-apply")) {
2017-10-09 08:35:48 +02:00
ui->cmdApply->setIcon(QIcon::fromTheme("dialog-apply"));
}
2021-02-15 23:12:44 +01:00
else if (QIcon::hasThemeIcon("gtk-apply")) {
2018-01-11 08:41:00 +01:00
ui->cmdApply->setIcon(QIcon::fromTheme("gtk-apply"));
}
2021-02-15 23:12:44 +01:00
else if (QIcon::hasThemeIcon("dialog-ok")) {
ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok"));
}
2021-02-15 23:12:44 +01:00
else if (QIcon::hasThemeIcon("gtk-ok")) {
2018-01-11 08:41:00 +01:00
ui->cmdApply->setIcon(QIcon::fromTheme("dialog-ok"));
}
// Set Icon for Cancel Button
2021-02-15 23:12:44 +01:00
if (QIcon::hasThemeIcon("dialog-cancel")) {
2017-10-09 08:35:48 +02:00
ui->cmdCancel->setIcon(QIcon::fromTheme("dialog-cancel"));
}
2021-02-15 23:12:44 +01:00
else if (QIcon::hasThemeIcon("gtk-cancel")) {
2018-01-11 08:41:00 +01:00
ui->cmdCancel->setIcon(QIcon::fromTheme("gtk-cancel"));
}
2017-10-09 08:35:48 +02:00
2017-12-12 04:45:46 +01:00
snapmaticTitle = QString();
2017-10-09 08:35:48 +02:00
smpic = 0;
2017-12-17 13:03:43 +01:00
#ifndef Q_OS_ANDROID
2017-10-09 08:35:48 +02:00
// DPI calculation
qreal screenRatio = AppEnv::screenRatio();
resize(400 * screenRatio, 360 * screenRatio);
2017-12-17 13:03:43 +01:00
#endif
2017-10-09 08:35:48 +02:00
}
SnapmaticEditor::~SnapmaticEditor()
{
delete ui;
}
void SnapmaticEditor::selfie_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
isSelfie = checked;
2017-10-09 08:35:48 +02:00
}
void SnapmaticEditor::mugshot_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
isMugshot = true;
ui->cbDirector->setEnabled(false);
ui->cbDirector->setChecked(false);
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
isMugshot = false;
ui->cbDirector->setEnabled(true);
}
}
void SnapmaticEditor::editor_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
isEditor = true;
ui->cbDirector->setEnabled(false);
ui->cbDirector->setChecked(false);
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
isEditor = false;
ui->cbDirector->setEnabled(true);
}
}
void SnapmaticEditor::on_rbSelfie_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
mugshot_toggled(false);
editor_toggled(false);
selfie_toggled(true);
}
}
void SnapmaticEditor::on_rbMugshot_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
selfie_toggled(false);
editor_toggled(false);
mugshot_toggled(true);
}
}
void SnapmaticEditor::on_rbEditor_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
selfie_toggled(false);
mugshot_toggled(false);
editor_toggled(true);
}
}
void SnapmaticEditor::on_rbCustom_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
selfie_toggled(false);
mugshot_toggled(false);
editor_toggled(false);
}
}
void SnapmaticEditor::setSnapmaticPicture(SnapmaticPicture *picture)
{
smpic = picture;
2017-12-12 04:45:46 +01:00
snapmaticProperties = smpic->getSnapmaticProperties();
2017-10-09 08:35:48 +02:00
ui->rbCustom->setChecked(true);
2017-12-12 04:45:46 +01:00
crewID = snapmaticProperties.crewID;
isSelfie = snapmaticProperties.isSelfie;
isMugshot = snapmaticProperties.isMug;
isEditor = snapmaticProperties.isFromRSEditor;
playersList = snapmaticProperties.playersList;
ui->cbDirector->setChecked(snapmaticProperties.isFromDirector);
ui->cbMeme->setChecked(snapmaticProperties.isMeme);
2021-02-15 23:12:44 +01:00
if (isSelfie) {
2017-10-09 08:35:48 +02:00
ui->rbSelfie->setChecked(true);
}
2021-02-15 23:12:44 +01:00
else if (isMugshot) {
2017-10-09 08:35:48 +02:00
ui->rbMugshot->setChecked(true);
}
2021-02-15 23:12:44 +01:00
else if (isEditor) {
2017-10-09 08:35:48 +02:00
ui->rbEditor->setChecked(true);
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
ui->rbCustom->setChecked(true);
}
setSnapmaticCrew(returnCrewName(crewID));
setSnapmaticTitle(picture->getPictureTitle());
2017-12-12 04:45:46 +01:00
setSnapmaticPlayers(insertPlayerNames(playersList));
}
void SnapmaticEditor::insertPlayerNames(QStringList *players)
{
2021-02-15 23:12:44 +01:00
for (int i = 0; i < players->size(); ++i) {
2017-12-12 04:45:46 +01:00
players->replace(i, profileDB->getPlayerName(players->at(i)));
}
}
QStringList SnapmaticEditor::insertPlayerNames(const QStringList &players)
{
QStringList playersWI = players;
insertPlayerNames(&playersWI);
return playersWI;
}
void SnapmaticEditor::setSnapmaticPlayers(const QStringList &players)
{
QString editStr = QString("<a href=\"g5e://editplayers\" style=\"text-decoration: none;\">%1</a>").arg(tr("Edit"));
QString playersStr;
2021-02-15 23:12:44 +01:00
if (players.length() != 1) {
2017-12-12 04:45:46 +01:00
playersStr = tr("Players: %1 (%2)", "Multiple Player are inserted here");
}
2021-02-15 23:12:44 +01:00
else {
2017-12-12 04:45:46 +01:00
playersStr = tr("Player: %1 (%2)", "One Player is inserted here");
}
2021-02-15 23:12:44 +01:00
if (players.length() != 0) {
2017-12-12 04:45:46 +01:00
ui->labPlayers->setText(playersStr.arg(players.join(", "), editStr));
}
2021-02-15 23:12:44 +01:00
else {
2017-12-12 04:45:46 +01:00
ui->labPlayers->setText(playersStr.arg(QApplication::translate("PictureDialog", "No Players"), editStr));
}
2017-12-17 13:03:43 +01:00
#ifndef Q_OS_ANDROID
ui->gbValues->resize(ui->gbValues->width(), ui->gbValues->heightForWidth(ui->gbValues->width()));
ui->frameWidget->resize(ui->gbValues->width(), ui->frameWidget->heightForWidth(ui->frameWidget->width()));
2021-02-15 23:12:44 +01:00
if (heightForWidth(width()) > height())
resize(width(), heightForWidth(width()));
2017-12-17 13:03:43 +01:00
#endif
2017-10-09 08:35:48 +02:00
}
void SnapmaticEditor::setSnapmaticTitle(const QString &title)
{
2021-02-15 23:12:44 +01:00
if (title.length() > 39) {
2017-10-09 08:35:48 +02:00
snapmaticTitle = title.left(39);
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
snapmaticTitle = title;
}
QString editStr = QString("<a href=\"g5e://edittitle\" style=\"text-decoration: none;\">%1</a>").arg(tr("Edit"));
QString titleStr = tr("Title: %1 (%2)").arg(StringParser::escapeString(snapmaticTitle), editStr);
ui->labTitle->setText(titleStr);
2021-02-15 23:12:44 +01:00
if (SnapmaticPicture::verifyTitle(snapmaticTitle)) {
ui->labAppropriate->setText(tr("Appropriate: %1").arg(QString("<span style=\"color: green\">%1</span>").arg(tr("Yes", "Yes, should work fine"))));
2017-10-09 08:35:48 +02:00
}
2021-02-15 23:12:44 +01:00
else {
ui->labAppropriate->setText(tr("Appropriate: %1").arg(QString("<span style=\"color: red\">%1</span>").arg(tr("No", "No, could lead to issues"))));
2017-10-09 08:35:48 +02:00
}
2017-12-17 13:03:43 +01:00
#ifndef Q_OS_ANDROID
ui->gbValues->resize(ui->gbValues->width(), ui->gbValues->heightForWidth(ui->gbValues->width()));
ui->frameWidget->resize(ui->gbValues->width(), ui->frameWidget->heightForWidth(ui->frameWidget->width()));
2021-02-15 23:12:44 +01:00
if (heightForWidth(width()) > height())
resize(width(), heightForWidth(width()));
2017-12-17 13:03:43 +01:00
#endif
2017-10-09 08:35:48 +02:00
}
void SnapmaticEditor::setSnapmaticCrew(const QString &crew)
{
QString editStr = QString("<a href=\"g5e://editcrew\" style=\"text-decoration: none;\">%1</a>").arg(tr("Edit"));
QString crewStr = tr("Crew: %1 (%2)").arg(StringParser::escapeString(crew), editStr);
ui->labCrew->setText(crewStr);
2017-12-17 13:03:43 +01:00
#ifndef Q_OS_ANDROID
ui->gbValues->resize(ui->gbValues->width(), ui->gbValues->heightForWidth(ui->gbValues->width()));
ui->frameWidget->resize(ui->gbValues->width(), ui->frameWidget->heightForWidth(ui->frameWidget->width()));
2021-02-15 23:12:44 +01:00
if (heightForWidth(width()) > height())
resize(width(), heightForWidth(width()));
2017-12-17 13:03:43 +01:00
#endif
2017-10-09 08:35:48 +02:00
}
QString SnapmaticEditor::returnCrewName(int crewID_)
{
return crewDB->getCrewName(crewID_);
}
void SnapmaticEditor::on_cmdCancel_clicked()
{
close();
}
void SnapmaticEditor::on_cmdApply_clicked()
{
2021-02-15 23:12:44 +01:00
if (ui->cbQualify->isChecked()) {
2017-10-09 08:35:48 +02:00
qualifyAvatar();
}
2017-12-12 04:45:46 +01:00
snapmaticProperties.crewID = crewID;
snapmaticProperties.isSelfie = isSelfie;
snapmaticProperties.isMug = isMugshot;
snapmaticProperties.isFromRSEditor = isEditor;
snapmaticProperties.isFromDirector = ui->cbDirector->isChecked();
snapmaticProperties.isMeme = ui->cbMeme->isChecked();
snapmaticProperties.playersList = playersList;
2021-02-15 23:12:44 +01:00
if (smpic) {
2017-10-12 22:21:45 +02:00
QString currentFilePath = smpic->getPictureFilePath();
QString originalFilePath = smpic->getOriginalPictureFilePath();
QString backupFileName = originalFilePath % ".bak";
2021-02-15 23:12:44 +01:00
if (!QFile::exists(backupFileName)) {
2017-10-12 22:21:45 +02:00
QFile::copy(currentFilePath, backupFileName);
2017-10-09 08:35:48 +02:00
}
SnapmaticProperties fallbackProperties = smpic->getSnapmaticProperties();
QString fallbackTitle = smpic->getPictureTitle();
2017-12-12 04:45:46 +01:00
smpic->setSnapmaticProperties(snapmaticProperties);
2017-10-09 08:35:48 +02:00
smpic->setPictureTitle(snapmaticTitle);
2021-02-15 23:12:44 +01:00
if (!smpic->exportPicture(currentFilePath)) {
2017-10-09 08:35:48 +02:00
QMessageBox::warning(this, tr("Snapmatic Properties"), tr("Patching of Snapmatic Properties failed because of I/O Error"));
smpic->setSnapmaticProperties(fallbackProperties);
smpic->setPictureTitle(fallbackTitle);
}
2021-02-15 23:12:44 +01:00
else {
2018-07-27 04:26:10 +02:00
smpic->updateStrings();
2017-10-09 08:35:48 +02:00
smpic->emitUpdate();
#ifdef GTA5SYNC_TELEMETRY
QSettings telemetrySettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
telemetrySettings.beginGroup("Telemetry");
bool pushUsageData = telemetrySettings.value("PushUsageData", false).toBool();
telemetrySettings.endGroup();
2021-02-15 23:12:44 +01:00
if (pushUsageData && Telemetry->canPush()) {
QJsonDocument jsonDocument;
QJsonObject jsonObject;
jsonObject["Type"] = "PropertyEdited";
jsonObject["EditedSize"] = QString::number(smpic->getContentMaxLength());
#if QT_VERSION >= 0x060000
jsonObject["EditedTime"] = QString::number(QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
#else
jsonObject["EditedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
#endif
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
#endif
2017-10-09 08:35:48 +02:00
}
}
close();
}
void SnapmaticEditor::qualifyAvatar()
{
ui->rbSelfie->setChecked(true);
ui->cbDirector->setChecked(false);
ui->cbMeme->setChecked(false);
ui->cmdApply->setDefault(true);
}
void SnapmaticEditor::on_cbQualify_toggled(bool checked)
{
2021-02-15 23:12:44 +01:00
if (checked) {
2017-10-09 08:35:48 +02:00
ui->cbMeme->setEnabled(false);
ui->cbDirector->setEnabled(false);
ui->rbCustom->setEnabled(false);
ui->rbSelfie->setEnabled(false);
ui->rbEditor->setEnabled(false);
ui->rbMugshot->setEnabled(false);
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
ui->cbMeme->setEnabled(true);
ui->rbCustom->setEnabled(true);
ui->rbSelfie->setEnabled(true);
ui->rbEditor->setEnabled(true);
ui->rbMugshot->setEnabled(true);
2021-02-15 23:12:44 +01:00
if (ui->rbSelfie->isChecked() || ui->rbCustom->isChecked()) {
2017-10-09 08:35:48 +02:00
ui->cbDirector->setEnabled(true);
}
}
}
2017-12-12 04:45:46 +01:00
void SnapmaticEditor::on_labPlayers_linkActivated(const QString &link)
{
2021-02-15 23:12:44 +01:00
if (link == "g5e://editplayers") {
2017-12-12 04:45:46 +01:00
PlayerListDialog *playerListDialog = new PlayerListDialog(playersList, profileDB, this);
connect(playerListDialog, SIGNAL(playerListUpdated(QStringList)), this, SLOT(playerListUpdated(QStringList)));
2017-12-17 13:03:43 +01:00
playerListDialog->setModal(true);
2017-12-12 04:45:46 +01:00
playerListDialog->show();
playerListDialog->exec();
delete playerListDialog;
}
}
2017-10-09 08:35:48 +02:00
void SnapmaticEditor::on_labTitle_linkActivated(const QString &link)
{
2021-02-15 23:12:44 +01:00
if (link == "g5e://edittitle") {
2017-10-09 08:35:48 +02:00
bool ok;
QString newTitle = QInputDialog::getText(this, tr("Snapmatic Title"), tr("New Snapmatic title:"), QLineEdit::Normal, snapmaticTitle, &ok, windowFlags());
2021-02-15 23:12:44 +01:00
if (ok && !newTitle.isEmpty()) {
2017-10-09 08:35:48 +02:00
setSnapmaticTitle(newTitle);
}
}
}
void SnapmaticEditor::on_labCrew_linkActivated(const QString &link)
{
2021-02-15 23:12:44 +01:00
if (link == "g5e://editcrew") {
2017-10-09 08:35:48 +02:00
bool ok;
int indexNum = 0;
QStringList itemList;
QStringList crewList = crewDB->getCrews();
2021-02-15 23:12:44 +01:00
if (!crewList.contains(QLatin1String("0"))) {
2017-10-09 08:35:48 +02:00
crewList += QLatin1String("0");
}
crewList.sort();
2021-02-15 23:12:44 +01:00
for (const QString &crew : crewList) {
2017-10-09 08:35:48 +02:00
itemList += QString("%1 (%2)").arg(crew, returnCrewName(crew.toInt()));
}
2021-02-15 23:12:44 +01:00
if (crewList.contains(QString::number(crewID))) {
2020-09-28 05:33:04 +02:00
indexNum = crewList.indexOf(QString::number(crewID));
2017-10-09 08:35:48 +02:00
}
QString newCrew = QInputDialog::getItem(this, tr("Snapmatic Crew"), tr("New Snapmatic crew:"), itemList, indexNum, true, &ok, windowFlags());
2021-02-15 23:12:44 +01:00
if (ok && !newCrew.isEmpty()) {
if (newCrew.contains(" "))
newCrew = newCrew.split(" ").at(0);
if (newCrew.length() > 10)
return;
2021-03-26 10:59:34 +01:00
for (const QChar &crewChar : qAsConst(newCrew)) {
2021-02-15 23:12:44 +01:00
if (!crewChar.isNumber()) {
2017-10-09 08:35:48 +02:00
return;
}
}
2021-02-15 23:12:44 +01:00
if (!crewList.contains(newCrew)) {
2018-08-25 03:11:37 +02:00
crewDB->addCrew(crewID);
}
2017-10-09 08:35:48 +02:00
crewID = newCrew.toInt();
setSnapmaticCrew(returnCrewName(crewID));
}
}
}
2017-12-12 04:45:46 +01:00
void SnapmaticEditor::playerListUpdated(QStringList playerList)
{
playersList = playerList;
setSnapmaticPlayers(insertPlayerNames(playerList));
}