File indexing completed on 2024-12-22 04:10:02

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Sven Langkamp <sven.langkamp@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_selection_commands.h"
0008 
0009 #include <klocalizedstring.h>
0010 
0011 #include "kis_image.h"
0012 #include "kis_default_bounds.h"
0013 #include "KisImageResolutionProxy.h"
0014 #include "kis_selection.h"
0015 #include "kis_undo_adapter.h"
0016 #include "kis_selection_mask.h"
0017 #include "kis_pixel_selection.h"
0018 #include "KisImageGlobalSelectionManagementInterface.h"
0019 #include "kis_group_layer.h"
0020 
0021 #include "kis_image_layer_remove_command.h"
0022 #include "kis_image_layer_add_command.h"
0023 #include "kis_selection_mask.h"
0024 #include "kis_activate_selection_mask_command.h"
0025 #include "KisChangeValueCommand.h"
0026 #include "KisChangeDeselectedMaskCommand.h"
0027 #include "KisNotifySelectionChangedCommand.h"
0028 
0029 
0030 KisSetGlobalSelectionCommand::KisSetGlobalSelectionCommand(KisImageWSP image, KisSelectionSP selection)
0031     : m_image(image)
0032 {
0033     KisImageSP imageSP = m_image.toStrongRef();
0034     if (!image) {
0035         return;
0036     }
0037     m_oldSelection = imageSP->globalSelection();
0038     m_newSelection = selection;
0039 }
0040 
0041 void KisSetGlobalSelectionCommand::populateChildCommands()
0042 {
0043     KisImageSP image = m_image.toStrongRef();
0044     KIS_SAFE_ASSERT_RECOVER_RETURN(image);
0045 
0046     addCommand(new KisNotifySelectionChangedCommand(image, KisNotifySelectionChangedCommand::INITIALIZING));
0047 
0048     KisSelectionMaskSP selectionMask = image->rootLayer()->selectionMask();
0049     if (selectionMask) {
0050         addCommand(new KisImageLayerRemoveCommand(image, selectionMask, false, false));
0051     }
0052 
0053     if (m_newSelection) {
0054         selectionMask = new KisSelectionMask(image, i18n("Selection Mask"));
0055         selectionMask->initSelection(image->rootLayer());
0056 
0057         // If we do not set the selection now, the setActive call coming next
0058         // can be very, very expensive, depending on the size of the image.
0059         selectionMask->setSelection(m_newSelection);
0060 
0061         addCommand(new KisImageLayerAddCommand(image, selectionMask,
0062                                                image->root(), image->root()->lastChild(),
0063                                                false, false));
0064         addCommand(new KisActivateSelectionMaskCommand(selectionMask, true));
0065 
0066     }
0067 
0068 
0069     addCommand(new KisChangeDeselectedMaskCommand(image));
0070     addCommand(new KisNotifySelectionChangedCommand(image, KisNotifySelectionChangedCommand::FINALIZING));
0071 }
0072 
0073 KisSetEmptyGlobalSelectionCommand::KisSetEmptyGlobalSelectionCommand(KisImageWSP image)
0074     : KisSetGlobalSelectionCommand(image,
0075                                    new KisSelection(new KisSelectionEmptyBounds(image),
0076                                                     toQShared(new KisImageResolutionProxy(image))))
0077 {
0078 }