lib

KoCustomVariablesDia.cpp
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 <tdelocale.h>
24#include <kbuttonbox.h>
25
26#include <tqcombobox.h>
27#include <tqvbox.h>
28#include <tqlabel.h>
29#include <tqpushbutton.h>
30#include <tqheader.h>
31#include <klineedit.h>
32#include <kdebug.h>
33
34/******************************************************************
35 *
36 * Class: KoVariableNameDia
37 *
38 ******************************************************************/
39
40KoVariableNameDia::KoVariableNameDia( TQWidget *parent )
41 : KDialogBase( parent, "", true,i18n( "Entry Name" ),Ok|Cancel )
42{
43 init();
44}
45
46
47KoVariableNameDia::KoVariableNameDia( TQWidget *parent, const TQPtrList<KoVariable>& vars )
48 : KDialogBase( parent, "", true, i18n( "Variable Name" ), Ok|Cancel )
49{
50
51 init();
52 enableButtonOK(false);
53 TQPtrListIterator<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
62void KoVariableNameDia::init()
63{
64 back = makeVBoxMainWidget();
65
66 TQHBox *row1 = new TQHBox( back );
67 row1->setSpacing( KDialog::spacingHint() );
68
69 TQLabel *l = new TQLabel( i18n( "Name:" ), row1 );
70 l->setFixedSize( l->sizeHint() );
71 names = new TQComboBox( true, row1 );
72 names->setFocus();
73
74 connect( names, TQ_SIGNAL( textChanged ( const TQString & )),
75 this, TQ_SLOT( textChanged ( const TQString & )));
76 connect( this, TQ_SIGNAL( okClicked() ),
77 this, TQ_SLOT( accept() ) );
78 connect( this, TQ_SIGNAL( cancelClicked() ),
79 this, TQ_SLOT( reject() ) );
80 enableButtonOK( !names->currentText().isEmpty() );
81 resize( 350, 100 );
82}
83
84TQString KoVariableNameDia::getName() const
85{
86 return names->currentText();
87}
88
89void KoVariableNameDia::textChanged ( const TQString &_text )
90{
91 enableButtonOK(!_text.isEmpty());
92}
93
94/******************************************************************
95 *
96 * Class: KoCustomVariablesListItem
97 *
98 ******************************************************************/
99
100KoCustomVariablesListItem::KoCustomVariablesListItem( TQListView *parent )
101 : TQListViewItem( parent )
102{
103 editWidget = new KLineEdit( listView()->viewport() );
104 listView()->addChild( editWidget );
105}
106
107void KoCustomVariablesListItem::setup()
108{
109 TQListViewItem::setup();
110 setHeight( TQMAX( listView()->fontMetrics().height(),
111 editWidget->sizeHint().height() ) );
112 //if ( listView()->columnWidth( 1 ) < editWidget->sizeHint().width() )
113 // listView()->setColumnWidth( 1, editWidget->sizeHint().width() );
114}
115
116void 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
124void KoCustomVariablesListItem::setVariable( KoCustomVariable *v )
125{
126 var = v;
127 editWidget->setText( var->value() );
128 setText( 0, v->name() );
129}
130
131KoCustomVariable *KoCustomVariablesListItem::getVariable() const
132{
133 return var;
134}
135
136void KoCustomVariablesListItem::applyValue()
137{
138 TQString newVal=editWidget->text();
139 if(var->value()!=newVal)
140 var->setValue( newVal );
141}
142
143int KoCustomVariablesListItem::width( const TQFontMetrics & fm, const TQListView *lv, int c ) const
144{
145 // The text of the 2nd column isn't known to TQListViewItem, only we know it
146 // (it's in our lineedit)
147 if ( c == 1 ) {
148 TQString val = editWidget->text();
149 int w = fm.width( val );
150 return w;
151 } else
152 return TQListViewItem::width( fm, lv, c );
153}
154
155/******************************************************************
156 *
157 * Class: KoCustomVariablesList
158 *
159 ******************************************************************/
160
161KoCustomVariablesList::KoCustomVariablesList( TQWidget *parent )
162 : TDEListView( parent )
163{
164 header()->setMovingEnabled( false );
165 addColumn( i18n( "Variable" ) );
166 addColumn( i18n( "Value" ) );
167 connect( header(), TQ_SIGNAL( sizeChange( int, int, int ) ),
168 this, TQ_SLOT( columnSizeChange( int, int, int ) ) );
169 connect( header(), TQ_SIGNAL( sectionClicked( int ) ),
170 this, TQ_SLOT( sectionClicked( int ) ) );
171
172 setResizeMode(TQListView::LastColumn);
173 setSorting( -1 );
174}
175
176void KoCustomVariablesList::setValues()
177{
178 TQListViewItemIterator it( this );
179 for ( ; it.current(); ++it )
180 ( (KoCustomVariablesListItem *)it.current() )->applyValue();
181}
182
183void KoCustomVariablesList::columnSizeChange( int c, int, int )
184{
185 if ( c == 0 || c == 1 )
186 updateItems();
187}
188
189void KoCustomVariablesList::sectionClicked( int )
190{
191 updateItems();
192}
193
194void KoCustomVariablesList::updateItems()
195{
196 TQListViewItemIterator it( this );
197 for ( ; it.current(); ++it )
198 ( (KoCustomVariablesListItem*)it.current() )->update();
199}
200
201/******************************************************************
202 *
203 * Class: KoCustomVariablesDia
204 *
205 ******************************************************************/
206
207KoCustomVariablesDia::KoCustomVariablesDia( TQWidget *parent, const TQPtrList<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 TQStringList lst;
216 TQPtrListIterator<KoVariable> it( variables );
217 for ( ; it.current() ; ++it ) {
218 KoVariable *var = it.current();
219 if ( var->type() == VT_CUSTOM ) {
221 if ( !lst.contains( v->name() ) ) {
222 lst.append( v->name() );
224 item->setVariable( v );
225 }
226 }
227 }
228
229
230 connect( this, TQ_SIGNAL( okClicked() ),
231 this, TQ_SLOT( slotOk() ) );
232 connect( this, TQ_SIGNAL( cancelClicked() ),
233 this, TQ_SLOT( reject() ) );
234 showButtonOK(lst.count()>0);
235
236 resize( 600, 400 );
237}
238
240{
241 list->setValues();
242 accept();
243}
244
245/******************************************************************
246 *
247 * Class: KoCustomVarDialog
248 *
249 ******************************************************************/
250
252 : KDialogBase( parent, "", true,i18n( "Add Variable" ), Ok|Cancel )
253{
254 init();
255 m_name->setFocus();
256
257
258 connect( this, TQ_SIGNAL( okClicked() ),
259 this, TQ_SLOT( slotAddOk() ) );
260 connect( this, TQ_SIGNAL( cancelClicked() ),
261 this, TQ_SLOT( reject() ) );
262
263 connect( m_name, TQ_SIGNAL( textChanged(const TQString&) ),
264 this, TQ_SLOT( slotTextChanged(const TQString&) ) );
265
266 enableButtonOK( false );
267 resize( 350, 100 );
268
269}
270// edit existing variable
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, TQ_SIGNAL( okClicked() ),
283 this, TQ_SLOT( slotEditOk() ) );
284 connect( this, TQ_SIGNAL( cancelClicked() ),
285 this, TQ_SLOT( reject() ) );
286
287 connect( m_value, TQ_SIGNAL( textChanged(const TQString&) ),
288 this, TQ_SLOT( slotTextChanged(const TQString&) ) );
289
290 enableButtonOK( true );
291 resize( 350, 100 );
292}
293
294void KoCustomVarDialog::init()
295{
296 back = makeVBoxMainWidget();
297 TQHBox *row1 = new TQHBox( back );
298 row1->setSpacing( KDialog::spacingHint() );
299 TQLabel *ln = new TQLabel( i18n( "Name:" ), row1 );
300 ln->setFixedSize( ln->sizeHint() );
301 m_name = new KLineEdit( row1 );
302
303 TQHBox *row2 = new TQHBox( back );
304 row2->setSpacing( KDialog::spacingHint() );
305 TQLabel *lv = new TQLabel( i18n( "Value:" ), row2 );
306 lv->setFixedSize( lv->sizeHint() );
307 m_value = new KLineEdit( row2 );
308}
309
310void KoCustomVarDialog::slotAddOk()
311{
312 accept();
313}
314void KoCustomVarDialog::slotEditOk()
315{
316 m_var->setValue( m_value->text() );
317 accept();
318}
319
320void KoCustomVarDialog::slotTextChanged(const TQString&text)
321{
322 enableButtonOK( !text.isEmpty() );
323}
324TQString KoCustomVarDialog::name()
325{
326 if ( m_name->text().isEmpty() )
327 return TQString( "No name" );
328 return m_name->text();
329}
330
331TQString KoCustomVarDialog::value()
332{
333 if ( m_value->text().isEmpty() )
334 return TQString( "No value" );
335 return m_value->text();
336}
void cancelClicked()
void enableButtonOK(bool state)
KDialogBase(TQWidget *parent=0, const char *name=0, bool modal=true, const TQString &caption=TQString::null, int buttonMask=Ok|Apply|Cancel, ButtonCode defaultButton=Ok, bool separator=false, const KGuiItem &user1=KGuiItem(), const KGuiItem &user2=KGuiItem(), const KGuiItem &user3=KGuiItem())
TQVBox * makeVBoxMainWidget()
void okClicked()
virtual void slotOk()
static int spacingHint()
KoCustomVarDialog(TQWidget *parent)
Add new variable.
A custom variable is a variable whose value is entered by the user.
Definition KoVariable.h:552
virtual TQString text(bool realValue=false)
Returns the text to be displayed for this variable It doesn't need to be cached, convert() is fast,...
Class: KoCustomVariablesListItem Used by KoCustomVariablesDia Represents an item in the listview,...
Class: KoCustomVariablesList The listview.
A KoVariable is a custom item, i.e.
Definition KoVariable.h:364
TDEAction * back(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)
const char * name(StdAction id)