Commit c4128a90 authored by Stefan Gerlach's avatar Stefan Gerlach

basic open/save

parent d1198255
2008-03-30 started with open/save projects
2008-03-22 Worksheet/Spreadsheet title
Spreadsheet selections
plot from spreadsheet
......
......@@ -101,13 +101,13 @@ more things :
menu
...
Plots
save/open
surface
qwt plot
3d ?
MainWin
Print Preview (see Qt textedit demo)
Export to eps, svg, ...
open, save, save as
dynamic menus (sheets + data sets)
View: autoscale, zoom, etc.
analysis functions
......
......@@ -50,7 +50,7 @@ int main (int argc, char *argv[]) {
// kDebug()<<"GSL_VERSION > 1.8"<<endl;
//#endif
MainWin* window = new MainWin();
MainWin* window = new MainWin(0,filename);
window->show();
// sleep(1); // to see splash screen
if(splash)
......
......@@ -16,6 +16,7 @@
#include <KStatusBar>
#include <KLocale>
#include <KDebug>
#include <KFilterDev>
#include "ProjectDialog.h"
#include "FunctionDialog.h"
#include "TitleDialog.h"
......@@ -23,22 +24,35 @@
#include "PlotDialog.h"
#include "WorksheetDialog.h"
#include "sheettype.h"
#include "pixmaps/pixmap.h"
MainWin::MainWin(QWidget *parent)
MainWin::MainWin(QWidget *parent, QString filename)
: KXmlGuiWindow(parent)
{
mdi = new QMdiArea;
setCentralWidget(mdi);
setCaption("LabPlot "LVERSION);
//setCaption("LabPlot "LVERSION+i18n(" : ")+project->Filename());
setupActions();
openNew();
// commandline filename
if(!filename.isEmpty() && !QFile::exists(filename)) {
int ret = KMessageBox::warningContinueCancel( this,
i18n( "Could not open file \'%1\'!").arg(filename),i18n("Warning"));
if (ret==KMessageBox::Cancel)
exit(-1);
}
else if (filename.contains(".lml") || filename.contains(".xml")) {
open(filename);
}
spreadsheetmenu = static_cast<QMenu*> (guiFactory()->container("spreadsheet",this));
connect(spreadsheetmenu, SIGNAL(aboutToShow()), SLOT(SpreadsheetMenu()) );
statusBar()->showMessage(i18n("Welcome to LabPlot ")+LVERSION);
}
void MainWin::setupActions() {
......@@ -47,9 +61,7 @@ void MainWin::setupActions() {
action = KStandardAction::open(this, SLOT(open()),actionCollection());
action->setEnabled(false);
action = KStandardAction::save(this, SLOT(save()),actionCollection());
action->setEnabled(false);
action = KStandardAction::saveAs(this, SLOT(saveAs()),actionCollection());
action->setEnabled(false);
action = KStandardAction::print(this, SLOT(print()),actionCollection());
action = KStandardAction::printPreview(this, SLOT(printPreview()),actionCollection());
action->setEnabled(false);
......@@ -159,7 +171,7 @@ bool MainWin::warnModified() {
i18n("Save Project"));
switch (want_save) {
case KMessageBox::Yes:
// TODO : saveXML();
save();
break;
case KMessageBox::No:
break;
......@@ -169,7 +181,6 @@ bool MainWin::warnModified() {
}
}
project->setChanged(false);
return 0;
}
......@@ -205,8 +216,8 @@ void MainWin::updateGUI() {
void MainWin::openNew() {
kDebug()<<"MainWin::New()"<<endl;
// // if(warnModified()) return;
//
if(warnModified()) return;
mdi->closeAllSubWindows();
updateGUI();
// /* gvpart=0;
......@@ -221,7 +232,170 @@ void MainWin::openNew() {
// defining_panzoom=0;
// */
project = new Project();
project->setChanged(true);
}
void MainWin::open(QString filename) {
kDebug()<<filename<<endl;
if (filename.isEmpty())
filename = QFileDialog::getOpenFileName(this,i18n("Open project"),QString::null,
i18n("LabPlot Projects (*.lml *.lml.gz *.lml.bz2 *.LML *.LML.GZ *.LML.BZ2)"));
if(filename.isEmpty())
return;
QIODevice *file = KFilterDev::deviceForFile(filename,QString::null,true);
if (file==0) file = new QFile(filename);
if ( file->open( QIODevice::ReadOnly | QFile::Text) == 0) {
KMessageBox::error(this, i18n("Sorry. Could not open file for reading!"));
return;
}
openNew();
openXML(file);
project->setFilename(filename);
project->setChanged(false);
//recent->addURL(fn);
setCaption("LabPlot "LVERSION+i18n(" : ")+project->Filename());
}
void MainWin::openXML(QIODevice *file) {
QDomDocument doc;
kDebug()<<" reading ..."<<endl;
if(doc.setContent(file) != true ) {
kDebug()<<"ERROR: reading file content"<<endl;
return;
}
file->close();
QDomElement root = doc.documentElement();
kDebug()<<"ROOT TAG = "<<root.tagName()<<endl;
kDebug()<<"ROOT ATTR version = "<<root.attribute("version")<<endl;
Q_ASSERT(root.tagName() == "LabPlot");
//Unused : project->setFilename(fn);
project->setLabPlot(root.attribute("version"));
QDomNode node = root.firstChild();
while(!node.isNull()) {
QDomElement e = node.toElement();
// kDebug()<<"TAG = "<<e.tagName()<<endl;
// kDebug()<<"TEXT = "<<e.text()<<endl;
if(e.tagName() == "SpeedMode")
kDebug()<<"Speedmode ="<<(bool)e.text().toInt()<<endl;
// speedmode = (bool)e.text().toInt();
else if(e.tagName() == "Project")
project->open(e.firstChild());
else if(e.tagName() == "Worksheet") {
Worksheet *p = newWorksheet();
p->open(e.firstChild());
}
else if(e.tagName() == "Spreadsheet") {
Spreadsheet *s = newSpreadsheet();
s->open(e.firstChild());
}
node = node.nextSibling();
}
}
void MainWin::save(QString filename) {
if (filename.isEmpty() ) {
if(project->Filename().isEmpty()) {
saveAs(); // need a file name
return;
}
else
filename = project->Filename();
}
kDebug()<<filename<<endl;
if(project->Filename() == filename && project->Changed() == false) {
kDebug()<<"no changes to be saved"<<endl;
return;
}
QIODevice *xmlfile = KFilterDev::deviceForFile(filename,QString::null,true);
if (xmlfile==0) xmlfile = new QFile(filename);
saveXML(xmlfile);
project->setFilename(filename);
setCaption("LabPlot "LVERSION+i18n(" : ")+project->Filename());
}
void MainWin::saveXML(QIODevice *file) {
kDebug()<<endl;
QDomDocument doc("LabPlot");
QString header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
QString doctype = "<!DOCTYPE LabPlotXML>\n";
doc.setContent(header+doctype);
QDomElement root = doc.createElement( "LabPlot" );
root.setAttribute("version",QString(LVERSION));
doc.appendChild( root );
QDomElement tag = project->save(doc);
root.appendChild( tag );
// tag = doc.createElement( "SpeedMode" );
// root.appendChild( tag );
// QDomText t = doc.createTextNode( QString::number(speedmode));
// tag.appendChild( t );
QList<QMdiSubWindow *> list = mdi->subWindowList();
for (int i = 0; i < list.size(); i++ ) {
QMdiSubWindow *subWindow = list[i];
kDebug()<<" Saving sheet "<<i<<endl;
Worksheet *w = (Worksheet *) subWindow->widget();
if(w && w->sheetType() == WORKSHEET) {
kDebug()<<" Saving worksheet "<<i<<endl;
QDomElement wtag = w->save(doc);
root.appendChild(wtag);
}
else {
Spreadsheet *s = (Spreadsheet *) subWindow->widget();
if( s==0 || s->sheetType() != SPREADSHEET) {
kDebug()<<"ERROR: unknown sheet found!"<<endl;
return;
}
kDebug()<<" Saving spreadsheet "<<i<<endl;
QDomElement stag = s->save(doc);
root.appendChild(stag);
}
}
if(file->open(QIODevice::WriteOnly | QFile::Text)) {
QTextStream ts(file);
doc.save(ts,4);
ts<<endl;
statusBar()->showMessage(i18n("Project saved"));
file->close();
project->setChanged(false);
}
else
KMessageBox::error(this, i18n("Sorry. Could not open file for writing!"));
}
void MainWin::saveAs() {
kDebug()<<endl;
QString fn = QFileDialog::getSaveFileName(this, i18n("Save project"),QString::null,
i18n("LabPlot Projects (*.lml *.lml.gz *.lml.bz2 *.LML *.LML.GZ *.LML.BZ2)"));
if(fn.isEmpty()) // "Cancel"
return;
if(fn.contains(QString(".lml"),Qt::CaseInsensitive) == false)
fn.append(".lml");
QFile file(fn);
if ( file.exists() ) {
int answer = KMessageBox::warningYesNoCancel( this,
i18n( "Overwrite\n\'%1\'?" ).arg(fn), i18n("Save Project"));
if (answer == KMessageBox::Cancel)
return;
else if (answer == KMessageBox::No)
saveAs();
}
save(fn);
}
void MainWin::print() {
......
......@@ -14,7 +14,7 @@ class MainWin : public KXmlGuiWindow
{
Q_OBJECT
public:
MainWin(QWidget *parent=0);
MainWin(QWidget *parent=0,QString filename=0);
int activeSheetIndex() const;
QMdiArea* getMdi() const { return mdi; }
Spreadsheet* activeSpreadsheet() const; //!< get active spreadsheet
......@@ -26,6 +26,8 @@ public:
void updateGUI(); //!< update GUI of main window
void updateSheetList(); //!< update dynamic sheet list menu
void updateSetList(); //!< update dynamic set list menu
void openXML(QIODevice *file); //!< do the actual opening
void saveXML(QIODevice *file); //!< do the actual saving
void addSet(Set *g, int sheet, PlotType ptype);
private:
QMdiArea *mdi;
......@@ -37,6 +39,9 @@ private:
public slots:
Spreadsheet* newSpreadsheet();
Worksheet* newWorksheet();
void save(QString filename=0); //!< save project (.lml format)
void open(QString filename); //!< open project (.lml format)
void saveAs(); //!< save as different file name (.lml format)
private slots:
void openNew();
void print();
......
......@@ -17,7 +17,7 @@ Project::Project()
changed=false;
}
QDomElement Project::saveXML(QDomDocument doc) {
QDomElement Project::save(QDomDocument doc) {
QDomElement ptag = doc.createElement( "Project" );
QDomElement tag = doc.createElement( "Title" );
......@@ -57,7 +57,7 @@ QDomElement Project::saveXML(QDomDocument doc) {
return ptag;
}
void Project::openXML(QDomNode node) {
void Project::open(QDomNode node) {
while(!node.isNull()) {
QDomElement e = node.toElement();
// kDebug()<<"PROJECT TAG = "<<e.tagName()<<endl;
......
......@@ -14,8 +14,8 @@ class Project
{
public:
Project();
QDomElement saveXML(QDomDocument doc);
void openXML(QDomNode node);
QDomElement save(QDomDocument doc);
void open(QDomNode node);
ACCESS(QString, filename, Filename);
ACCESS(int, version, Version);
ACCESS(QString, labplot, LabPlot);
......
......@@ -150,6 +150,94 @@ void Spreadsheet::Menu(QMenu *menu) {
connect(action, SIGNAL(triggered()),mw, SLOT(newSpreadsheet()));
}
QDomElement Spreadsheet::save(QDomDocument doc) {
kDebug()<<endl;
QDomElement sstag = doc.createElement( "Spreadsheet" );
QDomElement tag = doc.createElement( "Position" );
tag.setAttribute("x",QString::number(parentWidget()->pos().x()));
tag.setAttribute("y",QString::number(parentWidget()->pos().y()));
sstag.appendChild( tag );
tag = doc.createElement( "Size" );
tag.setAttribute("rows",QString::number(rowCount()));
tag.setAttribute("cols",QString::number(columnCount()));
sstag.appendChild( tag );
tag = doc.createElement( "Title" );
sstag.appendChild( tag );
QDomText t = doc.createTextNode( windowTitle() );
tag.appendChild( t );
tag = doc.createElement( "Notes" );
sstag.appendChild( tag );
t = doc.createTextNode( notes );
tag.appendChild( t );
/* tag = doc.createElement( "Datafile" );
sstag.appendChild( tag );
t = doc.createTextNode( datafile );
tag.appendChild( t );
*/
// header
for(int i=0;i < columnCount();i++) {
tag = doc.createElement( "Column" );
tag.setAttribute("nr",QString::number(i));
sstag.appendChild( tag );
t = doc.createTextNode( columnHeader(i) );
tag.appendChild( t );
}
// data
for(int i=0;i < rowCount();i++) {
for(int j=0;j < columnCount();j++) {
if(!text(i,j).isEmpty()) { // only filled cells
tag = doc.createElement( "Cell" );
tag.setAttribute("row",QString::number(i));
tag.setAttribute("col",QString::number(j));
// tag.setAttribute("masked",QString::number(((LTableItem *)table->item(i,j))->Masked()));
sstag.appendChild( tag );
t = doc.createTextNode( text(i,j) );
tag.appendChild( t );
}
}
}
return sstag;
}
void Spreadsheet::open(QDomNode node) {
kDebug()<<endl;
while(!node.isNull()) {
QDomElement e = node.toElement();
// kDebug()<<"SS TAG = "<<e.tagName()<<endl;
// kDebug()<<"SS TEXT = "<<e.text()<<endl;
if(e.tagName() == "Position") {
int px = e.attribute("x").toInt();
int py = e.attribute("y").toInt();
parentWidget()->move(QPoint(px,py));
if(px<0 || py<0) { // fullscreen
mw->getMdi()->cascadeSubWindows();
showMaximized();
}
}
else if(e.tagName() == "Size") {
setRowCount(e.attribute("rows").toInt());
setColumnCount(e.attribute("cols").toInt());
}
else if(e.tagName() == "Title")
setTitle(e.text());
else if(e.tagName() == "Notes")
notes = e.text();
// else if(e.tagName() == "Datafile")
// datafile = e.text();
else if(e.tagName() == "Column")
model()->setHeaderData(e.attribute("nr").toInt(),Qt::Horizontal,e.text());
else if(e.tagName() == "Cell") {
// item->setMasked(e.attribute("masked").toInt());
setText(e.attribute("row").toInt(), e.attribute("col").toInt(),e.text());
}
node = node.nextSibling();
}
}
void Spreadsheet::setTitle(QString title) {
kDebug()<<"title ="<<title<<endl;
bool ok=true;
......
......@@ -3,6 +3,7 @@
#include <QWidget>
#include <QMenu>
#include <QDomElement>
#include <QTableView>
#include "TableModel.h"
......@@ -19,6 +20,9 @@ public:
~Spreadsheet();
SheetType sheetType() const { return type; }
void resetHeader(int from=0);
QDomElement save(QDomDocument doc);
void open(QDomNode node);
void addSet(Set *set);
QString columnName(int col) const;
void setColumnName(int col, QString name);
......
......@@ -6,6 +6,7 @@
#include <KConfigGroup>
#include <QPrinter>
#include <QPrintDialog>
#include <KInputDialog>
#include "Worksheet.h"
#include "MainWin.h"
#include "Plot2DSimple.h"
......@@ -45,6 +46,170 @@ void Worksheet::paintEvent(QPaintEvent *) {
draw(&painter,width(),height());
}
QDomElement Worksheet::save(QDomDocument doc) {
kDebug()<<endl;
QDomElement wstag = doc.createElement( "Worksheet" );
wstag.setAttribute("api",QString::number(api));
// wstag.setAttribute("nr_plots",QString::number(nr_plots));
QDomElement tag = doc.createElement( "Position" );
tag.setAttribute("x",QString::number(parentWidget()->pos().x()));
tag.setAttribute("y",QString::number(parentWidget()->pos().y()));
wstag.appendChild( tag );
tag = doc.createElement( "Size" );
tag.setAttribute("width",QString::number(width()));
tag.setAttribute("height",QString::number(height()));
wstag.appendChild( tag );
tag = doc.createElement( "Title" );
wstag.appendChild( tag );
QDomText t = doc.createTextNode( windowTitle() );
tag.appendChild( t );
// TODO
/* tag = doc.createElement( "TitleEnabled" );
wstag.appendChild( tag );
t = doc.createTextNode( QString::number(title_enabled) );
tag.appendChild( t );
tag = doc.createElement( "Background" );
wstag.appendChild( tag );
t = doc.createTextNode( background.color().name() );
tag.appendChild( t );
tag = doc.createElement( "Brush" );
wstag.appendChild( tag );
t = doc.createTextNode( QString::number(background.style()) );
tag.appendChild( t );
tag = doc.createElement( "Timestamp" );
wstag.appendChild( tag );
QDomText t = doc.createTextNode( QString::number(timestamp.toTime_t()) );
tag.appendChild( t );
tag = doc.createElement( "TimestampEnabled" );
wstag.appendChild( tag );
t = doc.createTextNode( QString::number(timestamp_enabled) );
tag.appendChild( t );
tag = doc.createElement( "DrawObjectsFirst" );
wstag.appendChild( tag );
t = doc.createTextNode( QString::number(draw_objects_first) );
tag.appendChild( t );
*/
// drawing objects
//TODO
/* kDebug()<<" saving drawing objects"<<endl;
for(int i=0;i<NR_OBJECTS;i++) {
// only save used objects
if(! label[i]->Title().isEmpty()) {
tag = label[i]->saveXML(doc);
wstag.appendChild(tag);
}
Point start = line[i]->startPoint() ,end = line[i]->endPoint();
if(fabs(start.X()-end.X())>1.0e-6 || fabs(start.Y()-end.Y())>1.0e-6) {
tag = line[i]->saveXML(doc);
wstag.appendChild(tag);
}
start = rect[i]->startPoint();
end = rect[i]->endPoint();
if(fabs(start.X()-end.X())>1.0e-6 || fabs(start.Y()-end.Y())>1.0e-6) {
tag = rect[i]->saveXML(doc);
wstag.appendChild(tag);
}
start = rect[i]->startPoint();
end = rect[i]->endPoint();
if(fabs(start.X()-end.X())>1.0e-6 || fabs(start.Y()-end.Y())>1.0e-6) {
tag = ellipse[i]->saveXML(doc);
wstag.appendChild(tag);
}
if(! image[i]->Name().isEmpty()) {
tag = image[i]->saveXML(doc);
wstag.appendChild(tag);
}
}
*/
// all plots
// TODO
/* for (int i=0;i < plot.size();i++) {
kDebug()<<" saving Plot "<<i<<endl;
tag = plot[i]->savePlotXML(doc);
wstag.appendChild(tag);
}
*/
return wstag;
}
void Worksheet::open(QDomNode node) {
kDebug()<<endl;
int tmpnr_plots=0;
// int nr_label=0, nr_line=0, nr_rect=0, nr_ellipse=0, nr_image=0;
while(!node.isNull()) {
QDomElement e = node.toElement();
kDebug()<<"WS TAG = "<<e.tagName()<<endl;
kDebug()<<"WS TEXT = "<<e.text()<<endl;
if(e.tagName() == "Position") {
int px = e.attribute("x").toInt();
int py = e.attribute("y").toInt();
parentWidget()->move(QPoint(px,py));
if(px<0 || py<0) { // fullscreen
mw->getMdi()->cascadeSubWindows();
showMaximized();
}
}
else if(e.tagName() == "Size") {
int X = e.attribute("width").toInt();
int Y = e.attribute("height").toInt();
resize(X,Y);
kDebug()<<"Resizing to "<<X<<' '<<Y<<endl;
}
else if(e.tagName() == "Title")
setTitle(e.text());
// TODO
// else if(e.tagName() == "TitleEnabled")
// title_enabled = (bool) e.text().toInt();
// else if(e.tagName() == "Background")
// background.setColor(QColor(e.text()));
// else if(e.tagName() == "Brush")
// background.setStyle(Qt::BrushStyle(e.text().toInt()));
// else if(e.tagName() == "Timestamp")
// timestamp.setTime_t(e.text().toInt());
// else if(e.tagName() == "TimestampEnabled")
// timestamp_enabled = (bool) e.text().toInt();
// else if(e.tagName() == "DrawObjectsFirst")
// draw_objects_first = (bool) e.text().toInt();
/* else if(e.tagName() == "Label")
label[nr_label++]->openXML(e.firstChild());
else if(e.tagName() == "Line")
line[nr_line++]->openXML(e.firstChild());
else if(e.tagName() == "Rect")
rect[nr_rect++]->openXML(e.firstChild());
else if(e.tagName() == "Ellipse")
ellipse[nr_ellipse++]->openXML(e.firstChild());
else if(e.tagName() == "Image")
image[nr_image++]->openXML(e.firstChild());
*/
else if(e.tagName() == "Plot") {
// TODO
kDebug()<<" TAG Plot : type = "<<e.attribute("type").toInt()<<endl;
// newPlot((PlotType) e.attribute("type").toInt());
kDebug()<<"tmpnr_plots = "<<tmpnr_plots<<endl;
kDebug()<<" Calling openPlotXML()"<<endl;
// plot[tmpnr_plots++]->openPlotXML(e.firstChild());
}
node = node.nextSibling();
}
}
void Worksheet::setTitle(QString title) {
kDebug()<<"title ="<<title<<endl;
bool ok=true;
if(title.isEmpty())
title = KInputDialog::getText("LabPlot", i18n("Worksheet title : "), windowTitle(), &ok);