~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to lib/kformula/main.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include <memory>
4
4
 
5
5
#include <qaccel.h>
 
6
#include <qdom.h>
6
7
#include <qfile.h>
7
8
#include <qlayout.h>
8
 
#include <qlist.h>
 
9
#include <qptrlist.h>
9
10
#include <qmainwindow.h>
10
11
#include <qpainter.h>
11
12
#include <qstring.h>
12
13
#include <qtextstream.h>
13
14
#include <qwidget.h>
 
15
#include <qfileinfo.h>
14
16
 
15
 
#include <kapp.h>
 
17
#include <kapplication.h>
16
18
#include <kaboutdata.h>
17
19
#include <kcmdlineargs.h>
18
20
#include <kcommand.h>
19
21
#include <kdebug.h>
 
22
#include <kfiledialog.h>
20
23
 
21
24
#include "elementtype.h"
22
25
#include "kformulacommand.h"
30
33
 
31
34
class TestWidget : public KFormulaWidget {
32
35
public:
33
 
    TestWidget(KFormulaContainer* doc, QWidget* parent=0, const char* name=0, WFlags f=0)
 
36
    TestWidget(Container* doc, QWidget* parent=0, const char* name=0, WFlags f=0)
34
37
            : KFormulaWidget(doc, parent, name, f) {}
35
38
 
36
39
protected:
37
40
    virtual void keyPressEvent(QKeyEvent* event);
38
41
 
39
42
private:
40
 
    void importOld(QString file);
41
43
};
42
44
 
43
45
 
 
46
void save( QString filename, QDomDocument doc )
 
47
{
 
48
    QFile f( filename );
 
49
    if(!f.open(IO_Truncate | IO_ReadWrite)) {
 
50
        kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
 
51
        return;
 
52
    }
 
53
 
 
54
    QTextStream stream(&f);
 
55
    stream.setEncoding(QTextStream::UnicodeUTF8);
 
56
    doc.save(stream, 2);
 
57
    f.close();
 
58
}
 
59
 
 
60
 
 
61
void load( KFormula::Document* document, QString filename )
 
62
{
 
63
    QFile f(filename);
 
64
    if (!f.open(IO_ReadOnly)) {
 
65
        kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
 
66
        return;
 
67
    }
 
68
    QTextStream stream(&f);
 
69
    stream.setEncoding(QTextStream::UnicodeUTF8);
 
70
    QString content = stream.read();
 
71
    f.close();
 
72
    //kdDebug( DEBUGID ) << content << endl;
 
73
    QDomDocument doc;
 
74
    if ( !doc.setContent( content ) ) {
 
75
        return;
 
76
    }
 
77
    if ( !document->loadXML( doc ) ) {
 
78
        kdWarning( DEBUGID ) << "Failed." << endl;
 
79
    }
 
80
}
 
81
 
 
82
 
 
83
void saveMathML( KFormula::Container* formula, QString filename )
 
84
{
 
85
    QFile f( filename );
 
86
    if ( !f.open( IO_Truncate | IO_ReadWrite ) ) {
 
87
        kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
 
88
        return;
 
89
    }
 
90
 
 
91
    QTextStream stream( &f );
 
92
    stream.setEncoding( QTextStream::UnicodeUTF8 );
 
93
    formula->saveMathML( stream );
 
94
    f.close();
 
95
}
 
96
 
 
97
 
 
98
void loadMathML( KFormula::Container* formula, QString filename )
 
99
{
 
100
    QFile f( filename );
 
101
    if ( !f.open( IO_ReadOnly ) ) {
 
102
        kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
 
103
        return;
 
104
    }
 
105
    QTextStream stream( &f );
 
106
    stream.setEncoding( QTextStream::UnicodeUTF8 );
 
107
    QString content = stream.read();
 
108
 
 
109
    QDomDocument doc;
 
110
    QString errorMsg;
 
111
    int errorLine;
 
112
    int errorColumn;
 
113
    if ( !doc.setContent( content, true,
 
114
                          &errorMsg, &errorLine, &errorColumn ) ) {
 
115
        kdWarning( DEBUGID ) << "MathML built error: " << errorMsg
 
116
                             << " at line " << errorLine
 
117
                             << " and column " << errorColumn << endl;
 
118
        f.close();
 
119
        return;
 
120
    }
 
121
 
 
122
    /*kdDebug( DEBUGID ) << "Container::loadMathML\n"
 
123
      << doc.toCString() << endl;*/
 
124
 
 
125
    if ( !formula->loadMathML( doc ) ) {
 
126
        kdWarning( DEBUGID ) << "Failed." << endl;
 
127
    }
 
128
    f.close();
 
129
}
 
130
 
 
131
 
44
132
void TestWidget::keyPressEvent(QKeyEvent* event)
45
133
{
46
 
    KFormulaContainer* document = getDocument();
 
134
    Container* document = getDocument();
47
135
 
48
136
    //int action = event->key();
49
137
    int state = event->state();
50
138
    //MoveFlag flag = movementFlag(state);
51
139
 
52
 
    if (state & Qt::ControlButton) {
53
 
        switch (event->key()) {
54
 
            case Qt::Key_1: document->addSum(); return;
55
 
            case Qt::Key_2: document->addProduct(); return;
56
 
            case Qt::Key_3: document->addIntegral(); return;
57
 
            case Qt::Key_4: document->addRoot(); return;
58
 
            case Qt::Key_5: document->addFraction(); return;
59
 
            case Qt::Key_6: document->addMatrix(); return;
60
 
            case Qt::Key_7: document->addOneByTwoMatrix(); return;
61
 
            case Qt::Key_0: importOld("oldformula"); return;
 
140
    if ( ( state & Qt::ShiftButton ) && ( state & Qt::ControlButton ) ) {
 
141
        switch (event->key()) {
 
142
            case Qt::Key_B: document->document()->wrapper()->appendColumn(); return;
 
143
            case Qt::Key_I: document->document()->wrapper()->insertColumn(); return;
 
144
            case Qt::Key_R: document->document()->wrapper()->removeColumn(); return;
 
145
            case Qt::Key_Z: document->document()->wrapper()->redo(); return;
 
146
 
 
147
            case Qt::Key_M: saveMathML( document, "test.mml" ); return;
 
148
            case Qt::Key_O: {
 
149
                QString file = KFileDialog::getOpenFileName();
 
150
                kdDebug( DEBUGID ) << file << endl;
 
151
                if( !file.isEmpty() ) {
 
152
                    QFileInfo fi( file );
 
153
                    if ( fi.extension() == "mml" ) {
 
154
                        loadMathML( document, file );
 
155
                    }
 
156
                    else if ( fi.extension() == "xml" ) {
 
157
                        load( document->document(), file );
 
158
                    }
 
159
                }
 
160
                return;
 
161
        }
 
162
        }
 
163
    }
 
164
    else if (state & Qt::ControlButton) {
 
165
        switch (event->key()) {
 
166
            case Qt::Key_1: document->document()->wrapper()->addSum(); return;
 
167
            case Qt::Key_2: document->document()->wrapper()->addProduct(); return;
 
168
            case Qt::Key_3: document->document()->wrapper()->addIntegral(); return;
 
169
            case Qt::Key_4: document->document()->wrapper()->addRoot(); return;
 
170
            case Qt::Key_5: document->document()->wrapper()->addFraction(); return;
 
171
            case Qt::Key_6: document->document()->wrapper()->addMatrix(); return;
 
172
            case Qt::Key_7: document->document()->wrapper()->addOneByTwoMatrix(); return;
 
173
            case Qt::Key_8: document->document()->wrapper()->addOverline(); return;
 
174
            case Qt::Key_9: document->document()->wrapper()->addUnderline(); return;
62
175
            case Qt::Key_A: slotSelectAll(); return;
63
 
            case Qt::Key_C: document->copy(); return;
64
 
            case Qt::Key_D: document->replaceElementWithMainChild(BasicElement::afterCursor); return;
65
 
            case Qt::Key_G: document->makeGreek(); return;
66
 
            case Qt::Key_L: document->addGenericLowerIndex(); return;
67
 
            case Qt::Key_M: document->loadMathMl("mathml.xml"); return;
68
 
            case Qt::Key_O: document->load("test.xml"); return;
 
176
            case Qt::Key_B: document->document()->wrapper()->appendRow(); return;
 
177
            case Qt::Key_C: document->document()->wrapper()->copy(); return;
 
178
            case Qt::Key_D: document->document()->wrapper()->removeEnclosing(); return;
 
179
            case Qt::Key_G: document->document()->wrapper()->makeGreek(); return;
 
180
            case Qt::Key_I: document->document()->wrapper()->insertRow(); return;
 
181
            case Qt::Key_R: document->document()->wrapper()->removeRow(); return;
 
182
            case Qt::Key_K: document->document()->wrapper()->addMultiline(); return;
 
183
            case Qt::Key_L: document->document()->wrapper()->addGenericLowerIndex(); return;
 
184
            case Qt::Key_M: loadMathML( document, "test.mml" ); return;
 
185
            case Qt::Key_O: load( document->document(), "test.xml" ); return;
69
186
            case Qt::Key_Q: kapp->quit(); return;
70
 
            case Qt::Key_R: document->replaceElementWithMainChild(BasicElement::beforeCursor); return;
71
 
            case Qt::Key_S: document->save("test.xml"); return;
72
 
            case Qt::Key_T: cout << document->texString().latin1() << endl; return;
73
 
            case Qt::Key_U: document->addGenericUpperIndex(); return;
74
 
            case Qt::Key_V: document->paste(); return;
75
 
            case Qt::Key_X: document->cut(); return;
76
 
            case Qt::Key_Z: (state & Qt::ShiftButton) ? document->getDocument()->redo() : document->getDocument()->undo(); return;
 
187
            case Qt::Key_S: save( "test.xml", document->document()->saveXML() ); return;
 
188
            case Qt::Key_T: std::cout << document->texString().latin1() << std::endl; return;
 
189
            case Qt::Key_U: document->document()->wrapper()->addGenericUpperIndex(); return;
 
190
            case Qt::Key_V: document->document()->wrapper()->paste(); return;
 
191
            case Qt::Key_X: document->document()->wrapper()->cut(); return;
 
192
            case Qt::Key_Z: document->document()->wrapper()->undo(); return;
77
193
            default:
78
 
                //cerr << "Key: " << event->key() << endl;
 
194
                //std::cerr << "Key: " << event->key() << std::endl;
79
195
                break;
80
196
        }
81
197
    }
84
200
}
85
201
 
86
202
 
87
 
void TestWidget::importOld(QString file)
88
 
{
89
 
    QFile f(file);
90
 
    if (!f.open(IO_ReadOnly)) {
91
 
        cerr << "Error opening file" << endl;
92
 
        return;
93
 
    }
94
 
    QTextStream stream(&f);
95
 
    stream.setEncoding(QTextStream::Unicode);
96
 
    QString text = stream.readLine();
97
 
    getDocument()->importOldText(text);
98
 
}
99
 
 
100
 
 
101
203
ScrollView::ScrollView()
102
204
        : QScrollView(), child(0)
103
205
{
120
222
void ScrollView::cursorChanged(bool visible, bool /*selecting*/)
121
223
{
122
224
    if (visible) {
123
 
        double x = child->getCursorPoint().x();
124
 
        double y = child->getCursorPoint().y();
 
225
        int x = child->getCursorPoint().x();
 
226
        int y = child->getCursorPoint().y();
125
227
        ensureVisible(x, y);
126
228
    }
127
229
}
128
230
 
129
231
 
130
232
static const KCmdLineOptions options[]= {
131
 
    {0,0,0}
 
233
    { "+file", "File to open", 0 },
 
234
    KCmdLineLastOption
132
235
};
133
236
 
134
237
int main(int argc, char** argv)
135
238
{
136
 
    KAboutData aboutData("formula engine test", "KFormula test",
 
239
    KAboutData aboutData("math test", "KFormula test",
137
240
                         "0.01", "test", KAboutData::License_GPL,
138
 
                         "(c) 2001, Andrea Rizzi, Ulrich Kuettler");
139
 
    aboutData.addAuthor("Andrea Rizzi",0, "rizzi@kde.org");
140
 
    aboutData.addAuthor("Ulrich Kuettler",0, "ulrich.kuettler@mailbox.tu-dresden.delete");
 
241
                         "(c) 2003, Ulrich Kuettler");
 
242
    aboutData.addAuthor("Ulrich Kuettler",0, "ulrich.kuettler@gmx.de");
141
243
 
142
244
    KCmdLineArgs::init(argc, argv, &aboutData);
143
245
    KCmdLineArgs::addCmdLineOptions(options);
146
248
 
147
249
    app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
148
250
 
149
 
    KFormulaDocument* document = new KFormulaDocument;
150
 
    KFormulaContainer* container1 = document->createFormula();
151
 
    KFormulaContainer* container2 = document->createFormula();
 
251
    DocumentWrapper* wrapper = new DocumentWrapper( kapp->config(), 0 );
 
252
    Document* document = new Document;
 
253
    wrapper->document( document );
 
254
    Container* container1 = document->createFormula();
152
255
 
153
256
    ScrollView* scrollview1a = new ScrollView;
154
 
    ScrollView* scrollview1b = new ScrollView;
155
 
    ScrollView* scrollview2a = new ScrollView;
156
 
    ScrollView* scrollview2b = new ScrollView;
157
257
 
158
258
    KFormulaWidget* mw1a = new TestWidget(container1, scrollview1a, "test1a");
159
 
    KFormulaWidget* mw1b = new TestWidget(container1, scrollview1b, "test1b");
160
 
    KFormulaWidget* mw2a = new TestWidget(container2, scrollview2a, "test2a");
161
 
    KFormulaWidget* mw2b = new TestWidget(container2, scrollview2b, "test2b");
162
259
 
163
260
    scrollview1a->addChild(mw1a);
164
 
    scrollview1b->addChild(mw1b);
165
 
    scrollview2a->addChild(mw2a);
166
 
    scrollview2b->addChild(mw2b);
167
 
 
168
261
    scrollview1a->setCaption("Test1a of the formula engine");
169
 
    scrollview1b->setCaption("Test1b of the formula engine");
170
 
    scrollview2a->setCaption("Test2a of the formula engine (ro)");
171
 
    scrollview2b->setCaption("Test2b of the formula engine");
172
 
 
173
262
    scrollview1a->show();
174
 
    scrollview1b->show();
175
 
    scrollview2a->show();
176
 
    scrollview2b->show();
177
263
 
178
 
    // to keep things interessting
179
 
    mw2a->setReadOnly(true);
 
264
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
 
265
    for ( int i = 0; i < args->count(); ++i ) {
 
266
        QFileInfo fi( args->url( i ).path() );
 
267
        if ( fi.extension() == "mml" )
 
268
            loadMathML( container1, args->url( i ).path() );
 
269
        else if ( fi.extension() == "xml" )
 
270
            load( container1->document(), args->url( i ).path() );
 
271
    }
180
272
 
181
273
    int result = app.exec();
182
274
 
183
 
    delete container2;
184
275
    delete container1;
185
 
    delete document;
 
276
    delete wrapper;
186
277
 
187
278
    // Make sure there are no elements in the clipboard.
188
279
    // Okey for a debug app.
190
281
 
191
282
    int destruct = BasicElement::getEvilDestructionCount();
192
283
    if (destruct != 0) {
193
 
        cerr << "BasicElement::EvilDestructionCount: " << destruct << endl;
 
284
        std::cerr << "BasicElement::EvilDestructionCount: " << destruct << std::endl;
194
285
    }
195
 
    destruct = KFormulaCommand::getEvilDestructionCount();
 
286
    destruct = PlainCommand::getEvilDestructionCount();
196
287
    if (destruct != 0) {
197
 
        cerr << "KFormulaCommand::EvilDestructionCount: " << destruct << endl;
 
288
        std::cerr << "PlainCommand::EvilDestructionCount: " << destruct << std::endl;
198
289
    }
199
290
    destruct = ElementType::getEvilDestructionCount();
200
291
    if (destruct != 0) {
201
 
        cerr << "ElementType::EvilDestructionCount: " << destruct << endl;
 
292
        std::cerr << "ElementType::EvilDestructionCount: " << destruct << std::endl;
202
293
    }
203
294
 
204
295
    return result;