File indexing completed on 2024-05-26 05:33:29

0001 /*
0002     SPDX-FileCopyrightText: 2022 Marco Martin <mart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "tileseditoreffect.h"
0007 #include "effect/effecthandler.h"
0008 
0009 #include <QAction>
0010 #include <QQuickItem>
0011 #include <QTimer>
0012 
0013 #include <KGlobalAccel>
0014 #include <KLocalizedString>
0015 
0016 namespace KWin
0017 {
0018 
0019 TilesEditorEffect::TilesEditorEffect()
0020     : m_shutdownTimer(std::make_unique<QTimer>())
0021 {
0022     m_shutdownTimer->setSingleShot(true);
0023     connect(m_shutdownTimer.get(), &QTimer::timeout, this, &TilesEditorEffect::realDeactivate);
0024     connect(effects, &EffectsHandler::screenAboutToLock, this, &TilesEditorEffect::realDeactivate);
0025 
0026     const QKeySequence defaultToggleShortcut = Qt::META | Qt::Key_T;
0027     m_toggleAction = std::make_unique<QAction>();
0028     connect(m_toggleAction.get(), &QAction::triggered, this, &TilesEditorEffect::toggle);
0029     m_toggleAction->setObjectName(QStringLiteral("Edit Tiles"));
0030     m_toggleAction->setText(i18n("Toggle Tiles Editor"));
0031     KGlobalAccel::self()->setDefaultShortcut(m_toggleAction.get(), {defaultToggleShortcut});
0032     KGlobalAccel::self()->setShortcut(m_toggleAction.get(), {defaultToggleShortcut});
0033     m_toggleShortcut = KGlobalAccel::self()->shortcut(m_toggleAction.get());
0034 
0035     setSource(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwin/effects/tileseditor/qml/main.qml"))));
0036 }
0037 
0038 TilesEditorEffect::~TilesEditorEffect()
0039 {
0040 }
0041 
0042 QVariantMap TilesEditorEffect::initialProperties(Output *screen)
0043 {
0044     return QVariantMap{
0045         {QStringLiteral("effect"), QVariant::fromValue(this)},
0046         {QStringLiteral("targetScreen"), QVariant::fromValue(screen)},
0047     };
0048 }
0049 
0050 void TilesEditorEffect::reconfigure(ReconfigureFlags)
0051 {
0052     setAnimationDuration(animationTime(200));
0053 }
0054 
0055 void TilesEditorEffect::toggle()
0056 {
0057     if (!isRunning()) {
0058         activate();
0059     } else {
0060         deactivate(0);
0061     }
0062 }
0063 
0064 void TilesEditorEffect::activate()
0065 {
0066     setRunning(true);
0067 }
0068 
0069 void TilesEditorEffect::deactivate(int timeout)
0070 {
0071     const auto screens = effects->screens();
0072     for (const auto screen : screens) {
0073         if (QuickSceneView *view = viewForScreen(screen)) {
0074             QMetaObject::invokeMethod(view->rootItem(), "stop");
0075         }
0076     }
0077 
0078     m_shutdownTimer->start(timeout);
0079 }
0080 
0081 void TilesEditorEffect::realDeactivate()
0082 {
0083     setRunning(false);
0084 }
0085 
0086 int TilesEditorEffect::animationDuration() const
0087 {
0088     return m_animationDuration;
0089 }
0090 
0091 void TilesEditorEffect::setAnimationDuration(int duration)
0092 {
0093     if (m_animationDuration != duration) {
0094         m_animationDuration = duration;
0095         Q_EMIT animationDurationChanged();
0096     }
0097 }
0098 
0099 int TilesEditorEffect::requestedEffectChainPosition() const
0100 {
0101     return 70;
0102 }
0103 
0104 void TilesEditorEffect::grabbedKeyboardEvent(QKeyEvent *keyEvent)
0105 {
0106     if (m_toggleShortcut.contains(keyEvent->key() | keyEvent->modifiers())) {
0107         if (keyEvent->type() == QEvent::KeyPress) {
0108             toggle();
0109         }
0110         return;
0111     }
0112     QuickSceneEffect::grabbedKeyboardEvent(keyEvent);
0113 }
0114 
0115 } // namespace KWin
0116 
0117 #include "moc_tileseditoreffect.cpp"