00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kspread_selection.h"
00021 #include "kspread_canvas.h"
00022 #include "kspread_cell.h"
00023 #include "kspread_doc.h"
00024 #include "kspread_global.h"
00025 #include "kspread_sheet.h"
00026 #include "kspread_view.h"
00027
00028 KSpreadSelection::KSpreadSelection(KSpreadView* view)
00029 {
00030 m_marker = QPoint(1,1);
00031 m_cursorPosition = QPoint(1,1);
00032 m_anchor = QPoint(1,1);
00033
00034 m_chooseMarker = QPoint(0,0);
00035 m_chooseAnchor = QPoint(0,0);
00036 m_chooseCursor = QPoint(0,0);
00037
00038 m_chooseSheet = NULL;
00039 m_pView = view;
00040 }
00041
00042 KSpreadSelection::~KSpreadSelection()
00043 {
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 QPoint KSpreadSelection::marker() const
00069 {
00070 return m_marker;
00071 }
00072
00073 QRect KSpreadSelection::selection(bool extend) const
00074 {
00075 int left, top, right, bottom;
00076 left = QMIN(m_anchor.x(), m_marker.x());
00077 top = QMIN(m_anchor.y(), m_marker.y());
00078 right = QMAX(m_anchor.x(), m_marker.x());
00079 bottom = QMAX(m_anchor.y(), m_marker.y());
00080 QRect selection(QPoint(left, top), QPoint(right, bottom));
00081
00082 if (extend)
00083 return extendToMergedAreas(selection);
00084 return selection;
00085 }
00086
00087 bool KSpreadSelection::singleCellSelection() const
00088 {
00089 const KSpreadCell* cell =
00090 m_pView->activeSheet()->cellAt(m_marker.x(), m_marker.y());
00091
00092 QRect currentSelection = selection();
00093 return ((currentSelection.topLeft() == m_marker) &&
00094 (currentSelection.width() - 1 == cell->extraXCells()) &&
00095 (currentSelection.height() - 1 == cell->extraYCells()));
00096 }
00097
00098 QRect KSpreadSelection::selectionHandleArea() const
00099 {
00100 int column, row;
00101
00102
00103 if ( util_isRowSelected(selection()) ||
00104 util_isColumnSelected(selection()) )
00105 {
00106 column = marker().x();
00107 row = marker().y();
00108 }
00109 else
00110 {
00111 column = selection().right();
00112 row = selection().bottom();
00113 }
00114 const KSpreadCell* cell = m_pView->activeSheet()->cellAt(column, row);
00115
00116 double xpos = m_pView->activeSheet()->dblColumnPos( column );
00117 double ypos = m_pView->activeSheet()->dblRowPos( row );
00118 double width = cell->dblWidth( column );
00119 double height = cell->dblHeight( row );
00120
00121 QPoint rightBottom( m_pView->doc()->zoomItX( xpos + width ),
00122 m_pView->doc()->zoomItY( ypos + height ) );
00123
00124 QRect handle( ( rightBottom.x() - 2 ),
00125 ( rightBottom.y() - 2 ),
00126 ( 5 ),
00127 ( 5 ) );
00128 return handle;
00129 }
00130
00131 void KSpreadSelection::setSelection( const QPoint &newMarker,
00132 const QPoint &newAnchor,
00133 KSpreadSheet *sheet )
00134 {
00135 QRect oldSelection = selection();
00136 QPoint oldMarker = m_marker;
00137 m_marker = newMarker;
00138 m_anchor = newAnchor;
00139
00140 QRect newSelection = selection();
00141
00142
00143
00144
00145 const KSpreadCell* cell = sheet->cellAt(newMarker);
00146 if (!util_isColumnSelected(newSelection) &&
00147 !util_isRowSelected(newSelection) &&
00148 cell->isObscured() && cell->isObscuringForced())
00149 {
00150 cell = cell->obscuringCells().first();
00151 m_marker = QPoint(cell->column(), cell->row());
00152 }
00153
00154 newSelection = selection();
00155
00156
00157 if ( newSelection == oldSelection && newMarker == oldMarker &&
00158 m_pView->activeSheet() == sheet )
00159 return;
00160
00161
00162 if (!setCursorPosition(m_cursorPosition))
00163 {
00164 setCursorPosition(newMarker);
00165 }
00166
00167 m_pView->enableInsertColumn( !util_isRowSelected( newSelection ) );
00168 m_pView->enableInsertRow( !util_isColumnSelected( newSelection ) );
00169 m_pView->slotChangeSelection( sheet, oldSelection, oldMarker );
00170 }
00171
00172 void KSpreadSelection::setMarker( const QPoint &point,
00173 KSpreadSheet* sheet )
00174 {
00175 QPoint topLeft(point);
00176 const KSpreadCell* cell = sheet->cellAt(topLeft);
00177 if (cell->isObscured() && cell->isObscuringForced())
00178 {
00179 cell = cell->obscuringCells().first();
00180 topLeft = QPoint(cell->column(), cell->row());
00181 }
00182
00183 QPoint botRight(topLeft.x() + cell->extraXCells(),
00184 topLeft.y() + cell->extraYCells());
00185 setSelection( topLeft, botRight, sheet );
00186 }
00187
00188 QPoint KSpreadSelection::selectionAnchor()const
00189 {
00190 return m_anchor;
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 }
00221
00222 bool KSpreadSelection::setCursorPosition( const QPoint &position )
00223 {
00224 const KSpreadCell* cell = m_pView->activeSheet()->cellAt(m_marker);
00225
00226 QRect markerArea(m_marker, QPoint(m_marker.x() + cell->mergedXCells(),
00227 m_marker.y() + cell->mergedYCells()));
00228
00229 if (markerArea.contains(position))
00230 {
00231 m_cursorPosition = position;
00232 return true;
00233 }
00234 return false;
00235 }
00236
00237 QPoint KSpreadSelection::cursorPosition()const
00238 {
00239 return m_cursorPosition;
00240 }
00241
00242 QRect KSpreadSelection::getChooseRect()const
00243 {
00244 QRect chooseRect;
00245
00246 chooseRect.setLeft(QMIN(m_chooseMarker.x(), m_chooseAnchor.x()));
00247 chooseRect.setRight(QMAX(m_chooseMarker.x(), m_chooseAnchor.x()));
00248 chooseRect.setTop(QMIN(m_chooseMarker.y(), m_chooseAnchor.y()));
00249 chooseRect.setBottom(QMAX(m_chooseMarker.y(), m_chooseAnchor.y()));
00250
00251 return chooseRect;
00252 }
00253
00254
00255
00256 QRect KSpreadSelection::extendToMergedAreas(QRect area) const
00257 {
00258 const KSpreadCell *cell = m_pView->activeSheet()->
00259 cellAt(area.left(), area.top());
00260
00261 if( util_isColumnSelected(area) ||
00262 util_isRowSelected(area) )
00263 return area;
00264
00265 else if ( !(cell->isObscured() && cell->isObscuringForced()) &&
00266 (cell->extraXCells() + 1) >= area.width() &&
00267 (cell->extraYCells() + 1) >= area.height())
00268 {
00269
00270
00271
00272
00273
00274 area.setWidth(cell->extraXCells() + 1);
00275 area.setHeight(cell->extraYCells() + 1);
00276 }
00277 else
00278 {
00279 int top=area.top();
00280 int left=area.left();
00281 int bottom=area.bottom();
00282 int right=area.right();
00283 for ( int x = area.left(); x <= area.right(); x++ )
00284 for ( int y = area.top(); y <= area.bottom(); y++ )
00285 {
00286 cell = m_pView->activeSheet()->cellAt( x, y );
00287 if( cell->isForceExtraCells())
00288 {
00289 right=QMAX(right,cell->extraXCells()+x);
00290 bottom=QMAX(bottom,cell->extraYCells()+y);
00291 }
00292 else if ( cell->isObscured() && cell->isObscuringForced() )
00293 {
00294 cell = cell->obscuringCells().first();
00295 left=QMIN(left,cell->column());
00296 top=QMIN(top,cell->row());
00297 bottom=QMAX(bottom,cell->row() + cell->extraYCells());
00298 right=QMAX(right,cell->column() + cell->extraXCells());
00299 }
00300 }
00301
00302 area.setCoords(left,top,right,bottom);
00303 }
00304 return area;
00305 }