22#include <tqfileinfo.h>
24#include <tqpaintdevicemetrics.h>
29#include <tdetempfile.h>
30#include <KoTemplateChooseDia.h>
31#include <KoStoreDevice.h>
32#include <KoOasisStyles.h>
33#include <KoOasisLoadingContext.h>
34#include <KoXmlWriter.h>
37#include <KoOasisSettings.h>
38#include <KoMainWindow.h>
40#include "karbon_factory.h"
41#include "karbon_part.h"
42#include "karbon_part_iface.h"
43#include "karbon_view.h"
47#include "vpainterfactory.h"
48#include "vselection.h"
51#include "vdocumentdocker.h"
52#include "vtoolcontroller.h"
53#include "KoApplication.h"
55#include "commands/vtransformcmd.h"
60KarbonPart::KarbonPart( TQWidget* parentWidget,
const char* widgetName,
61 TQObject* parent,
const char* name,
bool singleViewMode )
62 : KoDocument( parentWidget, widgetName, parent, name, singleViewMode )
64 setInstance( KarbonFactory::instance(),
false );
65 setTemplateType(
"karbon_template" );
66 m_bShowStatusBar =
true;
70 connect( m_commandHistory, TQ_SIGNAL( documentRestored() ),
this, TQ_SLOT( slotDocumentRestored() ) );
71 connect( m_commandHistory, TQ_SIGNAL( commandExecuted(
VCommand * ) ),
this, TQ_SLOT( slotCommandExecuted(
VCommand * ) ) );
77 m_maxRecentFiles = 10;
83 m_pageLayout.format = KoPageFormat::defaultFormat();
84 m_pageLayout.orientation = PG_PORTRAIT;
85 m_pageLayout.ptWidth = MM_TO_POINT( KoPageFormat::width( m_pageLayout.format, m_pageLayout.orientation ) );
86 m_pageLayout.ptHeight = MM_TO_POINT( KoPageFormat::height( m_pageLayout.format, m_pageLayout.orientation ) );
87 m_doc.setWidth( m_pageLayout.ptWidth );
88 m_doc.setHeight( m_pageLayout.ptHeight );
90 m_doc.selection()->showHandle();
91 m_doc.selection()->setSelectObjects();
93 m_doc.selection()->selectNodes();
96KarbonPart::~KarbonPart()
99 delete m_commandHistory;
103DCOPObject* KarbonPart::dcopObject()
106 dcop =
new KarbonPartIface(
this );
112KarbonPart::setPageLayout( KoPageLayout& layout, KoUnit::Unit _unit )
114 m_pageLayout = layout;
116 m_doc.
setWidth( m_pageLayout.ptWidth );
117 m_doc.
setHeight( m_pageLayout.ptHeight );
123 if (flags==KoDocument::InitDocEmpty)
128 KoTemplateChooseDia::ReturnType result;
130 KoTemplateChooseDia::DialogType dlgtype;
131 if (flags != KoDocument::InitDocFileNew)
132 dlgtype = KoTemplateChooseDia::Everything;
134 dlgtype = KoTemplateChooseDia::OnlyTemplates;
136 result = KoTemplateChooseDia::choose( KarbonFactory::instance(), file, dlgtype,
"karbon_template", parentWidget );
138 if( result == KoTemplateChooseDia::Template )
141 bool ok = loadNativeFormat( file );
143 showLoadingErrorDialog();
147 else if( result == KoTemplateChooseDia::Empty )
151 else if( result == KoTemplateChooseDia::File )
154 return openURL( url );
161KarbonPart::createViewInstance( TQWidget* parent,
const char* name )
163 KarbonView *result =
new KarbonView(
this, parent, name );
168KarbonPart::removeView( KoView *view )
170 kdDebug(38000) <<
"KarbonPart::removeView" << endl;
171 KoDocument::removeView( view );
174double getAttribute(TQDomElement &element,
const char *attributeName,
double defaultValue)
177 if ( ( value = element.attribute( attributeName ) ) != TQString() )
178 return value.toDouble();
183int getAttribute(TQDomElement &element,
const char *attributeName,
int defaultValue)
186 if ( ( value = element.attribute( attributeName ) ) != TQString() )
187 return value.toInt();
195 bool success =
false;
197 TQDomElement doc = document.documentElement();
201 m_doc.loadDocumentContent( doc );
205 success = m_doc.loadXML( doc );
210 TQDomElement paper = doc.namedItem(
"PAPER" ).toElement();
211 if ( !paper.isNull() )
213 m_pageLayout.format =
static_cast<KoFormat
>( getAttribute( paper,
"format", 0 ) );
214 m_pageLayout.orientation =
static_cast<KoOrientation
>( getAttribute( paper,
"orientation", 0 ) );
216 if( m_pageLayout.format == PG_CUSTOM )
218 m_pageLayout.ptWidth = m_doc.
width();
219 m_pageLayout.ptHeight = m_doc.
height();
223 m_pageLayout.ptWidth = getAttribute( paper,
"width", 0.0 );
224 m_pageLayout.ptHeight = getAttribute( paper,
"height", 0.0 );
229 m_pageLayout.ptWidth = getAttribute( doc,
"width", 595.277);
230 m_pageLayout.ptHeight = getAttribute( doc,
"height", 841.891 );
233 kdDebug() <<
" ptWidth=" << m_pageLayout.ptWidth << endl;
234 kdDebug() <<
" ptHeight=" << m_pageLayout.ptHeight << endl;
235 TQDomElement borders = paper.namedItem(
"PAPERBORDERS" ).toElement();
236 if( !borders.isNull() )
238 if( borders.hasAttribute(
"ptLeft" ) )
239 m_pageLayout.ptLeft = borders.attribute(
"ptLeft" ).toDouble();
240 if( borders.hasAttribute(
"ptTop" ) )
241 m_pageLayout.ptTop = borders.attribute(
"ptTop" ).toDouble();
242 if( borders.hasAttribute(
"ptRight" ) )
243 m_pageLayout.ptRight = borders.attribute(
"ptRight" ).toDouble();
244 if( borders.hasAttribute(
"ptBottom" ) )
245 m_pageLayout.ptBottom = borders.attribute(
"ptBottom" ).toDouble();
248 setUnit( m_doc.
unit() );
256 TQDomDocument doc = m_doc.saveXML();
257 TQDomElement me = doc.documentElement();
258 TQDomElement paper = doc.createElement(
"PAPER" );
259 me.appendChild( paper );
260 paper.setAttribute(
"format",
static_cast<int>( m_pageLayout.format ) );
261 paper.setAttribute(
"pages", pageCount() );
262 paper.setAttribute(
"width", m_pageLayout.ptWidth );
263 paper.setAttribute(
"height", m_pageLayout.ptHeight );
264 paper.setAttribute(
"orientation",
static_cast<int>( m_pageLayout.orientation ) );
266 TQDomElement paperBorders = doc.createElement(
"PAPERBORDERS" );
267 paperBorders.setAttribute(
"ptLeft", m_pageLayout.ptLeft );
268 paperBorders.setAttribute(
"ptTop", m_pageLayout.ptTop );
269 paperBorders.setAttribute(
"ptRight", m_pageLayout.ptRight );
270 paperBorders.setAttribute(
"ptBottom", m_pageLayout.ptBottom );
271 paper.appendChild(paperBorders);
277KarbonPart::loadOasis(
const TQDomDocument &doc, KoOasisStyles &styles,
const TQDomDocument &settings, KoStore *store )
279 kdDebug(38000) <<
"Start loading OASIS document..." << doc.toString() << endl;
281 TQDomElement contents = doc.documentElement();
282 kdDebug(38000) <<
"Start loading OASIS document..." << contents.text() << endl;
283 kdDebug(38000) <<
"Start loading OASIS contents..." << contents.lastChild().localName() << endl;
284 kdDebug(38000) <<
"Start loading OASIS contents..." << contents.lastChild().namespaceURI() << endl;
285 kdDebug(38000) <<
"Start loading OASIS contents..." << contents.lastChild().isElement() << endl;
286 TQDomElement body( KoDom::namedItemNS( contents, KoXmlNS::office,
"body" ) );
287 kdDebug(38000) <<
"Start loading OASIS document..." << body.text() << endl;
290 kdDebug(38000) <<
"No office:body found!" << endl;
291 setErrorMessage( i18n(
"Invalid OASIS document. No office:body tag found." ) );
295 body = KoDom::namedItemNS( body, KoXmlNS::office,
"drawing");
298 kdDebug(38000) <<
"No office:drawing found!" << endl;
299 setErrorMessage( i18n(
"Invalid OASIS document. No office:drawing tag found." ) );
303 TQDomElement page( KoDom::namedItemNS( body, KoXmlNS::draw,
"page" ) );
306 kdDebug(38000) <<
"No office:drawing found!" << endl;
307 setErrorMessage( i18n(
"Invalid OASIS document. No draw:page tag found." ) );
311 TQString masterPageName =
"Standard";
312 TQDomElement *master = styles.masterPages()[ masterPageName ];
314 master = styles.masterPages()[
"Default" ];
318 TQDictIterator<TQDomElement> it( styles.masterPages() );
319 master = it.current();
322 const TQDomElement *style = master ? styles.findStyle( master->attributeNS( KoXmlNS::style,
"page-layout-name", TQString() ) ) : 0;
325 m_pageLayout.loadOasis( *style );
326 m_doc.
setWidth( m_pageLayout.ptWidth );
327 m_doc.
setHeight( m_pageLayout.ptHeight );
332 KoOasisLoadingContext context(
this, styles, store );
337 mat.translate( 0, -m_doc.
height() );
338 VTransformCmd trafo( 0L, mat );
339 trafo.visit( m_doc );
341 loadOasisSettings( settings );
347KarbonPart::loadOasisSettings(
const TQDomDocument&settingsDoc )
349 if ( settingsDoc.isNull() )
351 KoOasisSettings settings( settingsDoc );
352 KoOasisSettings::Items viewSettings = settings.itemSet(
"view-settings" );
353 if ( !viewSettings.isNull() )
355 setUnit(KoUnit::unit(viewSettings.parseConfigItemString(
"unit")));
362KarbonPart::saveOasis( KoStore *store, KoXmlWriter *manifestWriter )
364 if( !store->open(
"content.xml" ) )
367 KoStoreDevice storeDev( store );
368 KoXmlWriter* docWriter = createOasisXmlWriter( &storeDev,
"office:document-content" );
369 KoGenStyles mainStyles;
371 KoGenStyle pageLayout = m_pageLayout.saveOasis();
372 TQString layoutName = mainStyles.lookup( pageLayout,
"PL" );
373 KoGenStyle masterPage( KoGenStyle::STYLE_MASTER );
374 masterPage.addAttribute(
"style:page-layout-name", layoutName );
375 mainStyles.lookup( masterPage,
"Default", KoGenStyles::DontForceNumbering );
377 KTempFile contentTmpFile;
378 contentTmpFile.setAutoDelete(
true );
379 TQFile* tmpFile = contentTmpFile.file();
380 KoXmlWriter contentTmpWriter( tmpFile, 1 );
382 contentTmpWriter.startElement(
"office:body" );
383 contentTmpWriter.startElement(
"office:drawing" );
385 m_doc.saveOasis( store, &contentTmpWriter, mainStyles );
387 contentTmpWriter.endElement();
388 contentTmpWriter.endElement();
390 docWriter->startElement(
"office:automatic-styles" );
392 TQValueList<KoGenStyles::NamedStyle> styles = mainStyles.styles( VDocument::STYLE_GRAPHICAUTO );
393 TQValueList<KoGenStyles::NamedStyle>::const_iterator it = styles.begin();
394 for( ; it != styles.end() ; ++it )
395 (*it).style->writeStyle( docWriter, mainStyles,
"style:style", (*it).name ,
"style:graphic-properties" );
397 docWriter->endElement();
401 docWriter->addCompleteElement( tmpFile );
402 contentTmpFile.close();
404 docWriter->endElement();
405 docWriter->endDocument();
408 if( !store->close() )
411 manifestWriter->addManifestEntry(
"content.xml",
"text/xml" );
413 if( !store->open(
"styles.xml" ) )
416 KoXmlWriter* styleWriter = createOasisXmlWriter( &storeDev,
"office:document-styles" );
418 styleWriter->startElement(
"office:styles" );
420 styles = mainStyles.styles( VDocument::STYLE_LINEAR_GRADIENT );
422 for( ; it != styles.end() ; ++it )
423 (*it).style->writeStyle( styleWriter, mainStyles,
"svg:linearGradient", (*it).name, 0,
true,
true );
425 styles = mainStyles.styles( VDocument::STYLE_RADIAL_GRADIENT );
427 for( ; it != styles.end() ; ++it )
428 (*it).style->writeStyle( styleWriter, mainStyles,
"svg:radialGradient", (*it).name, 0,
true,
true );
430 styleWriter->endElement();
432 styleWriter->startElement(
"office:automatic-styles" );
434 TQValueList<KoGenStyles::NamedStyle> styleList = mainStyles.styles( KoGenStyle::STYLE_PAGELAYOUT );
435 it = styleList.begin();
437 for( ; it != styleList.end(); ++it )
438 (*it).style->writeStyle( styleWriter, mainStyles,
"style:page-layout", (*it).name,
"style:page-layout-properties" );
440 styleWriter->endElement();
442 styles = mainStyles.styles( KoGenStyle::STYLE_MASTER );
444 styleWriter->startElement(
"office:master-styles");
446 for( ; it != styles.end(); ++it)
447 (*it).style->writeStyle( styleWriter, mainStyles,
"style:master-page", (*it).name,
"");
449 styleWriter->endElement();
451 styleWriter->endElement();
452 styleWriter->endDocument();
455 if( !store->close() )
458 manifestWriter->addManifestEntry(
"styles.xml",
"text/xml" );
461 if(!store->open(
"settings.xml"))
465 KoXmlWriter& settingsWriter = *createOasisXmlWriter(&storeDev,
"office:document-settings");
466 settingsWriter.startElement(
"office:settings");
467 settingsWriter.startElement(
"config:config-item-set");
468 settingsWriter.addAttribute(
"config:name",
"view-settings");
470 KoUnit::saveOasis(&settingsWriter, unit());
471 saveOasisSettings( settingsWriter );
473 settingsWriter.endElement();
474 settingsWriter.endElement();
475 settingsWriter.endElement();
476 settingsWriter.endDocument();
477 delete &settingsWriter;
483 manifestWriter->addManifestEntry(
"settings.xml",
"text/xml");
485 setModified(
false );
490KarbonPart::saveOasisSettings( KoXmlWriter & )
515KarbonPart::slotDocumentRestored()
517 setModified(
false );
521KarbonPart::slotCommandExecuted(
VCommand *command )
527KarbonPart::clearHistory()
529 m_commandHistory->
clear();
535 TQPtrListIterator<KoView> itr( views() );
537 for( ; itr.current() ; ++itr )
538 static_cast<KarbonView*
>( itr.current() )->canvasWidget()->repaintAll( repaint );
544 TQPtrListIterator<KoView> itr( views() );
546 for( ; itr.current() ; ++itr )
547 static_cast<KarbonView*
>( itr.current() )->canvasWidget()->repaintAll( rect );
551KarbonPart::paintContent( TQPainter& painter,
const TQRect& rect,
552 bool ,
double ,
double )
554 kdDebug(38000) <<
"**** part->paintContent()" << endl;
556 KoRect r = KoRect::fromTQRect( rect );
557 double zoomFactorX = double( r.width() ) / double( document().width() );
558 double zoomFactorY = double( r.height() ) / double( document().height() );
559 double zoomFactor = kMin( zoomFactorX, zoomFactorY );
561 painter.eraseRect( rect );
562 VPainterFactory *painterFactory =
new VPainterFactory;
564 painterFactory->setPainter( painter.device(), rect.width(), rect.height() );
565 VPainter *p = painterFactory->painter();
568 p->setZoomFactor( zoomFactor );
569 kdDebug(38000) <<
"painter.worldMatrix().dx() : " << painter.worldMatrix().dx() << endl;
570 kdDebug(38000) <<
"painter.worldMatrix().dy() : " << painter.worldMatrix().dy() << endl;
571 kdDebug(38000) <<
"rect.x() : "<< rect.x() << endl;
572 kdDebug(38000) <<
"rect.y() : "<< rect.y() << endl;
573 kdDebug(38000) <<
"rect.width() : "<< rect.width() << endl;
574 kdDebug(38000) <<
"rect.height() : "<< rect.height() << endl;
576 TQWMatrix mat = painter.worldMatrix();
578 mat.translate( 0, -r.height() * zoomFactor );
579 p->setWorldMatrix( mat );
582 TQPtrListIterator<VLayer> itr( m_doc.
layers() );
584 for( ; itr.current(); ++itr )
586 itr.current()->draw( p, &r );
590 delete painterFactory;
594KarbonPart::setShowStatusBar(
bool b )
596 m_bShowStatusBar = b;
602 TQPtrListIterator<KoView> itr( views() );
604 for( ; itr.current(); ++itr )
611KarbonPart::setUndoRedoLimit(
int undos )
618KarbonPart::initConfig()
620 TDEConfig* config = KarbonPart::instance()->config();
622 if( config->hasGroup(
"Interface" ) )
624 config->setGroup(
"Interface" );
625 setAutoSave( config->readNumEntry(
"AutoSave", defaultAutoSave() / 60 ) * 60 );
626 m_maxRecentFiles = config->readNumEntry(
"NbRecentFile", 10 );
627 setShowStatusBar( config->readBoolEntry(
"ShowStatusBar" ,
true ) );
628 setBackupFile( config->readNumEntry(
"BackupFile",
true ) );
629 m_doc.saveAsPath( config->readBoolEntry(
"SaveAsPath",
true ) );
632 if( config->hasGroup(
"Misc" ) )
634 config->setGroup(
"Misc" );
635 undos = config->readNumEntry(
"UndoRedo", -1 );
636 TQString defaultUnit =
"cm";
638 if( TDEGlobal::locale()->measureSystem() == TDELocale::Imperial )
641 setUnit( KoUnit::unit( config->readEntry(
"Units", defaultUnit ) ) );
645 setUndoRedoLimit( undos );
649KarbonPart::mergeNativeFormat(
const TQString &file )
652 bool result = loadNativeFormat( file );
654 showLoadingErrorDialog();
660KarbonPart::addShell( KoMainWindow *shell )
662 connect( shell, TQ_SIGNAL( documentSaved() ), m_commandHistory, TQ_SLOT( documentSaved() ) );
663 KoDocument::addShell( shell );
668KarbonPart::slotUnitChanged( KoUnit::Unit )
673 if( m_toolController->activeTool() )
674 m_toolController->activeTool()->refreshUnit();
678#include "karbon_part.moc"
virtual bool initDoc(InitDocFlags flags, TQWidget *parentWidget=0)
standard koDocument inherited methods
void repaintAllViews(bool repaint=true)
repaint all views attached to this koDocument
void reorganizeGUI()
update attached view(s) on the current doc settings at this time only the status bar is handled
void addCommand(VCommand *cmd, bool repaint=false)
insert a command into the undo/redo-history:
virtual TQDomDocument saveXML()
file-> save and file-> save as call this method
void insertObject(VObject *object)
insert a new vobject
virtual bool loadXML(TQIODevice *, const TQDomDocument &document)
file-> open calls this method
Manages a set of commands.
void setUndoLimit(unsigned int limit)
Sets a new undo limit.
void clear()
Clears the command history by removing all commands.
void addCommand(VCommand *command, bool execute=true)
Adds a new command to the history.
void setRedoLimit(unsigned int limit)
Sets a new redo limit.
The base class for all karbon commands.
void setHeight(double height)
Sets document height.
double width() const
Returns document width.
void setWidth(double width)
Sets document width.
VSelection * selection() const
Returns a pointer to the selection.
KoUnit::Unit unit() const
Returns document unit.
virtual bool loadOasis(const TQDomElement &element, KoOasisLoadingContext &context)
Load this object's state from OpenDocument and initialize this object accordingly.
const VLayerList & layers() const
Returns the list of layers.
void setUnit(KoUnit::Unit unit)
Sets document unit.
void append(VObject *object)
Appends a new object to the active layer.
double height() const
Returns document height.
The base class for all karbon objects.
@ selected
visible, active and can be manipulated by tools
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
void clear()
Removes the references to all objects, not the objects themselves.