1 /*
2 * This file is part of the KDE project
3 * Copyright (C) 2014-2015 Denis Kuplyakov <dener.kup@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.*/
19
20 #include "NewSectionCommand.h"
21 #include <KoSection.h>
22 #include <KoSectionEnd.h>
23 #include <KoTextDocument.h>
24 #include <KoParagraphStyle.h>
25 #include <KoTextEditor.h>
26 #include <KoSectionUtils.h>
27 #include <KoSectionModel.h>
28
29 #include <klocalizedstring.h>
30 #include <kundo2command.h>
31
NewSectionCommand(QTextDocument * document)32 NewSectionCommand::NewSectionCommand(QTextDocument *document)
33 : KUndo2Command ()
34 , m_first(true)
35 , m_document(document)
36 {
37 setText(kundo2_i18n("New Section"));
38 }
39
~NewSectionCommand()40 NewSectionCommand::~NewSectionCommand()
41 {
42 }
43
undo()44 void NewSectionCommand::undo()
45 {
46 KUndo2Command::undo();
47 //FIXME: if it will go to KoTextCommandBase, place UndoRedoFinalizer here
48
49 // All formatting changes will be undone automatically.
50 // Lets handle Model Level (see KoSectionModel).
51 KoTextDocument(m_document).sectionModel()->deleteFromModel(m_section);
52 }
53
redo()54 void NewSectionCommand::redo()
55 {
56 KoTextDocument koDocument(m_document);
57 KoSectionModel *sectionModel = koDocument.sectionModel();
58
59 if (!m_first) {
60 KUndo2Command::redo();
61 //FIXME: if it will go to KoTextCommandBase, place UndoRedoFinalizer here
62
63 // All formatting changes will be redone automatically.
64 // Lets handle Model Level (see KoSectionModel).
65 sectionModel->insertToModel(m_section, m_childIdx);
66 } else {
67 m_first = false;
68
69 KoTextEditor *editor = koDocument.textEditor();
70 editor->newLine();
71
72 m_section = sectionModel->createSection(
73 editor->constCursor(),
74 sectionModel->sectionAtPosition(editor->constCursor().position())
75 );
76 m_childIdx = sectionModel->findRowOfChild(m_section);
77
78 KoSectionEnd *sectionEnd = sectionModel->createSectionEnd(m_section);
79 QTextBlockFormat fmt = editor->blockFormat();
80
81 QList<KoSection *> sectionStartings = KoSectionUtils::sectionStartings(fmt);
82 QList<KoSectionEnd *> sectionEndings = KoSectionUtils::sectionEndings(fmt);
83
84 sectionStartings.append(m_section);
85 sectionEndings.prepend(sectionEnd);
86
87 KoSectionUtils::setSectionStartings(fmt, sectionStartings);
88 KoSectionUtils::setSectionEndings(fmt, sectionEndings);
89
90 editor->setBlockFormat(fmt);
91 }
92 }
93