-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatplaintext.cpp
149 lines (121 loc) · 3.94 KB
/
formatplaintext.cpp
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
/*
This file is part of Todoodle.
Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "formatplaintext.h"
#include "dbg.h"
#include "textformats.h"
#include <QFile>
#include <QTextDocument>
#include <QTextStream>
#include <QTextFrame>
#include <QTextCursor>
#include <QTextList>
#include <QTextCodec>
FormatPlainText::FormatPlainText( QTextDocument *document )
: mDocument( document )
{
}
bool FormatPlainText::save( const QString &f )
{
QFile file( f );
if ( !file.open( QIODevice::WriteOnly ) ) return false;
QTextStream ts( &file );
ts.setCodec( QTextCodec::codecForName( "utf8" ) );
ts << toString();
return true;
}
QString FormatPlainText::toString()
{
QTextFrame *rootFrame = mDocument->rootFrame();
return frameToString( rootFrame );
}
QString FormatPlainText::frameToString( QTextFrame *frame )
{
QString out;
QTextFrame::iterator it;
for( it = frame->begin(); it != frame->end(); ++it ) {
QTextBlock block = it.currentBlock();
if ( block.isValid() ) {
bool isTitle = false;
bool isSubTitle = false;
int titleIndent = 0;
QTextCursor c( block );
if ( TextFormats::isTitle( c ) ) {
isTitle = true;
} else if ( TextFormats::isSubTitle( c ) ) {
isSubTitle = true;
}
QTextBlockFormat blockFormat = block.blockFormat();
if ( blockFormat.isValid() ) {
QTextList *list = block.textList();
if ( list ) {
QTextListFormat f = list->format();
out += indent( f.indent() * 4 );
titleIndent += f.indent() * 4;
switch( f.style() ) {
default:
case QTextListFormat::ListDisc:
out += "- ";
break;
case QTextListFormat::ListDecimal:
out += "1 ";
break;
}
} else {
out += indent( blockFormat.indent() * 4 );
titleIndent += blockFormat.indent() * 4;
}
}
QString outText;
QTextBlock::iterator it2;
for( it2 = block.begin(); it2 != block.end(); ++it2 ) {
QTextFragment fragment = it2.fragment();
if ( !fragment.isValid() ) continue;
QString text = fragment.text();
for( int i = 0; i < text.size(); ++i ) {
if ( text.at( i ) == 0xfffc ) {
QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
if ( imageFormat.isValid() ) {
if ( imageFormat.name().contains( "done" ) ) outText += "DONE: ";
else outText += "TODO: ";
} else {
dbg() << "NO IMAGE FORMAT" << endl;
}
} else {
outText += text.at( i );
}
}
}
out += outText + "\n";
if ( isTitle ) {
out += indent( titleIndent ) + indent( outText.length(), "=" ) + "\n";
} else if ( isSubTitle ) {
out += indent( titleIndent ) + indent( outText.length(), "-" ) + "\n";
}
}
if ( it.currentFrame() ) {
out += frameToString( it.currentFrame() );
}
}
return out;
}
QString FormatPlainText::indent( int indent, const QString &symbol )
{
QString out;
for( int i = 0; i < indent; ++i ) {
out.append( symbol );
}
return out;
}