KoPageLayoutSize.cpp
1/* This file is part of the KDE project
2 * Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
3 * Copyright (C) 2005 Thomas Zander <zander@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; version 2.
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 <KoPageLayoutDia.h>
21#include <KoPageLayoutSize.h>
22#include <KoUnit.h>
23#include <KoUnitWidgets.h>
24
25#include <tdelocale.h>
26#include <kiconloader.h>
27#include <tdemessagebox.h>
28#include <kdebug.h>
29
30#include <tqlabel.h>
31#include <tqlayout.h>
32#include <tqradiobutton.h>
33#include <tqhbox.h>
34#include <tqvgroupbox.h>
35#include <tqhbuttongroup.h>
36
37KoPageLayoutSize::KoPageLayoutSize(TQWidget *parent, const KoPageLayout& layout, KoUnit::Unit unit,const KoColumns& columns, bool unitChooser, bool enableBorders)
38 : TQWidget(parent), m_blockSignals(false) {
39 m_layout = layout;
40 m_unit = unit;
41
42 TQGridLayout *grid1 = new TQGridLayout( this, 5, 2, 0, KDialog::spacingHint() );
43 if ( unitChooser ) {
44 // ------------- unit _______________
45 TQWidget* unitFrame = new TQWidget( this );
46 grid1->addWidget( unitFrame, 0, 0, TQt::AlignLeft );
47 TQBoxLayout* unitLayout = new TQHBoxLayout( unitFrame, 0, KDialog::spacingHint() );
48
49 // label unit
50 TQLabel *lpgUnit = new TQLabel( i18n( "Unit:" ), unitFrame );
51 unitLayout->addWidget( lpgUnit, 0, TQt::AlignRight | TQt::AlignVCenter );
52
53 // combo unit
54 TQComboBox *cpgUnit = new TQComboBox( false, unitFrame, "cpgUnit" );
55 lpgUnit->setBuddy( cpgUnit );
56 cpgUnit->insertStringList( KoUnit::listOfUnitName() );
57 cpgUnit->setCurrentItem( unit );
58 unitLayout->addWidget( cpgUnit, 0, TQt::AlignLeft | TQt::AlignVCenter );
59 connect( cpgUnit, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( setUnitInt( int ) ) );
60 }
61 else {
62 TQString str=KoUnit::unitDescription(unit);
63
64 TQLabel *lpgUnit = new TQLabel( i18n("All values are given in %1.").arg(str), this );
65 grid1->addWidget( lpgUnit, 0, 0, TQt::AlignLeft );
66 }
67
68 // -------------- page size -----------------
69 TQVGroupBox *formatFrame = new TQVGroupBox( i18n( "Page Size" ), this );
70 grid1->addWidget( formatFrame, 1, 0 );
71
72 TQHBox *formatPageSize = new TQHBox( formatFrame );
73 formatPageSize->setSpacing( KDialog::spacingHint() );
74
75 // label page size
76 TQLabel *lpgFormat = new TQLabel( i18n( "&Size:" ), formatPageSize );
77
78 // combo size
79 cpgFormat = new TQComboBox( false, formatPageSize, "cpgFormat" );
80 cpgFormat->insertStringList( KoPageFormat::allFormats() );
81 lpgFormat->setBuddy( cpgFormat );
82 connect( cpgFormat, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( formatChanged( int ) ) );
83
84 // spacer
85 formatPageSize->setStretchFactor( new TQWidget( formatPageSize ), 10 );
86
87 TQHBox *formatCustomSize = new TQHBox( formatFrame );
88 formatCustomSize->setSpacing( KDialog::spacingHint() );
89
90 // label width
91 TQLabel *lpgWidth = new TQLabel( i18n( "&Width:" ), formatCustomSize );
92
93 // linedit width
94 epgWidth = new KoUnitDoubleSpinBox( formatCustomSize, "Width" );
95 lpgWidth->setBuddy( epgWidth );
96 if ( m_layout.format != PG_CUSTOM )
97 epgWidth->setEnabled( false );
98 connect( epgWidth, TQ_SIGNAL( valueChangedPt(double) ), this, TQ_SLOT( widthChanged(double) ) );
99
100 // label height
101 TQLabel *lpgHeight = new TQLabel( i18n( "&Height:" ), formatCustomSize );
102
103 // linedit height
104 epgHeight = new KoUnitDoubleSpinBox( formatCustomSize, "Height" );
105 lpgHeight->setBuddy( epgHeight );
106 if ( m_layout.format != PG_CUSTOM )
107 epgHeight->setEnabled( false );
108 connect( epgHeight, TQ_SIGNAL( valueChangedPt(double ) ), this, TQ_SLOT( heightChanged(double) ) );
109
110 // --------------- orientation ---------------
111 m_orientGroup = new TQHButtonGroup( i18n( "Orientation" ), this );
112 m_orientGroup->setInsideSpacing( KDialog::spacingHint() );
113 grid1->addWidget( m_orientGroup, 2, 0 );
114
115 TQLabel* lbPortrait = new TQLabel( m_orientGroup );
116 lbPortrait->setPixmap( TQPixmap( UserIcon( "koPortrait" ) ) );
117 lbPortrait->setMaximumWidth( lbPortrait->pixmap()->width() );
118 new TQRadioButton( i18n("&Portrait"), m_orientGroup );
119
120 TQLabel* lbLandscape = new TQLabel( m_orientGroup );
121 lbLandscape->setPixmap( TQPixmap( UserIcon( "koLandscape" ) ) );
122 lbLandscape->setMaximumWidth( lbLandscape->pixmap()->width() );
123 new TQRadioButton( i18n("La&ndscape"), m_orientGroup );
124
125 connect( m_orientGroup, TQ_SIGNAL (clicked (int)), this, TQ_SLOT( orientationChanged(int) ));
126
127 // --------------- page margins ---------------
128 TQVGroupBox *marginsFrame = new TQVGroupBox( i18n( "Margins" ), this );
129 marginsFrame->setColumnLayout( 0, TQt::Vertical );
130 marginsFrame->setMargin( KDialog::marginHint() );
131 grid1->addWidget( marginsFrame, 3, 0 );
132
133 TQGridLayout *marginsLayout = new TQGridLayout( marginsFrame->layout(), 3, 3,
135
136 // left margin
137 ebrLeft = new KoUnitDoubleSpinBox( marginsFrame, "Left" );
138 marginsLayout->addWidget( ebrLeft, 1, 0 );
139 connect( ebrLeft, TQ_SIGNAL( valueChangedPt( double ) ), this, TQ_SLOT( leftChanged( double ) ) );
140
141 // right margin
142 ebrRight = new KoUnitDoubleSpinBox( marginsFrame, "Right" );
143 marginsLayout->addWidget( ebrRight, 1, 2 );
144 connect( ebrRight, TQ_SIGNAL( valueChangedPt( double ) ), this, TQ_SLOT( rightChanged( double ) ) );
145
146 // top margin
147 ebrTop = new KoUnitDoubleSpinBox( marginsFrame, "Top" );
148 marginsLayout->addWidget( ebrTop, 0, 1 , TQt::AlignCenter );
149 connect( ebrTop, TQ_SIGNAL( valueChangedPt( double ) ), this, TQ_SLOT( topChanged( double ) ) );
150
151 // bottom margin
152 ebrBottom = new KoUnitDoubleSpinBox( marginsFrame, "Bottom" );
153 marginsLayout->addWidget( ebrBottom, 2, 1, TQt::AlignCenter );
154 connect( ebrBottom, TQ_SIGNAL( valueChangedPt( double ) ), this, TQ_SLOT( bottomChanged( double ) ) );
155
156 // ------------- preview -----------
157 pgPreview = new KoPagePreview( this, "Preview", m_layout );
158 grid1->addMultiCellWidget( pgPreview, 1, 3, 1, 1 );
159
160 // ------------- spacers -----------
161 TQWidget* spacer1 = new TQWidget( this );
162 TQWidget* spacer2 = new TQWidget( this );
163 spacer1->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding,
164 TQSizePolicy::Expanding ) );
165 spacer2->setSizePolicy( TQSizePolicy( TQSizePolicy::Expanding,
166 TQSizePolicy::Expanding ) );
167 grid1->addWidget( spacer1, 4, 0 );
168 grid1->addWidget( spacer2, 4, 1 );
169
170 setValues();
171 updatePreview();
172 pgPreview->setPageColumns( columns );
173 setEnableBorders(enableBorders);
174}
175
177 m_haveBorders = on;
178 ebrLeft->setEnabled( on );
179 ebrRight->setEnabled( on );
180 ebrTop->setEnabled( on );
181 ebrBottom->setEnabled( on );
182
183 // update m_layout
184 m_layout.ptLeft = on?ebrLeft->value():0;
185 m_layout.ptRight = on?ebrRight->value():0;
186 m_layout.ptTop = on?ebrTop->value():0;
187 m_layout.ptBottom = on?ebrBottom->value():0;
188
189 // use updated m_layout
190 updatePreview();
191 emit propertyChange(m_layout);
192}
193
194void KoPageLayoutSize::updatePreview() {
195 pgPreview->setPageLayout( m_layout );
196}
197
198void KoPageLayoutSize::setValues() {
199 // page format
200 cpgFormat->setCurrentItem( m_layout.format );
201 // orientation
202 m_orientGroup->setButton( m_layout.orientation == PG_PORTRAIT ? 0: 1 );
203
204 setUnit( m_unit );
205 updatePreview();
206}
207
209 m_unit = unit;
210 m_blockSignals = true; // due to non-atomic changes the propertyChange emits should be blocked
211
212 epgWidth->setUnit( m_unit );
213 epgWidth->setMinMaxStep( 0, KoUnit::fromUserValue( 9999, m_unit ), KoUnit::fromUserValue( 0.01, m_unit ) );
214 epgWidth->changeValue( m_layout.ptWidth );
215
216 epgHeight->setUnit( m_unit );
217 epgHeight->setMinMaxStep( 0, KoUnit::fromUserValue( 9999, m_unit ), KoUnit::fromUserValue( 0.01, m_unit ) );
218 epgHeight->changeValue( m_layout.ptHeight );
219
220 double dStep = KoUnit::fromUserValue( 0.2, m_unit );
221
222 ebrLeft->setUnit( m_unit );
223 ebrLeft->changeValue( m_layout.ptLeft );
224 ebrLeft->setMinMaxStep( 0, m_layout.ptWidth, dStep );
225
226 ebrRight->setUnit( m_unit );
227 ebrRight->changeValue( m_layout.ptRight );
228 ebrRight->setMinMaxStep( 0, m_layout.ptWidth, dStep );
229
230 ebrTop->setUnit( m_unit );
231 ebrTop->changeValue( m_layout.ptTop );
232 ebrTop->setMinMaxStep( 0, m_layout.ptHeight, dStep );
233
234 ebrBottom->setUnit( m_unit );
235 ebrBottom->changeValue( m_layout.ptBottom );
236 ebrBottom->setMinMaxStep( 0, m_layout.ptHeight, dStep );
237
238 m_blockSignals = false;
239}
240
241void KoPageLayoutSize::setUnitInt( int unit ) {
242 setUnit((KoUnit::Unit)unit);
243}
244
245void KoPageLayoutSize::formatChanged( int format ) {
246 if ( ( KoFormat )format == m_layout.format )
247 return;
248 m_layout.format = ( KoFormat )format;
249 bool enable = (KoFormat) format == PG_CUSTOM;
250 epgWidth->setEnabled( enable );
251 epgHeight->setEnabled( enable );
252
253 if ( m_layout.format != PG_CUSTOM ) {
254 m_layout.ptWidth = MM_TO_POINT( KoPageFormat::width(
255 m_layout.format, m_layout.orientation ) );
256 m_layout.ptHeight = MM_TO_POINT( KoPageFormat::height(
257 m_layout.format, m_layout.orientation ) );
258 }
259
260 epgWidth->changeValue( m_layout.ptWidth );
261 epgHeight->changeValue( m_layout.ptHeight );
262
263 updatePreview( );
264 emit propertyChange(m_layout);
265}
266
267void KoPageLayoutSize::orientationChanged(int which) {
268 m_layout.orientation = which == 0 ? PG_PORTRAIT : PG_LANDSCAPE;
269
270 // swap dimension
271 double val = epgWidth->value();
272 epgWidth->changeValue(epgHeight->value());
273 epgHeight->changeValue(val);
274 // and adjust margins
275 m_blockSignals = true;
276 val = ebrTop->value();
277 if(m_layout.orientation == PG_PORTRAIT) { // clockwise
278 ebrTop->changeValue(ebrRight->value());
279 ebrRight->changeValue(ebrBottom->value());
280 ebrBottom->changeValue(ebrLeft->value());
281 ebrLeft->changeValue(val);
282 } else { // counter clockwise
283 ebrTop->changeValue(ebrLeft->value());
284 ebrLeft->changeValue(ebrBottom->value());
285 ebrBottom->changeValue(ebrRight->value());
286 ebrRight->changeValue(val);
287 }
288 m_blockSignals = false;
289
290 setEnableBorders(m_haveBorders); // will update preview+emit
291}
292
293void KoPageLayoutSize::widthChanged(double width) {
294 if(m_blockSignals) return;
295 m_layout.ptWidth = width;
296 updatePreview();
297 emit propertyChange(m_layout);
298}
299void KoPageLayoutSize::heightChanged(double height) {
300 if(m_blockSignals) return;
301 m_layout.ptHeight = height;
302 updatePreview( );
303 emit propertyChange(m_layout);
304}
305void KoPageLayoutSize::leftChanged( double left ) {
306 if(m_blockSignals) return;
307 m_layout.ptLeft = left;
308 updatePreview();
309 emit propertyChange(m_layout);
310}
311void KoPageLayoutSize::rightChanged(double right) {
312 if(m_blockSignals) return;
313 m_layout.ptRight = right;
314 updatePreview();
315 emit propertyChange(m_layout);
316}
317void KoPageLayoutSize::topChanged(double top) {
318 if(m_blockSignals) return;
319 m_layout.ptTop = top;
320 updatePreview();
321 emit propertyChange(m_layout);
322}
323void KoPageLayoutSize::bottomChanged(double bottom) {
324 if(m_blockSignals) return;
325 m_layout.ptBottom = bottom;
326 updatePreview();
327 emit propertyChange(m_layout);
328}
329
331 if ( m_layout.ptLeft + m_layout.ptRight > m_layout.ptWidth ) {
332 KMessageBox::error( this,
333 i18n("The page width is smaller than the left and right margins."),
334 i18n("Page Layout Problem") );
335 return false;
336 }
337 if ( m_layout.ptTop + m_layout.ptBottom > m_layout.ptHeight ) {
338 KMessageBox::error( this,
339 i18n("The page height is smaller than the top and bottom margins."),
340 i18n("Page Layout Problem") );
341 return false;
342 }
343 return true;
344}
345
347 pgPreview->setPageColumns(columns);
348}
349
350#include <KoPageLayoutSize.moc>
static int marginHint()
static int spacingHint()
static void error(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
void setEnableBorders(bool on)
Enable the user to edit the page border size.
void setColumns(KoColumns &columns)
Update the page preview widget with the param columns.
KoPageLayoutSize(TQWidget *parent, const KoPageLayout &layout, KoUnit::Unit unit, const KoColumns &columns, bool unitChooser, bool enableBorders)
Contructor.
void setUnit(KoUnit::Unit unit)
Set a new unit for the widget updating the widgets.
void propertyChange(KoPageLayout &layout)
Emitted whenever the user changed something in the dialog.
KoPagePreview.
void setPageLayout(const KoPageLayout &)
set page layout
Spin box for double precision numbers with unit display.
void setMinMaxStep(double min, double max, double step)
Set minimum, maximum value and the step size (all in points) (by Tymoteusz Majewski,...
double value(void) const
static double fromUserValue(double value, Unit unit)
This method is the one to use to read a value from a dialog.
Definition KoUnit.cpp:120
static TQString unitDescription(Unit _unit)
Get the full (translated) description of a unit.
Definition KoUnit.cpp:43
Unit
Length units supported by KOffice.
Definition KoUnit.h:63
structure for columns
This structure defines the page layout, including its size in pt, its format (e.g.
KoFormat format
Page format.
double ptRight
Right margin in pt.
double ptHeight
Page height in pt.
KoOrientation orientation
Page orientation.
double ptLeft
Left margin in pt.
double ptBottom
Bottom margin in pt.
double ptWidth
Page width in pt.
double ptTop
Top margin in pt.