File indexing completed on 2024-05-19 04:00:02
0001 /* 0002 SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #ifndef KTEXTEDITOR_ATTRIBUTE_H 0008 #define KTEXTEDITOR_ATTRIBUTE_H 0009 0010 #include <QExplicitlySharedDataPointer> 0011 #include <QSharedData> 0012 #include <QTextCharFormat> 0013 0014 #include <KSyntaxHighlighting/Theme> 0015 0016 #include <ktexteditor_export.h> 0017 0018 class QAction; 0019 0020 namespace KTextEditor 0021 { 0022 0023 /** 0024 * \class Attribute attribute.h <KTextEditor/Attribute> 0025 * 0026 * \brief A class which provides customized text decorations. 0027 * 0028 * The Attribute class extends QTextCharFormat, the class which Qt 0029 * uses internally to provide formatting information to characters 0030 * in a text document. 0031 * 0032 * In addition to its inherited properties, it provides support for: 0033 * \li several customized text formatting properties 0034 * \li dynamic highlighting of associated ranges of text 0035 * \li binding of actions with associated ranges of text (note: not currently implemented) 0036 * 0037 * Implementations are not required to support all properties. 0038 * In particular, several properties are not supported for dynamic 0039 * highlighting (notably: font() and fontBold()). 0040 * 0041 * Unfortunately, as QTextFormat's setProperty() is not virtual, 0042 * changes that are made to this attribute cannot automatically be 0043 * redrawn. Once you have finished changing properties, you should 0044 * call changed() to force redrawing of affected ranges of text. 0045 * 0046 * \sa MovingInterface 0047 * 0048 * \author Hamish Rodda \<rodda@kde.org\> 0049 */ 0050 class KTEXTEDITOR_EXPORT Attribute : public QTextCharFormat, public QSharedData 0051 { 0052 public: 0053 /** 0054 * Shared data pointer for Attribute 0055 */ 0056 typedef QExplicitlySharedDataPointer<Attribute> Ptr; 0057 0058 /** 0059 * Default constructor. 0060 * The resulting Attribute has no properties set to begin with. 0061 */ 0062 Attribute(); 0063 0064 /** 0065 * Construct attribute with given name & default style properties. 0066 * @param name attribute name 0067 * @param style attribute default style 0068 */ 0069 Attribute(const QString &name, KSyntaxHighlighting::Theme::TextStyle style); 0070 0071 /** 0072 * Copy constructor. 0073 */ 0074 Attribute(const Attribute &a); 0075 0076 /** 0077 * Virtual destructor. 0078 */ 0079 virtual ~Attribute(); 0080 0081 // BEGIN custom properties 0082 0083 /** 0084 * \name Custom properties 0085 * 0086 * The following functions provide custom properties which can be set for 0087 * rendering by editor implementations. 0088 * \{ 0089 */ 0090 0091 /** 0092 * Attribute name 0093 * 0094 * \return attribute name 0095 */ 0096 QString name() const; 0097 0098 /** 0099 * Set attribute name 0100 * 0101 * \param name new attribute name 0102 */ 0103 void setName(const QString &name); 0104 0105 /** 0106 * Default style of this attribute 0107 * 0108 * \return default style 0109 */ 0110 KSyntaxHighlighting::Theme::TextStyle defaultStyle() const; 0111 0112 /** 0113 * Set default style of this attribute 0114 * 0115 * \param style new default style 0116 */ 0117 void setDefaultStyle(KSyntaxHighlighting::Theme::TextStyle style); 0118 0119 /** 0120 * Should spellchecking be skipped? 0121 * 0122 * \return skip spellchecking? 0123 */ 0124 bool skipSpellChecking() const; 0125 0126 /** 0127 * Set if we should spellchecking be skipped? 0128 * 0129 * @param skipspellchecking should spellchecking be skipped? 0130 */ 0131 void setSkipSpellChecking(bool skipspellchecking); 0132 0133 /** 0134 * Find out if the font weight is set to QFont::Bold. 0135 * 0136 * \return \c true if the font weight is exactly QFont::Bold, otherwise \c false 0137 * 0138 * \see QTextCharFormat::fontWeight() 0139 */ 0140 bool fontBold() const; 0141 0142 /** 0143 * Set the font weight to QFont::Bold. If \a bold is \p false, the weight will be set to 0 (normal). 0144 * 0145 * \param bold whether the font weight should be bold or not. 0146 * 0147 * \see QTextCharFormat::setFontWeight() 0148 */ 0149 void setFontBold(bool bold = true); 0150 0151 /** 0152 * Get the brush used to draw an outline around text, if any. 0153 * 0154 * \return brush to be used to draw an outline, or Qt::NoBrush if no outline is set. 0155 */ 0156 QBrush outline() const; 0157 0158 /** 0159 * Set a brush to be used to draw an outline around text. 0160 * 0161 * Use \p clearProperty(Outline) to clear. 0162 * 0163 * \param brush brush to be used to draw an outline. 0164 */ 0165 void setOutline(const QBrush &brush); 0166 0167 /** 0168 * Get the brush used to draw text when it is selected, if any. 0169 * 0170 * \return brush to be used to draw selected text, or Qt::NoBrush if not set 0171 */ 0172 QBrush selectedForeground() const; 0173 0174 /** 0175 * Set a brush to be used to draw selected text. 0176 * 0177 * Use \p clearProperty(SelectedForeground) to clear. 0178 * 0179 * \param foreground brush to be used to draw selected text. 0180 */ 0181 void setSelectedForeground(const QBrush &foreground); 0182 0183 /** 0184 * Get the brush used to draw the background of selected text, if any. 0185 * 0186 * \return brush to be used to draw the background of selected text, or Qt::NoBrush if not set 0187 */ 0188 QBrush selectedBackground() const; 0189 0190 /** 0191 * Set a brush to be used to draw the background of selected text, if any. 0192 * 0193 * Use \p clearProperty(SelectedBackground) to clear. 0194 * 0195 * \param brush brush to be used to draw the background of selected text 0196 */ 0197 void setSelectedBackground(const QBrush &brush); 0198 0199 /** 0200 * Determine whether background color is drawn over whitespace. Defaults to true if not set. 0201 * 0202 * \return whether the background color should be drawn over whitespace 0203 */ 0204 bool backgroundFillWhitespace() const; 0205 0206 /** 0207 * Set whether background color is drawn over whitespace. Defaults to true if not set. 0208 * 0209 * Use \p clearProperty(BackgroundFillWhitespace) to clear. 0210 * 0211 * \param fillWhitespace whether the background should be drawn over whitespace. 0212 */ 0213 void setBackgroundFillWhitespace(bool fillWhitespace); 0214 0215 /** 0216 * Clear all set properties. 0217 */ 0218 void clear(); 0219 0220 /** 0221 * Determine if any properties are set. 0222 * 0223 * \return \e true if any properties are set, otherwise \e false 0224 */ 0225 bool hasAnyProperty() const; 0226 0227 // END 0228 0229 // BEGIN Dynamic highlighting 0230 0231 /** 0232 * \name Dynamic highlighting 0233 * 0234 * The following functions allow for text to be highlighted dynamically based on 0235 * several events. 0236 * \{ 0237 */ 0238 0239 /** 0240 * Several automatic activation mechanisms exist for associated attributes. 0241 * Using this you can conveniently have your ranges highlighted when either 0242 * the mouse or cursor enter the range. 0243 */ 0244 enum ActivationType { 0245 /// Activate attribute on mouse in 0246 ActivateMouseIn = 0, 0247 /// Activate attribute on caret in 0248 ActivateCaretIn 0249 }; 0250 0251 /** 0252 * Return the attribute to use when the event referred to by \a type occurs. 0253 * 0254 * \param type the activation type for which to return the Attribute. 0255 * 0256 * \returns the attribute to be used for events specified by \a type, or null if none is set. 0257 */ 0258 Attribute::Ptr dynamicAttribute(ActivationType type) const; 0259 0260 /** 0261 * Set the attribute to use when the event referred to by \a type occurs. 0262 * 0263 * \note Nested dynamic attributes are ignored. 0264 * 0265 * \param type the activation type to set the attribute for 0266 * \param attribute the attribute to assign. As attribute is refcounted, ownership is not an issue. 0267 */ 0268 void setDynamicAttribute(ActivationType type, Attribute::Ptr attribute); 0269 0270 //!\} 0271 0272 // END 0273 0274 /** 0275 * Addition assignment operator. Use this to merge another Attribute with this Attribute. 0276 * Where both attributes have a particular property set, the property in \a a will 0277 * be used. 0278 * 0279 * \param a attribute to merge into this attribute. 0280 */ 0281 Attribute &operator+=(const Attribute &a); 0282 0283 /** 0284 * Replacement assignment operator. Use this to overwrite this Attribute with another Attribute. 0285 * 0286 * \param a attribute to assign to this attribute. 0287 */ 0288 Attribute &operator=(const Attribute &a); 0289 0290 private: 0291 /** 0292 * Private d-pointer 0293 */ 0294 class AttributePrivate *const d; 0295 }; 0296 0297 /** 0298 * @brief Attribute%s of a part of a line. 0299 * 0300 * An AttributeBlock represents an Attribute spanning the interval 0301 * [start, start + length) of a given line. An AttributeBlock is 0302 * obtained by calling KTextEditor::View::lineAttributes(). 0303 * 0304 * \see KTextEditor::View::lineAttributes() 0305 */ 0306 class AttributeBlock 0307 { 0308 public: 0309 /** 0310 * Constructor of AttributeBlock. 0311 */ 0312 AttributeBlock(int _start, int _length, const Attribute::Ptr &_attribute) 0313 : start(_start) 0314 , length(_length) 0315 , attribute(_attribute) 0316 { 0317 } 0318 0319 /** 0320 * The column this attribute starts at. 0321 */ 0322 int start; 0323 0324 /** 0325 * The number of columns this attribute spans. 0326 */ 0327 int length; 0328 0329 /** 0330 * The attribute for the current range. 0331 */ 0332 Attribute::Ptr attribute; 0333 }; 0334 0335 } 0336 0337 Q_DECLARE_TYPEINFO(KTextEditor::AttributeBlock, Q_RELOCATABLE_TYPE); 0338 0339 #endif