kspread

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