~ubuntu-branches/ubuntu/warty/koffice/warty

« back to all changes in this revision

Viewing changes to kspread/kspread_selection.cc

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
   Boston, MA 02111-1307, USA.
 
18
*/
 
19
 
 
20
#include "kspread_selection.h"
 
21
#include "kspread_canvas.h"
 
22
#include "kspread_cell.h"
 
23
#include "kspread_global.h"
 
24
#include "kspread_sheet.h"
 
25
#include "kspread_view.h"
 
26
 
 
27
KSpreadSelection::KSpreadSelection(KSpreadView* view)
 
28
{
 
29
  m_marker = QPoint(1,1);
 
30
  m_cursorPosition = QPoint(1,1);
 
31
  m_anchor = QPoint(1,1);
 
32
 
 
33
  m_chooseMarker = QPoint(0,0);
 
34
  m_chooseAnchor = QPoint(0,0);
 
35
  m_chooseCursor = QPoint(0,0);
 
36
 
 
37
  m_chooseTable = NULL;
 
38
  m_pView = view;
 
39
}
 
40
 
 
41
KSpreadSelection::~KSpreadSelection()
 
42
{
 
43
}
 
44
 
 
45
/******************************************
 
46
 * Functions dealing with the current selection
 
47
 */
 
48
/*
 
49
void KSpreadSelection::unselect()
 
50
{
 
51
    // No selection? Then do nothing.
 
52
    if ( singleCellSelection() )
 
53
        return;
 
54
 
 
55
    QRect r = m_rctSelection;
 
56
    // Discard the selection
 
57
    KSpreadCell *cell = cellAt(marker());
 
58
    m_rctSelection.setCoords( m_marker.x(), m_marker.y(),
 
59
                              m_marker.x() + cell->extraXCells(),
 
60
                              m_marker.y() + cell->extraYCells() );
 
61
 
 
62
    // Emit signal so that the views can update.
 
63
//    emit sig_unselect( this, r );
 
64
}
 
65
*/
 
66
 
 
67
QPoint KSpreadSelection::marker() const
 
68
{
 
69
  return m_marker;
 
70
}
 
71
 
 
72
QRect KSpreadSelection::selection() const
 
73
{
 
74
  int left, top, right, bottom;
 
75
  left = QMIN(m_anchor.x(), m_marker.x());
 
76
  top = QMIN(m_anchor.y(), m_marker.y());
 
77
  right = QMAX(m_anchor.x(), m_marker.x());
 
78
  bottom = QMAX(m_anchor.y(), m_marker.y());
 
79
  QRect selection(QPoint(left, top), QPoint(right, bottom));
 
80
 
 
81
  return extendToMergedAreas(selection);
 
82
}
 
83
 
 
84
bool KSpreadSelection::singleCellSelection() const
 
85
{
 
86
  const KSpreadCell* cell =
 
87
    m_pView->activeTable()->cellAt(m_marker.x(), m_marker.y());
 
88
 
 
89
  QRect currentSelection = selection();
 
90
  return ((currentSelection.topLeft() == m_marker) &&
 
91
          (currentSelection.width() - 1 == cell->extraXCells()) &&
 
92
          (currentSelection.height() - 1 == cell->extraYCells()));
 
93
}
 
94
 
 
95
QRect KSpreadSelection::selectionHandleArea() const
 
96
{
 
97
  int column, row;
 
98
 
 
99
  // complete rows/columns are selected, use the marker.
 
100
  if ( util_isRowSelected(selection()) ||
 
101
       util_isColumnSelected(selection()) )
 
102
  {
 
103
    column = marker().x();
 
104
    row = marker().y();
 
105
  }
 
106
  else
 
107
  {
 
108
    column = selection().right();
 
109
    row = selection().bottom();
 
110
  }
 
111
  const KSpreadCell* cell = m_pView->activeTable()->cellAt(column, row);
 
112
 
 
113
  double xpos = m_pView->activeTable()->dblColumnPos( column );
 
114
  double ypos = m_pView->activeTable()->dblRowPos( row );
 
115
  double width = cell->dblWidth( column );
 
116
  double height = cell->dblHeight( row );
 
117
 
 
118
  QPoint rightBottom( m_pView->doc()->zoomItX( xpos + width ),
 
119
                      m_pView->doc()->zoomItY( ypos + height ) );
 
120
 
 
121
  QRect handle( ( rightBottom.x() - 2 ),
 
122
                ( rightBottom.y() - 2 ),
 
123
                ( 5 ),
 
124
                ( 5 ) );
 
125
  return handle;
 
126
}
 
127
 
 
128
void KSpreadSelection::setSelection( const QPoint &newMarker,
 
129
                                     const QPoint &newAnchor,
 
130
                                     KSpreadSheet *table )
 
131
{
 
132
  QRect oldSelection = selection();
 
133
  QPoint oldMarker = m_marker;
 
134
  m_marker = newMarker;
 
135
  m_anchor = newAnchor;
 
136
 
 
137
  QRect newSelection = selection();
 
138
 
 
139
  const KSpreadCell* cell = table->cellAt(newMarker);
 
140
  if (!util_isColumnSelected(newSelection) &&
 
141
      !util_isRowSelected(newSelection) &&
 
142
      cell->isObscured() && cell->isObscuringForced())
 
143
  {
 
144
    cell = cell->obscuringCells().first();
 
145
    m_marker = QPoint(cell->column(), cell->row());
 
146
  }
 
147
 
 
148
  newSelection = selection();
 
149
 
 
150
  /* see if we've actually changed anything */
 
151
  if ( newSelection == oldSelection && newMarker == oldMarker &&
 
152
       m_pView->activeTable() == table )
 
153
    return;
 
154
 
 
155
  /* see if the cursor position is still valid */
 
156
  if (!setCursorPosition(m_cursorPosition))
 
157
  {
 
158
    setCursorPosition(newMarker);
 
159
  }
 
160
 
 
161
  m_pView->enableInsertColumn( !util_isRowSelected( newSelection ) );
 
162
  m_pView->enableInsertRow( !util_isColumnSelected( newSelection ) );
 
163
  m_pView->slotChangeSelection( table, oldSelection, oldMarker );
 
164
}
 
165
 
 
166
void KSpreadSelection::setMarker( const QPoint &point,
 
167
                                  KSpreadSheet* table )
 
168
{
 
169
  QPoint topLeft(point);
 
170
  const KSpreadCell* cell = table->cellAt(topLeft);
 
171
  if (cell->isObscured() && cell->isObscuringForced())
 
172
  {
 
173
    cell = cell->obscuringCells().first();
 
174
    topLeft = QPoint(cell->column(), cell->row());
 
175
  }
 
176
 
 
177
  QPoint botRight(topLeft.x() + cell->extraXCells(),
 
178
                  topLeft.y() + cell->extraYCells());
 
179
  setSelection( topLeft, botRight, table );
 
180
}
 
181
 
 
182
QPoint KSpreadSelection::selectionAnchor()const
 
183
{
 
184
  return m_anchor;
 
185
  /* the anchor is in the opposite corner of the selection rect from the marker */
 
186
 
 
187
  /* these show where the marker is */
 
188
/*
 
189
  bool atTop;
 
190
  bool atLeft;
 
191
  QRect anchorArea;
 
192
 
 
193
  atLeft = m_marker.x() == m_rctSelection.left();
 
194
  atTop = m_marker.y() == m_rctSelection.top();
 
195
 
 
196
  QPoint anchor(atLeft ? m_rctSelection.right() : m_rctSelection.left(),
 
197
                atTop ? m_rctSelection.bottom() : m_rctSelection.top());
 
198
 
 
199
  KSpreadSheet* table = m_pView->activeTable();
 
200
  KSpreadCell* cell = table->cellAt(anchor);
 
201
 
 
202
  if (cell->isObscured())
 
203
  {
 
204
    cell = cell->obscuringCells().first();
 
205
    anchorArea = QRect(QPoint(cell->column(), cell->row()), anchor);
 
206
  }
 
207
  else
 
208
  {
 
209
    anchorArea = QRect(anchor, anchor);
 
210
  }
 
211
 
 
212
  return anchorArea;
 
213
*/
 
214
}
 
215
 
 
216
bool KSpreadSelection::setCursorPosition( const QPoint &position )
 
217
{
 
218
  const KSpreadCell* cell = m_pView->activeTable()->cellAt(m_marker);
 
219
 
 
220
  QRect markerArea(m_marker, QPoint(m_marker.x() + cell->mergedXCells(),
 
221
                                    m_marker.y() + cell->mergedYCells()));
 
222
 
 
223
  if (markerArea.contains(position))
 
224
  {
 
225
    m_cursorPosition = position;
 
226
    return true;
 
227
  }
 
228
  return false;
 
229
}
 
230
 
 
231
QPoint KSpreadSelection::cursorPosition()const
 
232
{
 
233
  return m_cursorPosition;
 
234
}
 
235
 
 
236
QRect KSpreadSelection::getChooseRect()const
 
237
{
 
238
  QRect chooseRect;
 
239
 
 
240
  chooseRect.setLeft(QMIN(m_chooseMarker.x(), m_chooseAnchor.x()));
 
241
  chooseRect.setRight(QMAX(m_chooseMarker.x(), m_chooseAnchor.x()));
 
242
  chooseRect.setTop(QMIN(m_chooseMarker.y(), m_chooseAnchor.y()));
 
243
  chooseRect.setBottom(QMAX(m_chooseMarker.y(), m_chooseAnchor.y()));
 
244
 
 
245
  return chooseRect;
 
246
}
 
247
 
 
248
 
 
249
 
 
250
QRect KSpreadSelection::extendToMergedAreas(QRect area) const
 
251
{
 
252
  const KSpreadCell *cell = m_pView->activeTable()->
 
253
                            cellAt(area.left(), area.top());
 
254
 
 
255
  if( util_isColumnSelected(area) ||
 
256
      util_isRowSelected(area) )
 
257
    return area;
 
258
 
 
259
  else if ( !(cell->isObscured() && cell->isObscuringForced()) &&
 
260
            (cell->extraXCells() + 1) >= area.width() &&
 
261
            (cell->extraYCells() + 1) >= area.height())
 
262
  {
 
263
    /* if just a single cell is selected, we need to merge even when
 
264
       the obscuring isn't forced.  But only if this is the cell that
 
265
       is doing the obscuring -- we still want to be able to click on a cell
 
266
       that is being obscured.
 
267
    */
 
268
    area.setWidth(cell->extraXCells() + 1);
 
269
    area.setHeight(cell->extraYCells() + 1);
 
270
  }
 
271
  else
 
272
  {
 
273
    int top=area.top();
 
274
    int left=area.left();
 
275
    int bottom=area.bottom();
 
276
    int right=area.right();
 
277
    for ( int x = area.left(); x <= area.right(); x++ )
 
278
      for ( int y = area.top(); y <= area.bottom(); y++ )
 
279
      {
 
280
        cell = m_pView->activeTable()->cellAt( x, y );
 
281
        if( cell->isForceExtraCells())
 
282
        {
 
283
          right=QMAX(right,cell->extraXCells()+x);
 
284
          bottom=QMAX(bottom,cell->extraYCells()+y);
 
285
        }
 
286
        else if ( cell->isObscured() && cell->isObscuringForced() )
 
287
        {
 
288
          cell = cell->obscuringCells().first();
 
289
          left=QMIN(left,cell->column());
 
290
          top=QMIN(top,cell->row());
 
291
          bottom=QMAX(bottom,cell->row() + cell->extraYCells());
 
292
          right=QMAX(right,cell->column() + cell->extraXCells());
 
293
        }
 
294
      }
 
295
 
 
296
    area.setCoords(left,top,right,bottom);
 
297
  }
 
298
  return area;
 
299
}