kpresenter

KPrGeneralProperty.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
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#include "KPrGeneralProperty.h"
20
21#include <tqcheckbox.h>
22#include <tqgroupbox.h>
23#include <tqlabel.h>
24#include <tqlayout.h>
25#include <tqlineedit.h>
26
27#include <knuminput.h>
28#include <tdelocale.h>
29#include <KoUnitWidgets.h>
30
31#include <KoGeneralPropertyUi.h>
32
33KPrGeneralProperty::KPrGeneralProperty( TQWidget *parent, const char *name, GeneralValue &generalValue, KoUnit::Unit unit )
34: TQWidget( parent, name )
35, m_ratio( 1.0 )
36, m_generalValue( generalValue )
37, m_unit( unit )
38{
39 TQVBoxLayout *layout = new TQVBoxLayout( this );
40 layout->addWidget( m_ui = new KoGeneralPropertyUI( this ) );
41
42 TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
43 layout->addItem( spacer );
44
45 if ( m_generalValue.m_name.isNull() )
46 {
47 m_ui->nameLabel->setEnabled( false );
48 m_ui->nameInput->setEnabled( false );
49 }
50 else
51 {
52 m_ui->nameInput->setText( m_generalValue.m_name );
53 }
54
55 m_ui->positionGroup->setTitle( i18n( "Position" ) );
56
57 connect( m_ui->protect, TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( slotProtectToggled( bool ) ) );
58 connect( m_ui->keepRatio, TQ_SIGNAL( toggled( bool ) ), this, TQ_SLOT( slotKeepRatioToggled( bool ) ) );
59
60 double dStep = KoUnit::fromUserValue( 0.5, m_unit );
61 double dMax = KoUnit::fromUserValue( 9999, m_unit );
62 m_ui->xInput->setUnit( m_unit );
63 m_ui->xInput->setMinMaxStep( 0, dMax, dStep );
64
65 m_ui->yInput->setUnit( m_unit );
66 m_ui->yInput->setMinMaxStep( 0, dMax, dStep );
67
68 m_ui->widthInput->setUnit( m_unit );
69 m_ui->widthInput->setMinMaxStep( 0, dMax, dStep );
70 connect( m_ui->widthInput, TQ_SIGNAL( valueChanged( double ) ), this, TQ_SLOT( slotWidthChanged( double ) ) );
71
72 m_ui->heightInput->setUnit( m_unit );
73 m_ui->heightInput->setMinMaxStep( 0, dMax, dStep );
74 connect( m_ui->heightInput, TQ_SIGNAL( valueChanged( double ) ), this, TQ_SLOT( slotHeightChanged( double ) ) );
75 slotReset();
76}
77
78
79KPrGeneralProperty::~KPrGeneralProperty()
80{
81}
82
83
84int KPrGeneralProperty::getGeneralPropertyChange() const
85{
86 int flags = 0;
87
88 if ( !m_generalValue.m_name.isNull() && m_generalValue.m_name != m_ui->nameInput->text() )
89 flags |= Name;
90
91 if ( m_ui->protect->state() != TQButton::NoChange )
92 {
93 if ( ( m_ui->protect->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_protect )
94 flags |= Protect;
95
96 if ( !m_ui->protect->isOn() )
97 {
98 KoRect rect = getRect();
99 if ( m_generalValue.m_rect.left() != rect.left() )
100 flags |= Left;
101 if ( m_generalValue.m_rect.top() != rect.top() )
102 flags |= Top;
103 // this has to be done as the rect cahnges width/hight if left or top is changed
104 if ( TQABS( m_generalValue.m_rect.width() - rect.width() ) > 1e-6 )
105 flags |= Width;
106 if ( TQABS( m_generalValue.m_rect.height() - rect.height() ) > 1e-6 )
107 flags |= Height;
108 }
109 }
110
111 if ( m_ui->keepRatio->state() != TQButton::NoChange
112 && ( m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_keepRatio )
113 {
114 flags |= KeepRatio;
115 }
116
117 return flags;
118}
119
120
121KPrGeneralProperty::GeneralValue KPrGeneralProperty::getGeneralValue() const
122{
123 GeneralValue generalValue;
124 generalValue.m_name = m_ui->nameInput->isEnabled() ? m_ui->nameInput->text() : TQString();
125 generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
126 generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
127 generalValue.m_rect = getRect();
128 return generalValue;
129}
130
131
132void KPrGeneralProperty::apply()
133{
134 int flags = getGeneralPropertyChange();
135
136 if ( flags & Name )
137 m_generalValue.m_name = m_ui->nameInput->text();
138
139 if ( flags & Protect )
140 m_generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
141
142 if ( flags & KeepRatio )
143 m_generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
144
145 // get the values to the actual rect
146 m_generalValue.m_rect = getRect();
147}
148
149
150KoRect KPrGeneralProperty::getRect() const
151{
152 double x = TQMAX( 0, m_ui->xInput->value() );
153 double y = TQMAX( 0, m_ui->yInput->value() );
154 double w = TQMAX( 0, m_ui->widthInput->value() );
155 double h = TQMAX( 0, m_ui->heightInput->value() );
156
157 KoRect rect( x, y, w, h );
158 return rect;
159}
160
161
162void KPrGeneralProperty::setRect( KoRect &rect )
163{
164 m_ui->xInput->changeValue( TQMAX( 0.00, rect.left() ) );
165 m_ui->yInput->changeValue( TQMAX( 0.00, rect.top() ) );
166 m_ui->widthInput->changeValue( TQMAX( 0.00, rect.width() ) );
167 m_ui->heightInput->changeValue( TQMAX( 0.00, rect.height() ) );
168}
169
170
171void KPrGeneralProperty::slotReset()
172{
173 switch ( m_generalValue.m_protect )
174 {
175 case STATE_ON:
176 m_ui->protect->setChecked( true );
177 break;
178 case STATE_OFF:
179 m_ui->protect->setChecked( false );
180 break;
181 case STATE_UNDEF:
182 m_ui->protect->setTristate( true );
183 m_ui->protect->setNoChange();
184 break;
185 default:
186 m_ui->protect->setChecked( false );
187 break;
188 }
189
190 switch ( m_generalValue.m_keepRatio )
191 {
192 case STATE_ON:
193 m_ui->keepRatio->setChecked( true );
194 break;
195 case STATE_OFF:
196 m_ui->keepRatio->setChecked( false );
197 break;
198 case STATE_UNDEF:
199 m_ui->keepRatio->setTristate( true );
200 m_ui->keepRatio->setNoChange();
201 break;
202 default:
203 m_ui->keepRatio->setChecked( false );
204 break;
205 }
206
207 setRect( m_generalValue.m_rect );
208 // this is done due to the rounding so we can detect a change
209 m_generalValue.m_rect = getRect();
210}
211
212
213void KPrGeneralProperty::slotProtectToggled( bool state )
214{
215 m_ui->positionGroup->setEnabled( !state );
216}
217
218
219void KPrGeneralProperty::slotKeepRatioToggled( bool state )
220{
221 if ( state )
222 {
223 if ( m_ui->widthInput->value() == 0 )
224 {
225 m_ratio = 1.0;
226 }
227 else
228 {
229 m_ratio = m_ui->heightInput->value() / m_ui->widthInput->value();
230 }
231 }
232}
233
234
235void KPrGeneralProperty::slotWidthChanged( double value )
236{
237 if ( m_ui->keepRatio->isChecked() )
238 {
239 m_ui->heightInput->setValue( value * m_ratio );
240 }
241}
242
243
244void KPrGeneralProperty::slotHeightChanged( double value )
245{
246 if ( m_ui->keepRatio->isChecked() && m_ratio != 0 )
247 {
248 m_ui->widthInput->setValue( value / m_ratio );
249 }
250}
251
252
253#include "KPrGeneralProperty.moc"