File indexing completed on 2024-05-12 16:01:31

0001 /*
0002  *  SPDX-FileCopyrightText: 2013 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KIS_FILE_LAYER_H
0007 #define KIS_FILE_LAYER_H
0008 
0009 #include "kritaui_export.h"
0010 
0011 #include "kis_external_layer_iface.h"
0012 #include "kis_safe_document_loader.h"
0013 
0014 /**
0015  * @brief The KisFileLayer class loads a particular file as a layer into the layer stack.
0016  */
0017 class KRITAUI_EXPORT KisFileLayer : public KisExternalLayer
0018 {
0019     Q_OBJECT
0020 public:
0021 
0022     enum ScalingMethod {
0023         None,
0024         ToImageSize,
0025         ToImagePPI
0026     };
0027 
0028     KisFileLayer(KisImageWSP image, const QString &name, quint8 opacity);
0029     /**
0030      * @brief KisFileLayer create a new file layer with the given file
0031      * @param image the image the file layer will belong to
0032      * @param basePath the path to the image, if it has been saved before.
0033      * @param filename the path to the file, relative to the basePath
0034      * @param scalingMethod @see ScalingMethod
0035      * @param name the name of the layer
0036      * @param opacity the opacity of the layer
0037      */
0038     KisFileLayer(KisImageWSP image, const QString& basePath, const QString &filename, ScalingMethod scalingMethod, const QString &name, quint8 opacity, const KoColorSpace *fallbackColorSpace = 0);
0039     ~KisFileLayer() override;
0040     KisFileLayer(const KisFileLayer& rhs);
0041 
0042     QIcon icon() const override;
0043 
0044     void resetCache() override;
0045 
0046     KisPaintDeviceSP original() const override;
0047     KisPaintDeviceSP paintDevice() const override;
0048     void setSectionModelProperties(const KisBaseNode::PropertyList &properties) override;
0049     KisBaseNode::PropertyList sectionModelProperties() const override;
0050 
0051     /**
0052      * @brief setFileName replace the existing file with a new one
0053      * @param basePath the path to the image, if it has been saved before.
0054      * @param filename the path to the file, relative to the basePath
0055      */
0056     void setFileName(const QString &basePath, const QString &filename);
0057     QString fileName() const;
0058     QString path() const;
0059 
0060 
0061     ScalingMethod scalingMethod() const;
0062     void setScalingMethod(ScalingMethod method);
0063 
0064     KisNodeSP clone() const override;
0065     bool allowAsChild(KisNodeSP) const override;
0066 
0067     bool accept(KisNodeVisitor&) override;
0068     void accept(KisProcessingVisitor &visitor, KisUndoAdapter *undoAdapter) override;
0069 
0070     KUndo2Command* crop(const QRect & rect) override;
0071     KUndo2Command* transform(const QTransform &transform) override;
0072 
0073     void setImage(KisImageWSP image) override;
0074 
0075 private Q_SLOTS:
0076     void slotLoadingFinished(KisPaintDeviceSP projection, qreal xRes, qreal yRes, const QSize &size);
0077     void slotLoadingFailed();
0078     void slotFileExistsStateChanged(bool exists);
0079     void openFile() const;
0080 
0081 Q_SIGNALS:
0082     void sigRequestOpenFile();
0083 
0084 private:
0085     enum State {
0086         FileLoaded,
0087         FileNotFound,
0088         FileLoadingFailed
0089     };
0090 
0091     void changeState(State newState);
0092 
0093 private:
0094     QString m_basePath;
0095     QString m_filename;
0096     ScalingMethod m_scalingMethod {None};
0097 
0098     KisPaintDeviceSP m_paintDevice;
0099     KisSafeDocumentLoader m_loader;
0100     QSize m_generatedForImageSize;
0101     qreal m_generatedForXRes = 0.0;
0102     qreal m_generatedForYRes = 0.0;
0103 
0104     State m_state = FileNotFound;
0105 };
0106 
0107 #endif // KIS_FILE_LAYER_H