File indexing completed on 2024-05-12 15:59:15

0001 /*
0002  *  SPDX-FileCopyrightText: 2007 Cyrille Berger <cberger@cberger.net>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "kis_meta_data_entry.h"
0008 #include <QString>
0009 
0010 #include <kis_debug.h>
0011 
0012 #include "kis_meta_data_value.h"
0013 #include "kis_meta_data_schema.h"
0014 
0015 using namespace KisMetaData;
0016 
0017 struct Q_DECL_HIDDEN Entry::Private {
0018     QString name;
0019     const Schema* schema;
0020     Value value;
0021     bool valid;
0022 };
0023 
0024 Entry::Entry() :
0025         d(new Private)
0026 {
0027     d->schema = 0;
0028     d->valid = false;
0029 }
0030 
0031 Entry::Entry(const Schema* schema, QString name, const Value& value) :
0032         d(new Private)
0033 {
0034     Q_ASSERT(!name.isEmpty());
0035     if (!isValidName(name)) {
0036         errKrita << "Invalid metadata name:" << name;
0037         d->name = QString("INVALID: %1").arg(name);
0038     }
0039     else {
0040         d->name = name;
0041     }
0042     d->schema = schema;
0043     d->value = value;
0044     d->valid = true;
0045 }
0046 
0047 Entry::Entry(const Entry& e) : d(new Private())
0048 {
0049     d->valid = false;
0050     *this = e;
0051 }
0052 
0053 Entry::~Entry()
0054 {
0055     delete d;
0056 }
0057 
0058 QString Entry::name() const
0059 {
0060     return d->name;
0061 }
0062 
0063 const Schema* Entry::schema() const
0064 {
0065     Q_ASSERT(d->schema);
0066     return d->schema;
0067 }
0068 
0069 void Entry::setSchema(const KisMetaData::Schema* schema)
0070 {
0071     Q_ASSERT(schema);
0072     d->schema = schema;
0073 }
0074 
0075 QString Entry::qualifiedName() const
0076 {
0077     Q_ASSERT(d->schema);
0078     return d->schema->generateQualifiedName(d->name);
0079 }
0080 
0081 const Value& Entry::value() const
0082 {
0083     return d->value;
0084 }
0085 
0086 Value& Entry::value()
0087 {
0088     return d->value;
0089 }
0090 
0091 bool Entry::isValid() const
0092 {
0093     return d->valid;
0094 }
0095 
0096 bool Entry::isValidName(const QString& _name)
0097 {
0098     if (_name.length() < 1) {
0099         dbgMetaData << "Too small";
0100         return false;
0101     }
0102     if (!_name[0].isLetter()) {
0103         dbgMetaData << _name << " doesn't start by a letter";
0104         return false;
0105     }
0106     for (int i = 1; i < _name.length(); ++i) {
0107         QChar c = _name[i];
0108         if (!c.isLetterOrNumber()) {
0109             dbgMetaData << _name << " " << i << "th character isn't a letter or a digit";
0110             return false;
0111         }
0112     }
0113     return true;
0114 }
0115 
0116 
0117 bool Entry::operator==(const Entry& e) const
0118 {
0119     return qualifiedName() == e.qualifiedName();
0120 }
0121 
0122 Entry& Entry::operator=(const Entry & e)
0123 {
0124     if (e.isValid()) {
0125         Q_ASSERT(!isValid() || *this == e);
0126         d->name = e.d->name;
0127         d->schema = e.d->schema;
0128         d->value = e.d->value;
0129         d->valid = true;
0130     }
0131     return *this;
0132 }
0133 
0134 QDebug operator<<(QDebug debug, const Entry &c)
0135 {
0136     debug.nospace() << "Name: " << c.name() << " Qualified name: " << c.qualifiedName() << " Value: " << c.value();
0137     return debug.space();
0138 }