-
Notifications
You must be signed in to change notification settings - Fork 1
/
latexdocument.cpp
243 lines (201 loc) · 5.09 KB
/
latexdocument.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
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
#include "latexdocument.h"
#include <QDebug>
LatexDocument::LatexDocument():
mLastColumnNumber( 0 ),
mMaxRows( 20 )
{
}
void LatexDocument::header()
{
mPage += QString( "\\documentclass[a4paper,10pt]{article}\n"
"\\usepackage[utf8]{inputenc}\n"
"\\usepackage[czech]{babel}\n"
"\\usepackage{a4wide}\n"
"\\usepackage{tabulary}\n"
"\\begin{document}\n" );
}
void LatexDocument::footer()
{
mPage += QString( "\\end{document}\n" );
}
void LatexDocument::heading1( const QString &text )
{
mPage += QString( "\\section*{%1}\n" ).arg( text );
}
void LatexDocument::heading2( const QString &text )
{
mPage += QString( "\\subsection*{%1}\n" ).arg( text );
}
void LatexDocument::heading3( const QString &text )
{
mPage += QString( "\\subsubsection*{%1}\n" ).arg( text );
}
void LatexDocument::beginItemize()
{
mPage += QString( "\\begin{itemize}\n" );
}
void LatexDocument::endItemize()
{
mPage += QString( "\\end{itemize}\n" );
}
void LatexDocument::beginItem()
{
mPage += QString( "\\item " );
}
void LatexDocument::endItem()
{
mPage += QString( "\n" );
}
void LatexDocument::item( const QString &text )
{
mPage += QString( "\\item %1\n" ).arg( text );
}
void LatexDocument::beginTable()
{
mLastTableHeader.clear();
mLastTableContent.clear();
}
void LatexDocument::endTable()
{
QString table;
if ( mLastColumnNumber > 0 )
{
bool tooLongTable = mLastTableContent.size() < mMaxRows ? false : true;
QString beginTable = "\\begin{tabulary}{\\textwidth}{";
QString endTable = "\\end{tabulary}\n";
QString header = mLastTableHeader + "\\\\\\hline\\hline\n";
for ( int i = 0; i < mLastColumnNumber; i++ )
{
beginTable += "L";
}
beginTable += "}\n";
if ( tooLongTable )
{
int rows = 0;
while ( rows + mMaxRows < mLastTableContent.size() )
{
table += beginTable;
table += header;
for ( int i = rows; i < rows + mMaxRows; i++ )
{
table += mLastTableContent.at( i );
table += "\\\\\n";
}
table += endTable;
table += "\\newpage";
rows += mMaxRows;
}
table += beginTable;
table += header;
for ( int i = rows; i < mLastTableContent.size(); i++ )
{
table += mLastTableContent.at( i );
table += "\\\\\n";
}
table += endTable;
}
else
{
table += beginTable;
table += header;
table += mLastTableContent.join( "\\\\\n" );
table += endTable;
}
}
mPage += table;
}
void LatexDocument::tableHeader( const QStringList &columns )
{
mLastColumnNumber = columns.size();
if ( mLastColumnNumber > 0 )
{
QString tableHeader;
for ( QStringList::ConstIterator it = columns.begin(); it != columns.end(); ++it )
{
QString columnHeader = QString( "\\textbf{%1} & " ).arg( *it );
QStringList words = ( *it ).split( " " );
if ( words.size() == 1 )
{
columnHeader = QString( "\\mbox{\\textbf{%1}} & " ).arg( *it );
}
tableHeader += columnHeader;
}
tableHeader.chop( 2 );
mLastTableHeader = tableHeader;
}
}
void LatexDocument::tableRow( const QStringList &columns )
{
if ( mLastColumnNumber != columns.size() )
{
qWarning() << "inconsistent number of columns: " << mLastColumnNumber << columns.size();
return;
}
if ( mLastColumnNumber > 0 )
{
QString tableRow = QString( "%1 " ).arg( columns.at( 0 ) );
for ( QStringList::ConstIterator it = ++( columns.begin() ); it != columns.end(); ++it )
{
tableRow += QString( "& %1 " ).arg( *it );
}
mLastTableContent.append( tableRow );
}
}
void LatexDocument::tableRowOneColumnSpan( const QString &text )
{
if ( mLastColumnNumber != 0 )
{
mLastTableContent.append( QString( "\\multicolumn{%1}{l}{%2}" ).arg( mLastColumnNumber ).arg( text ) );
}
}
QString LatexDocument::link( const QString &href, const QString &text )
{
return text;
}
QString LatexDocument::superscript( const QString &text )
{
return QString( "$^{%1}$" ).arg( text );
}
QString LatexDocument::newLine()
{
return QString( "\\newline\n" );
}
void LatexDocument::keyValueTable( const KeyValList &content )
{
mPage += QString( "\\begin{tabulary}{\\textwidth}{LL}\n" );
for ( KeyValList::ConstIterator it = content.begin(); it != content.end(); ++it )
{
mPage += QString( "\\textbf{%1} & %2 \\\\\n" ).arg( it->first ).arg( it->second );
}
mPage += QString( "\\end{tabulary}\n" );
}
void LatexDocument::paragraph( const QString &text )
{
mPage += QString( "\n%1\n" ).arg( text );
}
void LatexDocument::table( const TableContent &content, bool header )
{
beginTable();
int i = 0;
if ( header && !content.isEmpty() )
{
tableHeader( content.at( 0 ) );
i++;
}
for ( ; i < content.size(); i++ )
{
tableRow( content.at( i ) );
}
endTable();
}
void LatexDocument::text( const QString &text )
{
mPage += QString( "%1\n" ).arg( text );
}
void LatexDocument::discardLastBeginTable()
{
}
bool LatexDocument::isLastTableEmpty()
{
return false;
}