KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

947 lines
23KB

  1. /* This file is part of the KDE project
  2. Copyright 2004 Ariya Hidayat <ariya@kde.org>
  3. Copyright 2004 Laurent Montel <montel@kde.org>
  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. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. * Boston, MA 02110-1301, USA.
  16. */
  17. #include "commands.h"
  18. #include "damages.h"
  19. #include "kspread_canvas.h"
  20. #include "kspread_doc.h"
  21. #include "kspread_locale.h"
  22. #include "kspread_map.h"
  23. #include "kspread_sheet.h"
  24. #include "kspread_undo.h"
  25. #include "kspread_util.h"
  26. #include "kspread_sheetprint.h"
  27. using namespace KSpread;
  28. // ----- UndoWrapperCommand -----
  29. UndoWrapperCommand::UndoWrapperCommand( UndoAction* ua )
  30. {
  31. undoAction = ua;
  32. }
  33. void UndoWrapperCommand::execute()
  34. {
  35. // This is not really safe and functional, but UndoWrapperCommand
  36. // is a workaround anyway. So leave it as it it (Ariya)
  37. undoAction->redo();
  38. }
  39. void UndoWrapperCommand::unexecute()
  40. {
  41. undoAction->undo();
  42. }
  43. TQString UndoWrapperCommand::name() const
  44. {
  45. return undoAction->getName();
  46. }
  47. // ----- MergeCellsCommand -----
  48. MergeCellCommand::MergeCellCommand( Cell* c, int cs, int rs )
  49. {
  50. cell = c;
  51. colSpan = cs;
  52. rowSpan = rs;
  53. oldColSpan = cell->extraXCells();
  54. oldRowSpan = cell->extraYCells();
  55. if( cell )
  56. {
  57. TQRect area( cell->column(), cell->row(), cs+1, rs+1 );
  58. rangeName = util_rangeName( area );
  59. }
  60. }
  61. TQString MergeCellCommand::name() const
  62. {
  63. if( rangeName.isEmpty() )
  64. return i18n("Merge Cells");
  65. else
  66. return i18n("Merge Cells %1").arg( rangeName );
  67. }
  68. void MergeCellCommand::execute()
  69. {
  70. Sheet* sheet = cell->sheet();
  71. if( !sheet ) return;
  72. sheet->changeMergedCell( cell->column(), cell->row(), colSpan, rowSpan);
  73. }
  74. void MergeCellCommand::unexecute()
  75. {
  76. Sheet* sheet = cell->sheet();
  77. if( !sheet ) return;
  78. sheet->changeMergedCell( cell->column(), cell->row(), oldColSpan, oldRowSpan);
  79. }
  80. // ----- DissociateCellCommand -----
  81. DissociateCellCommand::DissociateCellCommand( Cell* c )
  82. {
  83. cell = c;
  84. oldColSpan = cell->extraXCells();
  85. oldRowSpan = cell->extraYCells();
  86. }
  87. TQString DissociateCellCommand::name() const
  88. {
  89. return i18n("Dissociate Cell");
  90. }
  91. void DissociateCellCommand::execute()
  92. {
  93. Sheet* sheet = cell->sheet();
  94. if( !sheet ) return;
  95. sheet->changeMergedCell( cell->column(), cell->row(), 0, 0 );
  96. }
  97. void DissociateCellCommand::unexecute()
  98. {
  99. Sheet* sheet = cell->sheet();
  100. if( !sheet ) return;
  101. sheet->changeMergedCell( cell->column(), cell->row(), oldColSpan, oldRowSpan);
  102. }
  103. // ----- RenameSheetCommand -----
  104. RenameSheetCommand::RenameSheetCommand( Sheet* s, const TQString &name )
  105. {
  106. sheet = s;
  107. if( s ) oldName = s->sheetName();
  108. newName = name;
  109. }
  110. TQString RenameSheetCommand::name() const
  111. {
  112. return i18n("Rename Sheet");
  113. }
  114. void RenameSheetCommand::execute()
  115. {
  116. if( sheet )
  117. sheet->setSheetName( newName );
  118. }
  119. void RenameSheetCommand::unexecute()
  120. {
  121. if( sheet )
  122. sheet->setSheetName( oldName );
  123. }
  124. // ----- HideSheetCommand -----
  125. HideSheetCommand::HideSheetCommand( Sheet* sheet )
  126. {
  127. doc = sheet->doc();
  128. sheetName = sheet->sheetName();
  129. }
  130. void HideSheetCommand::execute()
  131. {
  132. Sheet* sheet = doc->map()->findSheet( sheetName );
  133. if( !sheet ) return;
  134. sheet->hideSheet( true );
  135. }
  136. void HideSheetCommand::unexecute()
  137. {
  138. Sheet* sheet = doc->map()->findSheet( sheetName );
  139. if( !sheet ) return;
  140. sheet->hideSheet( false );
  141. }
  142. TQString HideSheetCommand::name() const
  143. {
  144. TQString n = TQString( i18n("Hide Sheet %1") ).arg( sheetName );
  145. if( n.length() > 64 ) n = i18n("Hide Sheet");
  146. return n;
  147. }
  148. // ----- ShowSheetCommand -----
  149. ShowSheetCommand::ShowSheetCommand( Sheet* sheet )
  150. {
  151. doc = sheet->doc();
  152. sheetName = sheet->sheetName();
  153. }
  154. void ShowSheetCommand::execute()
  155. {
  156. Sheet* sheet = doc->map()->findSheet( sheetName );
  157. if( !sheet ) return;
  158. sheet->hideSheet( false );
  159. }
  160. void ShowSheetCommand::unexecute()
  161. {
  162. Sheet* sheet = doc->map()->findSheet( sheetName );
  163. if( !sheet ) return;
  164. sheet->hideSheet( true );
  165. }
  166. TQString ShowSheetCommand::name() const
  167. {
  168. TQString n = TQString( i18n("Show Sheet %1") ).arg( sheetName );
  169. if( n.length() > 64 ) n = i18n("Show Sheet");
  170. return n;
  171. }
  172. // ----- AddSheetCommand -----
  173. AddSheetCommand::AddSheetCommand( Sheet* s )
  174. {
  175. sheet = s;
  176. doc = sheet->doc();
  177. doc->map()->addSheet( s );
  178. }
  179. void AddSheetCommand::execute()
  180. {
  181. sheet->workbook()->insertSheet( sheet );
  182. doc->insertSheet( sheet );
  183. }
  184. void AddSheetCommand::unexecute()
  185. {
  186. sheet->workbook()->takeSheet( sheet );
  187. doc->takeSheet( sheet );
  188. }
  189. TQString AddSheetCommand::name() const
  190. {
  191. return i18n("Add Sheet");
  192. }
  193. // ----- RemoveSheetCommand -----
  194. RemoveSheetCommand::RemoveSheetCommand( Sheet* s )
  195. {
  196. sheet = s;
  197. doc = sheet->doc();
  198. }
  199. void RemoveSheetCommand::execute()
  200. {
  201. sheet->workbook()->takeSheet( sheet );
  202. doc->takeSheet( sheet );
  203. }
  204. void RemoveSheetCommand::unexecute()
  205. {
  206. sheet->workbook()->insertSheet( sheet );
  207. doc->insertSheet( sheet );
  208. }
  209. TQString RemoveSheetCommand::name() const
  210. {
  211. return i18n("Remove Sheet");
  212. }
  213. // ----- SheetPropertiesCommand -----
  214. SheetPropertiesCommand::SheetPropertiesCommand( Doc* d, Sheet* s )
  215. {
  216. sheet = s;
  217. doc = d;
  218. oldDirection = newDirection = sheet->layoutDirection();
  219. oldAutoCalc = newAutoCalc = sheet->getAutoCalc();
  220. oldShowGrid = newShowGrid = sheet->getShowGrid();
  221. oldShowPageBorders = newShowPageBorders = sheet->isShowPageBorders();
  222. oldShowFormula = newShowFormula = sheet->getShowFormula();
  223. oldHideZero = newHideZero = sheet->getHideZero();
  224. oldShowFormulaIndicator = newShowFormulaIndicator = sheet->getShowFormulaIndicator();
  225. oldShowCommentIndicator = newShowCommentIndicator = sheet->getShowCommentIndicator();
  226. oldColumnAsNumber = newColumnAsNumber = sheet->getShowColumnNumber();
  227. oldLcMode = newLcMode = sheet->getLcMode();
  228. oldCapitalizeFirstLetter = newCapitalizeFirstLetter = sheet->getFirstLetterUpper();
  229. }
  230. TQString SheetPropertiesCommand::name() const
  231. {
  232. return i18n("Change Sheet Properties");
  233. }
  234. void SheetPropertiesCommand::setLayoutDirection( Sheet::LayoutDirection dir )
  235. {
  236. newDirection = dir;
  237. }
  238. void SheetPropertiesCommand::setAutoCalc( bool b )
  239. {
  240. newAutoCalc = b;
  241. }
  242. void SheetPropertiesCommand::setShowGrid( bool b )
  243. {
  244. newShowGrid = b;
  245. }
  246. void SheetPropertiesCommand::setShowPageBorders( bool b )
  247. {
  248. newShowPageBorders = b;
  249. }
  250. void SheetPropertiesCommand::setShowFormula( bool b )
  251. {
  252. newShowFormula = b;
  253. }
  254. void SheetPropertiesCommand::setHideZero( bool b )
  255. {
  256. newHideZero = b;
  257. }
  258. void SheetPropertiesCommand::setShowFormulaIndicator( bool b )
  259. {
  260. newShowFormulaIndicator = b;
  261. }
  262. void SheetPropertiesCommand::setShowCommentIndicator( bool b )
  263. {
  264. newShowCommentIndicator = b;
  265. }
  266. void SheetPropertiesCommand::setColumnAsNumber( bool b )
  267. {
  268. newColumnAsNumber = b;
  269. }
  270. void SheetPropertiesCommand::setLcMode( bool b )
  271. {
  272. newLcMode = b;
  273. }
  274. void SheetPropertiesCommand::setCapitalizeFirstLetter( bool b )
  275. {
  276. newCapitalizeFirstLetter = b;
  277. }
  278. void SheetPropertiesCommand::execute()
  279. {
  280. sheet->setLayoutDirection( newDirection );
  281. sheet->setAutoCalc( newAutoCalc );
  282. sheet->setShowGrid( newShowGrid );
  283. sheet->setShowPageBorders( newShowPageBorders );
  284. sheet->setShowFormula( newShowFormula );
  285. sheet->setHideZero( newHideZero );
  286. sheet->setShowFormulaIndicator( newShowFormulaIndicator );
  287. sheet->setShowCommentIndicator( newShowCommentIndicator );
  288. sheet->setShowColumnNumber( newColumnAsNumber );
  289. sheet->setLcMode( newLcMode );
  290. sheet->setFirstLetterUpper( newCapitalizeFirstLetter );
  291. doc->addDamage( new SheetDamage( sheet, SheetDamage::PropertiesChanged ) );
  292. }
  293. void SheetPropertiesCommand::unexecute()
  294. {
  295. sheet->setLayoutDirection( oldDirection );
  296. sheet->setAutoCalc( oldAutoCalc );
  297. sheet->setShowGrid( oldShowGrid );
  298. sheet->setShowPageBorders( oldShowPageBorders );
  299. sheet->setShowFormula( oldShowFormula );
  300. sheet->setHideZero( oldHideZero );
  301. sheet->setShowFormulaIndicator( oldShowFormulaIndicator );
  302. sheet->setShowCommentIndicator( oldShowCommentIndicator );
  303. sheet->setShowColumnNumber( oldColumnAsNumber );
  304. sheet->setLcMode( oldLcMode );
  305. sheet->setFirstLetterUpper( oldCapitalizeFirstLetter );
  306. doc->addDamage( new SheetDamage( sheet, SheetDamage::PropertiesChanged ) );
  307. }
  308. // ----- InsertColumnCommand -----
  309. InsertColumnCommand::InsertColumnCommand( Sheet* s , unsigned int _column, unsigned int _nbCol )
  310. {
  311. doc = s->doc();
  312. sheetName = s->sheetName();
  313. insertPosColumn = _column;
  314. nbColumnInserted = _nbCol;
  315. }
  316. void InsertColumnCommand::execute()
  317. {
  318. Sheet* sheet = doc->map()->findSheet( sheetName );
  319. if( !sheet ) return;
  320. sheet->insertColumn( insertPosColumn,nbColumnInserted);
  321. }
  322. void InsertColumnCommand::unexecute()
  323. {
  324. Sheet* sheet = doc->map()->findSheet( sheetName );
  325. if( !sheet ) return;
  326. sheet->removeColumn( insertPosColumn,nbColumnInserted );
  327. }
  328. TQString InsertColumnCommand::name() const
  329. {
  330. return i18n("Insert Columns");
  331. }
  332. // ----- DefinePrintRangeCommand -----
  333. DefinePrintRangeCommand::DefinePrintRangeCommand( Sheet *s )
  334. {
  335. doc = s->doc();
  336. sheetName = s->sheetName();
  337. printRange = s->print()->printRange();
  338. }
  339. void DefinePrintRangeCommand::execute()
  340. {
  341. Sheet* sheet = doc->map()->findSheet( sheetName );
  342. if( !sheet ) return;
  343. sheet->print()->setPrintRange( printRangeRedo );
  344. }
  345. void DefinePrintRangeCommand::unexecute()
  346. {
  347. Sheet* sheet = doc->map()->findSheet( sheetName );
  348. if( !sheet ) return;
  349. printRangeRedo = sheet->print()->printRange();
  350. sheet->print()->setPrintRange( printRange );
  351. }
  352. TQString DefinePrintRangeCommand::name() const
  353. {
  354. return i18n("Set Page Layout");
  355. }
  356. // ----- PaperLayoutCommand -----
  357. PaperLayoutCommand::PaperLayoutCommand( Sheet *s )
  358. {
  359. doc = s->doc();
  360. sheetName = s->sheetName();
  361. pl = s->print()->paperLayout();
  362. hf = s->print()->headFootLine();
  363. unit = doc->unit();
  364. printGrid = s->print()->printGrid();
  365. printCommentIndicator = s->print()->printCommentIndicator();
  366. printFormulaIndicator = s->print()->printFormulaIndicator();
  367. printRange = s->print()->printRange();
  368. printRepeatColumns = s->print()->printRepeatColumns();
  369. printRepeatRows = s->print()->printRepeatRows();
  370. zoom = s->print()->zoom();
  371. pageLimitX = s->print()->pageLimitX();
  372. pageLimitY = s->print()->pageLimitY();
  373. }
  374. void PaperLayoutCommand::execute()
  375. {
  376. Sheet* sheet = doc->map()->findSheet( sheetName );
  377. if( !sheet ) return;
  378. SheetPrint* print = sheet->print();
  379. print->setPaperLayout( plRedo.ptLeft, plRedo.ptTop,
  380. plRedo.ptRight, plRedo.ptBottom,
  381. plRedo.format, plRedo.orientation );
  382. print->setHeadFootLine( hfRedo.headLeft, hfRedo.headMid, hfRedo.headRight,
  383. hfRedo.footLeft, hfRedo.footMid, hfRedo.footRight );
  384. doc->setUnit( unitRedo );
  385. print->setPrintGrid( printGridRedo );
  386. print->setPrintCommentIndicator( printCommentIndicatorRedo );
  387. print->setPrintFormulaIndicator( printFormulaIndicatorRedo );
  388. print->setPrintRange( printRangeRedo );
  389. print->setPrintRepeatColumns( printRepeatColumnsRedo );
  390. print->setPrintRepeatRows( printRepeatRowsRedo );
  391. print->setZoom( zoomRedo );
  392. print->setPageLimitX( pageLimitX );
  393. print->setPageLimitY( pageLimitY );
  394. }
  395. void PaperLayoutCommand::unexecute()
  396. {
  397. Sheet* sheet = doc->map()->findSheet( sheetName );
  398. if( !sheet ) return;
  399. SheetPrint* print = sheet->print();
  400. plRedo = print->paperLayout();
  401. print->setPaperLayout( pl.ptLeft, pl.ptTop,
  402. pl.ptRight, pl.ptBottom,
  403. pl.format, pl.orientation );
  404. hfRedo = print->headFootLine();
  405. print->setHeadFootLine( hf.headLeft, hf.headMid, hf.headRight,
  406. hf.footLeft, hf.footMid, hf.footRight );
  407. unitRedo = doc->unit();
  408. doc->setUnit( unit );
  409. printGridRedo = print->printGrid();
  410. print->setPrintGrid( printGrid );
  411. printCommentIndicatorRedo = print->printCommentIndicator();
  412. print->setPrintCommentIndicator( printCommentIndicator );
  413. printFormulaIndicatorRedo = print->printFormulaIndicator();
  414. print->setPrintFormulaIndicator( printFormulaIndicator );
  415. printRangeRedo = print->printRange();
  416. print->setPrintRange( printRange );
  417. printRepeatColumnsRedo = print->printRepeatColumns();
  418. print->setPrintRepeatColumns( printRepeatColumns );
  419. printRepeatRowsRedo = print->printRepeatRows();
  420. print->setPrintRepeatRows( printRepeatRows );
  421. zoomRedo = print->zoom();
  422. print->setZoom( zoom );
  423. pageLimitXRedo = print->pageLimitX();
  424. print->setPageLimitX( pageLimitX );
  425. pageLimitYRedo = print->pageLimitY();
  426. print->setPageLimitY( pageLimitY );
  427. }
  428. TQString PaperLayoutCommand::name() const
  429. {
  430. return i18n("Set Page Layout");
  431. }
  432. LinkCommand::LinkCommand( Cell* c, const TQString& text, const TQString& link )
  433. {
  434. cell = c;
  435. oldText = cell->text();
  436. oldLink = cell->link();
  437. newText = text;
  438. newLink = link;
  439. Sheet* s = cell->sheet();
  440. if( s ) doc = s->doc();
  441. }
  442. void LinkCommand::execute()
  443. {
  444. if( !cell ) return;
  445. if( !newText.isEmpty() )
  446. cell->setCellText( newText );
  447. cell->setLink( newLink );
  448. doc->addDamage( new CellDamage( cell ) );
  449. }
  450. void LinkCommand::unexecute()
  451. {
  452. if( !cell ) return;
  453. cell->setCellText( oldText );
  454. cell->setLink( oldLink );
  455. doc->addDamage( new CellDamage( cell ) );
  456. }
  457. TQString LinkCommand::name() const
  458. {
  459. return newLink.isEmpty() ? i18n("Remove Link") : i18n("Set Link");
  460. }
  461. ChangeObjectGeometryCommand::ChangeObjectGeometryCommand( EmbeddedObject *_obj, const KoPoint &_m_diff, const KoSize &_r_diff )
  462. : m_diff( _m_diff ), r_diff( _r_diff )
  463. {
  464. obj = _obj;
  465. obj->incCmdRef();
  466. doc = obj->sheet()->doc();
  467. }
  468. ChangeObjectGeometryCommand::~ChangeObjectGeometryCommand()
  469. {
  470. obj->decCmdRef();
  471. }
  472. void ChangeObjectGeometryCommand::execute()
  473. {
  474. doc->repaint( obj->geometry() );
  475. KoRect geometry = obj->geometry();
  476. geometry.moveBy( m_diff.x(), m_diff.y() );
  477. geometry.setWidth( geometry.width() + r_diff.width() );
  478. geometry.setHeight( geometry.height() + r_diff.height() );
  479. obj->setGeometry( geometry );
  480. // if ( object->isSelected())
  481. // doc->updateObjectStatusBarItem();
  482. doc->repaint( obj );
  483. }
  484. void ChangeObjectGeometryCommand::unexecute()
  485. {
  486. doc->repaint( obj->geometry() );
  487. KoRect geometry = obj->geometry();
  488. geometry.moveBy( -m_diff.x(), -m_diff.y() );
  489. geometry.setWidth( geometry.width() - r_diff.width() );
  490. geometry.setHeight( geometry.height() - r_diff.height() );
  491. obj->setGeometry( geometry );
  492. // if ( object->isSelected())
  493. // doc->updateObjectStatusBarItem();
  494. doc->repaint( obj );
  495. }
  496. TQString ChangeObjectGeometryCommand::name() const
  497. {
  498. /*if ( fabs( obj->geometry().width() - newGeometry.width() )<1e-3 && fabs( obj->geometry().height() - newGeometry.height() ) < 1e-3 )
  499. return i18n("Move Object");
  500. else */
  501. return i18n("Resize Object");
  502. }
  503. RemoveObjectCommand::RemoveObjectCommand( EmbeddedObject *_obj, bool _cut )
  504. {
  505. obj = _obj;
  506. cut = _cut;
  507. doc = obj->sheet()->doc();
  508. }
  509. RemoveObjectCommand::~RemoveObjectCommand()
  510. {
  511. if ( !executed )
  512. return;
  513. //kdDebug() << "*********Deleting object..." << endl;
  514. if ( obj->getType() == OBJECT_CHART )
  515. {
  516. EmbeddedKOfficeObject *chart = dynamic_cast<EmbeddedKOfficeObject *>(obj);
  517. chart->embeddedObject()->setDeleted(true);
  518. }
  519. delete obj;
  520. }
  521. void RemoveObjectCommand::execute()
  522. {
  523. // I don't think we need this:
  524. // Make shure that this child has no active embedded view -> activate ourselfs
  525. // doc()->emitBeginOperation( false );
  526. // partManager()->setActivePart( koDocument(), this );
  527. // partManager()->setSelectedPart( 0 );
  528. // doc()->emitEndOperation( d->activeSheet->visibleRect( d->canvas ) );
  529. doc->embeddedObjects().removeRef( obj );
  530. if ( obj->getType() == OBJECT_CHART || obj->getType()== OBJECT_KOFFICE_PART)
  531. {
  532. EmbeddedKOfficeObject *eko = dynamic_cast<EmbeddedKOfficeObject *>(obj);
  533. eko->embeddedObject()->setDeleted(true);
  534. }
  535. obj->setSelected( false );
  536. doc->repaint( obj );
  537. executed = true;
  538. }
  539. void RemoveObjectCommand::unexecute()
  540. {
  541. doc->embeddedObjects().append( obj );
  542. if ( obj->getType() == OBJECT_CHART || obj->getType()== OBJECT_KOFFICE_PART)
  543. {
  544. EmbeddedKOfficeObject *eko = dynamic_cast<EmbeddedKOfficeObject *>(obj);
  545. eko->embeddedObject()->setDeleted(false);
  546. }
  547. doc->repaint( obj );
  548. executed = false;
  549. }
  550. TQString RemoveObjectCommand::name() const
  551. {
  552. if ( cut )
  553. return i18n("Cut Object");
  554. else
  555. return i18n("Remove Object");
  556. }
  557. InsertObjectCommand::InsertObjectCommand( const KoRect& _geometry, KoDocumentEntry& _entry, Canvas *_canvas ) //child
  558. {
  559. geometry = _geometry;
  560. entry = _entry;
  561. canvas = _canvas;
  562. type = OBJECT_KOFFICE_PART;
  563. obj = 0;
  564. }
  565. InsertObjectCommand::InsertObjectCommand(const KoRect& _geometry, KoDocumentEntry& _entry, const TQRect& _data, Canvas *_canvas ) //chart
  566. {
  567. geometry = _geometry;
  568. entry = _entry;
  569. data = _data;
  570. canvas = _canvas;
  571. type = OBJECT_CHART;
  572. obj = 0;
  573. }
  574. InsertObjectCommand::InsertObjectCommand( const KoRect& _geometry , KURL& _file, Canvas *_canvas ) //picture
  575. {
  576. //In the case of pictures, only the top left point of the rectangle is relevant
  577. geometry = _geometry;
  578. file = _file;
  579. canvas = _canvas;
  580. type = OBJECT_PICTURE;
  581. obj = 0;
  582. }
  583. InsertObjectCommand::~InsertObjectCommand()
  584. {
  585. if ( executed )
  586. return;
  587. //kdDebug() << "*********Deleting object..." << endl;
  588. if ( obj->getType() == OBJECT_CHART )
  589. {
  590. EmbeddedKOfficeObject *chart = dynamic_cast<EmbeddedKOfficeObject *>(obj);
  591. chart->embeddedObject()->setDeleted(true);
  592. }
  593. delete obj;
  594. }
  595. void InsertObjectCommand::execute()
  596. {
  597. if ( obj ) //restore the object which was removed from the object list in InsertObjectCommand::unexecute()
  598. {
  599. canvas->doc()->embeddedObjects().append( obj );
  600. canvas->doc()->repaint( obj );
  601. }
  602. else
  603. {
  604. bool success = false;
  605. switch ( type )
  606. {
  607. case OBJECT_CHART:
  608. {
  609. success = canvas->activeSheet()->insertChart( geometry, entry, data );
  610. break;
  611. }
  612. case OBJECT_KOFFICE_PART:
  613. {
  614. success = canvas->activeSheet()->insertChild( geometry, entry );
  615. break;
  616. }
  617. case OBJECT_PICTURE:
  618. {
  619. success = canvas->activeSheet()->insertPicture( geometry.topLeft() , file );
  620. break;
  621. }
  622. default:
  623. break;
  624. }
  625. if ( success )
  626. {
  627. obj = canvas->doc()->embeddedObjects().last();
  628. obj->sheet()->unifyObjectName( obj );
  629. }
  630. else
  631. obj = 0;
  632. }
  633. executed = true;
  634. }
  635. void InsertObjectCommand::unexecute()
  636. {
  637. if ( !obj )
  638. return;
  639. canvas->doc()->embeddedObjects().removeRef( obj );
  640. obj->setSelected( false );
  641. canvas->doc()->repaint( obj );
  642. executed = false;
  643. }
  644. TQString InsertObjectCommand::name() const
  645. {
  646. return i18n("Insert Object");
  647. }
  648. RenameNameObjectCommand::RenameNameObjectCommand( const TQString &_name, const TQString &_objectName,
  649. EmbeddedObject *_obj, Doc *_doc ):
  650. KNamedCommand( _name ),
  651. newObjectName( _objectName ),
  652. object( _obj ),
  653. doc( _doc )
  654. {
  655. oldObjectName = object->getObjectName();
  656. m_page = object->sheet()/*doc->findPage( object )*/;
  657. }
  658. RenameNameObjectCommand::~RenameNameObjectCommand()
  659. {
  660. }
  661. void RenameNameObjectCommand::execute()
  662. {
  663. object->setObjectName( newObjectName );
  664. m_page->unifyObjectName( object );
  665. // doc->updateSideBarItem( m_page );
  666. }
  667. void RenameNameObjectCommand::unexecute()
  668. {
  669. object->setObjectName( oldObjectName );
  670. // doc->updateSideBarItem( m_page );
  671. }
  672. GeometryPropertiesCommand::GeometryPropertiesCommand( const TQString &name, TQPtrList<EmbeddedObject> &objects,
  673. bool newValue, KgpType type, Doc *_doc )
  674. : KNamedCommand( name )
  675. , m_objects( objects )
  676. , m_newValue( newValue )
  677. , m_type( type )
  678. , m_doc( _doc )
  679. {
  680. TQPtrListIterator<EmbeddedObject> it( m_objects );
  681. for ( ; it.current() ; ++it )
  682. {
  683. it.current()->incCmdRef();
  684. if ( m_type == ProtectSize )
  685. m_oldValue.append( it.current()->isProtect() );
  686. else if ( m_type == KeepRatio)
  687. m_oldValue.append( it.current()->isKeepRatio() );
  688. }
  689. }
  690. GeometryPropertiesCommand::GeometryPropertiesCommand( const TQString &name, TQValueList<bool> &lst,
  691. TQPtrList<EmbeddedObject> &objects, bool newValue,
  692. KgpType type, Doc *_doc)
  693. : KNamedCommand( name )
  694. , m_oldValue( lst )
  695. , m_objects( objects )
  696. , m_newValue( newValue )
  697. , m_type( type )
  698. , m_doc ( _doc )
  699. {
  700. TQPtrListIterator<EmbeddedObject> it( m_objects );
  701. for ( ; it.current() ; ++it )
  702. it.current()->incCmdRef();
  703. }
  704. GeometryPropertiesCommand::~GeometryPropertiesCommand()
  705. {
  706. TQPtrListIterator<EmbeddedObject> it( m_objects );
  707. for ( ; it.current() ; ++it )
  708. it.current()->decCmdRef();
  709. }
  710. void GeometryPropertiesCommand::execute()
  711. {
  712. TQPtrListIterator<EmbeddedObject> it( m_objects );
  713. for ( ; it.current() ; ++it )
  714. {
  715. if ( m_type == ProtectSize )
  716. {
  717. it.current()->setProtect( m_newValue );
  718. if ( it.current()->isSelected() )
  719. m_doc->repaint( it.current() );
  720. }
  721. else if ( m_type == KeepRatio)
  722. it.current()->setKeepRatio( m_newValue );
  723. }
  724. }
  725. void GeometryPropertiesCommand::unexecute()
  726. {
  727. EmbeddedObject *obj = 0;
  728. for ( unsigned int i = 0; i < m_objects.count(); ++i ) {
  729. obj = m_objects.at( i );
  730. if ( m_type == ProtectSize )
  731. {
  732. obj->setProtect( *m_oldValue.at(i) );
  733. if ( obj->isSelected() )
  734. m_doc->repaint( obj );
  735. }
  736. else if ( m_type == KeepRatio)
  737. obj->setKeepRatio( *m_oldValue.at(i) );
  738. }
  739. }
  740. MoveObjectByCmd::MoveObjectByCmd( const TQString &_name, const KoPoint &_diff, TQPtrList<EmbeddedObject> &_objects,
  741. Doc *_doc,Sheet *_page )
  742. : KNamedCommand( _name ), diff( _diff ), objects( _objects )
  743. {
  744. objects.setAutoDelete( false );
  745. doc = _doc;
  746. m_page=_page;
  747. TQPtrListIterator<EmbeddedObject> it( objects );
  748. for ( ; it.current() ; ++it )
  749. {
  750. it.current()->incCmdRef();
  751. }
  752. }
  753. MoveObjectByCmd::~MoveObjectByCmd()
  754. {
  755. TQPtrListIterator<EmbeddedObject> it( objects );
  756. for ( ; it.current() ; ++it )
  757. it.current()->decCmdRef();
  758. }
  759. void MoveObjectByCmd::execute()
  760. {
  761. TQRect oldRect;
  762. for ( unsigned int i = 0; i < objects.count(); i++ ) {
  763. doc->repaint( objects.at( i )->geometry() );
  764. KoRect r = objects.at( i )->geometry();
  765. r.moveBy( diff.x(), diff.y() );
  766. objects.at( i )->setGeometry( r );
  767. doc->repaint( objects.at( i ) );
  768. }
  769. }
  770. void MoveObjectByCmd::unexecute()
  771. {
  772. TQRect oldRect;
  773. for ( unsigned int i = 0; i < objects.count(); i++ ) {
  774. doc->repaint( objects.at( i )->geometry() );
  775. KoRect r = objects.at( i )->geometry();
  776. r.moveBy( -diff.x(), -diff.y() );
  777. objects.at( i )->setGeometry( r );
  778. doc->repaint( objects.at( i ) );
  779. }
  780. }