Android: Use FileUtils in SplashScreenContainerWidget

Change-Id: I6c03e3c99f676b79e673c272e838788ac7a0d3c7
Reviewed-by: Artem Sokolovskii <artem.sokolovskii@qt.io>
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
hjk
2021-08-17 16:19:15 +02:00
parent 6c45764fa1
commit d23afee096
3 changed files with 51 additions and 47 deletions

View File

@@ -28,6 +28,8 @@
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include <utils/filepath.h>
#include <utils/utilsicons.h>
#include <QCheckBox>
@@ -42,10 +44,11 @@
#include <QToolButton>
#include <QVBoxLayout>
using namespace Utils;
namespace Android {
namespace Internal {
namespace {
const char extraExtraExtraHighDpiImagePath[] = "/res/drawable-xxxhdpi/";
const char extraExtraHighDpiImagePath[] = "/res/drawable-xxhdpi/";
const char extraHighDpiImagePath[] = "/res/drawable-xhdpi/";
@@ -76,12 +79,10 @@ const int highDpiScalingRatio = 6;
const int mediumDpiScalingRatio = 4;
const int lowDpiScalingRatio = 3;
QString manifestDir(TextEditor::TextEditorWidget *textEditorWidget)
static FilePath manifestDir(TextEditor::TextEditorWidget *textEditorWidget)
{
// Get the manifest file's directory from its filepath.
return textEditorWidget->textDocument()->filePath().toFileInfo().absolutePath();
}
return textEditorWidget->textDocument()->filePath().absolutePath();
}
static SplashScreenWidget *addWidgetToPage(QWidget *page,
@@ -340,9 +341,9 @@ SplashScreenContainerWidget::SplashScreenContainerWidget(
emit splashScreensModified();
}
});
connect(m_masterImage, &QToolButton::clicked, [this]() {
const QString file = QFileDialog::getOpenFileName(this, tr("Select master image"),
QDir::homePath(), fileDialogImageFiles);
connect(m_masterImage, &QToolButton::clicked, [this] {
const FilePath file = FileUtils::getOpenFilePath(this, tr("Select master image"),
FileUtils::homePath(), fileDialogImageFiles);
if (!file.isEmpty()) {
for (auto &&imageWidget : m_imageWidgets)
imageWidget->setImageFromPath(file);
@@ -350,9 +351,9 @@ SplashScreenContainerWidget::SplashScreenContainerWidget(
emit splashScreensModified();
}
});
connect(m_portraitMasterImage, &QToolButton::clicked, [this]() {
const QString file = QFileDialog::getOpenFileName(this, tr("Select portrait master image"),
QDir::homePath(), fileDialogImageFiles);
connect(m_portraitMasterImage, &QToolButton::clicked, [this] {
const FilePath file = FileUtils::getOpenFilePath(this, tr("Select portrait master image"),
FileUtils::homePath(), fileDialogImageFiles);
if (!file.isEmpty()) {
for (auto &&imageWidget : m_portraitImageWidgets)
imageWidget->setImageFromPath(file);
@@ -360,9 +361,9 @@ SplashScreenContainerWidget::SplashScreenContainerWidget(
emit splashScreensModified();
}
});
connect(m_landscapeMasterImage, &QToolButton::clicked, [this]() {
const QString file = QFileDialog::getOpenFileName(this, tr("Select landscape master image"),
QDir::homePath(), fileDialogImageFiles);
connect(m_landscapeMasterImage, &QToolButton::clicked, [this] {
const FilePath file = FileUtils::getOpenFilePath(this, tr("Select landscape master image"),
FileUtils::homePath(), fileDialogImageFiles);
if (!file.isEmpty()) {
for (auto &&imageWidget : m_landscapeImageWidgets)
imageWidget->setImageFromPath(file);
@@ -406,7 +407,8 @@ void SplashScreenContainerWidget::loadImages()
void SplashScreenContainerWidget::loadSplashscreenDrawParams(const QString &name)
{
QFile file(QLatin1String("%1/res/drawable/%2.xml").arg(manifestDir(m_textEditorWidget)).arg(name));
const FilePath filePath = manifestDir(m_textEditorWidget).pathAppended("res/drawable/" + name + ".xml");
QFile file(filePath.toString());
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QXmlStreamReader reader(&file);
reader.setNamespaceProcessing(false);
@@ -524,7 +526,7 @@ void SplashScreenContainerWidget::setImageShowMode(const QString &mode)
void SplashScreenContainerWidget::createSplashscreenThemes()
{
const QString baseDir = manifestDir(m_textEditorWidget);
const QString baseDir = manifestDir(m_textEditorWidget).toString();
const QStringList splashscreenThemeFiles = {"/res/values/splashscreentheme.xml",
"/res/values-port/splashscreentheme.xml",
"/res/values-land/splashscreentheme.xml"};
@@ -596,18 +598,18 @@ void SplashScreenContainerWidget::createSplashscreenThemes()
void SplashScreenContainerWidget::checkSplashscreenImage(const QString &name)
{
if (isSplashscreenEnabled()) {
const QString baseDir = manifestDir(m_textEditorWidget);
const QStringList paths = {extraExtraExtraHighDpiImagePath,
extraExtraHighDpiImagePath,
extraHighDpiImagePath,
highDpiImagePath,
mediumDpiImagePath,
lowDpiImagePath};
const FilePath baseDir = manifestDir(m_textEditorWidget);
const QString paths[] = {extraExtraExtraHighDpiImagePath,
extraExtraHighDpiImagePath,
extraHighDpiImagePath,
highDpiImagePath,
mediumDpiImagePath,
lowDpiImagePath};
for (const QString &path : paths) {
const QString filePath(baseDir + path + name);
if (QFile::exists(filePath + ".png")
|| QFile::exists(filePath + ".jpg")) {
const FilePath filePath = baseDir.pathAppended(path + name);
if (filePath.stringAppended(".png").exists()
|| filePath.stringAppended(".jpg").exists()) {
setCurrentIndex(1);
break;
}

View File

@@ -38,17 +38,17 @@
#include <QToolButton>
#include <QVBoxLayout>
using namespace Utils;
namespace Android {
namespace Internal {
namespace {
static Q_LOGGING_CATEGORY(androidManifestEditorLog, "qtc.android.splashScreenWidget", QtWarningMsg)
const auto fileDialogImageFiles = QStringLiteral("%1 (*.png *.jpg)").arg(QWidget::tr("Images"));
QString manifestDir(TextEditor::TextEditorWidget *textEditorWidget)
FilePath manifestDir(TextEditor::TextEditorWidget *textEditorWidget)
{
// Get the manifest file's directory from its filepath.
return textEditorWidget->textDocument()->filePath().toFileInfo().absolutePath();
}
return textEditorWidget->textDocument()->filePath().absolutePath();
}
SplashScreenWidget::SplashScreenWidget(QWidget *parent) : QWidget(parent)
@@ -149,23 +149,22 @@ void SplashScreenWidget::showImageFullScreen(bool fullScreen)
loadImage();
}
void SplashScreenWidget::setImageFromPath(const QString &imagePath, bool resize)
void SplashScreenWidget::setImageFromPath(const FilePath &imagePath, bool resize)
{
if (!m_textEditorWidget)
return;
const QString baseDir = manifestDir(m_textEditorWidget);
const QString targetPath = baseDir + m_imagePath + m_imageFileName;
const FilePath baseDir = manifestDir(m_textEditorWidget);
const FilePath targetPath = baseDir / m_imagePath / m_imageFileName;
if (targetPath.isEmpty()) {
qCDebug(androidManifestEditorLog) << "Image target path is empty, cannot set image.";
return;
}
QImage image = QImage(imagePath);
QImage image = QImage(imagePath.toString());
if (image.isNull()) {
qCDebug(androidManifestEditorLog) << "Image '" << imagePath << "' not found or invalid format.";
return;
}
QDir dir;
if (!dir.mkpath(QFileInfo(targetPath).absolutePath())) {
if (!targetPath.absolutePath().ensureWritableDir()) {
qCDebug(androidManifestEditorLog) << "Cannot create image target path.";
return;
}
@@ -174,7 +173,7 @@ void SplashScreenWidget::setImageFromPath(const QString &imagePath, bool resize)
(float(image.height()) / float(m_maxScalingRatio)) * float(m_scalingRatio));
image = image.scaled(size);
}
QFile file(targetPath);
QFile file(targetPath.toString());
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
image.save(&file, "PNG");
file.close();
@@ -188,8 +187,9 @@ void SplashScreenWidget::setImageFromPath(const QString &imagePath, bool resize)
void SplashScreenWidget::selectImage()
{
const QString file = QFileDialog::getOpenFileName(this, m_imageSelectionText,
QDir::homePath(), fileDialogImageFiles);
const FilePath file = FileUtils::getOpenFilePath(this, m_imageSelectionText,
FileUtils::homePath(),
QStringLiteral("%1 (*.png *.jpg)").arg(tr("Images")));
if (file.isEmpty())
return;
setImageFromPath(file, false);
@@ -198,13 +198,13 @@ void SplashScreenWidget::selectImage()
void SplashScreenWidget::removeImage()
{
const QString baseDir = manifestDir(m_textEditorWidget);
const QString targetPath = baseDir + m_imagePath + m_imageFileName;
const FilePath baseDir = manifestDir(m_textEditorWidget);
const FilePath targetPath = baseDir / m_imagePath / m_imageFileName;
if (targetPath.isEmpty()) {
qCDebug(androidManifestEditorLog) << "Image target path empty, cannot remove image.";
return;
}
QFile::remove(targetPath);
targetPath.removeFile();
m_image = QImage();
m_splashScreenButton->update();
setScaleWarningLabelVisible(false);
@@ -222,13 +222,13 @@ void SplashScreenWidget::loadImage()
qCDebug(androidManifestEditorLog) << "Image name not set, cannot load image.";
return;
}
const QString baseDir = manifestDir(m_textEditorWidget);
const QString targetPath = baseDir + m_imagePath + m_imageFileName;
const FilePath baseDir = manifestDir(m_textEditorWidget);
const FilePath targetPath = baseDir / m_imagePath / m_imageFileName;
if (targetPath.isEmpty()) {
qCDebug(androidManifestEditorLog) << "Image target path empty, cannot load image.";
return;
}
QImage image = QImage(targetPath);
QImage image = QImage(targetPath.toString());
if (image.isNull()) {
qCDebug(androidManifestEditorLog) << "Cannot load image.";
return;

View File

@@ -25,6 +25,8 @@
#pragma once
#include <utils/filepath.h>
#include <QToolButton>
namespace TextEditor { class TextEditorWidget; }
@@ -66,7 +68,7 @@ public:
void clearImage();
void setBackgroundColor(const QColor &backgroundColor);
void showImageFullScreen(bool fullScreen);
void setImageFromPath(const QString &imagePath, bool resize = true);
void setImageFromPath(const Utils::FilePath &imagePath, bool resize = true);
void setImageFileName(const QString &imageFileName);
void loadImage();