File indexing completed on 2024-04-28 15:30:51

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