File indexing completed on 2024-05-19 05:05:20

0001 /***************************************************************************
0002  *   SPDX-License-Identifier: GPL-2.0-or-later
0003  *                                                                         *
0004  *   SPDX-FileCopyrightText: 2004-2023 Thomas Fischer <fischer@unix-ag.uni-kl.de>
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  *   This program is distributed in the hope that it will be useful,       *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0014  *   GNU General Public License for more details.                          *
0015  *                                                                         *
0016  *   You should have received a copy of the GNU General Public License     *
0017  *   along with this program; if not, see <https://www.gnu.org/licenses/>. *
0018  ***************************************************************************/
0019 
0020 #include "comment.h"
0021 
0022 #include <typeinfo>
0023 
0024 #include <QStringList>
0025 
0026 #include "logging_data.h"
0027 
0028 /**
0029  * Private class to store internal variables that should not be visible
0030  * in the interface as defined in the header file.
0031  */
0032 class Comment::Private
0033 {
0034 public:
0035     QString text;
0036     Preferences::CommentContext context;
0037     // For use with context 'Prefix'
0038     QString prefix;
0039 
0040     Private(const QString &_text, Preferences::CommentContext _context, const QString &_prefix)
0041             : text(_text), context(_context), prefix(_prefix)
0042     {
0043         if (context != Preferences::CommentContext::Prefix)
0044             prefix.clear();
0045         else if (context == Preferences::CommentContext::Prefix && (prefix.isEmpty() || !prefix.startsWith(QStringLiteral("%")))) {
0046             qCDebug(LOG_KBIBTEX_DATA) << "Requested to create Comment with context 'prefix', but provided prefix was either empty or did not start with '%'. Changing comments context to 'Verbatim' and clearing prefix.";
0047             context = Preferences::CommentContext::Verbatim;
0048             prefix.clear();
0049         }
0050     }
0051 };
0052 
0053 Comment::Comment(const QString &text, Preferences::CommentContext context, const QString &prefix)
0054         : Element(), d(new Comment::Private(text, context, prefix))
0055 {
0056     // nothing
0057 }
0058 
0059 Comment::Comment(const Comment &other)
0060         : Element(), d(new Comment::Private(other.d->text, other.d->context, other.d->prefix))
0061 {
0062     // nothing
0063 }
0064 
0065 Comment::~Comment()
0066 {
0067     delete d;
0068 }
0069 
0070 Comment &Comment::operator= (const Comment &other)
0071 {
0072     if (this != &other) {
0073         d->text = other.d->text;
0074         d->context = other.d->context;
0075         d->prefix = other.d->prefix;
0076     }
0077     return *this;
0078 }
0079 
0080 QString Comment::text() const
0081 {
0082     return d->text;
0083 }
0084 
0085 void Comment::setText(const QString &text)
0086 {
0087     d->text = text;
0088 }
0089 
0090 Preferences::CommentContext Comment::context() const
0091 {
0092     return d->context;
0093 }
0094 
0095 void Comment::setContext(Preferences::CommentContext context)
0096 {
0097     d->context = context;
0098 }
0099 
0100 QString Comment::prefix() const
0101 {
0102     return d->prefix;
0103 }
0104 
0105 void Comment::setPrefix(const QString &prefix)
0106 {
0107     d->prefix = prefix;
0108 }
0109 
0110 bool Comment::isComment(const Element &other) {
0111     return typeid(other) == typeid(Comment);
0112 }
0113 
0114 bool Comment::operator==(const Comment &other) const
0115 {
0116     return d->text == other.d->text && d->context == other.d->context && (d->context != Preferences::CommentContext::Prefix || d->prefix == other.d->prefix);
0117 }
0118 
0119 bool Comment::operator!=(const Comment &other) const
0120 {
0121     return !operator==(other);
0122 }
0123 
0124 QDebug operator<<(QDebug dbg, const Comment &comment) {
0125     dbg.nospace() << "Comment " << comment.text();
0126     return dbg;
0127 }