File indexing completed on 2024-04-14 03:55:27

0001 /*
0002     SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "attribute.h"
0008 #include "kateextendedattribute.h"
0009 
0010 #include <KSyntaxHighlighting/Theme>
0011 
0012 #include <QDebug>
0013 #include <QMetaEnum>
0014 
0015 using namespace KTextEditor;
0016 
0017 class KTextEditor::AttributePrivate
0018 {
0019 public:
0020     AttributePrivate()
0021     {
0022         dynamicAttributes.append(Attribute::Ptr());
0023         dynamicAttributes.append(Attribute::Ptr());
0024     }
0025 
0026     QList<Attribute::Ptr> dynamicAttributes;
0027 };
0028 
0029 Attribute::Attribute()
0030     : d(new AttributePrivate())
0031 {
0032 }
0033 
0034 Attribute::Attribute(const QString &name, KSyntaxHighlighting::Theme::TextStyle style)
0035     : d(new AttributePrivate())
0036 {
0037     setName(name);
0038     setDefaultStyle(style);
0039 }
0040 
0041 Attribute::Attribute(const Attribute &a)
0042     : QTextCharFormat(a)
0043     , QSharedData()
0044     , d(new AttributePrivate())
0045 {
0046     d->dynamicAttributes = a.d->dynamicAttributes;
0047 }
0048 
0049 Attribute::~Attribute()
0050 {
0051     delete d;
0052 }
0053 
0054 Attribute &Attribute::operator+=(const Attribute &a)
0055 {
0056     merge(a);
0057 
0058     for (int i = 0; i < a.d->dynamicAttributes.count(); ++i) {
0059         if (i < d->dynamicAttributes.count()) {
0060             if (a.d->dynamicAttributes[i]) {
0061                 d->dynamicAttributes[i] = a.d->dynamicAttributes[i];
0062             }
0063         } else {
0064             d->dynamicAttributes.append(a.d->dynamicAttributes[i]);
0065         }
0066     }
0067 
0068     return *this;
0069 }
0070 
0071 Attribute::Ptr Attribute::dynamicAttribute(ActivationType type) const
0072 {
0073     if (type < 0 || type >= d->dynamicAttributes.count()) {
0074         return Ptr();
0075     }
0076 
0077     return d->dynamicAttributes[type];
0078 }
0079 
0080 void Attribute::setDynamicAttribute(ActivationType type, Attribute::Ptr attribute)
0081 {
0082     if (type < 0 || type > ActivateCaretIn) {
0083         return;
0084     }
0085 
0086     d->dynamicAttributes[type] = std::move(attribute);
0087 }
0088 
0089 QString Attribute::name() const
0090 {
0091     return stringProperty(AttributeName);
0092 }
0093 
0094 void Attribute::setName(const QString &name)
0095 {
0096     setProperty(AttributeName, name);
0097 }
0098 
0099 KSyntaxHighlighting::Theme::TextStyle Attribute::defaultStyle() const
0100 {
0101     return static_cast<KSyntaxHighlighting::Theme::TextStyle>(intProperty(AttributeDefaultStyleIndex));
0102 }
0103 
0104 void Attribute::setDefaultStyle(KSyntaxHighlighting::Theme::TextStyle style)
0105 {
0106     setProperty(AttributeDefaultStyleIndex, QVariant(static_cast<int>(style)));
0107 }
0108 
0109 bool Attribute::skipSpellChecking() const
0110 {
0111     return boolProperty(Spellchecking);
0112 }
0113 
0114 void Attribute::setSkipSpellChecking(bool skipspellchecking)
0115 {
0116     setProperty(Spellchecking, QVariant(skipspellchecking));
0117 }
0118 
0119 QBrush Attribute::outline() const
0120 {
0121     if (hasProperty(Outline)) {
0122         return property(Outline).value<QBrush>();
0123     }
0124 
0125     return QBrush();
0126 }
0127 
0128 void Attribute::setOutline(const QBrush &brush)
0129 {
0130     setProperty(Outline, brush);
0131 }
0132 
0133 QBrush Attribute::selectedForeground() const
0134 {
0135     if (hasProperty(SelectedForeground)) {
0136         return property(SelectedForeground).value<QBrush>();
0137     }
0138 
0139     return QBrush();
0140 }
0141 
0142 void Attribute::setSelectedForeground(const QBrush &foreground)
0143 {
0144     setProperty(SelectedForeground, foreground);
0145 }
0146 
0147 bool Attribute::backgroundFillWhitespace() const
0148 {
0149     if (hasProperty(BackgroundFillWhitespace)) {
0150         return boolProperty(BackgroundFillWhitespace);
0151     }
0152 
0153     return true;
0154 }
0155 
0156 void Attribute::setBackgroundFillWhitespace(bool fillWhitespace)
0157 {
0158     setProperty(BackgroundFillWhitespace, fillWhitespace);
0159 }
0160 
0161 QBrush Attribute::selectedBackground() const
0162 {
0163     if (hasProperty(SelectedBackground)) {
0164         return property(SelectedBackground).value<QBrush>();
0165     }
0166 
0167     return QBrush();
0168 }
0169 
0170 void Attribute::setSelectedBackground(const QBrush &brush)
0171 {
0172     setProperty(SelectedBackground, brush);
0173 }
0174 
0175 void Attribute::clear()
0176 {
0177     QTextCharFormat::operator=(QTextCharFormat());
0178 
0179     d->dynamicAttributes.clear();
0180     d->dynamicAttributes.append(Ptr());
0181     d->dynamicAttributes.append(Ptr());
0182 }
0183 
0184 bool Attribute::fontBold() const
0185 {
0186     return fontWeight() == QFont::Bold;
0187 }
0188 
0189 void Attribute::setFontBold(bool bold)
0190 {
0191     if (bold) {
0192         setFontWeight(QFont::Bold);
0193     } else {
0194         clearProperty(QTextFormat::FontWeight);
0195     }
0196 }
0197 
0198 bool Attribute::hasAnyProperty() const
0199 {
0200     return !properties().isEmpty();
0201 }
0202 
0203 Attribute &KTextEditor::Attribute::operator=(const Attribute &a)
0204 {
0205     QTextCharFormat::operator=(a);
0206     Q_ASSERT(static_cast<QTextCharFormat>(*this) == a);
0207 
0208     d->dynamicAttributes = a.d->dynamicAttributes;
0209 
0210     return *this;
0211 }