00001
00002
#include <iostream>
00003
#include <memory>
00004
00005
#include <qaccel.h>
00006
#include <qdom.h>
00007
#include <qfile.h>
00008
#include <qlayout.h>
00009
#include <qptrlist.h>
00010
#include <qmainwindow.h>
00011
#include <qpainter.h>
00012
#include <qstring.h>
00013
#include <qtextstream.h>
00014
#include <qwidget.h>
00015
#include <qfileinfo.h>
00016
00017
#include <kapplication.h>
00018
#include <kaboutdata.h>
00019
#include <kcmdlineargs.h>
00020
#include <kcommand.h>
00021
#include <kdebug.h>
00022
#include <kfiledialog.h>
00023
00024
#include "elementtype.h"
00025
#include "kformulacommand.h"
00026
#include "kformulacontainer.h"
00027
#include "kformuladocument.h"
00028
#include "kformulawidget.h"
00029
#include "scrollview.h"
00030
00031
using namespace KFormula;
00032
00033
00034
class TestWidget :
public KFormulaWidget {
00035
public:
00036 TestWidget(
Container* doc,
QWidget* parent=0,
const char* name=0, WFlags f=0)
00037 :
KFormulaWidget(doc, parent, name, f) {}
00038
00039
protected:
00040
virtual void keyPressEvent(
QKeyEvent* event);
00041
00042
private:
00043 };
00044
00045
00046
void save(
QString filename,
QDomDocument doc )
00047 {
00048
QFile f( filename );
00049
if(!f.open(IO_Truncate | IO_ReadWrite)) {
00050 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
00051
return;
00052 }
00053
00054
QTextStream stream(&f);
00055 stream.setEncoding(QTextStream::UnicodeUTF8);
00056 doc.save(stream, 2);
00057 f.close();
00058 }
00059
00060
00061
void load( KFormula::Document* document,
QString filename )
00062 {
00063
QFile f(filename);
00064
if (!f.open(IO_ReadOnly)) {
00065 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
00066
return;
00067 }
00068
QTextStream stream(&f);
00069 stream.setEncoding(QTextStream::UnicodeUTF8);
00070
QString content = stream.read();
00071 f.close();
00072
00073
QDomDocument doc;
00074
if ( !doc.setContent( content ) ) {
00075
return;
00076 }
00077
if ( !document->loadXML( doc ) ) {
00078 kdWarning( DEBUGID ) <<
"Failed." << endl;
00079 }
00080 }
00081
00082
00083
void saveMathML( KFormula::Container* formula,
QString filename )
00084 {
00085
QFile f( filename );
00086
if ( !f.open( IO_Truncate | IO_ReadWrite ) ) {
00087 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
00088
return;
00089 }
00090
00091
QTextStream stream( &f );
00092 stream.setEncoding( QTextStream::UnicodeUTF8 );
00093 formula->saveMathML( stream );
00094 f.close();
00095 }
00096
00097
00098
void loadMathML( KFormula::Container* formula,
QString filename )
00099 {
00100
QFile f( filename );
00101
if ( !f.open( IO_ReadOnly ) ) {
00102 kdWarning( DEBUGID ) <<
"Error opening file " << filename.latin1() << endl;
00103
return;
00104 }
00105
QTextStream stream( &f );
00106 stream.setEncoding( QTextStream::UnicodeUTF8 );
00107
QString content = stream.read();
00108
00109
QDomDocument doc;
00110
QString errorMsg;
00111
int errorLine;
00112
int errorColumn;
00113
if ( !doc.setContent( content,
true,
00114 &errorMsg, &errorLine, &errorColumn ) ) {
00115 kdWarning( DEBUGID ) <<
"MathML built error: " << errorMsg
00116 <<
" at line " << errorLine
00117 <<
" and column " << errorColumn << endl;
00118 f.close();
00119
return;
00120 }
00121
00122
00123
00124
00125
if ( !formula->loadMathML( doc ) ) {
00126 kdWarning( DEBUGID ) <<
"Failed." << endl;
00127 }
00128 f.close();
00129 }
00130
00131
00132
void TestWidget::keyPressEvent(
QKeyEvent* event)
00133 {
00134
Container* document = getDocument();
00135
00136
00137
int state = event->state();
00138
00139
00140
if ( ( state & Qt::ShiftButton ) && ( state & Qt::ControlButton ) ) {
00141
switch (event->key()) {
00142
case Qt::Key_B: document->
document()->
wrapper()->
appendColumn();
return;
00143
case Qt::Key_I: document->
document()->
wrapper()->
insertColumn();
return;
00144
case Qt::Key_R: document->
document()->
wrapper()->
removeColumn();
return;
00145
case Qt::Key_Z: document->
document()->
wrapper()->
redo();
return;
00146
00147
case Qt::Key_M: saveMathML( document,
"test.mml" );
return;
00148
case Qt::Key_O: {
00149
QString file = KFileDialog::getOpenFileName();
00150 kdDebug( DEBUGID ) << file << endl;
00151
if( !file.isEmpty() ) {
00152
QFileInfo fi( file );
00153
if ( fi.extension() ==
"mml" ) {
00154 loadMathML( document, file );
00155 }
00156
else if ( fi.extension() ==
"xml" ) {
00157 load( document->
document(), file );
00158 }
00159 }
00160
return;
00161 }
00162 }
00163 }
00164
else if (state & Qt::ControlButton) {
00165
switch (event->key()) {
00166
case Qt::Key_1: document->
document()->
wrapper()->
addSum();
return;
00167
case Qt::Key_2: document->
document()->
wrapper()->
addProduct();
return;
00168
case Qt::Key_3: document->
document()->
wrapper()->
addIntegral();
return;
00169
case Qt::Key_4: document->
document()->
wrapper()->
addRoot();
return;
00170
case Qt::Key_5: document->
document()->
wrapper()->
addFraction();
return;
00171
case Qt::Key_6: document->
document()->
wrapper()->
addMatrix();
return;
00172
case Qt::Key_7: document->
document()->
wrapper()->
addOneByTwoMatrix();
return;
00173
case Qt::Key_8: document->
document()->
wrapper()->
addOverline();
return;
00174
case Qt::Key_9: document->
document()->
wrapper()->
addUnderline();
return;
00175
case Qt::Key_A: slotSelectAll();
return;
00176
case Qt::Key_B: document->
document()->
wrapper()->
appendRow();
return;
00177
case Qt::Key_C: document->
document()->
wrapper()->
copy();
return;
00178
case Qt::Key_D: document->
document()->
wrapper()->
removeEnclosing();
return;
00179
case Qt::Key_G: document->
document()->
wrapper()->
makeGreek();
return;
00180
case Qt::Key_I: document->
document()->
wrapper()->
insertRow();
return;
00181
case Qt::Key_R: document->
document()->
wrapper()->
removeRow();
return;
00182
case Qt::Key_K: document->
document()->
wrapper()->
addMultiline();
return;
00183
case Qt::Key_L: document->
document()->
wrapper()->
addGenericLowerIndex();
return;
00184
case Qt::Key_M: loadMathML( document,
"test.mml" );
return;
00185
case Qt::Key_O: load( document->
document(),
"test.xml" );
return;
00186
case Qt::Key_Q: kapp->quit();
return;
00187
case Qt::Key_S: save(
"test.xml", document->
document()->
saveXML() );
return;
00188
case Qt::Key_T: std::cout << document->
texString().latin1() << std::endl;
return;
00189
case Qt::Key_U: document->
document()->
wrapper()->
addGenericUpperIndex();
return;
00190
case Qt::Key_V: document->
document()->
wrapper()->
paste();
return;
00191
case Qt::Key_X: document->
document()->
wrapper()->
cut();
return;
00192
case Qt::Key_Z: document->
document()->
wrapper()->
undo();
return;
00193
default:
00194
00195
break;
00196 }
00197 }
00198
00199 KFormulaWidget::keyPressEvent(event);
00200 }
00201
00202
00203 ScrollView::ScrollView()
00204 :
QScrollView(), child(0)
00205 {
00206 }
00207
00208
void ScrollView::addChild(
KFormulaWidget* c,
int x,
int y)
00209 {
00210 QScrollView::addChild(c, x, y);
00211 child = c;
00212 connect(child, SIGNAL(cursorChanged(
bool,
bool)),
00213
this, SLOT(cursorChanged(
bool,
bool)));
00214 }
00215
00216
void ScrollView::focusInEvent(
QFocusEvent*)
00217 {
00218
if (child != 0) child->setFocus();
00219 }
00220
00221
00222
void ScrollView::cursorChanged(
bool visible,
bool )
00223 {
00224
if (visible) {
00225
int x = child->getCursorPoint().x();
00226
int y = child->getCursorPoint().y();
00227 ensureVisible(x, y);
00228 }
00229 }
00230
00231
00232
static const KCmdLineOptions options[]= {
00233 {
"+file",
"File to open", 0 },
00234 KCmdLineLastOption
00235 };
00236
00237
int main(
int argc,
char** argv)
00238 {
00239 KAboutData aboutData(
"math test",
"KFormula test",
00240
"0.01",
"test", KAboutData::License_GPL,
00241
"(c) 2003, Ulrich Kuettler");
00242 aboutData.addAuthor(
"Ulrich Kuettler",0,
"ulrich.kuettler@gmx.de");
00243
00244 KCmdLineArgs::init(argc, argv, &aboutData);
00245 KCmdLineArgs::addCmdLineOptions(options);
00246
00247 KApplication app;
00248
00249 app.connect(&app, SIGNAL(lastWindowClosed()), &app, SLOT(quit()));
00250
00251
DocumentWrapper* wrapper =
new DocumentWrapper( kapp->config(), 0 );
00252
Document* document =
new Document;
00253 wrapper->
document( document );
00254
Container* container1 = document->
createFormula();
00255
00256 ScrollView* scrollview1a =
new ScrollView;
00257
00258
KFormulaWidget* mw1a =
new TestWidget(container1, scrollview1a,
"test1a");
00259
00260 scrollview1a->addChild(mw1a);
00261 scrollview1a->setCaption(
"Test1a of the formula engine");
00262 scrollview1a->show();
00263
00264 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00265
for (
int i = 0; i < args->count(); ++i ) {
00266
QFileInfo fi( args->url( i ).path() );
00267
if ( fi.extension() ==
"mml" )
00268 loadMathML( container1, args->url( i ).path() );
00269
else if ( fi.extension() ==
"xml" )
00270 load( container1->
document(), args->url( i ).path() );
00271 }
00272
00273
int result = app.exec();
00274
00275
delete container1;
00276
delete wrapper;
00277
00278
00279
00280 QApplication::clipboard()->clear();
00281
00282
int destruct =
BasicElement::getEvilDestructionCount();
00283
if (destruct != 0) {
00284 std::cerr <<
"BasicElement::EvilDestructionCount: " << destruct << std::endl;
00285 }
00286 destruct =
PlainCommand::getEvilDestructionCount();
00287
if (destruct != 0) {
00288 std::cerr <<
"PlainCommand::EvilDestructionCount: " << destruct << std::endl;
00289 }
00290 destruct = ElementType::getEvilDestructionCount();
00291
if (destruct != 0) {
00292 std::cerr <<
"ElementType::EvilDestructionCount: " << destruct << std::endl;
00293 }
00294
00295
return result;
00296 }
00297
00298
#include "scrollview.moc"