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>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
20
#include "generalproperty.h"
22
#include <qcheckbox.h>
23
#include <qgroupbox.h>
26
#include <qlineedit.h>
28
#include <knuminput.h>
31
#include "generalpropertyui.h"
33
GeneralProperty::GeneralProperty( QWidget *parent, const char *name, GeneralValue &generalValue, KoUnit::Unit unit )
34
: QWidget( parent, name )
36
, m_generalValue( generalValue )
39
QVBoxLayout *layout = new QVBoxLayout( this );
40
layout->addWidget( m_ui = new GeneralPropertyUI( this ) );
42
QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
43
layout->addItem( spacer );
45
if ( m_generalValue.m_name.isNull() )
47
m_ui->nameLabel->setEnabled( false );
48
m_ui->nameInput->setEnabled( false );
52
m_ui->nameInput->setText( m_generalValue.m_name );
55
m_ui->positionGroup->setTitle( i18n( "Position (%1)" ).arg( KoUnit::unitName( m_unit ) ) );
56
m_ui->sizeGroup->setTitle( i18n( "Size (%1)" ).arg( KoUnit::unitName( m_unit ) ) );
58
connect( m_ui->protect, SIGNAL( toggled( bool ) ), this, SLOT( slotProtectToggled( bool ) ) );
59
connect( m_ui->keepRatio, SIGNAL( toggled( bool ) ), this, SLOT( slotKeepRatioToggled( bool ) ) );
61
m_ui->xInput->setRange( 0, 9999, 0.5, false );
62
m_ui->yInput->setRange( 0, 9999, 0.5, false );
63
m_ui->widthInput->setRange( 0, 9999, 0.5, false );
64
connect( m_ui->widthInput, SIGNAL( valueChanged( double ) ), this, SLOT( slotWidthChanged( double ) ) );
65
m_ui->heightInput->setRange( 0, 9999, 0.5, false );
66
connect( m_ui->heightInput, SIGNAL( valueChanged( double ) ), this, SLOT( slotHeightChanged( double ) ) );
72
GeneralProperty::~GeneralProperty()
77
int GeneralProperty::getGeneralPropertyChange() const
81
if ( !m_generalValue.m_name.isNull() && m_generalValue.m_name != m_ui->nameInput->text() )
84
if ( m_ui->protect->state() != QButton::NoChange )
86
if ( ( m_ui->protect->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_protect )
89
if ( !m_ui->protect->isOn() )
91
KoRect rect = getRect();
92
if ( m_generalValue.m_rect.left() != rect.left() )
94
if ( m_generalValue.m_rect.top() != rect.top() )
96
// this has to be done as the rect cahnges width/hight if left or top is changed
97
if ( QABS( m_generalValue.m_rect.width() - rect.width() ) > 1e-6 )
99
if ( QABS( m_generalValue.m_rect.height() - rect.height() ) > 1e-6 )
104
if ( m_ui->keepRatio->state() != QButton::NoChange
105
&& ( m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_keepRatio )
114
GeneralProperty::GeneralValue GeneralProperty::getGeneralValue() const
116
GeneralValue generalValue;
117
generalValue.m_name = m_ui->nameInput->isEnabled() ? m_ui->nameInput->text() : QString();
118
generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
119
generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
120
generalValue.m_rect = getRect();
125
void GeneralProperty::apply()
127
int flags = getGeneralPropertyChange();
130
m_generalValue.m_name = m_ui->nameInput->text();
132
if ( flags & Protect )
133
m_generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
135
if ( flags & KeepRatio )
136
m_generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
138
// get the values to the actual rect
139
m_generalValue.m_rect = getRect();
143
KoRect GeneralProperty::getRect() const
145
double x = QMAX( 0, KoUnit::fromUserValue( m_ui->xInput->value(), m_unit ) );
146
double y = QMAX( 0, KoUnit::fromUserValue( m_ui->yInput->value(), m_unit ) );
147
double w = QMAX( 0, KoUnit::fromUserValue( m_ui->widthInput->value(), m_unit ) );
148
double h = QMAX( 0, KoUnit::fromUserValue( m_ui->heightInput->value(), m_unit ) );
150
KoRect rect( x, y, w, h );
155
void GeneralProperty::setRect( KoRect &rect )
157
m_ui->xInput->setValue( KoUnit::toUserValue( QMAX( 0.00, rect.left() ), m_unit ) );
158
m_ui->yInput->setValue( KoUnit::toUserValue( QMAX( 0.00, rect.top() ), m_unit ) );
159
m_ui->widthInput->setValue( KoUnit::toUserValue( QMAX( 0.00, rect.width() ), m_unit ) );
160
m_ui->heightInput->setValue( KoUnit::toUserValue( QMAX( 0.00, rect.height() ), m_unit ) );
164
void GeneralProperty::slotReset()
166
switch ( m_generalValue.m_protect )
169
m_ui->protect->setChecked( true );
172
m_ui->protect->setChecked( false );
175
m_ui->protect->setTristate( true );
176
m_ui->protect->setNoChange();
179
m_ui->protect->setChecked( false );
183
switch ( m_generalValue.m_keepRatio )
186
m_ui->keepRatio->setChecked( true );
189
m_ui->keepRatio->setChecked( false );
192
m_ui->keepRatio->setTristate( true );
193
m_ui->keepRatio->setNoChange();
196
m_ui->keepRatio->setChecked( false );
200
setRect( m_generalValue.m_rect );
201
// this is done due to the rounding so we can detect a change
202
m_generalValue.m_rect = getRect();
206
void GeneralProperty::slotProtectToggled( bool state )
208
m_ui->positionGroup->setEnabled( !state );
209
m_ui->sizeGroup->setEnabled( !state );
213
void GeneralProperty::slotKeepRatioToggled( bool state )
217
if ( m_ui->widthInput->value() == 0 )
223
m_ratio = m_ui->heightInput->value() / m_ui->widthInput->value();
229
void GeneralProperty::slotWidthChanged( double value )
231
if ( m_ui->keepRatio->isChecked() )
233
m_ui->heightInput->setValue( value * m_ratio );
238
void GeneralProperty::slotHeightChanged( double value )
240
if ( m_ui->keepRatio->isChecked() && m_ratio != 0 )
242
m_ui->widthInput->setValue( value / m_ratio );
247
#include "generalproperty.moc"