kpresenter

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