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