1
/* This file is part of the KDE project
2
Copyright (C) 2002 Alexander Dymo <cloudtemple@mksat.net>
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.
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.
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., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
20
#include "kudesigner_doc.h"
21
#include "kudesigner_view.h"
23
#include "creportitem.h"
24
#include "ccalcfield.h"
28
#include "cspecialfield.h"
31
#include "mycommand.h"
35
#include <qpaintdevicemetrics.h>
36
#include <qfileinfo.h>
37
#include <qdockwindow.h>
42
#include "canvkutemplate.h"
43
#include "canvreportheader.h"
44
#include "canvreportfooter.h"
45
#include "canvpageheader.h"
46
#include "canvpagefooter.h"
47
#include "canvdetailheader.h"
48
#include "canvdetailfooter.h"
49
#include "canvdetail.h"
51
KudesignerDoc::KudesignerDoc(QObject* parent, const char* name)
52
: QObject(parent, name),m_plugin(0),m_propPos(DockRight)
53
/*KudesignerDoc::KudesignerDoc( QWidget *parentWidget, const char *widgetName, QObject* parent, const char* name, bool singleViewMode )
54
: KoDocument( parentWidget, widgetName, parent, name, singleViewMode ),m_plugin(0),m_propPos(DockRight)*/
57
history = new KCommandHistory(actionCollection());
59
connect( history, SIGNAL( documentRestored() ), this, SLOT( slotDocumentRestored() ) );
60
connect( history, SIGNAL( commandExecuted() ), this, SLOT( slotCommandExecuted() ) ); */
61
docCanvas = new MyCanvas(100, 100);
64
KudesignerDoc::~KudesignerDoc()
69
void KudesignerDoc::addCommand(KCommand *cmd)
71
// history->addCommand(cmd);
74
bool KudesignerDoc::initDoc()
76
// If nothing is loaded, do initialize here
81
/* KoTemplateChooseDia::ReturnType ret = KoTemplateChooseDia::choose(
82
KudesignerFactory::global(), _template, "application/x-kudesigner", "*.kut", i18n("Kugar Designer"),
83
KoTemplateChooseDia::Everything, "kudesigner_template");
84
if ( ret == KoTemplateChooseDia::Template ) {
85
QFileInfo fileInfo( _template );
86
QString fileName( fileInfo.dirPath( TRUE ) + "/" + fileInfo.baseName() + ".ktm" );
88
ok = loadNativeFormat( fileName );
90
} else if ( ret == KoTemplateChooseDia::File ) {
93
} else if ( ret == KoTemplateChooseDia::Empty ) {
94
QString fileName( locate( "kudesigner_template", "General/.source/A4.ktm", KudesignerFactory::global() ) );
96
ok = loadNativeFormat( fileName );
100
setModified( FALSE );
105
/*KoView* KudesignerDoc::createViewInstance( QWidget* parent, const char* name )
107
return new KudesignerView( this, parent, name );
110
bool KudesignerDoc::loadXML( QIODevice *, const QDomDocument & rt)
112
// TODO load the document from the QDomDocument
114
QDomNode report, rep;
115
for (QDomNode report = rt.firstChild(); !report.isNull(); report = report.nextSibling())
117
if (report.nodeName() == "KugarTemplate")
125
QDomNamedNodeMap attributes = report.attributes();
126
//getting the page width and height
130
if (attributes.namedItem("PageOrientation").nodeValue().toInt())
137
//zooming canvas according to screen resolution
141
printer = new QPrinter();
142
printer->setFullPage(true);
143
printer->setPageSize((QPrinter::PageSize)attributes.namedItem("PageSize").nodeValue().toInt());
144
printer->setOrientation((QPrinter::Orientation)attributes.namedItem("PageOrientation").nodeValue().toInt());
146
// Get the page metrics and set appropriate wigth and height
147
QPaintDeviceMetrics pdm(printer);
149
height = pdm.height();
151
//this is not needed anymore
155
docCanvas = new MyCanvas(width, height);
156
docCanvas->setAdvancePeriod(30);
158
//creating KugarTemplate object
159
CanvasKugarTemplate *templ = new CanvasKugarTemplate(0, 0, width, height, docCanvas);
161
templ->props["PageSize"]->setValue(attributes.namedItem("PageSize").nodeValue());
162
templ->props["PageOrientation"]->setValue(attributes.namedItem("PageOrientation").nodeValue());
163
templ->props["TopMargin"]->setValue(attributes.namedItem("TopMargin").nodeValue());
164
templ->props["BottomMargin"]->setValue(attributes.namedItem("BottomMargin").nodeValue());
165
templ->props["LeftMargin"]->setValue(attributes.namedItem("LeftMargin").nodeValue());
166
templ->props["RightMargin"]->setValue(attributes.namedItem("RightMargin").nodeValue());
168
// Get all the child report elements
169
QDomNodeList children = report.childNodes();
170
int childCount = children.length();
171
for (int j = 0; j < childCount; j++)
173
QDomNode child = children.item(j);
175
if (child.nodeType() == QDomNode::ElementNode)
177
if (child.nodeName() == "ReportHeader")
178
setReportHeaderAttributes(&child);
179
else if (child.nodeName() == "PageHeader")
180
setPageHeaderAttributes(&child);
181
else if (child.nodeName() == "DetailHeader")
182
setDetailHeaderAttributes(&child);
183
else if(child.nodeName() == "Detail")
185
canvas()->templ->detailsCount++;
186
setDetailAttributes(&child);
188
else if(child.nodeName() == "DetailFooter")
189
setDetailFooterAttributes(&child);
190
else if(child.nodeName() == "PageFooter")
191
setPageFooterAttributes(&child);
192
else if(child.nodeName() == "ReportFooter")
193
setReportFooterAttributes(&child);
196
docCanvas->templ->arrangeSections(FALSE);
197
QCanvasItemList l = docCanvas->allItems();
198
for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); it++)
207
QDomDocument KudesignerDoc::saveXML()
210
doc.setContent(docCanvas->templ->getXml());
216
void KudesignerDoc::paintContent( QPainter& painter, const QRect& rect, bool /*transparent*/,
217
double /*zoomX*/, double /*zoomY*/ )
219
// ####### handle transparency
221
// Need to draw only the document rectangle described in the parameter rect.
223
canvas()->drawArea(rect, &painter, TRUE);
227
void KudesignerDoc::commandExecuted()
232
void KudesignerDoc::documentRestored()
237
MyCanvas *KudesignerDoc::canvas()
242
void KudesignerDoc::setReportHeaderAttributes(QDomNode *node)
244
QDomNamedNodeMap attributes = node->attributes();
246
CanvasReportHeader *rh = new CanvasReportHeader(docCanvas->templ->props["LeftMargin"]->value().toInt(),
247
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
248
docCanvas->templ->props["LeftMargin"]->value().toInt(),
249
attributes.namedItem("Height").nodeValue().toInt(), docCanvas);
250
rh->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
251
docCanvas->templ->reportHeader = rh;
252
addReportItems(node, rh);
255
void KudesignerDoc::setReportFooterAttributes(QDomNode *node)
257
QDomNamedNodeMap attributes = node->attributes();
259
CanvasReportFooter *rf = new CanvasReportFooter(docCanvas->templ->props["LeftMargin"]->value().toInt(),
260
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
261
docCanvas->templ->props["LeftMargin"]->value().toInt(),
262
attributes.namedItem("Height").nodeValue().toInt(), docCanvas);
263
rf->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
264
docCanvas->templ->reportFooter = rf;
265
addReportItems(node, rf);
268
void KudesignerDoc::setPageHeaderAttributes(QDomNode *node)
270
QDomNamedNodeMap attributes = node->attributes();
272
CanvasPageHeader *ph = new CanvasPageHeader(docCanvas->templ->props["LeftMargin"]->value().toInt(),
273
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
274
docCanvas->templ->props["LeftMargin"]->value().toInt(),
275
attributes.namedItem("Height").nodeValue().toInt(), docCanvas);
276
ph->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
277
docCanvas->templ->pageHeader = ph;
278
addReportItems(node, ph);
281
void KudesignerDoc::setPageFooterAttributes(QDomNode *node)
283
QDomNamedNodeMap attributes = node->attributes();
285
CanvasPageFooter *pf = new CanvasPageFooter(docCanvas->templ->props["LeftMargin"]->value().toInt(),
286
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
287
docCanvas->templ->props["LeftMargin"]->value().toInt(),
288
attributes.namedItem("Height").nodeValue().toInt(), docCanvas);
289
pf->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
290
docCanvas->templ->pageFooter = pf;
291
addReportItems(node, pf);
294
void KudesignerDoc::setDetailHeaderAttributes(QDomNode *node)
296
QDomNamedNodeMap attributes = node->attributes();
298
CanvasDetailHeader *dh = new CanvasDetailHeader(docCanvas->templ->props["LeftMargin"]->value().toInt(),
299
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
300
docCanvas->templ->props["LeftMargin"]->value().toInt(),
301
attributes.namedItem("Height").nodeValue().toInt(),
302
attributes.namedItem("Level").nodeValue().toInt(), docCanvas);
303
dh->props["Level"]->setValue(attributes.namedItem("Level").nodeValue());
304
dh->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
305
docCanvas->templ->details[attributes.namedItem("Level").nodeValue().toInt()].first.first = dh;
306
addReportItems(node, dh);
309
void KudesignerDoc::setDetailAttributes(QDomNode *node)
311
QDomNamedNodeMap attributes = node->attributes();
313
CanvasDetail *d = new CanvasDetail(docCanvas->templ->props["LeftMargin"]->value().toInt(),
314
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
315
docCanvas->templ->props["LeftMargin"]->value().toInt(),
316
attributes.namedItem("Height").nodeValue().toInt(),
317
attributes.namedItem("Level").nodeValue().toInt(), docCanvas);
318
d->props["Level"]->setValue(attributes.namedItem("Level").nodeValue());
319
d->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
320
docCanvas->templ->details[attributes.namedItem("Level").nodeValue().toInt()].second = d;
321
addReportItems(node, d);
324
void KudesignerDoc::setDetailFooterAttributes(QDomNode *node)
326
QDomNamedNodeMap attributes = node->attributes();
328
CanvasDetailFooter *df = new CanvasDetailFooter(docCanvas->templ->props["LeftMargin"]->value().toInt(),
329
0, docCanvas->templ->width() - docCanvas->templ->props["RightMargin"]->value().toInt() -
330
docCanvas->templ->props["LeftMargin"]->value().toInt(),
331
attributes.namedItem("Height").nodeValue().toInt(),
332
attributes.namedItem("Level").nodeValue().toInt(), docCanvas);
333
df->props["Level"]->setValue(attributes.namedItem("Level").nodeValue());
334
df->props["Height"]->setValue(attributes.namedItem("Height").nodeValue());
335
docCanvas->templ->details[attributes.namedItem("Level").nodeValue().toInt()].first.second = df;
336
addReportItems(node, df);
340
void KudesignerDoc::addReportItems(QDomNode *node, CanvasBand *section)
342
QDomNodeList children = node->childNodes();
343
int childCount = children.length();
345
for (int j = 0; j < childCount; j++)
347
QDomNode child = children.item(j);
348
if(child.nodeType() == QDomNode::ElementNode)
350
if (child.nodeName() == "Line")
352
CanvasLine *line = new CanvasLine(0, 0, 50, 20, docCanvas);
353
section->items.append(line);
354
setReportItemAttributes(&child, line);
355
line->setSectionUndestructive(section);
358
else if (child.nodeName() == "Label")
360
CanvasLabel *label = new CanvasLabel(0, 0, 50, 20, docCanvas);
361
section->items.append(label);
362
setReportItemAttributes(&child, label);
363
label->setSectionUndestructive(section);
366
else if (child.nodeName() == "Special")
368
CanvasSpecialField *special = new CanvasSpecialField(0, 0, 50, 20, docCanvas);
369
section->items.append(special);
370
setReportItemAttributes(&child, special);
371
special->setSectionUndestructive(section);
374
else if (child.nodeName() == "Field")
376
CanvasField *field = new CanvasField(0, 0, 50, 20, docCanvas);
377
section->items.append(field);
378
setReportItemAttributes(&child, field);
379
field->setSectionUndestructive(section);
382
else if (child.nodeName() == "CalculatedField")
384
CanvasCalculatedField *calcField = new CanvasCalculatedField(0, 0, 50, 20, docCanvas);
385
section->items.append(calcField);
386
setReportItemAttributes(&child, calcField);
387
calcField->setSectionUndestructive(section);
394
void KudesignerDoc::setReportItemAttributes(QDomNode *node, CanvasReportItem *item)
396
QDomNamedNodeMap attributes = node->attributes();
398
for (unsigned int i = 0; i < attributes.count(); i++)
400
QString propertyName=attributes.item(i).nodeName();
401
QString propertyValue=attributes.item(i).nodeValue();
402
if (m_plugin) m_plugin->modifyItemPropertyOnLoad(item,item->props[propertyName],propertyName,propertyValue);
403
item->props[propertyName]->setValue(propertyValue);
407
void KudesignerDoc::loadPlugin(const QString &name)
409
// kdDebug()<<"Trying to load plugin: "<<name<<endl;
410
// KuDesignerPlugin *plug=KParts::ComponentFactory::createInstanceFromLibrary<KuDesignerPlugin>(name.utf8(),this);
412
// if (m_plugin) kdDebug()<<"plugin has been loaded"<<endl;
413
// else kdDebug()<<"plugin couldn't be loaded :("<<endl;
416
/*bool KudesignerDoc::completeSaving( KoStore* store )
418
if (m_plugin) return m_plugin->store(store);
422
bool KudesignerDoc::completeLoading( KoStore* store )
424
if (m_plugin) return m_plugin->load(store);
428
KuDesignerPlugin *KudesignerDoc::plugin()
433
Qt::Dock KudesignerDoc::propertyPosition()
438
void KudesignerDoc::setForcedPropertyEditorPosition(Dock d)
443
void KudesignerDoc::setModified( const bool val )
448
bool KudesignerDoc::modified( ) const
453
/*QActionCollection * KudesignerDoc::actionCollection( )
459
//#include "kudesigner_doc.moc"