27#include <tqtextbrowser.h>
28#include <tqtabwidget.h>
30#include "kspread_dlg_formula.h"
31#include "kspread_canvas.h"
32#include "kspread_util.h"
33#include "kspread_editors.h"
34#include "kspread_doc.h"
35#include "kspread_locale.h"
36#include "kspread_map.h"
38#include "kspread_sheet.h"
39#include "kspread_view.h"
42#include <tdeapplication.h>
44#include <kbuttonbox.h>
45#include <knumvalidator.h>
46#include <tqcombobox.h>
50#include <tqpushbutton.h>
56FormulaDialog::FormulaDialog(
View* parent,
const char* name,
const TQString& formulaName)
57 : KDialogBase( parent, name,false,i18n(
"Function"), Ok|Cancel )
59 setWFlags( TQt::WDestructiveClose );
65 Cell* cell = m_pView->activeSheet()->cellAt( m_pView->canvasWidget()->markerColumn(),
66 m_pView->canvasWidget()->markerRow() );
67 m_oldText=cell->
text();
69 if ( !m_pView->canvasWidget()->editor() )
71 m_pView->canvasWidget()->createEditor( Canvas::CellEditor );
72 if(cell->text().isEmpty())
73 m_pView->canvasWidget()->editor()->setText(
"=" );
75 if(cell->text().at(0)!=
'=')
76 m_pView->canvasWidget()->editor()->setText(
"="+cell->text() );
78 m_pView->canvasWidget()->editor()->setText( cell->text() );
81 Q_ASSERT( m_pView->canvasWidget()->editor() );
83 TQWidget *page =
new TQWidget(
this );
86 TQGridLayout *grid1 =
new TQGridLayout(page,11,2,KDialog::marginHint(), KDialog::spacingHint());
88 searchFunct =
new KLineEdit(page);
89 TQSizePolicy sp3( TQSizePolicy::Preferred, TQSizePolicy::Fixed );
90 searchFunct->setSizePolicy( sp3 );
92 grid1->addWidget( searchFunct, 0, 0 );
94 typeFunction =
new TQComboBox(page);
95 TQStringList cats = FunctionRepository::self()->groups();
96 cats.prepend( i18n(
"All") );
97 typeFunction->insertStringList( cats );
98 grid1->addWidget( typeFunction, 1, 0 );
100 functions =
new TQListBox(page);
101 TQSizePolicy sp1( TQSizePolicy::Preferred, TQSizePolicy::Expanding );
102 functions->setSizePolicy( sp1 );
103 grid1->addWidget( functions, 2, 0 );
105 selectFunction =
new TQPushButton( page );
106 TQToolTip::add(selectFunction, i18n(
"Insert function") );
107 selectFunction->setPixmap( BarIcon(
"go-down", TDEIcon::SizeSmall ) );
108 grid1->addWidget( selectFunction, 3, 0 );
110 result =
new TQLineEdit( page );
111 grid1->addMultiCellWidget( result, 4, 4, 0, 1 );
113 m_tabwidget =
new TQTabWidget( page );
114 TQSizePolicy sp2( TQSizePolicy::Expanding, TQSizePolicy::Expanding );
115 m_tabwidget->setSizePolicy( sp2 );
116 grid1->addMultiCellWidget( m_tabwidget, 0, 2, 1, 1 );
118 m_browser =
new TQTextBrowser( m_tabwidget );
119 m_browser->setMinimumWidth( 300 );
121 m_tabwidget->addTab( m_browser, i18n(
"&Help") );
122 int index = m_tabwidget->currentPageIndex();
124 m_input =
new TQWidget( m_tabwidget );
125 TQVBoxLayout *grid2 =
new TQVBoxLayout( m_input, KDialog::marginHint(), KDialog::spacingHint() );
129 label1 =
new TQLabel(m_input);
130 grid2->addWidget( label1 );
132 firstElement=
new TQLineEdit(m_input);
133 grid2->addWidget( firstElement );
135 label2=
new TQLabel(m_input);
136 grid2->addWidget( label2 );
138 secondElement=
new TQLineEdit(m_input);
139 grid2->addWidget( secondElement );
141 label3=
new TQLabel(m_input);
142 grid2->addWidget( label3 );
144 thirdElement=
new TQLineEdit(m_input);
145 grid2->addWidget( thirdElement );
147 label4=
new TQLabel(m_input);
148 grid2->addWidget( label4 );
150 fourElement=
new TQLineEdit(m_input);
151 grid2->addWidget( fourElement );
153 label5=
new TQLabel(m_input);
154 grid2->addWidget( label5 );
156 fiveElement=
new TQLineEdit(m_input);
157 grid2->addWidget( fiveElement );
159 grid2->addStretch( 10 );
161 m_tabwidget->addTab( m_input, i18n(
"&Parameters") );
162 m_tabwidget->setTabEnabled( m_input,
false );
164 m_tabwidget->setCurrentPage( index );
166 refresh_result =
true;
168 connect(
this, TQ_SIGNAL( cancelClicked() ),
this, TQ_SLOT( slotClose() ) );
169 connect(
this, TQ_SIGNAL( okClicked() ),
this, TQ_SLOT( slotOk() ) );
170 connect( typeFunction, TQ_SIGNAL( activated(
const TQString &) ),
171 this, TQ_SLOT( slotActivated(
const TQString &) ) );
172 connect( functions, TQ_SIGNAL( highlighted(
const TQString &) ),
173 this, TQ_SLOT( slotSelected(
const TQString &) ) );
174 connect( functions, TQ_SIGNAL( selected(
const TQString &) ),
175 this, TQ_SLOT( slotSelected(
const TQString &) ) );
176 connect( functions, TQ_SIGNAL( doubleClicked(TQListBoxItem * ) ),
177 this ,TQ_SLOT( slotDoubleClicked(TQListBoxItem *) ) );
179 slotActivated(i18n(
"All"));
181 connect( selectFunction, TQ_SIGNAL(clicked()),
182 this,TQ_SLOT(slotSelectButton()));
184 connect( firstElement,TQ_SIGNAL(textChanged (
const TQString & )),
185 this,TQ_SLOT(slotChangeText(
const TQString &)));
186 connect( secondElement,TQ_SIGNAL(textChanged (
const TQString & )),
187 this,TQ_SLOT(slotChangeText(
const TQString &)));
188 connect( thirdElement,TQ_SIGNAL(textChanged (
const TQString & )),
189 this,TQ_SLOT(slotChangeText(
const TQString &)));
190 connect( fourElement,TQ_SIGNAL(textChanged (
const TQString & )),
191 this,TQ_SLOT(slotChangeText(
const TQString &)));
192 connect( fiveElement,TQ_SIGNAL(textChanged (
const TQString & )),
193 this,TQ_SLOT(slotChangeText(
const TQString &)));
195 connect( m_pView->choice(), TQ_SIGNAL(changed(
const Region&)),
196 this, TQ_SLOT(slotSelectionChanged()));
198 connect( m_browser, TQ_SIGNAL( linkClicked(
const TQString& ) ),
199 this, TQ_SLOT( slotShowFunction(
const TQString& ) ) );
202 m_sheetName = m_pView->activeSheet()->sheetName();
204 TQString tmp_oldText = m_pView->canvasWidget()->editor()->text();
206 m_column = m_pView->canvasWidget()->markerColumn();
207 m_row = m_pView->canvasWidget()->markerRow();
209 if( tmp_oldText.isEmpty() )
210 result->setText(
"=");
213 if( tmp_oldText.at(0)!=
'=')
214 result->setText(
"=" + tmp_oldText );
216 result->setText( tmp_oldText );
220 m_pView->canvasWidget()->startChoose();
222 tqApp->installEventFilter(
this );
225 if( !formulaName.isEmpty() )
227 functions->setCurrentItem( functions->index( functions->findItem( formulaName ) ) );
228 slotDoubleClicked( functions->findItem( formulaName ) );
233 searchFunct->setFocus();
237 searchFunct->setCompletionMode( TDEGlobalSettings::CompletionAuto );
238 searchFunct->setCompletionObject( &listFunct,
true );
240 if( functions->currentItem() == -1 )
241 selectFunction->setEnabled(
false );
243 connect( searchFunct, TQ_SIGNAL( textChanged(
const TQString & ) ),
244 this, TQ_SLOT( slotSearchText(
const TQString &) ) );
245 connect( searchFunct, TQ_SIGNAL( returnPressed() ),
246 this, TQ_SLOT( slotPressReturn() ) );
249FormulaDialog::~FormulaDialog()
251 kdDebug(36001)<<
"FormulaDialog::~FormulaDialog() \n";
254void FormulaDialog::slotPressReturn()
265void FormulaDialog::slotSearchText(
const TQString &_text)
267 TQString result = listFunct.makeCompletion( _text.upper() );
268 if( !result.isNull() )
269 functions->setCurrentItem( functions->index( functions->findItem( result ) ) );
272bool FormulaDialog::eventFilter( TQObject* obj, TQEvent* ev )
274 if ( obj == firstElement && ev->type() == TQEvent::FocusIn )
275 m_focus = firstElement;
276 else if ( obj == secondElement && ev->type() == TQEvent::FocusIn )
277 m_focus = secondElement;
278 else if ( obj == thirdElement && ev->type() == TQEvent::FocusIn )
279 m_focus = thirdElement;
280 else if ( obj == fourElement && ev->type() == TQEvent::FocusIn )
281 m_focus = fourElement;
282 else if ( obj == fiveElement && ev->type() == TQEvent::FocusIn )
283 m_focus = fiveElement;
288 m_pView->canvasWidget()->startChoose();
293void FormulaDialog::slotOk()
295 m_pView->doc()->emitBeginOperation(
false );
297 m_pView->canvasWidget()->endChoose();
299 if( m_pView->activeSheet()->sheetName() != m_sheetName )
301 Sheet *sheet=m_pView->doc()->map()->
findSheet(m_sheetName);
303 m_pView->setActiveSheet(sheet);
307 m_pView->selectionInfo()->initialize( TQPoint( m_column, m_row ) );
311 if( m_pView->canvasWidget()->editor() != 0 )
313 Q_ASSERT( m_pView->canvasWidget()->editor() );
314 TQString tmp = result->text();
315 if( tmp.at(0) !=
'=')
317 int pos = m_pView->canvasWidget()->editor()->cursorPosition()+ tmp.length();
318 m_pView->canvasWidget()->editor()->setText( tmp );
319 m_pView->canvasWidget()->editor()->setFocus();
320 m_pView->canvasWidget()->editor()->setCursorPosition( pos );
323 m_pView->slotUpdateView( m_pView->activeSheet() );
328void FormulaDialog::slotClose()
330 m_pView->doc()->emitBeginOperation(
false );
332 m_pView->canvasWidget()->endChoose();
335 if(m_pView->activeSheet()->sheetName() != m_sheetName )
337 Sheet *sheet=m_pView->doc()->map()->
findSheet(m_sheetName);
340 m_pView->setActiveSheet(sheet);
345 m_pView->selectionInfo()->initialize( TQPoint( m_column, m_row ) );
349 if( m_pView->canvasWidget()->editor() != 0 )
351 Q_ASSERT( m_pView->canvasWidget()->editor() );
352 m_pView->canvasWidget()->editor()->setText( m_oldText );
353 m_pView->canvasWidget()->editor()->setFocus();
356 m_pView->slotUpdateView( m_pView->activeSheet() );
363void FormulaDialog::slotSelectButton()
365 if( functions->currentItem() != -1 )
367 slotDoubleClicked(functions->findItem(functions->text(functions->currentItem())));
371void FormulaDialog::slotChangeText(
const TQString& )
374 if( !refresh_result )
380 TQString tmp = m_leftText+m_funcName+
"(";
381 tmp += createFormula();
382 tmp = tmp+
")" + m_rightText;
384 result->setText( tmp );
387TQString FormulaDialog::createFormula()
396 int count = m_desc->params();
398 if(!firstElement->text().isEmpty() && count >= 1 )
400 tmp=tmp+createParameter(firstElement->text(), 0 );
404 if(!secondElement->text().isEmpty() && count >= 2 )
408 tmp=tmp+
";"+createParameter(secondElement->text(), 1 );
410 tmp=tmp+createParameter(secondElement->text(), 1 );
412 if(!thirdElement->text().isEmpty() && count >= 3 )
416 tmp=tmp+
";"+createParameter(thirdElement->text(), 2 );
418 tmp=tmp+createParameter(thirdElement->text(), 2 );
420 if(!fourElement->text().isEmpty() && count >= 4 )
424 tmp=tmp+
";"+createParameter(fourElement->text(), 3 );
426 tmp=tmp+createParameter(fourElement->text(), 3 );
428 if(!fiveElement->text().isEmpty() && count >= 5 )
432 tmp=tmp+
";"+createParameter(fiveElement->text(), 4 );
434 tmp=tmp+createParameter(fiveElement->text(), 4 );
440TQString FormulaDialog::createParameter(
const TQString& _text,
int param )
442 if ( _text.isEmpty() )
443 return TQString(
"" );
446 return TQString(
"" );
450 ParameterType elementType = m_desc->param( param ).type();
452 switch( elementType )
457 double tmp = m_pView->doc()->locale()->readNumber( _text, &isNumber );
461 if ( isNumber || _text.upper() ==
"FALSE" || _text.upper() ==
"TRUE" )
468 if ( _text[0] ==
'"' )
473 TQString tmp = _text;
476 while( ( pos = tmp.find(
'"', start ) ) != -1 )
478 if (tmp[pos - 1] !=
'\\')
479 tmp.replace( pos, 1,
"\\\"" );
489 Point p = Point( _text, m_pView->doc()->map() );
490 Range r = Range( _text, m_pView->doc()->map() );
492 if( !p.isValid() && !r.
isValid() )
497 TQString tmp = _text;
500 while( ( pos = tmp.find(
'"', start ) ) != -1 )
502 if (tmp[pos - 1] !=
'\\')
503 tmp.replace( pos, 1,
"\\\"" );
518 case KSpread_Boolean:
528static void showEntry( TQLineEdit* edit, TQLabel* label,
529 FunctionDescription* desc,
int param )
532 label->setText( desc->param( param ).helpText()+
":" );
534 ParameterType elementType = desc->param( param ).type();
535 KFloatValidator *validate=0L;
536 switch( elementType )
539 case KSpread_Boolean:
541 edit->clearValidator ();
544 validate=
new KFloatValidator (edit);
545 validate->setAcceptLocalizedNumbers(
true);
546 edit->setValidator(validate);
547 edit->setText(
"0" );
550 edit->setValidator(
new TQIntValidator (edit));
551 edit->setText(
"0" );
557void FormulaDialog::slotDoubleClicked( TQListBoxItem* item )
561 refresh_result =
false;
564 m_browser->setText(
"" );
569 int old_length = result->text().length();
572 m_browser->setText( m_desc->toTQML() );
573 m_tabwidget->setTabEnabled( m_input,
true );
574 m_tabwidget->setCurrentPage( 1 );
579 if( m_desc->params() > 0 )
581 m_focus = firstElement;
582 firstElement->setFocus();
584 showEntry( firstElement, label1, m_desc, 0 );
589 firstElement->hide();
592 if( m_desc->params() > 1 )
594 showEntry( secondElement, label2, m_desc, 1 );
599 secondElement->hide();
602 if( m_desc->params() > 2 )
604 showEntry( thirdElement, label3, m_desc, 2 );
609 thirdElement->hide();
612 if( m_desc->params() > 3 )
614 showEntry( fourElement, label4, m_desc, 3 );
622 if( m_desc->params() > 4 )
624 showEntry( fiveElement, label5, m_desc, 4 );
632 if( m_desc->params() > 5 )
634 kdDebug(36001) <<
"Error in param->nb_param" << endl;
636 refresh_result=
true;
640 if( result->cursorPosition() < old_length )
642 m_rightText=result->text().right(old_length-result->cursorPosition());
643 m_leftText=result->text().left(result->cursorPosition());
648 m_leftText=result->text();
650 int pos = result->cursorPosition();
651 result->setText( m_leftText+functions->text( functions->currentItem() ) +
"()" + m_rightText);
653 if (result->text()[0] !=
'=')
654 result->setText(
"=" + result->text());
659 if( m_desc->params() == 0 )
662 label1->setText( i18n(
"This function has no parameters.") );
665 result->setCursorPosition(pos+functions->text(functions->currentItem()).length()+2);
667 slotChangeText(
"" );
670void FormulaDialog::slotSelected(
const TQString& function )
672 FunctionDescription* desc =
673 FunctionRepository::self()->functionInfo (function);
676 m_browser->setText (i18n (
"Description is not available."));
680 if( functions->currentItem() !=- 1 )
681 selectFunction->setEnabled(
true );
684 refresh_result =
false;
686 m_funcName = function;
690 m_browser->setText( m_desc->toTQML() );
691 m_browser->setContentsPos( 0, 0 );
695 m_tabwidget->setCurrentPage( 0 );
696 m_tabwidget->setTabEnabled( m_input,
false );
703void FormulaDialog::slotShowFunction(
const TQString& function )
705 FunctionDescription* desc =
706 FunctionRepository::self()->functionInfo( function );
710 TQString category = desc->group();
711 typeFunction->setCurrentText( category );
712 slotActivated( category );
715 TQListBoxItem* item = functions->findItem( function,
716 TQt::ExactMatch | TQt::CaseSensitive );
717 if( item ) functions->setCurrentItem( item );
719 slotSelected( function );
722void FormulaDialog::slotSelectionChanged()
727 if (m_pView->choice()->isValid())
729 TQString area = m_pView->choice()->name();
730 m_focus->setText( area );
734void FormulaDialog::slotActivated(
const TQString& category )
737 if ( category == i18n(
"All") )
738 lst = FunctionRepository::self()->functionNames();
740 lst = FunctionRepository::self()->functionNames( category );
742 kdDebug(36001)<<
"category: "<<category<<
" ("<<lst.count()<<
"functions)" << endl;
745 functions->insertStringList( lst );
747 TQStringList upperList;
748 for ( TQStringList::Iterator it = lst.begin(); it != lst.end();++it )
749 upperList.append((*it).upper());
751 listFunct.setItems( upperList );
754 functions->setCurrentItem(0);
755 slotSelected( functions->text(0) );
758void FormulaDialog::closeEvent ( TQCloseEvent * e )
763#include "kspread_dlg_formula.moc"
For every cell in the spread sheet there is a Cell object.
TQString text() const
Return the text the user entered.
Sheet * findSheet(const TQString &_name)
searches for a sheet named _name
bool isValid() const
Returns true if this Range represents a valid region of a spreadsheet.
The one for all class for points and ranges.
The View class displays a KSpread document.