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