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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0004     SPDX-FileCopyrightText: 2008 Thomas McGuire <thomas.mcguire@gmx.net>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef KRICHTEXTWIDGET_H
0010 #define KRICHTEXTWIDGET_H
0011 
0012 #include "krichtextedit.h"
0013 
0014 #include "ktextwidgets_export.h"
0015 
0016 class QAction;
0017 class KRichTextWidgetPrivate;
0018 
0019 /**
0020  * @class KRichTextWidget krichtextwidget.h <KRichTextWidget>
0021  *
0022  * @brief A KRichTextEdit with common actions
0023  *
0024  * This class implements common actions which are often used with KRichTextEdit.
0025  * All you need to do is to call createActions(), and the actions will be
0026  * added to your KXMLGUIWindow. Remember to also add the chosen actions to
0027  * your application ui.rc file.
0028  *
0029  * See the KRichTextWidget::RichTextSupportValues enum for an overview of
0030  * supported actions.
0031  *
0032  * @author Stephen Kelly <steveire@gmail.com>
0033  * @author Thomas McGuire <thomas.mcguire@gmx.net>
0034  *
0035  * \image html krichtextedit.png "KRichTextWidget Widget"
0036  *
0037  * @since 4.1
0038  */
0039 class KTEXTWIDGETS_EXPORT KRichTextWidget : public KRichTextEdit
0040 {
0041     Q_OBJECT
0042     Q_PROPERTY(RichTextSupport richTextSupport READ richTextSupport WRITE setRichTextSupport)
0043 public:
0044     /**
0045      * These flags describe what actions will be created by createActions() after
0046      * passing a combination of these flags to setRichTextSupport().
0047      * @see RichTextSupport
0048      */
0049     enum RichTextSupportValues {
0050         /**
0051          * No rich text support at all, no actions will be created. Do not use
0052          * in combination with other flags.
0053          */
0054         DisableRichText = 0x00,
0055 
0056         /**
0057          * Action to format the selected text as bold. If no text is selected,
0058          * the word under the cursor is formatted bold.
0059          * This is a KToggleAction. The status is automatically updated when
0060          * the text cursor is moved.
0061          */
0062         SupportBold = 0x01,
0063 
0064         /**
0065          * Action to format the selected text as italic. If no text is selected,
0066          * the word under the cursor is formatted italic.
0067          * This is a KToggleAction. The status is automatically updated when
0068          * the text cursor is moved.
0069          */
0070         SupportItalic = 0x02,
0071 
0072         /**
0073          * Action to underline the selected text. If no text is selected,
0074          * the word under the cursor is underlined.
0075          * This is a KToggleAction. The status is automatically updated when
0076          * the text cursor is moved.
0077          */
0078         SupportUnderline = 0x04,
0079 
0080         /**
0081          * Action to strike out the selected text. If no text is selected,
0082          * the word under the cursor is struck out.
0083          * This is a KToggleAction. The status is automatically updated when
0084          * the text cursor is moved.
0085          */
0086         SupportStrikeOut = 0x08,
0087 
0088         /**
0089          * Action to change the font family of the currently selected text. If
0090          * no text is selected, the font family of the word under the cursor is
0091          * changed.
0092          * Displayed as a combobox when inserted into the toolbar.
0093          * This is a KFontAction. The status is automatically updated when
0094          * the text cursor is moved.
0095          */
0096         SupportFontFamily = 0x10,
0097 
0098         /**
0099          * Action to change the font size of the currently selected text. If no
0100          * text is selected, the font size of the word under the cursor is changed.
0101          * Displayed as a combobox when inserted into the toolbar.
0102          * This is a KFontSizeAction. The status is automatically updated when
0103          * the text cursor is moved.
0104          */
0105         SupportFontSize = 0x20,
0106 
0107         /**
0108          * Action to change the text color of the currently selected text. If no
0109          * text is selected, the text color of the word under the cursor is
0110          * changed.
0111          * Opens a QColorDialog to select the color.
0112          */
0113         SupportTextForegroundColor = 0x40,
0114 
0115         /**
0116          * Action to change the background color of the currently selected text. If no
0117          * text is selected, the background color of the word under the cursor is
0118          * changed.
0119          * Opens a QColorDialog to select the color.
0120          */
0121         SupportTextBackgroundColor = 0x80,
0122 
0123         /**
0124          * A combination of all the flags above.
0125          * Includes all actions that change the format of the text.
0126          */
0127         FullTextFormattingSupport = 0xff,
0128 
0129         /**
0130          * Action to make the current line a list element, change the
0131          * list style or remove list formatting.
0132          * Displayed as a combobox when inserted into a toolbar.
0133          * This is a KSelectAction. The status is automatically updated when
0134          * the text cursor is moved.
0135          */
0136         SupportChangeListStyle = 0x100,
0137 
0138         /**
0139          * Action to increase the current list nesting level. This makes it
0140          * possible to create nested lists.
0141          */
0142         SupportIndentLists = 0x200,
0143 
0144         /**
0145          * Action to decrease the current list nesting level.
0146          */
0147         SupportDedentLists = 0x400,
0148 
0149         /**
0150          * All of the three list actions above.
0151          * Includes all list-related actions.
0152          */
0153         FullListSupport = 0xf00,
0154 
0155         // Not implemented yet.
0156         //         SupportCreateTables = 0x1000,
0157         //         SupportChangeCellMargin = 0x2000,
0158         //         SupportChangeCellPadding = 0x4000,
0159         //         SupportChangeTableBorderWidth = 0x8000,
0160         //         SupportChangeTableBorderColor = 0x10000,
0161         //         SupportChangeTableBorderStyle = 0x20000,
0162         //         SupportChangeCellBackground = 0x40000,
0163         //         SupportCellFillPatterns = 0x80000,
0164         //
0165         //         FullTableSupport = 0xff000,
0166 
0167         /**
0168          * Actions to align the current paragraph left, righ, center or justify.
0169          * These actions are KToogleActions. The status is automatically updated when
0170          * the text cursor is moved.
0171          */
0172         SupportAlignment = 0x100000,
0173 
0174         // Not yet implemented SupportImages = 0x200000,
0175 
0176         /**
0177          * Action to insert a horizontal line.
0178          */
0179         SupportRuleLine = 0x400000,
0180 
0181         /**
0182          * Action to convert the current text to a hyperlink. If no text is selected,
0183          * the word under the cursor is converted.
0184          * This action opens a dialog where the user can enter the link target.
0185          */
0186         SupportHyperlinks = 0x800000,
0187 
0188         /**
0189          * Action to make the mouse cursor a format painter. The user can select
0190          * text with that painter. The selected text gets the same format as the
0191          * text that was previously selected.
0192          */
0193         SupportFormatPainting = 0x1000000,
0194 
0195         /**
0196          * Action to change the text of the whole text edit to plain text.
0197          * All rich text formatting will get lost.
0198          */
0199         SupportToPlainText = 0x2000000,
0200 
0201         /**
0202          * Actions to format text as superscript or subscript. If no text is selected,
0203          * the word under the cursor is formatted as selected.
0204          * This is a KToggleAction. The status is automatically updated when
0205          * the text cursor is moved.
0206          */
0207         SupportSuperScriptAndSubScript = 0x4000000,
0208 
0209         // SupportChangeParagraphSpacing = 0x200000,
0210 
0211         /**
0212          * Action to change direction of text to Right-To-Left or Left-To-Right.
0213          */
0214         SupportDirection = 0x8000000,
0215 
0216         /**
0217          * Action to make the current line a heading (up to six levels,
0218          * corresponding to HTML h1...h6 tags)
0219          * Displayed as a combobox when inserted into a toolbar.
0220          * This is a KSelectAction. The status is automatically updated when
0221          * the text cursor is moved.
0222          *
0223          * @since 5.70
0224          */
0225         SupportHeading = 0x10000000,
0226 
0227         /**
0228          * Includes all above actions for full rich text support
0229          */
0230         FullSupport = 0xffffffff,
0231     };
0232     /**
0233      * Stores a combination of #RichTextSupportValues values.
0234      */
0235     Q_DECLARE_FLAGS(RichTextSupport, RichTextSupportValues)
0236     Q_FLAG(RichTextSupport)
0237 
0238     /**
0239      * @brief Constructor
0240      * @param parent the parent widget
0241      */
0242     explicit KRichTextWidget(QWidget *parent);
0243 
0244     /**
0245      * Constructs a KRichTextWidget object
0246      *
0247      * @param text The initial text of the text edit, which is interpreted as
0248      *             HTML.
0249      * @param parent The parent widget
0250      */
0251     explicit KRichTextWidget(const QString &text, QWidget *parent = nullptr);
0252 
0253     /**
0254      * @brief Destructor
0255      */
0256     ~KRichTextWidget() override;
0257 
0258     /**
0259      * @brief Creates the actions and adds them to the given action collection.
0260      *
0261      * Call this before calling setupGUI() in your application, but after
0262      * calling setRichTextSupport().
0263      *
0264      * The XML file of your KXmlGuiWindow needs to have the action names in
0265      * them, so that the actions actually appear in the menu and in the toolbars.
0266      *
0267      * Below is a list of actions that are created,depending on the supported rich text
0268      * subset set by setRichTextSupport(). The list contains action names.
0269      * Those names need to be the same in your XML file.
0270      *
0271      * See the KRichTextWidget::RichTextSupportValues enum documentation for a
0272      * detailed explanation of each action.
0273      *
0274      * <table>
0275      * <tr><td><b>XML Name</b></td><td><b>RichTextSupportValues flag</b></td></tr>
0276      * <tr><td>format_text_foreground_color</td><td>SupportTextForegroundColor</td></tr>
0277      * <tr><td>format_text_background_color</td><td>SupportTextBackgroundColor</td></tr>
0278      * <tr><td>format_font_family</td><td>SupportFontFamily</td></tr>
0279      * <tr><td>format_font_size</td><td>SupportFontSize</td></tr>
0280      * <tr><td>format_text_bold</td><td>SupportBold</td></tr>
0281      * <tr><td>format_text_italic</td><td>SupportItalic</td></tr>
0282      * <tr><td>format_text_underline</td><td>SupportUnderline</td></tr>
0283      * <tr><td>format_text_strikeout</td><td>SupportStrikeOut</td></tr>
0284      * <tr><td>format_align_left</td><td>SupportAlignment</td></tr>
0285      * <tr><td>format_align_center</td><td>SupportAlignment</td></tr>
0286      * <tr><td>format_align_right</td><td>SupportAlignment</td></tr>
0287      * <tr><td>format_align_justify</td><td>SupportAlignment</td></tr>
0288      * <tr><td>direction_ltr</td><td>SupportDirection</td></tr>
0289      * <tr><td>direction_rtl</td><td>SupportDirection</td></tr>
0290      * <tr><td>format_list_style</td><td>SupportChangeListStyle</td></tr>
0291      * <tr><td>format_list_indent_more</td><td>SupportIndentLists</td></tr>
0292      * <tr><td>format_list_indent_less</td><td>SupportDedentLists</td></tr>
0293      * <tr><td>insert_horizontal_rule</td><td>SupportRuleLine</td></tr>
0294      * <tr><td>manage_link</td><td>SupportHyperlinks</td></tr>
0295      * <tr><td>format_painter</td><td>SupportFormatPainting</td></tr>
0296      * <tr><td>action_to_plain_text</td><td>SupportToPlainText</td></tr>
0297      * <tr><td>format_text_subscript & format_text_superscript</td><td>SupportSuperScriptAndSubScript</td></tr>
0298      * <tr><td>format_heading_level</td><td>SupportHeading</td></tr>
0299      * </table>
0300      *
0301      * @since 5.0
0302      */
0303     virtual QList<QAction *> createActions();
0304 
0305     /**
0306      * @brief Sets the supported rich text subset available.
0307      *
0308      * The default is KRichTextWidget::FullSupport and will be set in the
0309      * constructor.
0310      *
0311      * You need to call createActions() afterwards.
0312      *
0313      * @param support The supported subset.
0314      */
0315     void setRichTextSupport(const KRichTextWidget::RichTextSupport &support);
0316 
0317     /**
0318      * @brief Returns the supported rich text subset available.
0319      * @return The supported subset.
0320      */
0321     RichTextSupport richTextSupport() const;
0322 
0323     /**
0324      * Tells KRichTextWidget to update the state of the actions created by
0325      * createActions().
0326      * This is normally automatically done, but there might be a few cases where
0327      * you'll need to manually call this function.
0328      *
0329      * Call this function only after calling createActions().
0330      */
0331     void updateActionStates();
0332 
0333 public Q_SLOTS:
0334 
0335     /**
0336      * Disables or enables all of the actions created by
0337      * createActions().
0338      * This may be useful in cases where rich text mode may be set on or off.
0339      *
0340      * @param enabled Whether to enable or disable the actions.
0341      */
0342     void setActionsEnabled(bool enabled);
0343 
0344 protected:
0345     /**
0346      * Reimplemented.
0347      * Catches mouse release events. Used to know when a selection has been completed.
0348      */
0349     void mouseReleaseEvent(QMouseEvent *event) override;
0350 
0351 private:
0352     //@cond PRIVATE
0353     Q_DECLARE_PRIVATE(KRichTextWidget)
0354     //@endcond
0355 };
0356 
0357 Q_DECLARE_OPERATORS_FOR_FLAGS(KRichTextWidget::RichTextSupport)
0358 
0359 #endif