karbon

vdocument.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqdom.h>
21
22#include "vdocument.h"
23#include "vselection.h"
24#include "vvisitor.h"
25#include "vlayer.h"
26#include "vstroke.h"
27#include "vdashpattern.h"
28#include "vpainter.h"
29
30#include <KoStore.h>
31#include <KoPageLayout.h>
32#include <KoXmlWriter.h>
33
34#include <kdebug.h>
35
37 : VObject( 0L ),
38 m_width(0.), m_height(0.),
39 m_selectionMode( VDocument::ActiveLayer ),
40 m_unit( KoUnit::U_MM )
41{
42 m_selection = new VSelection( this );
43
44 // create a layer. we need at least one:
45 m_layers.setAutoDelete( true );
46 m_layers.append( new VLayer( this ) );
47 m_activeLayer = m_layers.getLast();
48 m_activeLayer->setSelected( true );
49
50 m_saveAsPath = true;
51}
52
54 : VObject( document ), m_width(0), m_height(0)
55{
56 m_selection = new VSelection( this );
57// TODO
58}
59
61{
62 delete( m_selection );
63}
64
65void
66VDocument::drawPage( VPainter *p, const KoPageLayout &pl, bool showPageMargins ) const
67{
68 p->setPen( TQt::black );
69 p->setBrush( TQt::white );
70 p->drawRect( 0, 0, m_width, m_height );
71
72 p->setPen( TQt::NoPen );
73 p->setBrush( TQt::black );
74 p->drawRect( m_width, - 2, 2, m_height );
75 p->drawRect( 0, - 2, m_width, 2 );
76 //p->drawRect( 0, m_height - 1, m_width, 1 );
77 // Draw Grid
78 if( m_gridData.isShow )
79 {
80 VStroke s( 0, 1 );
81 s.setColor( m_gridData.color );
82 double dx = m_gridData.freq.width();
83 double dy = m_gridData.freq.height();
84 p->setPen( s );
85 p->setBrush( TQt::NoBrush );
86 KoPoint p0( dx, dy );
87 while( p0.x() < m_width )
88 {
89 p->newPath();
90 p->moveTo( KoPoint( p0.x(), 0 ) );
91 p->lineTo( KoPoint( p0.x(), m_height ) );
92 p->strokePath();
93
94 p0.rx() += dx;
95 }
96 while( p0.y() < m_height )
97 {
98 p->newPath();
99 p->moveTo( KoPoint( 0, p0.y() ) );
100 p->lineTo( KoPoint( m_width, p0.y() ) );
101 p->strokePath();
102
103 p0.ry() += dy;
104 }
105 }
106 // Draw page margins
107 if( showPageMargins )
108 {
109 int ml = int( pl.ptLeft );
110 int mt = int( pl.ptTop );
111 int mr = int( pl.ptRight );
112 int mb = int( pl.ptBottom );
113
114 VStroke s( 0, 1 );
115 s.setColor( TQt::blue );
116 TQValueList<float> dashes;
117 s.dashPattern().setArray( dashes << 5 << 5 );
118 p->setPen( s );
119 p->setBrush( TQt::NoBrush );
120 p->drawRect(ml, mt, m_width-ml-mr, m_height-mt-mb);
121 }
122}
123
124void
125VDocument::draw( VPainter *painter, const KoRect* rect ) const
126{
127 TQPtrListIterator<VLayer> itr = m_layers;
128
129 for ( ; itr.current(); ++itr )
130 {
131 itr.current()->draw( painter, rect );
132 }
133}
134
135void
137{
138// if ( pos == -1 || !m_layers.insert( layer, pos ))
139 m_layers.append( layer );
140 m_activeLayer = layer;
141} // VDocument::insertLayer
142
143void
145{
146 m_layers.remove( layer );
147 if ( m_layers.count() == 0 )
148 m_layers.append( new VLayer( this ) );
149 m_activeLayer = m_layers.getLast();
150} // VDocument::removeLayer
151
153{
154 int pos = m_layers.find( layer );
155 return (pos != int( m_layers.count() ) - 1 && pos >= 0 );
156}
157
159{
160 int pos = m_layers.find( layer );
161 return (pos>0);
162}
163
164void
166{
167 int pos = m_layers.find( layer );
168 if( pos != int( m_layers.count() ) - 1 && pos >= 0 )
169 {
170 VLayer* layer = m_layers.take( pos );
171 m_layers.insert( pos + 1, layer );
172 }
173} // VDocument::raiseLayer
174
175void
177{
178 int pos = m_layers.find( layer );
179 if ( pos > 0 )
180 {
181 VLayer* layer = m_layers.take( pos );
182 m_layers.insert( pos - 1, layer );
183 }
184} // VDocument::lowerLayer
185
186int
188{
189 return m_layers.find( layer );
190} // VDocument::layerPos
191
192void
194{
195 if ( m_layers.find( layer ) != -1 )
196 m_activeLayer = layer;
197} // VDocument::setActiveLayer
198
199void
201{
202 m_activeLayer->append( object );
203}
204
205TQDomDocument
206VDocument::saveXML() const
207{
208 TQDomDocument doc;
209 TQDomElement me = doc.createElement( "DOC" );
210 doc.appendChild( me );
211 save( me );
212 return doc;
213}
214
215void
216VDocument::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles ) const
217{
218 docWriter->startElement( "draw:page" );
219 docWriter->addAttribute( "draw:name", name());
220 docWriter->addAttribute( "draw:id", "page1");
221 docWriter->addAttribute( "draw:master-page-name", "Default");
222
223 // save objects:
224 VLayerListIterator itr( m_layers );
225
226 int index = 0;
227 for ( ; itr.current(); ++itr )
228 itr.current()->saveOasis( store, docWriter, mainStyles, ++index );
229
230 docWriter->endElement(); // draw:page
231}
232
233void
234VDocument::save( TQDomElement& me ) const
235{
236 me.setAttribute( "mime", "application/x-karbon" ),
237 me.setAttribute( "version", "0.1" );
238 me.setAttribute( "editor", "Karbon14" );
239 me.setAttribute( "syntaxVersion", "0.1" );
240 if( m_width > 0. )
241 me.setAttribute( "width", m_width );
242 if( m_height > 0. )
243 me.setAttribute( "height", m_height );
244 me.setAttribute( "unit", KoUnit::unitName( m_unit ) );
245
246 // save objects:
247 VLayerListIterator itr( m_layers );
248
249 for ( ; itr.current(); ++itr )
250 itr.current()->save( me );
251}
252
253
256{
257 return new VDocument( *this );
258}
259
260void
261VDocument::load( const TQDomElement& doc )
262{
263 loadXML( doc );
264}
265
266bool
267VDocument::loadXML( const TQDomElement& doc )
268{
269 if( doc.attribute( "mime" ) != "application/x-karbon" ||
270 doc.attribute( "syntaxVersion" ) != "0.1" )
271 {
272 return false;
273 }
274
275 m_layers.clear();
276
277 m_width = doc.attribute( "width", "800.0" ).toDouble();
278 m_height = doc.attribute( "height", "550.0" ).toDouble();
279
280 m_unit = KoUnit::unit( doc.attribute( "unit", KoUnit::unitName( m_unit ) ) );
281
282 loadDocumentContent( doc );
283 return true;
284}
285
286void
287VDocument::loadDocumentContent( const TQDomElement& doc )
288{
289 TQDomNodeList list = doc.childNodes();
290 for( uint i = 0; i < list.count(); ++i )
291 {
292 if( list.item( i ).isElement() )
293 {
294 TQDomElement e = list.item( i ).toElement();
295
296 if( e.tagName() == "LAYER" )
297 {
298 VLayer* layer = new VLayer( this );
299 layer->load( e );
300 insertLayer( layer );
301 }
302 }
303 }
304}
305
306bool
307VDocument::loadOasis( const TQDomElement &element, KoOasisLoadingContext &context )
308{
309 return m_layers.current()->loadOasis( element, context );
310}
311
312void
314{
315 visitor.visitVDocument( *this );
316}
317
318TQString
320{
321 TQMap<const VObject *, TQString>::ConstIterator it = m_objectNames.find( obj );
322 return it == m_objectNames.end() ? 0L : it.data();
323}
All non-visual, static doc info is in here.
Definition vdocument.h:53
int layerPos(VLayer *layer)
Returns the position of the specified layer.
bool canLowerLayer(VLayer *layer)
Checks if specified layer can be lowered.
VDocument()
Constructs a new document.
Definition vdocument.cpp:36
void setActiveLayer(VLayer *layer)
Sets the active layer.
virtual void save(TQDomElement &element) const
Save this object's state to xml.
virtual VDocument * clone() const
Create an exact copy of this object.
void drawPage(VPainter *painter, const KoPageLayout &pl, bool drawPageMargins) const
Draw the document frame to a painting device.
Definition vdocument.cpp:66
void lowerLayer(VLayer *layer)
Lowers the layer.
void removeLayer(VLayer *layer)
Removes the layer.
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
bool canRaiseLayer(VLayer *layer)
Checks if specified layer can be raised.
virtual ~VDocument()
Destroys the document and all of the layers.
Definition vdocument.cpp:60
virtual bool loadOasis(const TQDomElement &element, KoOasisLoadingContext &context)
Load this object's state from OpenDocument and initialize this object accordingly.
@ ActiveLayer
selection within the active layer
Definition vdocument.h:57
virtual void draw(VPainter *painter, const KoRect *rect) const
Draw the object to a painting device.
virtual void accept(VVisitor &visitor)
Accept a VVisitor.
void append(VObject *object)
Appends a new object to the active layer.
TQString objectName(const VObject *obj) const
Returns custom name of specified object.
void raiseLayer(VLayer *layer)
Raises the layer.
void insertLayer(VLayer *layer)
Inserts a new layer.
void take(const VObject &object)
Removes the reference to child object, not the object itself.
Definition vgroup.cpp:364
void append(VObject *object)
Appends a new child object.
Definition vgroup.cpp:372
VLayer manages a set of vobjects.
Definition vlayer.h:37
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vlayer.cpp:168
VObject(VObject *parent, VState state=edit)
Constructs a new object that is child of parent and has the given state.
Definition vobject.cpp:36
virtual TQString name() const
Returns the name of the object.
Definition vobject.cpp:225
VDocument * document() const
Return document the object belongs to.
Definition vobject.cpp:216
VSelection manages a set of selected vobjects.
Definition vselection.h:62
Manages stroke properties.
Definition vstroke.h:49
The abstract visitor class.
Definition vvisitor.h:51
virtual void visitVDocument(VDocument &document)
Visit method for a VDocument.
Definition vvisitor.cpp:41