1.9.4 New base class for XML based file parsing (vym & Freemind)
authorinsilmaril
Mon Jul 30 09:47:09 2007 +0000 (2007-07-30)
changeset 571bc9083a4a7fb
parent 570 ab83f030285e
child 572 a97aabc21136
1.9.4 New base class for XML based file parsing (vym & Freemind)
mainwindow.cpp
mainwindow.h
mapeditor.cpp
     1.1 --- a/mainwindow.cpp	Mon Jul 30 09:47:08 2007 +0000
     1.2 +++ b/mainwindow.cpp	Mon Jul 30 09:47:09 2007 +0000
     1.3 @@ -310,6 +310,11 @@
     1.4  		connect( a, SIGNAL( triggered() ), this, SLOT( fileImportFirefoxBookmarks() ) );
     1.5  	}	
     1.6  
     1.7 +	a = new QAction("Freemind...",this);
     1.8 +	a->setStatusTip ( tr( "Import %1","status tip file menu").arg(" Freemind")  );
     1.9 +	fileImportMenu->addAction (a);
    1.10 +	connect( a, SIGNAL( triggered() ), this, SLOT( fileImportFreemind() ) );
    1.11 +
    1.12  	a = new QAction("Mind Manager...",this);
    1.13  	a->setStatusTip ( tr( "Import %1","status tip file menu").arg(" Mind Manager")  );
    1.14  	fileImportMenu->addAction (a);
    1.15 @@ -1817,13 +1822,13 @@
    1.16  			newME->setSmoothPixmap(actionViewToggleSmoothPixmapTransform->isOn());
    1.17  			// For the very first map we do not have flagrows yet...
    1.18  			newME->select("mc:");
    1.19 -			newME->load (clipboardDir+"/"+clipboardFile,ImportReplace);
    1.20 +			newME->load (clipboardDir+"/"+clipboardFile,ImportReplace, VymMap);
    1.21  		}
    1.22  
    1.23  	}
    1.24  }
    1.25  
    1.26 -ErrorCode Main::fileLoad(QString fn, const LoadMode &lmode)
    1.27 +ErrorCode Main::fileLoad(QString fn, const LoadMode &lmode, const FileType &ftype)
    1.28  {
    1.29  	ErrorCode err=success;
    1.30  	
    1.31 @@ -1990,7 +1995,7 @@
    1.32  
    1.33  			// Finally load map into mapEditor
    1.34  			me->setFilePath (mapfile,fn);
    1.35 -			err=me->load(mapfile,lmode);
    1.36 +			err=me->load(mapfile,lmode,ftype);
    1.37  
    1.38  			// Restore old (maybe empty) filepath, if this is an import
    1.39  			if (lmode!=NewMap)
    1.40 @@ -2311,7 +2316,7 @@
    1.41  		{
    1.42  			im.setFile (*it);
    1.43  			if (im.transform() && 
    1.44 -				success==fileLoad (im.getTransformedFile(),NewMap) && 
    1.45 +				success==fileLoad (im.getTransformedFile(),NewMap,FreemindMap) && 
    1.46  				currentMapEditor() )
    1.47  				currentMapEditor()->setFilePath ("");
    1.48  			++it;
    1.49 @@ -2320,6 +2325,37 @@
    1.50  	delete (fd);
    1.51  }
    1.52  
    1.53 +void Main::fileImportFreemind()
    1.54 +{
    1.55 +	QStringList filters;
    1.56 +	filters <<"Freemind map (*.mm)"<<"All files (*)";
    1.57 +	QFileDialog *fd=new QFileDialog( this);
    1.58 +	fd->setDir (lastFileDir);
    1.59 +	fd->setFileMode (QFileDialog::ExistingFiles);
    1.60 +	fd->setFilters (filters);
    1.61 +	fd->setCaption(vymName+ " - " +tr("Load Freemind map"));
    1.62 +	fd->show();
    1.63 +
    1.64 +	QString fn;
    1.65 +	if ( fd->exec() == QDialog::Accepted )
    1.66 +	{
    1.67 +		lastFileDir=fd->directory().path();
    1.68 +	    QStringList flist = fd->selectedFiles();
    1.69 +		QStringList::Iterator it = flist.begin();
    1.70 +		while( it != flist.end() ) 
    1.71 +		{
    1.72 +			fn = *it;
    1.73 +			if ( fileLoad (fn,NewMap, FreemindMap)  )
    1.74 +			{	
    1.75 +				currentMapEditor()->setFilePath ("");
    1.76 +			}	
    1.77 +			++it;
    1.78 +		}
    1.79 +	}
    1.80 +	delete (fd);
    1.81 +}
    1.82 +
    1.83 +
    1.84  void Main::fileImportMM()
    1.85  {
    1.86  	ImportMM im;
     2.1 --- a/mainwindow.h	Mon Jul 30 09:47:08 2007 +0000
     2.2 +++ b/mainwindow.h	Mon Jul 30 09:47:09 2007 +0000
     2.3 @@ -10,7 +10,7 @@
     2.4  #include "mapeditor.h"
     2.5  #include "simplescripteditor.h"
     2.6  #include "texteditor.h"
     2.7 -#include "xml.h"
     2.8 +//#include "xml.h"
     2.9  
    2.10  class Main : public QMainWindow 
    2.11  {
    2.12 @@ -60,7 +60,7 @@
    2.13  private slots:
    2.14  	void editorChanged(QWidget*);
    2.15  
    2.16 -    ErrorCode fileLoad(QString ,const LoadMode &);
    2.17 +    ErrorCode fileLoad(QString ,const LoadMode &, const FileType & ftype=VymMap);
    2.18      void fileLoad(const LoadMode &);
    2.19      void fileLoad();
    2.20  	void fileLoadRecent();
    2.21 @@ -74,6 +74,7 @@
    2.22      void fileSaveAs();
    2.23      void fileImportKDEBookmarks();
    2.24      void fileImportFirefoxBookmarks();
    2.25 +    void fileImportFreemind();
    2.26      void fileImportMM();
    2.27      void fileImportDir();
    2.28      void fileExportXML();
     3.1 --- a/mapeditor.cpp	Mon Jul 30 09:47:08 2007 +0000
     3.2 +++ b/mapeditor.cpp	Mon Jul 30 09:47:09 2007 +0000
     3.3 @@ -19,8 +19,8 @@
     3.4  #include "misc.h"
     3.5  #include "texteditor.h"
     3.6  #include "warningdialog.h"
     3.7 -#include "xml.h"
     3.8 -#include "xml-fm.h"
     3.9 +#include "xml-freemind.h"
    3.10 +#include "xml-vym.h"
    3.11  
    3.12  
    3.13  extern TextEditor *textEditor;
    3.14 @@ -1485,10 +1485,20 @@
    3.15  	return destPath;
    3.16  }
    3.17  
    3.18 -ErrorCode MapEditor::load (QString fname, LoadMode lmode)
    3.19 +ErrorCode MapEditor::load (QString fname, const LoadMode &lmode, const FileType &ftype)
    3.20  {
    3.21  	ErrorCode err=success;
    3.22  
    3.23 +	parseBaseHandler *handler;
    3.24 +	switch (ftype)
    3.25 +	{
    3.26 +		case VymMap: handler=new parseVYMHandler; break;
    3.27 +		case FreemindMap : handler=new parseFreemindHandler; break;
    3.28 +		default: 
    3.29 +			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
    3.30 +				   "Unknown FileType in MapEditor::load()");
    3.31 +		return aborted;	
    3.32 +	}
    3.33  	if (lmode==NewMap)
    3.34  	{
    3.35  		if (xelection.isEmpty() ) xelection.unselect();
    3.36 @@ -1513,9 +1523,7 @@
    3.37  				QString("addMapReplace(%1)").arg(fname),
    3.38  				QString("Add map %1 to %2").arg(fname).arg(getName(bo)));
    3.39  	}	
    3.40 -	
    3.41      
    3.42 -    parseVYMHandler handler;
    3.43  	QFile file( fname );
    3.44  
    3.45  	// I am paranoid: file should exist anyway
    3.46 @@ -1532,16 +1540,16 @@
    3.47  		blockSaveState=true;
    3.48  		QXmlInputSource source( file);
    3.49  		QXmlSimpleReader reader;
    3.50 -		reader.setContentHandler( &handler );
    3.51 -		reader.setErrorHandler( &handler );
    3.52 -		handler.setMapEditor( this );
    3.53 +		reader.setContentHandler( handler );
    3.54 +		reader.setErrorHandler( handler );
    3.55 +		handler->setMapEditor( this );
    3.56  
    3.57  
    3.58  		// We need to set the tmpDir in order  to load files with rel. path
    3.59  		QString tmpdir= fname.left(fname.findRev("/",-1));	
    3.60 -		handler.setTmpDir (tmpdir);
    3.61 -		handler.setInputFile (file.name());
    3.62 -		handler.setLoadMode (lmode);
    3.63 +		handler->setTmpDir (tmpdir);
    3.64 +		handler->setInputFile (file.name());
    3.65 +		handler->setLoadMode (lmode);
    3.66  		bool ok = reader.parse( source );
    3.67  		blockReposition=false;
    3.68  		blockSaveState=blockSaveStateOrg;
    3.69 @@ -1560,7 +1568,7 @@
    3.70  		} else 
    3.71  		{
    3.72  			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
    3.73 -					   tr( handler.errorProtocol() ) );
    3.74 +					   tr( handler->errorProtocol() ) );
    3.75  			// returnCode=1;	
    3.76  			// Still return "success": the map maybe at least
    3.77  			// partially read by the parser
    3.78 @@ -2208,10 +2216,10 @@
    3.79  		// Use the "historical" buffer
    3.80  		QString bakMapName=QDir::convertSeparators (QString("history-%1").arg(n));
    3.81  		QString bakMapDir=QDir::convertSeparators (tmpMapDir +"/"+bakMapName);
    3.82 -		load (bakMapDir+"/"+clipboardFile,ImportAdd);
    3.83 +		load (bakMapDir+"/"+clipboardFile,ImportAdd, VymMap);
    3.84  	} else
    3.85  		// Use the global buffer
    3.86 -		load (clipboardDir+"/"+clipboardFile,ImportAdd);
    3.87 +		load (clipboardDir+"/"+clipboardFile,ImportAdd, VymMap);
    3.88  	blockSaveState=old;
    3.89  }
    3.90  
    3.91 @@ -4111,82 +4119,11 @@
    3.92  
    3.93  void MapEditor::testFunction1()
    3.94  {
    3.95 -	ErrorCode err=success;
    3.96 -	LoadMode lmode=NewMap;
    3.97 -
    3.98 -	//QString fname="test/freemind/doc/freemind.xml";
    3.99 -	QString fname="test/freemind/doc/icons.mm";
   3.100 -    parseFMHandler handler;
   3.101 -	QFile file( fname );
   3.102 -
   3.103 -	// I am paranoid: file should exist anyway
   3.104 -	// according to check in mainwindow.
   3.105 -	if (!file.exists() )
   3.106 -	{
   3.107 -		QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   3.108 -				   tr("Couldn't open map " +fname)+".");
   3.109 -		err=aborted;	
   3.110 -	} else
   3.111 -	{
   3.112 -		bool blockSaveStateOrg=blockSaveState;
   3.113 -		blockReposition=true;
   3.114 -		blockSaveState=true;
   3.115 -		QXmlInputSource source( file);
   3.116 -		QXmlSimpleReader reader;
   3.117 -		reader.setContentHandler( &handler );
   3.118 -		reader.setErrorHandler( &handler );
   3.119 -		handler.setMapEditor( this );
   3.120 -
   3.121 -
   3.122 -		// We need to set the tmpDir in order  to load files with rel. path
   3.123 -		QString tmpdir= fname.left(fname.findRev("/",-1));	
   3.124 -		handler.setTmpDir (tmpdir);
   3.125 -		handler.setInputFile (file.name());
   3.126 -		//handler.setLoadMode (lmode);
   3.127 -		bool ok = reader.parse( source );
   3.128 -		blockReposition=false;
   3.129 -		blockSaveState=blockSaveStateOrg;
   3.130 -		file.close();
   3.131 -		if ( ok ) 
   3.132 -		{
   3.133 -			mapCenter->reposition();
   3.134 -			xelection.update();
   3.135 -			if (lmode==NewMap)
   3.136 -			{
   3.137 -				mapDefault=false;
   3.138 -				mapChanged=false;
   3.139 -				mapUnsaved=false;
   3.140 -				autosaveTimer->stop();
   3.141 -			}
   3.142 -		} else 
   3.143 -		{
   3.144 -			QMessageBox::critical( 0, tr( "Critical Parse Error" ),
   3.145 -					   tr( handler.errorProtocol() ) );
   3.146 -			// returnCode=1;	
   3.147 -			// Still return "success": the map maybe at least
   3.148 -			// partially read by the parser
   3.149 -		}	
   3.150 -	}	
   3.151 -	updateActions();
   3.152 -	//return err;
   3.153  /*
   3.154  	BranchObj *bo=xelection.getBranch();
   3.155  	if (bo) animObjList.append( bo );
   3.156  */	
   3.157  	
   3.158 -/*
   3.159 -	WarningDialog dia;
   3.160 -	dia.showCancelButton (true);
   3.161 -	dia.setText("This is a longer \nWarning");
   3.162 -	dia.setCaption("Warning: Flux problem");
   3.163 -	dia.setShowAgainName("mapeditor/testDialog");
   3.164 -	if (dia.exec()==QDialog::Accepted)
   3.165 -		cout << "accepted!\n";
   3.166 -	else	
   3.167 -		cout << "canceled!\n";
   3.168 -	return;
   3.169 -*/
   3.170 -
   3.171  /* TODO Hide hidden stuff temporary, maybe add this as regular function somewhere
   3.172  	if (hidemode==HideNone)
   3.173  	{