File indexing completed on 2024-05-19 05:21:42

0001 /**
0002  * Nested list helper
0003  *
0004  * SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 //@cond PRIVATE
0012 #include <QObject>
0013 class QTextEdit;
0014 
0015 class QKeyEvent;
0016 class QDropEvent;
0017 class QTextCursor;
0018 class QTextList;
0019 class QTextBlock;
0020 namespace KPIMTextEdit
0021 {
0022 /**
0023  *
0024  * @short Helper class for automatic handling of nested lists in a text edit
0025  *
0026  *
0027  * @author Stephen Kelly
0028  * @since 4.1
0029  * @internal
0030  */
0031 class NestedListHelper
0032 {
0033 public:
0034     /**
0035      * Create a helper
0036      *
0037      * @param te The text edit object to handle lists in.
0038      */
0039     explicit NestedListHelper(QTextEdit *te);
0040 
0041     /**
0042      *
0043      * Handles a key press before it is processed by the text edit widget.
0044      *
0045      * Currently this causes a backspace at the beginning of a line or with a
0046      * multi-line selection to decrease the nesting level of the list.
0047      *
0048      * @param event The event to be handled
0049      * @return Whether the event was completely handled by this method.
0050      */
0051     [[nodiscard]] bool handleBeforeKeyPressEvent(QKeyEvent *event);
0052 
0053     /**
0054      *
0055      * Handles a key press after it is processed by the text edit widget.
0056      *
0057      * Currently this causes a Return at the end of the last list item, or
0058      * a Backspace after the last list item to recalculate the spacing
0059      * between the list items.
0060      *
0061      * @param event The event to be handled
0062      * @return Whether the event was completely handled by this method.
0063      */
0064     bool handleAfterKeyPressEvent(QKeyEvent *event);
0065 
0066     /**
0067      * Increases the indent (nesting level) on the current list item or selection.
0068      */
0069     void handleOnIndentMore();
0070 
0071     /**
0072      * Decreases the indent (nesting level) on the current list item or selection.
0073      */
0074     void handleOnIndentLess();
0075 
0076     /**
0077      * Changes the style of the current list or creates a new list with
0078      * the specified style.
0079      *
0080      * @param styleIndex The QTextListStyle of the list.
0081      */
0082     void handleOnBulletType(int styleIndex);
0083 
0084     /**
0085      * @brief Check whether the current item in the list may be indented.
0086      *
0087      * An list item must have an item above it on the same or greater level
0088      * if it can be indented.
0089      *
0090      * Also, a block which is currently part of a list can be indented.
0091      *
0092      * @sa canDedent
0093      *
0094      * @return Whether the item can be indented.
0095      */
0096     [[nodiscard]] bool canIndent() const;
0097 
0098     /**
0099      * \brief Check whether the current item in the list may be dedented.
0100      *
0101      * An item may be dedented if it is part of a list. Otherwise it can't be.
0102      *
0103      * @sa canIndent
0104      *
0105      * @return Whether the item can be dedented.
0106      */
0107     [[nodiscard]] bool canDedent() const;
0108 
0109 private:
0110     [[nodiscard]] QTextCursor topOfSelection();
0111     [[nodiscard]] QTextCursor bottomOfSelection();
0112     void processList(QTextList *list);
0113     void reformatList(QTextBlock block);
0114     void reformatList();
0115 
0116     QTextEdit *const textEdit;
0117 
0118     int listBottomMargin;
0119     int listTopMargin;
0120     int listNoMargin;
0121 };
0122 }
0123 //@endcond