1 /***************************************************************************
2  *   (C) 2006-2007 Marius Roets <roets.marius@gmail.com>                   *
3  *   (C) 2007-2009 Michal Rudolf <mrudolf@kdewebdev.org>                  *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #ifndef OUTPUT_H_INCLUDED
12 #define OUTPUT_H_INCLUDED
13 
14 #include "gamex.h"
15 #include "outputoptions.h"
16 #include "database.h"
17 #include "filter.h"
18 
19 #include <QtCore>
20 #include <QtGui/QImage>
21 
22 typedef QImage (*BoardRenderingFunc)(const BoardX &b, QSize pxSize);
23 
24 /** @ingroup Core
25 The Output class converts game to various formats.
26 Usage:
27 @code
28 GameX game;
29 PgnDatabase db;
30 db.loadGame(0,game);
31 Output o(Output::Html,"/usr/local/data/template-file.template");
32 o.output(&game);
33 @endcode
34 */
35 
36 class Output : public QObject
37 {
38     Q_OBJECT
39 public:
40     /** The different types of markup that can be used.
41      * The settings for each is set in the template file.
42      * @see setTemplateFile() */
43     enum MarkupType
44     {
45         MarkupHeaderBlock,
46         MarkupNotationBlock,
47         MarkupResult,
48         MarkupDiagram,
49         MarkupNoFormat,
50         MarkupColumnStyleMainline,
51         MarkupColumnStyleMove,
52         MarkupColumnStyleRow,
53         MarkupMainLineMove,
54         MarkupMainLine,
55         MarkupVariationMove,
56         MarkupVariationInline,
57         MarkupVariationResume,
58         MarkupVariationResume1,
59         MarkupVariationResume2,
60         MarkupVariationResume3,
61         MarkupVariationResume4,
62         MarkupVariationResume5,
63         MarkupVariationResume6,
64         MarkupVariationResume7,
65         MarkupVariationResume8,
66         MarkupVariationResume9,
67         MarkupVariationIndent,
68         MarkupVariationIndent1,
69         MarkupNag,
70         MarkupAnnotationInline,
71         MarkupAnnotationIndent,
72         MarkupPreAnnotationInline,
73         MarkupPreAnnotationIndent,
74         MarkupHeaderLine,
75         MarkupHeaderTagName,
76         MarkupHeaderTagValue,
77         MarkupWhiteTag,
78         MarkupBlackTag,
79         MarkupEventTag,
80         MarkupDateTag,
81         MarkupSiteTag,
82         MarkupResultTag,
83         MarkupRoundTag,
84         MarkupMate
85     };
86     /** The supported output types */
87     enum OutputType
88     {
89         Html, /**< Exports the game in Html format */
90         Pgn, /**< Exports the game in PGN format */
91         Latex, /**< Exports the game in Latex format */
92         NotationWidget /**< Exports the game in format appropriate for the notation widget */
93     };
94     enum MoveToWrite
95     {
96         PreviousMove,
97         NextMove
98     };
99     enum CommentType
100     {
101         Precomment,
102         Comment
103     };
104     /* enum CommentIndentOption {
105      *    Always,
106      *    OnlyMainline,
107      *    Never
108      * }; */
109     /** Constructor.
110      * Creates an output object for the given output type. Output can then
111      * be generated by calling one of the output() methods
112      * @param output The format of the output to be generated
113      * @param pathToTemplateFile The full path to the file that contains the
114      *        the template for the output to be generated
115      * @see Output::OutputType */
116     Output(OutputType output, BoardRenderingFunc renderer, const QString& pathToTemplateFile = "");
117     ~Output();
118 
119     /** Create the output for the given game
120      * @return A string containing the game in the specified format
121      * @param game A pointer to the game object being output */
122     QString output(const GameX* game, bool upToCurrentMove = false);
123     /** Create the output for the given game
124      * @param filename The filename that the output will be written to.
125      * @param filter A GameX object. Exported, using the output(GameX* game) method */
126     void output(const QString& filename, const GameX& game);
127     /** Create the output for the given filter
128      * @param filename The filename that the output will be written to.
129      * @param filter A Filter object. All games in the filter will be output, one
130      *               after the other, using the output(GameX* game) method */
131     void output(const QString& filename, FilterX& filter);
132     /** Create the output for the given database
133      * @param filename The filename that the output will be written to.
134      * @param database A pointer to a database object. All games in the database will be output, one
135      *               after the other, using the output(GameX* game) method */
136     void output(const QString& filename, Database& database);
137 
138     /** Append output to a closed file */
139     bool append(const QString& filename, GameX& game);
140     /** Append a database to a closed file */
141     void append(const QString& filename, Database& database);
142 
143     /** User definable settings.
144      * Sets the filename of the file that contains the template that will be used
145      * when creating the output. See example template files for syntax.
146      * @param filename The full path to the file containing the template for the output */
147     void setTemplateFile(QString filename = "");
148     /** Static list of objects. */
149     static QMap<OutputType, QString>& getFormats();
150 
151 signals:
152     /** Operation progress. */
153     void progress(int);
154 protected:
155     QString outputTags(const GameX *game);
156 private:
157     /* User definable settings */
158     OutputOptions m_options;
159     /** The name of the current template file */
160     QString m_templateFilename;
161 
162     /* Internally used */
163     /** Function to render board into image */
164     BoardRenderingFunc m_renderer;
165     /** Text to be written at the top of the output */
166     QString m_header;
167     /** Text to be written at the bottom of the output */
168     QString m_footer;
169     /** The type of output that the object will generate */
170     OutputType m_outputType;
171     /** Indicator whether or not to write the move number, when it is black to move */
172     bool m_dirtyBlack;
173     /** Keep track of the current level of variation, for indent purposes */
174     int m_currentVariationLevel;
175     /** Character/string used for newline */
176     QString m_newlineChar;
177     /** Pointer to the game being exported */
178     GameX m_game;
179     /** Map containing the different types of outputs available, and a description of each */
180     static QMap<OutputType, QString> m_outputMap;
181     /** Map containing the start markup tag for each markup type */
182     QMap<MarkupType, QString> m_startTagMap;
183     /** Map containing the end markup tag for each markup type */
184     QMap<MarkupType, QString> m_endTagMap;
185     QMap<MarkupType, bool> m_expandable;
186 
187     /* Setting and retrieving of option. Methods to inteface
188      * with OutputOptions class.
189      */
190     /* Setting values */
191     /** Set option optionName to value optionValue */
192     bool setOption(const QString& optionName, bool optionValue);
193     /** Set option optionName to value optionValue */
194     bool setOption(const QString& optionName, int optionValue);
195     /** Set option optionName to value optionValue */
196     bool setOption(const QString& optionName, const QString& optionValue);
197     /* Retrieving values */
198     /** Return the value of option optionName as integer */
199     int getOptionAsInt(const QString& optionName);
200     /** Return the value of option optionName as QString */
201     QString getOptionAsString(const QString& optionName);
202     /** Return the value of option optionName as boolean */
203     bool getOptionAsBool(const QString& optionName);
204     /** Return the description of option optionName */
205     QString getOptionDescription(const QString& optionName);
206     /** Return a list of all options */
207     QStringList getOptionList();
208     /** Sets the start and end tag for a certain markup type */
209     void setMarkupTag(MarkupType type, const QString& startTag, const QString& endTag);
210     /** Returns the start and end tag for a certain markup type in startTag and endTag */
211     void markupTag(MarkupType type, QString& startTag, QString& endTag);
212     /** Read tag settings from user settings */
213     void readConfig();
214     /** Write tag settings to user settings */
215     void writeConfig();
216     /** Read the template file */
217     void readTemplateFile(const QString& path);
218     /** Sets the default settings for the specific output format */
219     void initialize();
220     /** Reload default tag settings */
221     void reset();
222 
223     /** Create the output for the given filter
224      * @param out A textstream that will be used to write the results to
225      * @param filter A Filter object. All games in the filter will be output, one
226      *               after the other, using the output(GameX* game) method */
227     void output(QTextStream& out, FilterX& filter);
228     /** Create the output for the given database
229      * @param out A textstream that will be used to write the results to
230      * @param database A pointer to a database object. All games in the database will be output, one
231      *               after the other, using the output(GameX* game) method */
232     void output(QTextStream& out, Database& database);
233 
234     /** Output of a single game - requires postProcessing */
235     QString outputGame(const GameX *g, bool upToCurrentMove);
236     /** postProcessing of a game output or a dataBase output */
237     void postProcessOutput(QString& text) const;
238 
239     /* Writing Methods */
240     /** writes a comment associated with a game with no moves */
241     QString writeGameComment(QString comment) const;
242     /** Writes a diagram */
243     QString writeDiagram(int n) const;
244     /** Writes a single move including nag and annotation */
245     QString writeMove(MoveToWrite moveToWrite = NextMove);
246     /** Writes a variation, including sub variations */
247     QString writeMainLine(MoveId upToNode);
248     /** Writes a variation, including sub variations */
249     QString writeVariation();
250     /** Writes a game tag */
251     QString writeTag(const QString& tagName, const QString& tagValue) const;
252     /** Writes all game tags */
253     QString writeAllTags() const;
254     /** Writes basic Tags for HTML */
255     QString writeBasicTagsHTML() const;
256     /** Writes comment. @p mvno keeps a string representing move number (used for indentation. */
257     QString writeComment(const QString& comment, const QString& mvno, CommentType type = Comment);
258 
259 };
260 
261 #endif
262 
263