1 /*
2  This file is part of the KDE project
3  * Copyright (C) 2009 Ganesh Paramasivam <ganesh@crystalfab.com>
4  * Copyright (C) 2012 C. Boemann <cbo@boemann.dk>
5  * Copyright (C) 2014-2015 Denis Kuplyakov <dener.kup@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.*/
21 
22 #ifndef DELETECOMMAND_H
23 #define DELETECOMMAND_H
24 
25 #include "KoTextCommandBase.h"
26 
27 #include <QTextCharFormat>
28 #include <QHash>
29 #include <QSet>
30 #include <QWeakPointer>
31 
32 class QTextDocument;
33 
34 class KoShapeController;
35 class KoInlineObject;
36 class KoTextRange;
37 class KoSection;
38 
39 class DeleteVisitor;
40 
41 class DeleteCommand : public KoTextCommandBase
42 {
43 public:
44     enum DeleteMode {
45         PreviousChar,
46         NextChar
47     };
48 
49     DeleteCommand(DeleteMode mode, QTextDocument *document, KoShapeController *shapeController, KUndo2Command* parent = 0);
50     ~DeleteCommand() override;
51 
52     void undo() override;
53     void redo() override;
54 
55     int id() const override;
56     bool mergeWith(const KUndo2Command *command) override;
57 
58 private:
59     friend class DeleteVisitor;
60 
61     struct SectionDeleteInfo {
SectionDeleteInfoSectionDeleteInfo62         SectionDeleteInfo(KoSection *_section, int _childIdx)
63             : section(_section)
64             , childIdx(_childIdx)
65         {
66         }
67 
68         bool operator<(const SectionDeleteInfo &other) const;
69 
70         KoSection *section; ///< Section to remove
71         int childIdx; ///< Position of section in parent's children() list
72     };
73 
74     QWeakPointer<QTextDocument> m_document;
75     KoShapeController *m_shapeController;
76 
77     QSet<KoInlineObject *> m_invalidInlineObjects;
78     QList<QTextCursor> m_cursorsToWholeDeleteBlocks;
79     QHash<int, KoTextRange *> m_rangesToRemove;
80     QList<SectionDeleteInfo> m_sectionsToRemove;
81 
82     bool m_first;
83     DeleteMode m_mode;
84     int m_position;
85     int m_length;
86     QTextCharFormat m_format;
87     bool m_mergePossible;
88 
89     void doDelete();
90     void deleteInlineObject(KoInlineObject *object);
91     bool checkMerge(const KUndo2Command *command);
92     void updateListChanges();
93     void finalizeSectionHandling(QTextCursor *caret, DeleteVisitor &visitor);
94     void deleteSectionsFromModel();
95     void insertSectionsToModel();
96 };
97 
98 #endif // DELETECOMMAND_H
99