nestedlisthelper_p.h 3.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * Nested list helper
 *
 * Copyright 2008  Stephen Kelly <steveire@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#ifndef NESTEDLISTHELPER_H
#define NESTEDLISTHELPER_H

//@cond PRIVATE
Laurent Montel's avatar
Laurent Montel committed
26
#include <QObject>
27 28 29 30 31 32 33
class QTextEdit;

class QKeyEvent;
class QDropEvent;
class QTextCursor;
class QTextList;
class QTextBlock;
Laurent Montel's avatar
Laurent Montel committed
34
namespace KPIMTextEdit {
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/**
 *
 * @short Helper class for automatic handling of nested lists in a text edit
 *
 *
 * @author Stephen Kelly
 * @since 4.1
 * @internal
 */
class NestedListHelper
{
public:

    /**
     * Create a helper
     *
     * @param te The text edit object to handle lists in.
     */
Laurent Montel's avatar
Laurent Montel committed
53
    explicit NestedListHelper(QTextEdit *te);
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    /**
     *
     * Handles a key press before it is processed by the text edit widget.
     *
     * Currently this causes a backspace at the beginning of a line or with a
     * multi-line selection to decrease the nesting level of the list.
     *
     * @param event The event to be handled
     * @return Whether the event was completely handled by this method.
     */
    bool handleBeforeKeyPressEvent(QKeyEvent *event);

    /**
     *
     * Handles a key press after it is processed by the text edit widget.
     *
     * Currently this causes a Return at the end of the last list item, or
     * a Backspace after the last list item to recalculate the spacing
     * between the list items.
     *
     * @param event The event to be handled
     * @return Whether the event was completely handled by this method.
     */
    bool handleAfterKeyPressEvent(QKeyEvent *event);

    bool handleAfterDropEvent(QDropEvent *event);

    /**
     * Increases the indent (nesting level) on the current list item or selection.
     */
    void handleOnIndentMore();

    /**
     * Decreases the indent (nesting level) on the current list item or selection.
     */
    void handleOnIndentLess();

    /**
     * Changes the style of the current list or creates a new list with
     * the specified style.
     *
     * @param styleIndex The QTextListStyle of the list.
     */
    void handleOnBulletType(int styleIndex);

    /**
     * @brief Check whether the current item in the list may be indented.
     *
     * An list item must have an item above it on the same or greater level
     * if it can be indented.
     *
     * Also, a block which is currently part of a list can be indented.
     *
     * @sa canDedent
     *
     * @return Whether the item can be indented.
     */
Laurent Montel's avatar
Laurent Montel committed
112
    Q_REQUIRED_RESULT bool canIndent() const;
113 114 115 116 117 118 119 120 121 122

    /**
     * \brief Check whether the current item in the list may be dedented.
     *
     * An item may be dedented if it is part of a list. Otherwise it can't be.
     *
     * @sa canIndent
     *
     * @return Whether the item can be dedented.
     */
Laurent Montel's avatar
Laurent Montel committed
123
    Q_REQUIRED_RESULT bool canDedent() const;
124 125 126 127 128 129 130 131

private:
    QTextCursor topOfSelection();
    QTextCursor bottomOfSelection();
    void processList(QTextList *list);
    void reformatList(QTextBlock block);
    void reformatList();

Laurent Montel's avatar
Laurent Montel committed
132
    QTextEdit *textEdit = nullptr;
133 134 135 136 137 138 139 140 141

    int listBottomMargin;
    int listTopMargin;
    int listNoMargin;
};
}
//@endcond

#endif