]> git.sven.stormbind.net Git - sven/vym.git/blob - vymtext.cpp
fb5159c21fd9deb7eba9dba4fc9de50498a09ed3
[sven/vym.git] / vymtext.cpp
1 #include "vymtext.h"
2 #include "misc.h"
3
4 #include <QRegExp>
5 #include <QDebug>
6 #include <QTextDocument>
7
8 /////////////////////////////////////////////////////////////////
9 // VymText  Base class for Vymnotes and Headings
10 /////////////////////////////////////////////////////////////////
11
12 VymText::VymText()
13 {
14     clear();
15 }
16 VymText::VymText(const VymText &other)
17 {
18     clear();
19     copy (other);
20     return;
21 }
22
23 VymText::VymText(const QString &s)
24 {
25     clear();
26     setPlainText (s);
27 }
28
29 bool VymText::operator== (const VymText &other)
30 {
31     if ( text == other.text &&
32          fonthint == other.fonthint &&
33         textmode == other.textmode &&
34         filenamehint == other.filenamehint
35     )
36         return true;
37     else
38         return false;
39 }
40
41 void VymText::operator= (const VymText &other)
42 {
43     copy (other);
44 }
45
46 void VymText::copy (const VymText &other)
47 {
48     text = other.text;
49     fonthint = other.fonthint;
50     filenamehint = other.filenamehint;
51     textmode = other.textmode;
52     color = other.color;
53 }
54
55 void VymText::clear()
56 {
57     text = "";
58     fonthint = "undef";
59     filenamehint = "";
60     textmode = AutoText;
61     color = Qt::black;
62 }
63
64 void VymText::setRichText(bool b)
65 {
66     if (b)
67         textmode = RichText;
68     else
69         textmode = PlainText;
70 }
71
72 bool VymText::isRichText()const
73 {
74     if (textmode == RichText)
75         return true;
76     else
77         return false;
78 }
79
80 void VymText::setText (const QString &s)
81 {
82     text = s;
83 }
84
85 void VymText::setRichText (const QString &s)
86 {
87     text = s;
88     textmode = RichText;
89 }
90
91 void VymText::setPlainText (const QString &s)
92 {
93     text = s;
94     textmode = PlainText;
95 }
96
97 void VymText::setAutoText (const QString &s)
98 {
99     clear();
100     if (Qt::mightBeRichText (s))
101         setRichText (s);
102     else
103         setPlainText (s);
104 }
105
106 QString VymText::getText() const
107 {
108     return text;
109 }
110
111 QString VymText::getTextASCII() const
112 {
113     return getTextASCII ("",80);
114 }
115
116 QString VymText::getTextASCII(QString indent, const int &) const //FIXME-3 use width
117 {
118     if (text.isEmpty()) return text;
119
120     int width = 80;
121     QString s;
122     QRegExp rx;
123     rx.setMinimal(true);
124
125     if (isRichText())
126         s = text;
127     else
128     {
129         if ( fonthint == "fixed")
130         {
131             s = text; 
132         } else
133         {
134             // Wordwrap
135
136             QString newnote;
137             QString curline;
138             uint n=0;
139             while ( (int)n < text.length() )
140             {
141                 curline = curline + text.at(n);
142                 if ( text.at(n) == '\n' )
143                 {
144                     s = s + curline ;
145                     curline = "";
146                 }
147
148                 if (curline.length() > width)
149                 {
150                     // Try to find last previous whitespace in curline
151                     uint i = curline.length() - 1;
152                     while ( i> 0 )
153                     {
154                         if ( curline.at(i) == ' ' )
155                         {
156                             s = s + curline.left(i) + '\n';
157                             curline = curline.right( curline.length() - i - 1 );
158                             break;
159                         }
160                         i--;
161                         if ( i == 0 )
162                         {
163                             // Cannot break this line into smaller parts
164                             s = s + curline;
165                             curline = "";
166                         }
167                     }
168                 }
169                 n++;
170             }
171             s = s + curline;
172         }
173
174         // Indent lines
175         rx.setPattern("^");
176         s = s.replace (rx,indent);
177         rx.setPattern("\n");
178         s = s.replace (rx, "\n" + indent) + "\n";
179
180         return s.trimmed();
181     }
182
183     // Remove all <style...> ...</style>
184     rx.setPattern("<style.*>.*</style>");
185     s.replace (rx,"");
186
187     // convert all "<br*>" to "\n"
188     rx.setPattern ("<br.*>");
189     s.replace (rx,"\n");
190
191     // convert all "</p>" to "\n"
192     rx.setPattern ("</p>");
193     s.replace (rx,"\n");
194
195     // remove all remaining tags
196     rx.setPattern ("<.*>");
197     s.replace (rx,"");
198
199     // If string starts with \n now, remove it.
200     // It would be wrong in an OOo export for example
201     while ( s.at(0) == '\n' ) s.remove (0,1);
202
203     // convert "&", "<" and ">"
204     rx.setPattern ("&gt;");
205     s.replace (rx,">");
206     rx.setPattern ("&lt;");
207     s.replace (rx,"<");
208     rx.setPattern ("&amp;");
209     s.replace (rx,"&");
210     rx.setPattern ("&quot;");
211     s.replace (rx,"\"");
212
213     // Indent everything
214     rx.setPattern ("^\n");
215     s.replace (rx,indent);
216     s = indent + s;   // Don't forget first line
217
218 /* FIXME-3  wrap text at width
219     if (fonthint !="fixed")
220     {
221     }
222 */
223     return s;
224 }
225
226 void VymText::setFontHint (const QString &s)
227 {
228     // only for backward compatibility (pre 1.5 )
229     fonthint=s;
230 }
231
232 QString VymText::getFontHint() const
233 {
234     // only for backward compatibility (pre 1.5 )
235     return fonthint;
236 }
237
238 void VymText::setFilenameHint (const QString &s)
239 {
240     filenamehint=s;
241 }
242
243 QString VymText::getFilenameHint() const
244 {
245     return filenamehint;
246 }
247
248 bool VymText::isEmpty ()
249 {
250     return text.isEmpty();
251 }
252
253 void VymText::setColor(QColor col)
254 {
255     color = col;
256 }
257
258 QColor VymText::getColor()
259 {
260     return color;
261 }
262
263 QString VymText::getAttributes()
264 {
265     QString ret;
266     if (textmode == RichText)
267         ret += attribut("textMode","richText");
268     else
269     {
270         ret += attribut("textMode","plainText");
271         ret += " " + attribut("fonthint", fonthint);
272     }
273     ret += " " + attribut("textColor", color.name() );
274     return ret;
275 }
276
277 QString VymText::saveToDir ()
278 {
279     return getCDATA( text );
280 }