10#include <tqmainwindow.h>
13#include <tqtextstream.h>
15#include <tqfileinfo.h>
17#include <tdeapplication.h>
18#include <tdeaboutdata.h>
19#include <tdecmdlineargs.h>
22#include <tdefiledialog.h>
24#include "elementtype.h"
25#include "kformulacommand.h"
26#include "kformulacontainer.h"
27#include "kformuladocument.h"
28#include "kformulawidget.h"
29#include "scrollview.h"
31using namespace KFormula;
36 TestWidget(
Container* doc, TQWidget* parent=0,
const char* name=0, WFlags f=0)
40 virtual void keyPressEvent(TQKeyEvent* event);
46void save(
const TQString &filename,
const TQDomDocument& doc )
49 if(!f.open(IO_Truncate | IO_ReadWrite)) {
50 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
54 TQTextStream stream(&f);
55 stream.setEncoding(TQTextStream::UnicodeUTF8);
61void load( KFormula::Document* document,
const TQString &filename )
64 if (!f.open(IO_ReadOnly)) {
65 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
68 TQTextStream stream(&f);
69 stream.setEncoding(TQTextStream::UnicodeUTF8);
70 TQString content = stream.read();
74 if ( !doc.setContent( content ) ) {
77 if ( !document->loadXML( doc ) ) {
78 kdWarning( DEBUGID ) <<
"Failed." << endl;
83void saveMathML( KFormula::Container* formula,
const TQString &filename,
bool oasisFormat )
86 if ( !f.open( IO_Truncate | IO_ReadWrite ) ) {
87 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
91 TQTextStream stream( &f );
92 stream.setEncoding( TQTextStream::UnicodeUTF8 );
93 formula->saveMathML( stream, oasisFormat );
98void loadMathML( KFormula::Container* formula,
const TQString &filename )
100 TQFile f( filename );
101 if ( !f.open( IO_ReadOnly ) ) {
102 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
105 TQTextStream stream( &f );
106 stream.setEncoding( TQTextStream::UnicodeUTF8 );
107 TQString content = stream.read();
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;
125 if ( !formula->loadMathML( doc ) ) {
126 kdWarning( DEBUGID ) <<
"Failed." << endl;
132void TestWidget::keyPressEvent(TQKeyEvent* event)
137 int state =
event->state();
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 );
return;
147 case TQt::Key_M: saveMathML( document,
"test.mml",
false );
return;
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 );
156 else if ( fi.extension() ==
"xml" ) {
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;
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;
199 KFormulaWidget::keyPressEvent(event);
203ScrollView::ScrollView()
204 : TQScrollView(), child(0)
210 TQScrollView::addChild(c, x, y);
212 connect(child, TQ_SIGNAL(cursorChanged(
bool,
bool)),
213 this, TQ_SLOT(cursorChanged(
bool,
bool)));
216void ScrollView::focusInEvent(TQFocusEvent*)
218 if (child != 0) child->setFocus();
222void ScrollView::cursorChanged(
bool visible,
bool )
225 int x = child->getCursorPoint().x();
226 int y = child->getCursorPoint().y();
232static const TDECmdLineOptions options[]= {
233 {
"+file",
"File to open", 0 },
237int main(
int argc,
char** argv)
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");
244 TDECmdLineArgs::init(argc, argv, &aboutData);
245 TDECmdLineArgs::addCmdLineOptions(options);
249 app.connect(&app, TQ_SIGNAL(lastWindowClosed()), &app, TQ_SLOT(
quit()));
256 ScrollView* scrollview1a =
new ScrollView;
258 KFormulaWidget* mw1a =
new TestWidget(container1, scrollview1a,
"test1a");
260 scrollview1a->addChild(mw1a);
261 scrollview1a->setCaption(
"Test1a of the formula engine");
262 scrollview1a->show();
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() );
273 int result = app.exec();
280 TQApplication::clipboard()->clear();
282 int destruct = BasicElement::getEvilDestructionCount();
284 std::cerr <<
"BasicElement::EvilDestructionCount: " << destruct << std::endl;
288 std::cerr <<
"PlainCommand::EvilDestructionCount: " << destruct << std::endl;
290 destruct = ElementType::getEvilDestructionCount();
292 std::cerr <<
"ElementType::EvilDestructionCount: " << destruct << std::endl;
298#include "scrollview.moc"
virtual Document * document() const
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.
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)