Menu

[c54480]: / vymtext.cpp  Maximize  Restore  History

Download this file

282 lines (241 with data), 5.7 kB

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