!186 feat: 支持 DTK 5.6.4 关于对话框“版本特性”显示功能

Merge pull request !186 from zty199/auto-7392693-dev-f4457187
This commit is contained in:
shenmo 2023-02-16 00:09:05 +00:00 committed by Gitee
commit 4114b51d87
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
7 changed files with 145 additions and 6 deletions

View File

@ -1,10 +1,14 @@
#include "application.h"
#include "mainwindow-dtk.h"
#include "utils/utils.h"
#include <DPlatformWindowHandle>
#include <DLog>
#include <DGuiApplicationHelper>
#include <DAboutDialog>
#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0))
#include <DFeatureDisplayDialog>
#endif
#include <QSettings>
#include <QStandardPaths>
@ -43,6 +47,13 @@ Application::Application(int &argc, char **argv)
// 初始化日志模块 (默认日志位置 ~/.cache/spark-union/spark-store)
DLogManager::registerConsoleAppender();
DLogManager::registerFileAppender();
// 获取版本特性信息
m_featuresJsonObj = Utils::parseFeatureJsonFile();
m_version = m_featuresJsonObj.value("version").toString(); // 获取版本号
#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0))
initFeatureDisplayDialog(); // 初始化版本特性对话框
#endif
}
void Application::handleAboutAction()
@ -65,9 +76,8 @@ void Application::checkAppConfigLocation()
}
}
void Application::setVersionAndBuildDateTime(const QString &version, const QString &buildDateTime)
void Application::setBuildDateTime(const QString &buildDateTime)
{
m_version = version;
m_buildDateTime = buildDateTime;
QSettings config(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/config.ini", QSettings::IniFormat);
@ -109,6 +119,9 @@ void Application::initAboutDialog()
dialog->setProductName(productName());
dialog->setProductIcon(productIcon());
dialog->setVersion(translate("DAboutDialog", "Version: %1").arg(applicationVersion()));
#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0))
dialog->setVersion(applicationVersion());
#endif
// 根据 shenmo 要求,不显示组织 Logo
// dialog->setCompanyLogo(QPixmap(":/icon/Logo-Spark.png"));
dialog->setCompanyLogo(QPixmap());
@ -118,9 +131,70 @@ void Application::initAboutDialog()
dialog->setLicense(translate("DAboutDialog", "%1 is released under %2").arg(productName()).arg(applicationLicense()));
setAboutDialog(dialog);
connect(aboutDialog(), &DAboutDialog::destroyed, this, [=] {
connect(aboutDialog(), &DAboutDialog::destroyed, this, [=]() {
setAboutDialog(nullptr);
});
#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0))
connect(aboutDialog(), &DAboutDialog::featureActivated, this, [=]() {
featureDisplayDialog()->show();
});
#endif
dialog->hide();
}
#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0))
/**
* @brief Application::initFeatureDisplayDialog
*/
void Application::initFeatureDisplayDialog()
{
if (featureDisplayDialog())
{
featureDisplayDialog()->deleteLater();
setFeatureDisplayDialog(nullptr);
}
// 自定义 DFeatureDisplayDialog
DFeatureDisplayDialog *dialog = new DFeatureDisplayDialog(m_mainWindow);
// 标题
dialog->setTitle(m_featuresJsonObj.value("title").toString());
// NOTE: json 文件中支持多语言;考虑到维护性,不放入翻译文件处理
if (m_featuresJsonObj.contains(QString("title[%1]").arg(QLocale::system().name())))
{
dialog->setTitle(m_featuresJsonObj.value(QString("title[%1]").arg(QLocale::system().name())).toString());
}
// 特性项
QList<DFeatureItem *> items;
foreach (const QJsonValue &jsonValue, m_featuresJsonObj.value("items").toArray())
{
QJsonObject jsonObj = jsonValue.toObject();
QString name = jsonObj.value("name").toString();
if (jsonObj.contains(QString("name[%1]").arg(QLocale::system().name())))
{
name = jsonObj.value(QString("name[%1]").arg(QLocale::system().name())).toString();
}
QString description = jsonObj.value("description").toString();
if (jsonObj.contains(QString("description[%1]").arg(QLocale::system().name())))
{
description = jsonObj.value(QString("description[%1]").arg(QLocale::system().name())).toString();
}
DFeatureItem *item = new DFeatureItem(QIcon::fromTheme("spark-store"), name, description, dialog);
items.append(item);
}
dialog->addItems(items); // NOTE: 也支持 addItem 依次添加单个 item
// “了解更多”链接按钮
dialog->setLinkUrl(m_featuresJsonObj.value("linkUrl").toString());
dialog->setLinkButtonVisible(m_featuresJsonObj.value("linkButtonVisible").toBool());
setFeatureDisplayDialog(dialog);
connect(featureDisplayDialog(), &DFeatureDisplayDialog::destroyed, this, [=]() {
setFeatureDisplayDialog(nullptr);
});
dialog->hide();
}
#endif

View File

@ -3,6 +3,8 @@
#include <DApplication>
#include <QJsonObject>
DWIDGET_USE_NAMESPACE
class MainWindow;
@ -16,13 +18,17 @@ public:
static void checkAppConfigLocation();
void setVersionAndBuildDateTime(const QString &version, const QString &buildDateTime);
void setBuildDateTime(const QString &buildDateTime);
void setMainWindow(MainWindow *window);
private:
void initAboutDialog();
#if (DTK_VERSION >= DTK_VERSION_CHECK(5, 6, 4, 0))
void initFeatureDisplayDialog();
#endif
private:
QJsonObject m_featuresJsonObj;
QString m_version;
QString m_buildDateTime;

View File

@ -44,6 +44,7 @@
<file>icon/light/text.svg</file>
<file>icon/light/update.svg</file>
<file>icon/logo.svg</file>
<file>json/features.json</file>
<file>tags/a2d-small.png</file>
<file>tags/a2d.png</file>
<file>tags/community-small.png</file>

View File

@ -0,0 +1,26 @@
{
"version": "4.2.3",
"title": "Features",
"title[zh_CN]": "版本特性",
"items": [
{
"icon": "/usr/share/icons/hicolor/scalable/apps/spark-store.svg",
"name": "Feature 1",
"name[zh_CN]": "特性 1",
"description": "Feature 1 detailed description...",
"description[zh_CN]": "特性 1 详细说明"
},
{
"icon": ":/icon/logo.svg",
"name": "Feature 2",
"description": "Feature 2 detailed description..."
},
{
"icon": "spark-store",
"name": "Fix 1",
"description": "Fix 1 detailed description..."
}
],
"linkUrl": "https://gitee.com/deepin-community-store/spark-store/releases/latest",
"linkButtonVisible": true
}

View File

@ -18,7 +18,6 @@ DWIDGET_USE_NAMESPACE
int main(int argc, char *argv[])
{
// Get build time
static const QString version = "4.2.3";
static const QDate buildDate = QLocale(QLocale::English).toDate(QString(__DATE__).replace(" ", " 0"), "MMM dd yyyy");
static const QTime buildTime = QTime::fromString(__TIME__, "hh:mm:ss");
static const QString buildDateTime = buildDate.toString("yyyy.MM.dd") + "-" + buildTime.toString("hh:mm:ss");
@ -59,7 +58,7 @@ int main(int argc, char *argv[])
int fakeArgc = argc + 2; // QCoreApplication 的 argc 要用引用,避免 c++ 编译器优化
Application a(fakeArgc, fakeArgs.data());
// 设置版本和构建时间
a.setVersionAndBuildDateTime(version, buildDateTime);
a.setBuildDateTime(buildDateTime);
// 限制单实例运行
if (!a.setSingleInstance("spark-store"))

View File

@ -8,6 +8,7 @@
#include <QSettings>
#include <QStandardPaths>
#include <QFile>
#include <QJsonDocument>
#define UOSDeveloperModeFile "/var/lib/deepin/developer-mode/enabled"
@ -182,3 +183,33 @@ void Utils::checkUOSDeveloperMode()
file.close();
config.sync(); // 写入更改至 config.ini并同步最新内容
}
/**
* @brief Utils::parseFeatureJsonFile json
* @return QJsonObject
*/
QJsonObject Utils::parseFeatureJsonFile()
{
QFile file(":/json/features.json");
if (!file.open(QFile::ReadOnly))
{
qWarning() << Q_FUNC_INFO << "features.json open failed";
return QJsonObject();
}
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(file.readAll(), &error);
if (error.error != QJsonParseError::NoError || jsonDoc.isNull())
{
qWarning() << Q_FUNC_INFO << "features.json validate failed:" << error.errorString();
return QJsonObject();
}
if (jsonDoc.isEmpty() || !jsonDoc.isObject())
{
qWarning() << Q_FUNC_INFO << "features jsonDoc parse failed:" << jsonDoc;
return QJsonObject();
}
return jsonDoc.object();
}

View File

@ -2,6 +2,7 @@
#define UTILS_H
#include <QObject>
#include <QJsonObject>
class Utils
{
@ -13,6 +14,7 @@ public:
static bool isUOS();
static void setQPAPlatform();
static void checkUOSDeveloperMode();
static QJsonObject parseFeatureJsonFile();
};
#endif // UTILS_H