File indexing completed on 2025-01-26 04:10:26

0001 /*
0002  * layergroupswitcher.cpp -- Part of Krita
0003  *
0004  * SPDX-FileCopyrightText: 2013 Boudewijn Rempt (boud@valdyas.org)
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "layergroupswitcher.h"
0010 
0011 #include <klocalizedstring.h>
0012 #include <kis_debug.h>
0013 #include <kpluginfactory.h>
0014 
0015 #include <kis_image.h>
0016 #include <kis_layer.h>
0017 #include <kis_group_layer.h>
0018 #include <kis_node.h>
0019 #include <kis_node_manager.h>
0020 #include <kis_global.h>
0021 #include <kis_types.h>
0022 #include <KisViewManager.h>
0023 
0024 #include "kis_action.h"
0025 
0026 K_PLUGIN_FACTORY_WITH_JSON(LayerGroupSwitcherFactory, "kritalayergroupswitcher.json", registerPlugin<LayerGroupSwitcher>();)
0027 
0028 LayerGroupSwitcher::LayerGroupSwitcher(QObject *parent, const QVariantList &)
0029     : KisActionPlugin(parent)
0030 {
0031     KisAction *action  = new KisAction(i18n("Move into previous group"), this);
0032     addAction("LayerGroupSwitcher/previous", action);
0033     connect(action, SIGNAL(triggered()), this, SLOT(moveIntoPreviousGroup()));
0034 
0035     action = new KisAction(i18n("Move into next group"), this);
0036     addAction("LayerGroupSwitcher/next", action);
0037     connect(action, SIGNAL(triggered()), this, SLOT(moveIntoNextGroup()));
0038 }
0039 
0040 LayerGroupSwitcher::~LayerGroupSwitcher()
0041 {
0042 }
0043 
0044 void LayerGroupSwitcher::moveIntoNextGroup()
0045 {
0046     KisImageSP image = viewManager()->image().toStrongRef();
0047     if (!image) {
0048         return;
0049     }
0050     KisNodeManager *nodeManager = viewManager()->nodeManager();
0051     KisLayerSP active = nodeManager->activeLayer();
0052     if (!active) {
0053         return;
0054     }
0055     if (active->parentLayer().data() == image->rootLayer().data()) {
0056         active->setVisible(false);
0057         nodeManager->activateNextNode();
0058         active = nodeManager->activeLayer();
0059         if (active) {
0060             active->setVisible(true);
0061         }
0062     }
0063     else if (active->parent()) {
0064         KisNodeSP parent = active->parent();
0065         if (parent) {
0066             int indexInGroup = active->parent()->index(active);
0067             nodeManager->slotNonUiActivatedNode(parent);
0068             parent->setVisible(false);
0069             nodeManager->activateNextNode();
0070             active = nodeManager->activeLayer();
0071             if (active) {
0072                 active->setVisible(true);
0073             }
0074             KisNodeSP child = active->at(indexInGroup);
0075             if (child) {
0076                 nodeManager->slotNonUiActivatedNode(child);
0077                 child->setVisible(true);
0078             }
0079         }
0080     }
0081     image->refreshGraph();
0082 
0083 }
0084 
0085 void LayerGroupSwitcher::moveIntoPreviousGroup()
0086 {
0087     KisImageSP image = viewManager()->image().toStrongRef();
0088     if (!image) {
0089         return;
0090     }
0091     KisNodeManager *nodeManager = viewManager()->nodeManager();
0092     KisLayerSP active = nodeManager->activeLayer();
0093     if (!active) {
0094         return;
0095     }
0096     if (active->parentLayer().data() == image->rootLayer().data()) {
0097         active->setVisible(false);
0098         nodeManager->activatePreviousNode();
0099         active = nodeManager->activeLayer();
0100         if (active) {
0101             active->setVisible(true);
0102         }
0103     }
0104     else if (active->parent()) {
0105         KisNodeSP parent = active->parent();
0106         if (parent) {
0107             int indexInGroup = active->parent()->index(active);
0108             nodeManager->slotNonUiActivatedNode(parent);
0109             parent->setVisible(false);
0110             nodeManager->activatePreviousNode();
0111             active = nodeManager->activeLayer();
0112             if (active) {
0113                 active->setVisible(true);
0114             }
0115             KisNodeSP child = active->at(indexInGroup);
0116             if (child) {
0117                 nodeManager->slotNonUiActivatedNode(child);
0118                 child->setVisible(true);
0119             }
0120         }
0121     }
0122     image->refreshGraph();
0123 }
0124 
0125 #include "layergroupswitcher.moc"