kword

KWInsertPicDia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3 Copyright (C) 2002 Nicolas GOUTTE <goutte@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqpainter.h>
22#include <tqpushbutton.h>
23#include <tqbitmap.h>
24#include <tqlayout.h>
25#include <tqcheckbox.h>
26#include <tqscrollview.h>
27
28#include <kdebug.h>
29#include <tdelocale.h>
30#include <tdefiledialog.h>
31#include <kimageio.h>
32#include <tdeio/netaccess.h>
33
34#include <KoPicture.h>
35#include <KoPictureFilePreview.h>
36
37#include "KWDocument.h"
38#include "KWInsertPicDia.h"
39#include <tqtimer.h>
40
46class KWInsertPicPreview : public TQScrollView
47{
48public:
49 KWInsertPicPreview( TQWidget *parent )
50 : TQScrollView( parent )
51 {
52 viewport()->setBackgroundMode( PaletteBase );
53 setMinimumSize( 300, 200 );
54 }
55
56 virtual ~KWInsertPicPreview() {}
57
58 bool setPicture( const KoPicture & picture )
59 {
60 if (!picture.isNull())
61 {
62 m_size = picture.getOriginalSize();
63 m_picture = picture;
64 resizeContents( m_size.width(), m_size.height() );
65 repaint( false );
66 return true;
67 }
68 else
69 return false;
70 }
71
72 void drawContents( TQPainter *p, int, int, int, int )
73 {
74 p->setBackgroundColor( TQt::white );
75 // Be sure that the background is white (for transparency)
76 p->fillRect(0, 0, m_size.width(), m_size.height(), TQBrush( TQt::white ));
77 m_picture.draw( *p, 0 ,0, m_size.width(), m_size.height());
78 }
79
80private:
81 KoPicture m_picture;
82 TQSize m_size;
83};
84
86
87KWInsertPicDia::KWInsertPicDia( TQWidget *parent, bool _inline, bool _keepRatio, KWDocument *_doc, const char *name )
88 : KDialogBase( Plain, i18n("Insert Picture"), Ok|Cancel, Ok, parent, name, true ),
89 m_bFirst ( true ), m_doc ( _doc )
90{
91 setInitialSize( TQSize(400, 300) );
92 TQWidget *page = plainPage();
93 TQGridLayout *grid = new TQGridLayout( page, 4, 2, KDialog::marginHint(), KDialog::spacingHint() );
94
95 TQPushButton *pbImage = new TQPushButton( i18n( "Choose &Picture..." ), page );
96 grid->addWidget( pbImage, 0, 0 );
97 connect( pbImage, TQ_SIGNAL( clicked() ), TQ_SLOT( slotChooseImage() ) );
98
99 m_cbInline = new TQCheckBox( i18n( "Insert picture inline" ), page );
100 grid->addWidget( m_cbInline, 1, 0 );
101
102 m_cbKeepRatio= new TQCheckBox( i18n("Retain original aspect ratio"),page);
103 grid->addWidget( m_cbKeepRatio, 2, 0);
104
105 m_preview = new KWInsertPicPreview( page );
106 grid->addMultiCellWidget( m_preview, 0, 3, 1, 1 );
107
108 // Stretch the buttons and checkboxes a little, but stretch the preview much more
109 grid->setRowStretch( 0, 1 );
110 grid->setRowStretch( 1, 1 );
111 grid->setRowStretch( 2, 1 );
112 grid->setRowStretch( 3, 10 );
113 grid->setColStretch( 0, 1 );
114 grid->setColStretch( 1, 10 );
115 m_cbKeepRatio->setChecked(_keepRatio);
116 m_cbInline->setChecked( _inline );
117 enableButtonOK( false );
118 setFocus();
119 slotChooseImage(); // save the user time, directly open the dialog
120}
121
122bool KWInsertPicDia::makeInline() const
123{
124 return m_cbInline->isChecked();
125}
126
127bool KWInsertPicDia::keepRatio() const
128{
129 return m_cbKeepRatio->isChecked();
130}
131
132void KWInsertPicDia::slotChooseImage()
133{
134 KoPicture tmppicture = KWInsertPicDia::selectPictureDia( ":picture", this );
135 if ( !tmppicture.isNull() ) // if canceled, keep current picture (#72827)
136 m_picture = tmppicture;
137 if ( m_picture.isNull() && m_bFirst)
138 {
139 kdDebug() << "KWInsertPicDia::slotChooseImage cancelled by user." << endl;
140 // Close, but delayed, otherwise it won't work (we only return from the ctor)
141 TQTimer::singleShot( 0, this, TQ_SLOT( cancel() ) );
142 return;
143 }
144 enableButtonOK ( m_preview->setPicture( m_picture ) );
145 m_bFirst = false;
146}
147
148KoPicture KWInsertPicDia::selectPictureDia( const TQString & _path, TQWidget* parent )
149{
150 TQStringList mimetypes ( KImageIO::mimeTypes( KImageIO::Reading ) );
151 mimetypes += KoPictureFilePreview::clipartMimeTypes();
152
153 KFileDialog fd( _path, TQString(), parent, 0, true );
154 fd.setMimeFilter( mimetypes );
155 fd.setCaption(i18n("Choose Picture"));
156 return selectPicture( fd, parent );
157}
158
159KoPicture KWInsertPicDia::selectPicture( KFileDialog & fd, TQWidget *parent )
160{
161 KoPicture picture;
162
163 fd.setPreviewWidget( new KoPictureFilePreview( &fd ) );
164 KURL url;
165 if ( fd.exec() == TQDialog::Accepted )
166 url = fd.selectedURL();
167
168 if( !url.isEmpty() )
169 picture.setKeyAndDownloadPicture( url, parent );
170
171 return picture;
172}
173
174KoPicture KWInsertPicDia::picture ( void ) const
175{
176 kdDebug() << m_picture.getKey().toString() << " selected in KWInsertPicDia" << endl;
177 return m_picture;
178}
179
180#include "KWInsertPicDia.moc"