22#include <tqcheckbox.h>
23#include <tqcombobox.h>
24#include <tqlistview.h>
25#include <tqmemarray.h>
29#include <tdemessagebox.h>
31#include "kspreadsubtotal.h"
32#include "kspread_sheet.h"
33#include "kspread_view.h"
34#include "kspread_doc.h"
35#include "kspread_util.h"
37#include "kspread_dlg_subtotal.h"
41SubtotalDialog::SubtotalDialog(
View * parent, TQRect
const & selection,
const char * name )
42 : KDialogBase(parent, name, true, i18n(
"Subtotals" ), Ok | Cancel | User1, Ok, true, KGuiItem(i18n(
"Remove All" )) ),
44 m_pSheet( m_pView->activeSheet() ),
45 m_selection( selection ),
46 m_dialog( new Subtotal( this ) )
48 setButtonBoxOrientation( TQt::Vertical );
49 setMainWidget( m_dialog );
55SubtotalDialog::~SubtotalDialog()
59void SubtotalDialog::slotOk()
61 int numOfCols = m_selection.width();
62 TQMemArray<int> columns( numOfCols );
66 int left = m_selection.left();
67 for ( TQListViewItem * item = m_dialog->m_columnList->firstChild(); item; item = item->nextSibling() )
69 if ( ((TQCheckListItem * ) item)->isOn() )
71 columns[n] = left + n;
81 KMessageBox::sorry(
this, i18n(
"You need to select at least one column for adding subtotals.") );
85 if ( m_dialog->m_replaceSubtotals->isChecked() )
86 removeSubtotalLines();
88 int mainCol = left + m_dialog->m_columnBox->currentItem();
89 int bottom = m_selection.bottom();
90 int top = m_selection.top();
91 left = m_selection.left();
92 TQString oldText = m_pSheet->cellAt( mainCol, top )->strOutText();
94 TQString result(
" " + i18n(
"Result") );
95 int lastChangedRow = top;
97 m_pView->doc()->emitBeginOperation(
false );
98 bool ignoreEmptyCells = m_dialog->m_IgnoreBox->isChecked();
100 if ( !m_dialog->m_summaryOnly->isChecked() )
103 kdDebug() <<
"Starting in row " << y << endl;
104 while ( y <= bottom )
107 newText = m_pSheet->cellAt( mainCol, y )->strOutText();
109 if ( ignoreEmptyCells && (newText.length() == 0) )
112 kdDebug() <<
"Still the same -> " << y << endl;
116 if (newText != oldText)
119 for (
int x = 0; x < numOfCols; ++x)
121 kdDebug() <<
"Column: " << x <<
", " << columns[x] << endl;
122 if (columns[x] != -1)
124 if (!addSubtotal( mainCol, columns[x], y - 1, lastChangedRow, addRow, oldText + result))
144 for (
int x = 0; x < numOfCols; ++x )
146 if ( columns[x] != -1 )
148 if ( !addSubtotal( mainCol, columns[x], y - 1, lastChangedRow, addRow, oldText + result ) )
156 if ( m_dialog->m_summaryBelow->isChecked() )
159 int bottom = m_selection.bottom();
160 for (
int x = 0; x < numOfCols; ++x)
162 if (columns[x] != -1)
164 addSubtotal( mainCol, columns[x], bottom, top, addRow, i18n(
"Grand Total") );
170 m_pView->slotUpdateView( m_pView->activeSheet() );
174void SubtotalDialog::slotCancel()
179void SubtotalDialog::slotUser1()
181 m_pView->doc()->emitBeginOperation(
false );
182 removeSubtotalLines();
183 m_pView->slotUpdateView( m_pView->activeSheet() );
187void SubtotalDialog::removeSubtotalLines()
189 kdDebug() <<
"Removing subtotal lines" << endl;
191 int r = m_selection.right();
192 int l = m_selection.left();
193 int t = m_selection.top();
198 for (
int y = m_selection.bottom(); y >= t; --y )
200 kdDebug() <<
"Checking row: " << y << endl;
201 bool containsSubtotal =
false;
202 for (
int x = l; x <= r; ++x )
204 cell = m_pSheet->cellAt( x, y );
209 if ( text.find(
"SUBTOTAL" ) != -1 )
211 containsSubtotal =
true;
216 if ( containsSubtotal )
218 kdDebug() <<
"Line " << y <<
" contains a subtotal " << endl;
219 TQRect rect( l, y, m_selection.width(), 1 );
221 m_pSheet->unshiftColumn( rect );
222 m_selection.setHeight( m_selection.height() - 1 );
225 kdDebug() <<
"Done removing subtotals" << endl;
228void SubtotalDialog::fillColumnBoxes()
230 int r = m_selection.right();
231 int row = m_selection.top();
234 TQCheckListItem * item;
237 TQString col( i18n(
"Column '%1' ") );
239 for (
int i = m_selection.left(); i <= r; ++i )
241 cell = m_pSheet->cellAt( i, row );
242 text = cell->strOutText();
244 if ( text.length() > 0 )
249 m_dialog->m_columnBox->insertItem( text );
251 item =
new TQCheckListItem( m_dialog->m_columnList,
253 TQCheckListItem::CheckBox );
255 m_dialog->m_columnList->insertItem( item );
259void SubtotalDialog::fillFunctionBox()
262 lst << i18n(
"Average" );
263 lst << i18n(
"Count" );
264 lst << i18n(
"CountA" );
265 lst << i18n(
"Max" );
266 lst << i18n(
"Min" );
267 lst << i18n(
"Product" );
268 lst << i18n(
"StDev" );
269 lst << i18n(
"StDevP" );
270 lst << i18n(
"Sum" );
271 lst << i18n(
"Var" );
272 lst << i18n(
"VarP" );
273 m_dialog->m_functionBox->insertStringList(lst);
276bool SubtotalDialog::addSubtotal(
int mainCol,
int column,
int row,
int topRow,
277 bool addRow, TQString
const & text )
279 kdDebug() <<
"Adding subtotal: " << mainCol <<
", " << column <<
", Rows: " << row <<
", " << topRow
280 <<
": addRow: " << addRow <<
", Text: " << text << endl;
283 TQRect rect(m_selection.left(), row + 1, m_selection.width(), 1);
284 if ( !m_pSheet->shiftColumn( rect ) )
287 m_selection.setHeight( m_selection.height() + 1 );
289 Cell * cell = m_pSheet->nonDefaultCell( mainCol, row + 1 );
291 cell->
format()->setTextFontBold(
true );
292 cell->
format()->setTextFontItalic(
true );
293 cell->
format()->setTextFontUnderline(
true );
298 TQString formula(
"=SUBTOTAL(");
299 formula += TQString::number( m_dialog->m_functionBox->currentItem() + 1 );
302 formula += TQString::number( topRow );
307 formula += TQString::number( row );
311 Cell * cell = m_pSheet->nonDefaultCell( column, row + 1 );
313 cell->
format()->setTextFontBold(
true );
314 cell->
format()->setTextFontItalic(
true );
315 cell->
format()->setTextFontUnderline(
true );
320#include "kspread_dlg_subtotal.moc"
For every cell in the spread sheet there is a Cell object.
TQString text() const
Return the text the user entered.
bool isFormula() const
Returns true if this cell holds a formula.
void setCellText(const TQString &_text, bool asString=false)
The high-level method for setting text, when the user inputs it.
Format * format() const
Returns the format object of this cell.
TQString columnName() const
Returns the column name of the cell.
bool isDefault() const
Returns true if this is a default cell (with row and column equal to zero).
The View class displays a KSpread document.