| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "attribute.h" |
| 8 | #include "kateextendedattribute.h" |
| 9 | |
| 10 | #include <KSyntaxHighlighting/Theme> |
| 11 | |
| 12 | #include <QDebug> |
| 13 | #include <QMetaEnum> |
| 14 | |
| 15 | using namespace KTextEditor; |
| 16 | |
| 17 | class KTextEditor::AttributePrivate |
| 18 | { |
| 19 | public: |
| 20 | AttributePrivate() |
| 21 | { |
| 22 | dynamicAttributes.append(t: Attribute::Ptr()); |
| 23 | dynamicAttributes.append(t: Attribute::Ptr()); |
| 24 | } |
| 25 | |
| 26 | QList<Attribute::Ptr> dynamicAttributes; |
| 27 | }; |
| 28 | |
| 29 | Attribute::Attribute() |
| 30 | : d(new AttributePrivate()) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | Attribute::Attribute(const QString &name, KSyntaxHighlighting::Theme::TextStyle style) |
| 35 | : d(new AttributePrivate()) |
| 36 | { |
| 37 | setName(name); |
| 38 | setDefaultStyle(style); |
| 39 | } |
| 40 | |
| 41 | Attribute::Attribute(const Attribute &a) |
| 42 | : QTextCharFormat(a) |
| 43 | , QSharedData() |
| 44 | , d(new AttributePrivate()) |
| 45 | { |
| 46 | d->dynamicAttributes = a.d->dynamicAttributes; |
| 47 | } |
| 48 | |
| 49 | Attribute::~Attribute() |
| 50 | { |
| 51 | delete d; |
| 52 | } |
| 53 | |
| 54 | Attribute &Attribute::operator+=(const Attribute &a) |
| 55 | { |
| 56 | merge(other: a); |
| 57 | |
| 58 | for (int i = 0; i < a.d->dynamicAttributes.count(); ++i) { |
| 59 | if (i < d->dynamicAttributes.count()) { |
| 60 | if (a.d->dynamicAttributes[i]) { |
| 61 | d->dynamicAttributes[i] = a.d->dynamicAttributes[i]; |
| 62 | } |
| 63 | } else { |
| 64 | d->dynamicAttributes.append(t: a.d->dynamicAttributes[i]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | Attribute::Ptr Attribute::dynamicAttribute(ActivationType type) const |
| 72 | { |
| 73 | if (type < 0 || type >= d->dynamicAttributes.count()) { |
| 74 | return Ptr(); |
| 75 | } |
| 76 | |
| 77 | return d->dynamicAttributes[type]; |
| 78 | } |
| 79 | |
| 80 | void Attribute::setDynamicAttribute(ActivationType type, Attribute::Ptr attribute) |
| 81 | { |
| 82 | if (type < 0 || type > ActivateCaretIn) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | d->dynamicAttributes[type] = std::move(attribute); |
| 87 | } |
| 88 | |
| 89 | QString Attribute::name() const |
| 90 | { |
| 91 | return stringProperty(propertyId: AttributeName); |
| 92 | } |
| 93 | |
| 94 | void Attribute::setName(const QString &name) |
| 95 | { |
| 96 | setProperty(propertyId: AttributeName, value: name); |
| 97 | } |
| 98 | |
| 99 | KSyntaxHighlighting::Theme::TextStyle Attribute::defaultStyle() const |
| 100 | { |
| 101 | return static_cast<KSyntaxHighlighting::Theme::TextStyle>(intProperty(propertyId: AttributeDefaultStyleIndex)); |
| 102 | } |
| 103 | |
| 104 | void Attribute::setDefaultStyle(KSyntaxHighlighting::Theme::TextStyle style) |
| 105 | { |
| 106 | setProperty(propertyId: AttributeDefaultStyleIndex, value: QVariant(static_cast<int>(style))); |
| 107 | } |
| 108 | |
| 109 | bool Attribute::skipSpellChecking() const |
| 110 | { |
| 111 | return boolProperty(propertyId: Spellchecking); |
| 112 | } |
| 113 | |
| 114 | void Attribute::setSkipSpellChecking(bool skipspellchecking) |
| 115 | { |
| 116 | setProperty(propertyId: Spellchecking, value: QVariant(skipspellchecking)); |
| 117 | } |
| 118 | |
| 119 | QBrush Attribute::outline() const |
| 120 | { |
| 121 | if (hasProperty(propertyId: Outline)) { |
| 122 | return property(propertyId: Outline).value<QBrush>(); |
| 123 | } |
| 124 | |
| 125 | return QBrush(); |
| 126 | } |
| 127 | |
| 128 | void Attribute::setOutline(const QBrush &brush) |
| 129 | { |
| 130 | setProperty(propertyId: Outline, value: brush); |
| 131 | } |
| 132 | |
| 133 | QBrush Attribute::selectedForeground() const |
| 134 | { |
| 135 | if (hasProperty(propertyId: SelectedForeground)) { |
| 136 | return property(propertyId: SelectedForeground).value<QBrush>(); |
| 137 | } |
| 138 | |
| 139 | return QBrush(); |
| 140 | } |
| 141 | |
| 142 | void Attribute::setSelectedForeground(const QBrush &foreground) |
| 143 | { |
| 144 | setProperty(propertyId: SelectedForeground, value: foreground); |
| 145 | } |
| 146 | |
| 147 | bool Attribute::backgroundFillWhitespace() const |
| 148 | { |
| 149 | if (hasProperty(propertyId: BackgroundFillWhitespace)) { |
| 150 | return boolProperty(propertyId: BackgroundFillWhitespace); |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | void Attribute::setBackgroundFillWhitespace(bool fillWhitespace) |
| 157 | { |
| 158 | setProperty(propertyId: BackgroundFillWhitespace, value: fillWhitespace); |
| 159 | } |
| 160 | |
| 161 | QBrush Attribute::selectedBackground() const |
| 162 | { |
| 163 | if (hasProperty(propertyId: SelectedBackground)) { |
| 164 | return property(propertyId: SelectedBackground).value<QBrush>(); |
| 165 | } |
| 166 | |
| 167 | return QBrush(); |
| 168 | } |
| 169 | |
| 170 | void Attribute::setSelectedBackground(const QBrush &brush) |
| 171 | { |
| 172 | setProperty(propertyId: SelectedBackground, value: brush); |
| 173 | } |
| 174 | |
| 175 | void Attribute::clear() |
| 176 | { |
| 177 | QTextCharFormat::operator=(QTextCharFormat()); |
| 178 | |
| 179 | d->dynamicAttributes.clear(); |
| 180 | d->dynamicAttributes.append(t: Ptr()); |
| 181 | d->dynamicAttributes.append(t: Ptr()); |
| 182 | } |
| 183 | |
| 184 | bool Attribute::fontBold() const |
| 185 | { |
| 186 | return fontWeight() == QFont::Bold; |
| 187 | } |
| 188 | |
| 189 | void Attribute::setFontBold(bool bold) |
| 190 | { |
| 191 | if (bold) { |
| 192 | setFontWeight(QFont::Bold); |
| 193 | } else { |
| 194 | clearProperty(propertyId: QTextFormat::FontWeight); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | bool Attribute::hasAnyProperty() const |
| 199 | { |
| 200 | return !properties().isEmpty(); |
| 201 | } |
| 202 | |
| 203 | Attribute &KTextEditor::Attribute::operator=(const Attribute &a) |
| 204 | { |
| 205 | QTextCharFormat::operator=(a); |
| 206 | Q_ASSERT(static_cast<QTextCharFormat>(*this) == a); |
| 207 | |
| 208 | d->dynamicAttributes = a.d->dynamicAttributes; |
| 209 | |
| 210 | return *this; |
| 211 | } |
| 212 | |