/* * MathPlanner 3.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 #include "Datastore.h" #include #include "Math.h" #include "Paper.h" #include "Error.h" #include "DragDrop.h" #include "AppControl.h" #include "FrameObjects.h" //#include "PaperGraph.h" #include "PaperImage.cpp" #include "PaperQPicture.cpp" #include "PaperTextEdit.cpp" // *************************************************************** // DataStorageDragObject // *************************************************************** DataDragObject::DataDragObject(DataStorage *data,QWidget *w) :QDragObject(w,"Math") { datastore=data; } DataDragObject::~DataDragObject() { if (datastore) delete datastore; } const char *DataDragObject::format(int x) const { if (x==0) return("MathPlannerData"); return(0); } bool DataDragObject::canDecode(QMimeSource *e) { return(e->provides("MathPlannerData")); } QByteArray DataDragObject::encodedData(const char *mime) const { char *rawdata; int size; QByteArray array; if (strcmp(mime,"MathPlannerData")==0) { datastore->Record(&rawdata,&size,false); array.setRawData((const char *)rawdata,size); } return(array); } DataStorage* DataDragObject::Decode(QMimeSource *event) { QByteArray ar; if (event->provides("MathPlannerData")) { ar=event->encodedData("MathPlannerData"); char *rawdata=ar.data(); int size=ar.size(); if (ar.isEmpty()) return(NULL); DataStorage *data=new DataStorage(); data->Build(rawdata,size); return(data); } return(NULL); } // *************************************************************** // MathFrameDragObject // *************************************************************** MathDragObject::MathDragObject(QWidget *w,MathFrame *f) :QDragObject(w,"Math") { QPoint pos; datastore=NULL; if (f) { datastore=f->BuildStorage(); QPixmap pixmap(f->Bounds().size()); pixmap.fill(QColor(qRgba(255,255,255,255))); pos=f->Position(); f->UnFocus(); f->CalculateBounds(); f->MoveTo(QPoint(0,f->Ascent())); /* if (f->ObjectType==PAPER_OBJECT_GRAPH) { PaperGraph *g=(PaperGraph *)f; g->DrawFunctions(); } */ QPainter paint; paint.begin(&pic); paint.setWindow(f->Bounds()); f->Draw(&paint); paint.end(); paint.begin(&pixmap); f->Draw(&paint); paint.end(); f->MoveTo(pos); image=pixmap; } } MathDragObject::~MathDragObject() { if (datastore) delete datastore; } const char *MathDragObject::format(int x) const { if (x==0) return("MathPlanner"); if (x==1) return("QPicture"); if (x==2) return("image/png"); if (x==3) return("image/bmp"); if (x==4) return("image/pgm"); if (x==5) return("image/pbm"); if (x==6) return("image/ppm"); if (x==7) return("image/xbm"); if (x==8) return("image/xpm"); return(0); } bool MathDragObject::canDecode(QMimeSource *e) { return(e->provides("MathPlanner")); } QByteArray MathDragObject::encodedData(const char *mime) const { QByteArray array; if (strcmp(mime,"MathPlanner")==0) { char *rawdata; int size; datastore->Record(&rawdata,&size,true); array.setRawData((const char *)rawdata,size); return(array); } else if (strcmp(mime,"QPicture")==0) { array.setRawData(pic.data(),pic.size()); return(array); } else { ImageDragObject ido(image); if (ido.provides(mime)) { array=ido.encodedData(mime); } return(array); } } DataStorage* MathDragObject::Decode(QMimeSource *event) { QByteArray ar; if (event->provides("MathPlanner")) { ar=event->encodedData("MathPlanner"); char *rawdata=ar.data(); int size=ar.size(); if (ar.isEmpty()) return(NULL); DataStorage *data=new DataStorage(); data->Build(rawdata,size); return(data); } return(NULL); } // *************************************************************** // ImageDragObject // *************************************************************** ImageDragObject::ImageDragObject(QImage i,QWidget *w,const char *name) :QDragObject(w,name) { image=i; } ImageDragObject::ImageDragObject(QWidget *w,const char *name) :QDragObject(w,name) { } ImageDragObject::~ImageDragObject() { } const char *ImageDragObject::format(int x) const { if (x==0) return("image/png"); // use this first if (x==1) return("image/bmp"); if (x==2) return("image/pgm"); if (x==3) return("image/pbm"); if (x==4) return("image/ppm"); if (x==5) return("image/xbm"); if (x==6) return("image/xpm"); return(NULL); } bool ImageDragObject::canDecode(QMimeSource *e) { if (e->provides("image/png")) return(true); if (e->provides("image/bmp")) return(true); if (e->provides("image/pgm")) return(true); if (e->provides("image/pbm")) return(true); if (e->provides("image/ppm")) return(true); if (e->provides("image/xbm")) return(true); if (e->provides("image/xpm")) return(true); return(false); } QByteArray ImageDragObject::encodedData(const char *mime) const { QByteArray array; char Output[5]; bool encode=false; if (strcmp(mime,"image/pbm")==0) strcpy(Output,"PBM"),encode=true; if (strcmp(mime,"image/bmp")==0) strcpy(Output,"BMP"),encode=true; if (strcmp(mime,"image/pgm")==0) strcpy(Output,"PGM"),encode=true; if (strcmp(mime,"image/ppm")==0) strcpy(Output,"PPM"),encode=true; if (strcmp(mime,"image/xbm")==0) strcpy(Output,"XBM"),encode=true; if (strcmp(mime,"image/png")==0) strcpy(Output,"PNG"),encode=true; if (strcmp(mime,"image/xpm")==0) strcpy(Output,"XPM"),encode=true; if (encode) { QBuffer buffer(array); buffer.open(IO_WriteOnly); QImageIO io(&buffer,Output); io.setImage(image); if (io.write()==false) { } } return(array); } QImage ImageDragObject::Decode(QMimeSource *event) { QByteArray array; char Output[5],Input[20]; bool encode=false; if (event->provides("image/png")) strcpy(Output,"PNG"),strcpy(Input,"image/png"),encode=true; else if (event->provides("image/xpm")) strcpy(Output,"XPM"),strcpy(Input,"image/xpm"),encode=true; else if (event->provides("image/pbm")) strcpy(Output,"PBM"),strcpy(Input,"image/pbm"),encode=true; else if (event->provides("image/bmp")) strcpy(Output,"BMP"),strcpy(Input,"image/bmp"),encode=true; else if (event->provides("image/pgm")) strcpy(Output,"PGM"),strcpy(Input,"image/pgm"),encode=true; else if (event->provides("image/ppm")) strcpy(Output,"PPM"),strcpy(Input,"image/ppm"),encode=true; else if (event->provides("image/xbm")) strcpy(Output,"XBM"),strcpy(Input,"image/xbm"),encode=true; if (encode) { array=event->encodedData(Input); QBuffer buffer(array); buffer.open(IO_ReadOnly); QImageIO io(&buffer,Output); if (io.read()) return(io.image()); } QImage i; return(i); } QImage ImageDragObject::Decode(QByteArray array,const char *mime) { char Output[5]; bool encode=false; if (strcmp(mime,"image/pbm")==0) strcpy(Output,"PBM"),encode=true; if (strcmp(mime,"image/bmp")==0) strcpy(Output,"BMP"),encode=true; if (strcmp(mime,"image/pgm")==0) strcpy(Output,"PGM"),encode=true; if (strcmp(mime,"image/ppm")==0) strcpy(Output,"PPM"),encode=true; if (strcmp(mime,"image/xbm")==0) strcpy(Output,"XBM"),encode=true; if (strcmp(mime,"image/xpm")==0) strcpy(Output,"XPM"),encode=true; if (strcmp(mime,"image/png")==0) strcpy(Output,"PNG"),encode=true; if (encode && array.isNull()==false) { QBuffer buffer(array); buffer.open(IO_ReadOnly); QImageIO io(&buffer,Output); if (io.read()) return(io.image()); } QImage i; return(i); } // *************************************************************** // QPictureDragObject // *************************************************************** QPictureDragObject::QPictureDragObject(QPicture *i,QWidget *w,const char *name) :QDragObject(w,name) { void *p=(void *)malloc(i->size()); memcpy(p,(const void *)i->data(),i->size()); array.assign((const char *)p,i->size()); } QPictureDragObject::QPictureDragObject(QWidget *w,const char *name) :QDragObject(w,name) { } QPictureDragObject::~QPictureDragObject() { } const char *QPictureDragObject::format(int x) const { if (x==0) return("QPicture"); //if (x==1) return("image/svg+xml"); //if (x==2) return("image/svg"); return(NULL); } bool QPictureDragObject::canDecode(QMimeSource *e) { if (e->provides("QPicture")) return(true); //if (e->provides("image/svg+xml")) return(true); //if (e->provides("image/svg")) return(true); return(false); } QByteArray QPictureDragObject::encodedData(const char *mime) const { if (strcmp(mime,"QPicture")==0) return(array); QByteArray a; return(a); } QPicture *QPictureDragObject::Decode(QMimeSource *event) { QByteArray array; QPicture *pi=new QPicture(); bool encode=false; if (event->provides("QPicture")) encode=true; if (encode) { array=event->encodedData("QPicture"); pi->setData(array.data(),array.size()); } return(pi); } QPicture *QPictureDragObject::Decode(QByteArray array) { QPicture *pi=new QPicture(); pi->setData(array.data(),array.size()); return(pi); }