xml.cpp
author insilmaril
Tue, 15 Aug 2006 11:42:10 +0000
branchqt4-port
changeset 13 9aadd0f58300
parent 2 608f976aa7bb
permissions -rw-r--r--
1.8.52 Toolbar layout is saved now.
insilmaril@0
     1
#include "xml.h"
insilmaril@0
     2
insilmaril@0
     3
#include <qmessagebox.h>
insilmaril@0
     4
#include <qcolor.h>
insilmaril@2
     5
#include <q3stylesheet.h>
insilmaril@2
     6
//Added by qt3to4:
insilmaril@2
     7
#include <QTextStream>
insilmaril@0
     8
#include <iostream>
insilmaril@0
     9
insilmaril@0
    10
#include "misc.h"
insilmaril@0
    11
#include "settings.h"
insilmaril@2
    12
#include "linkablemapobj.h"
insilmaril@0
    13
insilmaril@0
    14
#include "version.h"
insilmaril@0
    15
insilmaril@0
    16
static BranchObj *lastBranch;
insilmaril@0
    17
static FloatObj *lastFloat;
insilmaril@2
    18
static OrnamentedObj *lastOO;
insilmaril@0
    19
insilmaril@0
    20
extern Settings settings;
insilmaril@0
    21
insilmaril@0
    22
mapBuilderHandler::mapBuilderHandler() {}
insilmaril@0
    23
insilmaril@0
    24
mapBuilderHandler::~mapBuilderHandler() {}
insilmaril@0
    25
insilmaril@0
    26
QString mapBuilderHandler::errorProtocol() { return errorProt; }
insilmaril@0
    27
insilmaril@0
    28
bool mapBuilderHandler::startDocument()
insilmaril@0
    29
{
insilmaril@0
    30
    errorProt = "";
insilmaril@0
    31
    state = StateInit;
insilmaril@0
    32
    laststate = StateInit;
insilmaril@0
    33
    branchDepth=0;
insilmaril@0
    34
	htmldata="";
insilmaril@0
    35
	isVymPart=false;
insilmaril@0
    36
    return true;
insilmaril@0
    37
}
insilmaril@0
    38
insilmaril@0
    39
insilmaril@0
    40
QString mapBuilderHandler::parseHREF(QString href)
insilmaril@0
    41
{
insilmaril@0
    42
	QString type=href.section(":",0,0);
insilmaril@0
    43
	QString path=href.section(":",1,1);
insilmaril@0
    44
	if (!tmpDir.endsWith("/"))
insilmaril@0
    45
		return tmpDir + "/" + path;
insilmaril@0
    46
	else	
insilmaril@0
    47
		return tmpDir + path;
insilmaril@0
    48
}
insilmaril@0
    49
insilmaril@0
    50
bool mapBuilderHandler::startElement  ( const QString&, const QString&,
insilmaril@0
    51
                    const QString& eName, const QXmlAttributes& atts ) 
insilmaril@0
    52
{
insilmaril@0
    53
    QColor col;
insilmaril@2
    54
	//cout << "startElement <"<<eName<<">  state="<<state <<"  laststate="<<laststate<<"   loadMode="<<loadMode<<endl;
insilmaril@0
    55
    if ( state == StateInit && (eName == "vymmap")  ) 
insilmaril@0
    56
	{
insilmaril@0
    57
        state = StateMap;
insilmaril@0
    58
		if (!atts.value( "version").isEmpty() ) 
insilmaril@0
    59
		{
insilmaril@0
    60
			mc->setVersion(atts.value( "version" ));
insilmaril@0
    61
			if (!mc->checkVersion())
insilmaril@0
    62
				QMessageBox::warning( 0, "Warning: Version Problem" ,
insilmaril@0
    63
				   "<h3>Map is newer than VYM</h3>"
insilmaril@0
    64
				   "<p>The map you are just trying to load was "
insilmaril@0
    65
				   "saved using vym " +atts.value("version")+". "
insilmaril@2
    66
				   "The version of this vym is " __VYM_VERSION
insilmaril@0
    67
				   ". If you run into problems after pressing "
insilmaril@0
    68
				   "the ok-button below, updating vym should help.");
insilmaril@0
    69
insilmaril@0
    70
		}
insilmaril@0
    71
		if (loadMode==NewMap)
insilmaril@0
    72
		{
insilmaril@0
    73
			if (!atts.value( "author").isEmpty() )
insilmaril@0
    74
			{
insilmaril@0
    75
				mc->setAuthor(atts.value( "author" ) );
insilmaril@0
    76
			}
insilmaril@0
    77
			if (!atts.value( "comment").isEmpty() )
insilmaril@0
    78
			{
insilmaril@0
    79
				mc->setComment (atts.value( "comment" ) );
insilmaril@0
    80
			}
insilmaril@0
    81
			if (!atts.value( "backgroundColor").isEmpty() )
insilmaril@0
    82
			{
insilmaril@0
    83
				col.setNamedColor(atts.value("backgroundColor"));
insilmaril@0
    84
				mc->getCanvas()->setBackgroundColor(col);
insilmaril@0
    85
			}	    
insilmaril@0
    86
			if (!atts.value( "linkColorHint").isEmpty() ) 
insilmaril@0
    87
			{
insilmaril@0
    88
				if (atts.value("linkColorHint")=="HeadingColor")
insilmaril@0
    89
					me->setLinkColorHint(HeadingColor);
insilmaril@0
    90
				else
insilmaril@0
    91
					me->setLinkColorHint(DefaultColor);
insilmaril@0
    92
			}
insilmaril@0
    93
			if (!atts.value( "linkStyle").isEmpty() ) 
insilmaril@0
    94
			{
insilmaril@0
    95
				QString s=atts.value("linkStyle");
insilmaril@0
    96
				if (s=="StyleLine")
insilmaril@0
    97
					me->setLinkStyle(StyleLine);
insilmaril@0
    98
				else	
insilmaril@0
    99
					if (s=="StyleParabel")
insilmaril@0
   100
						me->setLinkStyle(StyleParabel);
insilmaril@0
   101
					else	
insilmaril@0
   102
						if (s=="StylePolyLine")
insilmaril@0
   103
							me->setLinkStyle(StylePolyLine);
insilmaril@0
   104
						else	
insilmaril@0
   105
							me->setLinkStyle(StylePolyParabel);
insilmaril@0
   106
			}	
insilmaril@0
   107
			if (!atts.value( "linkColor").isEmpty() ) 
insilmaril@0
   108
			{
insilmaril@0
   109
				col.setNamedColor(atts.value("linkColor"));
insilmaril@0
   110
				me->setLinkColor(col);
insilmaril@0
   111
			}	
insilmaril@2
   112
			if (!atts.value( "defXLinkColor").isEmpty() ) 
insilmaril@2
   113
			{
insilmaril@2
   114
				col.setNamedColor(atts.value("defXLinkColor"));
insilmaril@2
   115
				me->setDefXLinkColor(col);
insilmaril@2
   116
			}	
insilmaril@2
   117
			if (!atts.value( "defXLinkWidth").isEmpty() ) 
insilmaril@2
   118
			{
insilmaril@2
   119
				me->setDefXLinkWidth(atts.value("defXLinkWidth").toInt ());
insilmaril@2
   120
			}	
insilmaril@0
   121
		}	
insilmaril@0
   122
	} else if ( eName == "select" && state == StateMap ) 
insilmaril@0
   123
	{
insilmaril@0
   124
		state=StateMapSelect;
insilmaril@0
   125
	} else if ( eName == "setting" && state == StateMap ) 
insilmaril@0
   126
	{
insilmaril@0
   127
		state=StateMapSetting;
insilmaril@0
   128
		if (loadMode==NewMap)
insilmaril@0
   129
			readSettingAttr (atts);
insilmaril@0
   130
	} else if ( eName == "mapcenter" && state == StateMap ) 
insilmaril@0
   131
	{
insilmaril@0
   132
		state=StateMapCenter;
insilmaril@0
   133
		if (loadMode==NewMap)
insilmaril@0
   134
		{	
insilmaril@0
   135
			// Really use the found mapcenter as MCO in a new map
insilmaril@0
   136
			lastBranch=mc;	// avoid empty pointer
insilmaril@0
   137
		} else
insilmaril@0
   138
		{
insilmaril@0
   139
			// Treat the found mapcenter as a branch 
insilmaril@0
   140
			// in an existing map
insilmaril@0
   141
			LinkableMapObj* lmo=me->getSelection();
insilmaril@0
   142
			if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
insilmaril@0
   143
			        || (typeid(*lmo) == typeid(MapCenterObj) ) )
insilmaril@0
   144
			{
insilmaril@2
   145
				lastBranch=(BranchObj*)lmo;
insilmaril@0
   146
				if (loadMode==ImportAdd)
insilmaril@0
   147
				{
insilmaril@0
   148
					lastBranch->addBranch();
insilmaril@0
   149
					lastBranch=lastBranch->getLastBranch();
insilmaril@0
   150
				} else
insilmaril@0
   151
					lastBranch->clear();
insilmaril@0
   152
			} else
insilmaril@0
   153
				return false;
insilmaril@0
   154
		}
insilmaril@0
   155
		readBranchAttr (atts);
insilmaril@0
   156
	} else if ( (eName == "standardflag" ||eName == "standardFlag") && state == StateMapCenter) 
insilmaril@0
   157
	{
insilmaril@0
   158
		state=StateMapCenterStandardFlag;
insilmaril@0
   159
	} else if ( eName == "heading" && state == StateMapCenter) 
insilmaril@0
   160
	{
insilmaril@0
   161
		state=StateMapCenterHeading;
insilmaril@0
   162
		if (!atts.value( "textColor").isEmpty() ) 
insilmaril@0
   163
		{
insilmaril@0
   164
			col.setNamedColor(atts.value("textColor"));
insilmaril@2
   165
			lastBranch->setColor(col );
insilmaril@0
   166
		}	    
insilmaril@0
   167
	} else if ( eName == "note" && state == StateMapCenter) 
insilmaril@0
   168
	{	// only for backward compatibility (<1.4.6). Use htmlnote now.
insilmaril@0
   169
		state=StateMapCenterNote;
insilmaril@0
   170
		if (!readNoteAttr (atts) ) return false;
insilmaril@0
   171
	} else if ( eName == "htmlnote" && state == StateMapCenter) 
insilmaril@0
   172
	{
insilmaril@0
   173
		laststate=state;
insilmaril@0
   174
		state=StateHtmlNote;
insilmaril@0
   175
    } else if ( eName == "floatimage" && state == StateMapCenter ) 
insilmaril@0
   176
	{
insilmaril@0
   177
		state=StateMapCenterFloatImage;
insilmaril@0
   178
        lastBranch->addFloatImage();
insilmaril@0
   179
		lastFloat=lastBranch->getLastFloatImage();
insilmaril@0
   180
		if (!readFloatImageAttr(atts)) return false;
insilmaril@2
   181
	} else if ( (eName == "branch"||eName=="floatimage") && state == StateMap) 
insilmaril@0
   182
	{
insilmaril@0
   183
		// This is used in vymparts, which have no mapcenter!
insilmaril@0
   184
		isVymPart=true;
insilmaril@0
   185
		LinkableMapObj* lmo=me->getSelection();
insilmaril@2
   186
		if (!lmo)
insilmaril@2
   187
		{
insilmaril@2
   188
			// If a vym part is _loaded_ (not imported), 
insilmaril@2
   189
			// selection==lmo==NULL
insilmaril@2
   190
			// Treat it like ImportAdd then...
insilmaril@2
   191
			loadMode=ImportAdd;
insilmaril@2
   192
			lmo=mc;
insilmaril@2
   193
		}	
insilmaril@0
   194
		if (lmo && (typeid(*lmo) == typeid(BranchObj) ) 
insilmaril@0
   195
				|| (typeid(*lmo) == typeid(MapCenterObj) ) )
insilmaril@0
   196
		{
insilmaril@0
   197
			lastBranch=(BranchObj*)(lmo);
insilmaril@2
   198
			if (eName=="branch")
insilmaril@0
   199
			{
insilmaril@2
   200
				state=StateBranch;
insilmaril@2
   201
				if (loadMode==ImportAdd)
insilmaril@2
   202
				{
insilmaril@2
   203
					lastBranch->addBranch();
insilmaril@2
   204
					lastBranch=lastBranch->getLastBranch();
insilmaril@2
   205
					
insilmaril@2
   206
				} else
insilmaril@2
   207
					lastBranch->clear();
insilmaril@2
   208
				branchDepth=1;
insilmaril@2
   209
				readBranchAttr (atts);
insilmaril@2
   210
			} else if (eName=="floatimage")
insilmaril@2
   211
			{
insilmaril@2
   212
				state=StateFloatImage;
insilmaril@2
   213
				lastBranch->addFloatImage();
insilmaril@2
   214
				lastFloat=lastBranch->getLastFloatImage();
insilmaril@2
   215
				if (!readFloatImageAttr(atts)) return false;
insilmaril@2
   216
			} else return false;
insilmaril@2
   217
		} else return false;
insilmaril@0
   218
	} else if ( eName == "branch" && state == StateMapCenter) 
insilmaril@0
   219
	{
insilmaril@0
   220
		state=StateBranch;
insilmaril@0
   221
		branchDepth=1;
insilmaril@0
   222
		lastBranch->addBranch();
insilmaril@0
   223
		lastBranch=lastBranch->getLastBranch();
insilmaril@0
   224
		readBranchAttr (atts);
insilmaril@0
   225
	} else if ( (eName=="standardflag" ||eName == "standardFlag") && state == StateBranch) 
insilmaril@0
   226
	{
insilmaril@0
   227
		state=StateBranchStandardFlag;
insilmaril@0
   228
	} else if ( eName == "heading" && state == StateBranch) 
insilmaril@0
   229
	{
insilmaril@0
   230
		state=StateBranchHeading;
insilmaril@0
   231
		if (!atts.value( "textColor").isEmpty() ) 
insilmaril@0
   232
		{
insilmaril@0
   233
			col.setNamedColor(atts.value("textColor"));
insilmaril@2
   234
			lastBranch->setColor(col );
insilmaril@0
   235
		}	    
insilmaril@0
   236
    } else if ( eName == "note" && state == StateBranch) 
insilmaril@0
   237
	{
insilmaril@0
   238
        state=StateBranchNote;
insilmaril@0
   239
		if (!readNoteAttr (atts) ) return false;
insilmaril@0
   240
	} else if ( eName == "htmlnote" && state == StateBranch) 
insilmaril@0
   241
	{
insilmaril@0
   242
		laststate=state;
insilmaril@0
   243
		state=StateHtmlNote;
insilmaril@0
   244
		no.clear();
insilmaril@0
   245
		if (!atts.value( "fonthint").isEmpty() ) 
insilmaril@0
   246
			no.setFontHint(atts.value ("fonthint") );
insilmaril@0
   247
    } else if ( eName == "floatimage" && state == StateBranch ) 
insilmaril@0
   248
	{
insilmaril@0
   249
		state=StateBranchFloatImage;
insilmaril@0
   250
        lastBranch->addFloatImage();
insilmaril@0
   251
		lastFloat=lastBranch->getLastFloatImage();
insilmaril@0
   252
		if (!readFloatImageAttr(atts)) return false;
insilmaril@2
   253
    } else if ( eName == "xlink" && state == StateBranch ) 
insilmaril@2
   254
	{
insilmaril@2
   255
		state=StateBranchXLink;
insilmaril@2
   256
		if (!readXLinkAttr (atts)) return false;
insilmaril@0
   257
    } else if ( eName == "branch" && state == StateBranch ) 
insilmaril@0
   258
	{
insilmaril@0
   259
        lastBranch->addBranch();
insilmaril@0
   260
		lastBranch=lastBranch->getLastBranch();		
insilmaril@0
   261
        branchDepth++;
insilmaril@0
   262
		readBranchAttr (atts);
insilmaril@0
   263
    } else if ( eName == "html" && state == StateHtmlNote ) 
insilmaril@0
   264
	{
insilmaril@0
   265
		state=StateHtml;
insilmaril@0
   266
		htmldata="<"+eName;
insilmaril@0
   267
		readHtmlAttr(atts);
insilmaril@0
   268
		htmldata+=">";
insilmaril@0
   269
    } else if ( state == StateHtml ) 
insilmaril@0
   270
	{
insilmaril@0
   271
		// accept all while in html mode,
insilmaril@0
   272
		htmldata+="<"+eName;
insilmaril@0
   273
		readHtmlAttr(atts);
insilmaril@0
   274
		htmldata+=">";
insilmaril@0
   275
    } else
insilmaril@0
   276
        return false;   // Error
insilmaril@0
   277
    return true;
insilmaril@0
   278
}
insilmaril@0
   279
insilmaril@0
   280
bool mapBuilderHandler::endElement  ( const QString&, const QString&, const QString &eName)
insilmaril@0
   281
{
insilmaril@0
   282
//	cout << "endElement </"<<eName<<">  state="<<state <<"  laststate="<<laststate<<endl;
insilmaril@0
   283
    switch ( state ) 
insilmaril@0
   284
	{
insilmaril@0
   285
        case StateMapSelect: state=StateMap;  return true;
insilmaril@0
   286
        case StateMapSetting: state=StateMap;  return true;
insilmaril@0
   287
        case StateMapCenter: state=StateMap;  return true;
insilmaril@0
   288
        case StateMapCenterStandardFlag: state=StateMapCenter;  return true;
insilmaril@0
   289
        case StateMapCenterHeading: state=StateMapCenter;  return true;
insilmaril@0
   290
        case StateMapCenterNote: state=StateMapCenter;  return true;
insilmaril@0
   291
        case StateMapCenterFloatImage: state=StateMapCenter;  return true;
insilmaril@2
   292
        case StateFloatImage: state=StateMap; return true;
insilmaril@0
   293
        case StateBranch: 
insilmaril@0
   294
            if (branchDepth>1) 
insilmaril@0
   295
			{
insilmaril@0
   296
                branchDepth--;
insilmaril@0
   297
                state=StateBranch;
insilmaril@0
   298
            } else  
insilmaril@0
   299
			{
insilmaril@0
   300
                branchDepth=0;
insilmaril@0
   301
				if (isVymPart)
insilmaril@0
   302
					state=StateMap;
insilmaril@0
   303
				else
insilmaril@0
   304
					state=StateMapCenter;
insilmaril@0
   305
            }   
insilmaril@0
   306
			lastBranch=(BranchObj*)(lastBranch->getParObj());
insilmaril@0
   307
             return true;
insilmaril@0
   308
        case StateBranchStandardFlag: state=StateBranch;  return true;
insilmaril@0
   309
        case StateBranchHeading: state=StateBranch;  return true;
insilmaril@0
   310
        case StateBranchNote: state=StateBranch; return true;
insilmaril@0
   311
        case StateBranchFloatImage: state=StateBranch;  return true;
insilmaril@2
   312
        case StateBranchXLink: state=StateBranch;  return true;
insilmaril@0
   313
        case StateHtmlNote: state=laststate; return true;
insilmaril@0
   314
        case StateHtml: 
insilmaril@0
   315
			htmldata+="</"+eName+">";
insilmaril@0
   316
			if (eName=="html")
insilmaril@0
   317
			{
insilmaril@0
   318
				state=StateHtmlNote;  
insilmaril@0
   319
				htmldata.replace ("<br></br>","<br />");
insilmaril@0
   320
				no.setNote (htmldata);
insilmaril@0
   321
				lastBranch->setNote (no);
insilmaril@0
   322
				return true;
insilmaril@0
   323
			}	else
insilmaril@0
   324
			{
insilmaril@0
   325
				return true;
insilmaril@0
   326
			}	
insilmaril@0
   327
        case StateMap: state=StateInit;  return true;
insilmaril@0
   328
        default : 
insilmaril@0
   329
			// even for HTML includes, this should never be reached
insilmaril@0
   330
			return false;
insilmaril@0
   331
    }   
insilmaril@0
   332
}
insilmaril@0
   333
insilmaril@0
   334
bool mapBuilderHandler::characters   ( const QString& ch)
insilmaril@0
   335
{
insilmaril@0
   336
	//cout << "characters \""<<ch<<"\"  state="<<state <<"  laststate="<<laststate<<endl;
insilmaril@0
   337
insilmaril@0
   338
	QString ch_org=quotemeta (ch);
insilmaril@0
   339
    QString ch_simplified=ch.simplifyWhiteSpace();
insilmaril@0
   340
    if ( ch_simplified.isEmpty() ) return true;
insilmaril@0
   341
insilmaril@0
   342
    switch ( state ) 
insilmaril@0
   343
    {
insilmaril@0
   344
        case StateInit: break;
insilmaril@0
   345
        case StateMap: break; 
insilmaril@0
   346
		case StateMapSelect:
insilmaril@0
   347
			me->select(ch_simplified);
insilmaril@0
   348
			break;
insilmaril@0
   349
		case StateMapSetting:break;
insilmaril@0
   350
        case StateMapCenter: break;
insilmaril@0
   351
        case StateMapCenterStandardFlag: 
insilmaril@0
   352
            lastBranch->activateStandardFlag(ch_simplified); 
insilmaril@0
   353
            break;
insilmaril@0
   354
        case StateMapCenterHeading: 
insilmaril@0
   355
            lastBranch->setHeading(ch_simplified); 
insilmaril@0
   356
            break;
insilmaril@0
   357
        case StateMapCenterNote:
insilmaril@0
   358
			lastBranch->setNote(ch_simplified);
insilmaril@0
   359
			break;
insilmaril@0
   360
        case StateBranch: break;
insilmaril@0
   361
        case StateBranchStandardFlag: 
insilmaril@0
   362
            lastBranch->activateStandardFlag(ch_simplified); 
insilmaril@0
   363
            break;
insilmaril@0
   364
        case StateBranchHeading: 
insilmaril@0
   365
            lastBranch->setHeading(ch_simplified);
insilmaril@0
   366
            break;
insilmaril@0
   367
        case StateBranchNote: 
insilmaril@0
   368
			lastBranch->setNote(ch_simplified);
insilmaril@0
   369
			break;
insilmaril@0
   370
        case StateBranchFloatImage: break;
insilmaril@0
   371
        case StateHtmlNote: break;
insilmaril@0
   372
        case StateHtml:
insilmaril@0
   373
			htmldata+=ch_org;
insilmaril@0
   374
			break;
insilmaril@0
   375
        default: 
insilmaril@0
   376
			return false;
insilmaril@0
   377
    }
insilmaril@0
   378
    return true;
insilmaril@0
   379
}
insilmaril@0
   380
insilmaril@0
   381
QString mapBuilderHandler::errorString() 
insilmaril@0
   382
{
insilmaril@0
   383
    return "the document is not in the VYM file format";
insilmaril@0
   384
}
insilmaril@0
   385
insilmaril@0
   386
bool mapBuilderHandler::fatalError( const QXmlParseException& exception ) 
insilmaril@0
   387
{
insilmaril@2
   388
    errorProt += QString( "Fatal parsing error: %1 in line %2, column %3\n")
insilmaril@0
   389
    .arg( exception.message() )
insilmaril@0
   390
    .arg( exception.lineNumber() )
insilmaril@0
   391
    .arg( exception.columnNumber() );
insilmaril@2
   392
	// Try to read the bogus line
insilmaril@2
   393
	errorProt+=QString("File is: %1\n").arg(inputFile);
insilmaril@2
   394
	QString s;
insilmaril@2
   395
	if (loadStringFromDisk (inputFile,s))
insilmaril@2
   396
	{
insilmaril@2
   397
		QStringList sl=QStringList::split ("\n",s);
insilmaril@2
   398
		int i=1;
insilmaril@2
   399
		QStringList::Iterator it = sl.begin();
insilmaril@2
   400
		while (i<exception.lineNumber()-1)
insilmaril@2
   401
		{
insilmaril@2
   402
			it++;
insilmaril@2
   403
			i++;
insilmaril@2
   404
		}
insilmaril@2
   405
		s=*it;
insilmaril@2
   406
		s.insert (exception.columnNumber()-1,"<ERROR>");
insilmaril@2
   407
		errorProt+=s;
insilmaril@2
   408
    }
insilmaril@0
   409
    return QXmlDefaultHandler::fatalError( exception );
insilmaril@0
   410
}
insilmaril@0
   411
insilmaril@0
   412
void mapBuilderHandler::setMapEditor (MapEditor* e)
insilmaril@0
   413
{
insilmaril@0
   414
    me=e;
insilmaril@0
   415
	mc=me->getMapCenter();
insilmaril@0
   416
}
insilmaril@0
   417
insilmaril@0
   418
void mapBuilderHandler::setTmpDir (QString tp)
insilmaril@0
   419
{
insilmaril@0
   420
	tmpDir=tp;
insilmaril@0
   421
}
insilmaril@0
   422
insilmaril@2
   423
void mapBuilderHandler::setInputFile (QString f)
insilmaril@2
   424
{
insilmaril@2
   425
	inputFile=f;
insilmaril@2
   426
}
insilmaril@2
   427
insilmaril@0
   428
void mapBuilderHandler::setLoadMode (const LoadMode &lm)
insilmaril@0
   429
{
insilmaril@0
   430
	loadMode=lm;
insilmaril@0
   431
}
insilmaril@0
   432
insilmaril@0
   433
bool mapBuilderHandler::readBranchAttr (const QXmlAttributes& a)
insilmaril@0
   434
{
insilmaril@2
   435
	lastOO=lastBranch;
insilmaril@2
   436
	if (!readOOAttr(a)) return false;
insilmaril@2
   437
insilmaril@0
   438
	if (!a.value( "scrolled").isEmpty() )
insilmaril@0
   439
		lastBranch->toggleScroll();
insilmaril@0
   440
	if (!a.value( "frameType").isEmpty() ) 
insilmaril@0
   441
		lastBranch->setFrameType (a.value("frameType"));
insilmaril@2
   442
insilmaril@2
   443
	if (!a.value( "incImgV").isEmpty() ) 
insilmaril@2
   444
	{	
insilmaril@2
   445
		if (a.value("incImgV")=="true")
insilmaril@2
   446
			lastBranch->setIncludeImagesVer(true);
insilmaril@2
   447
		else	
insilmaril@2
   448
			lastBranch->setIncludeImagesVer(false);
insilmaril@2
   449
	}	
insilmaril@2
   450
	if (!a.value( "incImgH").isEmpty() ) 
insilmaril@2
   451
	{	
insilmaril@2
   452
		if (a.value("incImgH")=="true")
insilmaril@2
   453
			lastBranch->setIncludeImagesHor(true);
insilmaril@2
   454
		else	
insilmaril@2
   455
			lastBranch->setIncludeImagesHor(false);
insilmaril@2
   456
	}	
insilmaril@2
   457
	return true;	
insilmaril@2
   458
}
insilmaril@2
   459
insilmaril@2
   460
bool mapBuilderHandler::readOOAttr (const QXmlAttributes& a)
insilmaril@2
   461
{
insilmaril@2
   462
	if (lastOO)
insilmaril@2
   463
	{
insilmaril@2
   464
		bool okx,oky;
insilmaril@2
   465
		int x,y;
insilmaril@2
   466
		if (!a.value( "absPosX").isEmpty() && loadMode==NewMap && branchDepth<2) 
insilmaril@2
   467
		{
insilmaril@2
   468
			if (!a.value( "absPosY").isEmpty() ) 
insilmaril@2
   469
			{
insilmaril@2
   470
				x=a.value("absPosX").toInt (&okx, 10);
insilmaril@2
   471
				y=a.value("absPosY").toInt (&oky, 10);
insilmaril@2
   472
				if (okx && oky  )
insilmaril@2
   473
					lastOO->move(x,y);
insilmaril@2
   474
				else
insilmaril@2
   475
					return false;   // Couldn't read absPos
insilmaril@2
   476
			}           
insilmaril@2
   477
		}           
insilmaril@2
   478
		if (!a.value( "url").isEmpty() ) 
insilmaril@2
   479
			lastOO->setURL (a.value ("url"));
insilmaril@2
   480
		if (!a.value( "vymLink").isEmpty() ) 
insilmaril@2
   481
			lastOO->setVymLink (a.value ("vymLink"));
insilmaril@2
   482
		if (!a.value( "hideInExport").isEmpty() ) 
insilmaril@2
   483
			if (a.value("hideInExport")=="true")
insilmaril@2
   484
				lastOO->setHideInExport(true);
insilmaril@2
   485
insilmaril@2
   486
		if (!a.value( "hideLink").isEmpty()) 
insilmaril@2
   487
		{
insilmaril@2
   488
			if (a.value ("hideLink") =="true")
insilmaril@2
   489
				lastOO->setHideLinkUnselected(true);
insilmaril@2
   490
			else	
insilmaril@2
   491
				lastOO->setHideLinkUnselected(false);
insilmaril@2
   492
		}	
insilmaril@2
   493
	}
insilmaril@0
   494
	return true;	
insilmaril@0
   495
}
insilmaril@0
   496
insilmaril@0
   497
bool mapBuilderHandler::readNoteAttr (const QXmlAttributes& a)
insilmaril@0
   498
{	// only for backward compatibility (<1.4.6). Use htmlnote now.
insilmaril@0
   499
	no.clear();
insilmaril@0
   500
	QString fn;
insilmaril@0
   501
	if (!a.value( "href").isEmpty() ) 
insilmaril@0
   502
	{
insilmaril@0
   503
		// Load note
insilmaril@0
   504
		fn=parseHREF(a.value ("href") );
insilmaril@0
   505
		QFile file (fn);
insilmaril@0
   506
		QString s;						// Reading a note
insilmaril@0
   507
insilmaril@2
   508
		if ( !file.open( QIODevice::ReadOnly) )
insilmaril@0
   509
		{
insilmaril@0
   510
			qWarning ("mapBuilderHandler::readNoteAttr:  Couldn't load "+fn);
insilmaril@0
   511
			return false;
insilmaril@0
   512
		}	
insilmaril@0
   513
		QTextStream stream( &file );
insilmaril@0
   514
		QString lines;
insilmaril@2
   515
		while ( !stream.atEnd() ) {
insilmaril@0
   516
			lines += stream.readLine()+"\n"; 
insilmaril@0
   517
		}
insilmaril@0
   518
		file.close();
insilmaril@0
   519
		// Convert to richtext
insilmaril@2
   520
		if ( !Q3StyleSheet::mightBeRichText( lines ) )
insilmaril@0
   521
		{
insilmaril@0
   522
			// Here we are workarounding the QT conversion method:
insilmaril@0
   523
			// convertFromPlainText does not generate valid xml, needed
insilmaril@0
   524
			// for the parser, but just <p> and <br> without closing tags.
insilmaril@0
   525
			// So we have to add those by ourselves
insilmaril@0
   526
			//lines=quotemeta (lines);
insilmaril@2
   527
			lines = Q3StyleSheet::convertFromPlainText( lines, Q3StyleSheetItem::WhiteSpaceNormal );
insilmaril@0
   528
			lines.replace ("<br>","<br />");
insilmaril@0
   529
		}	
insilmaril@0
   530
insilmaril@0
   531
		lines ="<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body>"+lines + "</p></body></html>";
insilmaril@0
   532
		no.setNote (lines);
insilmaril@0
   533
	}		
insilmaril@0
   534
	if (!a.value( "fonthint").isEmpty() ) 
insilmaril@0
   535
		no.setFontHint(a.value ("fonthint") );
insilmaril@0
   536
	if (state == StateMapCenterNote) 	
insilmaril@0
   537
		mc->setNote(no);
insilmaril@0
   538
	else
insilmaril@0
   539
		lastBranch->setNote(no);
insilmaril@0
   540
	return true;
insilmaril@0
   541
}
insilmaril@0
   542
insilmaril@0
   543
bool mapBuilderHandler::readFloatImageAttr (const QXmlAttributes& a)
insilmaril@0
   544
{
insilmaril@2
   545
	lastOO=lastFloat;
insilmaril@2
   546
	
insilmaril@2
   547
	//if (!readOOAttr(a)) return false;
insilmaril@2
   548
insilmaril@0
   549
	if (!a.value( "useOrientation").isEmpty() ) 
insilmaril@0
   550
	{
insilmaril@0
   551
		if (a.value ("useOrientation") =="true")
insilmaril@0
   552
			lastFloat->setUseOrientation (true);
insilmaril@0
   553
		else	
insilmaril@0
   554
			lastFloat->setUseOrientation (false);
insilmaril@0
   555
	}	
insilmaril@0
   556
	if (!a.value( "href").isEmpty() )
insilmaril@0
   557
	{
insilmaril@0
   558
		// Load FloatImage
insilmaril@0
   559
		if (!lastFloat->load (parseHREF(a.value ("href") ) ))
insilmaril@0
   560
		{
insilmaril@0
   561
			QMessageBox::warning( 0, "Warning: " ,
insilmaril@0
   562
				"Couldn't load float image\n"+parseHREF(a.value ("href") ));
insilmaril@0
   563
			lastBranch->removeFloatImage(((FloatImageObj*)(lastFloat)));
insilmaril@0
   564
			lastFloat=NULL;
insilmaril@0
   565
			return true;
insilmaril@0
   566
		}
insilmaril@0
   567
		
insilmaril@0
   568
	}	
insilmaril@0
   569
	if (!a.value( "floatExport").isEmpty() ) 
insilmaril@0
   570
	{
insilmaril@2
   571
		// Only for compatibility. THis is not used since 1.7.11 
insilmaril@2
   572
		if (a.value ("floatExport") =="true")
insilmaril@2
   573
			lastFloat->setFloatExport(true);
insilmaril@0
   574
		else	
insilmaril@0
   575
			lastFloat->setFloatExport (false);
insilmaril@0
   576
	}	
insilmaril@0
   577
	if (!a.value( "zPlane").isEmpty() ) 
insilmaril@0
   578
		lastFloat->setZ (a.value("zPlane").toInt ());
insilmaril@0
   579
    int x,y;
insilmaril@0
   580
    bool okx,oky;
insilmaril@0
   581
	if (!a.value( "relPosX").isEmpty() ) 
insilmaril@0
   582
	{
insilmaril@0
   583
		if (!a.value( "relPosY").isEmpty() ) 
insilmaril@0
   584
		{
insilmaril@0
   585
			// read relPos
insilmaril@0
   586
			x=a.value("relPosX").toInt (&okx, 10);
insilmaril@0
   587
			y=a.value("relPosY").toInt (&oky, 10);
insilmaril@0
   588
			if (okx && oky) 
insilmaril@2
   589
				
insilmaril@2
   590
				{
insilmaril@2
   591
					lastFloat->setRelPos (QPoint (x,y) );
insilmaril@2
   592
					// make sure floats in mapcenter are repositioned to relative pos
insilmaril@2
   593
					if (mc==lastBranch) mc->positionContents();
insilmaril@2
   594
				}
insilmaril@0
   595
			else
insilmaril@0
   596
				// Couldn't read relPos
insilmaril@0
   597
				return false;  
insilmaril@0
   598
		}           
insilmaril@0
   599
	}	
insilmaril@2
   600
	
insilmaril@2
   601
	if (!readOOAttr(a)) return false;
insilmaril@2
   602
insilmaril@2
   603
	if (!a.value ("orgName").isEmpty() )
insilmaril@2
   604
	{
insilmaril@2
   605
		((FloatImageObj*)(lastFloat))->setOriginalFilename (a.value("orgName"));
insilmaril@2
   606
	}
insilmaril@0
   607
	return true;
insilmaril@0
   608
}
insilmaril@0
   609
insilmaril@2
   610
bool mapBuilderHandler::readXLinkAttr (const QXmlAttributes& a)
insilmaril@2
   611
{
insilmaril@2
   612
	QColor col;
insilmaril@2
   613
	bool okx;
insilmaril@2
   614
	bool success=false;
insilmaril@2
   615
	XLinkObj *xlo=new XLinkObj (mc->getCanvas());
insilmaril@2
   616
	if (!a.value( "color").isEmpty() ) 
insilmaril@2
   617
	{
insilmaril@2
   618
		col.setNamedColor(a.value("color"));
insilmaril@2
   619
		xlo->setColor (col);
insilmaril@2
   620
	}
insilmaril@2
   621
insilmaril@2
   622
	if (!a.value( "width").isEmpty() ) 
insilmaril@2
   623
	{
insilmaril@2
   624
		xlo->setWidth(a.value ("width").toInt (&okx, 10));
insilmaril@2
   625
	}
insilmaril@2
   626
insilmaril@2
   627
	if (!a.value( "beginBranch").isEmpty() ) 
insilmaril@2
   628
	{
insilmaril@2
   629
		if (!a.value( "endBranch").isEmpty() ) 
insilmaril@2
   630
		{
insilmaril@2
   631
			LinkableMapObj *lmo=mc->findObjBySelect (a.value( "beginBranch"));
insilmaril@2
   632
			if (lmo && typeid (*lmo)==typeid (BranchObj))
insilmaril@2
   633
			{
insilmaril@2
   634
				xlo->setBegin ((BranchObj*)(lmo));
insilmaril@2
   635
				lmo=mc->findObjBySelect (a.value( "endBranch"));
insilmaril@2
   636
				if (lmo && typeid (*lmo)==typeid (BranchObj))
insilmaril@2
   637
				{
insilmaril@2
   638
					xlo->setEnd ((BranchObj*)(lmo));
insilmaril@2
   639
					xlo->activate();
insilmaril@2
   640
				}
insilmaril@2
   641
			}
insilmaril@2
   642
			success=true; // Not all branches there yet, no error
insilmaril@2
   643
		}           
insilmaril@2
   644
	}	
insilmaril@2
   645
	if (!success) delete (xlo);
insilmaril@2
   646
	return success;
insilmaril@2
   647
}
insilmaril@2
   648
insilmaril@0
   649
bool mapBuilderHandler::readHtmlAttr (const QXmlAttributes& a)
insilmaril@0
   650
{
insilmaril@0
   651
	for (int i=1; i<=a.count(); i++)
insilmaril@0
   652
		htmldata+=" "+a.localName(i-1)+"=\""+a.value(i-1)+"\"";
insilmaril@0
   653
	return true;
insilmaril@0
   654
}
insilmaril@0
   655
insilmaril@0
   656
bool mapBuilderHandler::readSettingAttr (const QXmlAttributes& a)
insilmaril@0
   657
{
insilmaril@0
   658
	if (!a.value( "key").isEmpty() ) 
insilmaril@0
   659
	{
insilmaril@0
   660
		if (!a.value( "value").isEmpty() ) 
insilmaril@0
   661
			settings.setLocalEntry (me->getDestPath(), a.value ("key"), a.value ("value"));
insilmaril@0
   662
		else
insilmaril@0
   663
			return false;
insilmaril@0
   664
		
insilmaril@0
   665
	} else
insilmaril@0
   666
		return false;
insilmaril@0
   667
	
insilmaril@0
   668
	return true;
insilmaril@0
   669
}