1 /*
2  This file is part of the KDE project
3  * Copyright (C) 2012 C. Boemann <cbo@boemann.dk>
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 "InsertNoteCommand.h"
21 
22 #include <klocalizedstring.h>
23 
24 #include <KoTextEditor.h>
25 #include <KoTextDocument.h>
26 #include <KoInlineTextObjectManager.h>
27 #include <KoInlineNote.h>
28 
29 
InsertNoteCommand(KoInlineNote::Type type,QTextDocument * document)30 InsertNoteCommand::InsertNoteCommand(KoInlineNote::Type type, QTextDocument *document)
31     : KUndo2Command ()
32     , m_document(document)
33     , m_first(true)
34 {
35     if (type == KoInlineNote::Footnote) {
36         setText(kundo2_i18n("Insert Footnote"));
37     } else if (type == KoInlineNote::Endnote) {
38         setText(kundo2_i18n("Insert Endnote"));
39     }
40     m_inlineNote = new KoInlineNote(type);
41 }
42 
~InsertNoteCommand()43 InsertNoteCommand::~InsertNoteCommand()
44 {
45 }
46 
undo()47 void InsertNoteCommand::undo()
48 {
49     KUndo2Command::undo();
50 }
51 
redo()52 void InsertNoteCommand::redo()
53 {
54     if (!m_first) {
55         KUndo2Command::redo();
56         QTextCursor cursor(m_document.data());
57         cursor.setPosition(m_framePosition);
58         m_inlineNote->setTextFrame(cursor.currentFrame());
59         m_inlineNote->setMotherFrame(KoTextDocument(m_document).auxillaryFrame());
60     } else {
61         m_first = false;
62         if (m_document) {
63             KoTextEditor *textEditor = KoTextDocument(m_document).textEditor();
64             if (textEditor) {
65                 textEditor->beginEditBlock();
66                 QTextCursor *caret = textEditor->cursor();
67                 if (textEditor->hasSelection()) {
68                     textEditor->deleteChar(false, this);
69                 }
70                 KoInlineTextObjectManager *manager = KoTextDocument(m_document).inlineTextObjectManager();
71                 manager->insertInlineObject(*caret, m_inlineNote);
72                 m_inlineNote->setMotherFrame(KoTextDocument(m_document).auxillaryFrame());
73                 m_framePosition = m_inlineNote->textFrame()->lastPosition();
74                 textEditor->setPosition(m_framePosition);
75 
76                 textEditor->endEditBlock();
77             }
78         }
79     }
80 }
81