summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmoutputdevice.h
blob: 25b3c58b2259eb30145a63c789da2fe790f18b6a (plain)
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
//-*-C++-*-
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2003 by Andreas Zehender
    email                : [email protected]
**************************************************************************

**************************************************************************
*                                                                        *
*  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.                                   *
*                                                                        *
**************************************************************************/


#ifndef PMOUTPUTDEVICE_H
#define PMOUTPUTDEVICE_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "pmserializer.h"

#include <qstring.h>

class QTextStream;
class PMPovrayFormat;

/**
 * Output class for povray code.
 *
 * The class @ref PMPovrayFormat or a base class has factory methods
 * to create an instance of this class. The output device uses the
 * registered serialization methods of this povray format to serialize
 * objects.
 *
 * This class handles the indentation and position of brackets
 */

class PMOutputDevice : public PMSerializer
{
public:
   /**
    * Creates an PMOutputDevice that serializes the povray code
    * to the device
    */
   PMOutputDevice( QIODevice* dev, PMPovrayFormat* format );
   /** */
   virtual ~PMOutputDevice( );

   /** */
   virtual QString description( ) const;
   /** */
   virtual void serialize( PMObject* o );
   /** */
   virtual void close( );

   /**
    * Writes the povray object type, an open bracket to the text stream
    * and indents the next lines
    */
   void objectBegin( const QString& type );
   /**
    * Begins a declare with the identifier id
    */
   void declareBegin( const QString& id );
   /**
    * Writes an closing bracket to the text stream
    * and decreases the indentation
    */
   void objectEnd( );
   /**
    * Writes a single line to the text stream. Don't include
    * newlines in the string or indentation will not work properly.
    *
    * Adds a newline before the string.
    */
   void writeLine( const QString& str );
   /**
    * Writes the string to the text stream
    */
   void write( const QString& str );
   /**
    * Writes a new line to the text stream and indents the next line.
    */
   void newLine( );
   /**
    * Writes a comment string to the text stream
    */
   void writeComment( const QString& text );
   /**
    * Writes a semicolon after a call to objectEnd( )
    */
   void writeSemicolon( );
   /**
    * Writes a special name comment to the text stream, if the
    * name is not empty
    */
   void writeName( const QString& name );

   /**
    * Returns the basic indentation offset
    */
   static unsigned int indentationOffset( ) { return s_indentOffset; }
   /**
    * Sets the basic indentation offset
    */
   static void setIndentationOffset( unsigned int offset )
   { s_indentOffset = offset; }

   /**
    * If set to true, the open bracket after an object begin will be
    * written behind the object type, otherwise in a new line.
    */
   static void setBracketBehindType( bool yes ) { s_bracketBehindType = yes; }
   /**
    * Returns true if the open bracket after an object begin will be
    * written behind the object type
    */
   static bool bracketBehindType( ) { return s_bracketBehindType; }

   /**
    * Correctly escapes the string and puts quotation marks at the begin
    * and end of the string.
    *
    * Escapes only not escaped characters. "\"" and "\\" in the string
    * are not escaped.
    */
   static QString escapeAndQuoteString( const QString& s );

   /**
    * Calls the serialization method for the object o and class
    * given by the meta object.
    *
    * If no method for this class was registered in the corresponding
    * @ref PMPovrayFormat, an error is added the message list.
    */
   void callSerialization( const PMObject* o, const PMMetaObject* mo );

private:
   static unsigned int s_indentOffset;
   static bool s_bracketBehindType;

   PMPovrayFormat* m_pFormat;
   unsigned int m_indentation;
   QString m_indentString;
   QTextStream m_stream;
   bool m_lastWasComment;
   bool m_pendingNewLine;
   bool m_objectSeparation;
};


#endif