summaryrefslogtreecommitdiffstats
path: root/kformula/kformula_doc.cc
blob: 5eeb59f4be19d05b7321026dc729a11c41cdd078 (plain)
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
241
242
243
244
245
246
247
/* This file is part of the KDE project
   Copyright (C) 2001 Andrea Rizzi <rizzi@kde.org>
	              Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/


#include "kformula_doc.h"
#include "kformula_view.h"
#include "kformula_factory.h"

#include <tqbitmap.h>
#include <tqcolor.h>
#include <tqdom.h>
#include <tqpainter.h>
#include <tqpopupmenu.h>
#include <tqprinter.h>
#include <tqstring.h>
#include <tqwmatrix.h>
#include <tqfile.h>

#include <config.h>
#include <unistd.h>

#include <tdeaboutdialog.h>
#include <tdeaction.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <KoGlobal.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <kstdaction.h>
#include <kstandarddirs.h>
#include <kurl.h>
#include <KoXmlWriter.h>
#include <KoStoreDevice.h>
#include <tdetempfile.h>
#include <KoMainWindow.h>

#include <kformulacontainer.h>
#include <kformuladocument.h>


KFormulaDoc::KFormulaDoc(TQWidget *parentWidget, const char *widgetName, TQObject* parent, const char* name, bool singleViewMode)
        : KoDocument(parentWidget, widgetName, parent, name, singleViewMode)
{
    setInstance(KFormulaFactory::global(), false);
    //kdDebug(39001) << "General Settings" << endl;

    history = new KoCommandHistory( actionCollection() );
    wrapper = new KFormula::DocumentWrapper( kapp->config(),
                                             actionCollection(),
                                             history );
    document = new KFormula::Document;
    wrapper->document( document );
    formula = document->createFormula();

    document->setEnabled( true );

    // the modify flag
    connect(history, TQT_SIGNAL(commandExecuted()), this, TQT_SLOT(commandExecuted()));
    connect(history, TQT_SIGNAL(documentRestored()), this, TQT_SLOT(documentRestored()));
    dcopObject();
}


KFormulaDoc::~KFormulaDoc()
{
    delete history;
    delete wrapper;
}


bool KFormulaDoc::saveOasis( KoStore* store, KoXmlWriter* manifestWriter )
{
    if ( !store->open( "content.xml" ) )
        return false;

    KoStoreDevice dev( store );
    KoXmlWriter* contentWriter = createOasisXmlWriter( &dev, "math:math" );


    KTempFile contentTmpFile;
    contentTmpFile.setAutoDelete( true );
    TQFile* tmpFile = contentTmpFile.file();

    //todo save content
    TQTextStream stream(tmpFile);
    stream.setEncoding( TQTextStream::UnicodeUTF8 );
    formula->saveMathML( stream, true );

    tmpFile->close();
    contentWriter->addCompleteElement( TQT_TQIODEVICE(tmpFile) );
    contentTmpFile.close();



    contentWriter->endElement();
    delete contentWriter;

    if(!store->close())
        return false;

    manifestWriter->addManifestEntry("content.xml", "text/xml");

    setModified( false );

    return true;
}


TQDomDocument KFormulaDoc::saveXML()
{
    TQDomDocument doc = document->saveXML();
    history->documentSaved();
    return doc;
}

bool KFormulaDoc::loadOasis( const TQDomDocument& doc, KoOasisStyles&, const TQDomDocument&, KoStore* )
{
    // we don't have style into this format
    // we don't have settings into kformula (for the moment)
    // necessary to adapt kformula code to load MathML format with Oasis Extension.
    
    if ( document->loadOasis( doc ) ) {
        history->clear();
        history->documentSaved();
        return true;
    }
    return false;
}

bool KFormulaDoc::loadXML(TQIODevice *, const TQDomDocument& doc)
{
    if ( doc.doctype().name().lower() == "math"
         || doc.documentElement().tagName().lower() == "math" )
        if ( document->loadOasis( doc ) ) {
            history->clear();
            history->documentSaved();
            return true;
        }
    if ( document->loadXML( doc ) ) {
        history->clear();
        history->documentSaved();
        return true;
    }
    return false;
}

/**
 * Saves the document in native format, to a given file.
 * It is reimplemented to handle the special case of MathML, since it is
 * a native format but not compressed.
 */
bool KFormulaDoc::saveNativeFormat( const TQString & file )
{
    TQCString mimeType = outputMimeType();
    bool mathml = !mimeType.isEmpty() && mimeType.contains( "mathml", false );
    if ( mathml ) {
        TQFile f( file );
        if ( f.open( IO_WriteOnly | IO_Translate ) )
        {
            TQTextStream stream( &f );
            stream.setEncoding( TQTextStream::UnicodeUTF8 );
            formula->saveMathML( stream, false );
            f.close();
            return true;
        }
        else
            return false;
    }
    // If it's not MathML, let the parent handle it
    return KoDocument::saveNativeFormat( file );
}

KoView* KFormulaDoc::createViewInstance(TQWidget* _parent, const char *name)
{
    return new KFormulaPartView(this, _parent, name);
}

void KFormulaDoc::commandExecuted()
{
    if (formula->isEmpty()) {
        setEmpty();
    }
    setModified(true);
}

void KFormulaDoc::documentRestored()
{
    setModified(false);
}


bool KFormulaDoc::initDoc(InitDocFlags /*flags*/, TQWidget* /*parentWidget*/)
{
    // If nothing is loaded, do initialize here
    return TRUE;
}

void KFormulaDoc::showStartUpWidget(KoMainWindow* parent, bool /*alwaysShow*/)
{
    parent->setRootDocument( this );
}

bool KFormulaDoc::showEmbedInitDialog(TQWidget* /*parent*/)
{
  return true;
}

void KFormulaDoc::paintContent(TQPainter& painter, const TQRect& rect, bool transparent, double zoomX, double zoomY)
{
    // ####### handle transparency and zoom
    // Need to draw only the document rectangle described in the parameter rect.

    bool forPrint = painter.device() && painter.device()->devType() == TQInternal::Printer;
    document->setZoomAndResolution( 100, zoomX, zoomY, true, forPrint );
    if ( !transparent ) {
        painter.fillRect( rect, TQt::white );
    }
    formula->draw( painter, rect );
}

TQString KFormulaDoc::configFile() const
{
//    return readConfigFile( locate( "data", "kformula/kformula.rc",
//                                 KFormulaFactory::global() ) );

//    return readConfigFile( "kformula.rc" );
    return TQString();
}

#include "kformula_doc.moc"