summaryrefslogtreecommitdiffstats
path: root/kspread/kspread_object.h
blob: 2aaab40083178f0fcfafe45482fb6d07005043b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* This file is part of the KDE project
   Copyright (C) 2006 Fredrik Edemar <f_edemar@linux.se>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef KSPREAD_OBJECT_H
#define KSPREAD_OBJECT_H

#include "kspread_sheet.h"

#include <tdelocale.h>
#include <KoBrush.h>
#include <KoChild.h>
#include <KoDom.h>
#include <KoOasisLoadingContext.h>
#include <KoPen.h>
#include <KoPictureCollection.h>
#include <KoRect.h>
#include <KoSize.h>
#include <KoStore.h>
#include <KoStoreDevice.h>
#include <KoXmlNS.h>

namespace KoChart
{
class Part;
}

namespace KSpread
{

class Doc;
class EmbeddedKOfficeObject;
class EmbeddedObject;
class View;
class ChartBinding;

enum ObjType {
  OBJECT_GENERAL,
  OBJECT_KOFFICE_PART,
  OBJECT_CHART,
  OBJECT_PICTURE
};

enum SelectionMode {
    SM_NONE = 0,
    SM_MOVERESIZE = 1,
    SM_ROTATE = 2,
    SM_PROTECT = 3
};

enum PictureMirrorType {
  PM_NORMAL = 0,
  PM_HORIZONTAL = 1,
  PM_VERTICAL = 2,
  PM_HORIZONTALANDVERTICAL = 3
};

enum ImageEffect {
  IE_NONE = -1,
  IE_CHANNEL_INTENSITY = 0,
  IE_FADE = 1,
  IE_FLATTEN = 2,
  IE_INTENSITY = 3,
  IE_DESATURATE = 4,
  IE_CONTRAST = 5,
  IE_NORMALIZE = 6,
  IE_EQUALIZE = 7,
  IE_THRESHOLD = 8,
  IE_SOLARIZE = 9,
  IE_EMBOSS = 10,
  IE_DESPECKLE = 11,
  IE_CHARCOAL = 12,
  IE_NOISE = 13,
  IE_BLUR = 14,
  IE_EDGE = 15,
  IE_IMPLODE = 16,
  IE_OIL_PAINT = 17,
  IE_SHARPEN = 18,
  IE_SPREAD = 19,
  IE_SHADE = 20,
  IE_SWIRL = 21,
  IE_WAVE = 22
};

enum AlignType {
  AT_PAGE,
  AT_CELL
};

/**
 * Contains basic information for every object.
 */
class EmbeddedObject
{
  public:
    EmbeddedObject( Sheet *_sheet, const KoRect& _geometry );
    virtual ~EmbeddedObject();
    virtual ObjType getType() const { return OBJECT_GENERAL; }
    virtual TQString getTypeString() const
        { return TQString(); }

    KoRect geometry();
    void setGeometry( const KoRect &rect );
    virtual void moveBy( const KoPoint &_point );
    virtual void moveBy( double _dx, double _dy );
    virtual void resizeBy( const KoSize & _size );
    virtual void resizeBy( double _dx, double _dy );

    Sheet* sheet() const { return m_sheet; }

    void setSelected(bool s) { m_selected = s; }
    bool isSelected() const { return m_selected; }

    void setObjectName( const TQString &_objectName )
        { m_objectName = _objectName; }
    TQString getObjectName() const
        { return m_objectName; }

    virtual void setProtect( bool b ) { m_protect = b; }
    bool isProtect() const { return m_protect; }

    virtual void setKeepRatio( bool b ) { m_keepRatio = b; }
    bool isKeepRatio() const { return m_keepRatio; }

    virtual void rotate( float _angle ) { angle = _angle; }
    virtual float getAngle() const { return angle; }

    struct KSpreadOasisSaveContext
    {
      KSpreadOasisSaveContext( KoXmlWriter &_xmlWriter, KoGenStyles &_context,
                               int &_indexObj, int &_partIndexObj )
        : xmlWriter( _xmlWriter )
          , context( _context )
          , indexObj( _indexObj )
          , partIndexObj( _partIndexObj )
          {};

          KoXmlWriter &xmlWriter;
          KoGenStyles &context;
          int &indexObj;
          int &partIndexObj;
    };

    virtual bool load( const TQDomElement& element );
    virtual void loadOasis(const TQDomElement &element, KoOasisLoadingContext & context );
    virtual TQDomElement save( TQDomDocument& doc );
    virtual bool saveOasisObject( KSpreadOasisSaveContext &sc ) const;
    virtual void draw( TQPainter *_painter );
    
    /**
     * Renders the embedded object to a pixmap and returns the result.  
     * This is a convenience function which calculates the necessary x and y zoom factors to render
     * the pixmap at the given size and calls toPixmap(double,double)
     *
     * @param size Specifies the desired size of the returned pixmap.
     */
    TQPixmap toPixmap( TQSize size );
    
    /**
     * Renders the embedded object to a pixmap at 100% scale.  Equivalent to calling toPixmap( 1.0, 1.0 )
     */
    TQPixmap toPixmap();
    
    /**
     * Renders the embedded object to a pixmap at the specified x and y scale and returns the result.  
     */
    virtual TQPixmap toPixmap( double xZoom , double yZoom );
    
    
    void paintSelection( TQPainter *_painter, SelectionMode selectionMode );
    virtual TQCursor getCursor( const TQPoint &_point, ModifyType &_modType, TQRect &geometry ) const;

    virtual void removeFromObjList()
        { inObjList = false; }
    virtual void addToObjList()
        { inObjList = true; }
    virtual void incCmdRef()
        { cmds++; }
    virtual void decCmdRef()
        { cmds--; doDelete(); }

    //TEMP:
    virtual TQBrush getBrush() const { return m_brush.getBrush(); }


  protected:
    /**
     * Get the element name for saving the object
     */
    virtual const char * getOasisElementName() const = 0;
    virtual void saveOasisPosObject( KoXmlWriter &xmlWriter, int indexObj ) const;
    virtual bool saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const;

    virtual void doDelete();

    /**
     * Calculates the X and Y zoom factors required to render the embedded object at the given size
     *
     * @param desiredSize The desired size for the embedded object to be drawn at
     * @param xZoom This will be set to the required X zoom factor
     * @param yZoom This will be set to the required Y zoom factor
     */
    void calculateRequiredZoom( TQSize desiredSize , double& xZoom, double& yZoom );
    
    KoRect m_geometry;
    Sheet *m_sheet;
    Canvas *m_canvas;
    TQString m_objectName;
    bool m_selected:1;
    bool m_protect:1;
    bool m_keepRatio:1;
    bool inObjList:1;
    int cmds;
    float angle;

    KoPen pen;
    KoBrush m_brush;
};

/**
 * Holds an embedded koffice object.
 */
class EmbeddedKOfficeObject : public EmbeddedObject
{
  public:
    EmbeddedKOfficeObject( Doc *parent, Sheet *_sheet, KoDocument* doc, const KoRect& geometry );
    EmbeddedKOfficeObject( Doc *parent, Sheet *_sheet );
    virtual ~EmbeddedKOfficeObject();
    virtual ObjType getType() const { return OBJECT_KOFFICE_PART; }
    virtual TQString getTypeString() const
        { return i18n("Embedded Object"); }

    Doc* parent();
    KoDocumentChild *embeddedObject();

    bool load( const TQDomElement& element );
    virtual void loadOasis(const TQDomElement &element, KoOasisLoadingContext & context );
    TQDomElement save( TQDomDocument& doc );
    virtual void draw( TQPainter *_painter );

    /**
     * See EmbeddedObject::toPixmap(double,double)
     */
    virtual TQPixmap toPixmap(double xZoom , double yZoom);
    
    void activate( View *_view, Canvas *_canvas );
    void deactivate();

  protected:
    void updateChildGeometry();
    virtual const char * getOasisElementName() const;
    virtual bool saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const;

    KoDocumentChild *m_embeddedObject;
    Doc *m_parent;
};


/**
 * Holds an embedded chart object.
 */
class EmbeddedChart : public EmbeddedKOfficeObject
{
  public:
    EmbeddedChart( Doc *_spread, Sheet *_sheet, KoDocument* doc, const KoRect& _rect );
    EmbeddedChart( Doc *_spread, Sheet *_sheet );
    virtual ~EmbeddedChart();
    virtual ObjType getType() const { return OBJECT_CHART; }
    virtual TQString getTypeString() const
        { return i18n("Chart"); }

    void setDataArea( const TQRect& _data );
    void update();

    bool load( const TQDomElement& element );
    virtual void loadOasis(const TQDomElement &element, KoOasisLoadingContext & context );
    TQDomElement save( TQDomDocument& doc );
    virtual void draw( TQPainter *_painter );

/**
     * @reimp
 */
    bool loadDocument( KoStore* _store );

    KoChart::Part* chart();

  protected:
    virtual const char * getOasisElementName() const;
    virtual bool saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const;

  private:
    ChartBinding *m_pBinding;
};


/**
 * Holds a picture object.
 */
class EmbeddedPictureObject : public EmbeddedObject
{
  public:
    EmbeddedPictureObject(Sheet *_sheet, const KoRect& _geometry, KoPictureCollection *_imageCollection );
    EmbeddedPictureObject(Sheet *_sheet, const KoRect& _geometry, KoPictureCollection *_imageCollection, const KoPictureKey & key );
    EmbeddedPictureObject( Sheet *_sheet, KoPictureCollection *_imageCollection );
    virtual ~EmbeddedPictureObject();
    EmbeddedPictureObject &operator=( const EmbeddedPictureObject & );

    virtual ObjType getType() const { return OBJECT_PICTURE; }
    virtual TQString getTypeString() const
        { return i18n("Picture"); }
    bool load( const TQDomElement& element );
    virtual void loadOasis(const TQDomElement &element, KoOasisLoadingContext & context );
    TQDomElement save( TQDomDocument& doc );
    virtual void draw( TQPainter *_painter );
    
    /**
     * See EmbeddedObject::toPixmap(double,double)
     */
    virtual TQPixmap toPixmap(double xZoom , double yZoom);

        /**
     * Only used as a default value in the filedialog, in changePicture
     * \warning Do not use for anything else
         */
    TQString getFileName() const
    { return image.getKey().filename(); }

    KoPictureKey getKey() const
    { return image.getKey(); }

    TQSize originalSize() const
    { return image.getOriginalSize(); }

    void setPicture( const KoPictureKey & key );

    void reload( void );

    //virtual TQDomDocumentFragment save( TQDomDocument& doc, double offset );

    //virtual double load(const TQDomElement &element);
    //virtual void loadOasis(const TQDomElement &element, KoOasisLoadingContext & context, KPRLoadingInfo *info);

//     virtual void draw( TQPainter *_painter, KoZoomHandler*_zoomHandler,
//                        int /* page */, SelectionMode selectionMode, bool drawContour = FALSE );

    TQPixmap getOriginalPixmap();
    PictureMirrorType getPictureMirrorType() const { return mirrorType; }
    int getPictureDepth() const { return depth; }
    bool getPictureSwapRGB() const { return swapRGB; }
    bool getPictureGrayscal() const { return grayscal; }
    int getPictureBright() const { return bright; }

    ImageEffect getImageEffect() const {return m_effect;}
    TQVariant getIEParam1() const {return m_ie_par1;}
    TQVariant getIEParam2() const {return m_ie_par2;}
    TQVariant getIEParam3() const {return m_ie_par3;}
    void setImageEffect(ImageEffect eff) { m_effect = eff; }
    void setIEParams(TQVariant p1, TQVariant p2, TQVariant p3) {
      m_ie_par1=p1;
      m_ie_par2=p2;
      m_ie_par3=p3;
    }

    void setPictureMirrorType(const PictureMirrorType &_mirrorType) { mirrorType = _mirrorType; }
    void setPictureDepth(int _depth) { depth = _depth; }
    void setPictureSwapRGB(bool _swapRGB) { swapRGB = _swapRGB; }
    void setPictureGrayscal(bool _grayscal) { grayscal = _grayscal; }
    void setPictureBright(int _bright) { bright = _bright; }

    KoPicture picture() const { return image;}

    void loadPicture( const TQString & fileName );

    virtual void flip(bool horizontal );

  protected:
    virtual const char * getOasisElementName() const;
    virtual bool saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const;

    TQPixmap changePictureSettings( TQPixmap _tmpPixmap );
    virtual void saveOasisPictureElement( KoGenStyle &styleobjectauto ) const;
    void loadOasisPictureEffect(KoOasisLoadingContext & context );
    virtual void fillStyle( KoGenStyle& styleObjectAuto, KoGenStyles& mainStyles ) const;

    /**
     * @internal
     * Draws the shadow
     */
    void drawShadow( TQPainter* _painter,  KoZoomHandler* _zoomHandler);

    TQPixmap generatePixmap(KoZoomHandler*_zoomHandler);
    TQString convertValueToPercent( int val ) const;

    KoPictureCollection *imageCollection;
    KoPicture image;

    PictureMirrorType mirrorType, m_cachedMirrorType;
    int depth, m_cachedDepth;
    bool swapRGB, m_cachedSwapRGB;
    bool grayscal, m_cachedGrayscal;
    int bright, m_cachedBright;

    //image effect and its params
    ImageEffect m_effect, m_cachedEffect;
    TQVariant m_ie_par1, m_cachedPar1;
    TQVariant m_ie_par2, m_cachedPar2;
    TQVariant m_ie_par3, m_cachedPar3;

    TQPixmap m_cachedPixmap;
    TQRect m_cachedRect;
};

 } // namespace KSpread
#endif // KSPREAD_OBJECT_H