summaryrefslogtreecommitdiffstats
path: root/kspread/dialogs/kspread_dlg_subtotal.cpp
blob: 054cc1f761924f982881db005f2cde932d002230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* This file is part of the KDE project
   Copyright (C) 2002-2003 Norbert Andres <nandres@web.de>
             (C) 2002 Philipp Mueller <philipp.mueller@gmx.de>
             (C) 2002 Laurent Montel <montel@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqcheckbox.h>
#include <tqcombobox.h>
#include <tqlistview.h>
#include <tqmemarray.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <tdemessagebox.h>

#include "kspreadsubtotal.h"
#include "kspread_sheet.h"
#include "kspread_view.h"
#include "kspread_doc.h"
#include "kspread_util.h"

#include "kspread_dlg_subtotal.h"

using namespace KSpread;

SubtotalDialog::SubtotalDialog( View * parent, TQRect const & selection, const char * name )
  : KDialogBase(parent, name, true, i18n( "Subtotals" ), Ok | Cancel | User1, Ok, true, KGuiItem(i18n( "Remove All" )) ),
    m_pView( parent ),
    m_pSheet( m_pView->activeSheet() ),
    m_selection( selection ),
    m_dialog( new Subtotal( this ) )
{
  setButtonBoxOrientation( Qt::Vertical );
  setMainWidget( m_dialog );

  fillColumnBoxes();
  fillFunctionBox();
}

SubtotalDialog::~SubtotalDialog()
{
}

void SubtotalDialog::slotOk()
{
  int numOfCols = m_selection.width();
  TQMemArray<int> columns( numOfCols );

  int n = 0;
  bool empty = true;
  int left = m_selection.left();
  for ( TQListViewItem * item = m_dialog->m_columnList->firstChild(); item; item = item->nextSibling() )
  {
    if ( ((TQCheckListItem * ) item)->isOn() )
    {
      columns[n] = left + n;
      empty = false;
    }
    else
      columns[n] = -1;
    ++n;
  }

  if ( empty )
  {
    KMessageBox::sorry( this, i18n("You need to select at least one column for adding subtotals.") );
    return;
  }

  if ( m_dialog->m_replaceSubtotals->isChecked() )
    removeSubtotalLines();

  int mainCol = left + m_dialog->m_columnBox->currentItem();
  int bottom = m_selection.bottom();
  int top    = m_selection.top();
  left       = m_selection.left();
  TQString oldText = m_pSheet->cellAt( mainCol, top )->strOutText();
  TQString newText;
  TQString result( " " + i18n("Result") );
  int lastChangedRow = top;

  m_pView->doc()->emitBeginOperation( false );
  bool ignoreEmptyCells = m_dialog->m_IgnoreBox->isChecked();
  bool addRow;
  if ( !m_dialog->m_summaryOnly->isChecked() )
  {
    int y = top + 1;
    kdDebug() << "Starting in row " << y << endl;
    while ( y <= bottom )
    {
      addRow = true;
      newText = m_pSheet->cellAt( mainCol, y )->strOutText();

      if ( ignoreEmptyCells && (newText.length() == 0) )
      {
        ++y;
        kdDebug() << "Still the same -> " << y << endl;
        continue;
      }

      if (newText != oldText)
      {
        int saveY = y;
        for (int x = 0; x < numOfCols; ++x)
        {
          kdDebug() << "Column: " << x << ", " << columns[x] << endl;
          if (columns[x] != -1)
          {
            if (!addSubtotal( mainCol, columns[x], y - 1, lastChangedRow, addRow, oldText + result))
              reject();

            if ( addRow )
            {
              ++saveY;
              ++bottom;
            }

            addRow = false;
          }
        }
        y = saveY;
        lastChangedRow = y;
      }
      oldText = newText;
      ++y;
    }

    addRow = true;
    for ( int x = 0; x < numOfCols; ++x )
    {
      if ( columns[x] != -1 )
      {
        if ( !addSubtotal( mainCol, columns[x], y - 1, lastChangedRow, addRow, oldText + result ) )
          reject();
        addRow = false;
      }
    }
    ++y;
  }

  if ( m_dialog->m_summaryBelow->isChecked() )
  {
    addRow = true;
    int bottom = m_selection.bottom();
    for (int x = 0; x < numOfCols; ++x)
    {
      if (columns[x] != -1)
      {
        addSubtotal( mainCol, columns[x], bottom, top, addRow, i18n("Grand Total") );
        addRow = false;
      }
    }
  }

  m_pView->slotUpdateView( m_pView->activeSheet() );
  accept();
}

void SubtotalDialog::slotCancel()
{
  reject();
}

void SubtotalDialog::slotUser1()
{
  m_pView->doc()->emitBeginOperation( false );
  removeSubtotalLines();
  m_pView->slotUpdateView( m_pView->activeSheet() );
  accept();
}

void SubtotalDialog::removeSubtotalLines()
{
  kdDebug() << "Removing subtotal lines" << endl;

  int r = m_selection.right();
  int l = m_selection.left();
  int t = m_selection.top();

  Cell * cell;
  TQString text;

  for ( int y = m_selection.bottom(); y >= t; --y )
  {
    kdDebug() << "Checking row: " << y << endl;
    bool containsSubtotal = false;
    for (int x = l; x <= r; ++x )
    {
      cell = m_pSheet->cellAt( x, y );
      if ( cell->isDefault() || !cell->isFormula() )
        continue;

      text = cell->text();
      if ( text.find( "SUBTOTAL" ) != -1 )
      {
        containsSubtotal = true;
        break;
      }
    }

    if ( containsSubtotal )
    {
      kdDebug() << "Line " << y << " contains a subtotal " << endl;
      TQRect rect( l, y, m_selection.width(), 1 );

      m_pSheet->unshiftColumn( rect );
      m_selection.setHeight( m_selection.height() - 1 );
    }
  }
  kdDebug() << "Done removing subtotals" << endl;
}

void SubtotalDialog::fillColumnBoxes()
{
  int r = m_selection.right();
  int row = m_selection.top();

  Cell    * cell;
  TQCheckListItem * item;

  TQString text;
  TQString col( i18n( "Column '%1' ") );

  for ( int i = m_selection.left(); i <= r; ++i )
  {
    cell = m_pSheet->cellAt( i, row );
    text = cell->strOutText();

    if ( text.length() > 0 )
    {
      text = col.arg( Cell::columnName( i ) );
    }

    m_dialog->m_columnBox->insertItem( text );

    item = new TQCheckListItem( m_dialog->m_columnList,
                               text,
                               TQCheckListItem::CheckBox );
    item->setOn(false);
    m_dialog->m_columnList->insertItem( item );
  }
}

void SubtotalDialog::fillFunctionBox()
{
    TQStringList lst;
    lst << i18n( "Average" );
    lst << i18n( "Count" );
    lst << i18n( "CountA" );
    lst << i18n( "Max" );
    lst << i18n( "Min" );
    lst << i18n( "Product" );
    lst << i18n( "StDev" );
    lst << i18n( "StDevP" );
    lst << i18n( "Sum" );
    lst << i18n( "Var" );
    lst << i18n( "VarP" );
    m_dialog->m_functionBox->insertStringList(lst);
}

bool SubtotalDialog::addSubtotal( int mainCol, int column, int row, int topRow,
                                      bool addRow, TQString const & text )
{
  kdDebug() << "Adding subtotal: " << mainCol << ", " << column << ", Rows: " << row << ", " << topRow
            << ": addRow: " << addRow << ", Text: " << text << endl;
  if ( addRow )
  {
    TQRect rect(m_selection.left(), row + 1, m_selection.width(), 1);
    if ( !m_pSheet->shiftColumn( rect ) )
      return false;

    m_selection.setHeight( m_selection.height() + 1 );

    Cell * cell = m_pSheet->nonDefaultCell( mainCol, row + 1 );
    cell->setCellText( text );
    cell->format()->setTextFontBold( true );
    cell->format()->setTextFontItalic( true );
    cell->format()->setTextFontUnderline( true );
  }

  TQString colName = Cell::columnName( column );

  TQString formula("=SUBTOTAL(");
  formula += TQString::number( m_dialog->m_functionBox->currentItem() + 1 );
  formula += "; ";
  formula += colName;
  formula += TQString::number( topRow );
  // if ( topRow != row )
  {
    formula += ":";
    formula += colName;
    formula += TQString::number( row );
  }
  formula += ")";

  Cell * cell = m_pSheet->nonDefaultCell( column, row + 1 );
  cell->setCellText( formula );
  cell->format()->setTextFontBold( true );
  cell->format()->setTextFontItalic( true );
  cell->format()->setTextFontUnderline( true );

  return true;
}

#include "kspread_dlg_subtotal.moc"