File indexing completed on 2024-05-19 04:32:39

0001 /*
0002  *  SPDX-FileCopyrightText: 2015 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __LOD_OVERRIDE_H
0008 #define __LOD_OVERRIDE_H
0009 
0010 #include "kis_default_bounds_base.h"
0011 
0012 
0013 namespace TestUtil {
0014 
0015 class LodOverride
0016 {
0017 private:
0018 
0019     class LodDefaultBounds : public KisDefaultBoundsBase
0020     {
0021     public:
0022         LodDefaultBounds(int lod, KisDefaultBoundsBaseSP parent)
0023             : m_lod(lod), m_parent(parent)
0024         {
0025         }
0026 
0027         QRect bounds() const override {
0028             return m_parent->bounds();
0029         }
0030 
0031         virtual QRect imageBorderRect() const override {
0032             return m_parent->imageBorderRect();
0033         }
0034 
0035         bool wrapAroundMode() const override {
0036             return m_parent->wrapAroundMode();
0037         }
0038 
0039         WrapAroundAxis wrapAroundModeAxis() const override {
0040             return m_parent->wrapAroundModeAxis();
0041         }
0042 
0043         int currentLevelOfDetail() const override {
0044             return m_lod;
0045         }
0046 
0047         int currentTime() const override {
0048             return m_parent->currentTime();
0049         }
0050 
0051         bool externalFrameActive() const override {
0052             return m_parent->externalFrameActive();
0053         }
0054 
0055         KisDefaultBoundsBaseSP parent() const {
0056             return m_parent;
0057         }
0058 
0059         void * sourceCookie() const override {
0060             return 0;
0061         }
0062 
0063     private:
0064         int m_lod;
0065         KisDefaultBoundsBaseSP m_parent;
0066     };
0067 
0068 public:
0069     explicit LodOverride(int lod, KisImageSP image)
0070         : m_lod(lod), m_image(image)
0071     {
0072         overrideBounds(m_image->root(), OverrideDevice(m_lod));
0073     }
0074 
0075     ~LodOverride()
0076     {
0077         overrideBounds(m_image->root(), RestoreDevice());
0078     }
0079 
0080 private:
0081 
0082     template <class OverrideOp>
0083     void overrideBounds(KisNodeSP root, OverrideOp op) {
0084         op(root->paintDevice());
0085 
0086         if (root->original() != root->paintDevice()) {
0087             op(root->original());
0088         }
0089 
0090         if (root->projection() != root->original()) {
0091             op(root->projection());
0092         }
0093 
0094         KisNodeSP node = root->firstChild();
0095         while (node) {
0096             overrideBounds(node, op);
0097             node = node->nextSibling();
0098         }
0099     }
0100 
0101     struct OverrideDevice {
0102         OverrideDevice(int lod) : m_lod(lod) {}
0103 
0104         void operator() (KisPaintDeviceSP device) {
0105             if (!device) return;
0106 
0107             LodDefaultBounds *bounds = dynamic_cast<LodDefaultBounds*>(device->defaultBounds().data());
0108             if (bounds) return;
0109 
0110             device->setDefaultBounds(new LodDefaultBounds(m_lod, device->defaultBounds()));
0111         }
0112 
0113         int m_lod;
0114     };
0115 
0116     struct RestoreDevice {
0117         void operator() (KisPaintDeviceSP device) {
0118             if (!device) return;
0119 
0120             LodDefaultBounds *bounds = dynamic_cast<LodDefaultBounds*>(device->defaultBounds().data());
0121             if (!bounds) return;
0122 
0123             device->setDefaultBounds(bounds->parent());
0124         }
0125     };
0126 
0127 private:
0128     int m_lod;
0129     KisImageSP m_image;
0130 };
0131 
0132 }
0133 
0134 #endif /* __LOD_OVERRIDE_H */