forked from qt-creator/qt-creator
Android: Use FileUtils in AndroidManifestEditor
Also fix an untimely translation and its context. Change-Id: I9859216a458d5cff93bc9caa93164435b86d8383 Reviewed-by: Artem Sokolovskii <artem.sokolovskii@qt.io> Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
@@ -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.manifestEditor", QtWarningMsg)
|
static Q_LOGGING_CATEGORY(androidManifestEditorLog, "qtc.android.manifestEditor", QtWarningMsg)
|
||||||
const auto fileDialogIconFiles = QWidget::tr("Images (*.png *.jpg *.webp *.svg)");
|
|
||||||
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();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AndroidManifestEditorIconWidget::AndroidManifestEditorIconWidget(QWidget *parent) : QWidget(parent)
|
AndroidManifestEditorIconWidget::AndroidManifestEditorIconWidget(QWidget *parent) : QWidget(parent)
|
||||||
@@ -132,18 +132,17 @@ void AndroidManifestEditorIconWidget::clearIcon()
|
|||||||
|
|
||||||
void AndroidManifestEditorIconWidget::loadIcon()
|
void AndroidManifestEditorIconWidget::loadIcon()
|
||||||
{
|
{
|
||||||
QString baseDir = manifestDir(m_textEditorWidget);
|
const FilePath baseDir = manifestDir(m_textEditorWidget);
|
||||||
QString iconFile = baseDir + m_targetIconPath + m_targetIconFileName;
|
setIconFromPath(baseDir / m_targetIconPath / m_targetIconFileName);
|
||||||
setIconFromPath(iconFile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AndroidManifestEditorIconWidget::setIconFromPath(const QString &iconPath)
|
void AndroidManifestEditorIconWidget::setIconFromPath(const FilePath &iconPath)
|
||||||
{
|
{
|
||||||
if (!m_textEditorWidget)
|
if (!m_textEditorWidget)
|
||||||
return;
|
return;
|
||||||
m_iconPath = iconPath;
|
m_iconPath = iconPath;
|
||||||
QString baseDir = manifestDir(m_textEditorWidget);
|
FilePath baseDir = manifestDir(m_textEditorWidget);
|
||||||
QImage original(iconPath);
|
QImage original(iconPath.toString());
|
||||||
if (!original.isNull() && m_scaledToOriginalAspectRatio) {
|
if (!original.isNull() && m_scaledToOriginalAspectRatio) {
|
||||||
if ((original.width() > original.height() && m_buttonSize.height() > m_buttonSize.width())
|
if ((original.width() > original.height() && m_buttonSize.height() > m_buttonSize.width())
|
||||||
|| (original.height() > original.width() && m_buttonSize.width() > m_buttonSize.height())) {
|
|| (original.height() > original.width() && m_buttonSize.width() > m_buttonSize.height())) {
|
||||||
@@ -159,30 +158,30 @@ void AndroidManifestEditorIconWidget::setIconFromPath(const QString &iconPath)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
copyIcon();
|
copyIcon();
|
||||||
QString iconFile = baseDir + m_targetIconPath + m_targetIconFileName;
|
FilePath iconFile = baseDir + m_targetIconPath + m_targetIconFileName;
|
||||||
m_button->setIcon(QIcon(iconFile));
|
m_button->setIcon(QIcon(iconFile.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void AndroidManifestEditorIconWidget::selectIcon()
|
void AndroidManifestEditorIconWidget::selectIcon()
|
||||||
{
|
{
|
||||||
QString file = QFileDialog::getOpenFileName(this, m_iconSelectionText,
|
FilePath file = FileUtils::getOpenFilePath(this, m_iconSelectionText,
|
||||||
QDir::homePath(), fileDialogIconFiles);
|
FileUtils::homePath(),
|
||||||
|
tr("Images (*.png *.jpg *.webp *.svg)"));
|
||||||
if (file.isEmpty())
|
if (file.isEmpty())
|
||||||
return;
|
return;
|
||||||
setIconFromPath(file);
|
setIconFromPath(file);
|
||||||
emit iconSelected(file, this);
|
emit iconSelected(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AndroidManifestEditorIconWidget::removeIcon()
|
void AndroidManifestEditorIconWidget::removeIcon()
|
||||||
{
|
{
|
||||||
QString baseDir = manifestDir(m_textEditorWidget);
|
const FilePath baseDir = manifestDir(m_textEditorWidget);
|
||||||
const QString targetPath = baseDir + m_targetIconPath + m_targetIconFileName;
|
const FilePath targetPath = baseDir / m_targetIconPath / m_targetIconFileName;
|
||||||
if (targetPath.isEmpty()) {
|
if (targetPath.isEmpty()) {
|
||||||
qCDebug(androidManifestEditorLog) << "Icon target path empty, cannot remove icon.";
|
qCDebug(androidManifestEditorLog) << "Icon target path empty, cannot remove icon.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QFile targetFile(targetPath);
|
targetPath.removeFile();
|
||||||
targetFile.remove();
|
|
||||||
m_iconPath.clear();
|
m_iconPath.clear();
|
||||||
setScaleWarningLabelVisible(false);
|
setScaleWarningLabelVisible(false);
|
||||||
m_button->setIcon(QIcon());
|
m_button->setIcon(QIcon());
|
||||||
@@ -255,14 +254,9 @@ static QImage scaleWithoutStretching(const QImage& original, const QSize& target
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool similarFilesExist(const QString &path)
|
static bool similarFilesExist(const FilePath &path)
|
||||||
{
|
{
|
||||||
QFileInfo fileInfo(path);
|
const FilePaths entries = path.parentDir().dirEntries({path.completeBaseName() + ".*"}, {});
|
||||||
QDir imageDir(fileInfo.absolutePath());
|
|
||||||
QString baseName(fileInfo.completeBaseName());
|
|
||||||
baseName.append(QLatin1String(".*"));
|
|
||||||
imageDir.setNameFilters({baseName});
|
|
||||||
auto entries = imageDir.entryList();
|
|
||||||
return !entries.empty();
|
return !entries.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,13 +264,14 @@ void AndroidManifestEditorIconWidget::copyIcon()
|
|||||||
{
|
{
|
||||||
if (m_targetIconPath.isEmpty())
|
if (m_targetIconPath.isEmpty())
|
||||||
return;
|
return;
|
||||||
QString baseDir = manifestDir(m_textEditorWidget);
|
|
||||||
const QString targetPath = baseDir + m_targetIconPath + m_targetIconFileName;
|
const FilePath baseDir = manifestDir(m_textEditorWidget);
|
||||||
|
const FilePath targetPath = baseDir / m_targetIconPath / m_targetIconFileName;
|
||||||
if (targetPath.isEmpty()) {
|
if (targetPath.isEmpty()) {
|
||||||
qCDebug(androidManifestEditorLog) << "Icon target path empty, cannot copy icon.";
|
qCDebug(androidManifestEditorLog) << "Icon target path empty, cannot copy icon.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QImage original(m_iconPath);
|
QImage original(m_iconPath.toString());
|
||||||
if (m_iconPath != targetPath)
|
if (m_iconPath != targetPath)
|
||||||
removeIcon();
|
removeIcon();
|
||||||
if (original.isNull()) {
|
if (original.isNull()) {
|
||||||
@@ -287,8 +282,7 @@ void AndroidManifestEditorIconWidget::copyIcon()
|
|||||||
if (m_iconPath == targetPath)
|
if (m_iconPath == targetPath)
|
||||||
return;
|
return;
|
||||||
if (!targetPath.isEmpty() && !original.isNull()) {
|
if (!targetPath.isEmpty() && !original.isNull()) {
|
||||||
QDir dir;
|
if (!targetPath.absolutePath().ensureWritableDir()) {
|
||||||
if (!dir.mkpath(QFileInfo(targetPath).absolutePath())) {
|
|
||||||
qCDebug(androidManifestEditorLog) << "Cannot create icon target path.";
|
qCDebug(androidManifestEditorLog) << "Cannot create icon target path.";
|
||||||
m_iconPath.clear();
|
m_iconPath.clear();
|
||||||
return;
|
return;
|
||||||
@@ -300,7 +294,7 @@ void AndroidManifestEditorIconWidget::copyIcon()
|
|||||||
else
|
else
|
||||||
scaled = scaleWithoutStretching(original, m_iconSize);
|
scaled = scaleWithoutStretching(original, m_iconSize);
|
||||||
setScaleWarningLabelVisible(scaled.width() > original.width() || scaled.height() > original.height());
|
setScaleWarningLabelVisible(scaled.width() > original.width() || scaled.height() > original.height());
|
||||||
scaled.save(targetPath);
|
scaled.save(targetPath.toString());
|
||||||
m_iconPath = targetPath;
|
m_iconPath = targetPath;
|
||||||
} else {
|
} else {
|
||||||
m_iconPath.clear();
|
m_iconPath.clear();
|
||||||
|
@@ -25,11 +25,11 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <utils/filepath.h>
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
namespace TextEditor {
|
namespace TextEditor { class TextEditorWidget; }
|
||||||
class TextEditorWidget;
|
|
||||||
}
|
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QLabel;
|
class QLabel;
|
||||||
@@ -42,6 +42,7 @@ namespace Internal {
|
|||||||
class AndroidManifestEditorIconWidget : public QWidget
|
class AndroidManifestEditorIconWidget : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit AndroidManifestEditorIconWidget(QWidget *parent);
|
explicit AndroidManifestEditorIconWidget(QWidget *parent);
|
||||||
AndroidManifestEditorIconWidget(QWidget *parent,
|
AndroidManifestEditorIconWidget(QWidget *parent,
|
||||||
@@ -55,7 +56,7 @@ public:
|
|||||||
void setIcon(const QIcon &icon);
|
void setIcon(const QIcon &icon);
|
||||||
void clearIcon();
|
void clearIcon();
|
||||||
void loadIcon();
|
void loadIcon();
|
||||||
void setIconFromPath(const QString &iconPath);
|
void setIconFromPath(const Utils::FilePath &iconPath);
|
||||||
bool hasIcon() const;
|
bool hasIcon() const;
|
||||||
void setScaledToOriginalAspectRatio(bool scaled);
|
void setScaledToOriginalAspectRatio(bool scaled);
|
||||||
void setScaledWithoutStretching(bool scaled);
|
void setScaledWithoutStretching(bool scaled);
|
||||||
@@ -63,8 +64,9 @@ public:
|
|||||||
void setTargetIconPath(const QString &targetIconPath);
|
void setTargetIconPath(const QString &targetIconPath);
|
||||||
QString targetIconFileName() const;
|
QString targetIconFileName() const;
|
||||||
QString targetIconPath() const;
|
QString targetIconPath() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void iconSelected(const QString &path, AndroidManifestEditorIconWidget* iconWidget);
|
void iconSelected(const Utils::FilePath &path);
|
||||||
void iconRemoved();
|
void iconRemoved();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -78,7 +80,7 @@ private:
|
|||||||
QSize m_buttonSize;
|
QSize m_buttonSize;
|
||||||
QLabel *m_scaleWarningLabel = nullptr;
|
QLabel *m_scaleWarningLabel = nullptr;
|
||||||
TextEditor::TextEditorWidget *m_textEditorWidget = nullptr;
|
TextEditor::TextEditorWidget *m_textEditorWidget = nullptr;
|
||||||
QString m_iconPath;
|
Utils::FilePath m_iconPath;
|
||||||
QString m_targetIconPath;
|
QString m_targetIconPath;
|
||||||
QString m_targetIconFileName;
|
QString m_targetIconFileName;
|
||||||
QString m_iconSelectionText;
|
QString m_iconSelectionText;
|
||||||
|
Reference in New Issue
Block a user