noteobj.cpp
author insilmaril
Mon, 30 Jul 2007 09:47:29 +0000
changeset 578 610cc6d70683
parent 554 34520279e086
child 613 8fb5b3956b3e
permissions -rw-r--r--
1.9.4 New base class for XML based file parsing (vym & Freemind)
insilmaril@0
     1
#include <qfile.h>
insilmaril@0
     2
#include <qtextstream.h>
insilmaril@0
     3
#include <qmessagebox.h>
insilmaril@0
     4
#include <qregexp.h>
insilmaril@0
     5
insilmaril@0
     6
#include "noteobj.h"
insilmaril@0
     7
insilmaril@0
     8
/////////////////////////////////////////////////////////////////
insilmaril@0
     9
// NoteObj
insilmaril@0
    10
/////////////////////////////////////////////////////////////////
insilmaril@0
    11
insilmaril@0
    12
NoteObj::NoteObj()
insilmaril@0
    13
{
insilmaril@0
    14
	clear();
insilmaril@0
    15
}
insilmaril@0
    16
insilmaril@185
    17
NoteObj::NoteObj(const QString &s)
insilmaril@185
    18
{
insilmaril@185
    19
	clear();
insilmaril@185
    20
	note=s;
insilmaril@185
    21
}
insilmaril@185
    22
insilmaril@0
    23
void NoteObj::copy (NoteObj other)
insilmaril@0
    24
{
insilmaril@0
    25
	note=other.note;
insilmaril@0
    26
	fonthint=other.fonthint;
insilmaril@0
    27
	filenamehint="";
insilmaril@0
    28
}
insilmaril@0
    29
insilmaril@0
    30
void NoteObj::clear()
insilmaril@0
    31
{
insilmaril@0
    32
	note="";
insilmaril@0
    33
	fonthint="undef";
insilmaril@0
    34
	filenamehint="";
insilmaril@0
    35
}
insilmaril@0
    36
insilmaril@0
    37
void NoteObj::setNote (const QString &s)
insilmaril@0
    38
{
insilmaril@0
    39
	note=s;
insilmaril@0
    40
}
insilmaril@0
    41
insilmaril@0
    42
QString NoteObj::getNote()
insilmaril@0
    43
{
insilmaril@0
    44
	return note;
insilmaril@0
    45
}
insilmaril@0
    46
insilmaril@185
    47
QString NoteObj::getNoteASCII()
insilmaril@185
    48
{
insilmaril@185
    49
	QString r=note;
insilmaril@185
    50
insilmaril@551
    51
	// Remove all <style...> ...</style>
insilmaril@551
    52
	QRegExp rx ("<style.*>.*</style>");
insilmaril@551
    53
	rx.setMinimal(true);
insilmaril@551
    54
	r.replace (rx,"");
insilmaril@551
    55
insilmaril@185
    56
	// convert all "<br*>" to "\n"
insilmaril@551
    57
	rx.setPattern ("<br.*>");
insilmaril@551
    58
	r.replace (rx,"\n");
insilmaril@185
    59
insilmaril@185
    60
	// convert all "</p>" to "\n"
insilmaril@551
    61
	rx.setPattern ("</p>");
insilmaril@551
    62
	r.replace (rx,"\n");
insilmaril@185
    63
	
insilmaril@185
    64
	// remove all remaining tags 
insilmaril@551
    65
	rx.setPattern ("<.*>");
insilmaril@551
    66
	r.replace (rx,"");
insilmaril@185
    67
insilmaril@185
    68
	// If string starts with \n now, remove it.
insilmaril@185
    69
	// It would be wrong in an OOo export for example
insilmaril@185
    70
	while (r.at(0)=='\n') r.remove (0,1);
insilmaril@185
    71
	
insilmaril@185
    72
	// convert "&", "<" and ">"
insilmaril@551
    73
	rx.setPattern ("&gt;");
insilmaril@551
    74
	r.replace (rx,">");
insilmaril@551
    75
	rx.setPattern ("&lt;");
insilmaril@551
    76
	r.replace (rx,"<");
insilmaril@551
    77
	rx.setPattern ("&amp;");
insilmaril@551
    78
	r.replace (rx,"&");
insilmaril@551
    79
	rx.setPattern ("&quot;");
insilmaril@551
    80
	r.replace (rx,"\"");
insilmaril@185
    81
insilmaril@185
    82
	return r;
insilmaril@185
    83
}
insilmaril@185
    84
insilmaril@185
    85
QString NoteObj::getNoteOpenDoc()
insilmaril@185
    86
{
insilmaril@185
    87
	// Evil hack to transform QT Richtext into
insilmaril@185
    88
	// something which can be used in OpenDoc format
insilmaril@185
    89
	// 
insilmaril@185
    90
	// TODO create clean XML transformation which also
insilmaril@185
    91
	// considers fonts, colors, ...
insilmaril@185
    92
insilmaril@185
    93
	QString r=note;
insilmaril@185
    94
insilmaril@185
    95
	// convert all "<br*>"
insilmaril@185
    96
	QRegExp re("<br.*>");
insilmaril@185
    97
	re.setMinimal(true);
insilmaril@185
    98
	r.replace (re,"<text:line-break/>");
insilmaril@185
    99
insilmaril@185
   100
	// convert all "<p>" 
insilmaril@185
   101
	re.setPattern ("<p>");
insilmaril@185
   102
	r.replace (re,"<text:line-break/>");
insilmaril@185
   103
	
insilmaril@185
   104
	// Remove all other tags, e.g. paragraphs will be added in 
insilmaril@185
   105
	// templates used during export
insilmaril@185
   106
	re.setPattern ("</?html.*>");
insilmaril@185
   107
	r.replace (re,"");
insilmaril@185
   108
	re.setPattern ("</?head.*>");
insilmaril@185
   109
	r.replace (re,"");
insilmaril@185
   110
	re.setPattern ("</?body.*>");
insilmaril@185
   111
	r.replace (re,"");
insilmaril@185
   112
	re.setPattern ("</?meta.*>");
insilmaril@185
   113
	r.replace (re,"");
insilmaril@185
   114
	re.setPattern ("</?span.*>");
insilmaril@185
   115
	r.replace (re,"");
insilmaril@185
   116
	re.setPattern ("</?p.*>");
insilmaril@185
   117
	r.replace (re,"");
insilmaril@185
   118
insilmaril@185
   119
	r="<text:span text:style-name=\"vym-notestyle\">"+r+"</text:span>";
insilmaril@185
   120
	return r;
insilmaril@185
   121
}
insilmaril@185
   122
insilmaril@0
   123
void NoteObj::setFontHint (const QString &s)
insilmaril@0
   124
{
insilmaril@0
   125
	// only for backward compatibility (pre 1.5 )
insilmaril@0
   126
	fonthint=s;
insilmaril@0
   127
}
insilmaril@0
   128
insilmaril@0
   129
QString NoteObj::getFontHint()
insilmaril@0
   130
{
insilmaril@0
   131
	// only for backward compatibility (pre 1.5 )
insilmaril@0
   132
	return fonthint;
insilmaril@0
   133
}
insilmaril@0
   134
insilmaril@0
   135
void NoteObj::setFilenameHint (const QString &s)
insilmaril@0
   136
{
insilmaril@0
   137
	filenamehint=s;
insilmaril@0
   138
}
insilmaril@0
   139
insilmaril@0
   140
QString NoteObj::getFilenameHint()
insilmaril@0
   141
{
insilmaril@0
   142
	return filenamehint;
insilmaril@0
   143
}
insilmaril@0
   144
insilmaril@0
   145
bool NoteObj::isEmpty ()
insilmaril@0
   146
{
insilmaril@0
   147
	return note.isEmpty();
insilmaril@0
   148
}
insilmaril@0
   149
insilmaril@0
   150
QString NoteObj::saveToDir ()
insilmaril@0
   151
{
insilmaril@0
   152
	// QTextEdit may generate fontnames with unquoted &, like
insilmaril@0
   153
	// in "Lucida B&H". This is invalid in XML and thus would crash
insilmaril@0
   154
	// the XML parser
insilmaril@551
   155
insilmaril@551
   156
	// More invalid XML is generated with bullet lists:
insilmaril@551
   157
	// There are 2 <style> tags in one <li>, so we merge them here
insilmaril@551
   158
	int pos=0;
insilmaril@0
   159
	bool inbracket=false;
insilmaril@551
   160
	int begin_bracket;
insilmaril@0
   161
	bool inquot=false;
insilmaril@251
   162
	QString n=note;
insilmaril@251
   163
	while (pos<n.length())
insilmaril@0
   164
	{
insilmaril@551
   165
		if (n.mid(pos,1)=="<") 
insilmaril@551
   166
		{
insilmaril@551
   167
			inbracket=true;
insilmaril@551
   168
			begin_bracket=pos;
insilmaril@551
   169
		}
insilmaril@551
   170
		if (n.mid(pos,1)==">") 
insilmaril@551
   171
		{
insilmaril@551
   172
			inbracket=false;
insilmaril@551
   173
			QString s=n.mid(begin_bracket,pos-begin_bracket+1);
insilmaril@551
   174
			int sl=s.length();
insilmaril@551
   175
			if (s.count("style=\"")>1)
insilmaril@551
   176
			{
insilmaril@551
   177
				QRegExp rx("style=\\s*\"(.*)\"\\s*style=\\s*\"(.*)\"");
insilmaril@551
   178
				rx.setMinimal (true);
insilmaril@551
   179
				s.replace(rx,"style=\"\\1 \\2\"");
insilmaril@551
   180
				n.replace (begin_bracket,sl,s);
insilmaril@551
   181
				pos=pos-(sl-s.length());
insilmaril@551
   182
			}	
insilmaril@551
   183
		}	
insilmaril@251
   184
		if (n.mid(pos,1)=="\"" && inbracket)
insilmaril@0
   185
		{
insilmaril@0
   186
			if (!inquot)
insilmaril@0
   187
				inquot=true;
insilmaril@0
   188
			else
insilmaril@0
   189
				inquot=false;
insilmaril@0
   190
		}
insilmaril@251
   191
		if (n.mid(pos,1)=="&" && inquot)
insilmaril@0
   192
		{
insilmaril@0
   193
			// Now we are inside  <  "  "  >
insilmaril@251
   194
			n.replace(pos,1,"&amp;");
insilmaril@251
   195
			pos=pos+3;
insilmaril@0
   196
		}
insilmaril@0
   197
		pos++;
insilmaril@0
   198
	}
insilmaril@251
   199
	return beginElement ("htmlnote",attribut("fonthint",fonthint)) + "\n"+ n+ "\n" +endElement ("htmlnote");
insilmaril@0
   200
}
insilmaril@0
   201