00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "KoCustomVariablesDia.h"
00021 #include "KoCustomVariablesDia.moc"
00022
00023 #include <klocale.h>
00024 #include <kbuttonbox.h>
00025
00026 #include <QComboBox>
00027
00028 #include <QLabel>
00029 #include <QPushButton>
00030 #include <q3header.h>
00031
00032 #include <Q3PtrList>
00033 #include <klineedit.h>
00034 #include <kdebug.h>
00035 #include <kvbox.h>
00036
00037
00038
00039
00040
00041
00042
00043 KoVariableNameDia::KoVariableNameDia( QWidget *parent )
00044 : KDialog( parent )
00045 {
00046 setCaption( i18n( "Entry Name" ) );
00047 setModal( true );
00048 setButtons( Ok|Cancel );
00049 init();
00050 }
00051
00052
00053 KoVariableNameDia::KoVariableNameDia( QWidget *parent, const Q3PtrList<KoVariable>& vars )
00054 : KDialog( parent )
00055 {
00056 setCaption( i18n( "Variable Name" ) );
00057 setModal( true );
00058 setButtons( Ok|Cancel );
00059
00060 init();
00061 enableButtonOk(false);
00062 Q3PtrListIterator<KoVariable> it( vars );
00063 for ( ; it.current() ; ++it ) {
00064 KoVariable *var = it.current();
00065 if ( var->type() == VT_CUSTOM )
00066 names->insertItem( 0, ( (KoCustomVariable*) var )->name() );
00067 }
00068
00069 }
00070
00071 void KoVariableNameDia::init()
00072 {
00073 back = new KVBox();
00074 setMainWidget( back );
00075
00076 KHBox *row1 = new KHBox( back );
00077 row1->setSpacing( KDialog::spacingHint() );
00078
00079 QLabel *l = new QLabel( i18n( "Name:" ), row1 );
00080 l->setFixedSize( l->sizeHint() );
00081 names = new QComboBox(row1 );
00082 names->setEditable( true );
00083 names->setFocus();
00084
00085 connect( names, SIGNAL( textChanged ( const QString & )),
00086 this, SLOT( textChanged ( const QString & )));
00087 connect( this, SIGNAL( okClicked() ),
00088 this, SLOT( accept() ) );
00089 connect( this, SIGNAL( cancelClicked() ),
00090 this, SLOT( reject() ) );
00091 enableButtonOk( !names->currentText().isEmpty() );
00092 resize( 350, 100 );
00093 }
00094
00095 QString KoVariableNameDia::getName() const
00096 {
00097 return names->currentText();
00098 }
00099
00100 void KoVariableNameDia::textChanged ( const QString &_text )
00101 {
00102 enableButtonOk(!_text.isEmpty());
00103 }
00104
00105
00106
00107
00108
00109
00110
00111 KoCustomVariablesListItem::KoCustomVariablesListItem( Q3ListView *parent )
00112 : Q3ListViewItem( parent )
00113 {
00114 editWidget = new KLineEdit( listView()->viewport() );
00115 listView()->addChild( editWidget );
00116 }
00117
00118 void KoCustomVariablesListItem::setup()
00119 {
00120 Q3ListViewItem::setup();
00121 setHeight( qMax( listView()->fontMetrics().height(),
00122 editWidget->sizeHint().height() ) );
00123
00124
00125 }
00126
00127 void KoCustomVariablesListItem::update()
00128 {
00129 editWidget->resize( listView()->header()->cellSize( 1 ), height() );
00130 listView()->moveChild( editWidget, listView()->header()->cellPos( 1 ),
00131 listView()->itemPos( this ) + listView()->contentsY() );
00132 editWidget->show();
00133 }
00134
00135 void KoCustomVariablesListItem::setVariable( KoCustomVariable *v )
00136 {
00137 var = v;
00138 editWidget->setText( var->value() );
00139 setText( 0, v->name() );
00140 }
00141
00142 KoCustomVariable *KoCustomVariablesListItem::getVariable() const
00143 {
00144 return var;
00145 }
00146
00147 void KoCustomVariablesListItem::applyValue()
00148 {
00149 QString newVal=editWidget->text();
00150 if(var->value()!=newVal)
00151 var->setValue( newVal );
00152 }
00153
00154 int KoCustomVariablesListItem::width( const QFontMetrics & fm, const Q3ListView *lv, int c ) const
00155 {
00156
00157
00158 if ( c == 1 ) {
00159 QString val = editWidget->text();
00160 int w = fm.width( val );
00161 return w;
00162 } else
00163 return Q3ListViewItem::width( fm, lv, c );
00164 }
00165
00166
00167
00168
00169
00170
00171
00172 KoCustomVariablesList::KoCustomVariablesList( QWidget *parent )
00173 : K3ListView( parent )
00174 {
00175 header()->setMovingEnabled( false );
00176 addColumn( i18n( "Variable" ) );
00177 addColumn( i18n( "Value" ) );
00178 connect( header(), SIGNAL( sizeChange( int, int, int ) ),
00179 this, SLOT( columnSizeChange( int, int, int ) ) );
00180 connect( header(), SIGNAL( sectionClicked( int ) ),
00181 this, SLOT( sectionClicked( int ) ) );
00182
00183 setResizeMode(Q3ListView::LastColumn);
00184 setSorting( -1 );
00185 }
00186
00187 void KoCustomVariablesList::setValues()
00188 {
00189 Q3ListViewItemIterator it( this );
00190 for ( ; it.current(); ++it )
00191 ( (KoCustomVariablesListItem *)it.current() )->applyValue();
00192 }
00193
00194 void KoCustomVariablesList::columnSizeChange( int c, int, int )
00195 {
00196 if ( c == 0 || c == 1 )
00197 updateItems();
00198 }
00199
00200 void KoCustomVariablesList::sectionClicked( int )
00201 {
00202 updateItems();
00203 }
00204
00205 void KoCustomVariablesList::updateItems()
00206 {
00207 Q3ListViewItemIterator it( this );
00208 for ( ; it.current(); ++it )
00209 ( (KoCustomVariablesListItem*)it.current() )->update();
00210 }
00211
00212
00213
00214
00215
00216
00217
00218 KoCustomVariablesDia::KoCustomVariablesDia( QWidget *parent, const Q3PtrList<KoVariable> &variables )
00219 : KDialog( parent )
00220 {
00221 setCaption( i18n( "Variable Value Editor" ) );
00222 setModal( true );
00223 setButtons( Ok|Cancel );
00224
00225 back = new KVBox();
00226 setMainWidget(back);
00227
00228 list = new KoCustomVariablesList( back );
00229
00230 QStringList lst;
00231 Q3PtrListIterator<KoVariable> it( variables );
00232 for ( ; it.current() ; ++it ) {
00233 KoVariable *var = it.current();
00234 if ( var->type() == VT_CUSTOM ) {
00235 KoCustomVariable *v = (KoCustomVariable*)var;
00236 if ( !lst.contains( v->name() ) ) {
00237 lst.append( v->name() );
00238 KoCustomVariablesListItem *item = new KoCustomVariablesListItem( list );
00239 item->setVariable( v );
00240 }
00241 }
00242 }
00243
00244
00245 connect( this, SIGNAL( okClicked() ),
00246 this, SLOT( slotOk() ) );
00247 connect( this, SIGNAL( cancelClicked() ),
00248 this, SLOT( reject() ) );
00249 showButton(Ok, lst.count()>0);
00250
00251 resize( 600, 400 );
00252 }
00253
00254 void KoCustomVariablesDia::slotOk()
00255 {
00256 list->setValues();
00257 accept();
00258 }
00259
00260
00261
00262
00263
00264
00265
00266 KoCustomVarDialog::KoCustomVarDialog( QWidget *parent )
00267 : KDialog( parent )
00268 {
00269 setCaption( i18n( "Add Variable" ) );
00270 setModal( true );
00271 setButtons( Ok|Cancel );
00272
00273 init();
00274 m_name->setFocus();
00275
00276
00277 connect( this, SIGNAL( okClicked() ),
00278 this, SLOT( slotAddOk() ) );
00279 connect( this, SIGNAL( cancelClicked() ),
00280 this, SLOT( reject() ) );
00281
00282 connect( m_name, SIGNAL( textChanged(const QString&) ),
00283 this, SLOT( slotTextChanged(const QString&) ) );
00284
00285 enableButtonOk( false );
00286 resize( 350, 100 );
00287
00288 }
00289
00290 KoCustomVarDialog::KoCustomVarDialog( QWidget *parent, KoCustomVariable *var )
00291 : KDialog( parent )
00292 {
00293 setCaption( i18n( "Edit Variable" ) );
00294 setModal( true );
00295 setButtons( Ok|Cancel );
00296
00297 m_var = var;
00298 init();
00299 m_name->setText( m_var->name() );
00300 m_value->setText( m_var->value() );
00301 m_name->setReadOnly(true);
00302 m_value->setFocus();
00303
00304
00305 connect( this, SIGNAL( okClicked() ),
00306 this, SLOT( slotEditOk() ) );
00307 connect( this, SIGNAL( cancelClicked() ),
00308 this, SLOT( reject() ) );
00309
00310 connect( m_value, SIGNAL( textChanged(const QString&) ),
00311 this, SLOT( slotTextChanged(const QString&) ) );
00312
00313 enableButtonOk( true );
00314 resize( 350, 100 );
00315 }
00316
00317 void KoCustomVarDialog::init()
00318 {
00319 back = new KVBox();
00320 setMainWidget(back);
00321 KHBox *row1 = new KHBox( back );
00322 row1->setSpacing( KDialog::spacingHint() );
00323 QLabel *ln = new QLabel( i18n( "Name:" ), row1 );
00324 ln->setFixedSize( ln->sizeHint() );
00325 m_name = new KLineEdit( row1 );
00326
00327 KHBox *row2 = new KHBox( back );
00328 row2->setSpacing( KDialog::spacingHint() );
00329 QLabel *lv = new QLabel( i18n( "Value:" ), row2 );
00330 lv->setFixedSize( lv->sizeHint() );
00331 m_value = new KLineEdit( row2 );
00332 }
00333
00334 void KoCustomVarDialog::slotAddOk()
00335 {
00336 accept();
00337 }
00338 void KoCustomVarDialog::slotEditOk()
00339 {
00340 m_var->setValue( m_value->text() );
00341 accept();
00342 }
00343
00344 void KoCustomVarDialog::slotTextChanged(const QString&text)
00345 {
00346 enableButtonOk( !text.isEmpty() );
00347 }
00348 QString KoCustomVarDialog::name()
00349 {
00350 if ( m_name->text().isEmpty() )
00351 return QString( "No name" );
00352 return m_name->text();
00353 }
00354
00355 QString KoCustomVarDialog::value()
00356 {
00357 if ( m_value->text().isEmpty() )
00358 return QString( "No value" );
00359 return m_value->text();
00360 }