Krita

FileLayer.cpp
1 /*
2  * SPDX-FileCopyrightText: 2017 Wolthera van Hövell tot Westerflier <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 #include "FileLayer.h"
7 #include <kis_file_layer.h>
8 #include <kis_image.h>
9 #include <QFileInfo>
10 #include <QDir>
11 
12 FileLayer::FileLayer(KisImageSP image, const QString name, const QString baseName, const QString fileName, const QString scalingMethod, QObject *parent)
13  : Node(image, new KisFileLayer(image, name, OPACITY_OPAQUE_U8), parent)
14 {
15  KisFileLayer *file = dynamic_cast<KisFileLayer*>(this->node().data());
16  KIS_ASSERT(file);
17  KisFileLayer::ScalingMethod sm = KisFileLayer::None;
18  if (scalingMethod=="ToImageSize") {
19  sm= KisFileLayer::ToImageSize;
20  } else if (scalingMethod=="ToImagePPI") {
21  sm= KisFileLayer::ToImagePPI;
22  }
23  file->setScalingMethod(sm);
24 
25  const QString &basePath = QFileInfo(baseName).absolutePath();
26  const QString &absoluteFilePath = QFileInfo(fileName).absoluteFilePath();
27  file->setFileName(basePath, getFileNameFromAbsolute(basePath, absoluteFilePath));
28 }
29 
30 FileLayer::FileLayer(KisFileLayerSP layer, QObject *parent)
31  : Node(layer->image(), layer, parent)
32 {
33 
34 }
35 
36 FileLayer::~FileLayer()
37 {
38 
39 }
40 
42 {
43  return "filelayer";
44 }
45 
46 void FileLayer::setProperties(QString fileName, QString scalingMethod)
47 {
48  KisFileLayer *file = dynamic_cast<KisFileLayer*>(this->node().data());
49  KIS_ASSERT(file);
50  KisFileLayer::ScalingMethod sm = KisFileLayer::None;
51  if (scalingMethod.toLower() == "toimagesize") {
52  sm= KisFileLayer::ToImageSize;
53  } else if (scalingMethod.toLower() == "toimageppi") {
54  sm= KisFileLayer::ToImagePPI;
55  }
56  file->setScalingMethod(sm);
57 
58  const QString basePath = QFileInfo(file->path()).absolutePath();
59  const QString absoluteFilePath = QFileInfo(fileName).absoluteFilePath();
60  file->setFileName(basePath, getFileNameFromAbsolute(basePath, absoluteFilePath));
61 }
62 
64 {
65  KisFileLayer *file = dynamic_cast<KisFileLayer*>(this->node().data());
66  KIS_ASSERT_RECOVER_RETURN(file);
67  file->resetCache();
68 }
69 
71 {
72  const KisFileLayer *file = qobject_cast<const KisFileLayer*>(this->node());
73  KIS_ASSERT(file);
74  return file->path();
75 }
76 
78 {
79  const KisFileLayer *file = qobject_cast<const KisFileLayer*>(this->node());
80  KIS_ASSERT_RECOVER_RETURN_VALUE(file, "None");
81  KisFileLayer::ScalingMethod sm = file->scalingMethod();
82  QString method = "None";
83 
84  if (sm==KisFileLayer::ToImageSize) {
85  method = "ToImageSize";
86  } else if (sm==KisFileLayer::ToImagePPI) {
87  method = "ToImagePPI";
88  }
89  return method;
90 }
91 
92 QString FileLayer::getFileNameFromAbsolute(const QString &basePath, QString filePath)
93 {
94  KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(QFileInfo(filePath).isAbsolute(), filePath);
95 
96  // try to resolve symlink
97  {
98  const QFileInfo fi(filePath);
99  if (fi.isSymLink()) {
100  filePath = fi.symLinkTarget();
101  }
102  }
103 
104  if (!basePath.isEmpty()) {
105  QDir directory(basePath);
106  filePath = directory.relativeFilePath(filePath);
107  }
108 
109  return filePath;
110 }
111 
112 
QString scalingMethod() const
scalingMethod returns how the file referenced is scaled.
Definition: FileLayer.cpp:77
QString path() const
path
Definition: FileLayer.cpp:70
QString type() const override
type Krita has several types of nodes, split in layers and masks.
Definition: FileLayer.cpp:41
void setProperties(QString fileName, QString scalingMethod=QString("None"))
setProperties Change the properties of the file layer.
Definition: FileLayer.cpp:46
QString absoluteFilePath() const const
bool isEmpty() const const
QString absolutePath() const const
QString toLower() const const
Node represents a layer or mask in a Krita image's Node hierarchy.
Definition: Node.h:21
QString name(StandardShortcut id)
void resetCache()
makes the file layer to reload the connected image from disk
Definition: FileLayer.cpp:63
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Oct 6 2022 04:17:36 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.