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