kspread Library API Documentation

kspread_dlg_formula.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002-2003 Ariya Hidayat <ariya@kde.org>
00003              (C) 2002-2003 Norbert Andres <nandres@web.de>
00004              (C) 1999-2003 Laurent Montel <montel@kde.org>
00005              (C) 2002 Philipp Mueller <philipp.mueller@gmx.de>
00006              (C) 2002 John Dailey <dailey@vt.edu>
00007              (C) 2002 Daniel Herring <herring@eecs.ku.edu>
00008              (C) 2000-2001 Werner Trobin <trobin@kde.org>
00009              (C) 1998-2000 Torben Weis <weis@kde.org>
00010 
00011    This library is free software; you can redistribute it and/or
00012    modify it under the terms of the GNU Library General Public
00013    License as published by the Free Software Foundation; either
00014    version 2 of the License, or (at your option) any later version.
00015 
00016    This library is distributed in the hope that it will be useful,
00017    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019    Library General Public License for more details.
00020 
00021    You should have received a copy of the GNU Library General Public License
00022    along with this library; see the file COPYING.LIB.  If not, write to
00023    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00024    Boston, MA 02111-1307, USA.
00025 */
00026 
00027 #include <qtextbrowser.h>
00028 #include <qtabwidget.h>
00029 
00030 #include "kspread_dlg_formula.h"
00031 #include "kspread_canvas.h"
00032 #include "kspread_util.h"
00033 #include "kspread_editors.h"
00034 #include "kspread_doc.h"
00035 #include "kspread_locale.h"
00036 #include "kspread_map.h"
00037 #include "kspread_selection.h"
00038 #include "kspread_view.h"
00039 #include "kspread_functions.h"
00040 
00041 #include <kapplication.h>
00042 #include <kdebug.h>
00043 #include <kbuttonbox.h>
00044 #include <knumvalidator.h>
00045 #include <qcombobox.h>
00046 #include <qevent.h>
00047 #include <qlistbox.h>
00048 #include <qlabel.h>
00049 #include <qpushbutton.h>
00050 #include <klineedit.h>
00051 #include <qlayout.h>
00052 
00053 KSpreadDlgFormula::KSpreadDlgFormula( KSpreadView* parent, const char* name,const QString& formulaName)
00054     : KDialogBase( parent, name,false,i18n("Function"), Ok|Cancel )
00055 {
00056   setWFlags( Qt::WDestructiveClose );
00057 
00058     m_pView = parent;
00059     m_focus = 0;
00060     m_desc = 0;
00061 
00062     KSpreadCell* cell = m_pView->activeSheet()->cellAt( m_pView->canvasWidget()->markerColumn(),
00063                             m_pView->canvasWidget()->markerRow() );
00064      m_oldText=cell->text();
00065     // Make sure that there is a cell editor running.
00066     if ( !m_pView->canvasWidget()->editor() )
00067     {
00068         m_pView->canvasWidget()->createEditor( KSpreadCanvas::CellEditor );
00069         if(cell->text().isEmpty())
00070           m_pView->canvasWidget()->editor()->setText( "=" );
00071         else
00072           if(cell->text().at(0)!='=')
00073             m_pView->canvasWidget()->editor()->setText( "="+cell->text() );
00074           else
00075             m_pView->canvasWidget()->editor()->setText( cell->text() );
00076     }
00077 
00078     Q_ASSERT( m_pView->canvasWidget()->editor() );
00079 
00080     QWidget *page = new QWidget( this );
00081     setMainWidget(page);
00082 
00083     QGridLayout *grid1 = new QGridLayout(page,11,2,KDialog::marginHint(), KDialog::spacingHint());
00084 
00085     searchFunct = new KLineEdit(page);
00086     QSizePolicy sp3( QSizePolicy::Preferred, QSizePolicy::Fixed );
00087     searchFunct->setSizePolicy( sp3 );
00088 
00089     grid1->addWidget( searchFunct, 0, 0 );
00090 
00091     typeFunction = new QComboBox(page);
00092     QStringList cats = KSpreadFunctionRepository::self()->groups();
00093     cats.prepend( i18n("All") );
00094     typeFunction->insertStringList( cats  );
00095     grid1->addWidget( typeFunction, 1, 0 );
00096 
00097     functions = new QListBox(page);
00098     QSizePolicy sp1( QSizePolicy::Preferred, QSizePolicy::Expanding );
00099     functions->setSizePolicy( sp1 );
00100     grid1->addWidget( functions, 2, 0 );
00101 
00102     selectFunction = new QPushButton( page );
00103     QToolTip::add(selectFunction, i18n("Insert function") );
00104     selectFunction->setPixmap( BarIcon( "down", KIcon::SizeSmall ) );
00105     grid1->addWidget( selectFunction, 3, 0 );
00106 
00107     result = new QLineEdit( page );
00108     grid1->addMultiCellWidget( result, 4, 4, 0, 1 );
00109 
00110     m_tabwidget = new QTabWidget( page );
00111     QSizePolicy sp2( QSizePolicy::Expanding, QSizePolicy::Expanding );
00112     m_tabwidget->setSizePolicy( sp2 );
00113     grid1->addMultiCellWidget( m_tabwidget, 0, 2, 1, 1 );
00114 
00115     m_browser = new QTextBrowser( m_tabwidget );
00116     m_browser->setMinimumWidth( 300 );
00117 
00118     m_tabwidget->addTab( m_browser, i18n("&Help") );
00119     int index = m_tabwidget->currentPageIndex();
00120 
00121     m_input = new QWidget( m_tabwidget );
00122     QVBoxLayout *grid2 = new QVBoxLayout( m_input, KDialog::marginHint(), KDialog::spacingHint() );
00123 
00124     // grid2->setResizeMode (QLayout::Minimum);
00125 
00126     label1 = new QLabel(m_input);
00127     grid2->addWidget( label1 );
00128 
00129     firstElement=new QLineEdit(m_input);
00130     grid2->addWidget( firstElement );
00131 
00132     label2=new QLabel(m_input);
00133     grid2->addWidget( label2 );
00134 
00135     secondElement=new QLineEdit(m_input);
00136     grid2->addWidget( secondElement );
00137 
00138     label3=new QLabel(m_input);
00139     grid2->addWidget( label3 );
00140 
00141     thirdElement=new QLineEdit(m_input);
00142     grid2->addWidget( thirdElement );
00143 
00144     label4=new QLabel(m_input);
00145     grid2->addWidget( label4 );
00146 
00147     fourElement=new QLineEdit(m_input);
00148     grid2->addWidget( fourElement );
00149 
00150     label5=new QLabel(m_input);
00151     grid2->addWidget( label5 );
00152 
00153     fiveElement=new QLineEdit(m_input);
00154     grid2->addWidget( fiveElement );
00155 
00156     grid2->addStretch( 10 );
00157 
00158     m_tabwidget->addTab( m_input, i18n("&Parameters") );
00159     m_tabwidget->setTabEnabled( m_input, FALSE );
00160 
00161     m_tabwidget->setCurrentPage( index );
00162 
00163     refresh_result = true;
00164 
00165     connect( this, SIGNAL( cancelClicked() ), this, SLOT( slotClose() ) );
00166     connect( this, SIGNAL( okClicked() ), this, SLOT( slotOk() ) );
00167     connect( typeFunction, SIGNAL( activated(const QString &) ),
00168              this, SLOT( slotActivated(const QString &) ) );
00169     connect( functions, SIGNAL( highlighted(const QString &) ),
00170              this, SLOT( slotSelected(const QString &) ) );
00171     connect( functions, SIGNAL( selected(const QString &) ),
00172              this, SLOT( slotSelected(const QString &) ) );
00173     connect( functions, SIGNAL( doubleClicked(QListBoxItem * ) ),
00174              this ,SLOT( slotDoubleClicked(QListBoxItem *) ) );
00175 
00176     slotActivated(i18n("All"));
00177 
00178     connect( selectFunction, SIGNAL(clicked()),
00179          this,SLOT(slotSelectButton()));
00180 
00181     connect( firstElement,SIGNAL(textChanged ( const QString & )),
00182          this,SLOT(slotChangeText(const QString &)));
00183     connect( secondElement,SIGNAL(textChanged ( const QString & )),
00184          this,SLOT(slotChangeText(const QString &)));
00185     connect( thirdElement,SIGNAL(textChanged ( const QString & )),
00186          this,SLOT(slotChangeText(const QString &)));
00187     connect( fourElement,SIGNAL(textChanged ( const QString & )),
00188          this,SLOT(slotChangeText(const QString &)));
00189     connect( fiveElement,SIGNAL(textChanged ( const QString & )),
00190          this,SLOT(slotChangeText(const QString &)));
00191 
00192     connect( m_pView, SIGNAL( sig_chooseSelectionChanged( KSpreadSheet*, const QRect& ) ),
00193              this, SLOT( slotSelectionChanged( KSpreadSheet*, const QRect& ) ) );
00194 
00195     connect( m_browser, SIGNAL( linkClicked( const QString& ) ),
00196              this, SLOT( slotShowFunction( const QString& ) ) );
00197 
00198     // Save the name of the active sheet.
00199     m_sheetName = m_pView->activeSheet()->sheetName();
00200     // Save the cells current text.
00201     QString tmp_oldText = m_pView->canvasWidget()->editor()->text();
00202     // Position of the cell.
00203     m_column = m_pView->canvasWidget()->markerColumn();
00204     m_row = m_pView->canvasWidget()->markerRow();
00205 
00206     if( tmp_oldText.isEmpty() )
00207         result->setText("=");
00208     else
00209     {
00210         if( tmp_oldText.at(0)!='=')
00211         result->setText( "=" + tmp_oldText );
00212         else
00213         result->setText( tmp_oldText );
00214     }
00215 
00216     // Allow the user to select cells on the spreadsheet.
00217     m_pView->canvasWidget()->startChoose();
00218 
00219     qApp->installEventFilter( this );
00220 
00221     // Was a function name passed along with the constructor ? Then activate it.
00222     if( !formulaName.isEmpty() )
00223     {
00224         functions->setCurrentItem( functions->index( functions->findItem( formulaName ) ) );
00225         slotDoubleClicked( functions->findItem( formulaName ) );
00226     }
00227     else
00228     {
00229     // Set keyboard focus to allow selection of a formula.
00230         searchFunct->setFocus();
00231     }
00232 
00233     // Add auto completion.
00234     searchFunct->setCompletionMode( KGlobalSettings::CompletionAuto );
00235     searchFunct->setCompletionObject( &listFunct, true );
00236 
00237     if( functions->currentItem() == -1 )
00238         selectFunction->setEnabled( false );
00239 
00240     connect( searchFunct, SIGNAL( textChanged( const QString & ) ),
00241          this, SLOT( slotSearchText(const QString &) ) );
00242     connect( searchFunct, SIGNAL( returnPressed() ),
00243          this, SLOT( slotPressReturn() ) );
00244 }
00245 
00246 KSpreadDlgFormula::~KSpreadDlgFormula()
00247 {
00248     kdDebug(36001)<<"KSpreadDlgFormula::~KSpreadDlgFormula() \n";
00249 }
00250 
00251 
00252 void KSpreadDlgFormula::slotPressReturn()
00253 {
00254     //laurent 2001-07-07 desactivate this code
00255     //because kspread crash.
00256     //TODO fix it
00257     /*
00258     if( !functions->currentText().isEmpty() )
00259         slotDoubleClicked( functions->findItem( functions->currentText() ) );
00260     */
00261 }
00262 
00263 void KSpreadDlgFormula::slotSearchText(const QString &_text)
00264 {
00265     QString result = listFunct.makeCompletion( _text.upper() );
00266     if( !result.isNull() )
00267         functions->setCurrentItem( functions->index( functions->findItem( result ) ) );
00268 }
00269 
00270 bool KSpreadDlgFormula::eventFilter( QObject* obj, QEvent* ev )
00271 {
00272     if ( obj == firstElement && ev->type() == QEvent::FocusIn )
00273         m_focus = firstElement;
00274     else if ( obj == secondElement && ev->type() == QEvent::FocusIn )
00275         m_focus = secondElement;
00276     else if ( obj == thirdElement && ev->type() == QEvent::FocusIn )
00277         m_focus = thirdElement;
00278     else if ( obj == fourElement && ev->type() == QEvent::FocusIn )
00279         m_focus = fourElement;
00280     else if ( obj == fiveElement && ev->type() == QEvent::FocusIn )
00281         m_focus = fiveElement;
00282     else
00283         return FALSE;
00284 
00285     if ( m_focus )
00286         m_pView->canvasWidget()->startChoose();
00287 
00288     return FALSE;
00289 }
00290 
00291 void KSpreadDlgFormula::slotOk()
00292 {
00293     m_pView->doc()->emitBeginOperation( false );
00294 
00295     m_pView->canvasWidget()->endChoose();
00296     // Switch back to the old sheet
00297     if( m_pView->activeSheet()->sheetName() !=  m_sheetName )
00298     {
00299         KSpreadSheet *sheet=m_pView->doc()->map()->findSheet(m_sheetName);
00300         if( sheet)
00301         m_pView->setActiveSheet(sheet);
00302     }
00303 
00304     // Revert the marker to its original position
00305     m_pView->selectionInfo()->setMarker( QPoint( m_column, m_row ),
00306                                          m_pView->activeSheet());
00307 
00308     // If there is still an editor then set the text.
00309     // Usually the editor is always in place.
00310     if( m_pView->canvasWidget()->editor() != 0 )
00311     {
00312         Q_ASSERT( m_pView->canvasWidget()->editor() );
00313         QString tmp = result->text();
00314         if( tmp.at(0) != '=')
00315         tmp = "=" + tmp;
00316         int pos = m_pView->canvasWidget()->editor()->cursorPosition()+ tmp.length();
00317         m_pView->canvasWidget()->editor()->setText( tmp );
00318         m_pView->canvasWidget()->editor()->setFocus();
00319         m_pView->canvasWidget()->editor()->setCursorPosition( pos );
00320     }
00321 
00322     m_pView->slotUpdateView( m_pView->activeSheet() );
00323     accept();
00324     //  delete this;
00325 }
00326 
00327 void KSpreadDlgFormula::slotClose()
00328 {
00329     m_pView->doc()->emitBeginOperation( false );
00330 
00331     m_pView->canvasWidget()->endChoose();
00332 
00333     // Switch back to the old sheet
00334     if(m_pView->activeSheet()->sheetName() !=  m_sheetName )
00335     {
00336         KSpreadSheet *sheet=m_pView->doc()->map()->findSheet(m_sheetName);
00337         if( !sheet )
00338         return;
00339     m_pView->setActiveSheet(sheet);
00340     }
00341 
00342 
00343     // Revert the marker to its original position
00344     m_pView->selectionInfo()->setMarker( QPoint( m_column, m_row ),
00345                                          m_pView->activeSheet() );
00346 
00347     // If there is still an editor then reset the text.
00348     // Usually the editor is always in place.
00349     if( m_pView->canvasWidget()->editor() != 0 )
00350     {
00351         Q_ASSERT( m_pView->canvasWidget()->editor() );
00352         m_pView->canvasWidget()->editor()->setText( m_oldText );
00353         m_pView->canvasWidget()->editor()->setFocus();
00354     }
00355 
00356     m_pView->slotUpdateView( m_pView->activeSheet() );
00357     reject();
00358     //laurent 2002-01-03 comment this line otherwise kspread crash
00359     //but dialog box is not deleted => not good
00360     //delete this;
00361 }
00362 
00363 void KSpreadDlgFormula::slotSelectButton()
00364 {
00365     if( functions->currentItem() != -1 )
00366     {
00367         slotDoubleClicked(functions->findItem(functions->text(functions->currentItem())));
00368     }
00369 }
00370 
00371 void KSpreadDlgFormula::slotChangeText( const QString& )
00372 {
00373     // Test the lock
00374     if( !refresh_result )
00375     return;
00376 
00377     if ( m_focus == 0 )
00378     return;
00379 
00380     QString tmp = m_leftText+m_funcName+"(";
00381     tmp += createFormula();
00382     tmp = tmp+ ")" + m_rightText;
00383 
00384     result->setText( tmp );
00385 }
00386 
00387 QString KSpreadDlgFormula::createFormula()
00388 {
00389     QString tmp( "" );
00390 
00391     if ( !m_desc )
00392     return QString::null;
00393 
00394     bool first = TRUE;
00395 
00396     int count = m_desc->params();
00397 
00398     if(!firstElement->text().isEmpty() && count >= 1 )
00399     {
00400         tmp=tmp+createParameter(firstElement->text(), 0 );
00401     first = FALSE;
00402     }
00403 
00404     if(!secondElement->text().isEmpty() && count >= 2 )
00405     {
00406     first = FALSE;
00407     if ( !first )
00408         tmp=tmp+";"+createParameter(secondElement->text(), 1 );
00409     else
00410         tmp=tmp+createParameter(secondElement->text(), 1 );
00411     }
00412     if(!thirdElement->text().isEmpty() && count >= 3 )
00413     {
00414     first = FALSE;
00415     if ( !first )
00416         tmp=tmp+";"+createParameter(thirdElement->text(), 2 );
00417         else
00418         tmp=tmp+createParameter(thirdElement->text(), 2 );
00419     }
00420     if(!fourElement->text().isEmpty() && count >= 4 )
00421     {
00422     first = FALSE;
00423     if ( !first )
00424         tmp=tmp+";"+createParameter(fourElement->text(), 3 );
00425         else
00426         tmp=tmp+createParameter(fourElement->text(), 3 );
00427     }
00428     if(!fiveElement->text().isEmpty() && count >= 5 )
00429     {
00430     first = FALSE;
00431     if ( !first )
00432         tmp=tmp+";"+createParameter(fiveElement->text(), 4 );
00433         else
00434         tmp=tmp+createParameter(fiveElement->text(), 4 );
00435     }
00436 
00437     return(tmp);
00438 }
00439 
00440 QString KSpreadDlgFormula::createParameter( const QString& _text, int param )
00441 {
00442     if ( _text.isEmpty() )
00443     return QString( "" );
00444 
00445     if ( !m_desc )
00446     return QString( "" );
00447 
00448     QString text;
00449 
00450     KSpreadParameterType elementType = m_desc->param( param ).type();
00451 
00452     switch( elementType )
00453     {
00454     case KSpread_Any:
00455     {
00456         bool isNumber;
00457         double tmp = m_pView->doc()->locale()->readNumber( _text, &isNumber );
00458         Q_UNUSED( tmp );
00459 
00460         //In case of number or boolean return _text, else return value as KSpread_String
00461         if ( isNumber || _text.upper() =="FALSE" || _text.upper() == "TRUE" )
00462             return _text;
00463     }
00464     case KSpread_String:
00465     {
00466         // Does the text start with quotes?
00467         if ( _text[0] == '"' )
00468         {
00469             text = "\\"; // changed: was "\""
00470 
00471         // Escape quotes
00472         QString tmp = _text;
00473         int pos;
00474         int start = 1;
00475         while( ( pos = tmp.find( '"', start ) ) != -1 )
00476         {
00477           if (tmp[pos - 1] != '\\')
00478             tmp.replace( pos, 1, "\\\"" );
00479           else
00480             start = pos + 1;
00481         }
00482 
00483         text += tmp;
00484         text += "\"";
00485         }
00486         else
00487         {
00488         KSpreadPoint p = KSpreadPoint( _text, m_pView->doc()->map() );
00489         KSpreadRange r = KSpreadRange( _text, m_pView->doc()->map() );
00490 
00491         if( !p.isValid() && !r.isValid() )
00492         {
00493             text = "\"";
00494 
00495             // Escape quotes
00496             QString tmp = _text;
00497             int pos;
00498             int start = 1;
00499             while( ( pos = tmp.find( '"', start ) ) != -1 )
00500             {
00501                 if (tmp[pos - 1] != '\\')
00502               tmp.replace( pos, 1, "\\\"" );
00503             else
00504               start = pos + 1;
00505             }
00506 
00507             text += tmp;
00508             text += "\"";
00509         }
00510         else
00511             text = _text;
00512             }
00513         }
00514     return text;
00515     case KSpread_Float:
00516     return _text;
00517     case KSpread_Boolean:
00518     return _text;
00519     case KSpread_Int:
00520     return _text;
00521     }
00522 
00523     // Never reached
00524     return text;
00525 }
00526 
00527 static void showEntry( QLineEdit* edit, QLabel* label, KSpreadFunctionDescription* desc, int param )
00528 {
00529     edit->show();
00530     label->setText( desc->param( param ).helpText()+":" );
00531     label->show();
00532     KSpreadParameterType elementType = desc->param( param ).type();
00533     KFloatValidator *validate=0L;
00534     switch( elementType )
00535     {
00536     case KSpread_String:
00537     case KSpread_Boolean:
00538     case KSpread_Any:
00539       edit->clearValidator ();
00540       break;
00541     case KSpread_Float:
00542         validate=new KFloatValidator (edit);
00543         validate->setAcceptLocalizedNumbers(true);
00544         edit->setValidator(validate);
00545         edit->setText( "0" );
00546       break;
00547     case KSpread_Int:
00548       edit->setValidator(new QIntValidator (edit));
00549       edit->setText( "0" );
00550       break;
00551       }
00552 
00553 }
00554 
00555 void KSpreadDlgFormula::slotDoubleClicked( QListBoxItem* item )
00556 {
00557     if ( !item )
00558     return;
00559     refresh_result = false;
00560     if ( !m_desc )
00561     {
00562     m_browser->setText( "" );
00563     return;
00564     }
00565 
00566     m_focus = 0;
00567     int old_length = result->text().length();
00568 
00569     // Dont change order of these function calls due to a bug in Qt 2.2
00570     m_browser->setText( m_desc->toQML() );
00571     m_tabwidget->setTabEnabled( m_input, TRUE );
00572     m_tabwidget->setCurrentPage( 1 );
00573 
00574     //
00575     // Show as many QLineEdits as needed.
00576     //
00577     if( m_desc->params() > 0 )
00578     {
00579         m_focus = firstElement;
00580         firstElement->setFocus();
00581 
00582     showEntry( firstElement, label1, m_desc, 0 );
00583     }
00584     else
00585     {
00586     label1->hide();
00587     firstElement->hide();
00588     }
00589 
00590     if( m_desc->params() > 1 )
00591     {
00592     showEntry( secondElement, label2, m_desc, 1 );
00593     }
00594     else
00595     {
00596     label2->hide();
00597     secondElement->hide();
00598     }
00599 
00600     if( m_desc->params() > 2 )
00601     {
00602     showEntry( thirdElement, label3, m_desc, 2 );
00603     }
00604     else
00605     {
00606     label3->hide();
00607     thirdElement->hide();
00608     }
00609 
00610     if( m_desc->params() > 3 )
00611     {
00612     showEntry( fourElement, label4, m_desc, 3 );
00613     }
00614     else
00615     {
00616     label4->hide();
00617     fourElement->hide();
00618     }
00619 
00620     if( m_desc->params() > 4 )
00621     {
00622     showEntry( fiveElement, label5, m_desc, 4 );
00623     }
00624     else
00625     {
00626     label5->hide();
00627     fiveElement->hide();
00628     }
00629 
00630     if( m_desc->params() > 5 )
00631     {
00632         kdDebug(36001) << "Error in param->nb_param" << endl;
00633     }
00634     refresh_result= true;
00635     //
00636     // Put the new function call in the result.
00637     //
00638     if( result->cursorPosition() < old_length )
00639     {
00640         m_rightText=result->text().right(old_length-result->cursorPosition());
00641         m_leftText=result->text().left(result->cursorPosition());
00642     }
00643     else
00644     {
00645         m_rightText="";
00646         m_leftText=result->text();
00647     }
00648     int pos = result->cursorPosition();
00649     result->setText( m_leftText+functions->text( functions->currentItem() ) + "()" + m_rightText);
00650 
00651     if (result->text()[0] != '=')
00652       result->setText("=" + result->text());
00653 
00654     //
00655     // Put focus somewhere is there are no QLineEdits visible
00656     //
00657     if( m_desc->params() == 0 )
00658     {
00659     label1->show();
00660     label1->setText( i18n("This function has no parameters.") );
00661 
00662         result->setFocus();
00663         result->setCursorPosition(pos+functions->text(functions->currentItem()).length()+2);
00664     }
00665     slotChangeText( "" );
00666 }
00667 
00668 void KSpreadDlgFormula::slotSelected( const QString& function )
00669 {
00670     KSpreadFunctionDescription* desc =
00671        KSpreadFunctionRepository::self()->functionInfo( function );
00672     if ( !desc )
00673     {
00674     m_browser->setText( "" );
00675     return;
00676     }
00677 
00678     if( functions->currentItem() !=- 1 )
00679         selectFunction->setEnabled( TRUE );
00680 
00681     // Lock
00682     refresh_result = false;
00683 
00684     m_funcName = function;
00685     m_desc = desc;
00686 
00687     // Set the help text
00688     m_browser->setText( m_desc->toQML() );
00689     m_browser->setContentsPos( 0, 0 );
00690 
00691     m_focus=0;
00692 
00693     m_tabwidget->setCurrentPage( 0 );
00694     m_tabwidget->setTabEnabled( m_input, FALSE );
00695 
00696     // Unlock
00697     refresh_result=true;
00698 }
00699 
00700 // from hyperlink in the "Related Function"
00701 void KSpreadDlgFormula::slotShowFunction( const QString& function )
00702 {
00703     KSpreadFunctionDescription* desc =
00704        KSpreadFunctionRepository::self()->functionInfo( function );
00705     if ( !desc ) return;
00706 
00707     // select the category
00708     QString category = desc->group();
00709     typeFunction->setCurrentText( category );
00710     slotActivated( category );
00711 
00712     // select the function
00713     QListBoxItem* item = functions->findItem( function,
00714       Qt::ExactMatch | Qt::CaseSensitive );
00715     if( item ) functions->setCurrentItem( item );
00716 
00717     slotSelected( function );
00718 }
00719 
00720 void KSpreadDlgFormula::slotSelectionChanged( KSpreadSheet* _sheet, const QRect& _selection )
00721 {
00722     if ( !m_focus )
00723         return;
00724 
00725     if ( _selection.left() == 0 )
00726         return;
00727 
00728     if ( _selection.left() >= _selection.right() && _selection.top() >= _selection.bottom() )
00729     {
00730         int dx = _selection.right();
00731         int dy = _selection.bottom();
00732         QString tmp;
00733         tmp.setNum( dy );
00734         tmp = _sheet->sheetName() + "!" + KSpreadCell::columnName( dx ) + tmp;
00735         m_focus->setText( tmp );
00736     }
00737     else
00738     {
00739         QString area = util_rangeName( _sheet, _selection );
00740         m_focus->setText( area );
00741     }
00742 }
00743 
00744 void KSpreadDlgFormula::slotActivated( const QString& category )
00745 {
00746     QStringList lst;
00747     if ( category == i18n("All") )
00748     lst = KSpreadFunctionRepository::self()->functionNames();
00749     else
00750     lst = KSpreadFunctionRepository::self()->functionNames( category );
00751 
00752     kdDebug(36001)<<"category: "<<category<<" ("<<lst.count()<<"functions)" << endl;
00753 
00754     functions->clear();
00755     functions->insertStringList( lst );
00756 
00757     QStringList upperList;
00758     for ( QStringList::Iterator it = lst.begin(); it != lst.end();++it )
00759       upperList.append((*it).upper());
00760 
00761     listFunct.setItems( upperList );
00762 
00763     // Go to the first function in the list.
00764     functions->setCurrentItem(0);
00765     slotSelected( functions->text(0) );
00766 }
00767 
00768 void KSpreadDlgFormula::closeEvent ( QCloseEvent * e )
00769 {
00770   e->accept();
00771 }
00772 
00773 #include "kspread_dlg_formula.moc"
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:42:54 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003