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