kspread Library API Documentation

commands.cc

00001 /* This file is part of the KDE project
00002    Copyright 2004 Ariya Hidayat <ariya@kde.org>
00003    Copyright 2004 Laurent Montel <montel@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include "commands.h"
00022 #include "damages.h"
00023 #include "kspread_doc.h"
00024 #include "kspread_locale.h"
00025 #include "kspread_map.h"
00026 #include "kspread_sheet.h"
00027 #include "kspread_undo.h"
00028 #include "kspread_util.h"
00029 
00030 #include "kspread_sheetprint.h"
00031 
00032 using namespace KSpread;
00033 
00034 // ----- UndoWrapperCommand -----
00035 
00036 UndoWrapperCommand::UndoWrapperCommand( KSpreadUndoAction* ua )
00037 {
00038   undoAction = ua;
00039 }
00040 
00041 void UndoWrapperCommand::execute()
00042 {
00043   // This is not really safe and functional, but UndoWrapperCommand
00044   // is a workaround anyway. So leave it as it it (Ariya)
00045   undoAction->redo();
00046 }
00047 
00048 void UndoWrapperCommand::unexecute()
00049 {
00050   undoAction->undo();
00051 }
00052 
00053 QString UndoWrapperCommand::name() const
00054 {
00055   return undoAction->getName();
00056 }
00057 
00058 // ----- MergeCellsCommand -----
00059 
00060 MergeCellCommand::MergeCellCommand( KSpreadCell* c, int cs, int rs )
00061 {
00062   cell = c;
00063   colSpan = cs;
00064   rowSpan = rs;
00065   oldColSpan = cell->extraXCells();
00066   oldRowSpan = cell->extraYCells();
00067   if( cell )
00068   {
00069     QRect area( cell->column(), cell->row(), cs+1, rs+1 );
00070     rangeName = util_rangeName( area );
00071   }
00072 }
00073 
00074 QString MergeCellCommand::name() const
00075 {
00076   if( rangeName.isEmpty() )
00077     return i18n("Merge Cells");
00078   else
00079     return i18n("Merge Cells %1").arg( rangeName );
00080 }
00081 
00082 void MergeCellCommand::execute()
00083 {
00084   KSpreadSheet* sheet = cell->sheet();
00085   if( !sheet ) return;
00086   sheet->changeMergedCell( cell->column(), cell->row(), colSpan, rowSpan);
00087 }
00088 
00089 void MergeCellCommand::unexecute()
00090 {
00091   KSpreadSheet* sheet = cell->sheet();
00092   if( !sheet ) return;
00093   sheet->changeMergedCell( cell->column(), cell->row(), oldColSpan, oldRowSpan);
00094 }
00095 
00096 // ----- DissociateCellCommand -----
00097 
00098 DissociateCellCommand::DissociateCellCommand( KSpreadCell* c )
00099 {
00100   cell = c;
00101   oldColSpan = cell->extraXCells();
00102   oldRowSpan = cell->extraYCells();
00103 }
00104 
00105 QString DissociateCellCommand::name() const
00106 {
00107   return i18n("Dissociate Cell");
00108 }
00109 
00110 void DissociateCellCommand::execute()
00111 {
00112   KSpreadSheet* sheet = cell->sheet();
00113   if( !sheet ) return;
00114   sheet->changeMergedCell( cell->column(), cell->row(), 0, 0 );
00115 }
00116 
00117 void DissociateCellCommand::unexecute()
00118 {
00119   KSpreadSheet* sheet = cell->sheet();
00120   if( !sheet ) return;
00121   sheet->changeMergedCell( cell->column(), cell->row(), oldColSpan, oldRowSpan);
00122 }
00123 
00124 // ----- RenameSheetCommand -----
00125 
00126 RenameSheetCommand::RenameSheetCommand( KSpreadSheet* s, const QString &name )
00127 {
00128   sheet = s;
00129   if( s ) oldName = s->sheetName();
00130   newName = name;
00131 }
00132 
00133 QString RenameSheetCommand::name() const
00134 {
00135   return i18n("Rename Sheet");
00136 }
00137 
00138 void RenameSheetCommand::execute()
00139 {
00140   if( sheet )
00141     sheet->setSheetName( newName );
00142 }
00143 
00144 void RenameSheetCommand::unexecute()
00145 {
00146   if( sheet )
00147     sheet->setSheetName( oldName );
00148 }
00149 
00150 // ----- HideSheetCommand -----
00151 
00152 HideSheetCommand::HideSheetCommand( KSpreadSheet* sheet )
00153 {
00154   doc = sheet->doc();
00155   sheetName = sheet->sheetName();
00156 }
00157 
00158 void HideSheetCommand::execute()
00159 {
00160   KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00161   if( !sheet ) return;
00162 
00163   sheet->hideSheet( true );
00164 }
00165 
00166 void HideSheetCommand::unexecute()
00167 {
00168   KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00169   if( !sheet ) return;
00170 
00171   sheet->hideSheet( false );
00172 }
00173 
00174 QString HideSheetCommand::name() const
00175 {
00176     QString n = QString( i18n("Hide Sheet %1") ).arg( sheetName );
00177     if( n.length() > 64 ) n = i18n("Hide Sheet");
00178     return n;
00179 }
00180 
00181 // ----- ShowSheetCommand -----
00182 
00183 ShowSheetCommand::ShowSheetCommand( KSpreadSheet* sheet )
00184 {
00185   doc = sheet->doc();
00186   sheetName = sheet->sheetName();
00187 }
00188 
00189 void ShowSheetCommand::execute()
00190 {
00191   KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00192   if( !sheet ) return;
00193 
00194   sheet->hideSheet( false );
00195 }
00196 
00197 void ShowSheetCommand::unexecute()
00198 {
00199   KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00200   if( !sheet ) return;
00201 
00202   sheet->hideSheet( true );
00203 }
00204 
00205 QString ShowSheetCommand::name() const
00206 {
00207     QString n = QString( i18n("Show Sheet %1") ).arg( sheetName );
00208     if( n.length() > 64 ) n = i18n("Show Sheet");
00209     return n;
00210 }
00211 
00212 
00213 // ----- AddSheetCommand -----
00214 
00215 AddSheetCommand::AddSheetCommand( KSpreadSheet* s )
00216 {
00217     sheet = s;
00218     doc = sheet->doc();
00219     doc->map()->addSheet( s );
00220 }
00221 
00222 void AddSheetCommand::execute()
00223 {
00224     sheet->workbook()->insertSheet( sheet );
00225     doc->insertSheet( sheet );
00226 }
00227 
00228 void AddSheetCommand::unexecute()
00229 {
00230     sheet->workbook()->takeSheet( sheet );
00231     doc->takeSheet( sheet );
00232 }
00233 
00234 QString AddSheetCommand::name() const
00235 {
00236     return i18n("Add Sheet");
00237 }
00238 
00239 
00240 // ----- RemoveSheetCommand -----
00241 
00242 RemoveSheetCommand::RemoveSheetCommand( KSpreadSheet* s )
00243 {
00244     sheet = s;
00245     doc = sheet->doc();
00246 }
00247 
00248 void RemoveSheetCommand::execute()
00249 {
00250     sheet->workbook()->takeSheet( sheet );
00251     doc->takeSheet( sheet );
00252 }
00253 
00254 void RemoveSheetCommand::unexecute()
00255 {
00256     sheet->workbook()->insertSheet( sheet );
00257     doc->insertSheet( sheet );
00258 }
00259 
00260 QString RemoveSheetCommand::name() const
00261 {
00262     return i18n("Remove Sheet");
00263 }
00264 
00265 // ----- SheetPropertiesCommand -----
00266 
00267 SheetPropertiesCommand::SheetPropertiesCommand( KSpreadDoc* d, KSpreadSheet* s )
00268 {
00269     sheet = s;
00270     doc = d;
00271     oldDirection = newDirection = sheet->layoutDirection();
00272     oldAutoCalc = newAutoCalc = sheet->getAutoCalc();
00273     oldShowGrid = newShowGrid = sheet->getShowGrid();
00274     oldShowPageBorders = newShowPageBorders = sheet->isShowPageBorders();
00275     oldShowFormula = newShowFormula = sheet->getShowFormula();
00276     oldHideZero = newHideZero = sheet->getHideZero();
00277     oldShowFormulaIndicator = newShowFormulaIndicator = sheet->getShowFormulaIndicator();
00278     oldColumnAsNumber = newColumnAsNumber = sheet->getShowColumnNumber();
00279     oldLcMode = newLcMode = sheet->getLcMode();
00280     oldCapitalizeFirstLetter = newCapitalizeFirstLetter = sheet->getFirstLetterUpper();
00281 }
00282 
00283 QString SheetPropertiesCommand::name() const
00284 {
00285     return i18n("Change Sheet Properties");
00286 }
00287 
00288 void SheetPropertiesCommand::setLayoutDirection( KSpreadSheet::LayoutDirection dir )
00289 {
00290     newDirection = dir;
00291 }
00292 
00293 void SheetPropertiesCommand::setAutoCalc( bool b )
00294 {
00295     newAutoCalc = b;
00296 }
00297 
00298 void SheetPropertiesCommand::setShowGrid( bool b )
00299 {
00300     newShowGrid = b;
00301 }
00302 
00303 void SheetPropertiesCommand::setShowPageBorders( bool b )
00304 {
00305     newShowPageBorders = b;
00306 }
00307 
00308 void SheetPropertiesCommand::setShowFormula( bool b )
00309 {
00310     newShowFormula = b;
00311 }
00312 
00313 void SheetPropertiesCommand::setHideZero( bool b  )
00314 {
00315     newHideZero = b;
00316 }
00317 
00318 void SheetPropertiesCommand::setShowFormulaIndicator( bool b )
00319 {
00320     newShowFormulaIndicator = b;
00321 }
00322 
00323 void SheetPropertiesCommand::setColumnAsNumber( bool b  )
00324 {
00325     newColumnAsNumber = b;
00326 }
00327 
00328 void SheetPropertiesCommand::setLcMode( bool b  )
00329 {
00330     newLcMode = b;
00331 }
00332 
00333 void SheetPropertiesCommand::setCapitalizeFirstLetter( bool b )
00334 {
00335     newCapitalizeFirstLetter = b;
00336 }
00337 
00338 void SheetPropertiesCommand::execute()
00339 {
00340     sheet->setLayoutDirection( newDirection );
00341     sheet->setAutoCalc( newAutoCalc );
00342     sheet->setShowGrid( newShowGrid );
00343     sheet->setShowPageBorders( newShowPageBorders );
00344     sheet->setShowFormula( newShowFormula );
00345     sheet->setHideZero( newHideZero );
00346     sheet->setShowFormulaIndicator( newShowFormulaIndicator );
00347     sheet->setShowColumnNumber( newColumnAsNumber );
00348     sheet->setLcMode( newLcMode );
00349     sheet->setFirstLetterUpper( newCapitalizeFirstLetter );
00350     doc->addDamage( new SheetDamage( sheet, SheetDamage::PropertiesChanged ) );
00351 }
00352 
00353 void SheetPropertiesCommand::unexecute()
00354 {
00355     sheet->setLayoutDirection( oldDirection );
00356     sheet->setAutoCalc( oldAutoCalc );
00357     sheet->setShowGrid( oldShowGrid );
00358     sheet->setShowPageBorders( oldShowPageBorders );
00359     sheet->setShowFormula( oldShowFormula );
00360     sheet->setHideZero( oldHideZero );
00361     sheet->setShowFormulaIndicator( oldShowFormulaIndicator );
00362     sheet->setShowColumnNumber( oldColumnAsNumber );
00363     sheet->setLcMode( oldLcMode );
00364     sheet->setFirstLetterUpper( oldCapitalizeFirstLetter );
00365     doc->addDamage( new SheetDamage( sheet, SheetDamage::PropertiesChanged ) );
00366 }
00367 
00368 
00369 // ----- InsertColumnCommand -----
00370 
00371 InsertColumnCommand::InsertColumnCommand( KSpreadSheet* s , unsigned int _column, unsigned int _nbCol )
00372 {
00373   doc = s->doc();
00374   sheetName = s->sheetName();
00375   insertPosColumn = _column;
00376   nbColumnInserted = _nbCol;
00377 }
00378 
00379 void InsertColumnCommand::execute()
00380 {
00381     KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00382     if( !sheet ) return;
00383     sheet->insertColumn( insertPosColumn,nbColumnInserted);
00384 }
00385 
00386 void InsertColumnCommand::unexecute()
00387 {
00388     KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00389     if( !sheet ) return;
00390     sheet->removeColumn( insertPosColumn,nbColumnInserted );
00391 }
00392 
00393 QString InsertColumnCommand::name() const
00394 {
00395     return i18n("Insert Columns");
00396 }
00397 
00398 
00399 // ----- DefinePrintRangeCommand -----
00400 
00401 
00402 DefinePrintRangeCommand::DefinePrintRangeCommand( KSpreadSheet *s )
00403 {
00404   doc = s->doc();
00405   sheetName = s->sheetName();
00406   printRange = s->print()->printRange();
00407 }
00408 
00409 void DefinePrintRangeCommand::execute()
00410 {
00411     KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00412     if( !sheet ) return;
00413     sheet->print()->setPrintRange( printRangeRedo );
00414 
00415 }
00416 
00417 void DefinePrintRangeCommand::unexecute()
00418 {
00419     KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00420     if( !sheet ) return;
00421     printRangeRedo = sheet->print()->printRange();
00422     sheet->print()->setPrintRange( printRange );
00423 }
00424 
00425 QString DefinePrintRangeCommand::name() const
00426 {
00427     return i18n("Set Page Layout");
00428 }
00429 
00430 // ----- PaperLayoutCommand -----
00431 
00432 
00433 PaperLayoutCommand::PaperLayoutCommand( KSpreadSheet *s )
00434 {
00435   doc = s->doc();
00436   sheetName = s->sheetName();
00437   pl = s->print()->paperLayout();
00438   hf = s->print()->headFootLine();
00439   unit = doc->getUnit();
00440   printGrid = s->print()->printGrid();
00441   printCommentIndicator = s->print()->printCommentIndicator();
00442   printFormulaIndicator = s->print()->printFormulaIndicator();
00443   printRange = s->print()->printRange();
00444   printRepeatColumns = s->print()->printRepeatColumns();
00445   printRepeatRows = s->print()->printRepeatRows();
00446   zoom = s->print()->zoom();
00447   pageLimitX = s->print()->pageLimitX();
00448   pageLimitY = s->print()->pageLimitY();
00449 }
00450 
00451 void PaperLayoutCommand::execute()
00452 {
00453     KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00454     if( !sheet ) return;
00455     KSpreadSheetPrint* print = sheet->print();
00456 
00457     print->setPaperLayout( plRedo.ptLeft,  plRedo.ptTop,
00458                            plRedo.ptRight, plRedo.ptBottom,
00459                            plRedo.format, plRedo.orientation );
00460 
00461     print->setHeadFootLine( hfRedo.headLeft, hfRedo.headMid, hfRedo.headRight,
00462                             hfRedo.footLeft, hfRedo.footMid, hfRedo.footRight );
00463 
00464     doc->setUnit( unitRedo );
00465 
00466     print->setPrintGrid( printGridRedo );
00467     print->setPrintCommentIndicator( printCommentIndicatorRedo );
00468     print->setPrintFormulaIndicator( printFormulaIndicatorRedo );
00469 
00470     print->setPrintRange( printRangeRedo );
00471     print->setPrintRepeatColumns( printRepeatColumnsRedo );
00472     print->setPrintRepeatRows( printRepeatRowsRedo );
00473 
00474     print->setZoom( zoomRedo );
00475 
00476     print->setPageLimitX( pageLimitX );
00477     print->setPageLimitY( pageLimitY );
00478 }
00479 
00480 void PaperLayoutCommand::unexecute()
00481 {
00482     KSpreadSheet* sheet = doc->map()->findSheet( sheetName );
00483     if( !sheet ) return;
00484     KSpreadSheetPrint* print = sheet->print();
00485     plRedo = print->paperLayout();
00486     print->setPaperLayout( pl.ptLeft,  pl.ptTop,
00487                            pl.ptRight, pl.ptBottom,
00488                            pl.format,  pl.orientation );
00489 
00490     hfRedo = print->headFootLine();
00491     print->setHeadFootLine( hf.headLeft, hf.headMid, hf.headRight,
00492                             hf.footLeft, hf.footMid, hf.footRight );
00493 
00494     unitRedo = doc->getUnit();
00495     doc->setUnit( unit );
00496 
00497     printGridRedo = print->printGrid();
00498     print->setPrintGrid( printGrid );
00499 
00500     printCommentIndicatorRedo = print->printCommentIndicator();
00501     print->setPrintCommentIndicator( printCommentIndicator );
00502 
00503     printFormulaIndicatorRedo = print->printFormulaIndicator();
00504     print->setPrintFormulaIndicator( printFormulaIndicator );
00505 
00506     printRangeRedo = print->printRange();
00507     print->setPrintRange( printRange );
00508 
00509     printRepeatColumnsRedo = print->printRepeatColumns();
00510     print->setPrintRepeatColumns( printRepeatColumns );
00511 
00512     printRepeatRowsRedo = print->printRepeatRows();
00513     print->setPrintRepeatRows( printRepeatRows );
00514 
00515     zoomRedo = print->zoom();
00516     print->setZoom( zoom );
00517 
00518     pageLimitXRedo = print->pageLimitX();
00519     print->setPageLimitX( pageLimitX );
00520 
00521     pageLimitYRedo = print->pageLimitY();
00522     print->setPageLimitY( pageLimitY );
00523 
00524 }
00525 
00526 QString PaperLayoutCommand::name() const
00527 {
00528     return i18n("Set Page Layout");
00529 }
00530 
00531 LinkCommand::LinkCommand( KSpreadCell* c, const QString& text, const QString& link )
00532 {
00533   cell = c;
00534   oldText = cell->text();
00535   oldLink = cell->link();
00536   newText = text;
00537   newLink = link;
00538 
00539   KSpreadSheet* s = cell->sheet();
00540   if( s ) doc = s->doc();
00541 }
00542 
00543 void LinkCommand::execute()
00544 {
00545   if( !cell ) return;
00546 
00547   if( !newText.isEmpty() )
00548     cell->setCellText( newText );
00549   cell->setLink( newLink  );
00550 
00551   doc->addDamage( new CellDamage( cell ) );
00552 }
00553 
00554 void LinkCommand::unexecute()
00555 {
00556   if( !cell ) return;
00557 
00558   cell->setCellText( oldText );
00559   cell->setLink( oldLink );
00560 
00561   doc->addDamage( new CellDamage( cell ) );
00562 }
00563 
00564 QString LinkCommand::name() const
00565 {
00566   return newLink.isEmpty() ? i18n("Remove Link") : i18n("Set Link");
00567 }
00568 
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:42:44 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003