1
/* This file is part of the KDE project
2
Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@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.
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.
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.
20
#include "KoCustomVariablesDia.h"
21
#include "KoCustomVariablesDia.moc"
24
#include <kbuttonbox.h>
26
#include <qcombobox.h>
29
#include <qpushbutton.h>
31
#include <klineedit.h>
34
/******************************************************************
36
* Class: KoVariableNameDia
38
******************************************************************/
40
KoVariableNameDia::KoVariableNameDia( QWidget *parent )
41
: KDialogBase( parent, "", TRUE,i18n( "Entry Name" ),Ok|Cancel )
47
KoVariableNameDia::KoVariableNameDia( QWidget *parent, const QPtrList<KoVariable>& vars )
48
: KDialogBase( parent, "", TRUE, i18n( "Variable Name" ), Ok|Cancel )
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 );
62
void KoVariableNameDia::init()
64
back = makeVBoxMainWidget();
66
QHBox *row1 = new QHBox( back );
67
row1->setSpacing( KDialog::spacingHint() );
69
QLabel *l = new QLabel( i18n( "Name:" ), row1 );
70
l->setFixedSize( l->sizeHint() );
71
names = new QComboBox( TRUE, row1 );
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() );
84
QString KoVariableNameDia::getName() const
86
return names->currentText();
89
void KoVariableNameDia::textChanged ( const QString &_text )
91
enableButtonOK(!_text.isEmpty());
94
/******************************************************************
96
* Class: KoCustomVariablesListItem
98
******************************************************************/
100
KoCustomVariablesListItem::KoCustomVariablesListItem( QListView *parent )
101
: QListViewItem( parent )
103
editWidget = new KLineEdit( listView()->viewport() );
104
listView()->addChild( editWidget );
107
void KoCustomVariablesListItem::setup()
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() );
116
void KoCustomVariablesListItem::update()
118
editWidget->resize( listView()->header()->cellSize( 1 ), height() );
119
listView()->moveChild( editWidget, listView()->header()->cellPos( 1 ),
120
listView()->itemPos( this ) + listView()->contentsY() );
124
void KoCustomVariablesListItem::setVariable( KoCustomVariable *v )
127
editWidget->setText( var->value() );
128
setText( 0, v->name() );
131
KoCustomVariable *KoCustomVariablesListItem::getVariable() const
136
void KoCustomVariablesListItem::applyValue()
138
QString newVal=editWidget->text();
139
if(var->value()!=newVal)
140
var->setValue( newVal );
143
int KoCustomVariablesListItem::width( const QFontMetrics & fm, const QListView *lv, int c ) const
145
// The text of the 2nd column isn't known to QListViewItem, only we know it
146
// (it's in our lineedit)
148
QString val = editWidget->text();
149
int w = fm.width( val );
152
return QListViewItem::width( fm, lv, c );
155
/******************************************************************
157
* Class: KoCustomVariablesList
159
******************************************************************/
161
KoCustomVariablesList::KoCustomVariablesList( QWidget *parent )
162
: KListView( parent )
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 ) ) );
172
setResizeMode(QListView::LastColumn);
176
void KoCustomVariablesList::setValues()
178
QListViewItemIterator it( this );
179
for ( ; it.current(); ++it )
180
( (KoCustomVariablesListItem *)it.current() )->applyValue();
183
void KoCustomVariablesList::columnSizeChange( int c, int, int )
185
if ( c == 0 || c == 1 )
189
void KoCustomVariablesList::sectionClicked( int )
194
void KoCustomVariablesList::updateItems()
196
QListViewItemIterator it( this );
197
for ( ; it.current(); ++it )
198
( (KoCustomVariablesListItem*)it.current() )->update();
201
/******************************************************************
203
* Class: KoCustomVariablesDia
205
******************************************************************/
207
KoCustomVariablesDia::KoCustomVariablesDia( QWidget *parent, const QPtrList<KoVariable> &variables )
208
: KDialogBase( parent, "", TRUE,i18n( "Variable Value Editor" ), Ok|Cancel )
211
back = makeVBoxMainWidget();
213
list = new KoCustomVariablesList( back );
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 );
230
connect( this, SIGNAL( okClicked() ),
231
this, SLOT( slotOk() ) );
232
connect( this, SIGNAL( cancelClicked() ),
233
this, SLOT( reject() ) );
234
showButtonOK(lst.count()>0);
239
void KoCustomVariablesDia::slotOk()
245
/******************************************************************
247
* Class: KoCustomVarDialog
249
******************************************************************/
251
KoCustomVarDialog::KoCustomVarDialog( QWidget *parent )
252
: KDialogBase( parent, "", TRUE,i18n( "Add Variable" ), Ok|Cancel )
258
connect( this, SIGNAL( okClicked() ),
259
this, SLOT( slotAddOk() ) );
260
connect( this, SIGNAL( cancelClicked() ),
261
this, SLOT( reject() ) );
263
connect( m_name, SIGNAL( textChanged(const QString&) ),
264
this, SLOT( slotTextChanged(const QString&) ) );
266
enableButtonOK( false );
270
// edit existing variable
271
KoCustomVarDialog::KoCustomVarDialog( QWidget *parent, KoCustomVariable *var )
272
: KDialogBase( parent, "", TRUE,i18n( "Edit Variable" ), Ok|Cancel )
276
m_name->setText( m_var->name() );
277
m_value->setText( m_var->value() );
278
m_name->setReadOnly(true);
282
connect( this, SIGNAL( okClicked() ),
283
this, SLOT( slotEditOk() ) );
284
connect( this, SIGNAL( cancelClicked() ),
285
this, SLOT( reject() ) );
287
connect( m_value, SIGNAL( textChanged(const QString&) ),
288
this, SLOT( slotTextChanged(const QString&) ) );
290
enableButtonOK( true );
294
void KoCustomVarDialog::init()
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 );
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 );
310
void KoCustomVarDialog::slotAddOk()
314
void KoCustomVarDialog::slotEditOk()
316
m_var->setValue( m_value->text() );
320
void KoCustomVarDialog::slotTextChanged(const QString&text)
322
enableButtonOK( !text.isEmpty() );
324
QString KoCustomVarDialog::name()
326
if ( m_name->text().isEmpty() )
327
return QString( "No name" );
328
return m_name->text();
331
QString KoCustomVarDialog::value()
333
if ( m_value->text().isEmpty() )
334
return QString( "No value" );
335
return m_value->text();