31#include <tqcheckbox.h>
32#include <tqcombobox.h>
35#include <tqpushbutton.h>
38#include <kdialogbase.h>
39#include <tdemessagebox.h>
41#include "kspread_dlg_cons.h"
43#include <kspread_canvas.h>
44#include <kspread_doc.h>
45#include <kspread_global.h>
46#include <kspread_locale.h>
48#include <kspread_sheet.h>
49#include <kspread_util.h>
50#include <kspread_view.h>
53#include <valueconverter.h>
57ConsolidateDialog::ConsolidateDialog(
View* parent,
const char* name )
58 : KDialogBase( parent, name, false, i18n(
"Consolidate"), Ok|Cancel )
62 TQWidget* page =
new TQWidget(
this );
63 setMainWidget( page );
65 TQGridLayout *grid1 =
new TQGridLayout( page, 12, 2, marginHint(), spacingHint() );
68 tmpTQLabel =
new TQLabel( page,
"Label_1" );
69 grid1->addWidget(tmpTQLabel,0,0);
70 tmpTQLabel->setText( i18n(
"&Function:") );
72 m_pFunction =
new TQComboBox( page );
73 grid1->addWidget(m_pFunction,1,0);
74 tmpTQLabel->setBuddy(m_pFunction);
76 m_pFunction->insertItem( i18n(
"Sum"), Sum );
77 m_pFunction->insertItem( i18n(
"Average"), Average );
78 m_pFunction->insertItem( i18n(
"Count"), Count );
79 m_pFunction->insertItem( i18n(
"Max"), Max );
80 m_pFunction->insertItem( i18n(
"Min"), Min );
81 m_pFunction->insertItem( i18n(
"Product"), Product );
82 m_pFunction->insertItem( i18n(
"Standard Deviation"), StdDev );
83 m_pFunction->insertItem( i18n(
"Variance"), Var );
85 tmpTQLabel =
new TQLabel( page,
"Label_1" );
86 tmpTQLabel->setText( i18n(
"Re&ference:") );
87 grid1->addWidget(tmpTQLabel,2,0);
89 m_pRef =
new TQLineEdit( page );
90 grid1->addWidget(m_pRef,3,0);
91 tmpTQLabel->setBuddy(m_pRef);
93 tmpTQLabel =
new TQLabel( page,
"Label_1" );
94 grid1->addWidget(tmpTQLabel,4,0);
95 tmpTQLabel->setText( i18n(
"&Entered references:") );
97 m_pRefs =
new TQListBox( page );
98 grid1->addMultiCellWidget( m_pRefs,5,8,0,0);
99 tmpTQLabel->setBuddy(m_pRefs);
101 m_pRow =
new TQCheckBox( i18n(
"&Description in row"), page );
102 grid1->addWidget( m_pRow,9,0);
103 m_pCol =
new TQCheckBox( i18n(
"De&scription in column"), page );
104 grid1->addWidget(m_pCol,10,0);
105 m_pCopy =
new TQCheckBox( i18n(
"Co&py data"), page );
106 grid1->addWidget(m_pCopy,11,0);
108 m_pAdd =
new TQPushButton( i18n(
"&Add"), page );
109 grid1->addWidget(m_pAdd,2,1);
110 m_pRemove =
new TQPushButton( i18n(
"&Remove"), page );
111 grid1->addWidget(m_pRemove,3,1);
114 connect( m_pAdd, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( slotAdd() ) );
115 connect( m_pRemove, TQ_SIGNAL( clicked() ),
this, TQ_SLOT( slotRemove() ) );
116 connect( m_pRef, TQ_SIGNAL( returnPressed() ),
this, TQ_SLOT( slotReturnPressed() ) );
118 connect(m_pView->selectionInfo(), TQ_SIGNAL(changed(
const Region&)),
119 this, TQ_SLOT(slotSelectionChanged()));
122ConsolidateDialog::~ConsolidateDialog()
124 kdDebug(36001)<<
"Consolidate::~Consolidate()\n";
127enum Description { D_ROW, D_COL, D_NONE, D_BOTH };
139void ConsolidateDialog::slotOk()
141 m_pView->doc()->emitBeginOperation(
false );
143 Map *map = m_pView->doc()->map();
145 Sheet* sheet = m_pView->activeSheet();
146 int dx = m_pView->selectionInfo()->selection().left();
147 int dy = m_pView->selectionInfo()->selection().top();
151 switch( m_pFunction->currentItem() )
153 case Sum: function =
"SUM";
break;
154 case Average: function =
"AVERAGE";
break;
155 case Count: function =
"COUNT";
break;
156 case Max: function =
"MAX";
break;
157 case Min: function =
"MIN";
break;
158 case Product: function =
"PRODUCT";
break;
159 case StdDev: function =
"STDDEV";
break;
160 case Var: function =
"VARIANCE";
break;
164 TQStringList r = refs();
165 TQValueList<Range> ranges;
166 TQStringList::Iterator s = r.begin();
167 for( ; s != r.end(); ++s )
171 Q_ASSERT( r.isValid() );
173 if ( r.sheet() == 0 )
176 r.setSheetName(sheet->sheetName());
182 if ( m_pRow->isChecked() && m_pCol->isChecked() )
184 else if ( m_pRow->isChecked() )
186 else if ( m_pCol->isChecked() )
192 Q_ASSERT( ranges.count() > 0 );
193 TQValueList<Range>::Iterator it = ranges.begin();
194 int w = (*it).range().right() - (*it).range().left() + 1;
195 int h = (*it).range().bottom() - (*it).range().top() + 1;
196 if ( w <= ( ( desc == D_BOTH || desc == D_COL ) ? 1 : 0 ) ||
197 h <= ( ( desc == D_BOTH || desc == D_ROW ) ? 1 : 0 ) )
199 m_pView->slotUpdateView( m_pView->activeSheet() );
200 KMessageBox::error(
this, i18n(
"The range\n%1\nis too small" ).arg( *( r.begin() ) ));
204 if( (*it).range().bottom()==KS_rowMax || (*it).range().right()== KS_colMax )
206 m_pView->slotUpdateView( m_pView->activeSheet() );
207 KMessageBox::error(
this, i18n(
"The range\n%1\nis too large" ).arg( *( r.begin() ) ));
213 for( ; it != ranges.end(); ++it, i++ )
215 TQRect currentRange=(*it).range();
217 int w2 = currentRange.right() - currentRange.left() + 1;
218 int h2 = currentRange.bottom() - currentRange.top() + 1;
220 if(currentRange.bottom()==KS_rowMax || currentRange.right()== KS_colMax)
222 m_pView->slotUpdateView( m_pView->activeSheet() );
223 KMessageBox::error(
this, i18n(
"The range\n%1\nis too large" ).arg( r[i]));
226 if ( ( desc == D_NONE && ( w != w2 || h != h2 ) ) ||
227 ( desc == D_ROW && h != h2 ) ||
228 ( desc == D_COL && w != w2 ) )
230 m_pView->slotUpdateView( m_pView->activeSheet() );
231 TQString tmp = i18n(
"The ranges\n%1\nand\n%2\nhave different size").arg( *( r.begin() ) ).arg( r[i] );
232 KMessageBox::error(
this, tmp);
238 if ( desc == D_NONE )
242 dest.setCoords( dx, dy, dx + w - 1, dy + h - 1 );
244 for( ; it != ranges.end(); ++it )
246 Sheet *t = (*it).sheet();
250 TQRect currentRange=(*it).range();
252 r.setCoords( currentRange.left(), currentRange.top(), currentRange.right(), currentRange.bottom() );
253 if ( t == sheet && r.intersects( dest ) )
255 m_pView->slotUpdateView( m_pView->activeSheet() );
256 TQString tmp( i18n(
"The source tables intersect with the destination table") );
257 KMessageBox::error(
this, tmp);
262 for(
int x = 0; x < w; x++ )
264 for(
int y = 0; y < h; y++ )
267 TQString formula =
"=" + function +
"(";
269 for( ; it != ranges.end(); ++it )
271 Sheet *t = (*it).sheet();
273 Cell *c = t->cellAt( x + (*it).range().left(), y + (*it).range().top() );
276 if ( it != ranges.begin() )
278 formula += (*it).sheetName() +
"!";
279 formula += c->
name();
284 sheet->setText( dy + y, dx + x,
285 m_pCopy->isChecked() ? evaluate( formula, sheet ) : formula );
289 else if ( desc == D_ROW )
294 for( ; it != ranges.end(); ++it )
296 Sheet *t = (*it).sheet();
299 for(
int x = (*it).range().left(); x <= (*it).range().right() ; ++x )
301 Cell *c = t->cellAt( x, (*it).range().top() );
305 if ( !lst.contains( s ) )
314 dest.setCoords( dx, dy, dx + lst.count() - 1, dy + h - 1 );
316 for( ; it != ranges.end(); ++it )
318 Sheet *t = (*it).sheet();
321 TQRect currentRange=(*it).range();
322 r.setCoords( currentRange.left(), currentRange.top(), currentRange.right(), currentRange.bottom() );
323 if ( t == sheet && r.intersects( dest ) )
325 m_pView->slotUpdateView( m_pView->activeSheet() );
326 TQString tmp( i18n(
"The source tables intersect with the destination table") );
327 KMessageBox::error(
this, tmp);
334 TQStringList::Iterator s = lst.begin();
335 for( ; s != lst.end(); ++s, ++x )
337 sheet->setText( dy, dx + x, *s );
339 for(
int y = 1; y < h; ++y )
342 TQString formula =
"=" + function +
"(";
344 for( ; it != ranges.end(); ++it )
346 for(
int i = (*it).range().left(); i <= (*it).range().right(); ++i )
348 Sheet *t = (*it).sheet();
350 Cell *c = t->cellAt( i, (*it).range().top() );
357 if ( it != ranges.begin() )
359 formula += (*it).sheetName() +
"!";
360 formula +=
Cell::name( i, y + (*it).range().top() );
367 sheet->setText( dy + y, dx + x,
368 m_pCopy->isChecked() ? evaluate( formula, sheet ) : formula );
372 else if ( desc == D_COL )
377 for( ; it != ranges.end(); ++it )
379 Sheet *t = (*it).sheet();
381 for(
int y = (*it).range().top(); y <= (*it).range().bottom() ; ++y )
383 Cell *c = t->cellAt( (*it).range().left(), y );
387 if ( !s.isEmpty() && lst.find( s ) == lst.end() )
396 dest.setCoords( dx, dy, dx + w - 1, dy + lst.count() - 1 );
398 for( ; it != ranges.end(); ++it )
400 Sheet *t = (*it).sheet();
403 TQRect currentRange=(*it).range();
404 r.setCoords( currentRange.left(), currentRange.top(), currentRange.right(), currentRange.bottom() );
405 if ( t == sheet && r.intersects( dest ) )
407 m_pView->slotUpdateView( m_pView->activeSheet() );
408 TQString tmp( i18n(
"The source tables intersect with the destination table") );
409 KMessageBox::error(
this, tmp);
416 TQStringList::Iterator s = lst.begin();
417 for( ; s != lst.end(); ++s, ++y )
419 sheet->setText( dy + y, dx, *s );
421 for(
int x = 1; x < w; ++x )
424 TQString formula =
"=" + function +
"(";
426 for( ; it != ranges.end(); ++it )
428 for(
int i = (*it).range().top(); i <= (*it).range().bottom(); i++ )
430 Sheet *t = (*it).sheet();
432 Cell *c = t->cellAt( (*it).range().left(), i );
436 if ( !v.isEmpty() && *s == v )
440 if ( it != ranges.begin() ) formula +=
";";
441 formula += (*it).sheetName() +
"!";
442 formula +=
Cell::name( i, y + (*it).range().top() );
450 sheet->setText( dy + y, dx + x,
451 m_pCopy->isChecked() ? evaluate( formula, sheet ) : formula );
455 else if ( desc == D_BOTH )
460 for( ; it != ranges.end(); ++it )
462 Sheet *t = (*it).sheet();
464 for(
int y = (*it).range().top() + 1; y <= (*it).range().bottom() ; ++y )
466 Cell *c = t->cellAt( (*it).range().left(), y );
470 if ( !s.isEmpty() && cols.find( s ) == cols.end() )
480 for( ; it != ranges.end(); ++it )
482 Sheet *t = (*it).sheet();
484 for(
int x = (*it).range().left() + 1; x <= (*it).range().right() ; ++x )
486 Cell *c = t->cellAt( x, (*it).range().top() );
490 if ( !s.isEmpty() && rows.find( s ) == rows.end() )
499 dest.setCoords( dx, dy, dx + cols.count(), dy + rows.count() );
501 for( ; it != ranges.end(); ++it )
503 Sheet *t = (*it).sheet();
506 TQRect currentRange=(*it).range();
507 r.setCoords( currentRange.left(), currentRange.top(), currentRange.right(), currentRange.bottom() );
508 if ( t == sheet && r.intersects( dest ) )
510 m_pView->slotUpdateView( m_pView->activeSheet() );
511 TQString tmp( i18n(
"The source tables intersect with the destination table") );
512 KMessageBox::error(
this, tmp);
518 TQValueList<st_cell> lst;
520 for( ; it != ranges.end(); ++it )
522 Sheet *t = (*it).sheet();
524 for(
int x = (*it).range().left() + 1; x <= (*it).range().right() ; ++x )
526 Cell *c = t->cellAt( x, (*it).range().top() );
530 for(
int y = (*it).range().top() + 1; y <= (*it).range().bottom() ; ++y )
532 Cell *c2 = t->cellAt( (*it).range().left(), y );
536 Cell *c3 = t->cellAt( x, y );
543 k.sheet = (*it).sheetName();
556 TQStringList::Iterator s = rows.begin();
557 for( ; s != rows.end(); ++s, ++i )
558 sheet->setText( dy, dx + i, *s );
563 for( ; s != cols.end(); ++s, ++i )
564 sheet->setText( dy + i, dx, *s );
568 TQStringList::Iterator ydesc = rows.begin();
569 for( ; ydesc != rows.end(); ++ydesc, x++ )
572 TQStringList::Iterator xdesc = cols.begin();
573 for( ; xdesc != cols.end(); ++xdesc, y++ )
576 TQString formula =
"=" + function +
"(";
577 TQValueList<st_cell>::Iterator lit = lst.begin();
578 for( ; lit != lst.end(); ++lit )
580 if ( (*lit).xdesc == *xdesc && (*lit).ydesc == *ydesc )
583 if ( it != ranges.begin() ) formula +=
";";
584 formula += (*it).sheetName() +
"!";
585 formula +=
Cell::name( i, y + (*it).range().top() );
590 sheet->setText( dy + y, dx + x,
591 m_pCopy->isChecked() ? evaluate( formula, sheet ) : formula );
595 m_pView->updateEditWidget();
596 m_pView->slotUpdateView( m_pView->activeSheet() );
601void ConsolidateDialog::slotCancel()
607void ConsolidateDialog::slotAdd()
612void ConsolidateDialog::slotRemove()
614 int i = m_pRefs->currentItem();
618 m_pRefs->removeItem( i );
620 if ( m_pRefs->count() == 0 )
621 actionButton( Ok )->setEnabled(
false );
624TQStringList ConsolidateDialog::refs()
627 int c = m_pRefs->count();
629 for(
int i = 0; i < c; i++ )
630 list.append( m_pRefs->text( i ) );
635void ConsolidateDialog::slotSelectionChanged()
637 if (!m_pView->selectionInfo()->isValid())
639 m_pRef->setText(
"" );
643 TQString area = m_pView->selectionInfo()->
name();
644 m_pRef->setText( area );
645 m_pRef->setSelection( 0, area.length() );
648void ConsolidateDialog::slotReturnPressed()
650 TQString txt = m_pRef->text();
652 Range r( txt, m_pView->doc()->map() );
655 KMessageBox::error(
this, i18n(
"The range\n%1\n is malformed").arg( txt ));
659 if ( !txt.isEmpty() )
661 m_pRefs->insertItem( txt );
662 actionButton( Ok )->setEnabled(
true );
666void ConsolidateDialog::closeEvent ( TQCloseEvent * )
671TQString ConsolidateDialog::evaluate(
const TQString& formula, Sheet* sheet )
673 TQString result =
"###";
683 result = sheet->doc()->converter()->asString (res).asString ();
687#include "kspread_dlg_cons.moc"
For every cell in the spread sheet there is a Cell object.
TQString name() const
Returns the name of the cell.
const Value value() const
Returns the value that this cell holds.
bool isDefault() const
Returns true if this is a default cell (with row and column equal to zero).
A map is a simple container for all sheets.
Represents a region within a workbook.
The one for all class for points and ranges.
Provides a wrapper for cell value.
bool isNumber() const
Returns true if the type of this value is either integer or floating-point.
TQString asString() const
Returns the string value of this value.
The View class displays a KSpread document.