-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelementslocation.cpp
More file actions
953 lines (878 loc) · 23 KB
/
Copy pathelementslocation.cpp
File metadata and controls
953 lines (878 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "elementslocation.h"
#include "../elementscollectioncache.h"
#include "../factory/elementpicturefactory.h"
#include "../qetapp.h"
#include "../qetgraphicsitem/element.h"
#include "../qetproject.h"
#include "../qetxml.h"
#include "xmlelementcollection.h"
#include <QPicture>
// make this class usable with QVariant
int ElementsLocation::MetaTypeId = qRegisterMetaType<ElementsLocation>("ElementsLocation");
/**
@brief ElementsLocation::ElementsLocation
Constructor
*/
ElementsLocation::ElementsLocation()
{}
/**
@brief ElementsLocation::ElementsLocation
\~ @param path : Item location path
\~French Chemin de l'emplacement de l'element
\~ @param project : Project of the location of the element
\~French Projet de l'emplacement de l'element
*/
ElementsLocation::ElementsLocation(const QString &path, QETProject *project) :
m_project(project)
{
setPath(path);
}
/**
@brief ElementsLocation::~ElementsLocation
Destructeur
*/
ElementsLocation::~ElementsLocation()
{
}
/**
@brief ElementsLocation::ElementsLocation
The copy constructor
\~French Constructeur de copie
\~ @param other : Alternate item location to copy
\~French Autre emplacement d'element a copier
*/
ElementsLocation::ElementsLocation(const ElementsLocation &other) :
m_collection_path(other.m_collection_path),
m_file_system_path(other.m_file_system_path),
m_project(other.m_project)
{}
/**
@brief ElementsLocation::ElementLocation
Constructor, build an ElementLocation from a QMimeData,
the mime data format must be "application/x-qet-element-uri"
or "application/x-qet-category-uri".
This location can be null even if format is valid.
@param data
*/
ElementsLocation::ElementsLocation(const QMimeData *data)
{
if (data->hasFormat("application/x-qet-element-uri")
|| data->hasFormat("application/x-qet-category-uri"))
setPath(data->text());
}
/**
@brief ElementsLocation::operator =
Assignment operator
\~French Operateur d'affectation
\~ @param other :
Other item location to assign
\~French Autre emplacement d'element a affecter
\~ @return *this ElementsLocation
*/
ElementsLocation &ElementsLocation::operator=(const ElementsLocation &other) {
m_collection_path = other.m_collection_path;
m_file_system_path = other.m_file_system_path;
m_project = other.m_project;
return(*this);
}
/**
@brief ElementsLocation::operator ==
\~French Operateur de comparaison
\~ @param other : other item location to compare
\~French Autre emplacement d'element a comparer
\~ @return true if other and this ElementsLocation are identical,
false otherwise
\~French true si other et cet ElementsLocation sont identiques,
false sinon
*/
bool ElementsLocation::operator==(const ElementsLocation &other) const
{
return(
m_collection_path == other.m_collection_path &&\
m_project == other.m_project
);
}
/**
@brief ElementsLocation::operator !=
Operateur de comparaison
@param other Autre emplacement d'element a comparer
@return true si other et cet ElementsLocation sont differents,
false sinon
*/
bool ElementsLocation::operator!=(const ElementsLocation &other) const
{
return(
m_collection_path != other.m_collection_path ||\
m_project != other.m_project
);
}
/**
@brief ElementsLocation::baseName
@return The base name of the element or directory.
Unlike ElementsLocation::fileName,
this method don't return the extension name.
For example if this location represent an element they return myElement.
@see fileName()
*/
QString ElementsLocation::baseName() const
{
QRegularExpression regexp("^.*(?<name>[^/]+)\\.elmt$");
if (!regexp.isValid())
{
qWarning() <<"this is an error in the code"
<< regexp.errorString()
<< regexp.patternErrorOffset();
return QString();
}
QRegularExpressionMatch match = regexp.match(m_collection_path);
if (!match.hasMatch())
{
qDebug()<<"no Match => return"
<<m_collection_path;
return QString();
}
return match.captured("name");
}
/**
@brief ElementsLocation::collectionPath
Return the path of the represented element relative to collection
if protocol is true the path is prepended by
the collection type (common://, company://, custom:// or embed://)
else if false,
only the collection path is returned without the collection type.
@param protocol
@return the path
*/
QString ElementsLocation::collectionPath(bool protocol) const
{
if (protocol)
return m_collection_path;
else
{
QString path = m_collection_path;
return path.remove(QRegularExpression("common://|company://|custom://|macros://|embed://"));
}
}
/**
@brief ElementsLocation::projectCollectionPath
@return The path is in form : project0+embed://dir/subdir/myElement.elmt
If this item represent a file system thing, return a null QString;
*/
QString ElementsLocation::projectCollectionPath() const
{
if (isFileSystem())
return QString();
else
return QString("project"
% QString::number(QETApp::projectId(m_project))
% "+"
% collectionPath());
}
/**
@brief ElementsLocation::fileSystemPath
@return The file system path of this element,
(the separator is always '/' see QDir::toNativeSeparators())
If this element is embedded in a project return an empty string;
*/
QString ElementsLocation::fileSystemPath() const
{
if (!m_project)
return m_file_system_path;
else
return QString();
}
/**
@brief ElementsLocation::path
@return The path of this location.
@deprecated use instead collectionPath(true)
*/
QString ElementsLocation::path() const
{
return(m_collection_path);
}
/**
@brief ElementsLocation::setPath
Set the path of this item.
The path can be relative to a collection
(start by common://, company://, custom:// or embed://) or not.
@param path
*/
void ElementsLocation::setPath(const QString &path)
{
QString tmp_path = path;
#ifdef Q_OS_WIN32
tmp_path = QDir::fromNativeSeparators(path);
#endif
QString macrosPath = QETApp::userMacrosDir();
if (macrosPath.endsWith("/")) macrosPath.remove(macrosPath.length() - 1, 1);
if (m_project)
{
m_collection_path = path;
if (!path.startsWith("embed://"))
m_collection_path.prepend("embed://");
}
else if (tmp_path.startsWith("project"))
{
QRegularExpression re ("^project(?<project_id>[0-9])\\+(?<collection_path>embed://*.*)$");
if (!re.isValid()) return;
QRegularExpressionMatch match = re.match(tmp_path);
if (!match.hasMatch()) return;
bool conv_ok;
uint project_id = match.captured("project_id").toUInt(&conv_ok);
if (!conv_ok) return;
QETProject *project = QETApp::project(project_id);
if (project)
{
m_collection_path = match.captured("collection_path");
m_project = project;
}
}
else if (path.startsWith("common://") || path.startsWith("company://") || path.startsWith("custom://") || path.startsWith("macros://"))
{
QString p;
if (path.startsWith("common://"))
{
tmp_path.remove("common://");
p = QETApp::commonElementsDirN() % "/" % tmp_path;
}
else if (path.startsWith("company://"))
{
tmp_path.remove("company://");
p = QETApp::companyElementsDirN() % "/" % tmp_path;
}
else if (path.startsWith("macros://"))
{
tmp_path.remove("macros://");
p = macrosPath % "/" % tmp_path;
}
else
{
tmp_path.remove("custom://");
p = QETApp::customElementsDirN() % "/" % tmp_path;
}
m_file_system_path = p;
m_collection_path = path;
}
else
{
QString path_ = path;
if(path_.endsWith(".elmt") || path_.endsWith(".qetmak"))
{
m_file_system_path = path_;
if (path_.startsWith(QETApp::commonElementsDirN()))
{
path_.remove(QETApp::commonElementsDirN()+="/");
path_.prepend("common://");
m_collection_path = path_;
}
else if (path_.startsWith(QETApp::companyElementsDirN()))
{
path_.remove(QETApp::companyElementsDirN()+="/");
path_.prepend("company://");
m_collection_path = path_;
}
else if (path_.startsWith(macrosPath))
{
QString matchPath = macrosPath + "/";
path_.remove(matchPath);
path_.prepend("macros://");
m_collection_path = path_;
}
else if (path_.startsWith(QETApp::customElementsDirN()))
{
path_.remove(QETApp::customElementsDirN()+="/");
path_.prepend("custom://");
m_collection_path = path_;
}
}
else
{
m_file_system_path = path_;
if (path_.startsWith(QETApp::commonElementsDirN()))
{
path_.remove(QETApp::commonElementsDirN()+="/");
path_.prepend("common://");
m_collection_path = path_;
}
else if (path_.startsWith(QETApp::companyElementsDirN()))
{
path_.remove(QETApp::companyElementsDirN()+="/");
path_.prepend("company://");
m_collection_path = path_;
}
else if (path_.startsWith(macrosPath))
{
QString matchPath = macrosPath + "/";
path_.remove(matchPath);
path_.prepend("macros://");
m_collection_path = path_;
}
else if (path_.startsWith(QETApp::customElementsDirN()))
{
path_.remove(QETApp::customElementsDirN()+="/");
path_.prepend("custom://");
m_collection_path = path_;
}
}
}
}
/**
@brief ElementsLocation::addToPath
Add a string to the actual path of this location
@param string
@return True if the operation success
*/
bool ElementsLocation::addToPath(const QString &string)
{
if (m_collection_path.endsWith(".elmt", Qt::CaseInsensitive) ||
m_collection_path.endsWith(".qetmak", Qt::CaseInsensitive))
{
qDebug() << "ElementsLocation::addToPath :"
" Can't add string to the path of an element or template";
return(false);
}
QString added_path = string;
if (!m_collection_path.endsWith("/") && !added_path.startsWith("/"))
added_path.prepend("/");
if (isFileSystem())
m_file_system_path += added_path;
m_collection_path += added_path;
return(true);
}
/**
@brief ElementsLocation::parent
@return the location of the parent category, or a copy of this location
when it represents a root category.
*/
ElementsLocation ElementsLocation::parent() const
{
ElementsLocation copy(*this);
QRegularExpression re ("^(?<path_proto>[a-z]+://.*)/.*$");
if (!re.isValid())
{
qWarning()
<<QObject::tr("this is an error in the code")
<< re.errorString()
<< re.patternErrorOffset();
}
QRegularExpressionMatch match = re.match(m_collection_path);
if (!match.hasMatch())
{
qDebug()
<<"no Match => return"
<<m_collection_path;
}else {
copy.setPath(match.captured("path_proto"));
}
return(copy);
}
/**
@brief ElementsLocation::project
@return
the project of this location or 0 if it is not linked to a project.
\~French
le projet de cet emplacement ou 0 si celui-ci n'est pas lie a un projet.
*/
QETProject *ElementsLocation::project() const
{
return(m_project);
}
/**
@brief ElementsLocation::setProject
@param project :
the new project points to this location
Indicate 0 so that this location is no longer linked to a project.
\~French le nouveau projet pointe par cet emplacement
Indiquer 0 pour que cet emplacement ne soit plus lie a un projet.
*/
void ElementsLocation::setProject(QETProject *project) {
m_project = project;
}
/**
@brief ElementsLocation::isNull
@return true if the location seems usable (virtual path not empty).
\~French
true si l'emplacement semble utilisable (chemin virtuel non vide).
*/
bool ElementsLocation::isNull() const
{
return(m_collection_path.isEmpty());
}
/**
@brief ElementsLocation::toString
@return A character string representing the location
\~French Une chaine de caracteres representant l'emplacement
*/
QString ElementsLocation::toString() const
{
QString result;
if (m_project) {
int project_id = QETApp::projectId(m_project);
if (project_id != -1) {
result += "project" % QString().setNum(project_id) % "+";
}
}
result += m_collection_path;
return(result);
}
/**
@brief ElementsLocation::isElement
@return true if this location represent an element
*/
bool ElementsLocation::isElement() const
{
return m_collection_path.endsWith(".elmt") || m_collection_path.endsWith(".qetmak");
}
/**
@brief ElementsLocation::isDirectory
@return true if this location represent a directory
*/
bool ElementsLocation::isDirectory() const
{
return (!isElement() && !m_collection_path.isEmpty());
}
/**
@brief ElementsLocation::isFileSystem
@return true if
*/
bool ElementsLocation::isFileSystem() const
{
if (m_project) return false;
if (m_file_system_path.isEmpty()) return false;
return true;
}
/**
@brief ElementsLocation::isCommonCollection
@return
True if this location represent an item from the common collection
*/
bool ElementsLocation::isCommonCollection() const
{
return fileSystemPath().startsWith(QETApp::commonElementsDirN());
}
/**
@brief ElementsLocation::isCompanyCollection
@return
True if this location represent an item from the company collection
*/
bool ElementsLocation::isCompanyCollection() const
{
return fileSystemPath().startsWith(QETApp::companyElementsDirN());
}
/**
@brief ElementsLocation::isCustomCollection
@return
True if this location represent an item from the custom collection
*/
bool ElementsLocation::isCustomCollection() const
{
const QString dir = QETApp::customElementsDirN();
const QString path = fileSystemPath();
return path == dir || path.startsWith(dir + QLatin1Char('/'));
}
/**
@brief ElementsLocation::isProject
@return True if this location represent an item from a project.
*/
bool ElementsLocation::isProject() const
{
if (m_project && !m_collection_path.isEmpty())
return true;
else
return false;
}
/**
@brief ElementsLocation::exist
@return
True if this location represent an existing directory or element.
*/
bool ElementsLocation::exist() const
{
if (m_project)
{
return m_project->embeddedElementCollection()
->exist(collectionPath(false));
}
else
{
if (fileSystemPath().isEmpty()) return false;
if (isDirectory())
{
QDir dir(fileSystemPath());
return dir.exists();
}
else if (isElement())
return QFile::exists(fileSystemPath());
else
return false;
}
}
/**
@brief ElementsLocation::isWritable
@return True if this element can be writable (can use set xml)
*/
bool ElementsLocation::isWritable() const
{
if (m_project)
return !m_project->isReadOnly();
else if (isFileSystem())
{
if (fileSystemPath().startsWith(QETApp::commonElementsDirN()))
return false;
else
return true;
}
return false;
}
/**
@brief ElementsLocation::projectCollection
@return
If this location represents an item in an embedded project collection,
return this collection else return nullptr.
*/
XmlElementCollection *ElementsLocation::projectCollection() const
{
if (m_project)
return m_project->embeddedElementCollection();
else
return nullptr;
}
/**
@brief ElementsLocation::nameList
@return the namelist of the represented element or directory.
If namelist can't be set, return a empty namelist
*/
NamesList ElementsLocation::nameList()
{
NamesList nl;
if (isElement()) {
nl.fromXml(pugiXml());
}
if (isDirectory())
{
if (m_project)
nl.fromXml(m_project->embeddedElementCollection()
->directory(collectionPath(false)));
else
{
//Open the qet_directory file,
// to get the translated name of this dir
QFile dir_conf(fileSystemPath() % "/qet_directory");
if (dir_conf.exists() && dir_conf.open(
QIODevice::ReadOnly
| QIODevice::Text))
{
//Get the content of the file
QDomDocument document;
if (document.setContent(&dir_conf))
{
QDomElement root = document
.documentElement();
if (root.tagName() == "qet-directory")
nl.fromXml(root);
}
}
}
}
return nl;
}
/**
@brief ElementsLocation::xml
@return The definition of this element or directory.
The definition can be null.
*/
QDomElement ElementsLocation::xml() const
{
if (!m_project)
{
QFile file (m_file_system_path);
QDomDocument docu;
if (docu.setContent(&file))
return docu.documentElement();
}
else
{
QString str = m_collection_path;
if (isElement())
{
QDomElement element = m_project
->embeddedElementCollection()
->element(str.remove("embed://"));
return element.firstChildElement("definition");
}
else
{
QDomElement element = m_project
->embeddedElementCollection()
->directory(str.remove("embed://"));
return element;
}
}
return QDomElement();
}
/**
@brief ElementsLocation::pugiXml
@return the xml document of this element or directory
The definition can be null
*/
pugi::xml_document ElementsLocation::pugiXml() const
{
pugi::xml_document docu; //empty xml_document();
/* Except for linux OS (because linux keep in cache the file),
* we keep in memory the xml
* to avoid multiple access to file.
* keep in memory the XML,
* consumes a little more RAM,
* for this reason we don't use it for linux to minimize the RAM footprint.
*/
#ifndef Q_OS_LINUX
if (!m_string_stream.str().empty())
{
docu.load_string(m_string_stream.str().c_str());
return docu;
}
#endif
if (!m_project)
{
#ifndef Q_OS_LINUX
if (docu.load_file(m_file_system_path.toStdString().c_str())) {
docu.save(m_string_stream);
}
#else
docu.load_file(m_file_system_path.toStdString().c_str());
#endif
}
else
{
//Get the xml dom from Qt xml and copie to pugi xml
QDomDocument qdoc;
QString str = m_collection_path;
if (isElement()) {
QDomElement element = m_project->embeddedElementCollection()->element(str.remove("embed://"));
qdoc.appendChild(qdoc.importNode(element.firstChildElement("definition"),true));
} else {
QDomElement element = m_project->embeddedElementCollection()->directory(str.remove("embed://"));
qdoc.appendChild(qdoc.importNode(element, true));
}
docu.load_string(qdoc.toString(4).toStdString().c_str());
}
return docu;
}
/**
@brief ElementsLocation::setXml
Replace the current xml description by xml_document;
The document element of xml_document must have
tagname "definition" to be written
This definition must be writable
@param xml_document
@return true if success
*/
bool ElementsLocation::setXml(const QDomDocument &xml_document) const
{
if (!isWritable())
return false;
if (xml_document.documentElement().tagName() != "definition")
{
qDebug() << "ElementsLocation::setXml :"
" tag name of document element isn't 'definition'";
return false;
}
if (isFileSystem())
{
QString error;
QETXML::writeXmlFile(xml_document, fileSystemPath(), &error);
if (!error.isEmpty()) {
qDebug() << "ElementsLocation::setXml error : "
<< error;
return false;
}
else {
return true;
}
}
else if (isProject())
{
//Element exist, we overwrite the existing element.
if (exist())
{
QDomElement dom_element = xml();
QDomNode parent_node = dom_element.parentNode();
parent_node.removeChild(dom_element);
parent_node.appendChild(xml_document
.documentElement()
.cloneNode(true));
return true;
}
//Element doesn't exist, we create the element
else
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // ### Qt 6: remove
QString path_ = collectionPath(false);
QRegExp rx ("^(.*)/(.*\\.elmt)$");
if (rx.exactMatch(path_)) {
return project()
->embeddedElementCollection()
->addElementDefinition(
rx.cap(1),
rx.cap(2),
xml_document
.documentElement());
}
else {
qDebug() << "ElementsLocation::setXml :"
" rx don't match";
}
#else
#if TODO_LIST
#pragma message("@TODO remove code for QT 6 or later")
# pragma message("@TODO ad Core5Compat to Cmake")
#endif
qDebug() << "Help code for QT 6 or later";
QString path_ = collectionPath(false);
QRegularExpression rx("^(.*)/(.*\\.elmt)$");
if (rx.exactMatch(path_))
{
return project()
->embeddedElementCollection()
->addElementDefinition(
rx.cap(1),
rx.cap(2),
xml_document.documentElement());
}
else
{
qDebug() << "ElementsLocation::setXml :"
" rx don't match";
}
#endif
}
}
return false;
}
/**
@brief ElementsLocation::uuid
@return The uuid of the pointed element
Uuid can be null
*/
QUuid ElementsLocation::uuid() const
{
if (!isElement()) {
return QUuid();
}
auto document = pugiXml();
auto uuid_node = document.document_element().child("uuid");
if (uuid_node.empty()) {
return QUuid();
}
return QUuid(uuid_node.attribute("uuid").as_string());
}
/**
@brief ElementLocation::icon
@return The icon of the represented element.
If icon can't be set, return a null QIcon
*/
QIcon ElementsLocation::icon() const
{
if (!m_project)
{
ElementsCollectionCache *cache = QETApp::collectionCache();
// Make a copy of this to keep this method const
ElementsLocation loc(*this);
if (cache->fetchElement(loc))
return QIcon(cache->pixmap());
} else {
return QIcon(ElementPictureFactory::instance()->pixmap(*this));
}
return QIcon();
}
/**
@brief ElementLocation::name
@return The name of the represented element in the current locale
*/
QString ElementsLocation::name() const
{
NamesList nl;
nl.fromXml(pugiXml().document_element());
return nl.name(fileName());
}
/**
@brief ElementLocation::fileName
@return Return the file name of the represented item,
whatever the storage system (file system, xml collection)
with is file extension.
For example if this location represent an element,
they return myElement.elmt.
For a directory return myDirectory.
@see baseName
*/
QString ElementsLocation::fileName() const
{
if (m_collection_path.isEmpty())
return QString();
QStringList qsl = m_collection_path.split("/");
if (qsl.isEmpty())
return QString();
else
return qsl.last();
}
/**
@brief ElementsLocation::elementInformations
@return
the element information of the element represented by this location.
If the location is a directory, the returned diagram context is empty
*/
DiagramContext ElementsLocation::elementInformations() const
{
DiagramContext context;
if (isDirectory()) {
return context;
}
context.fromXml(pugiXml().document_element().child(
"elementInformations"),
"elementInformation");
return context;
}
/**
@brief operator <<
debug for processing ElementsLocation
@param debug
@param location
@return debug msg
*/
QDebug operator<< (QDebug debug, const ElementsLocation &location)
{
QDebugStateSaver saver(debug);
debug.noquote();
QString msg;
msg += "ElementsLocation(";
msg += (location.isProject()? location.projectCollectionPath()
: location.collectionPath(true));
msg += location.exist()? ", true" : ", false";
msg +=")";
debug << msg;
return debug;
}
/**
* @brief ElementsLocation::isMacrosCollection
* @return True if this location represent an item from the macros collection
*/
bool ElementsLocation::isMacrosCollection() const
{
QString macrosPath = QETApp::userMacrosDir();
if (macrosPath.endsWith("/")) macrosPath.remove(macrosPath.length() - 1, 1);
return fileSystemPath().startsWith(macrosPath);
}