kspread

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., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "commands.h"
00022 #include "damages.h"
00023 #include "kspread_canvas.h"
00024 #include "kspread_doc.h"
00025 #include "kspread_locale.h"
00026 #include "kspread_map.h"
00027 #include "kspread_sheet.h"
00028 #include "kspread_undo.h"
00029 #include "kspread_util.h"
00030 
00031 #include "kspread_sheetprint.h"
00032 
00033 using namespace KSpread;
00034 
00035 // ----- UndoWrapperCommand -----
00036 
00037 UndoWrapperCommand::UndoWrapperCommand( UndoAction* ua )
00038 {
00039   undoAction = ua;
00040 }
00041 
00042 void UndoWrapperCommand::execute()
00043 {
00044   // This is not really safe and functional, but UndoWrapperCommand
00045   // is a workaround anyway. So leave it as it it (Ariya)
00046   undoAction->redo();
00047 }
00048 
00049 void UndoWrapperCommand::unexecute()
00050 {
00051   undoAction->undo();
00052 }
00053 
00054 QString UndoWrapperCommand::name() const
00055 {
00056   return undoAction->getName();
00057 }
00058 
00059 // ----- MergeCellsCommand -----
00060 
00061 MergeCellCommand::MergeCellCommand( Cell* c, int cs, int rs )
00062 {
00063   cell = c;
00064   colSpan = cs;
00065   rowSpan = rs;
00066   oldColSpan = cell->extraXCells();
00067   oldRowSpan = cell->extraYCells();
00068   if( cell )
00069   {
00070     QRect area( cell->column(), cell->row(), cs+1, rs+1 );
00071     rangeName = util_rangeName( area );
00072   }
00073 }
00074 
00075 QString MergeCellCommand::name() const
00076 {
00077   if( rangeName.isEmpty() )
00078     return i18n("Merge Cells");
00079   else
00080     return i18n("Merge Cells %1").arg( rangeName );
00081 }
00082 
00083 void MergeCellCommand::execute()
00084 {
00085   Sheet* sheet = cell->sheet();
00086   if( !sheet ) return;
00087   sheet->changeMergedCell( cell->column(), cell->row(), colSpan, rowSpan);
00088 }
00089 
00090 void MergeCellCommand::unexecute()
00091 {
00092   Sheet* sheet = cell->sheet();
00093   if( !sheet ) return;
00094   sheet->changeMergedCell( cell->column(), cell->row(), oldColSpan, oldRowSpan);
00095 }
00096 
00097 // ----- DissociateCellCommand -----
00098 
00099 DissociateCellCommand::DissociateCellCommand( Cell* c )
00100 {
00101   cell = c;
00102   oldColSpan = cell->extraXCells();
00103   oldRowSpan = cell->extraYCells();
00104 }
00105 
00106 QString DissociateCellCommand::name() const
00107 {
00108   return i18n("Dissociate Cell");
00109 }
00110 
00111 void DissociateCellCommand::execute()
00112 {
00113   Sheet* sheet = cell->sheet();
00114   if( !sheet ) return;
00115   sheet->changeMergedCell( cell->column(), cell->row(), 0, 0 );
00116 }
00117 
00118 void DissociateCellCommand::unexecute()
00119 {
00120   Sheet* sheet = cell->sheet();
00121   if( !sheet ) return;
00122   sheet->changeMergedCell( cell->column(), cell->row(), oldColSpan, oldRowSpan);
00123 }
00124 
00125 // ----- RenameSheetCommand -----
00126 
00127 RenameSheetCommand::RenameSheetCommand( Sheet* s, const QString &name )
00128 {
00129   sheet = s;
00130   if( s ) oldName = s->sheetName();
00131   newName = name;
00132 }
00133 
00134 QString RenameSheetCommand::name() const
00135 {
00136   return i18n("Rename Sheet");
00137 }
00138 
00139 void RenameSheetCommand::execute()
00140 {
00141   if( sheet )
00142     sheet->setSheetName( newName );
00143 }
00144 
00145 void RenameSheetCommand::unexecute()
00146 {
00147   if( sheet )
00148     sheet->setSheetName( oldName );
00149 }
00150 
00151 // ----- HideSheetCommand -----
00152 
00153 HideSheetCommand::HideSheetCommand( Sheet* sheet )
00154 {
00155   doc = sheet->doc();
00156   sheetName = sheet->sheetName();
00157 }
00158 
00159 void HideSheetCommand::execute()
00160 {
00161   Sheet* sheet = doc->map()->findSheet( sheetName );
00162   if( !sheet ) return;
00163 
00164   sheet->hideSheet( true );
00165 }
00166 
00167 void HideSheetCommand::unexecute()
00168 {
00169   Sheet* sheet = doc->map()->findSheet( sheetName );
00170   if( !sheet ) return;
00171 
00172   sheet->hideSheet( false );
00173 }
00174 
00175 QString HideSheetCommand::name() const
00176 {
00177     QString n = QString( i18n("Hide Sheet %1") ).arg( sheetName );
00178     if( n.length() > 64 ) n = i18n("Hide Sheet");
00179     return n;
00180 }
00181 
00182 // ----- ShowSheetCommand -----
00183 
00184 ShowSheetCommand::ShowSheetCommand( Sheet* sheet )
00185 {
00186   doc = sheet->doc();
00187   sheetName = sheet->sheetName();
00188 }
00189 
00190 void ShowSheetCommand::execute()
00191 {
00192   Sheet* sheet = doc->map()->findSheet( sheetName );
00193   if( !sheet ) return;
00194 
00195   sheet->hideSheet( false );
00196 }
00197 
00198 void ShowSheetCommand::unexecute()
00199 {
00200   Sheet* sheet = doc->map()->findSheet( sheetName );
00201   if( !sheet ) return;
00202 
00203   sheet->hideSheet( true );
00204 }
00205 
00206 QString ShowSheetCommand::name() const
00207 {
00208     QString n = QString( i18n("Show Sheet %1") ).arg( sheetName );
00209     if( n.length() > 64 ) n = i18n("Show Sheet");
00210     return n;
00211 }
00212 
00213 
00214 // ----- AddSheetCommand -----
00215 
00216 AddSheetCommand::AddSheetCommand( Sheet* s )
00217 {
00218     sheet = s;
00219     doc = sheet->doc();
00220     doc->map()->addSheet( s );
00221 }
00222 
00223 void AddSheetCommand::execute()
00224 {
00225     sheet->workbook()->insertSheet( sheet );
00226     doc->insertSheet( sheet );
00227 }
00228 
00229 void AddSheetCommand::unexecute()
00230 {
00231     sheet->workbook()->takeSheet( sheet );
00232     doc->takeSheet( sheet );
00233 }
00234 
00235 QString AddSheetCommand::name() const
00236 {
00237     return i18n("Add Sheet");
00238 }
00239 
00240 
00241 // ----- RemoveSheetCommand -----
00242 
00243 RemoveSheetCommand::RemoveSheetCommand( Sheet* s )
00244 {
00245     sheet = s;
00246     doc = sheet->doc();
00247 }
00248 
00249 void RemoveSheetCommand::execute()
00250 {
00251     sheet->workbook()->takeSheet( sheet );
00252     doc->takeSheet( sheet );
00253 }
00254 
00255 void RemoveSheetCommand::unexecute()
00256 {
00257     sheet->workbook()->insertSheet( sheet );
00258     doc->insertSheet( sheet );
00259 }
00260 
00261 QString RemoveSheetCommand::name() const
00262 {
00263     return i18n("Remove Sheet");
00264 }
00265 
00266 // ----- SheetPropertiesCommand -----
00267 
00268 SheetPropertiesCommand::SheetPropertiesCommand( Doc* d, Sheet* s )
00269 {
00270     sheet = s;
00271     doc = d;
00272     oldDirection = newDirection = sheet->layoutDirection();
00273     oldAutoCalc = newAutoCalc = sheet->getAutoCalc();
00274     oldShowGrid = newShowGrid = sheet->getShowGrid();
00275     oldShowPageBorders = newShowPageBorders = sheet->isShowPageBorders();
00276     oldShowFormula = newShowFormula = sheet->getShowFormula();
00277     oldHideZero = newHideZero = sheet->getHideZero();
00278     oldShowFormulaIndicator = newShowFormulaIndicator = sheet->getShowFormulaIndicator();
00279     oldShowCommentIndicator = newShowCommentIndicator = sheet->getShowCommentIndicator();
00280     oldColumnAsNumber = newColumnAsNumber = sheet->getShowColumnNumber();
00281     oldLcMode = newLcMode = sheet->getLcMode();
00282     oldCapitalizeFirstLetter = newCapitalizeFirstLetter = sheet->getFirstLetterUpper();
00283 }
00284 
00285 QString SheetPropertiesCommand::name() const
00286 {
00287     return i18n("Change Sheet Properties");
00288 }
00289 
00290 void SheetPropertiesCommand::setLayoutDirection( Sheet::LayoutDirection dir )
00291 {
00292     newDirection = dir;
00293 }
00294 
00295 void SheetPropertiesCommand::setAutoCalc( bool b )
00296 {
00297     newAutoCalc = b;
00298 }
00299 
00300 void SheetPropertiesCommand::setShowGrid( bool b )
00301 {
00302     newShowGrid = b;
00303 }
00304 
00305 void SheetPropertiesCommand::setShowPageBorders( bool b )
00306 {
00307     newShowPageBorders = b;
00308 }
00309 
00310 void SheetPropertiesCommand::setShowFormula( bool b )
00311 {
00312     newShowFormula = b;
00313 }
00314 
00315 void SheetPropertiesCommand::setHideZero( bool b  )
00316 {
00317     newHideZero = b;
00318 }
00319 
00320 void SheetPropertiesCommand::setShowFormulaIndicator( bool b )
00321 {
00322     newShowFormulaIndicator = b;
00323 }
00324 
00325 void SheetPropertiesCommand::setShowCommentIndicator( bool b )
00326 {
00327   newShowCommentIndicator = b;
00328 }
00329 
00330 void SheetPropertiesCommand::setColumnAsNumber( bool b  )
00331 {
00332     newColumnAsNumber = b;
00333 }
00334 
00335 void SheetPropertiesCommand::setLcMode( bool b  )
00336 {
00337     newLcMode = b;
00338 }
00339 
00340 void SheetPropertiesCommand::setCapitalizeFirstLetter( bool b )
00341 {
00342     newCapitalizeFirstLetter = b;
00343 }
00344 
00345 void SheetPropertiesCommand::execute()
00346 {
00347     sheet->setLayoutDirection( newDirection );
00348     sheet->setAutoCalc( newAutoCalc );
00349     sheet->setShowGrid( newShowGrid );
00350     sheet->setShowPageBorders( newShowPageBorders );
00351     sheet->setShowFormula( newShowFormula );
00352     sheet->setHideZero( newHideZero );
00353     sheet->setShowFormulaIndicator( newShowFormulaIndicator );
00354     sheet->setShowCommentIndicator( newShowCommentIndicator );
00355     sheet->setShowColumnNumber( newColumnAsNumber );
00356     sheet->setLcMode( newLcMode );
00357     sheet->setFirstLetterUpper( newCapitalizeFirstLetter );
00358     doc->addDamage( new SheetDamage( sheet, SheetDamage::PropertiesChanged ) );
00359 }
00360 
00361 void SheetPropertiesCommand::unexecute()
00362 {
00363     sheet->setLayoutDirection( oldDirection );
00364     sheet->setAutoCalc( oldAutoCalc );
00365     sheet->setShowGrid( oldShowGrid );
00366     sheet->setShowPageBorders( oldShowPageBorders );
00367     sheet->setShowFormula( oldShowFormula );
00368     sheet->setHideZero( oldHideZero );
00369     sheet->setShowFormulaIndicator( oldShowFormulaIndicator );
00370     sheet->setShowCommentIndicator( oldShowCommentIndicator );
00371     sheet->setShowColumnNumber( oldColumnAsNumber );
00372     sheet->setLcMode( oldLcMode );
00373     sheet->setFirstLetterUpper( oldCapitalizeFirstLetter );
00374     doc->addDamage( new SheetDamage( sheet, SheetDamage::PropertiesChanged ) );
00375 }
00376 
00377 
00378 // ----- InsertColumnCommand -----
00379 
00380 InsertColumnCommand::InsertColumnCommand( Sheet* s , unsigned int _column, unsigned int _nbCol )
00381 {
00382   doc = s->doc();
00383   sheetName = s->sheetName();
00384   insertPosColumn = _column;
00385   nbColumnInserted = _nbCol;
00386 }
00387 
00388 void InsertColumnCommand::execute()
00389 {
00390     Sheet* sheet = doc->map()->findSheet( sheetName );
00391     if( !sheet ) return;
00392     sheet->insertColumn( insertPosColumn,nbColumnInserted);
00393 }
00394 
00395 void InsertColumnCommand::unexecute()
00396 {
00397     Sheet* sheet = doc->map()->findSheet( sheetName );
00398     if( !sheet ) return;
00399     sheet->removeColumn( insertPosColumn,nbColumnInserted );
00400 }
00401 
00402 QString InsertColumnCommand::name() const
00403 {
00404     return i18n("Insert Columns");
00405 }
00406 
00407 
00408 // ----- DefinePrintRangeCommand -----
00409 
00410 
00411 DefinePrintRangeCommand::DefinePrintRangeCommand( Sheet *s )
00412 {
00413   doc = s->doc();
00414   sheetName = s->sheetName();
00415   printRange = s->print()->printRange();
00416 }
00417 
00418 void DefinePrintRangeCommand::execute()
00419 {
00420     Sheet* sheet = doc->map()->findSheet( sheetName );
00421     if( !sheet ) return;
00422     sheet->print()->setPrintRange( printRangeRedo );
00423 
00424 }
00425 
00426 void DefinePrintRangeCommand::unexecute()
00427 {
00428     Sheet* sheet = doc->map()->findSheet( sheetName );
00429     if( !sheet ) return;
00430     printRangeRedo = sheet->print()->printRange();
00431     sheet->print()->setPrintRange( printRange );
00432 }
00433 
00434 QString DefinePrintRangeCommand::name() const
00435 {
00436     return i18n("Set Page Layout");
00437 }
00438 
00439 // ----- PaperLayoutCommand -----
00440 
00441 
00442 PaperLayoutCommand::PaperLayoutCommand( Sheet *s )
00443 {
00444   doc = s->doc();
00445   sheetName = s->sheetName();
00446   pl = s->print()->paperLayout();
00447   hf = s->print()->headFootLine();
00448   unit = doc->unit();
00449   printGrid = s->print()->printGrid();
00450   printCommentIndicator = s->print()->printCommentIndicator();
00451   printFormulaIndicator = s->print()->printFormulaIndicator();
00452   printRange = s->print()->printRange();
00453   printRepeatColumns = s->print()->printRepeatColumns();
00454   printRepeatRows = s->print()->printRepeatRows();
00455   zoom = s->print()->zoom();
00456   pageLimitX = s->print()->pageLimitX();
00457   pageLimitY = s->print()->pageLimitY();
00458 }
00459 
00460 void PaperLayoutCommand::execute()
00461 {
00462     Sheet* sheet = doc->map()->findSheet( sheetName );
00463     if( !sheet ) return;
00464     SheetPrint* print = sheet->print();
00465 
00466     print->setPaperLayout( plRedo.ptLeft,  plRedo.ptTop,
00467                            plRedo.ptRight, plRedo.ptBottom,
00468                            plRedo.format, plRedo.orientation );
00469 
00470     print->setHeadFootLine( hfRedo.headLeft, hfRedo.headMid, hfRedo.headRight,
00471                             hfRedo.footLeft, hfRedo.footMid, hfRedo.footRight );
00472 
00473     doc->setUnit( unitRedo );
00474 
00475     print->setPrintGrid( printGridRedo );
00476     print->setPrintCommentIndicator( printCommentIndicatorRedo );
00477     print->setPrintFormulaIndicator( printFormulaIndicatorRedo );
00478 
00479     print->setPrintRange( printRangeRedo );
00480     print->setPrintRepeatColumns( printRepeatColumnsRedo );
00481     print->setPrintRepeatRows( printRepeatRowsRedo );
00482 
00483     print->setZoom( zoomRedo );
00484 
00485     print->setPageLimitX( pageLimitX );
00486     print->setPageLimitY( pageLimitY );
00487 }
00488 
00489 void PaperLayoutCommand::unexecute()
00490 {
00491     Sheet* sheet = doc->map()->findSheet( sheetName );
00492     if( !sheet ) return;
00493     SheetPrint* print = sheet->print();
00494     plRedo = print->paperLayout();
00495     print->setPaperLayout( pl.ptLeft,  pl.ptTop,
00496                            pl.ptRight, pl.ptBottom,
00497                            pl.format,  pl.orientation );
00498 
00499     hfRedo = print->headFootLine();
00500     print->setHeadFootLine( hf.headLeft, hf.headMid, hf.headRight,
00501                             hf.footLeft, hf.footMid, hf.footRight );
00502 
00503     unitRedo = doc->unit();
00504     doc->setUnit( unit );
00505 
00506     printGridRedo = print->printGrid();
00507     print->setPrintGrid( printGrid );
00508 
00509     printCommentIndicatorRedo = print->printCommentIndicator();
00510     print->setPrintCommentIndicator( printCommentIndicator );
00511 
00512     printFormulaIndicatorRedo = print->printFormulaIndicator();
00513     print->setPrintFormulaIndicator( printFormulaIndicator );
00514 
00515     printRangeRedo = print->printRange();
00516     print->setPrintRange( printRange );
00517 
00518     printRepeatColumnsRedo = print->printRepeatColumns();
00519     print->setPrintRepeatColumns( printRepeatColumns );
00520 
00521     printRepeatRowsRedo = print->printRepeatRows();
00522     print->setPrintRepeatRows( printRepeatRows );
00523 
00524     zoomRedo = print->zoom();
00525     print->setZoom( zoom );
00526 
00527     pageLimitXRedo = print->pageLimitX();
00528     print->setPageLimitX( pageLimitX );
00529 
00530     pageLimitYRedo = print->pageLimitY();
00531     print->setPageLimitY( pageLimitY );
00532 
00533 }
00534 
00535 QString PaperLayoutCommand::name() const
00536 {
00537     return i18n("Set Page Layout");
00538 }
00539 
00540 LinkCommand::LinkCommand( Cell* c, const QString& text, const QString& link )
00541 {
00542   cell = c;
00543   oldText = cell->text();
00544   oldLink = cell->link();
00545   newText = text;
00546   newLink = link;
00547 
00548   Sheet* s = cell->sheet();
00549   if( s ) doc = s->doc();
00550 }
00551 
00552 void LinkCommand::execute()
00553 {
00554   if( !cell ) return;
00555 
00556   if( !newText.isEmpty() )
00557     cell->setCellText( newText );
00558   cell->setLink( newLink  );
00559 
00560   doc->addDamage( new CellDamage( cell ) );
00561 }
00562 
00563 void LinkCommand::unexecute()
00564 {
00565   if( !cell ) return;
00566 
00567   cell->setCellText( oldText );
00568   cell->setLink( oldLink );
00569 
00570   doc->addDamage( new CellDamage( cell ) );
00571 }
00572 
00573 QString LinkCommand::name() const
00574 {
00575   return newLink.isEmpty() ? i18n("Remove Link") : i18n("Set Link");
00576 }
00577 
00578 ChangeObjectGeometryCommand::ChangeObjectGeometryCommand( EmbeddedObject *_obj, const KoPoint &_m_diff, const KoSize &_r_diff )
00579   : m_diff( _m_diff ), r_diff( _r_diff )
00580 {
00581   obj = _obj;
00582   obj->incCmdRef();
00583   doc = obj->sheet()->doc();
00584 }
00585 
00586 ChangeObjectGeometryCommand::~ChangeObjectGeometryCommand()
00587 {
00588   obj->decCmdRef();
00589 }
00590 
00591 void ChangeObjectGeometryCommand::execute()
00592 {
00593     doc->repaint( obj->geometry() );
00594 
00595     KoRect geometry = obj->geometry();
00596     geometry.moveBy( m_diff.x(),  m_diff.y() );
00597     geometry.setWidth( geometry.width() + r_diff.width() );
00598     geometry.setHeight( geometry.height() + r_diff.height() );
00599     obj->setGeometry( geometry );
00600 
00601 //     if ( object->isSelected())
00602 //       doc->updateObjectStatusBarItem();
00603     doc->repaint( obj );
00604 }
00605 
00606 void ChangeObjectGeometryCommand::unexecute()
00607 {
00608   doc->repaint( obj->geometry() );
00609 
00610   KoRect geometry = obj->geometry();
00611   geometry.moveBy( -m_diff.x(),  -m_diff.y() );
00612   geometry.setWidth( geometry.width() - r_diff.width() );
00613   geometry.setHeight( geometry.height() - r_diff.height() );
00614   obj->setGeometry( geometry );
00615 
00616 //     if ( object->isSelected())
00617 //       doc->updateObjectStatusBarItem();
00618   doc->repaint( obj );
00619 }
00620 
00621 QString ChangeObjectGeometryCommand::name() const
00622 {
00623   /*if ( fabs( obj->geometry().width() - newGeometry.width() )<1e-3  && fabs( obj->geometry().height() - newGeometry.height() ) < 1e-3 )
00624     return i18n("Move Object");
00625   else */
00626     return i18n("Resize Object");
00627 }
00628 
00629 RemoveObjectCommand::RemoveObjectCommand( EmbeddedObject *_obj, bool _cut )
00630 {
00631   obj = _obj;
00632   cut = _cut;
00633   doc = obj->sheet()->doc();
00634 }
00635 
00636 RemoveObjectCommand::~RemoveObjectCommand()
00637 {
00638   if ( !executed )
00639     return;
00640   //kdDebug() << "*********Deleting object..." << endl;
00641   if ( obj->getType() == OBJECT_CHART )
00642   {
00643     EmbeddedKOfficeObject *chart = dynamic_cast<EmbeddedKOfficeObject *>(obj);
00644     chart->embeddedObject()->setDeleted(true);
00645   }
00646 
00647   delete obj;
00648 }
00649 
00650 void RemoveObjectCommand::execute()
00651 {
00652 
00653 //  I don't think we need this:
00654 //       Make shure that this child has no active embedded view -> activate ourselfs
00655 //       doc()->emitBeginOperation( false );
00656 //       partManager()->setActivePart( koDocument(), this );
00657 //       partManager()->setSelectedPart( 0 );
00658 //       doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
00659 
00660   doc->embeddedObjects().removeRef( obj );
00661   if ( obj->getType() == OBJECT_CHART ||  obj->getType()== OBJECT_KOFFICE_PART)
00662   {
00663     EmbeddedKOfficeObject *eko = dynamic_cast<EmbeddedKOfficeObject *>(obj);
00664     eko->embeddedObject()->setDeleted(true);
00665   }
00666 
00667   obj->setSelected( false );
00668   doc->repaint( obj );
00669   executed = true;
00670 }
00671 
00672 void RemoveObjectCommand::unexecute()
00673 {
00674   doc->embeddedObjects().append( obj );
00675   if ( obj->getType() == OBJECT_CHART ||  obj->getType()== OBJECT_KOFFICE_PART)
00676   {
00677     EmbeddedKOfficeObject *eko = dynamic_cast<EmbeddedKOfficeObject *>(obj);
00678     eko->embeddedObject()->setDeleted(false);
00679   }
00680   doc->repaint( obj );
00681   executed = false;
00682 }
00683 
00684 QString RemoveObjectCommand::name() const
00685 {
00686   if ( cut )
00687     return i18n("Cut Object");
00688   else
00689     return i18n("Remove Object");
00690 }
00691 
00692 InsertObjectCommand::InsertObjectCommand( const KoRect& _geometry, KoDocumentEntry& _entry, Canvas *_canvas ) //child
00693 {
00694   geometry = _geometry;
00695   entry = _entry;
00696   canvas = _canvas;
00697   type = OBJECT_KOFFICE_PART;
00698   obj = 0;
00699 }
00700 
00701 InsertObjectCommand::InsertObjectCommand(const KoRect& _geometry, KoDocumentEntry& _entry, const QRect& _data, Canvas *_canvas ) //chart
00702 {
00703   geometry = _geometry;
00704   entry = _entry;
00705   data = _data;
00706   canvas = _canvas;
00707   type = OBJECT_CHART;
00708   obj = 0;
00709 }
00710 
00711 InsertObjectCommand::InsertObjectCommand( const KoRect& _geometry , KURL& _file, Canvas *_canvas ) //picture
00712 {
00713   //In the case of pictures, only the top left point of the rectangle is relevant
00714   geometry = _geometry;
00715   file = _file;
00716   canvas = _canvas;
00717   type = OBJECT_PICTURE;
00718   obj = 0;
00719 }
00720 
00721 InsertObjectCommand::~InsertObjectCommand()
00722 {
00723   if ( executed )
00724     return;
00725   //kdDebug() << "*********Deleting object..." << endl;
00726   if ( obj->getType() == OBJECT_CHART )
00727   {
00728     EmbeddedKOfficeObject *chart = dynamic_cast<EmbeddedKOfficeObject *>(obj);
00729     chart->embeddedObject()->setDeleted(true);
00730   }
00731 
00732   delete obj;
00733 }
00734 
00735 void InsertObjectCommand::execute()
00736 {
00737   if ( obj ) //restore the object which was removed from the object list in InsertObjectCommand::unexecute()
00738   {
00739     canvas->doc()->embeddedObjects().append( obj );
00740     canvas->doc()->repaint( obj );
00741   }
00742   else
00743   {
00744     bool success = false;
00745     switch ( type )
00746     {
00747       case OBJECT_CHART:
00748       {
00749         success = canvas->activeSheet()->insertChart( geometry, entry, data );
00750         break;
00751       }
00752       case OBJECT_KOFFICE_PART:
00753       {
00754         success = canvas->activeSheet()->insertChild( geometry, entry );
00755         break;
00756       }
00757       case OBJECT_PICTURE:
00758       {
00759         success = canvas->activeSheet()->insertPicture( geometry.topLeft() , file );
00760         break;
00761       }
00762       default:
00763         break;
00764     }
00765     if ( success )
00766     {
00767       obj = canvas->doc()->embeddedObjects().last();
00768       obj->sheet()->unifyObjectName( obj );
00769     }
00770     else
00771       obj = 0;
00772   }
00773   executed = true;
00774 }
00775 
00776 void InsertObjectCommand::unexecute()
00777 {
00778   if ( !obj )
00779     return;
00780 
00781   canvas->doc()->embeddedObjects().removeRef( obj );
00782   obj->setSelected( false );
00783   canvas->doc()->repaint( obj );
00784 
00785   executed = false;
00786 }
00787 
00788 QString InsertObjectCommand::name() const
00789 {
00790   return i18n("Insert Object");
00791 }
00792 
00793 RenameNameObjectCommand::RenameNameObjectCommand( const QString &_name, const QString &_objectName,
00794                                             EmbeddedObject *_obj, Doc *_doc ):
00795     KNamedCommand( _name ),
00796     newObjectName( _objectName ),
00797     object( _obj ),
00798     doc( _doc )
00799 {
00800     oldObjectName = object->getObjectName();
00801 
00802     m_page = object->sheet()/*doc->findPage( object )*/;
00803 }
00804 
00805 RenameNameObjectCommand::~RenameNameObjectCommand()
00806 {
00807 }
00808 
00809 void RenameNameObjectCommand::execute()
00810 {
00811     object->setObjectName( newObjectName );
00812     m_page->unifyObjectName( object );
00813 
00814 //     doc->updateSideBarItem( m_page );
00815 }
00816 
00817 void RenameNameObjectCommand::unexecute()
00818 {
00819     object->setObjectName( oldObjectName );
00820 
00821 //     doc->updateSideBarItem( m_page );
00822 }
00823 
00824 GeometryPropertiesCommand::GeometryPropertiesCommand( const QString &name, QPtrList<EmbeddedObject> &objects,
00825                                                             bool newValue, KgpType type, Doc *_doc )
00826 : KNamedCommand( name )
00827 , m_objects( objects )
00828 , m_newValue( newValue )
00829 , m_type( type )
00830     , m_doc( _doc )
00831 {
00832     QPtrListIterator<EmbeddedObject> it( m_objects );
00833     for ( ; it.current() ; ++it )
00834     {
00835         it.current()->incCmdRef();
00836         if ( m_type == ProtectSize )
00837             m_oldValue.append( it.current()->isProtect() );
00838         else if ( m_type == KeepRatio)
00839             m_oldValue.append( it.current()->isKeepRatio() );
00840     }
00841 }
00842 
00843 GeometryPropertiesCommand::GeometryPropertiesCommand( const QString &name, QValueList<bool> &lst,
00844                                                             QPtrList<EmbeddedObject> &objects, bool newValue,
00845                                                             KgpType type, Doc *_doc)
00846 : KNamedCommand( name )
00847 , m_oldValue( lst )
00848 , m_objects( objects )
00849 , m_newValue( newValue )
00850 , m_type( type )
00851 , m_doc ( _doc )
00852 {
00853     QPtrListIterator<EmbeddedObject> it( m_objects );
00854     for ( ; it.current() ; ++it )
00855         it.current()->incCmdRef();
00856 }
00857 
00858 GeometryPropertiesCommand::~GeometryPropertiesCommand()
00859 {
00860     QPtrListIterator<EmbeddedObject> it( m_objects );
00861     for ( ; it.current() ; ++it )
00862         it.current()->decCmdRef();
00863 }
00864 
00865 void GeometryPropertiesCommand::execute()
00866 {
00867     QPtrListIterator<EmbeddedObject> it( m_objects );
00868     for ( ; it.current() ; ++it )
00869     {
00870         if ( m_type == ProtectSize )
00871         {
00872             it.current()->setProtect( m_newValue );
00873             if ( it.current()->isSelected() )
00874                 m_doc->repaint( it.current() );
00875         }
00876         else if ( m_type == KeepRatio)
00877             it.current()->setKeepRatio( m_newValue );
00878     }
00879 }
00880 
00881 void GeometryPropertiesCommand::unexecute()
00882 {
00883     EmbeddedObject *obj = 0;
00884     for ( unsigned int i = 0; i < m_objects.count(); ++i ) {
00885         obj = m_objects.at( i );
00886         if ( m_type == ProtectSize )
00887         {
00888             obj->setProtect( *m_oldValue.at(i) );
00889             if ( obj->isSelected() )
00890                 m_doc->repaint( obj );
00891         }
00892         else if ( m_type == KeepRatio)
00893             obj->setKeepRatio( *m_oldValue.at(i) );
00894     }
00895 }
00896 
00897 MoveObjectByCmd::MoveObjectByCmd( const QString &_name, const KoPoint &_diff, QPtrList<EmbeddedObject> &_objects,
00898                       Doc *_doc,Sheet *_page )
00899     : KNamedCommand( _name ), diff( _diff ), objects( _objects )
00900 {
00901     objects.setAutoDelete( false );
00902     doc = _doc;
00903     m_page=_page;
00904     QPtrListIterator<EmbeddedObject> it( objects );
00905     for ( ; it.current() ; ++it )
00906     {
00907         it.current()->incCmdRef();
00908     }
00909 }
00910 
00911 MoveObjectByCmd::~MoveObjectByCmd()
00912 {
00913     QPtrListIterator<EmbeddedObject> it( objects );
00914     for ( ; it.current() ; ++it )
00915         it.current()->decCmdRef();
00916 }
00917 
00918 void MoveObjectByCmd::execute()
00919 {
00920     QRect oldRect;
00921 
00922     for ( unsigned int i = 0; i < objects.count(); i++ ) {
00923         doc->repaint( objects.at( i )->geometry() );
00924 
00925         KoRect r = objects.at( i )->geometry();
00926         r.moveBy( diff.x(), diff.y() );
00927         objects.at( i )->setGeometry( r );
00928 
00929         doc->repaint( objects.at( i ) );
00930     }
00931 }
00932 
00933 void MoveObjectByCmd::unexecute()
00934 {
00935     QRect oldRect;
00936 
00937     for ( unsigned int i = 0; i < objects.count(); i++ ) {
00938         doc->repaint( objects.at( i )->geometry() );
00939 
00940         KoRect r = objects.at( i )->geometry();
00941         r.moveBy( -diff.x(), -diff.y() );
00942         objects.at( i )->setGeometry( r );
00943 
00944         doc->repaint( objects.at( i ) );
00945     }
00946 }
KDE Home | KDE Accessibility Home | Description of Access Keys