summaryrefslogtreecommitdiffstats
path: root/kword/KWInsertPicDia.cpp
blob: df29e33005b179eb37686a01de34f346304bbe69 (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
/* This file is part of the KDE project
   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 <qpainter.h>
#include <qpushbutton.h>
#include <qbitmap.h>
#include <qlayout.h>
#include <qcheckbox.h>
#include <qscrollview.h>

#include <kdebug.h>
#include <klocale.h>
#include <kfiledialog.h>
#include <kimageio.h>
#include <kio/netaccess.h>

#include <KoPicture.h>
#include <KoPictureFilePreview.h>

#include "KWDocument.h"
#include "KWInsertPicDia.h"
#include <qtimer.h>

/**
 * This is the preview that appears on the right of the "Insert picture" dialog.
 * Not to be confused with KoPictureFilePreview, the one inside the file dialog!
 * (Note: this one has to remain separate, for the day we add options like flipping, rotating, etc.)
 */
class KWInsertPicPreview : public QScrollView
{
public:
    KWInsertPicPreview( QWidget *parent )
        : QScrollView( parent )
    {
        viewport()->setBackgroundMode( PaletteBase );
        setMinimumSize( 300, 200 );
    }

    virtual ~KWInsertPicPreview() {}

    bool setPicture( const KoPicture & picture )
    {
        if (!picture.isNull())
        {
            m_size = picture.getOriginalSize();
            m_picture = picture;
            resizeContents( m_size.width(), m_size.height() );
            repaint( false );
            return true;
        }
        else
            return false;
    }

    void drawContents( QPainter *p, int, int, int, int )
    {
        p->setBackgroundColor( Qt::white );
        // Be sure that the background is white (for transparency)
        p->fillRect(0, 0, m_size.width(), m_size.height(), QBrush( Qt::white ));
        m_picture.draw( *p, 0 ,0, m_size.width(), m_size.height());
    }

private:
    KoPicture m_picture;
    QSize m_size;
};

//////////////

KWInsertPicDia::KWInsertPicDia( QWidget *parent, bool _inline, bool _keepRatio, KWDocument *_doc, const char *name )
    : KDialogBase( Plain, i18n("Insert Picture"), Ok|Cancel, Ok, parent, name, true ),
    m_bFirst ( true ), m_doc ( _doc )
{
    setInitialSize( QSize(400, 300) );
    QWidget *page = plainPage();
    QGridLayout *grid = new QGridLayout( page, 4, 2, KDialog::marginHint(), KDialog::spacingHint() );

    QPushButton *pbImage = new QPushButton( i18n( "Choose &Picture..." ), page );
    grid->addWidget( pbImage, 0, 0 );
    connect( pbImage, SIGNAL( clicked() ), SLOT( slotChooseImage() ) );

    m_cbInline = new QCheckBox( i18n( "Insert picture inline" ), page );
    grid->addWidget( m_cbInline, 1, 0 );

    m_cbKeepRatio= new QCheckBox( i18n("Retain original aspect ratio"),page);
    grid->addWidget( m_cbKeepRatio, 2, 0);

    m_preview = new KWInsertPicPreview( page );
    grid->addMultiCellWidget( m_preview, 0, 3, 1, 1 );

    // Stretch the buttons and checkboxes a little, but stretch the preview much more
    grid->setRowStretch( 0, 1 );
    grid->setRowStretch( 1, 1 );
    grid->setRowStretch( 2, 1 );
    grid->setRowStretch( 3, 10 );
    grid->setColStretch( 0, 1 );
    grid->setColStretch( 1, 10 );
    m_cbKeepRatio->setChecked(_keepRatio);
    m_cbInline->setChecked( _inline );
    enableButtonOK( false );
    setFocus();
    slotChooseImage(); // save the user time, directly open the dialog
}

bool KWInsertPicDia::makeInline() const
{
    return m_cbInline->isChecked();
}

bool KWInsertPicDia::keepRatio() const
{
    return m_cbKeepRatio->isChecked();
}

void KWInsertPicDia::slotChooseImage()
{
    KoPicture tmppicture = KWInsertPicDia::selectPictureDia( ":picture", this );
    if ( !tmppicture.isNull() ) // if canceled, keep current picture (#72827)
        m_picture = tmppicture;
    if ( m_picture.isNull() && m_bFirst)
    {
        kdDebug() << "KWInsertPicDia::slotChooseImage cancelled by user." << endl;
        // Close, but delayed, otherwise it won't work (we only return from the ctor)
        QTimer::singleShot( 0, this, SLOT( cancel() ) );
        return;
    }
    enableButtonOK ( m_preview->setPicture( m_picture ) );
    m_bFirst = false;
}

KoPicture KWInsertPicDia::selectPictureDia( const QString & _path, QWidget* parent )
{
    QStringList mimetypes ( KImageIO::mimeTypes( KImageIO::Reading ) );
    mimetypes += KoPictureFilePreview::clipartMimeTypes();

    KFileDialog fd( _path, QString::null, parent, 0, TRUE );
    fd.setMimeFilter( mimetypes );
    fd.setCaption(i18n("Choose Picture"));
    return selectPicture( fd, parent );
}

KoPicture KWInsertPicDia::selectPicture( KFileDialog & fd, QWidget *parent )
{
    KoPicture picture;

    fd.setPreviewWidget( new KoPictureFilePreview( &fd ) );
    KURL url;
    if ( fd.exec() == QDialog::Accepted )
        url = fd.selectedURL();

    if( !url.isEmpty() )
        picture.setKeyAndDownloadPicture( url, parent );

    return picture;
}

KoPicture KWInsertPicDia::picture ( void ) const
{
    kdDebug() << m_picture.getKey().toString() << " selected in KWInsertPicDia" << endl;
    return m_picture;
}

#include "KWInsertPicDia.moc"