File indexing completed on 2025-01-19 03:53:04

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2021-03-20
0007  * Description : a tool to export images to iNaturalist web service
0008  *
0009  * SPDX-FileCopyrightText: 2021      by Joerg Lohse <joergmlpts at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "inattaxon.h"
0016 
0017 // Local includes
0018 
0019 #include "inatutils.h"
0020 
0021 namespace DigikamGenericINatPlugin
0022 {
0023 
0024 class Q_DECL_HIDDEN Taxon::Private
0025 {
0026 public:
0027 
0028     Private()
0029         : id       (-1),
0030           parentId (-1),
0031           rankLevel(-1.0)
0032     {
0033     }
0034 
0035     int          id;
0036     int          parentId;
0037     QString      name;
0038     QString      rank;
0039     double       rankLevel;
0040     QString      commonName;
0041     QString      matchedTerm;
0042     QUrl         squareUrl;
0043     QList<Taxon> ancestors;
0044 };
0045 
0046 Taxon::Taxon(int id,
0047              int parentId,
0048              const QString& name,
0049              const QString& rank,
0050              double rankLevel,
0051              const QString& commonName,
0052              const QString& matchedTerm,
0053              const QUrl& squareUrl,
0054              const QList<Taxon>& ancestors)
0055     : d(new Private)
0056 {
0057     d->id          = id;
0058     d->parentId    = parentId;
0059     d->name        = name;
0060     d->rank        = rank;
0061     d->rankLevel   = rankLevel;
0062     d->commonName  = commonName;
0063     d->matchedTerm = matchedTerm;
0064     d->squareUrl   = squareUrl;
0065     d->ancestors   = ancestors;
0066 }
0067 
0068 Taxon::Taxon()
0069     : d(new Private)
0070 {
0071 }
0072 
0073 Taxon::Taxon(const Taxon& other)
0074     : d(new Private)
0075 {
0076     *d = *other.d;
0077 }
0078 
0079 Taxon::~Taxon()
0080 {
0081     delete d;
0082 }
0083 
0084 Taxon& Taxon::operator=(const Taxon& other)
0085 {
0086     *d = *other.d;
0087 
0088     return *this;
0089 }
0090 
0091 bool Taxon::operator!=(const Taxon& other) const
0092 {
0093     return (d->id != other.d->id);
0094 }
0095 
0096 bool Taxon::operator==(const Taxon& other) const
0097 {
0098     return (d->id == other.d->id);
0099 }
0100 
0101 int Taxon::id() const
0102 {
0103     return d->id;
0104 }
0105 
0106 int Taxon::parentId() const
0107 {
0108     return d->parentId;
0109 }
0110 
0111 const QString& Taxon::name() const
0112 {
0113     return d->name;
0114 }
0115 
0116 /**
0117  * return italicized scientific name
0118  */
0119 QString Taxon::htmlName() const
0120 {
0121     static const QString species    = QLatin1String("species");
0122     static const QString subspecies = QLatin1String("subspecies");
0123     static const QString variety    = QLatin1String("variety");
0124     static const QString hybrid     = QLatin1String("hybrid");
0125     static const QString italicOn   = QLatin1String("<i>");
0126     static const QString italicOff  = QLatin1String("</i>");
0127     static const QChar   blank      = QLatin1Char(' ');
0128 
0129     QString result;
0130 
0131     if ((rank() != species) && (rank() != subspecies) && (rank() != variety))
0132     {
0133         result    = localizedTaxonomicRank(rank()) + blank;
0134         result[0] = result[0].toTitleCase();
0135     }
0136 
0137     QStringList split = name().split(blank);
0138 
0139     if (split.count() == 3 &&
0140         ((rank() == subspecies) || (rank() == variety) || (rank() == hybrid)))
0141     {
0142         QString txt = (rank() == subspecies) ? QLatin1String(" ssp. ")
0143                                              : (rank() == variety) ? QLatin1String(" var. ")
0144                                                                    : QLatin1String(" x ");
0145         result += italicOn + split[0] + blank + split[1] + italicOff + txt +
0146                   italicOn + split[2] + italicOff;
0147     }
0148     else if (split.count() == 4)
0149     {
0150         result += italicOn + split[0] + blank + split[1] + italicOff + blank +
0151                   split[2] + blank + italicOn + split[3] + italicOff;
0152     }
0153     else
0154     {
0155         result += italicOn + name() + italicOff;
0156     }
0157 
0158     return result;
0159 }
0160 
0161 const QString& Taxon::rank() const
0162 {
0163     return d->rank;
0164 }
0165 
0166 double Taxon::rankLevel() const
0167 {
0168     return d->rankLevel;
0169 }
0170 
0171 const QString& Taxon::matchedTerm() const
0172 {
0173     return d->matchedTerm;
0174 }
0175 
0176 const QUrl& Taxon::squareUrl() const
0177 {
0178     return d->squareUrl;
0179 }
0180 
0181 const QString& Taxon::commonName() const
0182 {
0183     return d->commonName;
0184 }
0185 
0186 const QList<Taxon>& Taxon::ancestors() const
0187 {
0188     return d->ancestors;
0189 }
0190 
0191 bool Taxon::isValid() const
0192 {
0193     return (d->id != -1);
0194 }
0195 
0196 } // namespace DigikamGenericINatPlugin