File: noteobj.cpp

package info (click to toggle)
vym 1.10.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,376 kB
  • ctags: 1,926
  • sloc: cpp: 18,468; xml: 277; sh: 211; perl: 89; makefile: 26
file content (201 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <qfile.h>
#include <qtextstream.h>
#include <qmessagebox.h>
#include <qregexp.h>

#include "noteobj.h"

/////////////////////////////////////////////////////////////////
// NoteObj
/////////////////////////////////////////////////////////////////

NoteObj::NoteObj()
{
	clear();
}

NoteObj::NoteObj(const QString &s)
{
	clear();
	note=s;
}

void NoteObj::copy (NoteObj other)
{
	note=other.note;
	fonthint=other.fonthint;
	filenamehint="";
}

void NoteObj::clear()
{
	note="";
	fonthint="undef";
	filenamehint="";
}

void NoteObj::setNote (const QString &s)
{
	note=s;
}

QString NoteObj::getNote()
{
	return note;
}

QString NoteObj::getNoteASCII()
{
	QString r=note;

	// Remove all <style...> ...</style>
	QRegExp rx ("<style.*>.*</style>");
	rx.setMinimal(true);
	r.replace (rx,"");

	// convert all "<br*>" to "\n"
	rx.setPattern ("<br.*>");
	r.replace (rx,"\n");

	// convert all "</p>" to "\n"
	rx.setPattern ("</p>");
	r.replace (rx,"\n");
	
	// remove all remaining tags 
	rx.setPattern ("<.*>");
	r.replace (rx,"");

	// If string starts with \n now, remove it.
	// It would be wrong in an OOo export for example
	while (r.at(0)=='\n') r.remove (0,1);
	
	// convert "&", "<" and ">"
	rx.setPattern ("&gt;");
	r.replace (rx,">");
	rx.setPattern ("&lt;");
	r.replace (rx,"<");
	rx.setPattern ("&amp;");
	r.replace (rx,"&");
	rx.setPattern ("&quot;");
	r.replace (rx,"\"");

	return r;
}

QString NoteObj::getNoteOpenDoc()
{
	// Evil hack to transform QT Richtext into
	// something which can be used in OpenDoc format
	// 
	// TODO create clean XML transformation which also
	// considers fonts, colors, ...

	QString r=note;

	// convert all "<br*>"
	QRegExp re("<br.*>");
	re.setMinimal(true);
	r.replace (re,"<text:line-break/>");

	// convert all "<p>" 
	re.setPattern ("<p>");
	r.replace (re,"<text:line-break/>");
	
	// Remove all other tags, e.g. paragraphs will be added in 
	// templates used during export
	re.setPattern ("</?html.*>");
	r.replace (re,"");
	re.setPattern ("</?head.*>");
	r.replace (re,"");
	re.setPattern ("</?body.*>");
	r.replace (re,"");
	re.setPattern ("</?meta.*>");
	r.replace (re,"");
	re.setPattern ("</?span.*>");
	r.replace (re,"");
	re.setPattern ("</?p.*>");
	r.replace (re,"");

	r="<text:span text:style-name=\"vym-notestyle\">"+r+"</text:span>";
	return r;
}

void NoteObj::setFontHint (const QString &s)
{
	// only for backward compatibility (pre 1.5 )
	fonthint=s;
}

QString NoteObj::getFontHint()
{
	// only for backward compatibility (pre 1.5 )
	return fonthint;
}

void NoteObj::setFilenameHint (const QString &s)
{
	filenamehint=s;
}

QString NoteObj::getFilenameHint()
{
	return filenamehint;
}

bool NoteObj::isEmpty ()
{
	return note.isEmpty();
}

QString NoteObj::saveToDir ()
{
	// QTextEdit may generate fontnames with unquoted &, like
	// in "Lucida B&H". This is invalid in XML and thus would crash
	// the XML parser

	// More invalid XML is generated with bullet lists:
	// There are 2 <style> tags in one <li>, so we merge them here
	int pos=0;
	bool inbracket=false;
	int begin_bracket;
	bool inquot=false;
	QString n=note;
	while (pos<n.length())
	{
		if (n.mid(pos,1)=="<") 
		{
			inbracket=true;
			begin_bracket=pos;
		}
		if (n.mid(pos,1)==">") 
		{
			inbracket=false;
			QString s=n.mid(begin_bracket,pos-begin_bracket+1);
			int sl=s.length();
			if (s.count("style=\"")>1)
			{
				QRegExp rx("style=\\s*\"(.*)\"\\s*style=\\s*\"(.*)\"");
				rx.setMinimal (true);
				s.replace(rx,"style=\"\\1 \\2\"");
				n.replace (begin_bracket,sl,s);
				pos=pos-(sl-s.length());
			}	
		}	
		if (n.mid(pos,1)=="\"" && inbracket)
		{
			if (!inquot)
				inquot=true;
			else
				inquot=false;
		}
		if (n.mid(pos,1)=="&" && inquot)
		{
			// Now we are inside  <  "  "  >
			n.replace(pos,1,"&amp;");
			pos=pos+3;
		}
		pos++;
	}
	return beginElement ("htmlnote",attribut("fonthint",fonthint)) + "\n"+ n+ "\n" +endElement ("htmlnote");
}