summaryrefslogtreecommitdiffstats
path: root/kspread/kspread_generalProperty.cpp
blob: f233576addfbb44effbd41dc79e13d1e473b1e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
/* This file is part of the KDE project
   Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/
#include "kspread_generalProperty.h"

#include <tqcheckbox.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>

#include <knuminput.h>
#include <tdelocale.h>
#include <KoGeneralPropertyUi.h>
#include <KoUnitWidgets.h>

using namespace KSpread;

GeneralProperty::GeneralProperty( TQWidget *parent, const char *name, GeneralValue &generalValue, KoUnit::Unit unit )
: TQWidget( parent, name )
, m_ratio( 1.0 )
, m_generalValue( generalValue )
, m_unit( unit )
{
    TQVBoxLayout *layout = new TQVBoxLayout( this );
    layout->addWidget( m_ui = new KoGeneralPropertyUI( this ) );

    TQSpacerItem* spacer = new TQSpacerItem( 20, 20, TQSizePolicy::Minimum, TQSizePolicy::Expanding );
    layout->addItem( spacer );

    if ( m_generalValue.m_name.isNull() )
    {
        m_ui->nameLabel->setEnabled( false );
        m_ui->nameInput->setEnabled( false );
    }
    else
    {
        m_ui->nameInput->setText( m_generalValue.m_name );
    }

    m_ui->positionGroup->setTitle( i18n( "Position" ) );

    connect( m_ui->protect, TQT_SIGNAL( toggled( bool ) ), this, TQT_SLOT( slotProtectToggled( bool ) ) );
    connect( m_ui->keepRatio, TQT_SIGNAL( toggled( bool ) ), this, TQT_SLOT( slotKeepRatioToggled( bool ) ) );

    double dStep = KoUnit::fromUserValue( 0.5, m_unit );
    double dMax = KoUnit::fromUserValue( 9999, m_unit );
    m_ui->xInput->setUnit( m_unit );
    m_ui->xInput->setMinMaxStep( 0, dMax, dStep );

    m_ui->yInput->setUnit( m_unit );
    m_ui->yInput->setMinMaxStep( 0, dMax, dStep );

    m_ui->widthInput->setUnit( m_unit );
    m_ui->widthInput->setMinMaxStep( 0, dMax, dStep );
    connect( m_ui->widthInput, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( slotWidthChanged( double ) ) );

    m_ui->heightInput->setUnit( m_unit );
    m_ui->heightInput->setMinMaxStep( 0, dMax, dStep );
    connect( m_ui->heightInput, TQT_SIGNAL( valueChanged( double ) ), this, TQT_SLOT( slotHeightChanged( double ) ) );
    slotReset();
}


GeneralProperty::~GeneralProperty()
{
}


int GeneralProperty::getGeneralPropertyChange() const
{
    int flags = 0;

    if ( !m_generalValue.m_name.isNull() && m_generalValue.m_name != m_ui->nameInput->text() )
        flags |= Name;

    if ( m_ui->protect->state() != TQButton::NoChange )
    {
        if ( ( m_ui->protect->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_protect )
            flags |= Protect;

        if ( !m_ui->protect->isOn() )
        {
            KoRect rect = getRect();
            if ( m_generalValue.m_rect.left() != rect.left() )
                flags |= Left;
            if ( m_generalValue.m_rect.top() != rect.top() )
                flags |= Top;
            // this has to be done as the rect cahnges width/hight if left or top is changed
            if ( TQABS( m_generalValue.m_rect.width() - rect.width() ) > 1e-6 )
                flags |= Width;
            if ( TQABS( m_generalValue.m_rect.height() - rect.height() ) > 1e-6 )
                flags |= Height;
        }
    }

    if ( m_ui->keepRatio->state() != TQButton::NoChange
         && ( m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF ) != m_generalValue.m_keepRatio )
    {
        flags |= KeepRatio;
    }

    return flags;
}


GeneralProperty::GeneralValue GeneralProperty::getGeneralValue() const
{
    GeneralValue generalValue;
    generalValue.m_name = m_ui->nameInput->isEnabled() ? m_ui->nameInput->text() : TQString();
    generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;
    generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;
    generalValue.m_rect = getRect();
    return generalValue;
}


void GeneralProperty::apply()
{
    int flags = getGeneralPropertyChange();

    if ( flags & Name )
        m_generalValue.m_name = m_ui->nameInput->text();

    if ( flags & Protect )
        m_generalValue.m_protect = m_ui->protect->isOn() ? STATE_ON : STATE_OFF;

    if ( flags & KeepRatio )
        m_generalValue.m_keepRatio = m_ui->keepRatio->isOn() ? STATE_ON : STATE_OFF;

    // get the values to the actual rect
    m_generalValue.m_rect = getRect();
}


KoRect GeneralProperty::getRect() const
{
    double x = TQMAX( 0, m_ui->xInput->value() );
    double y = TQMAX( 0, m_ui->yInput->value() );
    double w = TQMAX( 0, m_ui->widthInput->value() );
    double h = TQMAX( 0, m_ui->heightInput->value() );

    KoRect rect( x, y, w, h );
    return rect;
}


void GeneralProperty::setRect( KoRect &rect )
{
    m_ui->xInput->changeValue( TQMAX( 0.00, rect.left() ) );
    m_ui->yInput->changeValue( TQMAX( 0.00, rect.top() ) );
    m_ui->widthInput->changeValue( TQMAX( 0.00, rect.width() ) );
    m_ui->heightInput->changeValue( TQMAX( 0.00, rect.height() ) );
}


void GeneralProperty::slotReset()
{
    switch ( m_generalValue.m_protect )
    {
        case STATE_ON:
            m_ui->protect->setChecked( true );
            break;
        case STATE_OFF:
            m_ui->protect->setChecked( false );
            break;
        case STATE_UNDEF:
            m_ui->protect->setTristate( true );
            m_ui->protect->setNoChange();
            break;
        default:
            m_ui->protect->setChecked( false );
            break;
    }

    switch ( m_generalValue.m_keepRatio )
    {
        case STATE_ON:
            m_ui->keepRatio->setChecked( true );
            break;
        case STATE_OFF:
            m_ui->keepRatio->setChecked( false );
            break;
        case STATE_UNDEF:
            m_ui->keepRatio->setTristate( true );
            m_ui->keepRatio->setNoChange();
            break;
        default:
            m_ui->keepRatio->setChecked( false );
            break;
    }

    setRect( m_generalValue.m_rect );
    // this is done due to the rounding so we can detect a change
    m_generalValue.m_rect = getRect();
}


void GeneralProperty::slotProtectToggled( bool state )
{
    m_ui->positionGroup->setEnabled( !state );
}


void GeneralProperty::slotKeepRatioToggled( bool state )
{
    if ( state )
    {
        if ( m_ui->widthInput->value() == 0 )
        {
            m_ratio = 1.0;
        }
        else
        {
            m_ratio = m_ui->heightInput->value() / m_ui->widthInput->value();
        }
    }
}


void GeneralProperty::slotWidthChanged( double value )
{
    if ( m_ui->keepRatio->isChecked() )
    {
        m_ui->heightInput->setValue( value * m_ratio );
    }
}


void GeneralProperty::slotHeightChanged( double value )
{
    if ( m_ui->keepRatio->isChecked() && m_ratio != 0 )
    {
        m_ui->widthInput->setValue( value / m_ratio );
    }
}


#include "kspread_generalProperty.moc"