|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | + |
| 5 | +#include "screenscale.h" |
| 6 | + |
| 7 | +#include <DConfig> |
| 8 | + |
| 9 | +#include <QDebug> |
| 10 | +#include <QJsonArray> |
| 11 | +#include <QJsonDocument> |
| 12 | +#include <QJsonObject> |
| 13 | + |
| 14 | +#include <algorithm> |
| 15 | +#include <cmath> |
| 16 | + |
| 17 | +ScreenScale::ScreenScale(QObject *parent) |
| 18 | + : QObject(parent) |
| 19 | +{ |
| 20 | + m_config = DTK_CORE_NAMESPACE::DConfig::create( |
| 21 | + "org.deepin.dde.daemon", "org.deepin.dde.ScreenScale1", "", this); |
| 22 | + if (!m_config || !m_config->isValid()) { |
| 23 | + qWarning() << "DConfig instance for ScreenScale is null or invalid"; |
| 24 | + m_config = nullptr; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/* |
| 29 | +测试入参示例: |
| 30 | +
|
| 31 | +1. 单屏 1920x1080: |
| 32 | +[{"widthPx":1920,"heightPx":1080,"widthMm":477,"heightMm":268}] |
| 33 | +
|
| 34 | +2. 单屏 4K: |
| 35 | +[{"widthPx":3840,"heightPx":2160,"widthMm":597,"heightMm":336}] |
| 36 | +
|
| 37 | +3. 笔记本 14英寸: |
| 38 | +[{"widthPx":1920,"heightPx":1080,"widthMm":310,"heightMm":174}] |
| 39 | +
|
| 40 | +4. 多屏 (笔记本 + 外接显示器): |
| 41 | +[{"widthPx":1920,"heightPx":1080,"widthMm":310,"heightMm":174},{"widthPx":2560,"heightPx":1440,"widthMm":597,"heightMm":336}] |
| 42 | +*/ |
| 43 | +QString ScreenScale::GetScreenScaleInfo(const QString &screensJson) |
| 44 | +{ |
| 45 | + qDebug() << "GetScreenScaleInfo input:" << screensJson; |
| 46 | + |
| 47 | + QJsonParseError error; |
| 48 | + QJsonDocument doc = QJsonDocument::fromJson(screensJson.toUtf8(), &error); |
| 49 | + if (error.error != QJsonParseError::NoError) { |
| 50 | + qWarning() << "Failed to parse screens JSON:" << error.errorString(); |
| 51 | + QString result = R"({"current":1.0,"recommended":1.0,"available":[1.0,1.25]})"; |
| 52 | + qDebug() << "GetScreenScaleInfo output:" << result; |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + QJsonArray screens = doc.array(); |
| 57 | + if (screens.isEmpty()) { |
| 58 | + QString result = R"({"current":1.0,"recommended":1.0,"available":[1.0,1.25]})"; |
| 59 | + qDebug() << "GetScreenScaleInfo output:" << result; |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + double step = getScaleStep(); |
| 64 | + |
| 65 | + // 计算推荐缩放 |
| 66 | + double recommended = 3.0; |
| 67 | + for (const auto &screenVal : screens) { |
| 68 | + QJsonObject screen = screenVal.toObject(); |
| 69 | + double scale = calcRecommendedScale( |
| 70 | + screen["widthPx"].toDouble(), |
| 71 | + screen["heightPx"].toDouble(), |
| 72 | + screen["widthMm"].toDouble(), |
| 73 | + screen["heightMm"].toDouble(), |
| 74 | + step); |
| 75 | + if (scale < recommended) { |
| 76 | + recommended = scale; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // 计算可用缩放列表(确保缩放后有效分辨率不小于 1024x768) |
| 81 | + double maxScale = 3.0; |
| 82 | + for (const auto &screenVal : screens) { |
| 83 | + QJsonObject screen = screenVal.toObject(); |
| 84 | + double widthPx = screen["widthPx"].toDouble(); |
| 85 | + double heightPx = screen["heightPx"].toDouble(); |
| 86 | + |
| 87 | + double limit = std::min(widthPx / 1024.0, heightPx / 768.0); |
| 88 | + if (limit < maxScale) { |
| 89 | + maxScale = limit; |
| 90 | + } |
| 91 | + } |
| 92 | + maxScale = std::clamp(std::floor(maxScale / step) * step, 1.0, 3.0); |
| 93 | + |
| 94 | + QJsonArray available; |
| 95 | + int count = static_cast<int>((maxScale - 1.0 + 0.0001) / step); |
| 96 | + for (int i = 0; i <= count; ++i) { |
| 97 | + available.append(1.0 + i * step); |
| 98 | + } |
| 99 | + |
| 100 | + // 确定当前缩放值 |
| 101 | + double current = recommended; |
| 102 | + if (m_config && m_config->isValid()) { |
| 103 | + double configured = m_config->value("scaleFactor").toDouble(); |
| 104 | + if (configured > 0.0) { |
| 105 | + for (const auto &v : available) { |
| 106 | + if (qFuzzyCompare(v.toDouble(), configured)) { |
| 107 | + current = configured; |
| 108 | + break; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + QJsonObject result; |
| 115 | + result["current"] = current; |
| 116 | + result["recommended"] = recommended; |
| 117 | + result["available"] = available; |
| 118 | + |
| 119 | + QString output = QJsonDocument(result).toJson(QJsonDocument::Compact); |
| 120 | + qDebug() << "GetScreenScaleInfo output:" << output; |
| 121 | + return output; |
| 122 | +} |
| 123 | + |
| 124 | +void ScreenScale::SetScaleFactor(double factor) |
| 125 | +{ |
| 126 | + qDebug() << "SetScaleFactor input:" << factor; |
| 127 | + |
| 128 | + if (!m_config || !m_config->isValid()) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (std::isnan(factor) || std::isinf(factor) || factor < 1.0 || factor > 3.0) { |
| 133 | + qWarning() << "Invalid scale factor:" << factor; |
| 134 | + if (calledFromDBus()) { |
| 135 | + sendErrorReply("org.deepin.dde.ScreenScale1.Error.InvalidParameter", |
| 136 | + "Invalid scale factor. Must be between 1.0 and 3.0."); |
| 137 | + } |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + double current = m_config->value("scaleFactor").toDouble(); |
| 142 | + if (qFuzzyCompare(current, factor)) { |
| 143 | + qDebug() << "Scale factor unchanged:" << factor; |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + m_config->setValue("scaleFactor", factor); |
| 148 | + qDebug() << "Scale factor set to:" << factor; |
| 149 | + Q_EMIT ScaleFactorChanged(factor); |
| 150 | +} |
| 151 | + |
| 152 | +double ScreenScale::getScaleStep() const |
| 153 | +{ |
| 154 | + double step = 0.25; |
| 155 | + if (m_config && m_config->isValid()) { |
| 156 | + step = m_config->value("scaleStep", 0.25).toDouble(); |
| 157 | + } |
| 158 | + return step > 0 ? step : 0.25; |
| 159 | +} |
| 160 | + |
| 161 | +double ScreenScale::calcRecommendedScale( |
| 162 | + double widthPx, double heightPx, double widthMm, double heightMm, double step) const |
| 163 | +{ |
| 164 | + if (widthMm <= 0.0 || heightMm <= 0.0) { |
| 165 | + return 1.0; |
| 166 | + } |
| 167 | + |
| 168 | + double lenPx = std::sqrt(widthPx * widthPx + heightPx * heightPx); |
| 169 | + double lenMm = std::sqrt(widthMm * widthMm + heightMm * heightMm); |
| 170 | + |
| 171 | + // 标准 1080p 21.5 英寸显示器 |
| 172 | + double lenPxStd = std::sqrt(1920.0 * 1920.0 + 1080.0 * 1080.0); |
| 173 | + double lenMmStd = std::sqrt(477.0 * 477.0 + 268.0 * 268.0); |
| 174 | + |
| 175 | + double a = 0.00158; // 经验修正系数 |
| 176 | + double fix = (lenMm - lenMmStd) * (lenPx / lenPxStd) * a; |
| 177 | + double scaleFactor = (lenPx / lenMm) / (lenPxStd / lenMmStd) + fix; |
| 178 | + |
| 179 | + // 对齐到 step |
| 180 | + return std::clamp(std::round(scaleFactor / step) * step, 1.0, 3.0); |
| 181 | +} |
0 commit comments