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

« back to all changes in this revision

Viewing changes to lib/kotext/KoCustomVariablesDia.cpp

  • 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
/* This file is part of the KDE project
 
2
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
 
3
 
 
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
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "KoCustomVariablesDia.h"
 
21
#include "KoCustomVariablesDia.moc"
 
22
 
 
23
#include <klocale.h>
 
24
#include <kbuttonbox.h>
 
25
 
 
26
#include <qcombobox.h>
 
27
#include <qvbox.h>
 
28
#include <qlabel.h>
 
29
#include <qpushbutton.h>
 
30
#include <qheader.h>
 
31
#include <klineedit.h>
 
32
#include <kdebug.h>
 
33
 
 
34
/******************************************************************
 
35
 *
 
36
 * Class: KoVariableNameDia
 
37
 *
 
38
 ******************************************************************/
 
39
 
 
40
KoVariableNameDia::KoVariableNameDia( QWidget *parent )
 
41
    : KDialogBase( parent, "", TRUE,i18n( "Entry Name" ),Ok|Cancel )
 
42
{
 
43
    init();
 
44
}
 
45
 
 
46
 
 
47
KoVariableNameDia::KoVariableNameDia( QWidget *parent, const QPtrList<KoVariable>& vars )
 
48
    : KDialogBase( parent, "", TRUE, i18n( "Variable Name" ), Ok|Cancel )
 
49
{
 
50
 
 
51
    init();
 
52
    enableButtonOK(false);
 
53
    QPtrListIterator<KoVariable> it( vars );
 
54
     for ( ; it.current() ; ++it ) {
 
55
        KoVariable *var = it.current();
 
56
        if ( var->type() == VT_CUSTOM )
 
57
            names->insertItem( ( (KoCustomVariable*) var )->name(), -1 );
 
58
    }
 
59
 
 
60
}
 
61
 
 
62
void KoVariableNameDia::init()
 
63
{
 
64
    back = makeVBoxMainWidget();
 
65
 
 
66
    QHBox *row1 = new QHBox( back );
 
67
    row1->setSpacing( KDialog::spacingHint() );
 
68
 
 
69
    QLabel *l = new QLabel( i18n( "Name:" ), row1 );
 
70
    l->setFixedSize( l->sizeHint() );
 
71
    names = new QComboBox( TRUE, row1 );
 
72
    names->setFocus();
 
73
 
 
74
    connect( names, SIGNAL( textChanged ( const QString & )),
 
75
             this, SLOT( textChanged ( const QString & )));
 
76
    connect( this, SIGNAL( okClicked() ),
 
77
             this, SLOT( accept() ) );
 
78
    connect( this, SIGNAL( cancelClicked() ),
 
79
             this, SLOT( reject() ) );
 
80
    enableButtonOK( !names->currentText().isEmpty() );
 
81
    resize( 350, 100 );
 
82
}
 
83
 
 
84
QString KoVariableNameDia::getName() const
 
85
{
 
86
    return names->currentText();
 
87
}
 
88
 
 
89
void KoVariableNameDia::textChanged ( const QString &_text )
 
90
{
 
91
    enableButtonOK(!_text.isEmpty());
 
92
}
 
93
 
 
94
/******************************************************************
 
95
 *
 
96
 * Class: KoCustomVariablesListItem
 
97
 *
 
98
 ******************************************************************/
 
99
 
 
100
KoCustomVariablesListItem::KoCustomVariablesListItem( QListView *parent )
 
101
    : QListViewItem( parent )
 
102
{
 
103
    editWidget = new KLineEdit( listView()->viewport() );
 
104
    listView()->addChild( editWidget );
 
105
}
 
106
 
 
107
void KoCustomVariablesListItem::setup()
 
108
{
 
109
    QListViewItem::setup();
 
110
    setHeight( QMAX( listView()->fontMetrics().height(),
 
111
                     editWidget->sizeHint().height() ) );
 
112
    //if ( listView()->columnWidth( 1 ) < editWidget->sizeHint().width() )
 
113
    //    listView()->setColumnWidth( 1, editWidget->sizeHint().width() );
 
114
}
 
115
 
 
116
void KoCustomVariablesListItem::update()
 
117
{
 
118
    editWidget->resize( listView()->header()->cellSize( 1 ), height() );
 
119
    listView()->moveChild( editWidget, listView()->header()->cellPos( 1 ),
 
120
                           listView()->itemPos( this ) + listView()->contentsY() );
 
121
    editWidget->show();
 
122
}
 
123
 
 
124
void KoCustomVariablesListItem::setVariable( KoCustomVariable *v )
 
125
{
 
126
    var = v;
 
127
    editWidget->setText( var->value() );
 
128
    setText( 0, v->name() );
 
129
}
 
130
 
 
131
KoCustomVariable *KoCustomVariablesListItem::getVariable() const
 
132
{
 
133
    return var;
 
134
}
 
135
 
 
136
void KoCustomVariablesListItem::applyValue()
 
137
{
 
138
    QString newVal=editWidget->text();
 
139
    if(var->value()!=newVal)
 
140
        var->setValue( newVal );
 
141
}
 
142
 
 
143
int KoCustomVariablesListItem::width( const QFontMetrics & fm, const QListView *lv, int c ) const
 
144
{
 
145
    // The text of the 2nd column isn't known to QListViewItem, only we know it
 
146
    // (it's in our lineedit)
 
147
    if ( c == 1 ) {
 
148
        QString val = editWidget->text();
 
149
        int w = fm.width( val );
 
150
        return w;
 
151
    } else
 
152
        return QListViewItem::width( fm, lv, c );
 
153
}
 
154
 
 
155
/******************************************************************
 
156
 *
 
157
 * Class: KoCustomVariablesList
 
158
 *
 
159
 ******************************************************************/
 
160
 
 
161
KoCustomVariablesList::KoCustomVariablesList( QWidget *parent )
 
162
    : KListView( parent )
 
163
{
 
164
    header()->setMovingEnabled( FALSE );
 
165
    addColumn( i18n( "Variable" ) );
 
166
    addColumn( i18n( "Value" ) );
 
167
    connect( header(), SIGNAL( sizeChange( int, int, int ) ),
 
168
             this, SLOT( columnSizeChange( int, int, int ) ) );
 
169
    connect( header(), SIGNAL( sectionClicked( int ) ),
 
170
             this, SLOT( sectionClicked( int ) ) );
 
171
 
 
172
    setResizeMode(QListView::LastColumn);
 
173
    setSorting( -1 );
 
174
}
 
175
 
 
176
void KoCustomVariablesList::setValues()
 
177
{
 
178
    QListViewItemIterator it( this );
 
179
    for ( ; it.current(); ++it )
 
180
        ( (KoCustomVariablesListItem *)it.current() )->applyValue();
 
181
}
 
182
 
 
183
void KoCustomVariablesList::columnSizeChange( int c, int, int )
 
184
{
 
185
    if ( c == 0 || c == 1 )
 
186
        updateItems();
 
187
}
 
188
 
 
189
void KoCustomVariablesList::sectionClicked( int )
 
190
{
 
191
    updateItems();
 
192
}
 
193
 
 
194
void KoCustomVariablesList::updateItems()
 
195
{
 
196
    QListViewItemIterator it( this );
 
197
    for ( ; it.current(); ++it )
 
198
        ( (KoCustomVariablesListItem*)it.current() )->update();
 
199
}
 
200
 
 
201
/******************************************************************
 
202
 *
 
203
 * Class: KoCustomVariablesDia
 
204
 *
 
205
 ******************************************************************/
 
206
 
 
207
KoCustomVariablesDia::KoCustomVariablesDia( QWidget *parent, const QPtrList<KoVariable> &variables )
 
208
    : KDialogBase( parent, "", TRUE,i18n( "Variable Value Editor" ), Ok|Cancel )
 
209
{
 
210
 
 
211
    back = makeVBoxMainWidget();
 
212
 
 
213
    list = new KoCustomVariablesList( back );
 
214
 
 
215
    QStringList lst;
 
216
    QPtrListIterator<KoVariable> it( variables );
 
217
    for ( ; it.current() ; ++it ) {
 
218
        KoVariable *var = it.current();
 
219
        if ( var->type() == VT_CUSTOM ) {
 
220
            KoCustomVariable *v = (KoCustomVariable*)var;
 
221
            if ( !lst.contains( v->name() ) ) {
 
222
                lst.append( v->name() );
 
223
                KoCustomVariablesListItem *item = new KoCustomVariablesListItem( list );
 
224
                item->setVariable( v );
 
225
            }
 
226
        }
 
227
    }
 
228
 
 
229
 
 
230
    connect( this, SIGNAL( okClicked() ),
 
231
             this, SLOT( slotOk() ) );
 
232
    connect( this, SIGNAL( cancelClicked() ),
 
233
             this, SLOT( reject() ) );
 
234
    showButtonOK(lst.count()>0);
 
235
 
 
236
    resize( 600, 400 );
 
237
}
 
238
 
 
239
void KoCustomVariablesDia::slotOk()
 
240
{
 
241
    list->setValues();
 
242
    accept();
 
243
}
 
244
 
 
245
/******************************************************************
 
246
 *
 
247
 * Class: KoCustomVarDialog
 
248
 *
 
249
 ******************************************************************/
 
250
 
 
251
KoCustomVarDialog::KoCustomVarDialog( QWidget *parent )
 
252
    : KDialogBase( parent, "", TRUE,i18n( "Add Variable" ), Ok|Cancel )
 
253
{
 
254
    init();
 
255
    m_name->setFocus();
 
256
 
 
257
 
 
258
    connect( this, SIGNAL( okClicked() ),
 
259
             this, SLOT( slotAddOk() ) );
 
260
    connect( this, SIGNAL( cancelClicked() ),
 
261
             this, SLOT( reject() ) );
 
262
 
 
263
    connect( m_name, SIGNAL( textChanged(const QString&) ),
 
264
             this, SLOT( slotTextChanged(const QString&) ) );
 
265
 
 
266
    enableButtonOK( false );
 
267
    resize( 350, 100 );
 
268
 
 
269
}
 
270
// edit existing variable
 
271
KoCustomVarDialog::KoCustomVarDialog( QWidget *parent, KoCustomVariable *var )
 
272
    : KDialogBase( parent, "", TRUE,i18n( "Edit Variable" ), Ok|Cancel )
 
273
{
 
274
    m_var = var;
 
275
    init();
 
276
    m_name->setText( m_var->name() );
 
277
    m_value->setText( m_var->value() );
 
278
    m_name->setReadOnly(true);
 
279
    m_value->setFocus();
 
280
 
 
281
 
 
282
    connect( this, SIGNAL( okClicked() ),
 
283
             this, SLOT( slotEditOk() ) );
 
284
    connect( this, SIGNAL( cancelClicked() ),
 
285
             this, SLOT( reject() ) );
 
286
 
 
287
    connect( m_value, SIGNAL( textChanged(const QString&) ),
 
288
             this, SLOT( slotTextChanged(const QString&) ) );
 
289
 
 
290
    enableButtonOK( true );
 
291
    resize( 350, 100 );
 
292
}
 
293
 
 
294
void KoCustomVarDialog::init()
 
295
{
 
296
    back = makeVBoxMainWidget();
 
297
    QHBox *row1 = new QHBox( back );
 
298
    row1->setSpacing( KDialog::spacingHint() );
 
299
    QLabel *ln = new QLabel( i18n( "Name:" ), row1 );
 
300
    ln->setFixedSize( ln->sizeHint() );
 
301
    m_name = new KLineEdit( row1 );
 
302
 
 
303
    QHBox *row2 = new QHBox( back );
 
304
    row2->setSpacing( KDialog::spacingHint() );
 
305
    QLabel *lv = new QLabel( i18n( "Value:" ), row2 );
 
306
    lv->setFixedSize( lv->sizeHint() );
 
307
    m_value = new KLineEdit( row2 );
 
308
}
 
309
 
 
310
void KoCustomVarDialog::slotAddOk()
 
311
{
 
312
    accept();
 
313
}
 
314
void KoCustomVarDialog::slotEditOk()
 
315
{
 
316
    m_var->setValue( m_value->text() );
 
317
    accept();
 
318
}
 
319
 
 
320
void KoCustomVarDialog::slotTextChanged(const QString&text)
 
321
{
 
322
    enableButtonOK( !text.isEmpty() );
 
323
}
 
324
QString KoCustomVarDialog::name()
 
325
{
 
326
    if ( m_name->text().isEmpty() )
 
327
        return QString( "No name" );
 
328
    return m_name->text();
 
329
}
 
330
 
 
331
QString KoCustomVarDialog::value()
 
332
{
 
333
    if ( m_value->text().isEmpty() )
 
334
        return QString( "No value" );
 
335
    return m_value->text();
 
336
}