File indexing completed on 2024-04-28 05:36:25

0001 /*
0002     SPDX-FileCopyrightText: 2013 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2014 Sebastian Kügler <sebas@kde.org>
0004     SPDX-FileCopyrightText: 2014 David Edmundson <davidedmunsdon@kde.org>
0005     SPDX-FileCopyrightText: 2021 Jonah Brüchert <jbb@kaidan.im>
0006     SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 
0010 */
0011 
0012 #include "units.h"
0013 
0014 #include <KConfigGroup>
0015 #include <KSharedConfig>
0016 
0017 #include <cmath>
0018 
0019 constexpr int defaultLongDuration = 200;
0020 
0021 Units::Units(QObject *parent)
0022     : Kirigami::Platform::Units(parent)
0023     , m_animationSpeedWatcher(KConfigWatcher::create(KSharedConfig::openConfig()))
0024 {
0025     connect(m_animationSpeedWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) {
0026         if (group.name() == QLatin1String("KDE") && names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
0027             updateAnimationSpeed();
0028         }
0029     });
0030 
0031     updateAnimationSpeed();
0032 
0033     setGridUnit(gridUnit() + std::fmod(gridUnit(), 2));
0034     setSmallSpacing(4);
0035     setLargeSpacing(8);
0036 }
0037 
0038 // Copy from plasma-framework/src/declarativeimports/core/units.cpp, since we don't want to depend on plasma-framework here
0039 void Units::updateAnimationSpeed()
0040 {
0041     KConfigGroup generalCfg = KConfigGroup(KSharedConfig::openConfig(), QStringLiteral("KDE"));
0042     const qreal animationSpeedModifier = qMax(0.0, generalCfg.readEntry("AnimationDurationFactor", 1.0));
0043 
0044     // Read the old longDuration value for compatibility
0045     KConfigGroup cfg = KConfigGroup(KSharedConfig::openConfig(QStringLiteral("plasmarc")), QStringLiteral("Units"));
0046     int longDuration = cfg.readEntry("longDuration", defaultLongDuration);
0047 
0048     longDuration = qRound(longDuration * animationSpeedModifier);
0049 
0050     // Animators with a duration of 0 do not fire reliably
0051     // see Bug 357532 and QTBUG-39766
0052     longDuration = qMax(1, longDuration);
0053 
0054     setVeryShortDuration(longDuration / 4);
0055     setShortDuration(longDuration / 2);
0056     setLongDuration(longDuration);
0057     setVeryLongDuration(longDuration * 2);
0058 }
0059 
0060 #include "moc_units.cpp"