gta5view/src/SnapmaticWidget.cpp

496 lines
18 KiB
C++
Raw 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 "SnapmaticWidget.h"
#include "ui_SnapmaticWidget.h"
#include "MapLocationDialog.h"
#include "JsonEditorDialog.h"
2017-10-09 08:35:48 +02:00
#include "SnapmaticPicture.h"
#include "SnapmaticEditor.h"
#include "DatabaseThread.h"
#include "PictureDialog.h"
#include "PictureExport.h"
#include "StringParser.h"
2018-07-21 18:33:08 +02:00
#include "ImportDialog.h"
2017-10-09 08:35:48 +02:00
#include "AppEnv.h"
#include "config.h"
#include <QStringBuilder>
#include <QMessageBox>
#include <QPainter>
2017-10-09 08:35:48 +02:00
#include <QPixmap>
#include <QTimer>
#include <QMenu>
#include <QFile>
#ifdef GTA5SYNC_TELEMETRY
#include "TelemetryClass.h"
#include <QJsonDocument>
#include <QJsonObject>
#endif
2017-12-17 13:03:43 +01:00
SnapmaticWidget::SnapmaticWidget(ProfileDatabase *profileDB, CrewDatabase *crewDB, DatabaseThread *threadDB, QString profileName, QWidget *parent) :
ProfileWidget(parent), profileDB(profileDB), crewDB(crewDB), threadDB(threadDB), profileName(profileName),
2017-10-09 08:35:48 +02:00
ui(new Ui::SnapmaticWidget)
{
ui->setupUi(this);
ui->cmdView->setVisible(false);
ui->cmdCopy->setVisible(false);
ui->cmdExport->setVisible(false);
ui->cmdDelete->setVisible(false);
ui->cbSelected->setVisible(false);
QPalette palette;
palette.setCurrentColorGroup(QPalette::Disabled);
highlightHiddenColor = palette.text().color();
ui->SnapmaticFrame->setMouseTracking(true);
ui->labPicture->setMouseTracking(true);
ui->labPicStr->setMouseTracking(true);
ui->cbSelected->setMouseTracking(true);
smpic = nullptr;
}
SnapmaticWidget::~SnapmaticWidget()
{
delete ui;
}
void SnapmaticWidget::setSnapmaticPicture(SnapmaticPicture *picture)
{
smpic = picture;
QObject::connect(picture, SIGNAL(updated()), this, SLOT(snapmaticUpdated()));
2017-12-17 13:03:43 +01:00
QObject::connect(picture, SIGNAL(customSignal(QString)), this, SLOT(customSignal(QString)));
2017-10-09 08:35:48 +02:00
const qreal screenRatio = AppEnv::screenRatio();
const qreal screenRatioPR = AppEnv::screenRatioPR();
const QSize renderResolution(48 * screenRatio * screenRatioPR, 27 * screenRatio * screenRatioPR);
2017-10-09 08:35:48 +02:00
ui->labPicture->setFixedSize(48 * screenRatio, 27 * screenRatio);
2019-01-13 14:32:12 +01:00
ui->labPicture->setScaledContents(true);
QPixmap renderPixmap(renderResolution);
renderPixmap.fill(Qt::transparent);
QPainter renderPainter(&renderPixmap);
2021-02-15 23:12:44 +01:00
const QImage originalImage = picture->getImage();
const QImage renderImage = originalImage.scaled(renderResolution, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Stack smash
if (renderImage.width() < renderResolution.width()) {
renderPainter.drawImage((renderResolution.width() - renderImage.width()) / 2, 0, renderImage, Qt::AutoColor);
}
else if (renderImage.height() < renderResolution.height()) {
renderPainter.drawImage(0, (renderResolution.height() - renderImage.height()) / 2, renderImage, Qt::AutoColor);
}
else {
renderPainter.drawImage(0, 0, renderImage, Qt::AutoColor);
}
renderPainter.end();
2019-01-13 14:32:12 +01:00
#if QT_VERSION >= 0x050600
renderPixmap.setDevicePixelRatio(screenRatioPR);
2019-01-13 14:32:12 +01:00
#endif
ui->labPicStr->setText(smpic->getPictureStr() % "\n" % smpic->getPictureTitl());
ui->labPicture->setPixmap(renderPixmap);
2017-10-09 08:35:48 +02:00
picture->clearCache();
adjustTextColor();
}
void SnapmaticWidget::snapmaticUpdated()
{
2017-12-17 13:03:43 +01:00
ui->labPicStr->setText(smpic->getPictureStr() % "\n" % smpic->getPictureTitl());
}
void SnapmaticWidget::customSignal(QString signal)
{
2021-02-15 23:12:44 +01:00
if (signal == "PictureUpdated") {
2017-12-17 13:03:43 +01:00
QPixmap SnapmaticPixmap = QPixmap::fromImage(smpic->getImage().scaled(ui->labPicture->width(), ui->labPicture->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation), Qt::AutoColor);
ui->labPicture->setPixmap(SnapmaticPixmap);
}
2017-10-09 08:35:48 +02:00
}
void SnapmaticWidget::retranslate()
{
smpic->updateStrings();
2017-12-17 13:03:43 +01:00
ui->labPicStr->setText(smpic->getPictureStr() % "\n" % smpic->getPictureTitl());
2017-10-09 08:35:48 +02:00
}
void SnapmaticWidget::on_cmdView_clicked()
{
QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
settings.beginGroup("Interface");
2019-07-24 18:57:00 +02:00
bool navigationBar = settings.value("NavigationBar", true).toBool();
2017-10-09 08:35:48 +02:00
settings.endGroup();
2017-12-17 13:03:43 +01:00
PictureDialog *picDialog = new PictureDialog(profileDB, crewDB, profileName, this);
2017-10-09 08:35:48 +02:00
picDialog->setSnapmaticPicture(smpic, true);
picDialog->setModal(true);
// be ready for crewName and playerName updated
QObject::connect(threadDB, SIGNAL(crewNameUpdated()), picDialog, SLOT(crewNameUpdated()));
QObject::connect(threadDB, SIGNAL(playerNameUpdated()), picDialog, SLOT(playerNameUpdated()));
QObject::connect(picDialog, SIGNAL(nextPictureRequested()), this, SLOT(dialogNextPictureRequested()));
QObject::connect(picDialog, SIGNAL(previousPictureRequested()), this, SLOT(dialogPreviousPictureRequested()));
// add previous next buttons
2021-02-15 23:12:44 +01:00
if (navigationBar)
picDialog->addPreviousNextButtons();
2017-10-09 08:35:48 +02:00
// show picture dialog
#ifdef Q_OS_ANDROID
// Android ...
picDialog->showMaximized();
#else
picDialog->show();
2019-04-22 04:59:26 +02:00
if (navigationBar) picDialog->styliseDialog();
2017-10-09 08:35:48 +02:00
//picDialog->adaptNewDialogSize();
picDialog->setMinimumSize(picDialog->size());
picDialog->setMaximumSize(picDialog->size());
#endif
picDialog->exec();
delete picDialog;
}
void SnapmaticWidget::on_cmdCopy_clicked()
{
PictureExport::exportAsSnapmatic(this, smpic);
}
void SnapmaticWidget::on_cmdExport_clicked()
{
PictureExport::exportAsPicture(this, smpic);
}
void SnapmaticWidget::on_cmdDelete_clicked()
{
2021-02-15 23:12:44 +01:00
if (deletePicture())
emit pictureDeleted();
2017-10-09 08:35:48 +02:00
}
bool SnapmaticWidget::deletePicture()
{
2018-02-04 02:34:16 +01:00
int uchoice = QMessageBox::question(this, tr("Delete picture"), tr("Are you sure to delete %1 from your Snapmatic pictures?").arg("\""+smpic->getPictureTitle()+"\""), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
2021-02-15 23:12:44 +01:00
if (uchoice == QMessageBox::Yes) {
if (smpic->deletePictureFile()) {
#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"] = "DeleteSuccess";
2018-07-28 05:06:46 +02:00
jsonObject["ExtraFlags"] = "Snapmatic";
jsonObject["DeletedSize"] = QString::number(smpic->ragePhoto()->photoSize());
#if QT_VERSION >= 0x060000
jsonObject["DeletedTime"] = QString::number(QDateTime::currentDateTimeUtc().toSecsSinceEpoch());
#else
jsonObject["DeletedTime"] = QString::number(QDateTime::currentDateTimeUtc().toTime_t());
#endif
jsonDocument.setObject(jsonObject);
Telemetry->push(TelemetryCategory::PersonalData, jsonDocument);
}
#endif
2017-10-09 08:35:48 +02:00
return true;
}
2021-02-15 23:12:44 +01:00
else {
2018-02-04 02:34:16 +01:00
QMessageBox::warning(this, tr("Delete picture"), tr("Failed at deleting %1 from your Snapmatic pictures").arg("\""+smpic->getPictureTitle()+"\""));
2017-10-09 08:35:48 +02:00
}
}
return false;
}
void SnapmaticWidget::mousePressEvent(QMouseEvent *ev)
{
ProfileWidget::mousePressEvent(ev);
}
void SnapmaticWidget::mouseReleaseEvent(QMouseEvent *ev)
{
ProfileWidget::mouseReleaseEvent(ev);
2021-02-15 23:12:44 +01:00
if (ui->cbSelected->isVisible()) {
if (rect().contains(ev->pos()) && ev->button() == Qt::LeftButton) {
2017-10-09 08:35:48 +02:00
ui->cbSelected->setChecked(!ui->cbSelected->isChecked());
}
}
2021-02-15 23:12:44 +01:00
else {
2020-11-23 03:13:07 +01:00
const int contentMode = getContentMode();
2021-02-15 23:12:44 +01:00
if ((contentMode == 0 || contentMode == 10 || contentMode == 20) && rect().contains(ev->pos()) && ev->button() == Qt::LeftButton) {
if (ev->modifiers().testFlag(Qt::ShiftModifier)) {
2017-10-09 08:35:48 +02:00
ui->cbSelected->setChecked(!ui->cbSelected->isChecked());
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
on_cmdView_clicked();
}
}
2021-02-15 23:12:44 +01:00
else if (!ui->cbSelected->isVisible() && (contentMode == 1 || contentMode == 11 || contentMode == 21) && ev->button() == Qt::LeftButton && ev->modifiers().testFlag(Qt::ShiftModifier)) {
2017-10-09 08:35:48 +02:00
ui->cbSelected->setChecked(!ui->cbSelected->isChecked());
}
}
}
void SnapmaticWidget::mouseDoubleClickEvent(QMouseEvent *ev)
{
ProfileWidget::mouseDoubleClickEvent(ev);
2020-11-23 03:13:07 +01:00
const int contentMode = getContentMode();
2021-02-15 23:12:44 +01:00
if (!ui->cbSelected->isVisible() && (contentMode == 1 || contentMode == 11 || contentMode == 21) && ev->button() == Qt::LeftButton) {
2017-10-09 08:35:48 +02:00
on_cmdView_clicked();
}
}
void SnapmaticWidget::setSelected(bool isSelected)
{
ui->cbSelected->setChecked(isSelected);
}
void SnapmaticWidget::pictureSelected()
{
setSelected(!ui->cbSelected->isChecked());
}
void SnapmaticWidget::contextMenuEvent(QContextMenuEvent *ev)
{
emit contextMenuTriggered(ev);
}
void SnapmaticWidget::dialogNextPictureRequested()
{
emit nextPictureRequested((QWidget*)sender());
}
void SnapmaticWidget::dialogPreviousPictureRequested()
{
emit previousPictureRequested((QWidget*)sender());
}
void SnapmaticWidget::on_cbSelected_stateChanged(int arg1)
{
2021-02-15 23:12:44 +01:00
if (arg1 == Qt::Checked) {
2017-10-09 08:35:48 +02:00
emit widgetSelected();
}
2021-02-15 23:12:44 +01:00
else if (arg1 == Qt::Unchecked) {
2017-10-09 08:35:48 +02:00
emit widgetDeselected();
}
}
void SnapmaticWidget::adjustTextColor()
{
2021-02-15 23:12:44 +01:00
if (isHidden()) {
2017-10-09 08:35:48 +02:00
ui->labPicStr->setStyleSheet(QString("QLabel{color: rgb(%1, %2, %3);}").arg(QString::number(highlightHiddenColor.red()), QString::number(highlightHiddenColor.green()), QString::number(highlightHiddenColor.blue())));
}
2021-02-15 23:12:44 +01:00
else {
2017-10-09 08:35:48 +02:00
ui->labPicStr->setStyleSheet("");
}
}
bool SnapmaticWidget::makePictureHidden()
{
2021-02-15 23:12:44 +01:00
if (smpic->setPictureHidden()) {
2017-10-09 08:35:48 +02:00
adjustTextColor();
return true;
}
return false;
}
bool SnapmaticWidget::makePictureVisible()
{
2021-02-15 23:12:44 +01:00
if (smpic->setPictureVisible()) {
2017-10-09 08:35:48 +02:00
adjustTextColor();
return true;
}
return false;
}
void SnapmaticWidget::makePictureHiddenSlot()
{
2018-01-17 00:59:40 +01:00
if (!makePictureHidden())
2018-02-04 02:34:16 +01:00
QMessageBox::warning(this, QApplication::translate("UserInterface", "Hide In-game"), QApplication::translate("SnapmaticWidget", "Failed to hide %1 In-game from your Snapmatic pictures").arg("\""+smpic->getPictureTitle()+"\""));
2017-10-09 08:35:48 +02:00
}
void SnapmaticWidget::makePictureVisibleSlot()
{
2018-01-17 00:59:40 +01:00
if (!makePictureVisible())
2018-02-04 02:34:16 +01:00
QMessageBox::warning(this, QApplication::translate("UserInterface", "Show In-game"), QApplication::translate("SnapmaticWidget", "Failed to show %1 In-game from your Snapmatic pictures").arg("\""+smpic->getPictureTitle()+"\""));
2017-10-09 08:35:48 +02:00
}
void SnapmaticWidget::editSnapmaticProperties()
{
2017-12-12 04:45:46 +01:00
SnapmaticEditor *snapmaticEditor = new SnapmaticEditor(crewDB, profileDB, this);
2017-10-09 08:35:48 +02:00
snapmaticEditor->setSnapmaticPicture(smpic);
snapmaticEditor->setModal(true);
snapmaticEditor->show();
2017-10-09 08:35:48 +02:00
snapmaticEditor->exec();
delete snapmaticEditor;
}
void SnapmaticWidget::editSnapmaticRawJson()
{
JsonEditorDialog *jsonEditor = new JsonEditorDialog(smpic, this);
jsonEditor->setModal(true);
jsonEditor->show();
jsonEditor->exec();
delete jsonEditor;
}
2017-12-17 13:03:43 +01:00
void SnapmaticWidget::editSnapmaticImage()
{
2018-07-21 18:33:08 +02:00
QImage *currentImage = new QImage(smpic->getImage());
ImportDialog *importDialog = new ImportDialog(profileName, this);
importDialog->setImage(currentImage);
importDialog->enableOverwriteMode();
importDialog->setModal(true);
importDialog->exec();
2021-02-15 23:12:44 +01:00
if (importDialog->isImportAgreed()) {
2018-07-21 18:33:08 +02:00
const QByteArray previousPicture = smpic->getPictureStream();
2021-06-17 06:14:35 +02:00
bool success = smpic->setImage(importDialog->image(), importDialog->isUnlimitedBuffer());
2021-02-15 23:12:44 +01:00
if (success) {
2018-07-21 18:33:08 +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)) {
2018-07-21 18:33:08 +02:00
QFile::copy(currentFilePath, backupFileName);
}
2021-02-15 23:12:44 +01:00
if (!smpic->exportPicture(currentFilePath)) {
2018-07-21 18:33:08 +02:00
smpic->setPictureStream(previousPicture);
QMessageBox::warning(this, QApplication::translate("ImageEditorDialog", "Snapmatic Image Editor"), QApplication::translate("ImageEditorDialog", "Patching of Snapmatic Image failed because of I/O Error"));
return;
}
smpic->emitCustomSignal("PictureUpdated");
#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"] = "ImageEdited";
jsonObject["ExtraFlags"] = "Interface";
jsonObject["EditedSize"] = QString::number(smpic->ragePhoto()->photoSize());
#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
2018-07-21 18:33:08 +02:00
}
2021-02-15 23:12:44 +01:00
else {
2018-07-21 18:33:08 +02:00
QMessageBox::warning(this, QApplication::translate("ImageEditorDialog", "Snapmatic Image Editor"), QApplication::translate("ImageEditorDialog", "Patching of Snapmatic Image failed because of Image Error"));
return;
}
}
delete importDialog;
2017-12-17 13:03:43 +01:00
}
void SnapmaticWidget::openMapViewer()
{
SnapmaticPicture *picture = smpic;
2021-02-15 23:12:44 +01:00
SnapmaticProperties currentProperties = picture->getSnapmaticProperties();
MapLocationDialog *mapLocDialog = new MapLocationDialog(currentProperties.location.x, currentProperties.location.y, this);
mapLocDialog->setCayoPerico(currentProperties.location.isCayoPerico);
mapLocDialog->setModal(true);
mapLocDialog->show();
mapLocDialog->exec();
2021-02-15 23:12:44 +01:00
if (mapLocDialog->propUpdated()) {
// Update Snapmatic Properties
2021-02-15 23:12:44 +01:00
currentProperties.location.x = mapLocDialog->getXpos();
currentProperties.location.y = mapLocDialog->getYpos();
currentProperties.location.z = 0;
// Update Snapmatic Picture
QString currentFilePath = picture->getPictureFilePath();
QString originalFilePath = picture->getOriginalPictureFilePath();
QString backupFileName = originalFilePath % ".bak";
2021-02-15 23:12:44 +01:00
if (!QFile::exists(backupFileName)) {
QFile::copy(currentFilePath, backupFileName);
}
SnapmaticProperties fallbackProperties = picture->getSnapmaticProperties();
2021-02-15 23:12:44 +01:00
picture->setSnapmaticProperties(currentProperties);
if (!picture->exportPicture(currentFilePath)) {
QMessageBox::warning(this, SnapmaticEditor::tr("Snapmatic Properties"), SnapmaticEditor::tr("Patching of Snapmatic Properties failed because of I/O Error"));
picture->setSnapmaticProperties(fallbackProperties);
}
#ifdef GTA5SYNC_TELEMETRY
2021-02-15 23:12:44 +01:00
else {
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"] = "LocationEdited";
jsonObject["ExtraFlags"] = "Interface";
jsonObject["EditedSize"] = QString::number(smpic->ragePhoto()->photoSize());
#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
}
delete mapLocDialog;
}
2017-10-09 08:35:48 +02:00
bool SnapmaticWidget::isSelected()
{
return ui->cbSelected->isChecked();
}
bool SnapmaticWidget::isHidden()
{
return smpic->isHidden();
}
void SnapmaticWidget::setSelectionMode(bool selectionMode)
{
ui->cbSelected->setVisible(selectionMode);
}
void SnapmaticWidget::selectAllWidgets()
{
emit allWidgetsSelected();
}
void SnapmaticWidget::deselectAllWidgets()
{
emit allWidgetsDeselected();
}
SnapmaticPicture* SnapmaticWidget::getPicture()
{
return smpic;
}
QString SnapmaticWidget::getPicturePath()
{
return smpic->getPictureFilePath();
}
QString SnapmaticWidget::getWidgetType()
{
return "SnapmaticWidget";
}