Krita Source Code Documentation
Loading...
Searching...
No Matches
KisNodeQueryPath Class Reference

#include <kis_node_query_path.h>

+ Inheritance diagram for KisNodeQueryPath:

Public Member Functions

bool isRelative () const
 
 KisNodeQueryPath (const KisNodeQueryPath &)
 
KisNodeQueryPathoperator= (const KisNodeQueryPath &)
 
void queryLevel (int _level, KisNodeSP _node, QList< KisNodeSP > &_result)
 
QList< KisNodeSPqueryNodes (KisImageWSP image, KisNodeSP currentNode) const
 
KisNodeSP queryUniqueNode (KisImageWSP image, KisNodeSP currentNode=0) const
 
void simplifyPath ()
 This function will remove unneeded call to parent, for instance, "1/../3/../5" => "5".
 
QString toString () const
 
 ~KisNodeQueryPath ()
 
- Public Member Functions inherited from Private
 Private (KisCanvas2 *c)
 

Static Public Member Functions

static KisNodeQueryPath absolutePath (KisNodeSP node)
 
static KisNodeQueryPath fromString (const QString &path)
 

Public Attributes

QList< PathElementelements
 
bool relative
 
- Public Attributes inherited from Private
KisCanvas2canvas
 
int displayedFrame
 
int intendedFrame
 

Private Member Functions

 KisNodeQueryPath ()
 

Private Attributes

Private *const d
 

Detailed Description

This class represent a path to access a node starting from another node.

Definition at line 28 of file kis_node_query_path.cc.

Constructor & Destructor Documentation

◆ KisNodeQueryPath() [1/2]

KisNodeQueryPath::KisNodeQueryPath ( )
private

Definition at line 92 of file kis_node_query_path.cc.

92 : d(new Private)
93{
94}

◆ ~KisNodeQueryPath()

KisNodeQueryPath::~KisNodeQueryPath ( )

Definition at line 96 of file kis_node_query_path.cc.

97{
98 delete d;
99}

References d.

◆ KisNodeQueryPath() [2/2]

KisNodeQueryPath::KisNodeQueryPath ( const KisNodeQueryPath & nqp)

Definition at line 101 of file kis_node_query_path.cc.

101 : d(new Private(*nqp.d))
102{
103}

Member Function Documentation

◆ absolutePath()

KisNodeQueryPath KisNodeQueryPath::absolutePath ( KisNodeSP node)
static

Definition at line 198 of file kis_node_query_path.cc.

199{
200 KisNodeQueryPath path;
201 path.d->relative = false;
202 KisNodeSP parent = 0;
203 while ((parent = node->parent())) {
204 int index = parent->index(node);
205 if (index >= 0) {
206 path.d->elements.push_front(index);
207 }
208 node = parent;
209 }
210 return path;
211}
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327
KisNodeWSP parent
Definition kis_node.cpp:86

References d, and KisNode::parent.

◆ fromString()

KisNodeQueryPath KisNodeQueryPath::fromString ( const QString & path)
static
Parameters
path
errif non null, it will be filled with an error message
See also
toString for an explanation of the string format

Definition at line 169 of file kis_node_query_path.cc.

170{
171 KisNodeQueryPath path;
172 if (_path.size() == 0 || _path == ".") {
173 path.d->relative = true;
174 return path;
175 }
176 if (_path == "/") {
177 path.d->relative = false;
178 return path;
179 }
180 path.d->relative = !(_path.at(0) == '/');
181 QStringList indexes = _path.split('/');
182 if (!path.d->relative) {
183 indexes.pop_front(); // In case of an absolute path "/1/2", the list is "", "1", "2" which is not good
184 }
185 Q_FOREACH (const QString& index, indexes) {
186 if (index == "*") {
187 path.d->elements.push_back(PathElement::Wildcard);
188 } else if (index == "..") {
189 path.d->elements.push_back(PathElement::Parent);
190 } else {
191 path.d->elements.push_back(index.toInt());
192 }
193 }
194 path.d->simplifyPath();
195 return path;
196}

References d, PathElement::Parent, and PathElement::Wildcard.

◆ isRelative()

bool KisNodeQueryPath::isRelative ( ) const

Definition at line 111 of file kis_node_query_path.cc.

112{
113 return d->relative;
114}

References d.

◆ operator=()

KisNodeQueryPath & KisNodeQueryPath::operator= ( const KisNodeQueryPath & nqp)

Definition at line 105 of file kis_node_query_path.cc.

106{
107 *d = *nqp.d;
108 return *this;
109}

References d.

◆ queryLevel()

void KisNodeQueryPath::queryLevel ( int _level,
KisNodeSP _node,
QList< KisNodeSP > & _result )
inline

Definition at line 57 of file kis_node_query_path.cc.

57 {
58 if (_level >= elements.size()) {
59 _result.push_back(_node);
60 } else {
61 PathElement pe = elements[_level];
62
63 switch (pe.type) {
65 for (KisNodeSP child = _node->firstChild();
66 child != 0; child = child->nextSibling()) {
67 queryLevel(_level + 1, child, _result);
68 }
69 }
70 break;
72 if (_node->parent()) {
73 queryLevel(_level + 1, _node->parent(), _result);
74 } else {
75 dbgKrita << "No parent";
76 }
77 break;
78 }
79 case PathElement::Index: {
80 if (pe.index < _node->childCount()) {
81 queryLevel(_level + 1, _node->at(pe.index), _result);
82 } else {
83 dbgKrita << "No parent";
84 }
85 break;
86 }
87 }
88 }
89 }
#define dbgKrita
Definition kis_debug.h:45
QList< PathElement > elements
void queryLevel(int _level, KisNodeSP _node, QList< KisNodeSP > &_result)
KisNodeSP firstChild() const
Definition kis_node.cpp:361
quint32 childCount() const
Definition kis_node.cpp:414
KisNodeSP at(quint32 index) const
Definition kis_node.cpp:421
unsigned int index

References KisNode::at(), KisNode::childCount(), dbgKrita, KisNode::firstChild(), PathElement::Index, PathElement::index, KisNode::parent, PathElement::Parent, PathElement::type, and PathElement::Wildcard.

◆ queryNodes()

QList< KisNodeSP > KisNodeQueryPath::queryNodes ( KisImageWSP image,
KisNodeSP currentNode ) const

Definition at line 117 of file kis_node_query_path.cc.

118{
119 KisNodeSP _node;
120 if (d->relative) {
121 _node = currentNode;
122 } else {
123 _node = image->root();
124 }
125
126 QList<KisNodeSP> result;
127
128 d->queryLevel(0, _node, result);
129
130 return result;
131}

References d, and KisNodeFacade::root.

◆ queryUniqueNode()

KisNodeSP KisNodeQueryPath::queryUniqueNode ( KisImageWSP image,
KisNodeSP currentNode = 0 ) const

Definition at line 133 of file kis_node_query_path.cc.

134{
135 QList<KisNodeSP> result = queryNodes(image, currentNode);
136 KIS_ASSERT_RECOVER_NOOP(result.size() <= 1);
137
138 return !result.isEmpty() ? result.first() : 0;
139}
#define KIS_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:97
QList< KisNodeSP > queryNodes(KisImageWSP image, KisNodeSP currentNode) const

References KIS_ASSERT_RECOVER_NOOP, and queryNodes().

◆ simplifyPath()

void KisNodeQueryPath::simplifyPath ( )
inline

This function will remove unneeded call to parent, for instance, "1/../3/../5" => "5".

Definition at line 32 of file kis_node_query_path.cc.

32 {
33 // No elements then return
34 if (elements.isEmpty()) return;
35 QList<PathElement> newelements;
36 int i = 0;
37 for (; i < elements.count() && elements[i].type == PathElement::Parent; ++i) {
38 newelements.push_back(PathElement::Parent);
39 }
40 // Loop over the element of the list
41 for (; i < elements.count(); ++i) {
42 PathElement pe = elements[i];
43 // If it's the last element, or the next element isn't a parent
44 if (pe.type != PathElement::Parent) {
45 newelements.push_back(pe);
46 } else {
47 if (newelements.isEmpty() || newelements.last().type == PathElement::Parent) {
48 newelements.push_back(PathElement::Parent);
49 } else {
50 newelements.removeLast();
51 }
52 }
53 }
54 // Set the new list
55 elements = newelements;
56 }

References PathElement::Parent, and PathElement::type.

◆ toString()

QString KisNodeQueryPath::toString ( ) const

This function return a string representing this path. Which is a list separated by '\' of:

  • '*': represents all layers
  • '..': represents the parent layer
  • number: index of the layer
  • '.': represents the current layer

For instance: "1/*" return all children of the first layer, "../3" return the third layer of the parent of the current layer If the string starts with "/" then it's an absolute path, otherwise it's a relative path.

Definition at line 141 of file kis_node_query_path.cc.

142{
143 QString str;
144 if (!d->relative) {
145 str = '/';
146 } else if (d->elements.count() == 0) {
147 return QString('.');
148 }
149 for (int i = 0; i < d->elements.count(); ++i) {
150 PathElement pe = d->elements[i];
151 switch (pe.type) {
153 str += '*';
154 break;
156 str += "..";
157 break;
159 str += QString::number(pe.index);
160 break;
161 }
162 if (i != d->elements.count() - 1) {
163 str += '/';
164 }
165 }
166 return str;
167}

References d, PathElement::Index, PathElement::index, PathElement::Parent, PathElement::type, and PathElement::Wildcard.

Member Data Documentation

◆ d

Private* const KisNodeQueryPath::d
private

Definition at line 46 of file kis_node_query_path.h.

◆ elements

QList<PathElement> KisNodeQueryPath::elements

Definition at line 29 of file kis_node_query_path.cc.

◆ relative

bool KisNodeQueryPath::relative

Definition at line 30 of file kis_node_query_path.cc.


The documentation for this class was generated from the following files: