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