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