~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to kugar/kudesigner_qt2/kudesigner_doc.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2002 Alexander Dymo <cloudtemple@mksat.net>
 
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., 59 Temple Place - Suite 330,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
#include "kudesigner_doc.h"
 
21
#include "kudesigner_view.h"
 
22
#include "canvbox.h"
 
23
#include "creportitem.h"
 
24
#include "ccalcfield.h"
 
25
#include "cfield.h"
 
26
#include "clabel.h"
 
27
#include "cline.h"
 
28
#include "cspecialfield.h"
 
29
#include "property.h"
 
30
 
 
31
#include "mycommand.h"
 
32
 
 
33
#include <qpainter.h>
 
34
#include <qprinter.h>
 
35
#include <qpaintdevicemetrics.h>
 
36
#include <qfileinfo.h>
 
37
#include <qdockwindow.h>
 
38
#include <qdom.h>
 
39
 
 
40
#include "mycanvas.h"
 
41
 
 
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"
 
50
 
 
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)*/
 
55
{
 
56
/*
 
57
    history = new KCommandHistory(actionCollection());
 
58
 
 
59
    connect( history, SIGNAL( documentRestored() ), this, SLOT( slotDocumentRestored() ) );
 
60
    connect( history, SIGNAL( commandExecuted() ), this, SLOT( slotCommandExecuted() ) ); */
 
61
    docCanvas = new MyCanvas(100, 100);
 
62
}
 
63
 
 
64
KudesignerDoc::~KudesignerDoc()
 
65
{
 
66
    delete history;
 
67
}
 
68
 
 
69
void KudesignerDoc::addCommand(KCommand *cmd)
 
70
{
 
71
//    history->addCommand(cmd);
 
72
}
 
73
 
 
74
bool KudesignerDoc::initDoc()
 
75
{
 
76
    // If nothing is loaded, do initialize here
 
77
    bool ok = FALSE;
 
78
 
 
79
    QString _template;
 
80
 
 
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" );
 
87
        resetURL();
 
88
        ok = loadNativeFormat( fileName );
 
89
        setEmpty();
 
90
    } else if ( ret == KoTemplateChooseDia::File ) {
 
91
        KURL url( _template);
 
92
        ok = openURL( url );
 
93
    } else if ( ret == KoTemplateChooseDia::Empty ) {
 
94
        QString fileName( locate( "kudesigner_template", "General/.source/A4.ktm", KudesignerFactory::global() ) );
 
95
        resetURL();
 
96
        ok = loadNativeFormat( fileName );
 
97
        qDebug("%d", ok);
 
98
        setEmpty();
 
99
    }
 
100
    setModified( FALSE );
 
101
*/
 
102
    return ok;
 
103
}
 
104
 
 
105
/*KoView* KudesignerDoc::createViewInstance( QWidget* parent, const char* name )
 
106
{
 
107
    return new KudesignerView( this, parent, name );
 
108
}
 
109
*/
 
110
bool KudesignerDoc::loadXML( QIODevice *, const QDomDocument & rt)
 
111
{
 
112
    // TODO load the document from the QDomDocument
 
113
 
 
114
    QDomNode report, rep;
 
115
    for (QDomNode report = rt.firstChild(); !report.isNull(); report = report.nextSibling())
 
116
    {
 
117
        if (report.nodeName() == "KugarTemplate")
 
118
        {
 
119
            rep = report;
 
120
            break;
 
121
        }
 
122
    }
 
123
    report = rep;
 
124
 
 
125
    QDomNamedNodeMap attributes = report.attributes();
 
126
    //getting the page width and height
 
127
    int height = 297;
 
128
    int width = 210;
 
129
 
 
130
    if (attributes.namedItem("PageOrientation").nodeValue().toInt())
 
131
    {
 
132
        int temp = height;
 
133
        height = width;
 
134
        width = temp;
 
135
    }
 
136
 
 
137
    //zooming canvas according to screen resolution
 
138
    QPrinter* printer;
 
139
 
 
140
    // Set the page size
 
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());
 
145
 
 
146
    // Get the page metrics and set appropriate wigth and height
 
147
    QPaintDeviceMetrics pdm(printer);
 
148
    width = pdm.width();
 
149
    height = pdm.height();
 
150
 
 
151
    //this is not needed anymore
 
152
    delete printer;
 
153
 
 
154
    //creating canvas
 
155
    docCanvas = new MyCanvas(width, height);
 
156
    docCanvas->setAdvancePeriod(30);
 
157
 
 
158
    //creating KugarTemplate object
 
159
    CanvasKugarTemplate *templ = new CanvasKugarTemplate(0, 0, width, height, docCanvas);
 
160
    templ->show();
 
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());
 
167
 
 
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++)
 
172
    {
 
173
        QDomNode child = children.item(j);
 
174
 
 
175
        if (child.nodeType() == QDomNode::ElementNode)
 
176
        {
 
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")
 
184
            {
 
185
                canvas()->templ->detailsCount++;
 
186
                setDetailAttributes(&child);
 
187
            }
 
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);
 
194
        }
 
195
    }
 
196
    docCanvas->templ->arrangeSections(FALSE);
 
197
    QCanvasItemList l = docCanvas->allItems();
 
198
    for (QCanvasItemList::Iterator it = l.begin(); it != l.end(); it++)
 
199
    {
 
200
        (*it)->show();
 
201
    }
 
202
    docCanvas->update();
 
203
 
 
204
    return true;
 
205
}
 
206
 
 
207
QDomDocument KudesignerDoc::saveXML()
 
208
{
 
209
        QDomDocument doc;
 
210
    doc.setContent(docCanvas->templ->getXml());
 
211
 
 
212
    return doc;
 
213
}
 
214
 
 
215
 
 
216
void KudesignerDoc::paintContent( QPainter& painter, const QRect& rect, bool /*transparent*/,
 
217
                                double /*zoomX*/, double /*zoomY*/ )
 
218
{
 
219
    // ####### handle transparency
 
220
 
 
221
    // Need to draw only the document rectangle described in the parameter rect.
 
222
 
 
223
    canvas()->drawArea(rect, &painter, TRUE);
 
224
 
 
225
}
 
226
 
 
227
void KudesignerDoc::commandExecuted()
 
228
{
 
229
    setModified(true);
 
230
}
 
231
 
 
232
void KudesignerDoc::documentRestored()
 
233
{
 
234
    setModified(false);
 
235
}
 
236
 
 
237
MyCanvas *KudesignerDoc::canvas()
 
238
{
 
239
    return docCanvas;
 
240
}
 
241
 
 
242
void KudesignerDoc::setReportHeaderAttributes(QDomNode *node)
 
243
{
 
244
    QDomNamedNodeMap attributes = node->attributes();
 
245
 
 
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);
 
253
}
 
254
 
 
255
void KudesignerDoc::setReportFooterAttributes(QDomNode *node)
 
256
{
 
257
    QDomNamedNodeMap attributes = node->attributes();
 
258
 
 
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);
 
266
}
 
267
 
 
268
void KudesignerDoc::setPageHeaderAttributes(QDomNode *node)
 
269
{
 
270
    QDomNamedNodeMap attributes = node->attributes();
 
271
 
 
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);
 
279
}
 
280
 
 
281
void KudesignerDoc::setPageFooterAttributes(QDomNode *node)
 
282
{
 
283
    QDomNamedNodeMap attributes = node->attributes();
 
284
 
 
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);
 
292
}
 
293
 
 
294
void KudesignerDoc::setDetailHeaderAttributes(QDomNode *node)
 
295
{
 
296
    QDomNamedNodeMap attributes = node->attributes();
 
297
 
 
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);
 
307
}
 
308
 
 
309
void KudesignerDoc::setDetailAttributes(QDomNode *node)
 
310
{
 
311
    QDomNamedNodeMap attributes = node->attributes();
 
312
 
 
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);
 
322
}
 
323
 
 
324
void KudesignerDoc::setDetailFooterAttributes(QDomNode *node)
 
325
{
 
326
    QDomNamedNodeMap attributes = node->attributes();
 
327
 
 
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);
 
337
}
 
338
 
 
339
 
 
340
void KudesignerDoc::addReportItems(QDomNode *node, CanvasBand *section)
 
341
{
 
342
    QDomNodeList children = node->childNodes();
 
343
    int childCount = children.length();
 
344
 
 
345
    for (int j = 0; j < childCount; j++)
 
346
    {
 
347
        QDomNode child = children.item(j);
 
348
        if(child.nodeType() == QDomNode::ElementNode)
 
349
        {
 
350
            if (child.nodeName() == "Line")
 
351
            {
 
352
                CanvasLine *line = new CanvasLine(0, 0, 50, 20, docCanvas);
 
353
                section->items.append(line);
 
354
                setReportItemAttributes(&child, line);
 
355
                line->setSectionUndestructive(section);
 
356
                //line->show();
 
357
            }
 
358
            else if (child.nodeName() == "Label")
 
359
            {
 
360
                CanvasLabel *label = new CanvasLabel(0, 0, 50, 20, docCanvas);
 
361
                section->items.append(label);
 
362
                setReportItemAttributes(&child, label);
 
363
                label->setSectionUndestructive(section);
 
364
                //label->show();
 
365
            }
 
366
            else if (child.nodeName() == "Special")
 
367
            {
 
368
                CanvasSpecialField *special = new CanvasSpecialField(0, 0, 50, 20, docCanvas);
 
369
                section->items.append(special);
 
370
                setReportItemAttributes(&child, special);
 
371
                special->setSectionUndestructive(section);
 
372
                //special->show();
 
373
            }
 
374
            else if (child.nodeName() == "Field")
 
375
            {
 
376
                CanvasField *field = new CanvasField(0, 0, 50, 20, docCanvas);
 
377
                section->items.append(field);
 
378
                setReportItemAttributes(&child, field);
 
379
                field->setSectionUndestructive(section);
 
380
                //field->show();
 
381
            }
 
382
            else if (child.nodeName() == "CalculatedField")
 
383
            {
 
384
                CanvasCalculatedField *calcField = new CanvasCalculatedField(0, 0, 50, 20, docCanvas);
 
385
                section->items.append(calcField);
 
386
                setReportItemAttributes(&child, calcField);
 
387
                calcField->setSectionUndestructive(section);
 
388
                //calcField->show();
 
389
            }
 
390
        }
 
391
    }
 
392
}
 
393
 
 
394
void KudesignerDoc::setReportItemAttributes(QDomNode *node, CanvasReportItem *item)
 
395
{
 
396
    QDomNamedNodeMap attributes = node->attributes();
 
397
 
 
398
    for (unsigned int i = 0; i < attributes.count(); i++)
 
399
    {
 
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);
 
404
    }
 
405
}
 
406
 
 
407
void KudesignerDoc::loadPlugin(const QString &name)
 
408
{
 
409
//    kdDebug()<<"Trying to load plugin: "<<name<<endl;
 
410
//    KuDesignerPlugin *plug=KParts::ComponentFactory::createInstanceFromLibrary<KuDesignerPlugin>(name.utf8(),this);
 
411
//    m_plugin=plug;
 
412
//    if (m_plugin) kdDebug()<<"plugin has been loaded"<<endl;
 
413
//    else kdDebug()<<"plugin couldn't be loaded :("<<endl;
 
414
}
 
415
 
 
416
/*bool KudesignerDoc::completeSaving( KoStore* store )
 
417
{
 
418
        if (m_plugin) return m_plugin->store(store);
 
419
        return true;
 
420
}
 
421
 
 
422
bool KudesignerDoc::completeLoading( KoStore* store )
 
423
{
 
424
        if (m_plugin) return m_plugin->load(store);
 
425
        return true;
 
426
}
 
427
*/
 
428
KuDesignerPlugin *KudesignerDoc::plugin()
 
429
{
 
430
        return m_plugin;
 
431
}
 
432
 
 
433
Qt::Dock KudesignerDoc::propertyPosition()
 
434
{
 
435
        return m_propPos;
 
436
}
 
437
 
 
438
void KudesignerDoc::setForcedPropertyEditorPosition(Dock d)
 
439
{
 
440
        m_propPos=d;
 
441
}
 
442
 
 
443
void KudesignerDoc::setModified( const bool val )
 
444
{
 
445
    m_modified = val;
 
446
}
 
447
 
 
448
bool KudesignerDoc::modified( ) const
 
449
{
 
450
    return m_modified;
 
451
}
 
452
 
 
453
/*QActionCollection * KudesignerDoc::actionCollection( )
 
454
{
 
455
    return ac;
 
456
}*/
 
457
 
 
458
 
 
459
//#include "kudesigner_doc.moc"