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