lib

kformula/main.cpp
1
2#include <iostream>
3#include <memory>
4
5#include <tqaccel.h>
6#include <tqdom.h>
7#include <tqfile.h>
8#include <tqlayout.h>
9#include <tqptrlist.h>
10#include <tqmainwindow.h>
11#include <tqpainter.h>
12#include <tqstring.h>
13#include <tqtextstream.h>
14#include <tqwidget.h>
15#include <tqfileinfo.h>
16
17#include <tdeapplication.h>
18#include <tdeaboutdata.h>
19#include <tdecmdlineargs.h>
20#include <kcommand.h>
21#include <kdebug.h>
22#include <tdefiledialog.h>
23
24#include "elementtype.h"
25#include "kformulacommand.h"
26#include "kformulacontainer.h"
27#include "kformuladocument.h"
28#include "kformulawidget.h"
29#include "scrollview.h"
30
31using namespace KFormula;
32
33
34class TestWidget : public KFormulaWidget {
35public:
36 TestWidget(Container* doc, TQWidget* parent=0, const char* name=0, WFlags f=0)
37 : KFormulaWidget(doc, parent, name, f) {}
38
39protected:
40 virtual void keyPressEvent(TQKeyEvent* event);
41
42private:
43};
44
45
46void save( const TQString &filename, const TQDomDocument& doc )
47{
48 TQFile f( filename );
49 if(!f.open(IO_Truncate | IO_ReadWrite)) {
50 kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
51 return;
52 }
53
54 TQTextStream stream(&f);
55 stream.setEncoding(TQTextStream::UnicodeUTF8);
56 doc.save(stream, 2);
57 f.close();
58}
59
60
61void load( KFormula::Document* document, const TQString &filename )
62{
63 TQFile f(filename);
64 if (!f.open(IO_ReadOnly)) {
65 kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
66 return;
67 }
68 TQTextStream stream(&f);
69 stream.setEncoding(TQTextStream::UnicodeUTF8);
70 TQString content = stream.read();
71 f.close();
72 //kdDebug( DEBUGID ) << content << endl;
73 TQDomDocument doc;
74 if ( !doc.setContent( content ) ) {
75 return;
76 }
77 if ( !document->loadXML( doc ) ) {
78 kdWarning( DEBUGID ) << "Failed." << endl;
79 }
80}
81
82
83void saveMathML( KFormula::Container* formula, const TQString &filename, bool oasisFormat )
84{
85 TQFile f( filename );
86 if ( !f.open( IO_Truncate | IO_ReadWrite ) ) {
87 kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
88 return;
89 }
90
91 TQTextStream stream( &f );
92 stream.setEncoding( TQTextStream::UnicodeUTF8 );
93 formula->saveMathML( stream, oasisFormat );
94 f.close();
95}
96
97
98void loadMathML( KFormula::Container* formula, const TQString &filename )
99{
100 TQFile f( filename );
101 if ( !f.open( IO_ReadOnly ) ) {
102 kdWarning( DEBUGID ) << "Error opening file " << filename.latin1() << endl;
103 return;
104 }
105 TQTextStream stream( &f );
106 stream.setEncoding( TQTextStream::UnicodeUTF8 );
107 TQString content = stream.read();
108
109 TQDomDocument doc;
110 TQString 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
132void TestWidget::keyPressEvent(TQKeyEvent* event)
133{
134 Container* document = getDocument();
135
136 //int action = event->key();
137 int state = event->state();
138 //MoveFlag flag = movementFlag(state);
139
140 if ( ( state & TQt::ShiftButton ) && ( state & TQt::ControlButton ) ) {
141 switch (event->key()) {
142 case TQt::Key_B: document->document()->wrapper()->appendColumn(); return;
143 case TQt::Key_I: document->document()->wrapper()->insertColumn(); return;
144 case TQt::Key_R: document->document()->wrapper()->removeColumn(); return;
145 case TQt::Key_Z: document->document()->wrapper()->redo(); return;
146 case TQt::Key_F: saveMathML( document, "test.mml", true/*save to oasis format*/ ); return;
147 case TQt::Key_M: saveMathML( document, "test.mml", false ); return;
148 case TQt::Key_O: {
149 TQString file = KFileDialog::getOpenFileName();
150 kdDebug( DEBUGID ) << file << endl;
151 if( !file.isEmpty() ) {
152 TQFileInfo 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 & TQt::ControlButton) {
165 switch (event->key()) {
166 case TQt::Key_1: document->document()->wrapper()->addSum(); return;
167 case TQt::Key_2: document->document()->wrapper()->addProduct(); return;
168 case TQt::Key_3: document->document()->wrapper()->addIntegral(); return;
169 case TQt::Key_4: document->document()->wrapper()->addRoot(); return;
170 case TQt::Key_5: document->document()->wrapper()->addFraction(); return;
171 case TQt::Key_6: document->document()->wrapper()->addMatrix(); return;
172 case TQt::Key_7: document->document()->wrapper()->addOneByTwoMatrix(); return;
173 case TQt::Key_8: document->document()->wrapper()->addOverline(); return;
174 case TQt::Key_9: document->document()->wrapper()->addUnderline(); return;
175 case TQt::Key_A: slotSelectAll(); return;
176 case TQt::Key_B: document->document()->wrapper()->appendRow(); return;
177 case TQt::Key_C: document->document()->wrapper()->copy(); return;
178 case TQt::Key_D: document->document()->wrapper()->removeEnclosing(); return;
179 case TQt::Key_G: document->document()->wrapper()->makeGreek(); return;
180 case TQt::Key_I: document->document()->wrapper()->insertRow(); return;
181 case TQt::Key_R: document->document()->wrapper()->removeRow(); return;
182 case TQt::Key_K: document->document()->wrapper()->addMultiline(); return;
183 case TQt::Key_L: document->document()->wrapper()->addGenericLowerIndex(); return;
184 case TQt::Key_M: loadMathML( document, "test.mml" ); return;
185 case TQt::Key_O: load( document->document(), "test.xml" ); return;
186 case TQt::Key_Q: kapp->quit(); return;
187 case TQt::Key_S: save( "test.xml", document->document()->saveXML() ); return;
188 case TQt::Key_T: std::cout << document->texString().latin1() << std::endl; return;
189 case TQt::Key_U: document->document()->wrapper()->addGenericUpperIndex(); return;
190 case TQt::Key_V: document->document()->wrapper()->paste(); return;
191 case TQt::Key_X: document->document()->wrapper()->cut(); return;
192 case TQt::Key_Z: document->document()->wrapper()->undo(); return;
193 default:
194 //std::cerr << "Key: " << event->key() << std::endl;
195 break;
196 }
197 }
198
199 KFormulaWidget::keyPressEvent(event);
200}
201
202
203ScrollView::ScrollView()
204 : TQScrollView(), child(0)
205{
206}
207
208void ScrollView::addChild(KFormulaWidget* c, int x, int y)
209{
210 TQScrollView::addChild(c, x, y);
211 child = c;
212 connect(child, TQ_SIGNAL(cursorChanged(bool, bool)),
213 this, TQ_SLOT(cursorChanged(bool, bool)));
214}
215
216void ScrollView::focusInEvent(TQFocusEvent*)
217{
218 if (child != 0) child->setFocus();
219}
220
221
222void ScrollView::cursorChanged(bool visible, bool /*selecting*/)
223{
224 if (visible) {
225 int x = child->getCursorPoint().x();
226 int y = child->getCursorPoint().y();
227 ensureVisible(x, y);
228 }
229}
230
231
232static const TDECmdLineOptions options[]= {
233 { "+file", "File to open", 0 },
234 TDECmdLineLastOption
235};
236
237int main(int argc, char** argv)
238{
239 TDEAboutData aboutData("math test", "KFormula test",
240 "0.01", "test", TDEAboutData::License_GPL,
241 "(c) 2003, Ulrich Kuettler");
242 aboutData.addAuthor("Ulrich Kuettler",0, "ulrich.kuettler@gmx.de");
243
244 TDECmdLineArgs::init(argc, argv, &aboutData);
245 TDECmdLineArgs::addCmdLineOptions(options);
246
247 TDEApplication app;
248
249 app.connect(&app, TQ_SIGNAL(lastWindowClosed()), &app, TQ_SLOT(quit()));
250
251 DocumentWrapper* wrapper = new DocumentWrapper( kapp->config(), 0 );
252 Document* document = new Document;
253 wrapper->document( document );
254 Container* container1 = document->createFormula();
255
256 ScrollView* scrollview1a = new ScrollView;
257
258 KFormulaWidget* mw1a = new TestWidget(container1, scrollview1a, "test1a");
259
260 scrollview1a->addChild(mw1a);
261 scrollview1a->setCaption("Test1a of the formula engine");
262 scrollview1a->show();
263
264 TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
265 for ( int i = 0; i < args->count(); ++i ) {
266 TQFileInfo 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 }
272
273 int result = app.exec();
274
275 delete container1;
276 delete wrapper;
277
278 // Make sure there are no elements in the clipboard.
279 // Okey for a debug app.
280 TQApplication::clipboard()->clear();
281
282 int destruct = BasicElement::getEvilDestructionCount();
283 if (destruct != 0) {
284 std::cerr << "BasicElement::EvilDestructionCount: " << destruct << std::endl;
285 }
287 if (destruct != 0) {
288 std::cerr << "PlainCommand::EvilDestructionCount: " << destruct << std::endl;
289 }
290 destruct = ElementType::getEvilDestructionCount();
291 if (destruct != 0) {
292 std::cerr << "ElementType::EvilDestructionCount: " << destruct << std::endl;
293 }
294
295 return result;
296}
297
298#include "scrollview.moc"
The document.
virtual Document * document() const
TQString texString()
A Wrapper that constracts the actions and must be given a real document to work with.
Document * document() const
A document that can contain a lot of formulas (container).
virtual Container * createFormula(int pos=-1, bool registerMe=true)
Factory method.
TQDomDocument saveXML()
Save the document with all its formulae.
Container * getDocument()
The document we show.
static int getEvilDestructionCount()
debug only.
TDEAction * save(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
TDEAction * quit(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
const char * name(StdAction id)