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

View File

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

View File

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