noteobj.cpp
author insilmaril
Wed, 18 Oct 2006 10:45:00 +0000
changeset 393 053b8645e3e9
parent 251 795d0eb5700f
child 551 2913f96fd233
permissions -rw-r--r--
undo/redo for delete BranchObj. Added calling commands in testmenu
     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 	// convert all "<br*>" to "\n"
    52 	QRegExp re("<br.*>");
    53 	re.setMinimal(true);
    54 	r.replace (re,"\n");
    55 
    56 	// convert all "</p>" to "\n"
    57 	re.setPattern ("</p>");
    58 	r.replace (re,"\n");
    59 	
    60 	// remove all remaining tags 
    61 	re.setPattern ("<.*>");
    62 	r.replace (re,"");
    63 
    64 	// If string starts with \n now, remove it.
    65 	// It would be wrong in an OOo export for example
    66 	while (r.at(0)=='\n') r.remove (0,1);
    67 	
    68 	// convert "&", "<" and ">"
    69 	re.setPattern ("&gt;");
    70 	r.replace (re,">");
    71 	re.setPattern ("&lt;");
    72 	r.replace (re,"<");
    73 	re.setPattern ("&amp;");
    74 	r.replace (re,"&");
    75 	re.setPattern ("&quot;");
    76 	r.replace (re,"\"");
    77 
    78 	return r;
    79 }
    80 
    81 QString NoteObj::getNoteOpenDoc()
    82 {
    83 	// Evil hack to transform QT Richtext into
    84 	// something which can be used in OpenDoc format
    85 	// 
    86 	// TODO create clean XML transformation which also
    87 	// considers fonts, colors, ...
    88 
    89 	QString r=note;
    90 
    91 	// convert all "<br*>"
    92 	QRegExp re("<br.*>");
    93 	re.setMinimal(true);
    94 	r.replace (re,"<text:line-break/>");
    95 
    96 	// convert all "<p>" 
    97 	re.setPattern ("<p>");
    98 	r.replace (re,"<text:line-break/>");
    99 	
   100 	// Remove all other tags, e.g. paragraphs will be added in 
   101 	// templates used during export
   102 	re.setPattern ("</?html.*>");
   103 	r.replace (re,"");
   104 	re.setPattern ("</?head.*>");
   105 	r.replace (re,"");
   106 	re.setPattern ("</?body.*>");
   107 	r.replace (re,"");
   108 	re.setPattern ("</?meta.*>");
   109 	r.replace (re,"");
   110 	re.setPattern ("</?span.*>");
   111 	r.replace (re,"");
   112 	re.setPattern ("</?p.*>");
   113 	r.replace (re,"");
   114 
   115 	r="<text:span text:style-name=\"vym-notestyle\">"+r+"</text:span>";
   116 	return r;
   117 }
   118 
   119 void NoteObj::setFontHint (const QString &s)
   120 {
   121 	// only for backward compatibility (pre 1.5 )
   122 	fonthint=s;
   123 }
   124 
   125 QString NoteObj::getFontHint()
   126 {
   127 	// only for backward compatibility (pre 1.5 )
   128 	return fonthint;
   129 }
   130 
   131 void NoteObj::setFilenameHint (const QString &s)
   132 {
   133 	filenamehint=s;
   134 }
   135 
   136 QString NoteObj::getFilenameHint()
   137 {
   138 	return filenamehint;
   139 }
   140 
   141 bool NoteObj::isEmpty ()
   142 {
   143 	return note.isEmpty();
   144 }
   145 
   146 QString NoteObj::saveToDir ()
   147 {
   148 	// QTextEdit may generate fontnames with unquoted &, like
   149 	// in "Lucida B&H". This is invalid in XML and thus would crash
   150 	// the XML parser
   151 	uint pos=0;
   152 	bool inbracket=false;
   153 	bool inquot=false;
   154 	QString n=note;
   155 	while (pos<n.length())
   156 	{
   157 		if (n.mid(pos,1)=="<") inbracket=true;
   158 		if (n.mid(pos,1)==">") inbracket=false;
   159 		if (n.mid(pos,1)=="\"" && inbracket)
   160 		{
   161 			if (!inquot)
   162 				inquot=true;
   163 			else
   164 				inquot=false;
   165 		}
   166 		if (n.mid(pos,1)=="&" && inquot)
   167 		{
   168 			// Now we are inside  <  "  "  >
   169 			n.replace(pos,1,"&amp;");
   170 			pos=pos+3;
   171 		}
   172 		pos++;
   173 	}
   174 	return beginElement ("htmlnote",attribut("fonthint",fonthint)) + "\n"+ n+ "\n" +endElement ("htmlnote");
   175 }
   176