filters

pngexportdia.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqcheckbox.h>
21#include <tqimage.h>
22#include <tqlabel.h>
23#include <tqlayout.h>
24#include <tqlineedit.h>
25#include <tqpaintdevice.h>
26#include <tqrect.h>
27#include <tqvbuttongroup.h>
28#include <tqwidget.h>
29
30#include <tdeapplication.h>
31#include <kdebug.h>
32#include <tdelocale.h>
33#include <tdemessagebox.h>
34
35#include <kformulacontainer.h>
36#include <kformuladocument.h>
37#include <kformulamimesource.h>
38
39#include "pngexportdia.h"
40#include <knuminput.h>
41
42PNGExportDia::PNGExportDia( const TQDomDocument &dom, const TQString &outFile, TQWidget *parent, const char *name )
43 : KDialogBase( parent, name, true, i18n("PNG Export Filter Parameters" ), Ok|Cancel ),
44 _fileOut( outFile )
45{
46 kapp->restoreOverrideCursor();
47 wrapper = new KFormula::DocumentWrapper( kapp->config(), 0 );
48 KFormula::Document* doc = new KFormula::Document;
49 wrapper->document( doc );
50 formula = doc->createFormula();
51 if ( !doc->loadXML( dom ) ) {
52 kdError() << "Failed." << endl;
53 }
54
55 setupGUI();
56
57 TQRect rect = formula->boundingRect();
58 realWidth = rect.width();
59 realHeight = rect.height();
60 widthEdit->setValue( realWidth );
61 heightEdit->setValue( realHeight );
62 percWidthEdit->setValue( 100 );
63 percHeightEdit->setValue( 100 );
64
65 connectAll();
66 connect( proportional, TQ_SIGNAL( clicked() ),
67 this, TQ_SLOT( proportionalClicked() ) );
68}
69
70PNGExportDia::~PNGExportDia()
71{
72 delete wrapper;
73}
74
75void PNGExportDia::connectAll()
76{
77 connect( widthEdit, TQ_SIGNAL( valueChanged(int) ),
78 this, TQ_SLOT( widthChanged( int ) ) );
79 connect( heightEdit, TQ_SIGNAL( valueChanged(int) ),
80 this, TQ_SLOT( heightChanged( int ) ) );
81 connect( percWidthEdit, TQ_SIGNAL( valueChanged(double) ),
82 this, TQ_SLOT( percentWidthChanged( double ) ) );
83 connect( percHeightEdit, TQ_SIGNAL( valueChanged(double) ),
84 this, TQ_SLOT( percentHeightChanged(double ) ) );
85}
86
87void PNGExportDia::disconnectAll()
88{
89 disconnect( widthEdit, TQ_SIGNAL( valueChanged(int) ),
90 this, TQ_SLOT( widthChanged( int ) ) );
91 disconnect( heightEdit, TQ_SIGNAL( valueChanged(int) ),
92 this, TQ_SLOT( heightChanged( int ) ) );
93 disconnect( percWidthEdit, TQ_SIGNAL( valueChanged(double) ),
94 this, TQ_SLOT( percentWidthChanged( double ) ) );
95 disconnect( percHeightEdit, TQ_SIGNAL( valueChanged(double) ),
96 this, TQ_SLOT( percentHeightChanged(double ) ) );
97}
98
99void PNGExportDia::widthChanged( int width )
100{
101 disconnectAll();
102 width = TQMIN( width, realWidth*10 );
103 width = TQMAX( width, realWidth/10 );
104 double percent = 100.*static_cast<double>( width )/static_cast<double>( realWidth );
105 percWidthEdit->setValue( percent );
106 if ( proportional->isChecked() ) {
107 percHeightEdit->setValue( percent );
108 int height = static_cast<int>( realHeight*percent/100. );
109 heightEdit->setValue( height );
110 }
111 connectAll();
112}
113
114void PNGExportDia::heightChanged( int height )
115{
116 disconnectAll();
117 height = TQMIN( height, realHeight*10 );
118 height = TQMAX( height, realHeight/10 );
119 double percent = 100.*static_cast<double>( height )/static_cast<double>( realHeight );
120 percHeightEdit->setValue( percent );
121 if ( proportional->isChecked() ) {
122 percWidthEdit->setValue( percent );
123 int width = static_cast<int>( realWidth*percent/100. );
124 widthEdit->setValue( width );
125 }
126 connectAll();
127}
128
129void PNGExportDia::percentWidthChanged( double percent )
130{
131 disconnectAll();
132 percent = TQMIN( percent, 1000 );
133 percent = TQMAX( percent, 10 );
134 int width = static_cast<int>( realWidth*percent/100. );
135 widthEdit->setValue( width );
136 if ( proportional->isChecked() ) {
137 int height = static_cast<int>( realHeight*percent/100. );
138 heightEdit->setValue( height );
139 percHeightEdit->setValue( percent );
140 }
141 connectAll();
142}
143
144void PNGExportDia::percentHeightChanged( double percent )
145{
146 disconnectAll();
147 percent = TQMIN( percent, 1000 );
148 percent = TQMAX( percent, 10 );
149 if ( proportional->isChecked() ) {
150 int width = static_cast<int>( realWidth*percent/100. );
151 widthEdit->setValue( width );
152 percWidthEdit->setValue( percent );
153 }
154 int height = static_cast<int>( realHeight*percent/100. );
155 heightEdit->setValue( height );
156 connectAll();
157}
158
159void PNGExportDia::proportionalClicked()
160{
161 if ( proportional->isChecked() ) {
162 disconnectAll();
163 int width = widthEdit->value( );
164 width = TQMIN( width, realWidth*10 );
165 width = TQMAX( width, realWidth/10 );
166 double percent = 100.*static_cast<double>( width )/static_cast<double>( realWidth );
167 percHeightEdit->setValue( percent );
168 int height = static_cast<int>( realHeight*percent/100. );
169 heightEdit->setValue( height );
170 connectAll();
171 }
172}
173
174void PNGExportDia::setupGUI()
175{
176 TQWidget *page = new TQWidget( this );
177 setMainWidget(page);
178
179 TQBoxLayout* mainLayout = new TQVBoxLayout( page, KDialog::marginHint(), KDialog::spacingHint() );
180
181 proportional = new TQCheckBox( page, "proportional" );
182 proportional->setText( i18n( "Keep ratio" ) );
183 proportional->setChecked( true );
184 mainLayout->addWidget( proportional );
185
186 TQLabel* height = new TQLabel( page, "Height" );
187 height->setText( i18n( "Height" ) );
188 widthEdit = new KIntNumInput( page, "widthEdit" );
189 TQLabel* width = new TQLabel( page, "Width" );
190 width->setText( i18n( "Width" ) );
191 heightEdit = new KIntNumInput( page, "heightEdit" );
192
193 TQGridLayout* layout1 = new TQGridLayout;
194 layout1->addWidget( height, 1, 0 );
195 layout1->addWidget( widthEdit, 0, 1 );
196 layout1->addWidget( width, 0, 0 );
197 layout1->addWidget( heightEdit, 1, 1 );
198
199 mainLayout->addLayout( layout1 );
200
201 TQLabel* percentHeight = new TQLabel( page, "PercentHeight" );
202 percentHeight->setText( i18n( "Height (%)" ) );
203 TQLabel* percentWidth = new TQLabel( page, "PercentWidth" );
204 percentWidth->setText( i18n( "Width (%)" ) );
205 percWidthEdit = new KDoubleNumInput( page, "percWidthEdit" );
206 percHeightEdit = new KDoubleNumInput( page, "percHeightEdit" );
207
208 TQGridLayout* layout2 = new TQGridLayout;
209 layout2->addWidget( percWidthEdit, 0, 1 );
210 layout2->addWidget( percHeightEdit, 1, 1 );
211 layout2->addWidget( percentHeight, 1, 0 );
212 layout2->addWidget( percentWidth, 0, 0 );
213
214 mainLayout->addLayout( layout2 );
215
216 /* Display the main layout */
217 mainLayout->addStretch( 5 );
218 mainLayout->activate();
219}
220
221
222void PNGExportDia::slotOk()
223{
224 hide();
225 //doc->setZoomAndResolution( 100, 600, 600 );
226 //doc->setZoomAndResolution( 1000, TQPaintDevice::x11AppDpiX(), TQPaintDevice::x11AppDpiY() );
227 //doc->newZoomAndResolution( false, false );
228 int width = widthEdit->value();
229 int height = heightEdit->value();
230// kdDebug( KFormula::DEBUGID ) << k_funcinfo
231// << "(" << width << " " << height << ")"
232// << endl;
233// width = realWidth;
234// height = realHeight;
235 TQImage image = formula->drawImage( width, height );
236 if ( !image.save( _fileOut, "PNG" ) ) {
237 KMessageBox::error( 0, i18n( "Failed to write file." ), i18n( "PNG Export Error" ) );
238 }
239 reject();
240}
241
242#include "pngexportdia.moc"