Changeset View
Changeset View
Standalone View
Standalone View
kcm/src/scalingconfig.cpp
Show All 24 Lines | |||||
25 | #include <KConfigGroup> | 25 | #include <KConfigGroup> | ||
26 | 26 | | |||
27 | #include <KScreen/Output> | 27 | #include <KScreen/Output> | ||
28 | 28 | | |||
29 | //we want a scale between 1 and 3.0 in intervals of 0.1 | 29 | //we want a scale between 1 and 3.0 in intervals of 0.1 | ||
30 | //slider can only handle ints so goes 10-30 | 30 | //slider can only handle ints so goes 10-30 | ||
31 | #define SLIDER_RATIO 10.0 | 31 | #define SLIDER_RATIO 10.0 | ||
32 | 32 | | |||
33 | ScalingConfig::ScalingConfig(const KScreen::OutputList &outputList, QWidget* parent): | 33 | ScalingConfig::ScalingConfig(const KScreen::OutputPtr &output, QWidget* parent): | ||
34 | QDialog(parent), | 34 | QDialog(parent), | ||
35 | m_outputList(outputList) | 35 | m_output(output) | ||
36 | { | 36 | { | ||
37 | 37 | | |||
38 | ui.setupUi(this); | 38 | ui.setupUi(this); | ||
39 | 39 | | |||
40 | ui.warningWidget->setText(i18n("Scaling changes will come into effect after restart")); | 40 | ui.warningWidget->setText(i18n("Scaling changes will come into effect after restart")); | ||
41 | ui.warningWidget->show(); | 41 | ui.warningWidget->show(); | ||
42 | 42 | | |||
43 | 43 | | |||
Show All 11 Lines | |||||
55 | } | 55 | } | ||
56 | 56 | | |||
57 | ScalingConfig::~ScalingConfig() | 57 | ScalingConfig::~ScalingConfig() | ||
58 | { | 58 | { | ||
59 | } | 59 | } | ||
60 | 60 | | |||
61 | void ScalingConfig::load() | 61 | void ScalingConfig::load() | ||
62 | { | 62 | { | ||
63 | //we load UI from a config, as rdb value might not be updated yet | 63 | m_initialScalingFactor = 1.0; | ||
64 | | ||||
64 | auto config = KSharedConfig::openConfig(QStringLiteral("kdeglobals")); | 65 | auto config = KSharedConfig::openConfig(QStringLiteral("kdeglobals")); | ||
65 | const qreal dpi = config->group("KScreen").readEntry("ScaleFactor", 1.0); | 66 | const QString screenFactorsString = config->group("KScreen").readEntry("ScreenScaleFactors"); | ||
67 | const QStringList screenFactorList = screenFactorsString.split(';'); | ||||
68 | for (const QString screenFactor : screenFactorList) { | ||||
69 | const QStringList parts = screenFactor.split('='); | ||||
70 | QString name = parts[0]; | ||||
71 | qreal factor; | ||||
72 | bool ok; | ||||
66 | 73 | | |||
67 | m_initialScalingFactor = dpi; | 74 | if (parts.size() == 2 && (factor = parts[1].toDouble(&ok), ok)) { | ||
68 | ui.scaleSlider->setValue(dpi * SLIDER_RATIO); | 75 | m_screenFactors[name] = factor; | ||
76 | if (name == m_output->name()) { | ||||
77 | m_initialScalingFactor = factor; | ||||
78 | } | ||||
79 | } | ||||
80 | } | ||||
81 | | ||||
82 | ui.scaleSlider->setValue(m_initialScalingFactor * SLIDER_RATIO); | ||||
69 | } | 83 | } | ||
70 | 84 | | |||
71 | void ScalingConfig::accept() | 85 | void ScalingConfig::accept() | ||
72 | { | 86 | { | ||
73 | if (scaleFactor() == m_initialScalingFactor) { | 87 | const qreal scalingFactor = scaleFactor(); | ||
88 | if (scalingFactor == m_initialScalingFactor) { | ||||
74 | QDialog::accept(); | 89 | QDialog::accept(); | ||
75 | return; | 90 | return; | ||
76 | } | 91 | } | ||
77 | const qreal scalingFactor = scaleFactor(); | | |||
78 | 92 | | |||
79 | //save to config | 93 | m_screenFactors[m_output->name()] = scalingFactor; | ||
80 | auto config = KSharedConfig::openConfig(QStringLiteral("kdeglobals")); | | |||
81 | config->group("KScreen").writeEntry("ScaleFactor", scalingFactor); | | |||
82 | 94 | | |||
83 | //write env var to be used by startkde.sh to populate the QT_SCREEN_SCALE_FACTORS env var | 95 | //write env var to be used by startkde.sh to populate the QT_SCREEN_SCALE_FACTORS env var | ||
84 | //we use QT_SCREEN_SCALE_FACTORS as opposed to QT_SCALE_FACTOR as we need to use one that will *NOT* scale fonts according to the scale | 96 | auto config = KSharedConfig::openConfig(QStringLiteral("kdeglobals")); | ||
85 | //scaling the fonts makes sense if you don't also set a font DPI, but we *need* to set a font DPI for both PlasmaShell which does it's own thing, and for KDE4/GTK2 applications | | |||
86 | | ||||
87 | QString screenFactors; | 97 | QString screenFactors; | ||
88 | foreach (const KScreen::OutputPtr &output, m_outputList) { | 98 | for (auto screenFactor = m_screenFactors.constBegin(); screenFactor != m_screenFactors.constEnd(); screenFactor++) { | ||
89 | screenFactors.append(output->name() + QLatin1Char('=') + QString::number(scalingFactor) + QLatin1Char(';')); | 99 | screenFactors.append(screenFactor.key() + QLatin1Char('=') + QString::number(screenFactor.value()) + QLatin1Char(';')); | ||
90 | } | 100 | } | ||
91 | config->group("KScreen").writeEntry("ScreenScaleFactors", screenFactors); | 101 | config->group("KScreen").writeEntry("ScreenScaleFactors", screenFactors); | ||
92 | 102 | | |||
93 | 103 | //The forced font DPI is used both by Plasmashell and non-Qt 5 applications, so it's set | |||
94 | KConfig fontConfig("kcmfonts"); | 104 | //to the DPI of the primary monitor. | ||
95 | auto fontConfigGroup = fontConfig.group("General"); | 105 | if(m_output->isPrimary()) { | ||
96 | 106 | const qreal value = qFuzzyCompare(scalingFactor, 1.0) ? 0 : scaleDPI(); | |||
97 | if (qFuzzyCompare(scalingFactor, 1.0)) { | 107 | KConfig("kcmfonts").group("General").writeEntry("forceFontDPI", value); | ||
98 | //if dpi is the default (96) remove the entry rather than setting it | 108 | //It's not applied immediately as ScreenScaleFactors only takes effect on in next session, | ||
99 | QProcess proc; | 109 | //which would cause a mismatch between scale and font size. | ||
100 | proc.start(QStringLiteral("xrdb -quiet -remove -nocpp")); | | |||
101 | if (proc.waitForStarted()) { | | |||
102 | proc.write(QByteArray("Xft.dpi\n")); | | |||
103 | proc.closeWriteChannel(); | | |||
104 | proc.waitForFinished(); | | |||
105 | } | | |||
106 | fontConfigGroup.writeEntry("forceFontDPI", 0); | | |||
107 | } else { | | |||
108 | QProcess proc; | | |||
109 | proc.start(QStringLiteral("xrdb -quiet -merge -nocpp")); | | |||
110 | if (proc.waitForStarted()) { | | |||
111 | proc.write(QByteArray("Xft.dpi: " + QString::number(scaleDPI()).toLatin1())); | | |||
112 | proc.closeWriteChannel(); | | |||
113 | proc.waitForFinished(); | | |||
114 | } | | |||
115 | fontConfigGroup.writeEntry("forceFontDPI", scaleDPI()); | | |||
116 | } | 110 | } | ||
117 | 111 | | |||
118 | | ||||
119 | QDialog::accept(); | 112 | QDialog::accept(); | ||
120 | } | 113 | } | ||
121 | 114 | | |||
122 | int ScalingConfig::scaleDPI() const | 115 | int ScalingConfig::scaleDPI() const | ||
123 | { | 116 | { | ||
124 | return qRound(scaleFactor() * 96.0); | 117 | return qRound(scaleFactor() * 96.0); | ||
125 | } | 118 | } | ||
126 | 119 | | |||
127 | qreal ScalingConfig::scaleFactor() const | 120 | qreal ScalingConfig::scaleFactor() const | ||
128 | { | 121 | { | ||
129 | return ui.scaleSlider->value() / SLIDER_RATIO; | 122 | return ui.scaleSlider->value() / SLIDER_RATIO; | ||
130 | } | 123 | } | ||
131 | 124 | |