/* * MathPlanner 3.1.0 - Mathematical design tool. * Copyright(C) 2002 Jarmo Nikkanen * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation. * * You should have received a copy of the GNU General Public License with this program. * */ #include "Datastore.h" #include "Math.h" #include "AppControl.h" #include "Paper.h" #include "Error.h" #include "ListOfGreekAlphabets.h" #include "Setup.h" #include "ConfigReader.h" #include "Extern.h" #include #include #include #include #include #include #include #include #include #include #include MathWindow::MathWindow(QString doc) :QMainWindow(0,"MathPlanner") { int i; // Setup Main Window ApplicationCaption=QString("MathPlanner ")+QString(_MPL_VERSION); setCaption(ApplicationCaption); CurrentDocumentName=""; mainbox=new QVBox(this); mainbox->resize(700,550); mainbox->show(); scrollview=new QScrollView(mainbox); setCentralWidget(mainbox); // Clear Papers for(i=0;i<32;i++) paper[i]=NULL; Active_paper=0; // Create ApplicationControl and Menus AppControl=new ApplicationControl(this); AppControl->AddError(""); BuildMenu(); setIcon(ICON("mathplannerapp")); // AppControl is required // SET DOCUMENT NAME AND OPEN IT if (!doc.isEmpty()) Open(doc); else if (AppControl->Prefs->Int("OpenPreviousFile")) { Open(config_path+QString("/MathPlanner-autosave.mpl2")); } } void MathWindow::MessageReceived(DataStorage *msg,int rec) { if (GetActivePaper()==NULL) return; // Do nothing... if no paper if (rec==TO_MAIN) { if (ISMSG(MSG_PIICONSTANT)) SymbolSelected(GREEK_pii); if (ISMSG(MSG_CONFIGURATION)) { scrollview->setContentsPos(0,0); GetActivePaper()->UpDate(); } if (ISMSG(MSG_RESETNAME)) setCaption(ApplicationCaption); if (ISMSG(MSG_TEXTFRAME) || ISMSG(MSG_IMAGEFRAME)) { QPoint point; msg->ReadQPoint("point",&point); GetActivePaper()->PasteObject(point); } if (ISMSG(MSG_IMAGEEDIT)) { QRect rect; msg->ReadQRect("rect",&rect); QImage pixmap(rect.size(),32); pixmap.fill(0x00); pixmap.setAlphaBuffer(true); pixmap.fill(0x00); if (!pixmap.save(config_path+"/temp.png","PNG")) ErrorReport("Save","Unable to save image"); QString image_editor=AppControl->Prefs->String("ImageEditor"); QString file(config_path+"/temp.png"); QProcess *proc= new QProcess(this); proc->addArgument(image_editor); proc->addArgument(file); if (!proc->start()) { ErrorReport("ImageEditor","Unable to execute program"); } DropDefaultImage=true; } } } void MathWindow::PostMessage(DataStorage *msg,int dest) { if (GetActivePaper()==NULL) return; // Do nothing... if no paper if (dest==TO_PAPER) GetActivePaper()->MessageReceived(msg,dest); else if (dest==TO_APPCONTROL) AppControl->MessageReceived(msg,dest); else if (dest==TO_MAIN) MessageReceived(msg,dest); else GetActivePaper()->PostMessage(msg,dest); } class Paper *MathWindow::GetActivePaper() { return(paper[Active_paper]); } QString MathWindow::Browser() { return(AppControl->Prefs->String("WEBBROWSER")); } int MathWindow::Style() { return(AppControl->Prefs->Int("GUITheme")); } // AppControl & Prefs must be operational before calling BuildMenu void MathWindow::BuildMenu() { ToolBarB=new QToolBar(this); pm1=ICON("filenew"); pm2=ICON("fileopen"); pm3=ICON("filesave"); pm4=ICON("configure"); pm8=ICON("viewmag+"); pm9=ICON("viewmag-"); pm10=ICON("fileprint"); pm14=ICON("reload"); pm15=ICON("editcopy"); pm16=ICON("editcut"); pm17=ICON("editpaste"); pm18=ICON("editselectall"); pm19=ICON("editclear"); pm20=ICON("editdelete"); pm21=ICON("frame_graph"); pm22=ICON("frame_text"); pm23=ICON("frame_image"); pm24=ICON(AppControl->Prefs->String("ImageEditorIcon")); pop1=new QPopupMenu(this); pop1->insertItem(pm1,tr("New..."),this,SLOT(Uusi()),CTRL+Key_N); pop1->insertItem(pm2,tr("Open..."),this,SLOT(Avaa()),CTRL+Key_O); pop1->insertSeparator(); pop1->insertItem(pm3,tr("Save"),this,SLOT(SaveCurrent()),CTRL+Key_S,0); pop1->insertItem(pm3,tr("Save As..."),this,SLOT(Tallenna())); pop1->insertSeparator(); pop1->insertItem(pm10,tr("Print..."),this,SLOT(Print()),CTRL+Key_P); pop1->insertSeparator(); pop1->insertItem(pm4,tr("Preferences..."),this,SLOT(Prefs())); pop1->insertSeparator(); pop1->insertItem(tr("Quit"),this,SLOT(close())); menuBar()->insertItem(tr(" File "),pop1); pop2=new QPopupMenu(this); pop2->insertItem(pm16,tr("Cut"),this,SLOT(Cut()),CTRL+Key_X); pop2->insertItem(pm15,tr("Copy"),this,SLOT(Copy()),CTRL+Key_C); pop2->insertItem(pm17,tr("Paste"),this,SLOT(Paste()),CTRL+Key_V); pop2->insertSeparator(); pop2->insertItem(pm18,tr("Select All"),this,SLOT(SelectAll()),CTRL+Key_A); pop2->insertItem(pm19,tr("Clear"),this,SLOT(SelectNone())); pop2->insertSeparator(); pop2->insertItem(pm20,tr("Delete"),this,SLOT(DeleteFunction()),CTRL+Key_D); menuBar()->insertItem(tr(" Edit "),pop2); QPopupMenu *pop4=new QPopupMenu(this); QPopupMenu *pop5=new QPopupMenu(this); pop4->insertItem(tr("Upper Case"),pop5); pop4->insertSeparator(); pop4->insertItem(QChar(GREEK_alfa)+QString(" Alfa"),GREEK_alfa); pop4->insertItem(QChar(GREEK_beta)+QString(" Beta"),GREEK_beta); pop4->insertItem(QChar(GREEK_gamma)+QString(" Gamma"),GREEK_gamma); pop4->insertItem(QChar(GREEK_delta)+QString(" Delta"),GREEK_delta); pop4->insertItem(QChar(GREEK_epsilon)+QString(" Epsilon"),GREEK_epsilon); pop4->insertItem(QChar(GREEK_zeeta)+QString(" Zeeta"),GREEK_zeeta); pop4->insertItem(QChar(GREEK_eeta)+QString(" Eeta"),GREEK_eeta); pop4->insertItem(QChar(GREEK_theeta)+QString(" Theeta"),GREEK_theeta); pop4->insertItem(QChar(GREEK_ioota)+QString(" Ioota"),GREEK_ioota); pop4->insertItem(QChar(GREEK_kappa)+QString(" Kappa"),GREEK_kappa); pop4->insertItem(QChar(GREEK_lambda)+QString(" Lambda"),GREEK_lambda); pop4->insertItem(QChar(GREEK_myy)+QString(" Myy"),GREEK_myy); pop4->insertItem(QChar(GREEK_nyy)+QString(" Nyy"),GREEK_nyy); pop4->insertItem(QChar(GREEK_ksii)+QString(" Ksii"),GREEK_ksii); pop4->insertItem(QChar(GREEK_omikron)+QString(" Omikron"),GREEK_omikron); pop4->insertItem(QChar(GREEK_pii)+QString(" Pii"),GREEK_pii); pop4->insertItem(QChar(GREEK_rhoo)+QString(" Rhoo"),GREEK_rhoo); pop4->insertItem(QChar(GREEK_sigma)+QString(" Sigma"),GREEK_sigma); pop4->insertItem(QChar(GREEK_tau)+QString(" Tau"),GREEK_tau); pop4->insertItem(QChar(GREEK_ypsilon)+QString(" Ypsilon"),GREEK_ypsilon); pop4->insertItem(QChar(GREEK_fii)+QString(" Fii"),GREEK_fii); pop4->insertItem(QChar(GREEK_khii)+QString(" Khii"),GREEK_khii); pop4->insertItem(QChar(GREEK_psii)+QString(" Psii"),GREEK_psii); pop4->insertItem(QChar(GREEK_omega)+QString(" Omega"),GREEK_omega); pop5->insertItem(QChar(GREEK_ALFA)+QString(" Alfa"),GREEK_ALFA); pop5->insertItem(QChar(GREEK_BETA)+QString(" Beta"),GREEK_BETA); pop5->insertItem(QChar(GREEK_GAMMA)+QString(" Gamma"),GREEK_GAMMA); pop5->insertItem(QChar(GREEK_DELTA)+QString(" Delta"),GREEK_DELTA); pop5->insertItem(QChar(GREEK_EPSILON)+QString(" Epsilon"),GREEK_EPSILON); pop5->insertItem(QChar(GREEK_ZEETA)+QString(" Zeeta"),GREEK_ZEETA); pop5->insertItem(QChar(GREEK_EETA)+QString(" Eeta"),GREEK_EETA); pop5->insertItem(QChar(GREEK_THEETA)+QString(" Theeta"),GREEK_THEETA); pop5->insertItem(QChar(GREEK_IOOTA)+QString(" Ioota"),GREEK_IOOTA); pop5->insertItem(QChar(GREEK_KAPPA)+QString(" Kappa"),GREEK_KAPPA); pop5->insertItem(QChar(GREEK_LAMBDA)+QString(" Lambda"),GREEK_LAMBDA); pop5->insertItem(QChar(GREEK_MYY)+QString(" Myy"),GREEK_MYY); pop5->insertItem(QChar(GREEK_NYY)+QString(" Nyy"),GREEK_NYY); pop5->insertItem(QChar(GREEK_KSII)+QString(" Ksii"),GREEK_KSII); pop5->insertItem(QChar(GREEK_OMIKRON)+QString(" Omikron"),GREEK_OMIKRON); pop5->insertItem(QChar(GREEK_PII)+QString(" Pii"),GREEK_PII); pop5->insertItem(QChar(GREEK_RHOO)+QString(" Rhoo"),GREEK_RHOO); pop5->insertItem(QChar(GREEK_SIGMA)+QString(" Sigma"),GREEK_SIGMA); pop5->insertItem(QChar(GREEK_TAU)+QString(" Tau"),GREEK_TAU); pop5->insertItem(QChar(GREEK_YPSILON)+QString(" Ypsilon"),GREEK_YPSILON); pop5->insertItem(QChar(GREEK_FII)+QString(" Fii"),GREEK_FII); pop5->insertItem(QChar(GREEK_KHII)+QString(" Khii"),GREEK_KHII); pop5->insertItem(QChar(GREEK_PSII)+QString(" Psii"),GREEK_PSII); pop5->insertItem(QChar(GREEK_OMEGA)+QString(" Omega"),GREEK_OMEGA); menuBar()->insertItem(tr(" Symbols "),pop4); connect(pop4,SIGNAL(activated(int)),SLOT(SymbolSelected(int))); connect(pop5,SIGNAL(activated(int)),SLOT(SymbolSelected(int))); menuBar()->insertItem(tr(" Functions "),AppControl->FunctionMenu); QPopupMenu *pop6=new QPopupMenu(this); pop6->insertItem(tr("User manual"),this,SLOT(Help())); pop6->insertSeparator(); pop6->insertItem(tr("About..."),this,SLOT(About())); menuBar()->insertItem(tr(" Help "),pop6); tb1=new QToolButton(pm1,QString(tr("New")),QString::null,this,SLOT(Uusi()),ToolBarB); tb2=new QToolButton(pm2,QString(tr("Open")),QString::null,this,SLOT(Avaa()),ToolBarB); tb3=new QToolButton(pm3,QString(tr("Save")),QString::null,this,SLOT(Tallenna()),ToolBarB); ToolBarB->addSeparator(); tb4=new QToolButton(pm4,QString(tr("Preferences")),QString::null,this,SLOT(Prefs()),ToolBarB); // Removed MPL 3.0-Pre1 tb5=new QToolButton(pm10,QString(tr("Print")),QString::null,this,SLOT(Print()),ToolBarB); ToolBarB->addSeparator(); tb6=new QToolButton(pm15,QString(tr("Copy")),QString::null,this,SLOT(Copy()),ToolBarB); tb7=new QToolButton(pm16,QString(tr("Cut")),QString::null,this,SLOT(Cut()),ToolBarB); tb8=new QToolButton(pm17,QString(tr("Paste")),QString::null,this,SLOT(Paste()),ToolBarB); ToolBarB->addSeparator(); tb9=new QToolButton(pm18,QString(tr("Select All")),QString::null,this,SLOT(SelectAll()),ToolBarB); tb10=new QToolButton(pm19,QString(tr("Select none")),QString::null,this,SLOT(SelectNone()),ToolBarB); tb11=new QToolButton(pm20,QString(tr("Delete function")),QString::null,this,SLOT(DeleteFunction()),ToolBarB); ToolBarB->addSeparator(); tb12=new QToolButton(pm8,QString(tr("Zoom In")),QString::null,this,SLOT(NextZoomFactor()),ToolBarB); tb13=new QToolButton(pm9,QString(tr("Zoom Out")),QString::null,this,SLOT(PrevZoomFactor()),ToolBarB); ToolBarB->addSeparator(); tb21=new QToolButton(pm24,QString(tr("ImageEditor")),QString::null,this,SLOT(ImageEditor()),ToolBarB); ToolBarB->addSeparator(); tb18=new QToolButton(pm21,QString(tr("Graph frame (not operational)")),QString::null,this,SLOT(AddGraph()),ToolBarB); tb19=new QToolButton(pm22,QString(tr("Text frame")),QString::null,this,SLOT(FrameText()),ToolBarB); tb20=new QToolButton(pm23,QString(tr("Image frame")),QString::null,this,SLOT(FrameImage()),ToolBarB); ToolBarB->addSeparator(); tb17=new QToolButton(pm14,QString(tr("Calculate / Update")),QString::null,this,SLOT(Calculate()),ToolBarB); addToolBar(ToolBarB,0,Top,true); setDockEnabled(ToolBarB,Top,true); setToolBarsMovable(true); setDockEnabled(Left,true); } void MathWindow::ImageEditor() { Paper *pap=GetActivePaper(); if (pap) pap->SelectRect(MSG_IMAGEEDIT,TO_MAIN); } void MathWindow::resizeEvent( QResizeEvent *R ) { } void MathWindow::Uusi() { CurrentDocumentName=""; Paper *pap=GetActivePaper(); if (!pap) { NewPage(); pap=GetActivePaper(); } if (pap) pap->New(); // Clear and setup page } void MathWindow::Avaa() { QFileDialog *dia=new QFileDialog(home_path); QString s( dia->getOpenFileName(home_path, "MathPlanner (*.mpl2)", this ) ); if ( s.isEmpty() ) { delete dia; return; } Open(s); delete dia; } void MathWindow::Tallenna() { if (GetActivePaper()==NULL) return; // Nothing to save QFileDialog *dia=new QFileDialog(home_path); QString fileName = dia->getSaveFileName(home_path+QString("/.mpl2"), "MathPlanner (*.mpl2)", this ); if ( !fileName.isNull() ) { Save(fileName); } delete dia; } void MathWindow::SaveCurrent() { if (GetActivePaper()==NULL) return; // Nothing to save if (!CurrentDocumentName.isEmpty()) Save(CurrentDocumentName); } void MathWindow::Rescue() { // This is called when application is closed if (GetActivePaper()==NULL) return; // Nothing to save Save(config_path+QString("/MathPlanner-autosave.mpl2")); } void MathWindow::FrameText() { Paper *pap=GetActivePaper(); if (pap) { QApplication::clipboard()->setData(new QTextDrag(QString("_TextFrame"))); pap->SelectPosition(MSG_TEXTFRAME,TO_MAIN); } } void MathWindow::FrameImage() { if (GetActivePaper()==NULL) return; QString s; QFileDialog *dia=new QFileDialog(home_path); if (DropDefaultImage) s=dia->getOpenFileName(config_path+"/temp.png", "Images (*.png *.xpm *.jpg)", this ); else s=dia->getOpenFileName(home_path, "Images (*.png *.xpm *.jpg)", this ); DropDefaultImage=false; if ( s.isEmpty() ) { delete dia; return; } delete dia; Paper *pap=GetActivePaper(); if (pap) { QImage image(s); QApplication::clipboard()->setData(new QImageDrag(image)); pap->SelectPosition(MSG_IMAGEFRAME,TO_MAIN); } } void MathWindow::NextZoomFactor() { } void MathWindow::PrevZoomFactor() { } /* void MathWindow::None() { } */ void MathWindow::Help() { QString exe(doc_dir+"/Doc.htm"); QProcess *proc= new QProcess(this); QStringList list; list=QStringList::split(QString(" "),webbrowser); list<setArguments(list); if (!proc->start()) { ErrorReport("Webbrowser","Unable to execute program: "+webbrowser+"\nFile: "+exe); } } void MathWindow::Prefs() { AppControl->Setup->Show(); } void MathWindow::Print() { QPrinter *printer=new QPrinter(); Paper *pap=GetActivePaper(); if (pap) { pap->UnFocusCurrentFunction(); if ( printer->setup( this ) ) { QPainter paint; paint.begin(printer); if (AppControl->Prefs->Int("PrintGrid")) pap->DrawGrid(&paint); paint.setWindow(pap->Scale(100,AppControl->Prefs->Rect("PaperWidth","PaperHeight"))); AppControl->SetModeFlag(MPL_MF_ALPHA,AppControl->Prefs->Int("PrintAlpha")); AppControl->SetModeFlag(MPL_MF_BW,AppControl->Prefs->Int("PrintInBW")); pap->Draw(&paint,QRect(0,0,0,0),true); AppControl->SetModeFlag(MPL_MF_BW,false); AppControl->SetModeFlag(MPL_MF_ALPHA,true); paint.end(); } } delete printer; } void MathWindow::SymbolSelected(int id) { QKeyEvent key(QEvent::KeyPress,0,id,0,QString(QChar(id))); QApplication::sendEvent(focusWidget(),&key); } void MathWindow::AddGraph() { //Paper *pap=GetActivePaper(); //if (pap) pap->AddGraph(); } void MathWindow::DragObject() //Global copy { Paper *pap=GetActivePaper(); if (pap) pap->CopyObject(); } void MathWindow::DropObject() //Global paste { Paper *pap=GetActivePaper(); if (pap) pap->PasteObject(); } void MathWindow::DeleteFunction() { Paper *pap=GetActivePaper(); if (pap) pap->DeleteCurrentFunction(); } void MathWindow::SelectNone() { Paper *pap=GetActivePaper(); if (pap) pap->UnFocusCurrentFunction(); } void MathWindow::SelectAll() { POSTMESSAGE(MSG_SELECTALL,TO_FRAME); } void MathWindow::Copy() { POSTMESSAGE(MSG_COPY,TO_FRAME); } void MathWindow::Paste() { POSTMESSAGE(MSG_PASTE,TO_FRAME); } void MathWindow::Cut() { POSTMESSAGE(MSG_CUT,TO_FRAME); } int MathWindow::GetFreePage() { int i; for(i=0;i<32;i++) { if (paper[i]==NULL) return(i); } return(-1); } QPoint MathWindow::PaperPosition(int i) // What is this ??????? Multible pages { int w=AppControl->Prefs->Int("PaperWidth")+10; int h=AppControl->Prefs->Int("PaperHeight")+10; int hp=i/4; int wp=i-(hp*4); return(QPoint(10+wp*w,10*hp*h)); } void MathWindow::About() { QMessageBox::about( this, QString("About MathPlanner ")+QString(_MPL_VERSION)+QString(" ")+QString(_MPL_DATE), "MathPlanner Is created by Jarmo Nikkanen. MathPlanner can be freely\n" "distributed under GPL. You should have received a copy of the GNU \n" "General Public License with this program.\n\n" "Comments, Ideas, Bug reports are received at MathPlanner home page\n\n" "http://koti.mbnet.fi/jarmonik\n\n" "or via e-mail: jarmonik@mbnet.fi\n\n" "I hope you enjoy using this program\n"); } void MathWindow::NewPage() { Bug("Adding a Page"); int i=GetFreePage(); if (i>=0) { Active_paper=i; paper[i]=new Paper(scrollview->viewport(),AppControl->Prefs->Rect("PaperWidth","PaperHeight"),AppControl); scrollview->addChild(paper[i]); paper[i]->show(); } } void MathWindow::Calculate() // This is not used { int i; for(i=0;i<32;i++) { if (paper[i]) { paper[i]->UpDate(); } } } void MathWindow::Save(QString file) { // NOTE: Must save all pages in future if (GetActivePaper()==NULL) return; // Nothing to save datastore=new DataStorage(); datastore->AddInt("version",310); DataStorage *ds=AppControl->Prefs->Build(); datastore->AddDataStorage("prefs2",ds,false); delete ds; ds=paper[0]->BuildStorage(); datastore->AddDataStorage("page#0",ds,false); delete ds; char *rawdata; int size; datastore->Record(&rawdata,&size,true); QFile f(file); f.open( IO_WriteOnly ); f.writeBlock(rawdata,(unsigned int)size); free(rawdata); f.close(); delete datastore; CurrentDocumentName=file; setCaption(ApplicationCaption+QString(" / ")+QFileInfo(f).fileName()); AppControl->DocumentChanged(false); } void MathWindow::Open(QString file) { DataStorage *ds; char *rawdata=0; bool loadprefs=true; Bug("** Opening File 1**"); Uusi(); // Intialize New page. Clear if exists Bug("** Opening File 2**"); QFile f(file); if (f.exists()==false) { if (file!=config_path+QString("/MathPlanner-autosave.mpl2")) ErrorReport("Open File","Requested file not found..."); } else { f.open( IO_ReadOnly ); int siz=f.size(); rawdata=(char *)malloc(siz+16); if (!rawdata) ErrorReport("Open","Memory error"); f.readBlock(rawdata,siz); datastore=new DataStorage(); datastore->Build(rawdata,siz); free(rawdata); if (datastore->Contains("prefs2") && loadprefs) { ds=new DataStorage(); datastore->ReadDataStorage("prefs2",ds); AppControl->Prefs->Record(ds); delete ds; } ds=new DataStorage(); datastore->ReadDataStorage("page#0",ds); paper[0]->Record(ds); delete ds; delete datastore; AppControl->DocumentChanged(false); CurrentDocumentName=file; setCaption(ApplicationCaption+QString(" / ")+QFileInfo(f).fileName()); // SEND UPDATE MESSAGES DataStorage *msg=MESSAGE(MSG_CONFIGURATION); PostMessage(msg,TO_APPCONTROL); PostMessage(msg,TO_MAIN); paper[0]->UpDate(); Bug("***** Loading Completed *****"); } }