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

0001 /*
0002     Nested list helper
0003     SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #ifndef NESTEDLISTHELPER_H
0009 #define NESTEDLISTHELPER_H
0010 
0011 //@cond PRIVATE
0012 
0013 class QTextEdit;
0014 
0015 class QKeyEvent;
0016 class QDropEvent;
0017 class QTextCursor;
0018 class QTextList;
0019 class QTextBlock;
0020 
0021 /**
0022  *
0023  * @short Helper class for automatic handling of nested lists in a text edit
0024  *
0025  *
0026  * @author Stephen Kelly
0027  * @since 4.1
0028  * @internal
0029  */
0030 class NestedListHelper
0031 {
0032 public:
0033     /**
0034      * Create a helper
0035      *
0036      * @param te The text edit object to handle lists in.
0037      */
0038     explicit NestedListHelper(QTextEdit *te);
0039 
0040     /**
0041      * Destructor
0042      */
0043     ~NestedListHelper();
0044 
0045     /**
0046      *
0047      * Handles a key press before it is processed by the text edit widget.
0048      *
0049      * This includes:
0050      *   1. Backspace at the beginning of a line decreases nesting level
0051      *   2. Return at the empty list element decreases nesting level
0052      *   3. Tab at the beginning of a line OR with a multi-line selection
0053      * increases nesting level
0054      *
0055      * @param event The event to be handled
0056      * @return Whether the event was completely handled by this method.
0057      */
0058     bool handleKeyPressEvent(QKeyEvent *event);
0059 
0060     bool handleAfterDropEvent(QDropEvent *event);
0061 
0062     /**
0063      * Changes the indent (nesting level) on a current list item or selection
0064      * by the value @p delta (typically, +1 or -1)
0065      */
0066     void changeIndent(int delta);
0067 
0068     /**
0069      * Changes the style of the current list or creates a new list with
0070      * the specified style.
0071      *
0072      * @param styleIndex The QTextListStyle of the list.
0073      */
0074     void handleOnBulletType(int styleIndex);
0075 
0076     /**
0077      * @brief Check whether the current item in the list may be indented.
0078      *
0079      * An list item must have an item above it on the same or greater level
0080      * if it can be indented.
0081      *
0082      * Also, a block which is currently part of a list can be indented.
0083      *
0084      * @sa canDedent
0085      *
0086      * @return Whether the item can be indented.
0087      */
0088     bool canIndent() const;
0089 
0090     /**
0091      * \brief Check whether the current item in the list may be dedented.
0092      *
0093      * An item may be dedented if it is part of a list.
0094      * The next item must be at the same or lesser level.
0095      *
0096      * @sa canIndent
0097      *
0098      * @return Whether the item can be dedented.
0099      */
0100     bool canDedent() const;
0101 
0102 private:
0103     QTextCursor topOfSelection() const;
0104     QTextCursor bottomOfSelection() const;
0105     void processList(QTextList *list);
0106     void reformatList(QTextBlock block);
0107     void reformatList();
0108 
0109     QTextEdit *const textEdit;
0110 };
0111 
0112 //@endcond
0113 
0114 #endif