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

« back to all changes in this revision

Viewing changes to filters/kword/libexport/PictureClipart.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
/* This file is part of the KDE project
 
2
   Copyright (c) 2001 Simon Hausmann <hausmann@kde.org>
 
3
   Copyright (c) 2001 David Faure <faure@kde.org>
 
4
   Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
 
5
 
 
6
   This library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2 of the License, or (at your option) any later version.
 
10
 
 
11
   This library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public License
 
17
   along with this library; see the file COPYING.LIB.  If not, write to
 
18
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 * Boston, MA 02110-1301, USA.
 
20
*/
 
21
 
 
22
#include "PictureClipart.h"
 
23
 
 
24
#include <QBuffer>
 
25
#include <QPainter>
 
26
#include <QPixmap>
 
27
 
 
28
#include <kdebug.h>
 
29
 
 
30
 
 
31
#include "PictureKey.h"
 
32
#include "PictureBase.h"
 
33
 
 
34
 
 
35
PictureClipart::PictureClipart(void) : m_clipart(PictureType::formatVersionQPicture)
 
36
{
 
37
}
 
38
 
 
39
PictureClipart::~PictureClipart(void)
 
40
{
 
41
}
 
42
 
 
43
PictureBase* PictureClipart::newCopy(void) const
 
44
{
 
45
    return new PictureClipart(*this);
 
46
}
 
47
 
 
48
PictureType::Type PictureClipart::getType(void) const
 
49
{
 
50
    return PictureType::TypeClipart;
 
51
}
 
52
 
 
53
bool PictureClipart::isNull(void) const
 
54
{
 
55
    return m_clipart.isNull();
 
56
}
 
57
 
 
58
void PictureClipart::drawQPicture(QPicture& clipart, QPainter& painter,
 
59
                                    int x, int y, int width, int height, int sx, int sy, int sw, int sh)
 
60
{
 
61
    kDebug(30508) << "Drawing PictureClipart" << this;
 
62
    kDebug(30508) << "  x=" << x << " y=" << y << " width=" << width << " height=" << height;
 
63
    kDebug(30508) << "  sx=" << sx << " sy=" << sy << " sw=" << sw << " sh=" << sh;
 
64
    painter.save();
 
65
    // Thanks to Harri, Qt3 makes it much easier than Qt2 ;)
 
66
    QRect br = clipart.boundingRect();
 
67
    kDebug(30508) << "  Bounding rect." << br;
 
68
 
 
69
    painter.translate(x, y); // Translating must be done before scaling!
 
70
    if (br.width() && br.height())
 
71
        painter.scale(qreal(width) / qreal(br.width()), qreal(height) / qreal(br.height()));
 
72
    else
 
73
        kWarning(30508) << "Null bounding rectangle: " << br.width() << " x " << br.height();
 
74
    painter.drawPicture(0, 0, clipart);
 
75
    painter.restore();
 
76
}
 
77
 
 
78
void PictureClipart::draw(QPainter& painter, int x, int y, int width, int height, int sx, int sy, int sw, int sh, bool /*fastMode*/)
 
79
{
 
80
    drawQPicture(m_clipart, painter, x, y, width, height, sx, sy, sw, sh);
 
81
}
 
82
 
 
83
bool PictureClipart::loadData(const QByteArray& array, const QString& extension)
 
84
{
 
85
    // Second, create the original clipart
 
86
    kDebug(30508) << "Trying to load clipart... (Size:" << m_rawData.size() << ")";
 
87
    m_rawData = array;
 
88
    QBuffer buffer(&m_rawData);
 
89
    buffer.open(QIODevice::ReadOnly);
 
90
    bool check = true;
 
91
    if (extension == "svg") {
 
92
        if (!m_clipart.load(&buffer, "svg")) {
 
93
            kWarning(30508) << "Loading SVG has failed! (PictureClipart::load)";
 
94
            check = false;
 
95
        }
 
96
    } else {
 
97
        if (!m_clipart.load(&buffer, NULL)) {
 
98
            kWarning(30508) << "Loading QPicture has failed! (PictureClipart::load)";
 
99
            check = false;
 
100
        }
 
101
    }
 
102
    buffer.close();
 
103
    return check;
 
104
}
 
105
 
 
106
bool PictureClipart::save(QIODevice* io) const
 
107
{
 
108
    // We save the raw data, as the SVG supposrt in QPicture is poor
 
109
    qint64 size = io->write(m_rawData); // WARNING: writeBlock returns Q_LONG but size() Q_ULONG!
 
110
    return (size == m_rawData.size());
 
111
}
 
112
 
 
113
QSize PictureClipart::getOriginalSize(void) const
 
114
{
 
115
    return m_clipart.boundingRect().size();
 
116
}
 
117
 
 
118
QPixmap PictureClipart::generatePixmap(const QSize& size, bool /*smoothScale*/)
 
119
{
 
120
    // Not sure if it works, but it worked for PictureFilePreviewWidget::setClipart
 
121
    QPixmap pixmap(size);
 
122
    QPainter p;
 
123
 
 
124
    p.begin(&pixmap);
 
125
    p.setBackground(QBrush(Qt::white));
 
126
    pixmap.fill(Qt::white);
 
127
 
 
128
    QRect br = m_clipart.boundingRect();
 
129
    if (br.width() && br.height())
 
130
        p.scale((qreal)pixmap.width() / (qreal)br.width(), (qreal)pixmap.height() / (qreal)br.height());
 
131
    p.drawPicture(0, 0, m_clipart);
 
132
    p.end();
 
133
    return pixmap;
 
134
}
 
135
 
 
136
QString PictureClipart::getMimeType(const QString& extension) const
 
137
{
 
138
    if (extension == "svg")
 
139
        return "image/svg+xml";
 
140
    else
 
141
        return "image/x-vnd.trolltech.qpicture";
 
142
}
 
143