summaryrefslogtreecommitdiffstats
path: root/lib/kofficecore/KoPictureWmf.cpp
blob: 3f2d0a7e0c3581e5136c29cdf78d5327f2031a0f (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
/* This file is part of the KDE project
   Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
   Copyright (c) 2001 David Faure <faure@kde.org>
   Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>

   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, or (at your option) any later version.

   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.
*/

#include <tqbuffer.h>
#include <tqpainter.h>
#include <tqpicture.h>
#include <tqpixmap.h>

#include <kdebug.h>
#include <tdeversion.h>
#if ! KDE_IS_VERSION( 3,1,90 )
#include <kdebugclasses.h>
#endif

#include <kowmfpaint.h>
#include "KoPictureKey.h"
#include "KoPictureBase.h"
#include "KoPictureWmf.h"

KoPictureWmf::KoPictureWmf(void) : m_clipart(KoPictureType::formatVersionTQPicture)
{
}

KoPictureWmf::~KoPictureWmf(void)
{
}

KoPictureBase* KoPictureWmf::newCopy(void) const
{
    return new KoPictureWmf(*this);
}

KoPictureType::Type KoPictureWmf::getType(void) const
{
    return KoPictureType::TypeWmf;
}

bool KoPictureWmf::isNull(void) const
{
    return m_clipart.isNull();
}

void KoPictureWmf::drawTQPicture(TQPicture& clipart, TQPainter& painter,
    int x, int y, int width, int height, int sx, int sy, int sw, int sh)
{
    kdDebug(30003) << "Drawing KoPictureWmf " << this << endl;
    kdDebug(30003) << "  x=" << x << " y=" << y << " width=" << width << " height=" << height << endl;
    kdDebug(30003) << "  sx=" << sx << " sy=" << sy << " sw=" << sw << " sh=" << sh << endl;
    painter.save();
    // Thanks to Harri, TQt3 makes it much easier than TQt2 ;)
    TQRect br = clipart.boundingRect();
    kdDebug(30003) << "  Bounding rect. " << br << endl;

    painter.translate(x,y); // Translating must be done before scaling!
    if ( br.width() && br.height() )
        painter.scale(double(width)/double(br.width()),double(height)/double(br.height()));
    else
        kdWarning(30003) << "Null bounding rectangle: " << br.width() << " x " << br.height() << endl;
    painter.drawPicture(0,0,clipart);
    painter.restore();
}

void KoPictureWmf::draw(TQPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
{
    drawTQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
}

bool KoPictureWmf::loadData(const TQByteArray& array, const TQString& /* extension */)
{
    // Second, create the original clipart
    kdDebug(30003) << "Trying to load clipart... (Size:" << array.size() << ")" << endl;
    m_rawData=array;

    KoWmfPaint wmf;
    if (!wmf.load(array))
    {
        kdWarning(30003) << "Loading WMF has failed! (KoPictureWmf::load)" << endl;
        return false;
    }
    m_originalSize = wmf.boundingRect().size();
    // draw wmf file with relative coordinate
    wmf.play(m_clipart, true);

    return true;
}

bool KoPictureWmf::save(TQIODevice* io) const
{
    // We save the raw data, as the SVG supposrt in TQPicture is poor
    TQ_ULONG size=io->writeBlock(m_rawData); // WARNING: writeBlock returns TQ_LONG but size() TQ_ULONG!
    return (size==m_rawData.size());
}

TQSize KoPictureWmf::getOriginalSize(void) const
{
    return m_originalSize;
}

TQPixmap KoPictureWmf::generatePixmap(const TQSize& size, bool /*smoothScale*/)
{
    // Not sure if it works, but it worked for KoPictureFilePreviewWidget::setClipart
    TQPixmap pixmap(size);
    TQPainter p;

    p.begin( &pixmap );
    p.setBackgroundColor( TQt::white );
    pixmap.fill( TQt::white );

    if ( m_originalSize.width() && m_originalSize.height() )
        p.scale( (double)pixmap.width() / (double)m_originalSize.width(), (double)pixmap.height() / (double)m_originalSize.height() );
    p.drawPicture( m_clipart );
    p.end();
    return pixmap;
}

TQString KoPictureWmf::getMimeType(const TQString& /* extension */) const
{
    return "image/x-wmf";
}