Warning, file /office/calligra/libs/text/OdfTextTrackStyles.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2006, 2009 Thomas Zander <zander@kde.org>
0003  * Copyright (C) 2012-2014 C. Boemann <cbo@boemann.dk>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "OdfTextTrackStyles.h"
0022 #include "KoTextDocument.h"
0023 #include "KoParagraphStyle.h"
0024 #include "KoCharacterStyle.h"
0025 
0026 #include <QTextDocument>
0027 #include "TextDebug.h"
0028 
0029 
0030 QHash<QObject *, OdfTextTrackStyles *> OdfTextTrackStyles::instances;
0031 
0032 OdfTextTrackStyles *OdfTextTrackStyles::instance(KoStyleManager *manager)
0033 {
0034     if (! instances.contains(manager)) {
0035         instances[manager] = new OdfTextTrackStyles(manager);
0036         connect(manager,SIGNAL(destroyed(QObject*)),instances[manager], SLOT(styleManagerDied(QObject*)));
0037     }
0038 
0039     return instances[manager];
0040 }
0041 
0042 void OdfTextTrackStyles::registerDocument(QTextDocument *qDoc)
0043 {
0044     if (! m_documents.contains(qDoc)) {
0045         m_documents.append(qDoc);
0046         connect(qDoc,SIGNAL(destroyed(QObject*)), this, SLOT(documentDied(QObject*)));
0047     }
0048 }
0049 
0050 void OdfTextTrackStyles::unregisterDocument(QTextDocument *qDoc)
0051 {
0052     if (m_documents.contains(qDoc)) {
0053         m_documents.removeOne(qDoc);
0054     }
0055 }
0056 
0057 OdfTextTrackStyles::OdfTextTrackStyles(KoStyleManager *manager)
0058         : QObject(manager)
0059         , m_styleManager(manager)
0060         , m_changeCommand(0)
0061 {
0062     connect(manager, SIGNAL(editHasBegun()), this, SLOT(beginEdit()));
0063     connect(manager, SIGNAL(editHasEnded()), this, SLOT(endEdit()));
0064     connect(manager, SIGNAL(styleHasChanged(int,const KoCharacterStyle*,const KoCharacterStyle*)), this, SLOT(recordStyleChange(int,const KoCharacterStyle*,const KoCharacterStyle*)));
0065     connect(manager, SIGNAL(styleHasChanged(int,const KoParagraphStyle*,const KoParagraphStyle*)), this, SLOT(recordStyleChange(int,const KoParagraphStyle*,const KoParagraphStyle*)));
0066 }
0067 
0068 OdfTextTrackStyles::~OdfTextTrackStyles()
0069 {
0070 }
0071 
0072 void OdfTextTrackStyles::beginEdit()
0073 {
0074     Q_ASSERT(m_changeCommand == 0);
0075     m_changeCommand = new ChangeStylesMacroCommand(m_documents, m_styleManager.data());
0076 }
0077 
0078 void OdfTextTrackStyles::endEdit()
0079 {
0080     if (m_documents.length() > 0) {
0081         KUndo2Stack *undoStack= KoTextDocument(m_documents.first()).undoStack();
0082         if (undoStack) {
0083             undoStack->push(m_changeCommand);
0084         }
0085     } else
0086         delete m_changeCommand;
0087 
0088     m_changeCommand = 0;
0089 }
0090 
0091 void OdfTextTrackStyles::recordStyleChange(int id, const KoParagraphStyle *origStyle, const KoParagraphStyle *newStyle)
0092 {
0093     m_changeCommand->changedStyle(id);
0094 
0095     if (origStyle != newStyle) {
0096         m_changeCommand->origStyle(origStyle->clone());
0097         m_changeCommand->changedStyle(newStyle->clone());
0098     }
0099 }
0100 
0101 void OdfTextTrackStyles::recordStyleChange(int id, const KoCharacterStyle *origStyle, const KoCharacterStyle *newStyle)
0102 {
0103    m_changeCommand->changedStyle(id);
0104 
0105     if (origStyle != newStyle) {
0106         m_changeCommand->origStyle(origStyle->clone());
0107         m_changeCommand->changedStyle(newStyle->clone());
0108     }
0109 }
0110 
0111 void OdfTextTrackStyles::styleManagerDied(QObject *manager)
0112 {
0113     OdfTextTrackStyles::instances.remove(manager);
0114 }
0115 
0116 void OdfTextTrackStyles::documentDied(QObject *document)
0117 {
0118     unregisterDocument(static_cast<QTextDocument *>(document));
0119 }