| 1 | /* |
| 2 | Nested list helper |
| 3 | SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifndef NESTEDLISTHELPER_H |
| 9 | #define NESTEDLISTHELPER_H |
| 10 | |
| 11 | class QTextEdit; |
| 12 | |
| 13 | class QKeyEvent; |
| 14 | class QDropEvent; |
| 15 | class QTextCursor; |
| 16 | class QTextList; |
| 17 | class QTextBlock; |
| 18 | |
| 19 | /*! |
| 20 | * \brief Helper class for automatic handling of nested lists in a text edit |
| 21 | * |
| 22 | * \since 4.1 |
| 23 | * \internal |
| 24 | */ |
| 25 | class NestedListHelper |
| 26 | { |
| 27 | public: |
| 28 | /*! |
| 29 | * Create a helper |
| 30 | * |
| 31 | * \a The text edit object to handle lists in. |
| 32 | */ |
| 33 | explicit NestedListHelper(QTextEdit *te); |
| 34 | |
| 35 | /*! |
| 36 | * Destructor |
| 37 | */ |
| 38 | ~NestedListHelper(); |
| 39 | |
| 40 | /*! |
| 41 | * |
| 42 | * Handles a key press before it is processed by the text edit widget. |
| 43 | * |
| 44 | * This includes: |
| 45 | * 1. Backspace at the beginning of a line decreases nesting level |
| 46 | * 2. Return at the empty list element decreases nesting level |
| 47 | * 3. Tab at the beginning of a line OR with a multi-line selection |
| 48 | * increases nesting level |
| 49 | * |
| 50 | * \a event The event to be handled |
| 51 | * |
| 52 | * Returns Whether the event was completely handled by this method. |
| 53 | */ |
| 54 | bool handleKeyPressEvent(QKeyEvent *event); |
| 55 | |
| 56 | bool handleAfterDropEvent(QDropEvent *event); |
| 57 | |
| 58 | /*! |
| 59 | * Changes the indent (nesting level) on a current list item or selection |
| 60 | * by the value \a delta (typically, +1 or -1) |
| 61 | */ |
| 62 | void changeIndent(int delta); |
| 63 | |
| 64 | /*! |
| 65 | * Changes the style of the current list or creates a new list with |
| 66 | * the specified style. |
| 67 | * |
| 68 | * \a styleIndex The QTextListStyle of the list. |
| 69 | */ |
| 70 | void handleOnBulletType(int styleIndex); |
| 71 | |
| 72 | /*! |
| 73 | * \brief Check whether the current item in the list may be indented. |
| 74 | * |
| 75 | * An list item must have an item above it on the same or greater level |
| 76 | * if it can be indented. |
| 77 | * |
| 78 | * Also, a block which is currently part of a list can be indented. |
| 79 | * |
| 80 | * \sa canDedent |
| 81 | * |
| 82 | * Returns Whether the item can be indented. |
| 83 | */ |
| 84 | bool canIndent() const; |
| 85 | |
| 86 | /*! |
| 87 | * \brief Check whether the current item in the list may be dedented. |
| 88 | * |
| 89 | * An item may be dedented if it is part of a list. |
| 90 | * The next item must be at the same or lesser level. |
| 91 | * |
| 92 | * \sa canIndent |
| 93 | * |
| 94 | * Returns Whether the item can be dedented. |
| 95 | */ |
| 96 | bool canDedent() const; |
| 97 | |
| 98 | private: |
| 99 | QTextCursor topOfSelection() const; |
| 100 | QTextCursor bottomOfSelection() const; |
| 101 | void processList(QTextList *list); |
| 102 | void reformatList(QTextBlock block); |
| 103 | void reformatList(); |
| 104 | |
| 105 | QTextEdit *const textEdit; |
| 106 | }; |
| 107 | |
| 108 | #endif |
| 109 | |