2023-02-16 13:51:55 +08:00
|
|
|
|
// Copyright (C) 2018 ~ 2020 Uniontech Technology Co., Ltd.
|
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
2020-08-05 19:46:15 +08:00
|
|
|
|
#ifndef UTILS
|
|
|
|
|
#define UTILS
|
2018-08-23 16:44:39 +08:00
|
|
|
|
#include <QPixmap>
|
|
|
|
|
#include <QImageReader>
|
|
|
|
|
#include <QApplication>
|
2019-04-23 14:28:09 +08:00
|
|
|
|
#include <QScreen>
|
2020-08-19 14:13:35 +08:00
|
|
|
|
#include <QGSettings>
|
2021-03-20 12:10:45 +08:00
|
|
|
|
#include <QDebug>
|
2018-08-23 16:44:39 +08:00
|
|
|
|
|
2021-12-31 15:09:12 +08:00
|
|
|
|
#include "imageutil.h"
|
|
|
|
|
|
2018-08-23 16:44:39 +08:00
|
|
|
|
namespace Utils {
|
2020-08-19 14:13:35 +08:00
|
|
|
|
|
2020-09-11 17:55:36 +08:00
|
|
|
|
#define ICBC_CONF_FILE "/etc/deepin/icbc.conf"
|
2020-08-19 14:13:35 +08:00
|
|
|
|
|
2021-08-06 17:23:05 +08:00
|
|
|
|
const bool IS_WAYLAND_DISPLAY = !qgetenv("WAYLAND_DISPLAY").isEmpty();
|
|
|
|
|
|
2022-12-12 09:52:52 +00:00
|
|
|
|
inline bool isDraging()
|
|
|
|
|
{
|
|
|
|
|
if (!qApp->property("isDraging").isValid())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return qApp->property("isDraging").toBool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void setIsDraging(bool isDraging)
|
|
|
|
|
{
|
|
|
|
|
qApp->setProperty("isDraging", isDraging);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 12:10:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief SettingsPtr 根据给定信息返回一个QGSettings指针
|
|
|
|
|
* @param schema_id The id of the schema
|
|
|
|
|
* @param path If non-empty, specifies the path for a relocatable schema
|
|
|
|
|
* @param parent 创建指针的付对象
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2021-08-23 11:39:52 +08:00
|
|
|
|
inline QGSettings *SettingsPtr(const QString &schema_id, const QByteArray &path = QByteArray(), QObject *parent = nullptr)
|
|
|
|
|
{
|
2021-03-20 12:10:45 +08:00
|
|
|
|
if (QGSettings::isSchemaInstalled(schema_id.toUtf8())) {
|
|
|
|
|
QGSettings *settings = new QGSettings(schema_id.toUtf8(), path, parent);
|
|
|
|
|
return settings;
|
|
|
|
|
}
|
|
|
|
|
qDebug() << "Cannot find gsettings, schema_id:" << schema_id;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief SettingsPtr 根据给定信息返回一个QGSettings指针
|
|
|
|
|
* @param module 传入QGSettings构造函数时,会添加"com.deepin.dde.dock.module."前缀
|
|
|
|
|
* @param path If non-empty, specifies the path for a relocatable schema
|
|
|
|
|
* @param parent 创建指针的付对象
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2021-08-23 11:39:52 +08:00
|
|
|
|
inline const QGSettings *ModuleSettingsPtr(const QString &module, const QByteArray &path = QByteArray(), QObject *parent = nullptr)
|
|
|
|
|
{
|
2021-03-20 12:10:45 +08:00
|
|
|
|
return SettingsPtr("com.deepin.dde.dock.module." + module, path, parent);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-24 17:56:20 +08:00
|
|
|
|
/* convert 'some-key' to 'someKey' or 'SomeKey'.
|
|
|
|
|
* the second form is needed for appending to 'set' for 'setSomeKey'
|
|
|
|
|
*/
|
|
|
|
|
inline QString qtify_name(const char *name)
|
|
|
|
|
{
|
|
|
|
|
bool next_cap = false;
|
|
|
|
|
QString result;
|
|
|
|
|
|
|
|
|
|
while (*name) {
|
|
|
|
|
if (*name == '-') {
|
|
|
|
|
next_cap = true;
|
|
|
|
|
} else if (next_cap) {
|
|
|
|
|
result.append(QChar(*name).toUpper().toLatin1());
|
|
|
|
|
next_cap = false;
|
|
|
|
|
} else {
|
|
|
|
|
result.append(*name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-20 12:10:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief SettingValue 根据给定信息返回获取的值
|
|
|
|
|
* @param schema_id The id of the schema
|
|
|
|
|
* @param path If non-empty, specifies the path for a relocatable schema
|
|
|
|
|
* @param key 对应信息的key值
|
|
|
|
|
* @param fallback 如果找不到信息,返回此默认值
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2021-08-23 11:39:52 +08:00
|
|
|
|
inline const QVariant SettingValue(const QString &schema_id, const QByteArray &path = QByteArray(), const QString &key = QString(), const QVariant &fallback = QVariant())
|
|
|
|
|
{
|
2021-03-20 12:10:45 +08:00
|
|
|
|
const QGSettings *settings = SettingsPtr(schema_id, path);
|
2021-03-24 17:56:20 +08:00
|
|
|
|
|
|
|
|
|
if (settings && ((settings->keys().contains(key)) || settings->keys().contains(qtify_name(key.toUtf8().data())))) {
|
2021-03-20 12:10:45 +08:00
|
|
|
|
QVariant v = settings->get(key);
|
|
|
|
|
delete settings;
|
|
|
|
|
return v;
|
|
|
|
|
} else{
|
|
|
|
|
qDebug() << "Cannot find gsettings, schema_id:" << schema_id
|
|
|
|
|
<< " path:" << path << " key:" << key
|
|
|
|
|
<< "Use fallback value:" << fallback;
|
2021-08-23 11:39:52 +08:00
|
|
|
|
// 如果settings->keys()不包含key则会存在内存泄露,所以需要释放
|
|
|
|
|
if (settings)
|
|
|
|
|
delete settings;
|
|
|
|
|
|
2021-03-20 12:10:45 +08:00
|
|
|
|
return fallback;
|
|
|
|
|
}
|
2021-03-12 13:20:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-23 11:39:52 +08:00
|
|
|
|
inline bool SettingSaveValue(const QString &schema_id, const QByteArray &path, const QString &key, const QVariant &value)
|
|
|
|
|
{
|
|
|
|
|
QGSettings *settings = SettingsPtr(schema_id, path);
|
|
|
|
|
|
|
|
|
|
if (settings && ((settings->keys().contains(key)) || settings->keys().contains(qtify_name(key.toUtf8().data())))) {
|
|
|
|
|
settings->set(key, value);
|
|
|
|
|
delete settings;
|
|
|
|
|
return true;
|
|
|
|
|
} else{
|
|
|
|
|
qDebug() << "Cannot find gsettings, schema_id:" << schema_id
|
|
|
|
|
<< " path:" << path << " key:" << key;
|
|
|
|
|
if (settings)
|
|
|
|
|
delete settings;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline QPixmap renderSVG(const QString &path, const QSize &size, const qreal devicePixelRatio)
|
|
|
|
|
{
|
2018-08-23 16:44:39 +08:00
|
|
|
|
QImageReader reader;
|
|
|
|
|
QPixmap pixmap;
|
|
|
|
|
reader.setFileName(path);
|
|
|
|
|
if (reader.canRead()) {
|
2019-04-28 10:10:42 +08:00
|
|
|
|
reader.setScaledSize(size * devicePixelRatio);
|
2018-08-23 16:44:39 +08:00
|
|
|
|
pixmap = QPixmap::fromImage(reader.read());
|
2019-04-28 10:10:42 +08:00
|
|
|
|
pixmap.setDevicePixelRatio(devicePixelRatio);
|
2018-08-23 16:44:39 +08:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pixmap.load(path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return pixmap;
|
2020-08-06 14:56:44 +08:00
|
|
|
|
}
|
2019-04-23 14:28:09 +08:00
|
|
|
|
|
2021-03-20 12:10:45 +08:00
|
|
|
|
inline QScreen *screenAt(const QPoint &point) {
|
2020-08-06 14:56:44 +08:00
|
|
|
|
for (QScreen *screen : qApp->screens()) {
|
|
|
|
|
const QRect r { screen->geometry() };
|
|
|
|
|
const QRect rect { r.topLeft(), r.size() * screen->devicePixelRatio() };
|
|
|
|
|
if (rect.contains(point)) {
|
|
|
|
|
return screen;
|
2019-04-23 14:28:09 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 14:56:44 +08:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2020-06-21 17:51:23 +08:00
|
|
|
|
|
2020-08-06 14:56:44 +08:00
|
|
|
|
//!!! 注意:这里传入的QPoint是未计算缩放的
|
2021-03-20 12:10:45 +08:00
|
|
|
|
inline QScreen *screenAtByScaled(const QPoint &point) {
|
2020-08-06 14:56:44 +08:00
|
|
|
|
for (QScreen *screen : qApp->screens()) {
|
|
|
|
|
const QRect r { screen->geometry() };
|
|
|
|
|
QRect rect { r.topLeft(), r.size() * screen->devicePixelRatio() };
|
|
|
|
|
if (rect.contains(point)) {
|
|
|
|
|
return screen;
|
|
|
|
|
}
|
2019-04-23 14:28:09 +08:00
|
|
|
|
}
|
2020-08-06 14:56:44 +08:00
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2021-03-12 13:20:13 +08:00
|
|
|
|
|
2020-11-06 17:52:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 比较两个插件版本号的大小
|
|
|
|
|
* @param pluginApi1 第一个插件版本号
|
|
|
|
|
* @param pluginApi2 第二个插件版本号
|
|
|
|
|
* @return 0:两个版本号相等,1:第一个版本号大,-1:第二个版本号大
|
|
|
|
|
*/
|
2021-08-23 11:39:52 +08:00
|
|
|
|
inline int comparePluginApi(const QString &pluginApi1, const QString &pluginApi2)
|
|
|
|
|
{
|
2020-11-06 17:52:08 +08:00
|
|
|
|
// 版本号相同
|
|
|
|
|
if (pluginApi1 == pluginApi2)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// 拆分版本号
|
2022-11-04 06:29:03 +00:00
|
|
|
|
QStringList subPluginApis1 = pluginApi1.split(".", Qt::SkipEmptyParts, Qt::CaseSensitive);
|
|
|
|
|
QStringList subPluginApis2 = pluginApi2.split(".", Qt::SkipEmptyParts, Qt::CaseSensitive);
|
2020-11-06 17:52:08 +08:00
|
|
|
|
for (int i = 0; i < subPluginApis1.size(); ++i) {
|
|
|
|
|
auto subPluginApi1 = subPluginApis1[i];
|
|
|
|
|
if (subPluginApis2.size() > i) {
|
|
|
|
|
auto subPluginApi2 = subPluginApis2[i];
|
|
|
|
|
|
|
|
|
|
// 相等判断下一个子版本号
|
|
|
|
|
if (subPluginApi1 == subPluginApi2)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// 转成整形比较
|
|
|
|
|
if (subPluginApi1.toInt() > subPluginApi2.toInt()) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 循环结束但是没有返回,说明子版本号个数不同,且前面的子版本号都相同
|
|
|
|
|
// 子版本号多的版本号大
|
|
|
|
|
if (subPluginApis1.size() > subPluginApis2.size()) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-31 15:09:12 +08:00
|
|
|
|
|
|
|
|
|
inline void updateCursor(QWidget *w)
|
|
|
|
|
{
|
|
|
|
|
static QString lastCursorTheme;
|
|
|
|
|
static int lastCursorSize = 0;
|
|
|
|
|
QString theme = Utils::SettingValue("com.deepin.xsettings", "/com/deepin/xsettings/", "gtk-cursor-theme-name", "bloom").toString();
|
|
|
|
|
int cursorSize = Utils::SettingValue("com.deepin.xsettings", "/com/deepin/xsettings/", "gtk-cursor-theme-size", 24).toInt();
|
|
|
|
|
if (theme != lastCursorTheme || cursorSize != lastCursorSize) {
|
|
|
|
|
QCursor *cursor = ImageUtil::loadQCursorFromX11Cursor(theme.toStdString().c_str(), "left_ptr", cursorSize);
|
|
|
|
|
if (!cursor)
|
|
|
|
|
return;
|
|
|
|
|
lastCursorTheme = theme;
|
|
|
|
|
lastCursorSize = cursorSize;
|
|
|
|
|
w->setCursor(*cursor);
|
|
|
|
|
static QCursor *lastArrowCursor = nullptr;
|
|
|
|
|
if (lastArrowCursor)
|
|
|
|
|
delete lastArrowCursor;
|
|
|
|
|
|
|
|
|
|
lastArrowCursor = cursor;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 14:28:09 +08:00
|
|
|
|
}
|
2020-08-05 19:46:15 +08:00
|
|
|
|
|
|
|
|
|
#endif // UTILS
|