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