00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qdom.h>
00021
00022 #include "vdocument.h"
00023 #include "vselection.h"
00024 #include "vvisitor.h"
00025 #include "vlayer.h"
00026 #include "vstroke.h"
00027 #include "vdashpattern.h"
00028 #include "vpainter.h"
00029
00030 #include <KoStore.h>
00031 #include <KoPageLayout.h>
00032 #include <KoXmlWriter.h>
00033
00034 #include <kdebug.h>
00035
00036 VDocument::VDocument()
00037 : VObject( 0L ),
00038 m_width(0.), m_height(0.),
00039 m_selectionMode( VDocument::ActiveLayer ),
00040 m_unit( KoUnit::U_MM )
00041 {
00042 m_selection = new VSelection( this );
00043
00044
00045 m_layers.setAutoDelete( true );
00046 m_layers.append( new VLayer( this ) );
00047 m_activeLayer = m_layers.getLast();
00048 m_activeLayer->setSelected( true );
00049
00050 m_saveAsPath = true;
00051 }
00052
00053 VDocument::VDocument( const VDocument& document )
00054 : VObject( document ), m_width(0), m_height(0)
00055 {
00056 m_selection = new VSelection( this );
00057
00058 }
00059
00060 VDocument::~VDocument()
00061 {
00062 delete( m_selection );
00063 }
00064
00065 void
00066 VDocument::drawPage( VPainter *p, const KoPageLayout &pl, bool showPageMargins ) const
00067 {
00068 p->setPen( Qt::black );
00069 p->setBrush( Qt::white );
00070 p->drawRect( 0, 0, m_width, m_height );
00071
00072 p->setPen( Qt::NoPen );
00073 p->setBrush( Qt::black );
00074 p->drawRect( m_width, - 2, 2, m_height );
00075 p->drawRect( 0, - 2, m_width, 2 );
00076
00077
00078 if( m_gridData.isShow )
00079 {
00080 VStroke s( 0, 1 );
00081 s.setColor( m_gridData.color );
00082 double dx = m_gridData.freq.width();
00083 double dy = m_gridData.freq.height();
00084 p->setPen( s );
00085 p->setBrush( Qt::NoBrush );
00086 KoPoint p0( dx, dy );
00087 while( p0.x() < m_width )
00088 {
00089 p->newPath();
00090 p->moveTo( KoPoint( p0.x(), 0 ) );
00091 p->lineTo( KoPoint( p0.x(), m_height ) );
00092 p->strokePath();
00093
00094 p0.rx() += dx;
00095 }
00096 while( p0.y() < m_height )
00097 {
00098 p->newPath();
00099 p->moveTo( KoPoint( 0, p0.y() ) );
00100 p->lineTo( KoPoint( m_width, p0.y() ) );
00101 p->strokePath();
00102
00103 p0.ry() += dy;
00104 }
00105 }
00106
00107 if( showPageMargins )
00108 {
00109 int ml = int( pl.ptLeft );
00110 int mt = int( pl.ptTop );
00111 int mr = int( pl.ptRight );
00112 int mb = int( pl.ptBottom );
00113
00114 VStroke s( 0, 1 );
00115 s.setColor( Qt::blue );
00116 QValueList<float> dashes;
00117 s.dashPattern().setArray( dashes << 5 << 5 );
00118 p->setPen( s );
00119 p->setBrush( Qt::NoBrush );
00120 p->drawRect(ml, mt, m_width-ml-mr, m_height-mt-mb);
00121 }
00122 }
00123
00124 void
00125 VDocument::draw( VPainter *painter, const KoRect* rect ) const
00126 {
00127 QPtrListIterator<VLayer> itr = m_layers;
00128
00129 for ( ; itr.current(); ++itr )
00130 {
00131 itr.current()->draw( painter, rect );
00132 }
00133 }
00134
00135 void
00136 VDocument::insertLayer( VLayer* layer )
00137 {
00138
00139 m_layers.append( layer );
00140 m_activeLayer = layer;
00141 }
00142
00143 void
00144 VDocument::removeLayer( VLayer* layer )
00145 {
00146 m_layers.remove( layer );
00147 if ( m_layers.count() == 0 )
00148 m_layers.append( new VLayer( this ) );
00149 m_activeLayer = m_layers.getLast();
00150 }
00151
00152 bool VDocument::canRaiseLayer( VLayer* layer )
00153 {
00154 int pos = m_layers.find( layer );
00155 return (pos != int( m_layers.count() ) - 1 && pos >= 0 );
00156 }
00157
00158 bool VDocument::canLowerLayer( VLayer* layer )
00159 {
00160 int pos = m_layers.find( layer );
00161 return (pos>0);
00162 }
00163
00164 void
00165 VDocument::raiseLayer( VLayer* layer )
00166 {
00167 int pos = m_layers.find( layer );
00168 if( pos != int( m_layers.count() ) - 1 && pos >= 0 )
00169 {
00170 VLayer* layer = m_layers.take( pos );
00171 m_layers.insert( pos + 1, layer );
00172 }
00173 }
00174
00175 void
00176 VDocument::lowerLayer( VLayer* layer )
00177 {
00178 int pos = m_layers.find( layer );
00179 if ( pos > 0 )
00180 {
00181 VLayer* layer = m_layers.take( pos );
00182 m_layers.insert( pos - 1, layer );
00183 }
00184 }
00185
00186 int
00187 VDocument::layerPos( VLayer* layer )
00188 {
00189 return m_layers.find( layer );
00190 }
00191
00192 void
00193 VDocument::setActiveLayer( VLayer* layer )
00194 {
00195 if ( m_layers.find( layer ) != -1 )
00196 m_activeLayer = layer;
00197 }
00198
00199 void
00200 VDocument::append( VObject* object )
00201 {
00202 m_activeLayer->append( object );
00203 }
00204
00205 QDomDocument
00206 VDocument::saveXML() const
00207 {
00208 QDomDocument doc;
00209 QDomElement me = doc.createElement( "DOC" );
00210 doc.appendChild( me );
00211 save( me );
00212 return doc;
00213 }
00214
00215 void
00216 VDocument::saveOasis( KoStore *store, KoXmlWriter *docWriter, KoGenStyles &mainStyles ) const
00217 {
00218 docWriter->startElement( "draw:page" );
00219 docWriter->addAttribute( "draw:name", name());
00220 docWriter->addAttribute( "draw:id", "page1");
00221 docWriter->addAttribute( "draw:master-page-name", "Default");
00222
00223
00224 VLayerListIterator itr( m_layers );
00225
00226 int index = 0;
00227 for ( ; itr.current(); ++itr )
00228 itr.current()->saveOasis( store, docWriter, mainStyles, ++index );
00229
00230 docWriter->endElement();
00231 }
00232
00233 void
00234 VDocument::save( QDomElement& me ) const
00235 {
00236 me.setAttribute( "mime", "application/x-karbon" ),
00237 me.setAttribute( "version", "0.1" );
00238 me.setAttribute( "editor", "Karbon14" );
00239 me.setAttribute( "syntaxVersion", "0.1" );
00240 if( m_width > 0. )
00241 me.setAttribute( "width", m_width );
00242 if( m_height > 0. )
00243 me.setAttribute( "height", m_height );
00244 me.setAttribute( "unit", KoUnit::unitName( m_unit ) );
00245
00246
00247 VLayerListIterator itr( m_layers );
00248
00249 for ( ; itr.current(); ++itr )
00250 itr.current()->save( me );
00251 }
00252
00253
00254 VDocument*
00255 VDocument::clone() const
00256 {
00257 return new VDocument( *this );
00258 }
00259
00260 void
00261 VDocument::load( const QDomElement& doc )
00262 {
00263 loadXML( doc );
00264 }
00265
00266 bool
00267 VDocument::loadXML( const QDomElement& doc )
00268 {
00269 if( doc.attribute( "mime" ) != "application/x-karbon" ||
00270 doc.attribute( "syntaxVersion" ) != "0.1" )
00271 {
00272 return false;
00273 }
00274
00275 m_layers.clear();
00276
00277 m_width = doc.attribute( "width", "800.0" ).toDouble();
00278 m_height = doc.attribute( "height", "550.0" ).toDouble();
00279
00280 m_unit = KoUnit::unit( doc.attribute( "unit", KoUnit::unitName( m_unit ) ) );
00281
00282 loadDocumentContent( doc );
00283 return true;
00284 }
00285
00286 void
00287 VDocument::loadDocumentContent( const QDomElement& doc )
00288 {
00289 QDomNodeList list = doc.childNodes();
00290 for( uint i = 0; i < list.count(); ++i )
00291 {
00292 if( list.item( i ).isElement() )
00293 {
00294 QDomElement e = list.item( i ).toElement();
00295
00296 if( e.tagName() == "LAYER" )
00297 {
00298 VLayer* layer = new VLayer( this );
00299 layer->load( e );
00300 insertLayer( layer );
00301 }
00302 }
00303 }
00304 }
00305
00306 bool
00307 VDocument::loadOasis( const QDomElement &element, KoOasisLoadingContext &context )
00308 {
00309 return m_layers.current()->loadOasis( element, context );
00310 }
00311
00312 void
00313 VDocument::accept( VVisitor& visitor )
00314 {
00315 visitor.visitVDocument( *this );
00316 }
00317
00318 QString
00319 VDocument::objectName( const VObject *obj ) const
00320 {
00321 QMap<const VObject *, QString>::ConstIterator it = m_objectNames.find( obj );
00322 return it == m_objectNames.end() ? 0L : it.data();
00323 }