summaryrefslogtreecommitdiffstats
path: root/filters/libdialogfilter/exportsizedia.cpp
blob: 6c455e04b8a54001723329192779d3f2642bf184 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* This file is part of the KDE project
   Copyright (C) 2002 Ulrich Kuettler <ulrich.kuettler@mailbox.tu-dresden.de>

   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 <tqcheckbox.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpaintdevice.h>
#include <tqrect.h>
#include <tqvbuttongroup.h>
#include <tqwidget.h>

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <knuminput.h>

#include "exportsizedia.h"


ExportSizeDia::ExportSizeDia( int width, int height, 
			    TQWidget *parent, const char *name )
    : KDialogBase( parent, name, true,
		   i18n("Export Filter Parameters" ), Ok|Cancel )
{
    kapp->restoreOverrideCursor();

    setupGUI();

    m_realWidth  = width;
    m_realHeight = height;
    m_widthEdit ->setValue( m_realWidth );
    m_heightEdit->setValue( m_realHeight  );
    m_percWidthEdit->setValue( 100 );
    m_percHeightEdit->setValue( 100 );

    connectAll();
    connect( m_proportional, TQ_SIGNAL( clicked() ),
             this,         TQ_SLOT( proportionalClicked() ) );
}


ExportSizeDia::~ExportSizeDia()
{
}


void ExportSizeDia::setupGUI()
{
    //resize( size() );
    TQWidget *page = new TQWidget( this );
    setMainWidget(page);

#if 0
    TQBoxLayout* mainLayout = new TQVBoxLayout( page, 
					      KDialog::marginHint(), 
					      KDialog::spacingHint() );
#else
    TQGridLayout *mainLayout = new TQGridLayout( page, 5, 2,
					       KDialog::marginHint(), 
					       KDialog::spacingHint() );
#endif
    m_proportional = new TQCheckBox( page, "proportional" );
    m_proportional->setText( i18n( "Keep ratio" ) );
    m_proportional->setChecked( true );
    mainLayout->addWidget( m_proportional, 0, 0 );

    TQLabel* width = new TQLabel( page, "width" );
    width->setText( i18n( "Width:" ) );
    m_widthEdit = new KIntNumInput( page, "widthEdit" );
    TQLabel* height = new TQLabel( page, "height" );
    height->setText( i18n( "Height:" ) );
    m_heightEdit = new KIntNumInput( page, "heightEdit" );

    mainLayout->addWidget( width,      1, 0 );
    mainLayout->addWidget( m_widthEdit,  1, 1 );
    mainLayout->addWidget( height,     2, 0 );
    mainLayout->addWidget( m_heightEdit, 2, 1 );

    TQLabel* percentWidth = new TQLabel( page, "PercentWidth" );
    percentWidth->setText( i18n( "Width (%):" ) );
    m_percWidthEdit = new KDoubleNumInput( page, "percWidthEdit" );
    TQLabel* percentHeight = new TQLabel( page, "PercentHeight" );
    percentHeight->setText( i18n( "Height (%):" ) );
    m_percHeightEdit = new KDoubleNumInput( page, "percHeightEdit" );

    mainLayout->addWidget( percentWidth,   3, 0 );
    mainLayout->addWidget( m_percHeightEdit, 3, 1 );
    mainLayout->addWidget( percentHeight,  4, 0 );
    mainLayout->addWidget( m_percWidthEdit,  4, 1 );

    /* Display the main layout */
    //mainLayout->addStretch( 5 );
    mainLayout->activate();
}


// ----------------------------------------------------------------
//                          public methods

int ExportSizeDia::width() const
{
    return m_widthEdit->value();
}


int ExportSizeDia::height() const
{
    return m_heightEdit->value();
}


// ----------------------------------------------------------------
//                            slots


void ExportSizeDia::widthChanged( int width )
{
    disconnectAll();
    width = TQMIN( width, m_realWidth * 10 );
    width = TQMAX( width, m_realWidth / 10 );
    double percent = (100.0 * static_cast<double>( width ) 
		      / static_cast<double>( m_realWidth ));
    m_percWidthEdit->setValue(  percent  );
    if ( m_proportional->isChecked() ) {
        m_percHeightEdit->setValue( percent );
        int height = static_cast<int>( m_realHeight * percent / 100.0 );
        m_heightEdit->setValue(  height );
    }
    connectAll();
}


void ExportSizeDia::heightChanged( int height )
{
    disconnectAll();
    height = TQMIN( height, m_realHeight * 10 );
    height = TQMAX( height, m_realHeight / 10 );
    double percent = (100.0 * static_cast<double>( height )
		      / static_cast<double>( m_realHeight ));
    m_percHeightEdit->setValue( percent  );
    if ( m_proportional->isChecked() ) {
        m_percWidthEdit->setValue(  percent  );
        int width = static_cast<int>( m_realWidth * percent / 100.0 );
        m_widthEdit->setValue( width );
    }
    connectAll();
}


void ExportSizeDia::percentWidthChanged( double percent )
{
    disconnectAll();
    percent = TQMIN( percent, 1000 );
    percent = TQMAX( percent, 10 );
    int width = static_cast<int>( m_realWidth * percent / 100. );
    m_widthEdit->setValue(  width  );
    if ( m_proportional->isChecked() ) {
        int height = static_cast<int>( m_realHeight * percent / 100. );
        m_heightEdit->setValue(  height  );
        m_percHeightEdit->setValue(  percent );
    }
    connectAll();
}


void ExportSizeDia::percentHeightChanged( double percent )
{
    disconnectAll();
    percent = TQMIN( percent, 1000 );
    percent = TQMAX( percent, 10 );
    if ( m_proportional->isChecked() ) {
        int width = static_cast<int>( m_realWidth * percent / 100. );
        m_widthEdit->setValue(  width  );
        m_percWidthEdit->setValue(  percent  );
    }
    int height = static_cast<int>( m_realHeight * percent / 100. );
    m_heightEdit->setValue(  height  );
    connectAll();
}


void ExportSizeDia::proportionalClicked()
{
    if ( m_proportional->isChecked() ) {
        disconnectAll();
        int width = m_widthEdit->value( );
        width = TQMIN( width, m_realWidth * 10 );
        width = TQMAX( width, m_realWidth / 10 );
        double percent = (100.0 * static_cast<double>( width )
			  / static_cast<double>( m_realWidth ));
        m_percHeightEdit->setValue(  percent  );
        int height = static_cast<int>( m_realHeight * percent / 100. );
        m_heightEdit->setValue(  height  );
        connectAll();
    }
}


// ----------------------------------------------------------------
//                          private methods


void ExportSizeDia::connectAll()
{
    connect( m_widthEdit,      TQ_SIGNAL( valueChanged(int) ),
             this,             TQ_SLOT( widthChanged( int ) ) );
    connect( m_heightEdit,     TQ_SIGNAL( valueChanged(int) ),
             this,             TQ_SLOT( heightChanged( int ) ) );
    connect( m_percWidthEdit,  TQ_SIGNAL( valueChanged(double) ),
             this,             TQ_SLOT( percentWidthChanged( double ) ) );
    connect( m_percHeightEdit, TQ_SIGNAL( valueChanged(double) ),
             this,             TQ_SLOT( percentHeightChanged(double ) ) );
}


void ExportSizeDia::disconnectAll()
{
    disconnect( m_widthEdit,      TQ_SIGNAL( valueChanged(int) ),
		this,             TQ_SLOT( widthChanged( int ) ) );
    disconnect( m_heightEdit,     TQ_SIGNAL( valueChanged(int) ),
		this,             TQ_SLOT( heightChanged( int ) ) );
    disconnect( m_percWidthEdit,  TQ_SIGNAL( valueChanged(double) ),
		this,             TQ_SLOT( percentWidthChanged( double ) ) );
    disconnect( m_percHeightEdit, TQ_SIGNAL( valueChanged(double) ),
		this,             TQ_SLOT( percentHeightChanged(double ) ) );
}


#if 0
void ExportSizeDia::slotOk()
{
    hide();
    //doc->setZoomAndResolution( 100, 600, 600 );
    //doc->setZoomAndResolution( 1000, TQPaintDevice::x11AppDpiX(), TQPaintDevice::x11AppDpiY() );
    //doc->newZoomAndResolution( false, false );
    int width = widthEdit->value();
    int height = heightEdit->value();
//     kdDebug( KFormula::DEBUGID ) << k_funcinfo
//                                  << "(" << width << " " << height << ")"
//                                  << endl;
//     width = realWidth;
//     height = realHeight;
    TQImage image = formula->drawImage( width, height );
    if ( !image.save( _fileOut, "PNG" ) ) {
        KMessageBox::error( 0, i18n( "Failed to write file." ), i18n( "PNG Export Error" ) );
    }
    reject();
}
#endif

#include "exportsizedia.moc"