KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
7.4KB

  1. // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
  2. /* This file is part of the KDE project
  3. Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
  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. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. #include "kspread_generalProperty.h"
  18. #include <tqcheckbox.h>
  19. #include <tqgroupbox.h>
  20. #include <tqlabel.h>
  21. #include <tqlayout.h>
  22. #include <tqlineedit.h>
  23. #include <knuminput.h>
  24. #include <tdelocale.h>
  25. #include <KoGeneralPropertyUi.h>
  26. #include <KoUnitWidgets.h>
  27. using namespace KSpread;
  28. GeneralProperty::GeneralProperty( TQWidget *parent, const char *name, GeneralValue &generalValue, KoUnit::Unit unit )
  29. : TQWidget( parent, name )
  30. , m_ratio( 1.0 )
  31. , m_generalValue( generalValue )
  32. , m_unit( unit )
  33. {
  34. TQVBoxLayout *layout = new TQVBoxLayout( this );
  35. layout->addWidget( m_ui = new KoGeneralPropertyUI( this ) );
  36. TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
  37. layout->addItem( spacer );
  38. if ( m_generalValue.m_name.isNull() )
  39. {
  40. m_ui->nameLabel->setEnabled( false );
  41. m_ui->nameInput->setEnabled( false );
  42. }
  43. else
  44. {
  45. m_ui->nameInput->setText( m_generalValue.m_name );
  46. }
  47. m_ui->positionGroup->setTitle( i18n( "Position" ) );
  48. connect( m_ui->protect, TQT_SIGNAL( toggled( bool ) ), this, TQT_SLOT( slotProtectToggled( bool ) ) );
  49. connect( m_ui->keepRatio, TQT_SIGNAL( toggled( bool ) ), this, TQT_SLOT( slotKeepRatioToggled( bool ) ) );
  50. double dStep = KoUnit::fromUserValue( 0.5, m_unit );
  51. double dMax = KoUnit::fromUserValue( 9999, m_unit );
  52. m_ui->xInput->setUnit( m_unit );
  53. m_ui->xInput->setMinMaxStep( 0, dMax, dStep );
  54. m_ui->yInput->setUnit( m_unit );
  55. m_ui->yInput->setMinMaxStep( 0, dMax, dStep );
  56. m_ui->widthInput->setUnit( m_unit );
  57. m_ui->widthInput->setMinMaxStep( 0, dMax, dStep );
  58. connect( m_ui->widthInput, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( slotWidthChanged( double ) ) );
  59. m_ui->heightInput->setUnit( m_unit );
  60. m_ui->heightInput->setMinMaxStep( 0, dMax, dStep );
  61. connect( m_ui->heightInput, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( slotHeightChanged( double ) ) );
  62. slotReset();
  63. }
  64. GeneralProperty::~GeneralProperty()
  65. {
  66. }
  67. int GeneralProperty::getGeneralPropertyChange() const
  68. {
  69. int flags = 0;
  70. if ( !m_generalValue.m_name.isNull() && m_generalValue.m_name != m_ui->nameInput->text() )
  71. flags |= Name;
  72. if ( m_ui->protect->state() != TQButton::NoChange )
  73. {
  74. if ( ( m_ui->protect->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_protect )
  75. flags |= Protect;
  76. if ( !m_ui->protect->isOn() )
  77. {
  78. KoRect rect = getRect();
  79. if ( m_generalValue.m_rect.left() != rect.left() )
  80. flags |= Left;
  81. if ( m_generalValue.m_rect.top() != rect.top() )
  82. flags |= Top;
  83. // this has to be done as the rect cahnges width/hight if left or top is changed
  84. if ( TQABS( m_generalValue.m_rect.width() - rect.width() ) > 1e-6 )
  85. flags |= Width;
  86. if ( TQABS( m_generalValue.m_rect.height() - rect.height() ) > 1e-6 )
  87. flags |= Height;
  88. }
  89. }
  90. if ( m_ui->keepRatio->state() != TQButton::NoChange
  91. && ( m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_keepRatio )
  92. {
  93. flags |= KeepRatio;
  94. }
  95. return flags;
  96. }
  97. GeneralProperty::GeneralValue GeneralProperty::getGeneralValue() const
  98. {
  99. GeneralValue generalValue;
  100. generalValue.m_name = m_ui->nameInput->isEnabled() ? m_ui->nameInput->text() : TQString();
  101. generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
  102. generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
  103. generalValue.m_rect = getRect();
  104. return generalValue;
  105. }
  106. void GeneralProperty::apply()
  107. {
  108. int flags = getGeneralPropertyChange();
  109. if ( flags & Name )
  110. m_generalValue.m_name = m_ui->nameInput->text();
  111. if ( flags & Protect )
  112. m_generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
  113. if ( flags & KeepRatio )
  114. m_generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
  115. // get the values to the actual rect
  116. m_generalValue.m_rect = getRect();
  117. }
  118. KoRect GeneralProperty::getRect() const
  119. {
  120. double x = TQMAX( 0, m_ui->xInput->value() );
  121. double y = TQMAX( 0, m_ui->yInput->value() );
  122. double w = TQMAX( 0, m_ui->widthInput->value() );
  123. double h = TQMAX( 0, m_ui->heightInput->value() );
  124. KoRect rect( x, y, w, h );
  125. return rect;
  126. }
  127. void GeneralProperty::setRect( KoRect &rect )
  128. {
  129. m_ui->xInput->changeValue( TQMAX( 0.00, rect.left() ) );
  130. m_ui->yInput->changeValue( TQMAX( 0.00, rect.top() ) );
  131. m_ui->widthInput->changeValue( TQMAX( 0.00, rect.width() ) );
  132. m_ui->heightInput->changeValue( TQMAX( 0.00, rect.height() ) );
  133. }
  134. void GeneralProperty::slotReset()
  135. {
  136. switch ( m_generalValue.m_protect )
  137. {
  138. case STATE_ON:
  139. m_ui->protect->setChecked( true );
  140. break;
  141. case STATE_OFF:
  142. m_ui->protect->setChecked( false );
  143. break;
  144. case STATE_UNDEF:
  145. m_ui->protect->setTristate( true );
  146. m_ui->protect->setNoChange();
  147. break;
  148. default:
  149. m_ui->protect->setChecked( false );
  150. break;
  151. }
  152. switch ( m_generalValue.m_keepRatio )
  153. {
  154. case STATE_ON:
  155. m_ui->keepRatio->setChecked( true );
  156. break;
  157. case STATE_OFF:
  158. m_ui->keepRatio->setChecked( false );
  159. break;
  160. case STATE_UNDEF:
  161. m_ui->keepRatio->setTristate( true );
  162. m_ui->keepRatio->setNoChange();
  163. break;
  164. default:
  165. m_ui->keepRatio->setChecked( false );
  166. break;
  167. }
  168. setRect( m_generalValue.m_rect );
  169. // this is done due to the rounding so we can detect a change
  170. m_generalValue.m_rect = getRect();
  171. }
  172. void GeneralProperty::slotProtectToggled( bool state )
  173. {
  174. m_ui->positionGroup->setEnabled( !state );
  175. }
  176. void GeneralProperty::slotKeepRatioToggled( bool state )
  177. {
  178. if ( state )
  179. {
  180. if ( m_ui->widthInput->value() == 0 )
  181. {
  182. m_ratio = 1.0;
  183. }
  184. else
  185. {
  186. m_ratio = m_ui->heightInput->value() / m_ui->widthInput->value();
  187. }
  188. }
  189. }
  190. void GeneralProperty::slotWidthChanged( double value )
  191. {
  192. if ( m_ui->keepRatio->isChecked() )
  193. {
  194. m_ui->heightInput->setValue( value * m_ratio );
  195. }
  196. }
  197. void GeneralProperty::slotHeightChanged( double value )
  198. {
  199. if ( m_ui->keepRatio->isChecked() && m_ratio != 0 )
  200. {
  201. m_ui->widthInput->setValue( value / m_ratio );
  202. }
  203. }
  204. #include "kspread_generalProperty.moc"