noteobj.cpp
author insilmaril
Wed, 22 Aug 2007 07:25:05 +0000
changeset 586 dad3e4c36e7f
parent 554 34520279e086
child 613 8fb5b3956b3e
permissions -rw-r--r--
Added missing images for generation of manual
     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 	QString r=note;
    50 
    51 	// Remove all <style...> ...</style>
    52 	QRegExp rx ("<style.*>.*</style>");
    53 	rx.setMinimal(true);
    54 	r.replace (rx,"");
    55 
    56 	// convert all "<br*>" to "\n"
    57 	rx.setPattern ("<br.*>");
    58 	r.replace (rx,"\n");
    59 
    60 	// convert all "</p>" to "\n"
    61 	rx.setPattern ("</p>");
    62 	r.replace (rx,"\n");
    63 	
    64 	// remove all remaining tags 
    65 	rx.setPattern ("<.*>");
    66 	r.replace (rx,"");
    67 
    68 	// If string starts with \n now, remove it.
    69 	// It would be wrong in an OOo export for example
    70 	while (r.at(0)=='\n') r.remove (0,1);
    71 	
    72 	// convert "&", "<" and ">"
    73 	rx.setPattern ("&gt;");
    74 	r.replace (rx,">");
    75 	rx.setPattern ("&lt;");
    76 	r.replace (rx,"<");
    77 	rx.setPattern ("&amp;");
    78 	r.replace (rx,"&");
    79 	rx.setPattern ("&quot;");
    80 	r.replace (rx,"\"");
    81 
    82 	return r;
    83 }
    84 
    85 QString NoteObj::getNoteOpenDoc()
    86 {
    87 	// Evil hack to transform QT Richtext into
    88 	// something which can be used in OpenDoc format
    89 	// 
    90 	// TODO create clean XML transformation which also
    91 	// considers fonts, colors, ...
    92 
    93 	QString r=note;
    94 
    95 	// convert all "<br*>"
    96 	QRegExp re("<br.*>");
    97 	re.setMinimal(true);
    98 	r.replace (re,"<text:line-break/>");
    99 
   100 	// convert all "<p>" 
   101 	re.setPattern ("<p>");
   102 	r.replace (re,"<text:line-break/>");
   103 	
   104 	// Remove all other tags, e.g. paragraphs will be added in 
   105 	// templates used during export
   106 	re.setPattern ("</?html.*>");
   107 	r.replace (re,"");
   108 	re.setPattern ("</?head.*>");
   109 	r.replace (re,"");
   110 	re.setPattern ("</?body.*>");
   111 	r.replace (re,"");
   112 	re.setPattern ("</?meta.*>");
   113 	r.replace (re,"");
   114 	re.setPattern ("</?span.*>");
   115 	r.replace (re,"");
   116 	re.setPattern ("</?p.*>");
   117 	r.replace (re,"");
   118 
   119 	r="<text:span text:style-name=\"vym-notestyle\">"+r+"</text:span>";
   120 	return r;
   121 }
   122 
   123 void NoteObj::setFontHint (const QString &s)
   124 {
   125 	// only for backward compatibility (pre 1.5 )
   126 	fonthint=s;
   127 }
   128 
   129 QString NoteObj::getFontHint()
   130 {
   131 	// only for backward compatibility (pre 1.5 )
   132 	return fonthint;
   133 }
   134 
   135 void NoteObj::setFilenameHint (const QString &s)
   136 {
   137 	filenamehint=s;
   138 }
   139 
   140 QString NoteObj::getFilenameHint()
   141 {
   142 	return filenamehint;
   143 }
   144 
   145 bool NoteObj::isEmpty ()
   146 {
   147 	return note.isEmpty();
   148 }
   149 
   150 QString NoteObj::saveToDir ()
   151 {
   152 	// QTextEdit may generate fontnames with unquoted &, like
   153 	// in "Lucida B&H". This is invalid in XML and thus would crash
   154 	// the XML parser
   155 
   156 	// More invalid XML is generated with bullet lists:
   157 	// There are 2 <style> tags in one <li>, so we merge them here
   158 	int pos=0;
   159 	bool inbracket=false;
   160 	int begin_bracket;
   161 	bool inquot=false;
   162 	QString n=note;
   163 	while (pos<n.length())
   164 	{
   165 		if (n.mid(pos,1)=="<") 
   166 		{
   167 			inbracket=true;
   168 			begin_bracket=pos;
   169 		}
   170 		if (n.mid(pos,1)==">") 
   171 		{
   172 			inbracket=false;
   173 			QString s=n.mid(begin_bracket,pos-begin_bracket+1);
   174 			int sl=s.length();
   175 			if (s.count("style=\"")>1)
   176 			{
   177 				QRegExp rx("style=\\s*\"(.*)\"\\s*style=\\s*\"(.*)\"");
   178 				rx.setMinimal (true);
   179 				s.replace(rx,"style=\"\\1 \\2\"");
   180 				n.replace (begin_bracket,sl,s);
   181 				pos=pos-(sl-s.length());
   182 			}	
   183 		}	
   184 		if (n.mid(pos,1)=="\"" && inbracket)
   185 		{
   186 			if (!inquot)
   187 				inquot=true;
   188 			else
   189 				inquot=false;
   190 		}
   191 		if (n.mid(pos,1)=="&" && inquot)
   192 		{
   193 			// Now we are inside  <  "  "  >
   194 			n.replace(pos,1,"&amp;");
   195 			pos=pos+3;
   196 		}
   197 		pos++;
   198 	}
   199 	return beginElement ("htmlnote",attribut("fonthint",fonthint)) + "\n"+ n+ "\n" +endElement ("htmlnote");
   200 }
   201