filters

pngexportdia.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qcheckbox.h>
00021 #include <qimage.h>
00022 #include <qlabel.h>
00023 #include <qlayout.h>
00024 #include <qlineedit.h>
00025 #include <qpaintdevice.h>
00026 #include <qrect.h>
00027 #include <qvbuttongroup.h>
00028 #include <qwidget.h>
00029 
00030 #include <kapplication.h>
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 #include <kmessagebox.h>
00034 
00035 #include <kformulacontainer.h>
00036 #include <kformuladocument.h>
00037 #include <kformulamimesource.h>
00038 
00039 #include "pngexportdia.h"
00040 #include <knuminput.h>
00041 
00042 PNGExportDia::PNGExportDia( const QDomDocument &dom, const QString &outFile, QWidget *parent, const char *name )
00043     : KDialogBase( parent, name, true, i18n("PNG Export Filter Parameters" ), Ok|Cancel ),
00044       _fileOut( outFile )
00045 {
00046     kapp->restoreOverrideCursor();
00047     wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
00048     KFormula::Document* doc = new KFormula::Document;
00049     wrapper->document( doc );
00050     formula = doc->createFormula();
00051     if ( !doc->loadXML( dom ) ) {
00052         kdError() << "Failed." << endl;
00053     }
00054 
00055     setupGUI();
00056 
00057     QRect rect = formula->boundingRect();
00058     realWidth = rect.width();
00059     realHeight = rect.height();
00060     widthEdit->setValue(  realWidth );
00061     heightEdit->setValue(  realHeight  );
00062     percWidthEdit->setValue( 100 );
00063     percHeightEdit->setValue( 100 );
00064 
00065     connectAll();
00066     connect( proportional, SIGNAL( clicked() ),
00067              this, SLOT( proportionalClicked() ) );
00068 }
00069 
00070 PNGExportDia::~PNGExportDia()
00071 {
00072     delete wrapper;
00073 }
00074 
00075 void PNGExportDia::connectAll()
00076 {
00077     connect( widthEdit, SIGNAL( valueChanged(int) ),
00078              this, SLOT( widthChanged( int ) ) );
00079     connect( heightEdit, SIGNAL( valueChanged(int) ),
00080              this, SLOT( heightChanged( int ) ) );
00081     connect( percWidthEdit, SIGNAL( valueChanged(double) ),
00082              this, SLOT( percentWidthChanged( double ) ) );
00083     connect( percHeightEdit, SIGNAL( valueChanged(double) ),
00084              this, SLOT( percentHeightChanged(double ) ) );
00085 }
00086 
00087 void PNGExportDia::disconnectAll()
00088 {
00089     disconnect( widthEdit, SIGNAL( valueChanged(int) ),
00090              this, SLOT( widthChanged( int ) ) );
00091     disconnect( heightEdit, SIGNAL( valueChanged(int) ),
00092              this, SLOT( heightChanged( int ) ) );
00093     disconnect( percWidthEdit, SIGNAL( valueChanged(double) ),
00094              this, SLOT( percentWidthChanged( double ) ) );
00095     disconnect( percHeightEdit, SIGNAL( valueChanged(double) ),
00096              this, SLOT( percentHeightChanged(double ) ) );
00097 }
00098 
00099 void PNGExportDia::widthChanged( int width )
00100 {
00101     disconnectAll();
00102     width = QMIN( width, realWidth*10 );
00103     width = QMAX( width, realWidth/10 );
00104     double percent = 100.*static_cast<double>( width )/static_cast<double>( realWidth );
00105     percWidthEdit->setValue(  percent  );
00106     if ( proportional->isChecked() ) {
00107         percHeightEdit->setValue( percent );
00108         int height = static_cast<int>( realHeight*percent/100. );
00109         heightEdit->setValue(  height );
00110     }
00111     connectAll();
00112 }
00113 
00114 void PNGExportDia::heightChanged( int height )
00115 {
00116     disconnectAll();
00117     height = QMIN( height, realHeight*10 );
00118     height = QMAX( height, realHeight/10 );
00119     double percent = 100.*static_cast<double>( height )/static_cast<double>( realHeight );
00120     percHeightEdit->setValue(  percent  );
00121     if ( proportional->isChecked() ) {
00122         percWidthEdit->setValue(  percent  );
00123         int width = static_cast<int>( realWidth*percent/100. );
00124         widthEdit->setValue( width );
00125     }
00126     connectAll();
00127 }
00128 
00129 void PNGExportDia::percentWidthChanged( double percent )
00130 {
00131     disconnectAll();
00132     percent = QMIN( percent, 1000 );
00133     percent = QMAX( percent, 10 );
00134     int width = static_cast<int>( realWidth*percent/100. );
00135     widthEdit->setValue(  width  );
00136     if ( proportional->isChecked() ) {
00137         int height = static_cast<int>( realHeight*percent/100. );
00138         heightEdit->setValue(  height  );
00139         percHeightEdit->setValue(  percent );
00140     }
00141     connectAll();
00142 }
00143 
00144 void PNGExportDia::percentHeightChanged( double percent )
00145 {
00146     disconnectAll();
00147     percent = QMIN( percent, 1000 );
00148     percent = QMAX( percent, 10 );
00149     if ( proportional->isChecked() ) {
00150         int width = static_cast<int>( realWidth*percent/100. );
00151         widthEdit->setValue(  width  );
00152         percWidthEdit->setValue(  percent  );
00153     }
00154     int height = static_cast<int>( realHeight*percent/100. );
00155     heightEdit->setValue(  height  );
00156     connectAll();
00157 }
00158 
00159 void PNGExportDia::proportionalClicked()
00160 {
00161     if ( proportional->isChecked() ) {
00162         disconnectAll();
00163         int width = widthEdit->value( );
00164         width = QMIN( width, realWidth*10 );
00165         width = QMAX( width, realWidth/10 );
00166         double percent = 100.*static_cast<double>( width )/static_cast<double>( realWidth );
00167         percHeightEdit->setValue(  percent  );
00168         int height = static_cast<int>( realHeight*percent/100. );
00169         heightEdit->setValue(  height  );
00170         connectAll();
00171     }
00172 }
00173 
00174 void PNGExportDia::setupGUI()
00175 {
00176     QWidget *page = new QWidget( this );
00177     setMainWidget(page);
00178 
00179     QBoxLayout* mainLayout = new QVBoxLayout( page, KDialog::marginHint(), KDialog::spacingHint() );
00180 
00181     proportional = new QCheckBox( page, "proportional" );
00182     proportional->setText( i18n( "Keep ratio" ) );
00183     proportional->setChecked( true );
00184     mainLayout->addWidget( proportional );
00185 
00186     QLabel* height = new QLabel( page, "Height" );
00187     height->setText( i18n( "Height" ) );
00188     widthEdit = new KIntNumInput( page, "widthEdit" );
00189     QLabel* width = new QLabel( page, "Width" );
00190     width->setText( i18n( "Width" ) );
00191     heightEdit = new KIntNumInput( page, "heightEdit" );
00192 
00193     QGridLayout* layout1 = new QGridLayout;
00194     layout1->addWidget( height, 1, 0 );
00195     layout1->addWidget( widthEdit, 0, 1 );
00196     layout1->addWidget( width, 0, 0 );
00197     layout1->addWidget( heightEdit, 1, 1 );
00198 
00199     mainLayout->addLayout( layout1 );
00200 
00201     QLabel* percentHeight = new QLabel( page, "PercentHeight" );
00202     percentHeight->setText( i18n( "Height (%)" ) );
00203     QLabel* percentWidth = new QLabel( page, "PercentWidth" );
00204     percentWidth->setText( i18n( "Width (%)" ) );
00205     percWidthEdit = new KDoubleNumInput( page, "percWidthEdit" );
00206     percHeightEdit = new KDoubleNumInput( page, "percHeightEdit" );
00207 
00208     QGridLayout* layout2 = new QGridLayout;
00209     layout2->addWidget( percWidthEdit, 0, 1 );
00210     layout2->addWidget( percHeightEdit, 1, 1 );
00211     layout2->addWidget( percentHeight, 1, 0 );
00212     layout2->addWidget( percentWidth, 0, 0 );
00213 
00214     mainLayout->addLayout( layout2 );
00215 
00216     /* Display the main layout */
00217     mainLayout->addStretch( 5 );
00218     mainLayout->activate();
00219 }
00220 
00221 
00222 void PNGExportDia::slotOk()
00223 {
00224     hide();
00225     //doc->setZoomAndResolution( 100, 600, 600 );
00226     //doc->setZoomAndResolution( 1000, QPaintDevice::x11AppDpiX(), QPaintDevice::x11AppDpiY() );
00227     //doc->newZoomAndResolution( false, false );
00228     int width = widthEdit->value();
00229     int height = heightEdit->value();
00230 //     kdDebug( KFormula::DEBUGID ) << k_funcinfo
00231 //                                  << "(" << width << " " << height << ")"
00232 //                                  << endl;
00233 //     width = realWidth;
00234 //     height = realHeight;
00235     QImage image = formula->drawImage( width, height );
00236     if ( !image.save( _fileOut, "PNG" ) ) {
00237         KMessageBox::error( 0, i18n( "Failed to write file." ), i18n( "PNG Export Error" ) );
00238     }
00239     reject();
00240 }
00241 
00242 #include "pngexportdia.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys