moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / filters / exporttoimagedialog.cc
blobbb1581beadf9836cc4271e7838fe993b2ebdb619
1 // Copyright (C) 2002 Dominique Devriese <devriese@kde.org>
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 // 02111-1307, USA.
18 #include "exporttoimagedialog.h"
19 #include "exporttoimagedialog.moc"
21 #include "../kig/kig_document.h"
22 #include "../kig/kig_part.h"
23 #include "../kig/kig_view.h"
24 #include "../misc/kigpainter.h"
26 #include <qcheckbox.h>
28 #include <kiconloader.h>
29 #include <kimageio.h>
30 #include <klocale.h>
31 #include <kmessagebox.h>
32 #include <knuminput.h>
33 #include <kpushbutton.h>
34 #include <kurlrequester.h>
36 ExportToImageDialog::ExportToImageDialog( KigWidget* v, const KigPart* part )
37 : ExportToImageDialogBase( v, "Export to image dialog", true ),
38 mv( v ), mpart( part ), msize( v->size() ), minternallysettingstuff( false )
40 KIconLoader* l = part->instance()->iconLoader();
41 OKButton->setIconSet( QIconSet( l->loadIcon( "button_ok", KIcon::Small ) ) );
42 CancelButton->setIconSet( QIconSet( l->loadIcon( "button_cancel", KIcon::Small ) ) );
44 WidthInput->setValue( msize.width() );
45 HeightInput->setValue( msize.height() );
47 showGridCheckBox->setChecked( part->document().grid() );
48 showAxesCheckBox->setChecked( part->document().axes() );
50 static bool kimageioRegistered = false;
51 if ( ! kimageioRegistered )
53 KImageIO::registerFormats();
54 kimageioRegistered = true;
57 URLRequester->setFilter( KImageIO::pattern( KImageIO::Writing ) );
58 URLRequester->setMode( KFile::File | KFile::LocalOnly );
59 URLRequester->setCaption( i18n( "Export to Image" ) );
61 connect( OKButton, SIGNAL( clicked() ), this, SLOT( slotOKPressed() ) );
62 connect( CancelButton, SIGNAL( clicked() ), this, SLOT( slotCancelPressed() ) );
63 connect( WidthInput, SIGNAL( valueChanged( int ) ), this, SLOT( slotWidthChanged( int ) ) );
64 connect( HeightInput, SIGNAL( valueChanged( int ) ), this, SLOT( slotHeightChanged( int ) ) );
67 void ExportToImageDialog::slotOKPressed()
69 QString filename = URLRequester->url();
70 if ( filename.isEmpty() )
72 KMessageBox::sorry( mv, i18n( "Please enter a file name." ) );
73 return;
75 QFile file( filename );
76 if ( file.exists() )
78 int ret = KMessageBox::warningYesNo( mv,
79 i18n( "The file \"%1\" already exists. Do you wish to overwrite it?" )
80 .arg( filename ), i18n( "Overwrite file?" ) );
81 if ( ret != KMessageBox::Yes ) return;
84 if ( ! file.open( IO_WriteOnly ) )
86 KMessageBox::sorry( mv,
87 i18n( "The file \"%1\" could not be opened. Please check if the file permissions are set correctly." )
88 .arg( filename ) );
89 return;
92 QString type = KImageIO::type( filename );
93 if ( type.isNull() )
95 KMessageBox::sorry( mv,
96 i18n( "Sorry, this file format is not supported." ) );
97 return;
100 kdDebug() << type << endl;
102 QPixmap img( QSize( WidthInput->value(), HeightInput->value() ) );
103 img.fill( Qt::white );
104 KigPainter p( ScreenInfo( mv->screenInfo().shownRect(), img.rect() ), &img, mpart->document());
105 p.setWholeWinOverlay();
106 p.drawGrid( mpart->document().coordinateSystem(), showGridCheckBox->isOn(), showAxesCheckBox->isOn() );
107 // FIXME: show the selections ?
108 p.drawObjects( mpart->document().objects(), false );
109 if ( ! img.save( filename, type.latin1() ) )
111 KMessageBox::error( mv, i18n( "Sorry, something went wrong while saving to image \"%1\"" ).arg( filename ) );
112 return;
114 else accept();
117 void ExportToImageDialog::slotCancelPressed()
119 reject();
122 void ExportToImageDialog::slotWidthChanged( int w )
124 if ( ! minternallysettingstuff )
126 minternallysettingstuff = true;
127 HeightInput->setValue( w * msize.height() / msize.width() );
128 minternallysettingstuff = false;
132 void ExportToImageDialog::slotHeightChanged( int h )
134 if ( ! minternallysettingstuff )
136 minternallysettingstuff = true;
137 WidthInput->setValue( h * msize.width() / msize.height() );
138 minternallysettingstuff = false;
142 ExportToImageDialog::~ExportToImageDialog()