~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kpresenter/generalproperty.cc

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-04-20 21:38:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060420213853-j5lxluqvymxt2zny
Tags: 1:1.5.0-0ubuntu2
UbuntuĀ uploadĀ 

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
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.
9
 
 
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.
14
 
 
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.
19
 
*/
20
 
#include "generalproperty.h"
21
 
 
22
 
#include <qcheckbox.h>
23
 
#include <qgroupbox.h>
24
 
#include <qlabel.h>
25
 
#include <qlayout.h>
26
 
#include <qlineedit.h>
27
 
 
28
 
#include <knuminput.h>
29
 
#include <klocale.h>
30
 
 
31
 
#include "generalpropertyui.h"
32
 
 
33
 
GeneralProperty::GeneralProperty( QWidget *parent, const char *name, GeneralValue &generalValue, KoUnit::Unit unit )
34
 
: QWidget( parent, name )
35
 
, m_ratio( 1.0 )
36
 
, m_generalValue( generalValue )
37
 
, m_unit( unit )
38
 
{
39
 
    QVBoxLayout *layout = new QVBoxLayout( this );
40
 
    layout->addWidget( m_ui = new GeneralPropertyUI( this ) );
41
 
 
42
 
    QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding );
43
 
    layout->addItem( spacer );
44
 
 
45
 
    if ( m_generalValue.m_name.isNull() )
46
 
    {
47
 
        m_ui->nameLabel->setEnabled( false );
48
 
        m_ui->nameInput->setEnabled( false );
49
 
    }
50
 
    else
51
 
    {
52
 
        m_ui->nameInput->setText( m_generalValue.m_name );
53
 
    }
54
 
 
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 ) ) );
57
 
 
58
 
    connect( m_ui->protect, SIGNAL( toggled( bool ) ), this, SLOT( slotProtectToggled( bool ) ) );
59
 
    connect( m_ui->keepRatio, SIGNAL( toggled( bool ) ), this, SLOT( slotKeepRatioToggled( bool ) ) );
60
 
 
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 ) ) );
67
 
 
68
 
    slotReset();
69
 
}
70
 
 
71
 
 
72
 
GeneralProperty::~GeneralProperty()
73
 
{
74
 
}
75
 
 
76
 
 
77
 
int GeneralProperty::getGeneralPropertyChange() const
78
 
{
79
 
    int flags = 0;
80
 
 
81
 
    if ( !m_generalValue.m_name.isNull() && m_generalValue.m_name != m_ui->nameInput->text() )
82
 
        flags |= Name;
83
 
 
84
 
    if ( m_ui->protect->state() != QButton::NoChange )
85
 
    {
86
 
        if ( ( m_ui->protect->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_protect )
87
 
            flags |= Protect;
88
 
 
89
 
        if ( !m_ui->protect->isOn() )
90
 
        {
91
 
            KoRect rect = getRect();
92
 
            if ( m_generalValue.m_rect.left() != rect.left() )
93
 
                flags |= Left;
94
 
            if ( m_generalValue.m_rect.top() != rect.top() )
95
 
                flags |= 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 )
98
 
                flags |= Width;
99
 
            if ( QABS( m_generalValue.m_rect.height() - rect.height() ) > 1e-6 )
100
 
                flags |= Height;
101
 
        }
102
 
    }
103
 
 
104
 
    if ( m_ui->keepRatio->state() != QButton::NoChange
105
 
         && ( m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_keepRatio )
106
 
    {
107
 
        flags |= KeepRatio;
108
 
    }
109
 
 
110
 
    return flags;
111
 
}
112
 
 
113
 
 
114
 
GeneralProperty::GeneralValue GeneralProperty::getGeneralValue() const
115
 
{
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();
121
 
    return generalValue;
122
 
}
123
 
 
124
 
 
125
 
void GeneralProperty::apply()
126
 
{
127
 
    int flags = getGeneralPropertyChange();
128
 
 
129
 
    if ( flags & Name )
130
 
        m_generalValue.m_name = m_ui->nameInput->text();
131
 
 
132
 
    if ( flags & Protect )
133
 
        m_generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
134
 
 
135
 
    if ( flags & KeepRatio )
136
 
        m_generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
137
 
 
138
 
    // get the values to the actual rect
139
 
    m_generalValue.m_rect = getRect();
140
 
}
141
 
 
142
 
 
143
 
KoRect GeneralProperty::getRect() const
144
 
{
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 ) );
149
 
 
150
 
    KoRect rect( x, y, w, h );
151
 
    return rect;
152
 
}
153
 
 
154
 
 
155
 
void GeneralProperty::setRect( KoRect &rect )
156
 
{
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 ) );
161
 
}
162
 
 
163
 
 
164
 
void GeneralProperty::slotReset()
165
 
{
166
 
    switch ( m_generalValue.m_protect )
167
 
    {
168
 
        case STATE_ON:
169
 
            m_ui->protect->setChecked( true );
170
 
            break;
171
 
        case STATE_OFF:
172
 
            m_ui->protect->setChecked( false );
173
 
            break;
174
 
        case STATE_UNDEF:
175
 
            m_ui->protect->setTristate( true );
176
 
            m_ui->protect->setNoChange();
177
 
            break;
178
 
        default:
179
 
            m_ui->protect->setChecked( false );
180
 
            break;
181
 
    }
182
 
 
183
 
    switch ( m_generalValue.m_keepRatio )
184
 
    {
185
 
        case STATE_ON:
186
 
            m_ui->keepRatio->setChecked( true );
187
 
            break;
188
 
        case STATE_OFF:
189
 
            m_ui->keepRatio->setChecked( false );
190
 
            break;
191
 
        case STATE_UNDEF:
192
 
            m_ui->keepRatio->setTristate( true );
193
 
            m_ui->keepRatio->setNoChange();
194
 
            break;
195
 
        default:
196
 
            m_ui->keepRatio->setChecked( false );
197
 
            break;
198
 
    }
199
 
 
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();
203
 
}
204
 
 
205
 
 
206
 
void GeneralProperty::slotProtectToggled( bool state )
207
 
{
208
 
    m_ui->positionGroup->setEnabled( !state );
209
 
    m_ui->sizeGroup->setEnabled( !state );
210
 
}
211
 
 
212
 
 
213
 
void GeneralProperty::slotKeepRatioToggled( bool state )
214
 
{
215
 
    if ( state )
216
 
    {
217
 
        if ( m_ui->widthInput->value() == 0 )
218
 
        {
219
 
            m_ratio = 1.0;
220
 
        }
221
 
        else
222
 
        {
223
 
            m_ratio = m_ui->heightInput->value() / m_ui->widthInput->value();
224
 
        }
225
 
    }
226
 
}
227
 
 
228
 
 
229
 
void GeneralProperty::slotWidthChanged( double value )
230
 
{
231
 
    if ( m_ui->keepRatio->isChecked() )
232
 
    {
233
 
        m_ui->heightInput->setValue( value * m_ratio );
234
 
    }
235
 
}
236
 
 
237
 
 
238
 
void GeneralProperty::slotHeightChanged( double value )
239
 
{
240
 
    if ( m_ui->keepRatio->isChecked() && m_ratio != 0 )
241
 
    {
242
 
        m_ui->widthInput->setValue( value / m_ratio );
243
 
    }
244
 
}
245
 
 
246
 
 
247
 
#include "generalproperty.moc"