kspread

kspread_generalProperty.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
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             // this has to be done as the rect cahnges width/hight if left or top is changed
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     // get the values to the actual rect
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     // this is done due to the rounding so we can detect a change
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"
KDE Home | KDE Accessibility Home | Description of Access Keys