~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to filters/kpresenter/powerpoint/libppt/objects.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* libppt - library to read PowerPoint presentation
2
 
   Copyright (C) 2005 Yolla Indria <yolla.indria@gmail.com>
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 "objects.h"
21
 
#include "ustring.h"
22
 
 
23
 
#include <string>
24
 
#include <map>
25
 
#include <vector>
26
 
#include <iostream>
27
 
#include <QColor>
28
 
 
29
 
using namespace Libppt;
30
 
 
31
 
class PropertyValue
32
 
{
33
 
public:
34
 
    enum { InvalidType,  IntType, DoubleType, StringType, BoolType, ColorType } type;
35
 
    bool b;
36
 
    int i;
37
 
    double d;
38
 
    std::string s;
39
 
    Color c;
40
 
 
41
 
    PropertyValue() {
42
 
        type = InvalidType;
43
 
        b = false;
44
 
        i = 0;
45
 
        d = 0.0;
46
 
 
47
 
 
48
 
    }
49
 
};
50
 
 
51
 
class Object::Private
52
 
{
53
 
public:
54
 
    int id;
55
 
    double top;
56
 
    double left;
57
 
    double width;
58
 
    double height;
59
 
    bool background;
60
 
    std::map<std::string, PropertyValue> properties;
61
 
};
62
 
 
63
 
Object::Object()
64
 
{
65
 
    d = new Private;
66
 
    d->id = -1;
67
 
    d->top = 0.0;
68
 
    d->left = 0.0;
69
 
    d->width = 10.0;
70
 
    d->height = 3.0;
71
 
    d->background = false;
72
 
}
73
 
 
74
 
Object::~Object()
75
 
{
76
 
    delete d;
77
 
}
78
 
 
79
 
int Object::id() const
80
 
{
81
 
    return d->id;
82
 
}
83
 
 
84
 
void Object::setId(int id)
85
 
{
86
 
    d->id = id;
87
 
}
88
 
 
89
 
double Object::top() const
90
 
{
91
 
    return d->top;
92
 
}
93
 
 
94
 
double Object::left() const
95
 
{
96
 
    return d->left;
97
 
}
98
 
 
99
 
double Object::width() const
100
 
{
101
 
    return d->width;
102
 
}
103
 
 
104
 
double Object::height() const
105
 
{
106
 
    return d->height;
107
 
}
108
 
 
109
 
void Object::setTop(double top)
110
 
{
111
 
    d->top = top;
112
 
}
113
 
 
114
 
void Object::setLeft(double left)
115
 
{
116
 
    d->left = left;
117
 
}
118
 
 
119
 
void Object::setWidth(double width)
120
 
{
121
 
    d->width = width;
122
 
}
123
 
 
124
 
void Object::setHeight(double height)
125
 
{
126
 
    d->height = height;
127
 
}
128
 
 
129
 
bool Object::isBackground() const
130
 
{
131
 
    return d->background;
132
 
}
133
 
 
134
 
void Object::setBackground(bool bg)
135
 
{
136
 
    d->background = bg;
137
 
}
138
 
 
139
 
bool Object::hasProperty(std::string name)
140
 
{
141
 
    std::map<std::string, PropertyValue>::const_iterator i;
142
 
    i = d->properties.find(name);
143
 
    if (i ==  d->properties.end())
144
 
        return false;
145
 
    else
146
 
        return true;
147
 
}
148
 
 
149
 
void Object::setProperty(std::string name, std::string value)
150
 
{
151
 
    PropertyValue pv;
152
 
    pv.type = PropertyValue::StringType;
153
 
    pv.s = value;
154
 
    d->properties[ name ] = pv;
155
 
}
156
 
 
157
 
void Object::setProperty(std::string name, int value)
158
 
{
159
 
    PropertyValue pv;
160
 
    pv.type = PropertyValue::IntType;
161
 
    pv.i = value;
162
 
    d->properties[ name ] = pv;
163
 
}
164
 
 
165
 
void Object::setProperty(std::string name, double value)
166
 
{
167
 
    PropertyValue pv;
168
 
    pv.type = PropertyValue::DoubleType;
169
 
    pv.d = value;
170
 
    d->properties[ name ] = pv;
171
 
}
172
 
 
173
 
void Object::setProperty(std::string name, bool value)
174
 
{
175
 
    PropertyValue pv;
176
 
    pv.type = PropertyValue::BoolType;
177
 
    pv.b = value;
178
 
    d->properties[ name ] = pv;
179
 
}
180
 
 
181
 
void Object::setProperty(std::string name, Color value)
182
 
{
183
 
    PropertyValue pv;
184
 
    pv.type = PropertyValue::ColorType;
185
 
    pv.c = value;
186
 
    d->properties[ name ] = pv;
187
 
}
188
 
 
189
 
int Object::getIntProperty(std::string name)
190
 
{
191
 
    PropertyValue pv;
192
 
    pv = d->properties[ name ];
193
 
    if (pv.type == PropertyValue::IntType)
194
 
        return pv.i;
195
 
    else
196
 
        return 0;
197
 
}
198
 
 
199
 
double Object::getDoubleProperty(std::string name)
200
 
{
201
 
    PropertyValue pv;
202
 
    pv = d->properties[ name ];
203
 
    if (pv.type == PropertyValue::DoubleType)
204
 
        return pv.d;
205
 
    else
206
 
        return 0;
207
 
}
208
 
 
209
 
bool Object::getBoolProperty(std::string name)
210
 
{
211
 
    PropertyValue pv;
212
 
    pv = d->properties[ name ];
213
 
    if (pv.type == PropertyValue::BoolType)
214
 
        return pv.b;
215
 
    else
216
 
        return false;
217
 
 
218
 
}
219
 
 
220
 
std::string Object::getStrProperty(std::string name)
221
 
{
222
 
    PropertyValue pv;
223
 
    pv = d->properties[ name ];
224
 
    if (pv.type == PropertyValue::StringType)
225
 
        return pv.s;
226
 
    else
227
 
        return "NoString";
228
 
}
229
 
 
230
 
Color Object::getColorProperty(std::string name)
231
 
{
232
 
    PropertyValue pv;
233
 
    pv = d->properties[ name ];
234
 
    if (pv.type == PropertyValue::ColorType)
235
 
        return pv.c;
236
 
    else
237
 
        return Color(153, 204, 255); // #99ccff
238
 
 
239
 
}
240
 
 
241
 
class TextObject::Private : public QSharedData
242
 
{
243
 
public:
244
 
    Private();
245
 
    ~Private();
246
 
    unsigned int type;
247
 
 
248
 
    /**
249
 
    * @brief Actual text for this object
250
 
    */
251
 
    QString text;
252
 
 
253
 
    /**
254
 
    * @brief Text style that applies to this object
255
 
    *
256
 
    */
257
 
    StyleTextPropAtom *atom;
258
 
 
259
 
    /**
260
 
    * @brief Struct that contains precalculated style names based on
261
 
    * TextCFException and TextPFException combinations.
262
 
    *
263
 
    * For each individual character in this object's text three styles apply:
264
 
    * Paragraph style, List style and Character style. These are parsed from
265
 
    * TextCFException and TextPFException. For each character there is a
266
 
    * corresponding pair of TextCFException and TextPFException.
267
 
    *
268
 
    * Saving of styles is done before saving text contents so we'll cache
269
 
    * the style names and pairs of TextCFException and TextPFException.
270
 
    *
271
 
    *
272
 
    */
273
 
    struct StyleName {
274
 
        /**
275
 
        * @brief Text style name (e.g. T1)
276
 
        *
277
 
        */
278
 
        QString text;
279
 
 
280
 
        /**
281
 
        * @brief Paragraph style (e.g. P1)
282
 
        *
283
 
        */
284
 
        QString paragraph;
285
 
 
286
 
        /**
287
 
        * @brief List style (e.g. L1)
288
 
        *
289
 
        */
290
 
        QString list;
291
 
 
292
 
        /**
293
 
        * @brief TextCFException the style names are parsed from
294
 
        */
295
 
        const TextCFException *cf;
296
 
 
297
 
        /**
298
 
        * @brief TextPFException the style names are parsed from
299
 
        */
300
 
        const TextPFException *pf;
301
 
    };
302
 
 
303
 
    /**
304
 
    * @brief List of pre parsed style names that apply to this text
305
 
    */
306
 
    QList<StyleName> styleNames;
307
 
};
308
 
 
309
 
 
310
 
TextObject::Private::Private()
311
 
        : type(0)
312
 
        , atom(0)
313
 
{
314
 
 
315
 
}
316
 
 
317
 
TextObject::Private::~Private()
318
 
{
319
 
    delete atom;
320
 
}
321
 
 
322
 
 
323
 
void TextObject::setStyleTextProperty(const StyleTextPropAtom *atom)
324
 
{
325
 
    //We don't know the lifespan of this atom so we'll make a copy of it
326
 
    d->atom =  new StyleTextPropAtom(*atom);
327
 
}
328
 
 
329
 
 
330
 
TextObject::TextObject(): Object()
331
 
{
332
 
    d = new Private;
333
 
}
334
 
 
335
 
TextObject::~TextObject()
336
 
{
337
 
}
338
 
 
339
 
unsigned int TextObject::type() const
340
 
{
341
 
    return d->type;
342
 
}
343
 
 
344
 
const char* TextObject::typeAsString() const
345
 
{
346
 
    switch (d->type) {
347
 
    case  Title       : return "Title";
348
 
    case  Body        : return "Body";
349
 
    case  Notes       : return "Notes";
350
 
    case  NotUsed     : return "NotUsed";
351
 
    case  Other       : return "Other";
352
 
    case  CenterBody  : return "CenterBody";
353
 
    case  CenterTitle : return "CenterTitle";
354
 
    case  HalfBody    : return "HalfBody";
355
 
    case  QuarterBody : return "QuarterBody";
356
 
    default: break;
357
 
    }
358
 
 
359
 
    return "Unknown";
360
 
}
361
 
 
362
 
void TextObject::addStylenames(const TextCFException *cf,
363
 
                               const TextPFException *pf,
364
 
                               const QString &text,
365
 
                               const QString &paragraph,
366
 
                               const QString &list)
367
 
{
368
 
    TextObject::Private::StyleName style;
369
 
    style.cf = cf;
370
 
    style.pf = pf;
371
 
    style.text = text;
372
 
    style.paragraph = paragraph;
373
 
    style.list = list;
374
 
    d->styleNames << style;
375
 
}
376
 
 
377
 
QString TextObject::textStyleName(const TextCFException *cf,
378
 
                                  const TextPFException *pf) const
379
 
{
380
 
    int pos = findStyle(cf, pf);
381
 
    if (pos >= 0) {
382
 
        return d->styleNames[pos].text;
383
 
    }
384
 
 
385
 
    return QString();
386
 
}
387
 
 
388
 
QString TextObject::paragraphStyleName(const TextCFException *cf,
389
 
                                       const TextPFException *pf) const
390
 
{
391
 
    int pos = findStyle(cf, pf);
392
 
    if (pos >= 0) {
393
 
        return d->styleNames[pos].paragraph;
394
 
    }
395
 
 
396
 
    return QString();
397
 
}
398
 
 
399
 
QString TextObject::listStyleName(const TextCFException *cf,
400
 
                                  const TextPFException *pf) const
401
 
{
402
 
    int pos = findStyle(cf, pf);
403
 
    if (pos >= 0) {
404
 
        return d->styleNames[pos].list;
405
 
    }
406
 
 
407
 
    return QString();
408
 
}
409
 
 
410
 
int TextObject::findStyle(const TextCFException *cf,
411
 
                          const TextPFException *pf) const
412
 
{
413
 
    for (int i = 0;i < d->styleNames.size();i++) {
414
 
        if (d->styleNames[i].cf == cf && d->styleNames[i].pf == pf) {
415
 
            return i;
416
 
        }
417
 
    }
418
 
 
419
 
    return -1;
420
 
}
421
 
 
422
 
StyleTextPropAtom *TextObject::styleTextProperty()
423
 
{
424
 
    return d->atom;
425
 
}
426
 
 
427
 
 
428
 
void TextObject::setType(unsigned int type)
429
 
{
430
 
    d->type = type;
431
 
}
432
 
 
433
 
QString TextObject::text() const
434
 
{
435
 
    return d->text;
436
 
}
437
 
 
438
 
 
439
 
void TextObject::setText(const QString& text)
440
 
{
441
 
    d->text = text;
442
 
}
443
 
 
444
 
 
445
 
void TextObject::convertFrom(Object* object)
446
 
{
447
 
    setId(object->id());
448
 
    setLeft(object->left());
449
 
    setTop(object->top());
450
 
    setWidth(object->width());
451
 
    setHeight(object->height());
452
 
 
453
 
    if (object->isText()) {
454
 
        TextObject* textObj = static_cast<TextObject*>(object);
455
 
        setType(textObj->type());
456
 
        // jgn lupa diganti :  setText( textObj->text() );
457
 
    }
458
 
}
459
 
 
460
 
class GroupObject::Private
461
 
{
462
 
public:
463
 
    std::vector<Object*> objects;
464
 
 
465
 
    // in group nesting, coordinate transformations may occur
466
 
    // these values define the transformation
467
 
    double xoffset;
468
 
    double yoffset;
469
 
    double xscale;
470
 
    double yscale;
471
 
 
472
 
    double vx;
473
 
    double vy;
474
 
    double vwidth;
475
 
    double vheight;
476
 
 
477
 
    Private() : xoffset(0), yoffset(0), xscale(25.4 / 576), yscale(25.4 / 576),
478
 
            vx(0), vy(0), vwidth(0), vheight(0) {
479
 
    }
480
 
};
481
 
 
482
 
GroupObject::GroupObject()
483
 
{
484
 
    d = new Private;
485
 
}
486
 
 
487
 
GroupObject::~GroupObject()
488
 
{
489
 
    for (unsigned i = 0; i < d->objects.size(); i++)
490
 
        delete d->objects[i];
491
 
    delete d;
492
 
}
493
 
 
494
 
unsigned GroupObject::objectCount() const
495
 
{
496
 
    return d->objects.size();
497
 
}
498
 
 
499
 
Object* GroupObject::object(unsigned i)
500
 
{
501
 
    return d->objects[i];
502
 
}
503
 
 
504
 
void GroupObject::addObject(Object* object)
505
 
{
506
 
    d->objects.push_back(object);
507
 
}
508
 
 
509
 
void GroupObject::takeObject(Object* object)
510
 
{
511
 
    std::vector<Object*> result;
512
 
    for (unsigned i = 0; i < d->objects.size(); i++) {
513
 
        Object* obj = d->objects[i];
514
 
        if (obj != object)
515
 
            result.push_back(obj);
516
 
    }
517
 
 
518
 
    d->objects.clear();
519
 
    for (unsigned j = 0; j < result.size(); j++)
520
 
        d->objects.push_back(result[j]);
521
 
}
522
 
 
523
 
void
524
 
GroupObject::setViewportDimensions(double x, double y,
525
 
                                   double width, double height)
526
 
{
527
 
    d->vx = x;
528
 
    d->vy = y;
529
 
    d->vwidth = width;
530
 
    d->vheight = height;
531
 
}
532
 
 
533
 
void
534
 
GroupObject::setDimensions(double x, double y, double width, double height)
535
 
{
536
 
    if (width && height && d->vwidth && d->vheight) {
537
 
        // adapt the offset and scaling
538
 
        d->xoffset += d->xscale * x;
539
 
        d->xscale *= width / d->vwidth;
540
 
        d->xoffset -= d->xscale * d->vx;
541
 
        d->yoffset += d->yscale * y;
542
 
        d->yscale *= height / d->vheight;
543
 
        d->yoffset -= d->yscale * d->vy;
544
 
    }
545
 
}
546
 
 
547
 
double
548
 
GroupObject::getXOffset() const
549
 
{
550
 
    return d->xoffset;
551
 
}
552
 
 
553
 
double
554
 
GroupObject::getYOffset() const
555
 
{
556
 
    return d->yoffset;
557
 
}
558
 
 
559
 
double
560
 
GroupObject::getXScale() const
561
 
{
562
 
    return d->xscale;
563
 
}
564
 
 
565
 
double
566
 
GroupObject::getYScale() const
567
 
{
568
 
    return d->yscale;
569
 
}
570
 
 
571
 
class DrawObject::Private
572
 
{
573
 
public:
574
 
    unsigned shape;
575
 
    bool isVerFlip;
576
 
    bool isHorFlip;
577
 
 
578
 
    /**
579
 
    * @brief Name of the style corresponding this object
580
 
    *
581
 
    * This is usually generated by KoGenStyles
582
 
    */
583
 
    QString styleName;
584
 
};
585
 
 
586
 
DrawObject::DrawObject()
587
 
{
588
 
    d = new Private;
589
 
    d->shape = None;
590
 
}
591
 
 
592
 
DrawObject::~DrawObject()
593
 
{
594
 
    delete d;
595
 
}
596
 
 
597
 
unsigned DrawObject::shape() const
598
 
{
599
 
    return d->shape;
600
 
}
601
 
 
602
 
void DrawObject::setShape(unsigned s)
603
 
{
604
 
    d->shape = s;
605
 
}
606
 
 
607
 
bool DrawObject::isVerFlip() const
608
 
{
609
 
    return d->isVerFlip;
610
 
}
611
 
 
612
 
QString DrawObject::styleName() const
613
 
{
614
 
    return d->styleName;
615
 
}
616
 
 
617
 
void DrawObject::setVerFlip(bool isVerFlip)
618
 
{
619
 
    d->isVerFlip = isVerFlip;
620
 
}
621
 
 
622
 
bool DrawObject::isHorFlip() const
623
 
{
624
 
    return d->isHorFlip;
625
 
}
626
 
 
627
 
void DrawObject::setHorFlip(bool isHorFlip)
628
 
{
629
 
    d->isHorFlip = isHorFlip;
630
 
}
631
 
 
632
 
void DrawObject::setStyleName(const QString &name)
633
 
{
634
 
    d->styleName = name;
635
 
}
636
 
 
637
 
class TextFont::Private : public QSharedData
638
 
{
639
 
public:
640
 
    Private();
641
 
    ~Private();
642
 
 
643
 
    /**
644
 
    * @brief Font's name
645
 
    */
646
 
    QString fontName;
647
 
 
648
 
    /**
649
 
    * @brief Font's charset
650
 
    *
651
 
    * See http://msdn.microsoft.com/en-us/library/dd145037(VS.85).aspx
652
 
    * for more information
653
 
    */
654
 
    int charset;
655
 
 
656
 
    /**
657
 
    * @brief Font's clip precision
658
 
    *
659
 
    * See http://msdn.microsoft.com/en-us/library/dd145037(VS.85).aspx
660
 
    * for more information
661
 
    */
662
 
    int clipPrecision;
663
 
 
664
 
    /**
665
 
    * @brief Font's quality
666
 
    *
667
 
    * See http://msdn.microsoft.com/en-us/library/dd145037(VS.85).aspx
668
 
    * for more information
669
 
    */
670
 
    int quality;
671
 
 
672
 
    /**
673
 
    * @brief Font's pitch and family
674
 
    *
675
 
    * See http://msdn.microsoft.com/en-us/library/dd145037(VS.85).aspx
676
 
    * for more information
677
 
    */
678
 
    int pitchAndFamily;
679
 
};
680
 
 
681
 
TextFont::Private::Private()
682
 
        : charset(0)
683
 
        , clipPrecision(0)
684
 
        , quality(0)
685
 
        , pitchAndFamily(0)
686
 
{
687
 
 
688
 
}
689
 
 
690
 
TextFont::Private::~Private()
691
 
{
692
 
}
693
 
 
694
 
 
695
 
TextFont::TextFont()
696
 
{
697
 
    d = new Private();
698
 
}
699
 
 
700
 
TextFont::TextFont(const QString &fontName,
701
 
                   int charset,
702
 
                   int clipPrecision,
703
 
                   int quality,
704
 
                   int pitchAndFamily)
705
 
{
706
 
    d = new Private();
707
 
    d->fontName = fontName;
708
 
    d->charset = charset;
709
 
    d->clipPrecision = clipPrecision;
710
 
    d->quality = quality;
711
 
    d->pitchAndFamily = pitchAndFamily;
712
 
}
713
 
 
714
 
TextFont::TextFont(const TextFont &source)
715
 
        : d(source.d)
716
 
{
717
 
}
718
 
 
719
 
 
720
 
TextFont::~TextFont()
721
 
{
722
 
}
723
 
 
724
 
QString TextFont::name() const
725
 
{
726
 
    return d->fontName;
727
 
}
728
 
 
729
 
int TextFont::charset() const
730
 
{
731
 
    return d->charset;
732
 
}
733
 
 
734
 
int TextFont::clipPrecision() const
735
 
{
736
 
    return d->clipPrecision;
737
 
}
738
 
 
739
 
int TextFont::quality() const
740
 
{
741
 
    return d->quality;
742
 
}
743
 
 
744
 
int TextFont::pitchAndFamily() const
745
 
{
746
 
    return d->pitchAndFamily;
747
 
}
748
 
 
749
 
 
750
 
 
751
 
class TextFontCollection::Private
752
 
{
753
 
public:
754
 
    QList<TextFont> fonts;
755
 
};
756
 
 
757
 
TextFontCollection::TextFontCollection()
758
 
{
759
 
    d = new Private;
760
 
}
761
 
 
762
 
TextFontCollection::~TextFontCollection()
763
 
{
764
 
    delete d;
765
 
}
766
 
 
767
 
unsigned TextFontCollection::listSize() const
768
 
{
769
 
    return d->fonts.size();
770
 
}
771
 
 
772
 
void TextFontCollection::addFont(const TextFont &font)
773
 
{
774
 
    d->fonts << font;
775
 
}
776
 
 
777
 
TextFont* TextFontCollection::getFont(unsigned int index)
778
 
{
779
 
    if (index < listSize()) {
780
 
        return &d->fonts[index];
781
 
    }
782
 
 
783
 
    return 0;
784
 
}
785