lib

KoCustomVariablesDia.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KoCustomVariablesDia.h"
00021 #include "KoCustomVariablesDia.moc"
00022 
00023 #include <klocale.h>
00024 #include <kbuttonbox.h>
00025 
00026 #include <qcombobox.h>
00027 #include <qvbox.h>
00028 #include <qlabel.h>
00029 #include <qpushbutton.h>
00030 #include <qheader.h>
00031 #include <klineedit.h>
00032 #include <kdebug.h>
00033 
00034 /******************************************************************
00035  *
00036  * Class: KoVariableNameDia
00037  *
00038  ******************************************************************/
00039 
00040 KoVariableNameDia::KoVariableNameDia( QWidget *parent )
00041     : KDialogBase( parent, "", TRUE,i18n( "Entry Name" ),Ok|Cancel )
00042 {
00043     init();
00044 }
00045 
00046 
00047 KoVariableNameDia::KoVariableNameDia( QWidget *parent, const QPtrList<KoVariable>& vars )
00048     : KDialogBase( parent, "", TRUE, i18n( "Variable Name" ), Ok|Cancel )
00049 {
00050 
00051     init();
00052     enableButtonOK(false);
00053     QPtrListIterator<KoVariable> it( vars );
00054      for ( ; it.current() ; ++it ) {
00055         KoVariable *var = it.current();
00056         if ( var->type() == VT_CUSTOM )
00057             names->insertItem( ( (KoCustomVariable*) var )->name(), -1 );
00058     }
00059 
00060 }
00061 
00062 void KoVariableNameDia::init()
00063 {
00064     back = makeVBoxMainWidget();
00065 
00066     QHBox *row1 = new QHBox( back );
00067     row1->setSpacing( KDialog::spacingHint() );
00068 
00069     QLabel *l = new QLabel( i18n( "Name:" ), row1 );
00070     l->setFixedSize( l->sizeHint() );
00071     names = new QComboBox( TRUE, row1 );
00072     names->setFocus();
00073 
00074     connect( names, SIGNAL( textChanged ( const QString & )),
00075              this, SLOT( textChanged ( const QString & )));
00076     connect( this, SIGNAL( okClicked() ),
00077              this, SLOT( accept() ) );
00078     connect( this, SIGNAL( cancelClicked() ),
00079              this, SLOT( reject() ) );
00080     enableButtonOK( !names->currentText().isEmpty() );
00081     resize( 350, 100 );
00082 }
00083 
00084 QString KoVariableNameDia::getName() const
00085 {
00086     return names->currentText();
00087 }
00088 
00089 void KoVariableNameDia::textChanged ( const QString &_text )
00090 {
00091     enableButtonOK(!_text.isEmpty());
00092 }
00093 
00094 /******************************************************************
00095  *
00096  * Class: KoCustomVariablesListItem
00097  *
00098  ******************************************************************/
00099 
00100 KoCustomVariablesListItem::KoCustomVariablesListItem( QListView *parent )
00101     : QListViewItem( parent )
00102 {
00103     editWidget = new KLineEdit( listView()->viewport() );
00104     listView()->addChild( editWidget );
00105 }
00106 
00107 void KoCustomVariablesListItem::setup()
00108 {
00109     QListViewItem::setup();
00110     setHeight( QMAX( listView()->fontMetrics().height(),
00111                      editWidget->sizeHint().height() ) );
00112     //if ( listView()->columnWidth( 1 ) < editWidget->sizeHint().width() )
00113     //    listView()->setColumnWidth( 1, editWidget->sizeHint().width() );
00114 }
00115 
00116 void KoCustomVariablesListItem::update()
00117 {
00118     editWidget->resize( listView()->header()->cellSize( 1 ), height() );
00119     listView()->moveChild( editWidget, listView()->header()->cellPos( 1 ),
00120                            listView()->itemPos( this ) + listView()->contentsY() );
00121     editWidget->show();
00122 }
00123 
00124 void KoCustomVariablesListItem::setVariable( KoCustomVariable *v )
00125 {
00126     var = v;
00127     editWidget->setText( var->value() );
00128     setText( 0, v->name() );
00129 }
00130 
00131 KoCustomVariable *KoCustomVariablesListItem::getVariable() const
00132 {
00133     return var;
00134 }
00135 
00136 void KoCustomVariablesListItem::applyValue()
00137 {
00138     QString newVal=editWidget->text();
00139     if(var->value()!=newVal)
00140         var->setValue( newVal );
00141 }
00142 
00143 int KoCustomVariablesListItem::width( const QFontMetrics & fm, const QListView *lv, int c ) const
00144 {
00145     // The text of the 2nd column isn't known to QListViewItem, only we know it
00146     // (it's in our lineedit)
00147     if ( c == 1 ) {
00148         QString val = editWidget->text();
00149         int w = fm.width( val );
00150         return w;
00151     } else
00152         return QListViewItem::width( fm, lv, c );
00153 }
00154 
00155 /******************************************************************
00156  *
00157  * Class: KoCustomVariablesList
00158  *
00159  ******************************************************************/
00160 
00161 KoCustomVariablesList::KoCustomVariablesList( QWidget *parent )
00162     : KListView( parent )
00163 {
00164     header()->setMovingEnabled( FALSE );
00165     addColumn( i18n( "Variable" ) );
00166     addColumn( i18n( "Value" ) );
00167     connect( header(), SIGNAL( sizeChange( int, int, int ) ),
00168              this, SLOT( columnSizeChange( int, int, int ) ) );
00169     connect( header(), SIGNAL( sectionClicked( int ) ),
00170              this, SLOT( sectionClicked( int ) ) );
00171 
00172     setResizeMode(QListView::LastColumn);
00173     setSorting( -1 );
00174 }
00175 
00176 void KoCustomVariablesList::setValues()
00177 {
00178     QListViewItemIterator it( this );
00179     for ( ; it.current(); ++it )
00180         ( (KoCustomVariablesListItem *)it.current() )->applyValue();
00181 }
00182 
00183 void KoCustomVariablesList::columnSizeChange( int c, int, int )
00184 {
00185     if ( c == 0 || c == 1 )
00186         updateItems();
00187 }
00188 
00189 void KoCustomVariablesList::sectionClicked( int )
00190 {
00191     updateItems();
00192 }
00193 
00194 void KoCustomVariablesList::updateItems()
00195 {
00196     QListViewItemIterator it( this );
00197     for ( ; it.current(); ++it )
00198         ( (KoCustomVariablesListItem*)it.current() )->update();
00199 }
00200 
00201 /******************************************************************
00202  *
00203  * Class: KoCustomVariablesDia
00204  *
00205  ******************************************************************/
00206 
00207 KoCustomVariablesDia::KoCustomVariablesDia( QWidget *parent, const QPtrList<KoVariable> &variables )
00208     : KDialogBase( parent, "", TRUE,i18n( "Variable Value Editor" ), Ok|Cancel )
00209 {
00210 
00211     back = makeVBoxMainWidget();
00212 
00213     list = new KoCustomVariablesList( back );
00214 
00215     QStringList lst;
00216     QPtrListIterator<KoVariable> it( variables );
00217     for ( ; it.current() ; ++it ) {
00218         KoVariable *var = it.current();
00219         if ( var->type() == VT_CUSTOM ) {
00220             KoCustomVariable *v = (KoCustomVariable*)var;
00221             if ( !lst.contains( v->name() ) ) {
00222                 lst.append( v->name() );
00223                 KoCustomVariablesListItem *item = new KoCustomVariablesListItem( list );
00224                 item->setVariable( v );
00225             }
00226         }
00227     }
00228 
00229 
00230     connect( this, SIGNAL( okClicked() ),
00231              this, SLOT( slotOk() ) );
00232     connect( this, SIGNAL( cancelClicked() ),
00233              this, SLOT( reject() ) );
00234     showButtonOK(lst.count()>0);
00235 
00236     resize( 600, 400 );
00237 }
00238 
00239 void KoCustomVariablesDia::slotOk()
00240 {
00241     list->setValues();
00242     accept();
00243 }
00244 
00245 /******************************************************************
00246  *
00247  * Class: KoCustomVarDialog
00248  *
00249  ******************************************************************/
00250 
00251 KoCustomVarDialog::KoCustomVarDialog( QWidget *parent )
00252     : KDialogBase( parent, "", TRUE,i18n( "Add Variable" ), Ok|Cancel )
00253 {
00254     init();
00255     m_name->setFocus();
00256 
00257 
00258     connect( this, SIGNAL( okClicked() ),
00259              this, SLOT( slotAddOk() ) );
00260     connect( this, SIGNAL( cancelClicked() ),
00261              this, SLOT( reject() ) );
00262 
00263     connect( m_name, SIGNAL( textChanged(const QString&) ),
00264              this, SLOT( slotTextChanged(const QString&) ) );
00265 
00266     enableButtonOK( false );
00267     resize( 350, 100 );
00268 
00269 }
00270 // edit existing variable
00271 KoCustomVarDialog::KoCustomVarDialog( QWidget *parent, KoCustomVariable *var )
00272     : KDialogBase( parent, "", TRUE,i18n( "Edit Variable" ), Ok|Cancel )
00273 {
00274     m_var = var;
00275     init();
00276     m_name->setText( m_var->name() );
00277     m_value->setText( m_var->value() );
00278     m_name->setReadOnly(true);
00279     m_value->setFocus();
00280 
00281 
00282     connect( this, SIGNAL( okClicked() ),
00283              this, SLOT( slotEditOk() ) );
00284     connect( this, SIGNAL( cancelClicked() ),
00285              this, SLOT( reject() ) );
00286 
00287     connect( m_value, SIGNAL( textChanged(const QString&) ),
00288              this, SLOT( slotTextChanged(const QString&) ) );
00289 
00290     enableButtonOK( true );
00291     resize( 350, 100 );
00292 }
00293 
00294 void KoCustomVarDialog::init()
00295 {
00296     back = makeVBoxMainWidget();
00297     QHBox *row1 = new QHBox( back );
00298     row1->setSpacing( KDialog::spacingHint() );
00299     QLabel *ln = new QLabel( i18n( "Name:" ), row1 );
00300     ln->setFixedSize( ln->sizeHint() );
00301     m_name = new KLineEdit( row1 );
00302 
00303     QHBox *row2 = new QHBox( back );
00304     row2->setSpacing( KDialog::spacingHint() );
00305     QLabel *lv = new QLabel( i18n( "Value:" ), row2 );
00306     lv->setFixedSize( lv->sizeHint() );
00307     m_value = new KLineEdit( row2 );
00308 }
00309 
00310 void KoCustomVarDialog::slotAddOk()
00311 {
00312     accept();
00313 }
00314 void KoCustomVarDialog::slotEditOk()
00315 {
00316     m_var->setValue( m_value->text() );
00317     accept();
00318 }
00319 
00320 void KoCustomVarDialog::slotTextChanged(const QString&text)
00321 {
00322     enableButtonOK( !text.isEmpty() );
00323 }
00324 QString KoCustomVarDialog::name()
00325 {
00326     if ( m_name->text().isEmpty() )
00327         return QString( "No name" );
00328     return m_name->text();
00329 }
00330 
00331 QString KoCustomVarDialog::value()
00332 {
00333     if ( m_value->text().isEmpty() )
00334         return QString( "No value" );
00335     return m_value->text();
00336 }
KDE Home | KDE Accessibility Home | Description of Access Keys