noteobj.cpp
author insilmaril
Mon, 14 Jan 2008 16:26:59 +0000
changeset 646 371945eb6d31
parent 645 2abfdb7e85f4
child 663 827d334d55f1
permissions -rw-r--r--
Added french manual by Claude
     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 /* FIXME	wrap text at width
    88 	if (fonthint !="fixed")
    89 	{
    90 	}
    91 */	
    92 	return r;
    93 }
    94 
    95 QString NoteObj::getNoteOpenDoc()
    96 {
    97 	// Evil hack to transform QT Richtext into
    98 	// something which can be used in OpenDoc format
    99 	// 
   100 	// TODO create clean XML transformation which also
   101 	// considers fonts, colors, ...
   102 
   103 	QString r=note;
   104 
   105 	// convert all "<br*>"
   106 	QRegExp re("<br.*>");
   107 	re.setMinimal(true);
   108 	r.replace (re,"<text:line-break/>");
   109 
   110 	// convert all "<p>" 
   111 	re.setPattern ("<p>");
   112 	r.replace (re,"<text:line-break/>");
   113 	
   114 	// Remove all other tags, e.g. paragraphs will be added in 
   115 	// templates used during export
   116 	re.setPattern ("</?html.*>");
   117 	r.replace (re,"");
   118 	re.setPattern ("</?head.*>");
   119 	r.replace (re,"");
   120 	re.setPattern ("</?body.*>");
   121 	r.replace (re,"");
   122 	re.setPattern ("</?meta.*>");
   123 	r.replace (re,"");
   124 	re.setPattern ("</?span.*>");
   125 	r.replace (re,"");
   126 	re.setPattern ("</?p.*>");
   127 	r.replace (re,"");
   128 
   129 	r="<text:span text:style-name=\"vym-notestyle\">"+r+"</text:span>";
   130 	return r;
   131 }
   132 
   133 void NoteObj::setFontHint (const QString &s)
   134 {
   135 	// only for backward compatibility (pre 1.5 )
   136 	fonthint=s;
   137 }
   138 
   139 QString NoteObj::getFontHint()
   140 {
   141 	// only for backward compatibility (pre 1.5 )
   142 	return fonthint;
   143 }
   144 
   145 void NoteObj::setFilenameHint (const QString &s)
   146 {
   147 	filenamehint=s;
   148 }
   149 
   150 QString NoteObj::getFilenameHint()
   151 {
   152 	return filenamehint;
   153 }
   154 
   155 bool NoteObj::isEmpty ()
   156 {
   157 	return note.isEmpty();
   158 }
   159 
   160 QString NoteObj::saveToDir ()
   161 {
   162 	// QTextEdit may generate fontnames with unquoted &, like
   163 	// in "Lucida B&H". This is invalid in XML and thus would crash
   164 	// the XML parser
   165 
   166 	// More invalid XML is generated with bullet lists:
   167 	// There are 2 <style> tags in one <li>, so we merge them here
   168 	int pos=0;
   169 	bool inbracket=false;
   170 	int begin_bracket=0;
   171 	bool inquot=false;
   172 	QString n=note;
   173 	while (pos<n.length())
   174 	{
   175 		if (n.mid(pos,1)=="<") 
   176 		{
   177 			inbracket=true;
   178 			begin_bracket=pos;
   179 		}
   180 		if (n.mid(pos,1)==">") 
   181 		{
   182 			inbracket=false;
   183 			QString s=n.mid(begin_bracket,pos-begin_bracket+1);
   184 			int sl=s.length();
   185 			if (s.count("style=\"")>1)
   186 			{
   187 				QRegExp rx("style=\\s*\"(.*)\"\\s*style=\\s*\"(.*)\"");
   188 				rx.setMinimal (true);
   189 				s.replace(rx,"style=\"\\1 \\2\"");
   190 				n.replace (begin_bracket,sl,s);
   191 				pos=pos-(sl-s.length());
   192 			}	
   193 		}	
   194 		if (n.mid(pos,1)=="\"" && inbracket)
   195 		{
   196 			if (!inquot)
   197 				inquot=true;
   198 			else
   199 				inquot=false;
   200 		}
   201 		if (n.mid(pos,1)=="&" && inquot)
   202 		{
   203 			// Now we are inside  <  "  "  >
   204 			n.replace(pos,1,"&amp;");
   205 			pos=pos+3;
   206 		}
   207 		pos++;
   208 	}
   209 	return beginElement ("htmlnote",attribut("fonthint",fonthint)) + "\n"+ n+ "\n" +endElement ("htmlnote");
   210 }
   211