gta5view/SnapmaticEditor.cpp

341 lines
9.6 KiB
C++
Raw Normal View History

2016-12-14 00:21:06 +01:00
/*****************************************************************************
* gta5sync GRAND THEFT AUTO V SYNC
2017-02-11 04:21:55 +01:00
* Copyright (C) 2016-2017 Syping
2016-12-14 00:21:06 +01: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-03-01 15:19:40 +01:00
#include "StringParser.h"
#include "AppEnv.h"
2017-08-26 14:13:35 +02:00
#include <QStringBuilder>
2017-03-01 15:19:40 +01:00
#include <QTextDocument>
#include <QInputDialog>
2016-12-14 00:21:06 +01:00
#include <QMessageBox>
#include <QDebug>
#include <QFile>
2017-03-30 03:44:48 +02:00
SnapmaticEditor::SnapmaticEditor(CrewDatabase *crewDB, QWidget *parent) :
QDialog(parent), crewDB(crewDB),
2016-12-14 00:21:06 +01:00
ui(new Ui::SnapmaticEditor)
{
ui->setupUi(this);
ui->cmdApply->setDefault(true);
2017-02-11 04:21:55 +01:00
if (QIcon::hasThemeIcon("dialog-apply"))
{
ui->cmdApply->setIcon(QIcon::fromTheme("dialog-apply"));
}
if (QIcon::hasThemeIcon("dialog-cancel"))
{
ui->cmdCancel->setIcon(QIcon::fromTheme("dialog-cancel"));
}
2017-03-01 15:19:40 +01:00
snapmaticTitle = "";
2016-12-14 00:21:06 +01:00
smpic = 0;
// DPI calculation
qreal screenRatio = AppEnv::screenRatio();
resize(400 * screenRatio, 360 * screenRatio);
2016-12-14 00:21:06 +01:00
}
SnapmaticEditor::~SnapmaticEditor()
{
delete ui;
}
2017-03-30 03:44:48 +02:00
void SnapmaticEditor::selfie_toggled(bool checked)
2016-12-14 00:21:06 +01:00
{
if (checked)
{
2017-03-30 03:44:48 +02:00
isSelfie = true;
2016-12-14 00:21:06 +01:00
}
2017-03-30 03:44:48 +02:00
else
2016-12-14 00:21:06 +01:00
{
2017-03-30 03:44:48 +02:00
isSelfie = false;
2016-12-14 00:21:06 +01:00
}
}
2017-03-30 03:44:48 +02:00
void SnapmaticEditor::mugshot_toggled(bool checked)
2016-12-14 00:21:06 +01:00
{
if (checked)
{
2017-03-30 03:44:48 +02:00
isMugshot = true;
2016-12-14 00:21:06 +01:00
ui->cbDirector->setEnabled(false);
2017-03-30 03:44:48 +02:00
ui->cbDirector->setChecked(false);
2016-12-14 00:21:06 +01:00
}
else
{
2017-03-30 03:44:48 +02:00
isMugshot = false;
2016-12-14 00:21:06 +01:00
ui->cbDirector->setEnabled(true);
}
}
2017-03-30 03:44:48 +02:00
void SnapmaticEditor::editor_toggled(bool checked)
2016-12-14 00:21:06 +01:00
{
if (checked)
{
2017-03-30 03:44:48 +02:00
isEditor = true;
2016-12-14 00:21:06 +01:00
ui->cbDirector->setEnabled(false);
2017-03-30 03:44:48 +02:00
ui->cbDirector->setChecked(false);
2016-12-14 00:21:06 +01:00
}
else
{
2017-03-30 03:44:48 +02:00
isEditor = false;
2016-12-14 00:21:06 +01:00
ui->cbDirector->setEnabled(true);
}
}
void SnapmaticEditor::on_rbSelfie_toggled(bool checked)
{
if (checked)
{
2017-03-30 03:44:48 +02:00
mugshot_toggled(false);
editor_toggled(false);
selfie_toggled(true);
2016-12-14 00:21:06 +01:00
}
}
void SnapmaticEditor::on_rbMugshot_toggled(bool checked)
{
if (checked)
{
2017-03-30 03:44:48 +02:00
selfie_toggled(false);
editor_toggled(false);
mugshot_toggled(true);
2016-12-14 00:21:06 +01:00
}
}
void SnapmaticEditor::on_rbEditor_toggled(bool checked)
{
if (checked)
{
2017-03-30 03:44:48 +02:00
selfie_toggled(false);
mugshot_toggled(false);
editor_toggled(true);
2016-12-14 00:21:06 +01:00
}
}
void SnapmaticEditor::on_rbCustom_toggled(bool checked)
{
if (checked)
{
2017-03-30 03:44:48 +02:00
selfie_toggled(false);
mugshot_toggled(false);
editor_toggled(false);
2016-12-14 00:21:06 +01:00
}
}
void SnapmaticEditor::setSnapmaticPicture(SnapmaticPicture *picture)
{
smpic = picture;
localSpJson = smpic->getSnapmaticProperties();
ui->rbCustom->setChecked(true);
2017-03-30 03:44:48 +02:00
crewID = localSpJson.crewID;
isSelfie = localSpJson.isSelfie;
isMugshot = localSpJson.isMug;
isEditor = localSpJson.isFromRSEditor;
2016-12-14 00:21:06 +01:00
ui->cbDirector->setChecked(localSpJson.isFromDirector);
ui->cbMeme->setChecked(localSpJson.isMeme);
2017-03-30 03:44:48 +02:00
if (isSelfie)
{
ui->rbSelfie->setChecked(true);
}
2017-03-30 03:44:48 +02:00
else if (isMugshot)
{
ui->rbMugshot->setChecked(true);
}
2017-03-30 03:44:48 +02:00
else if (isEditor)
{
ui->rbEditor->setChecked(true);
}
else
{
ui->rbCustom->setChecked(true);
}
2017-03-30 03:44:48 +02:00
setSnapmaticCrew(returnCrewName(crewID));
2017-03-01 15:19:40 +01:00
setSnapmaticTitle(picture->getPictureTitle());
}
void SnapmaticEditor::setSnapmaticTitle(const QString &title)
{
if (title.length() > 39)
{
snapmaticTitle = title.left(39);
}
else
{
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);
if (SnapmaticPicture::verifyTitle(snapmaticTitle))
{
ui->labAppropriate->setText(tr("Appropriate: %1").arg(QString("<span style=\"color: green\">%1</a>").arg(tr("Yes", "Yes, should work fine"))));
}
else
{
ui->labAppropriate->setText(tr("Appropriate: %1").arg(QString("<span style=\"color: red\">%1</a>").arg(tr("No", "No, could lead to issues"))));
}
2016-12-14 00:21:06 +01:00
}
2017-03-30 03:44: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);
}
QString SnapmaticEditor::returnCrewName(int crewID_)
{
return crewDB->getCrewName(crewID_);
}
2016-12-14 00:21:06 +01:00
void SnapmaticEditor::on_cmdCancel_clicked()
{
close();
}
void SnapmaticEditor::on_cmdApply_clicked()
{
if (ui->cbQualify->isChecked())
{
qualifyAvatar();
}
2017-03-30 03:44:48 +02:00
localSpJson.crewID = crewID;
localSpJson.isSelfie = isSelfie;
localSpJson.isMug = isMugshot;
localSpJson.isFromRSEditor = isEditor;
2016-12-14 00:21:06 +01:00
localSpJson.isFromDirector = ui->cbDirector->isChecked();
localSpJson.isMeme = ui->cbMeme->isChecked();
if (smpic)
{
2017-02-11 04:21:55 +01:00
QString originalFileName = smpic->getPictureFilePath();
2016-12-14 00:21:06 +01:00
QString adjustedFileName = originalFileName;
if (adjustedFileName.right(7) == ".hidden") // for the hidden file system
{
adjustedFileName.remove(adjustedFileName.length() - 7, 7);
}
2017-08-26 14:13:35 +02:00
QString backupFileName = adjustedFileName % ".bak";
2016-12-14 00:21:06 +01:00
if (!QFile::exists(backupFileName))
{
QFile::copy(adjustedFileName, backupFileName);
}
2017-03-01 15:19:40 +01:00
SnapmaticProperties fallbackProperties = smpic->getSnapmaticProperties();
QString fallbackTitle = smpic->getPictureTitle();
2016-12-14 00:21:06 +01:00
smpic->setSnapmaticProperties(localSpJson);
2017-03-01 15:19:40 +01:00
smpic->setPictureTitle(snapmaticTitle);
if (!smpic->exportPicture(originalFileName))
2016-12-14 00:21:06 +01:00
{
QMessageBox::warning(this, tr("Snapmatic Properties"), tr("Patching of Snapmatic Properties failed because of I/O Error"));
2017-03-01 15:19:40 +01:00
smpic->setSnapmaticProperties(fallbackProperties);
smpic->setPictureTitle(fallbackTitle);
}
else
{
smpic->emitUpdate();
2016-12-14 00:21:06 +01: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)
{
if (checked)
{
ui->cbMeme->setEnabled(false);
ui->cbDirector->setEnabled(false);
ui->rbCustom->setEnabled(false);
ui->rbSelfie->setEnabled(false);
ui->rbEditor->setEnabled(false);
ui->rbMugshot->setEnabled(false);
}
else
{
ui->cbMeme->setEnabled(true);
ui->rbCustom->setEnabled(true);
ui->rbSelfie->setEnabled(true);
ui->rbEditor->setEnabled(true);
ui->rbMugshot->setEnabled(true);
if (ui->rbSelfie->isChecked() || ui->rbCustom->isChecked())
{
ui->cbDirector->setEnabled(true);
}
}
}
2017-03-01 15:19:40 +01:00
void SnapmaticEditor::on_labTitle_linkActivated(const QString &link)
{
if (link == "g5e://edittitle")
{
bool ok;
QString newTitle = QInputDialog::getText(this, tr("Snapmatic Title"), tr("New Snapmatic title:"), QLineEdit::Normal, snapmaticTitle, &ok, windowFlags());
if (ok && !newTitle.isEmpty())
{
setSnapmaticTitle(newTitle);
}
}
}
2017-03-30 03:44:48 +02:00
void SnapmaticEditor::on_labCrew_linkActivated(const QString &link)
{
if (link == "g5e://editcrew")
{
bool ok;
int indexNum = 0;
QStringList itemList;
QStringList crewList = crewDB->getCrews();
2017-08-26 14:13:35 +02:00
if (!crewList.contains(QLatin1String("0")))
{
crewList.append(QLatin1String("0"));
}
2017-03-30 03:44:48 +02:00
crewList.sort();
foreach(const QString &crew, crewList)
{
itemList.append(QString("%1 (%2)").arg(crew, returnCrewName(crew.toInt())));
}
if (crewList.contains(QString::number(crewID)))
{
indexNum = crewList.indexOf(QRegExp(QString::number(crewID)));
}
QString newCrew = QInputDialog::getItem(this, tr("Snapmatic Crew"), tr("New Snapmatic crew:"), itemList, indexNum, true, &ok, windowFlags());
if (ok && !newCrew.isEmpty())
{
if (newCrew.contains(" ")) newCrew = newCrew.split(" ").at(0);
if (newCrew.length() > 10) return;
foreach (const QChar &crewChar, newCrew)
{
if (!crewChar.isNumber())
{
return;
}
}
crewID = newCrew.toInt();
setSnapmaticCrew(returnCrewName(crewID));
}
}
}