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