00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kspread_generalProperty.h"
00021
00022 #include <qcheckbox.h>
00023 #include <qgroupbox.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qlineedit.h>
00027
00028 #include <knuminput.h>
00029 #include <klocale.h>
00030 #include <KoGeneralPropertyUi.h>
00031 #include <KoUnitWidgets.h>
00032
00033 using namespace KSpread;
00034
00035 GeneralProperty::GeneralProperty( QWidget *parent, const char *name, GeneralValue &generalValue, KoUnit::Unit unit )
00036 : QWidget( parent, name )
00037 , m_ratio( 1.0 )
00038 , m_generalValue( generalValue )
00039 , m_unit( unit )
00040 {
00041 QVBoxLayout *layout = new QVBoxLayout( this );
00042 layout->addWidget( m_ui = new KoGeneralPropertyUI( this ) );
00043
00044 QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
00045 layout->addItem( spacer );
00046
00047 if ( m_generalValue.m_name.isNull() )
00048 {
00049 m_ui->nameLabel->setEnabled( false );
00050 m_ui->nameInput->setEnabled( false );
00051 }
00052 else
00053 {
00054 m_ui->nameInput->setText( m_generalValue.m_name );
00055 }
00056
00057 m_ui->positionGroup->setTitle( i18n( "Position" ) );
00058
00059 connect( m_ui->protect, SIGNAL( toggled( bool ) ), this, SLOT( slotProtectToggled( bool ) ) );
00060 connect( m_ui->keepRatio, SIGNAL( toggled( bool ) ), this, SLOT( slotKeepRatioToggled( bool ) ) );
00061
00062 double dStep = KoUnit::fromUserValue( 0.5, m_unit );
00063 double dMax = KoUnit::fromUserValue( 9999, m_unit );
00064 m_ui->xInput->setUnit( m_unit );
00065 m_ui->xInput->setMinMaxStep( 0, dMax, dStep );
00066
00067 m_ui->yInput->setUnit( m_unit );
00068 m_ui->yInput->setMinMaxStep( 0, dMax, dStep );
00069
00070 m_ui->widthInput->setUnit( m_unit );
00071 m_ui->widthInput->setMinMaxStep( 0, dMax, dStep );
00072 connect( m_ui->widthInput, SIGNAL( valueChanged( double ) ), this, SLOT( slotWidthChanged( double ) ) );
00073
00074 m_ui->heightInput->setUnit( m_unit );
00075 m_ui->heightInput->setMinMaxStep( 0, dMax, dStep );
00076 connect( m_ui->heightInput, SIGNAL( valueChanged( double ) ), this, SLOT( slotHeightChanged( double ) ) );
00077 slotReset();
00078 }
00079
00080
00081 GeneralProperty::~GeneralProperty()
00082 {
00083 }
00084
00085
00086 int GeneralProperty::getGeneralPropertyChange() const
00087 {
00088 int flags = 0;
00089
00090 if ( !m_generalValue.m_name.isNull() && m_generalValue.m_name != m_ui->nameInput->text() )
00091 flags |= Name;
00092
00093 if ( m_ui->protect->state() != QButton::NoChange )
00094 {
00095 if ( ( m_ui->protect->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_protect )
00096 flags |= Protect;
00097
00098 if ( !m_ui->protect->isOn() )
00099 {
00100 KoRect rect = getRect();
00101 if ( m_generalValue.m_rect.left() != rect.left() )
00102 flags |= Left;
00103 if ( m_generalValue.m_rect.top() != rect.top() )
00104 flags |= Top;
00105
00106 if ( QABS( m_generalValue.m_rect.width() - rect.width() ) > 1e-6 )
00107 flags |= Width;
00108 if ( QABS( m_generalValue.m_rect.height() - rect.height() ) > 1e-6 )
00109 flags |= Height;
00110 }
00111 }
00112
00113 if ( m_ui->keepRatio->state() != QButton::NoChange
00114 && ( m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_keepRatio )
00115 {
00116 flags |= KeepRatio;
00117 }
00118
00119 return flags;
00120 }
00121
00122
00123 GeneralProperty::GeneralValue GeneralProperty::getGeneralValue() const
00124 {
00125 GeneralValue generalValue;
00126 generalValue.m_name = m_ui->nameInput->isEnabled() ? m_ui->nameInput->text() : QString();
00127 generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
00128 generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
00129 generalValue.m_rect = getRect();
00130 return generalValue;
00131 }
00132
00133
00134 void GeneralProperty::apply()
00135 {
00136 int flags = getGeneralPropertyChange();
00137
00138 if ( flags & Name )
00139 m_generalValue.m_name = m_ui->nameInput->text();
00140
00141 if ( flags & Protect )
00142 m_generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
00143
00144 if ( flags & KeepRatio )
00145 m_generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
00146
00147
00148 m_generalValue.m_rect = getRect();
00149 }
00150
00151
00152 KoRect GeneralProperty::getRect() const
00153 {
00154 double x = QMAX( 0, m_ui->xInput->value() );
00155 double y = QMAX( 0, m_ui->yInput->value() );
00156 double w = QMAX( 0, m_ui->widthInput->value() );
00157 double h = QMAX( 0, m_ui->heightInput->value() );
00158
00159 KoRect rect( x, y, w, h );
00160 return rect;
00161 }
00162
00163
00164 void GeneralProperty::setRect( KoRect &rect )
00165 {
00166 m_ui->xInput->changeValue( QMAX( 0.00, rect.left() ) );
00167 m_ui->yInput->changeValue( QMAX( 0.00, rect.top() ) );
00168 m_ui->widthInput->changeValue( QMAX( 0.00, rect.width() ) );
00169 m_ui->heightInput->changeValue( QMAX( 0.00, rect.height() ) );
00170 }
00171
00172
00173 void GeneralProperty::slotReset()
00174 {
00175 switch ( m_generalValue.m_protect )
00176 {
00177 case STATE_ON:
00178 m_ui->protect->setChecked( true );
00179 break;
00180 case STATE_OFF:
00181 m_ui->protect->setChecked( false );
00182 break;
00183 case STATE_UNDEF:
00184 m_ui->protect->setTristate( true );
00185 m_ui->protect->setNoChange();
00186 break;
00187 default:
00188 m_ui->protect->setChecked( false );
00189 break;
00190 }
00191
00192 switch ( m_generalValue.m_keepRatio )
00193 {
00194 case STATE_ON:
00195 m_ui->keepRatio->setChecked( true );
00196 break;
00197 case STATE_OFF:
00198 m_ui->keepRatio->setChecked( false );
00199 break;
00200 case STATE_UNDEF:
00201 m_ui->keepRatio->setTristate( true );
00202 m_ui->keepRatio->setNoChange();
00203 break;
00204 default:
00205 m_ui->keepRatio->setChecked( false );
00206 break;
00207 }
00208
00209 setRect( m_generalValue.m_rect );
00210
00211 m_generalValue.m_rect = getRect();
00212 }
00213
00214
00215 void GeneralProperty::slotProtectToggled( bool state )
00216 {
00217 m_ui->positionGroup->setEnabled( !state );
00218 }
00219
00220
00221 void GeneralProperty::slotKeepRatioToggled( bool state )
00222 {
00223 if ( state )
00224 {
00225 if ( m_ui->widthInput->value() == 0 )
00226 {
00227 m_ratio = 1.0;
00228 }
00229 else
00230 {
00231 m_ratio = m_ui->heightInput->value() / m_ui->widthInput->value();
00232 }
00233 }
00234 }
00235
00236
00237 void GeneralProperty::slotWidthChanged( double value )
00238 {
00239 if ( m_ui->keepRatio->isChecked() )
00240 {
00241 m_ui->heightInput->setValue( value * m_ratio );
00242 }
00243 }
00244
00245
00246 void GeneralProperty::slotHeightChanged( double value )
00247 {
00248 if ( m_ui->keepRatio->isChecked() && m_ratio != 0 )
00249 {
00250 m_ui->widthInput->setValue( value / m_ratio );
00251 }
00252 }
00253
00254
00255 #include "kspread_generalProperty.moc"