File: MsooXmlDiagramReader.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (240 lines) | stat: -rw-r--r-- 9,703 bytes parent folder | download | duplicates (3)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * This file is part of Office 2007 Filters for Calligra
 *
 * Copyright (C) 2010 Sebastian Sauer <sebsauer@kdab.com>
 * Copyright (c) 2010 Carlos Licea <carlos@kdab.com>
 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: Suresh Chande suresh.chande@nokia.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * 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 St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "MsooXmlDiagramReader.h"

#define MSOOXML_CURRENT_NS "dgm"
#define MSOOXML_CURRENT_CLASS MsooXmlDiagramReader
#define BIND_READ_CLASS MSOOXML_CURRENT_CLASS

#include <MsooXmlReader_p.h>
#include <MsooXmlUtils.h>
#include <KoXmlWriter.h>
#include <KoGenStyles.h>
#include <QExplicitlySharedDataPointer>
#include <typeinfo>

#include "MsooXmlDiagramReader_p.h"

using namespace MSOOXML;

MsooXmlDiagramReaderContext::MsooXmlDiagramReaderContext(KoGenStyles* styles)
    : MSOOXML::MsooXmlReaderContext()
    , m_styles(styles)
    , m_context(new Diagram::Context)
{
}

MsooXmlDiagramReaderContext::~MsooXmlDiagramReaderContext()
{
    delete m_context;
}

int MsooXmlDiagramReaderContext::shapeListSize() const
{
    return m_context->m_shapeList.size();
}

void MsooXmlDiagramReaderContext::saveIndex(KoXmlWriter* xmlWriter, const QRect &rect)
{
    // The root layout node always inherits the canvas dimensions by default.
    m_context->m_rootLayout->m_values["l"] = rect.x();
    m_context->m_rootLayout->m_values["t"] = rect.y();
    m_context->m_rootLayout->m_values["w"] = rect.width();
    m_context->m_rootLayout->m_values["h"] = rect.height();
	kDebug() << "drawingRect" << rect;

    // Do the (re-)layout.
    m_context->m_rootLayout->layoutAtom(m_context);    
    // Write the content.
    m_context->m_rootLayout->writeAtom(m_context, xmlWriter, m_styles);  
}

MsooXmlDiagramReader::MsooXmlDiagramReader(KoOdfWriters *writers)
    : MSOOXML::MsooXmlCommonReader(writers)
    , m_type(InvalidType)
{
}

MsooXmlDiagramReader::~MsooXmlDiagramReader()
{
}

KoFilter::ConversionStatus MsooXmlDiagramReader::read(MSOOXML::MsooXmlReaderContext* context)
{
    m_context = dynamic_cast<MsooXmlDiagramReaderContext*>(context);
    Q_ASSERT(m_context);

    readNext();
    if (!isStartDocument()) {
        return KoFilter::WrongFormat;
    }

    readNext();
    if (qualifiedName() == QLatin1String("dgm:dataModel")) {
        m_type = DataModelType;
        
        Diagram::PointListNode rootList;
        while (!atEnd()) {
            QXmlStreamReader::TokenType tokenType = readNext();
            if (tokenType == QXmlStreamReader::Invalid || tokenType == QXmlStreamReader::EndDocument) break;
            if (isStartElement()) {
                if (qualifiedName() == QLatin1String("dgm:ptLst")) { // list of points
                    rootList.readAll(m_context->m_context, this);
                } else if (qualifiedName() == QLatin1String("dgm:cxnLst")) { // list of connections
                    m_context->m_context->m_connections->readAll(m_context->m_context, this);
                }
            }
        }
        
        QMap<QString, Diagram::PointNode*> pointMap;
        foreach(Diagram::AbstractNode* node, rootList.children()) {
            if (Diagram::PointNode* point = dynamic_cast<Diagram::PointNode*>(node))
                if (!point->m_modelId.isEmpty())
                    pointMap[point->m_modelId] = point;
        }

        foreach(Diagram::AbstractNode* node, m_context->m_context->m_connections->children()) {
            if (Diagram::ConnectionNode* connection = dynamic_cast<Diagram::ConnectionNode*>(node)) {
                Q_ASSERT(pointMap.contains(connection->m_srcId));
                Q_ASSERT(pointMap.contains(connection->m_destId));
                Q_ASSERT(connection->m_parTransId.isEmpty() || pointMap.contains(connection->m_parTransId));
                Q_ASSERT(connection->m_sibTransId.isEmpty() || pointMap.contains(connection->m_sibTransId));
                Diagram::PointNode* source = pointMap.value(connection->m_srcId);
                Diagram::PointNode* destination = pointMap.value(connection->m_destId);
                Diagram::PointNode* parent = connection->m_parTransId.isEmpty() ? 0 : pointMap.value(connection->m_parTransId);
                Diagram::PointNode* sibling = connection->m_sibTransId.isEmpty() ? 0 : pointMap.value(connection->m_sibTransId);

                const int srcOrd = connection->m_srcOrd;
                const int destOrd = connection->m_destOrd;
                Q_UNUSED(destOrd); //TODO how to apply that one?

                if (parent) {
                    // add a transition between parent and child
                    Q_ASSERT(parent->m_type == "parTrans");
                    Q_ASSERT(rootList.children().contains(parent));
                    rootList.removeChild(parent);
                    source->insertChild(srcOrd, parent);
                }

                if (sibling) {
                     // add a transition between siblings
                    Q_ASSERT(sibling->m_type == "sibTrans");
                    Q_ASSERT(rootList.children().contains(sibling));
                    rootList.removeChild(sibling);
                    source->insertChild(srcOrd, sibling);
                }

                if (connection->m_type == QLatin1String("parOf")) {
                    // This defines a parent-child relationship in the sense that node X is a parent of node Y.
                    Q_ASSERT(rootList.children().contains(destination));
                    //Q_ASSERT(rootList.children().at(destOrd) == destination);
                    rootList.removeChild(destination);
                    source->insertChild(srcOrd, destination);
                } else if (connection->m_type == QLatin1String("presOf")) {
                    // A presentation type relationship. This type of relationship exists to actually present data.
                    //TODO
                } else if (connection->m_type == QLatin1String("presParOf")) {
                    // A relationship defining a parent of a presentation node.
                    //TODO
                }
            }
        }

        delete m_context->m_context->m_rootPoint;
        foreach(Diagram::AbstractNode* node, rootList.children()) {
            if (Diagram::PointNode* pt = dynamic_cast<Diagram::PointNode*>(node)) {
                if (pt->m_type == QLatin1String("doc")) {
                    m_context->m_context->m_rootPoint = pt;
                    break;
                }
            }
        }
        if (!m_context->m_context->m_rootPoint) {
            kWarning() << "Data-definition doesn't specify a root-node";
            return KoFilter::WrongFormat;
        }
        Q_ASSERT(rootList.children().contains(m_context->m_context->m_rootPoint));
        rootList.removeChild(m_context->m_context->m_rootPoint);
        //Q_ASSERT(rootList.children().isEmpty());
        m_context->m_context->setCurrentNode(m_context->m_context->m_rootPoint);

#if 0
        QFile visGraphFile( "graphDump" );
        visGraphFile.open( QFile::WriteOnly | QFile::Truncate );
        QTextStream visGraph( &visGraphFile );
        visGraph << "digraph { \n";
        m_context->m_context->m_rootPoint->dump( visGraph );
        visGraph << "}\n";
        visGraphFile.close();
#endif
        //m_context->m_context->m_rootPoint->dump(m_context->m_context, 0);
        //Q_ASSERT( false );
    }
    else if (qualifiedName() == QLatin1String("dgm:layoutDef")) {
        m_type = LayoutDefType;

        while (!atEnd()) {
            QXmlStreamReader::TokenType tokenType = readNext();
            if(tokenType == QXmlStreamReader::Invalid || tokenType == QXmlStreamReader::EndDocument) break;
            if (isStartElement()) {
                if (qualifiedName() == QLatin1String("dgm:layoutNode")) {
                    m_context->m_context->m_rootLayout->readAll(m_context->m_context, this);
                }
                //ELSE_TRY_READ_IF(title)
                //ELSE_TRY_READ_IF(sampData)
                //ELSE_TRY_READ_IF(styleData)
            }
        }
        if (!m_context->m_context->m_rootPoint) {
            kWarning() << "Layout-definition doesn't specify a root-point";
            return KoFilter::WrongFormat;
        }

        // build the layout-tree. This solves ForEachAtom, ChooseAtom and other things that need to be executed.
        m_context->m_context->m_rootLayout->build(m_context->m_context);
        // once done we start a second build-process that does additional things once the layout-tree is fully available.
        m_context->m_context->m_rootLayout->finishBuild(m_context->m_context);

        //m_context->m_context->m_rootLayout->dump(m_context->m_context,0);
        //Q_ASSERT( false );
    }
    else if (qualifiedName() == QLatin1String("dgm:styleDef")) {
        m_type = StyleDefType;
        //TODO
    }
    else if (qualifiedName() == QLatin1String("dgm:colorsDef")) {
        m_type = ColorsDefType;
        //TODO
    }
    else {
        return KoFilter::WrongFormat;
    }

    m_context = 0;
    m_type = InvalidType;
    return KoFilter::OK;
}