File indexing completed on 2025-01-26 04:08:11

0001 /*
0002  * imagesize.cc -- Part of Krita
0003  *
0004  * SPDX-FileCopyrightText: 2004 Boudewijn Rempt (boud@valdyas.org)
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "imagesize.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_global.h>
0018 #include <kis_types.h>
0019 #include <KisViewManager.h>
0020 
0021 #include <kis_image_manager.h>
0022 #include <kis_node_manager.h>
0023 #include <kis_group_layer.h>
0024 #include <kis_selection_mask.h>
0025 #include <kis_selection.h>
0026 
0027 #include "dlg_imagesize.h"
0028 #include "dlg_canvassize.h"
0029 #include "dlg_layersize.h"
0030 #include "kis_filter_strategy.h"
0031 #include "kis_action.h"
0032 #include "kis_action_manager.h"
0033 
0034 ImageSize::ImageSize(QObject *parent)
0035     : KisActionPlugin(parent)
0036 {
0037 
0038     KisAction *action  = createAction("imagesize");
0039     connect(action, SIGNAL(triggered()), this, SLOT(slotImageSize()));
0040 
0041     action = createAction("canvassize");
0042     connect(action, SIGNAL(triggered()), this, SLOT(slotCanvasSize()));
0043 
0044     action = createAction("layersize");
0045     connect(action, SIGNAL(triggered()), this, SLOT(slotLayerSize()));
0046 
0047     action = createAction("scaleAllLayers");
0048     connect(action, SIGNAL(triggered()), this, SLOT(slotScaleAllLayers()));
0049 
0050     action  = createAction("selectionscale");
0051     connect(action, SIGNAL(triggered()), this, SLOT(slotSelectionScale()));
0052 }
0053 
0054 ImageSize::~ImageSize()
0055 {
0056 }
0057 
0058 void ImageSize::slotImageSize()
0059 {
0060     KisImageSP image = viewManager()->image().toStrongRef();
0061     if (!image) return;
0062 
0063     if (!viewManager()->blockUntilOperationsFinished(image)) return;
0064 
0065     DlgImageSize * dlgImageSize = new DlgImageSize(viewManager()->mainWindowAsQWidget(), image->width(), image->height(), image->yRes());
0066     Q_CHECK_PTR(dlgImageSize);
0067     dlgImageSize->setObjectName("ImageSize");
0068 
0069     if (dlgImageSize->exec() == QDialog::Accepted) {
0070         const QSize desiredSize(dlgImageSize->desiredWidth(), dlgImageSize->desiredHeight());
0071         double res = dlgImageSize->desiredResolution();
0072         viewManager()->imageManager()->scaleCurrentImage(desiredSize, res, res, dlgImageSize->filterType());
0073     }
0074 
0075     delete dlgImageSize;
0076 }
0077 
0078 void ImageSize::slotCanvasSize()
0079 {
0080     KisImageWSP image = viewManager()->image();
0081     if (!image) return;
0082 
0083     if(!viewManager()->blockUntilOperationsFinished(image)) return;
0084 
0085     DlgCanvasSize * dlgCanvasSize = new DlgCanvasSize(viewManager()->mainWindowAsQWidget(), image->width(), image->height(), image->yRes());
0086     Q_CHECK_PTR(dlgCanvasSize);
0087 
0088     if (dlgCanvasSize->exec() == QDialog::Accepted) {
0089         qint32 width = dlgCanvasSize->width();
0090         qint32 height = dlgCanvasSize->height();
0091         qint32 xOffset = dlgCanvasSize->xOffset();
0092         qint32 yOffset = dlgCanvasSize->yOffset();
0093 
0094         viewManager()->imageManager()->resizeCurrentImage(width, height, xOffset, yOffset);
0095     }
0096     delete dlgCanvasSize;
0097 }
0098 
0099 void ImageSize::scaleLayerImpl(KisNodeSP rootNode)
0100 {
0101    scaleLayersImpl(KisNodeList{rootNode});
0102 }
0103 void ImageSize::scaleLayersImpl(KisNodeList nodes)
0104 {
0105     KisImageWSP image = viewManager()->image();
0106     if (!image) return;
0107 
0108     if(!viewManager()->blockUntilOperationsFinished(image)) return;
0109 
0110     QRect bounds;
0111     KisSelectionSP selection = viewManager()->selection();
0112 
0113     if (selection) {
0114         bounds = selection->selectedExactRect();
0115     }
0116     else {
0117         Q_FOREACH(KisNodeSP node, nodes) {
0118             KisPaintDeviceSP dev = node->projection();
0119             KIS_SAFE_ASSERT_RECOVER_RETURN(dev);
0120             bounds = bounds.united(dev->exactBounds());
0121         }
0122     }
0123 
0124     DlgLayerSize * dlgLayerSize = new DlgLayerSize(viewManager()->mainWindowAsQWidget(), "LayerSize", bounds.width(), bounds.height(), image->yRes());
0125     Q_CHECK_PTR(dlgLayerSize);
0126     dlgLayerSize->setCaption(i18np("Resize Layer", "Resize %1 Layers", nodes.size()));
0127 
0128     if (dlgLayerSize->exec() == QDialog::Accepted) {
0129         const QSize desiredSize(dlgLayerSize->desiredWidth(), dlgLayerSize->desiredHeight());
0130 
0131         viewManager()->image()->scaleNodes(nodes,
0132                                           QRectF(bounds).center(),
0133                                           qreal(desiredSize.width()) / bounds.width(),
0134                                           qreal(desiredSize.height()) / bounds.height(),
0135                                           dlgLayerSize->filterType(),
0136                                           selection);
0137     }
0138     delete dlgLayerSize;
0139 }
0140 
0141 void ImageSize::slotLayerSize()
0142 {
0143     scaleLayersImpl(viewManager()->nodeManager()->selectedNodes());
0144 }
0145 
0146 void ImageSize::slotScaleAllLayers()
0147 {
0148     KisImageWSP image = viewManager()->image();
0149     if (!image) return;
0150 
0151     scaleLayerImpl(image->root());
0152 }
0153 
0154 void ImageSize::slotSelectionScale()
0155 {
0156     KisImageSP image = viewManager()->image();
0157     if (!image) return;
0158 
0159     if(!viewManager()->blockUntilOperationsFinished(image)) return;
0160 
0161     KisLayerSP layer = viewManager()->activeLayer();
0162 
0163     KIS_ASSERT_RECOVER_RETURN(image && layer);
0164 
0165     KisSelectionMaskSP selectionMask = layer->selectionMask();
0166     if (!selectionMask) {
0167         selectionMask = image->rootLayer()->selectionMask();
0168     }
0169 
0170     KIS_ASSERT_RECOVER_RETURN(selectionMask);
0171 
0172     QRect rc = selectionMask->selection()->selectedExactRect();
0173     DlgLayerSize * dlgSize = new DlgLayerSize(viewManager()->mainWindowAsQWidget(), "SelectionScale", rc.width(), rc.height(), image->yRes());
0174     dlgSize->setCaption(i18n("Scale Selection"));
0175 
0176     if (dlgSize->exec() == QDialog::Accepted) {
0177         qint32 w = dlgSize->desiredWidth();
0178         qint32 h = dlgSize->desiredHeight();
0179 
0180         image->scaleNode(selectionMask,
0181                          QRectF(rc).center(),
0182                          qreal(w) / rc.width(),
0183                          qreal(h) / rc.height(),
0184                          dlgSize->filterType(), 0);
0185     }
0186     delete dlgSize;
0187 }