File indexing completed on 2024-04-21 03:58:27

0001 /*
0002     krichtextedit.h
0003     SPDX-FileCopyrightText: 2007 Laurent Montel <montel@kde.org>
0004     SPDX-FileCopyrightText: 2008 Thomas McGuire <thomas.mcguire@gmx.net>
0005     SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.1-or-later
0008 */
0009 
0010 #ifndef KRICHTEXTEDIT_H
0011 #define KRICHTEXTEDIT_H
0012 
0013 #include <ktextedit.h>
0014 
0015 class QKeyEvent;
0016 
0017 class KRichTextEditPrivate;
0018 
0019 #include <ktextwidgets_export.h>
0020 
0021 /**
0022  * @class KRichTextEdit krichtextedit.h <KRichTextEdit>
0023  *
0024  * The KRichTextEdit class provides a widget to edit and display rich text.
0025  *
0026  * It offers several additional rich text editing functions to KTextEdit and makes
0027  * them easier to access including:
0028  *
0029  * @li Changing fonts, sizes.
0030  * @li Font formatting, such as bold, underline, italic, foreground and
0031  *     background color.
0032  * @li Paragraph alignment
0033  * @li Ability to edit and remove hyperlinks
0034  * @li Nested list handling
0035  * @li Simple actions to insert tables. TODO
0036  *
0037  * The KRichTextEdit can be in two modes: Rich text mode and plain text mode.
0038  * Calling functions which modify the format/style of the text will automatically
0039  * enable the rich text mode. Rich text mode is sometimes also referred to as
0040  * HTML mode.
0041  *
0042  * Do not call setAcceptRichText() or acceptRichText() yourself. Instead simply
0043  * connect to the slots which insert the rich text, use switchToPlainText() or
0044  * enableRichTextMode().
0045  *
0046  * \image html krichtextedit.png "KRichTextEdit Widget"
0047  *
0048  * @since 4.1
0049  */
0050 class KTEXTWIDGETS_EXPORT KRichTextEdit : public KTextEdit
0051 {
0052     Q_OBJECT
0053 
0054 public:
0055     /**
0056      * The mode the edit widget is in.
0057      */
0058     enum Mode {
0059         Plain, ///< Plain text mode
0060         Rich, ///< Rich text mode
0061     };
0062 
0063     /**
0064      * Constructs a KRichTextEdit object
0065      *
0066      * @param text The initial text of the text edit, which is interpreted as
0067      *             HTML.
0068      * @param parent The parent widget
0069      */
0070     explicit KRichTextEdit(const QString &text, QWidget *parent = nullptr);
0071 
0072     /**
0073      * Constructs a KRichTextEdit object.
0074      *
0075      * @param parent The parent widget
0076      */
0077     explicit KRichTextEdit(QWidget *parent = nullptr);
0078 
0079     /**
0080      * Destructor.
0081      */
0082     ~KRichTextEdit() override;
0083 
0084     /**
0085      * This enables rich text mode. Nothing is done except changing the internal
0086      * mode and allowing rich text pastes.
0087      */
0088     void enableRichTextMode();
0089 
0090     /**
0091      * @return The current text mode
0092      */
0093     Mode textMode() const;
0094 
0095     /**
0096      * @return The plain text string if in plain text mode or the HTML code
0097      *         if in rich text mode. The text is not word-wrapped.
0098      */
0099     QString textOrHtml() const;
0100 
0101     /**
0102      * Replaces all the content of the text edit with the given string.
0103      * If the string is in rich text format, the text is inserted as rich text,
0104      * otherwise it is inserted as plain text.
0105      *
0106      * @param text The text to insert
0107      */
0108     void setTextOrHtml(const QString &text);
0109 
0110     /**
0111      * Returns the text of the link at the current position or an empty string
0112      * if the cursor is not on a link.
0113      *
0114      * @sa currentLinkUrl
0115      * @return The link text
0116      */
0117     QString currentLinkText() const;
0118 
0119     /**
0120      * Returns the URL target (href) of the link at the current position or an
0121      * empty string if the cursor is not on a link.
0122      *
0123      * @sa currentLinkText
0124      * @return The link target URL
0125      */
0126     QString currentLinkUrl() const;
0127 
0128     /**
0129      * If the cursor is on a link, sets the @a cursor to a selection of the
0130      * text of the link. If the @a cursor is not on a link, selects the current word
0131      * or existing selection.
0132      *
0133      * @param cursor The cursor to use to select the text.
0134      * @sa updateLink
0135      */
0136     void selectLinkText(QTextCursor *cursor) const;
0137 
0138     /**
0139      * Convenience function to select the link text using the active cursor.
0140      *
0141      * @sa selectLinkText
0142      */
0143     void selectLinkText() const;
0144 
0145     /**
0146      * Replaces the current selection with a hyperlink with the link URL @a linkUrl
0147      * and the link text @a linkText.
0148      *
0149      * @sa selectLinkText
0150      * @sa currentLinkUrl
0151      * @sa currentLinkText
0152      * @param linkUrl The link will get this URL as href (target)
0153      * @param linkText The link will get this alternative text, which is the
0154      *                 text displayed in the text edit.
0155      */
0156     void updateLink(const QString &linkUrl, const QString &linkText);
0157 
0158     /**
0159      * Returns true if the list item at the current position can be indented.
0160      *
0161      * @sa canDedentList
0162      */
0163     bool canIndentList() const;
0164 
0165     /**
0166      * Returns true if the list item at the current position can be dedented.
0167      *
0168      * @sa canIndentList
0169      */
0170     bool canDedentList() const;
0171 
0172 public Q_SLOTS:
0173 
0174     /**
0175      * Sets the alignment of the current block to Left Aligned
0176      */
0177     void alignLeft();
0178 
0179     /**
0180      * Sets the alignment of the current block to Centered
0181      */
0182     void alignCenter();
0183 
0184     /**
0185      * Sets the alignment of the current block to Right Aligned
0186      */
0187     void alignRight();
0188 
0189     /**
0190      * Sets the alignment of the current block to Justified
0191      */
0192     void alignJustify();
0193 
0194     /**
0195      * Sets the direction of the current block to Right-To-Left
0196      *
0197      * @since 4.6
0198      */
0199     void makeRightToLeft();
0200 
0201     /**
0202      * Sets the direction of the current block to Left-To-Right
0203      *
0204      * @since 4.6
0205      */
0206     void makeLeftToRight();
0207 
0208     /**
0209      * Sets the list style of the current list, or creates a new list using the
0210      * current block. The @a _styleindex corresponds to the QTextListFormat::Style
0211      *
0212      * @param _styleIndex The list will get this style
0213      */
0214     void setListStyle(int _styleIndex);
0215 
0216     /**
0217      * Increases the nesting level of the current block or selected blocks.
0218      *
0219      * @sa canIndentList
0220      */
0221     void indentListMore();
0222 
0223     /**
0224      * Decreases the nesting level of the current block or selected blocks.
0225      *
0226      * @sa canDedentList
0227      */
0228     void indentListLess();
0229 
0230     /**
0231      * Sets the current word or selection to the font family @a fontFamily
0232      *
0233      * @param fontFamily The text's font family will be changed to this one
0234      */
0235     void setFontFamily(const QString &fontFamily);
0236 
0237     /**
0238      * Sets the current word or selection to the font size @a size
0239      *
0240      * @param size The text's font will get this size
0241      */
0242     void setFontSize(int size);
0243 
0244     /**
0245      * Sets the current word or selection to the font @a font
0246      *
0247      * @param font the font of the text will be set to this font
0248      */
0249     void setFont(const QFont &font);
0250 
0251     /**
0252      * Toggles the bold formatting of the current word or selection at the current
0253      * cursor position.
0254      *
0255      * @param bold If true, the text will be set to bold
0256      */
0257     void setTextBold(bool bold);
0258 
0259     /**
0260      * Toggles the italic formatting of the current word or selection at the current
0261      * cursor position.
0262      *
0263      * @param italic If true, the text will be set to italic
0264      */
0265     void setTextItalic(bool italic);
0266 
0267     /**
0268      * Toggles the underline formatting of the current word or selection at the current
0269      * cursor position.
0270      *
0271      * @param underline If true, the text will be underlined
0272      */
0273     void setTextUnderline(bool underline);
0274 
0275     /**
0276      * Toggles the strikeout formatting of the current word or selection at the current
0277      * cursor position.
0278      *
0279      * @param strikeOut If true, the text will be struck out
0280      */
0281     void setTextStrikeOut(bool strikeOut);
0282 
0283     /**
0284      * Sets the foreground color of the current word or selection to @a color.
0285      *
0286      * @param color The text will get this background color
0287      */
0288     void setTextForegroundColor(const QColor &color);
0289 
0290     /**
0291      * Sets the background color of the current word or selection to @a color.
0292      *
0293      * @param color The text will get this foreground color
0294      */
0295     void setTextBackgroundColor(const QColor &color);
0296 
0297     /**
0298      * Inserts a horizontal rule below the current block.
0299      */
0300     void insertHorizontalRule();
0301 
0302     /**
0303      * This will switch the editor to plain text mode.
0304      * All rich text formatting will be destroyed.
0305      */
0306     void switchToPlainText();
0307 
0308     /**
0309      * This will clean some of the bad html produced by the underlying QTextEdit
0310      * It walks over all lines and cleans up a bit. Should be improved to produce
0311      * our own Html.
0312      */
0313     QString toCleanHtml() const;
0314 
0315     /**
0316      * Toggles the superscript formatting of the current word or selection at the current
0317      * cursor position.
0318      *
0319      * @param superscript If true, the text will be set to superscript
0320      */
0321     void setTextSuperScript(bool superscript);
0322 
0323     /**
0324      * Toggles the subscript formatting of the current word or selection at the current
0325      * cursor position.
0326      *
0327      * @param subscript If true, the text will be set to subscript
0328      */
0329     void setTextSubScript(bool subscript);
0330 
0331     /**
0332      * Sets the heading level of a current block or selection
0333      *
0334      * @param level Heading level (value should be between 0 and 6)
0335      * (0 is "normal text", 1 is the largest heading, 6 is the smallest one)
0336      *
0337      * @since 5.70
0338      */
0339     void setHeadingLevel(int level);
0340 
0341     /**
0342      * @since 4.10
0343      * Because of binary compatibility constraints, insertPlainText
0344      * is not virtual. Therefore it must dynamically detect and call this slot.
0345      */
0346     void insertPlainTextImplementation();
0347 
0348 Q_SIGNALS:
0349 
0350     /**
0351      * Emitted whenever the text mode is changed.
0352      *
0353      * @param mode The new text mode
0354      */
0355     void textModeChanged(KRichTextEdit::Mode mode);
0356 
0357 protected:
0358     /**
0359      * Reimplemented.
0360      * Catches key press events. Used to handle some key presses on lists.
0361      */
0362     void keyPressEvent(QKeyEvent *event) override;
0363 
0364 protected:
0365     KTEXTWIDGETS_NO_EXPORT KRichTextEdit(KRichTextEditPrivate &dd, const QString &text, QWidget *parent);
0366     KTEXTWIDGETS_NO_EXPORT KRichTextEdit(KRichTextEditPrivate &dd, QWidget *parent);
0367 
0368 private:
0369     //@cond PRIVATE
0370     Q_DECLARE_PRIVATE(KRichTextEdit)
0371     //@endcond
0372 };
0373 
0374 #endif