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.

1724 lines
44KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2005 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either
  6. version 2 of the License.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public License
  12. along with this library; see the file COPYING.LIB. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  14. Boston, MA 02110-1301, USA.
  15. */
  16. #include <float.h>
  17. #include <tqcolor.h>
  18. #include <kdebug.h>
  19. #include <klocale.h>
  20. #include <kmessagebox.h>
  21. #include <kstaticdeleter.h>
  22. #include "kspread_canvas.h"
  23. #include "kspread_cell.h"
  24. #include "kspread_doc.h"
  25. #include "kspread_map.h"
  26. #include "kspread_sheet.h"
  27. #include "kspread_style.h"
  28. #include "kspread_style_manager.h"
  29. #include "kspread_undo.h"
  30. #include "kspread_view.h"
  31. #include "manipulator.h"
  32. using namespace KSpread;
  33. //BEGIN Non-contiguous selection adaption todos
  34. // TODO Stefan: InsertColumn
  35. // TODO Stefan: InsertRow
  36. // TODO Stefan: DeleteColumn
  37. // TODO Stefan: DeleteRow
  38. // TODO Stefan: SortInc
  39. // TODO Stefan: SortDec
  40. // TODO Stefan: FillSelection ?
  41. // TODO Stefan: RemoveComment (works, but not a manipulator yet)
  42. // TODO Stefan: ClearText (works, but not a manipulator yet)
  43. // TODO Stefan: ClearValidity (works, but not a manipulator yet)
  44. // TODO Stefan: Validity (works, but not a manipulator yet)
  45. // TODO Stefan: Conditional (works, but not a manipulator yet)
  46. // TODO Stefan: Copy (works, but not a manipulator yet)
  47. // TODO Stefan: Delete (works, but not a manipulator yet)
  48. // TODO Stefan: Cut (works, but not a manipulator yet)
  49. // TODO Stefan: Paste (works, but not a manipulator yet)
  50. // TODO Stefan: Paste Special (works, but not a manipulator yet)
  51. // TODO Stefan: Paste with insertion (works, but not a manipulator yet)
  52. // TODO Stefan: more ????
  53. //END
  54. //BEGIN NOTE Stefan: some words on operations
  55. //
  56. // 1. SubTotal
  57. // a) Makes no sense to extend to non-contiguous selections (NCS) as
  58. // it refers to a change in one column.
  59. // b) No special undo command available yet.
  60. //
  61. // 2. AutoSum
  62. // a) should insert cell at the end of the selection, if the last
  63. // is not empty
  64. // b) opens an editor, if the user's intention is fuzzy -> hard to
  65. // convert to NCS
  66. //END
  67. /***************************************************************************
  68. class Manipulator
  69. ****************************************************************************/
  70. Manipulator::Manipulator()
  71. : Region(),
  72. KCommand(),
  73. m_sheet(0),
  74. m_creation(true),
  75. m_reverse(false),
  76. m_firstrun(true),
  77. m_format(true),
  78. m_register(true)
  79. {
  80. }
  81. Manipulator::~Manipulator()
  82. {
  83. }
  84. void Manipulator::execute()
  85. {
  86. if (!m_sheet)
  87. {
  88. kdWarning() << "Manipulator::execute(): No explicit m_sheet is set. "
  89. << "Manipulating all sheets of the region." << endl;
  90. }
  91. bool successfully = true;
  92. successfully = preProcessing();
  93. if (!successfully)
  94. {
  95. kdWarning() << "Manipulator::execute(): preprocessing was not successful!" << endl;
  96. return; // do nothing if pre-processing fails
  97. }
  98. m_sheet->doc()->setModified(true);
  99. m_sheet->doc()->undoLock ();
  100. m_sheet->doc()->emitBeginOperation();
  101. successfully = true;
  102. Region::Iterator endOfList(cells().end());
  103. for (Region::Iterator it = cells().begin(); it != endOfList; ++it)
  104. {
  105. successfully = successfully && process(*it);
  106. }
  107. if (!successfully)
  108. {
  109. kdWarning() << "Manipulator::execute(): processing was not successful!" << endl;
  110. }
  111. successfully = true;
  112. successfully = postProcessing();
  113. if (!successfully)
  114. {
  115. kdWarning() << "Manipulator::execute(): postprocessing was not successful!" << endl;
  116. }
  117. m_sheet->setRegionPaintDirty( *this );
  118. m_sheet->doc()->emitEndOperation();
  119. m_sheet->doc()->undoUnlock ();
  120. // add me to undo if needed
  121. if (m_firstrun && m_register)
  122. {
  123. // addCommand itself checks for undo lock
  124. m_sheet->doc()->addCommand (this);
  125. // if we add something to undo, then the document surely is modified ...
  126. m_sheet->doc()->setModified (true);
  127. }
  128. m_firstrun = false;
  129. }
  130. void Manipulator::unexecute()
  131. {
  132. m_reverse = !m_reverse;
  133. execute();
  134. m_reverse = !m_reverse;
  135. }
  136. bool Manipulator::process(Element* element)
  137. {
  138. Sheet* sheet = m_sheet; // TODO Stefan: element->sheet();
  139. if (m_sheet && sheet != m_sheet)
  140. {
  141. return true;
  142. }
  143. TQRect range = element->rect().normalize();
  144. if (m_format && element->isColumn())
  145. {
  146. for (int col = range.left(); col <= range.right(); ++col)
  147. {
  148. kdDebug() << "Processing column " << col << "." << endl;
  149. ColumnFormat* format = sheet->nonDefaultColumnFormat(col);
  150. process(format);
  151. // TODO Stefan: process cells with this property
  152. }
  153. }
  154. else if (m_format && element->isRow())
  155. {
  156. for (int row = range.top(); row <= range.bottom(); ++row)
  157. {
  158. kdDebug() << "Processing row " << row << "." << endl;
  159. RowFormat* format = sheet->nonDefaultRowFormat(row);
  160. process(format);
  161. // TODO Stefan: process cells with this property
  162. }
  163. }
  164. else
  165. {
  166. kdDebug() << "Processing cell(s) at " << range << "." << endl;
  167. for (int col = range.left(); col <= range.right(); ++col)
  168. {
  169. sheet->enableScrollBarUpdates(false);
  170. for (int row = range.top(); row <= range.bottom(); ++row)
  171. {
  172. Cell* cell = sheet->cellAt(col, row);
  173. /* (Tomas) don't force working on obscurring cells - most manipulators don't want this, and those that do can do that manually ... Plus I think that no manipulator should do it anyway ...
  174. if ( cell->isPartOfMerged() )
  175. {
  176. cell = cell->obscuringCells().first();
  177. }
  178. */
  179. //if (testCondition(cell))
  180. {
  181. if (cell == sheet->defaultCell() && m_creation)
  182. {
  183. Style* style = sheet->doc()->styleManager()->defaultStyle();
  184. cell = new Cell(sheet, style, col, row);
  185. sheet->insertCell(cell);
  186. }
  187. if (!process(cell))
  188. {
  189. return false;
  190. }
  191. }
  192. }
  193. sheet->enableScrollBarUpdates(true);
  194. sheet->checkRangeVBorder(range.bottom());
  195. }
  196. sheet->checkRangeHBorder(range.right());
  197. }
  198. return true;
  199. }
  200. /***************************************************************************
  201. class FormatManipulator
  202. ****************************************************************************/
  203. FormatManipulator::FormatManipulator()
  204. {
  205. m_properties = 0;
  206. // initialize pens with invalid color
  207. m_topBorderPen = TQPen(TQColor(), 0, TQt::NoPen);
  208. m_bottomBorderPen = TQPen(TQColor(), 0, TQt::NoPen);
  209. m_leftBorderPen = TQPen(TQColor(), 0, TQt::NoPen);
  210. m_rightBorderPen = TQPen(TQColor(), 0, TQt::NoPen);
  211. m_horizontalPen = TQPen(TQColor(), 0, TQt::NoPen);
  212. m_verticalPen = TQPen(TQColor(), 0, TQt::NoPen);
  213. m_fallDiagonalPen = TQPen(TQColor(), 0, TQt::NoPen);
  214. m_goUpDiagonalPen = TQPen(TQColor(), 0, TQt::NoPen);
  215. }
  216. FormatManipulator::~FormatManipulator()
  217. {
  218. TQValueList<layoutCell>::Iterator it2;
  219. for ( it2 = m_lstFormats.begin(); it2 != m_lstFormats.end(); ++it2 )
  220. {
  221. delete (*it2).l;
  222. }
  223. m_lstFormats.clear();
  224. for ( it2 = m_lstRedoFormats.begin(); it2 != m_lstRedoFormats.end(); ++it2 )
  225. {
  226. delete (*it2).l;
  227. }
  228. m_lstRedoFormats.clear();
  229. TQValueList<layoutColumn>::Iterator it3;
  230. for ( it3 = m_lstColFormats.begin(); it3 != m_lstColFormats.end(); ++it3 )
  231. {
  232. delete (*it3).l;
  233. }
  234. m_lstColFormats.clear();
  235. for ( it3 = m_lstRedoColFormats.begin(); it3 != m_lstRedoColFormats.end(); ++it3 )
  236. {
  237. delete (*it3).l;
  238. }
  239. m_lstRedoColFormats.clear();
  240. TQValueList<layoutRow>::Iterator it4;
  241. for ( it4 = m_lstRowFormats.begin(); it4 != m_lstRowFormats.end(); ++it4 )
  242. {
  243. delete (*it4).l;
  244. }
  245. m_lstRowFormats.clear();
  246. for ( it4 = m_lstRedoRowFormats.begin(); it4 != m_lstRedoRowFormats.end(); ++it4 )
  247. {
  248. delete (*it4).l;
  249. }
  250. m_lstRedoRowFormats.clear();
  251. }
  252. bool FormatManipulator::preProcessing ()
  253. {
  254. if (m_reverse)
  255. copyFormat (m_lstRedoFormats, m_lstRedoColFormats, m_lstRedoRowFormats);
  256. else
  257. copyFormat (m_lstFormats, m_lstColFormats, m_lstRowFormats);
  258. return true;
  259. }
  260. bool FormatManipulator::process (Element *element)
  261. {
  262. // see what is selected; if nothing, take marker position
  263. TQRect range = element->rect().normalize();
  264. if (!m_reverse) {
  265. int top = range.top();
  266. int left = range.left();
  267. int bottom = range.bottom();
  268. int right = range.right();
  269. // create cells in rows if complete columns selected
  270. Cell * cell;
  271. if ( element->isColumn() )
  272. {
  273. for ( RowFormat * row = m_sheet->firstRow(); row; row = row->next() )
  274. {
  275. if ( !row->isDefault() )
  276. {
  277. for ( int col = left; col <= right; ++col )
  278. {
  279. cell = m_sheet->nonDefaultCell( col, row->row() );
  280. }
  281. }
  282. }
  283. }
  284. // complete rows selected ?
  285. if ( element->isRow() )
  286. {
  287. for ( int row = top; row <= bottom; ++row )
  288. {
  289. cell = m_sheet->getFirstCellRow( row );
  290. while ( cell )
  291. {
  292. prepareCell( cell );
  293. cell = m_sheet->getNextCellRight( cell->column(), row );
  294. }
  295. RowFormat * rowFormat = m_sheet->nonDefaultRowFormat(row);
  296. doWork(rowFormat, row==top, row==bottom, false, false);
  297. }
  298. }
  299. // complete columns selected ?
  300. else if ( element->isColumn() )
  301. {
  302. for ( int col = left; col <= right; ++col )
  303. {
  304. cell = m_sheet->getFirstCellColumn( col );
  305. while ( cell )
  306. {
  307. prepareCell( cell );
  308. cell = m_sheet->getNextCellDown( col, cell->row() );
  309. }
  310. ColumnFormat * colFormat = m_sheet->nonDefaultColumnFormat( col );
  311. doWork(colFormat, false, false, col==left, col==right);
  312. }
  313. for ( RowFormat * rowFormat = m_sheet->firstRow(); rowFormat; rowFormat = rowFormat->next() )
  314. {
  315. if ( !rowFormat->isDefault() && testCondition( rowFormat ) )
  316. {
  317. for ( int col = left; col <= right; ++col )
  318. {
  319. cell = m_sheet->nonDefaultCell(col, rowFormat->row() );
  320. doWork(cell->format(), false, false, col==left, col==right );
  321. }
  322. }
  323. }
  324. }
  325. // cell region selected
  326. else
  327. {
  328. for ( int col = left; col <= right; ++col )
  329. {
  330. for ( int row = top; row <= bottom; ++row )
  331. {
  332. cell = m_sheet->nonDefaultCell(col,row);
  333. if ( !cell->isPartOfMerged() )
  334. {
  335. cell->setDisplayDirtyFlag();
  336. doWork(cell->format(), row==top, row==bottom, col==left, col==right);
  337. cell->clearDisplayDirtyFlag();
  338. }
  339. }
  340. }
  341. }
  342. }
  343. else
  344. { // undoing
  345. if( element->isColumn() )
  346. {
  347. TQValueList<layoutColumn>::Iterator it2;
  348. for ( it2 = m_lstColFormats.begin(); it2 != m_lstColFormats.end(); ++it2 )
  349. {
  350. ColumnFormat * col = m_sheet->nonDefaultColumnFormat( (*it2).col );
  351. col->copy( *(*it2).l );
  352. }
  353. }
  354. else if( element->isRow() )
  355. {
  356. TQValueList<layoutRow>::Iterator it2;
  357. for ( it2 = m_lstRowFormats.begin(); it2 != m_lstRowFormats.end(); ++it2 )
  358. {
  359. RowFormat * row = m_sheet->nonDefaultRowFormat( (*it2).row );
  360. row->copy( *(*it2).l );
  361. }
  362. }
  363. TQValueList<layoutCell>::Iterator it2;
  364. for ( it2 = m_lstFormats.begin(); it2 != m_lstFormats.end(); ++it2 )
  365. {
  366. Cell *cell = m_sheet->nonDefaultCell( (*it2).col,(*it2).row );
  367. cell->format()->copy( *(*it2).l );
  368. cell->setLayoutDirtyFlag();
  369. cell->setDisplayDirtyFlag();
  370. m_sheet->updateCell( cell, (*it2).col, (*it2).row );
  371. }
  372. }
  373. return true;
  374. }
  375. void FormatManipulator::copyFormat(TQValueList<layoutCell> & list,
  376. TQValueList<layoutColumn> & listCol,
  377. TQValueList<layoutRow> & listRow)
  378. {
  379. TQValueList<layoutCell>::Iterator end = list.end();
  380. for (TQValueList<layoutCell>::Iterator it2 = list.begin(); it2 != end; ++it2)
  381. {
  382. delete (*it2).l;
  383. }
  384. list.clear();
  385. Cell * cell;
  386. Region::ConstIterator endOfList(cells().constEnd());
  387. for (Region::ConstIterator it = cells().constBegin(); it != endOfList; ++it)
  388. {
  389. TQRect range = (*it)->rect().normalize();
  390. int bottom = range.bottom();
  391. int right = range.right();
  392. if ( (*it)->isColumn() )
  393. {
  394. /* Don't need to go through the loop twice...
  395. for (int i = range.left(); i <= right; ++i)
  396. {
  397. layoutColumn tmptqlayout;
  398. tmptqlayout.col = i;
  399. tmptqlayout.l = new ColumnFormat( m_sheet, i );
  400. tmptqlayout.l->copy( *(m_sheet->columnFormat( i )) );
  401. listCol.append(tmptqlayout);
  402. }
  403. */
  404. for ( int col = range.left(); col <= right; ++col )
  405. {
  406. layoutColumn tmptqlayout;
  407. tmptqlayout.col = col;
  408. tmptqlayout.l = new ColumnFormat( m_sheet, col );
  409. tmptqlayout.l->copy( *(m_sheet->columnFormat( col )) );
  410. listCol.append(tmptqlayout);
  411. cell = m_sheet->getFirstCellColumn( col );
  412. while ( cell )
  413. {
  414. if ( cell->isPartOfMerged() )
  415. {
  416. cell = m_sheet->getNextCellDown( col, cell->row() );
  417. continue;
  418. }
  419. layoutCell tmptqlayout;
  420. tmptqlayout.col = col;
  421. tmptqlayout.row = cell->row();
  422. tmptqlayout.l = new Format( m_sheet, 0 );
  423. tmptqlayout.l->copy( *(m_sheet->cellAt( tmptqlayout.col, tmptqlayout.row )->format()) );
  424. list.append(tmptqlayout);
  425. cell = m_sheet->getNextCellDown( col, cell->row() );
  426. }
  427. }
  428. /*
  429. Cell * cell = m_sheet->firstCell();
  430. for( ; cell; cell = cell->nextCell() )
  431. {
  432. int col = cell->column();
  433. if ( range.left() <= col && right >= col
  434. && !cell->isPartOfMerged())
  435. {
  436. layoutCell tmptqlayout;
  437. tmptqlayout.col = cell->column();
  438. tmptqlayout.row = cell->row();
  439. tmptqlayout.l = new Format( m_sheet, 0 );
  440. tmptqlayout.l->copy( *(m_sheet->cellAt( tmptqlayout.col, tmptqlayout.row )) );
  441. list.append(tmptqlayout);
  442. }
  443. }
  444. */
  445. }
  446. else if ((*it)->isRow())
  447. {
  448. for ( int row = range.top(); row <= bottom; ++row )
  449. {
  450. layoutRow tmptqlayout;
  451. tmptqlayout.row = row;
  452. tmptqlayout.l = new RowFormat( m_sheet, row );
  453. tmptqlayout.l->copy( *(m_sheet->rowFormat( row )) );
  454. listRow.append(tmptqlayout);
  455. cell = m_sheet->getFirstCellRow( row );
  456. while ( cell )
  457. {
  458. if ( cell->isPartOfMerged() )
  459. {
  460. cell = m_sheet->getNextCellRight( cell->column(), row );
  461. continue;
  462. }
  463. layoutCell tmptqlayout;
  464. tmptqlayout.col = cell->column();
  465. tmptqlayout.row = row;
  466. tmptqlayout.l = new Format( m_sheet, 0 );
  467. tmptqlayout.l->copy( *(m_sheet->cellAt( cell->column(), row )->format()) );
  468. list.append(tmptqlayout);
  469. cell = m_sheet->getNextCellRight( cell->column(), row );
  470. }
  471. }
  472. /*
  473. Cell * cell = m_sheet->firstCell();
  474. for( ; cell; cell = cell->nextCell() )
  475. {
  476. int row = cell->row();
  477. if ( range.top() <= row && bottom >= row
  478. && !cell->isPartOfMerged())
  479. {
  480. layoutCell tmptqlayout;
  481. tmptqlayout.col = cell->column();
  482. tmptqlayout.row = cell->row();
  483. tmptqlayout.l = new Format( m_sheet, 0 );
  484. tmptqlayout.l->copy( *(m_sheet->cellAt( tmptqlayout.col, tmptqlayout.row )) );
  485. list.append(tmptqlayout);
  486. }
  487. }
  488. */
  489. }
  490. else
  491. {
  492. for ( int row = range.top(); row <= bottom; ++row )
  493. for ( int col = range.left(); col <= right; ++col )
  494. {
  495. Cell * cell = m_sheet->nonDefaultCell( col, row );
  496. if ( !cell->isPartOfMerged() )
  497. {
  498. layoutCell tmptqlayout;
  499. tmptqlayout.col = col;
  500. tmptqlayout.row = row;
  501. tmptqlayout.l = new Format( m_sheet, 0 );
  502. tmptqlayout.l->copy( *(m_sheet->cellAt( col, row )->format()) );
  503. list.append(tmptqlayout);
  504. }
  505. }
  506. }
  507. }
  508. }
  509. bool FormatManipulator::testCondition(RowFormat* row)
  510. {
  511. for (TQ_UINT32 property = Format::PAlign;
  512. property <= Format::PHideFormula;
  513. property *= 2)
  514. {
  515. if (m_properties & property)
  516. {
  517. return ( row->hasProperty((Format::Properties) property) );
  518. }
  519. }
  520. return false;
  521. }
  522. void FormatManipulator::doWork(Format* format,
  523. bool isTop, bool isBottom,
  524. bool isLeft, bool isRight)
  525. {
  526. // SetSelectionFontWorker
  527. // SetSelectionSizeWorker
  528. if (m_properties & Format::PFont)
  529. {
  530. if ( !m_font.isEmpty() )
  531. format->setTextFontFamily( m_font );
  532. if ( m_size > 0 )
  533. format->setTextFontSize( m_size );
  534. if ( m_italic >= 0 )
  535. format->setTextFontItalic( (bool)m_italic );
  536. if ( m_bold >= 0 )
  537. format->setTextFontBold( (bool)m_bold );
  538. if ( m_underline >= 0 )
  539. format->setTextFontUnderline( (bool)m_underline );
  540. if ( m_strike >= 0 )
  541. format->setTextFontStrike( (bool)m_strike );
  542. }
  543. // SetSelectionAngleWorker
  544. if (m_properties & Format::PAngle)
  545. {
  546. format->setAngle( m_angle );
  547. }
  548. // SetSelectionTextColorWorker
  549. if (m_properties & Format::PTextPen)
  550. {
  551. format->setTextColor( m_textColor );
  552. }
  553. // SetSelectionBgColorWorker
  554. if (m_properties & Format::PBackgroundColor)
  555. {
  556. format->setBgColor( m_backgroundColor );
  557. }
  558. // SetSelectionBorderAllWorker
  559. if (m_properties & Format::PLeftBorder)
  560. {
  561. if (isLeft)
  562. {
  563. if (m_leftBorderPen.color().isValid())
  564. {
  565. format->setLeftBorderPen(m_leftBorderPen);
  566. }
  567. }
  568. else
  569. {
  570. if (m_verticalPen.color().isValid())
  571. {
  572. format->setLeftBorderPen(m_verticalPen);
  573. }
  574. }
  575. }
  576. if (m_properties & Format::PRightBorder)
  577. {
  578. if (isRight)
  579. {
  580. if (m_rightBorderPen.color().isValid())
  581. {
  582. format->setRightBorderPen(m_rightBorderPen);
  583. }
  584. }
  585. else
  586. {
  587. if (m_verticalPen.color().isValid())
  588. {
  589. format->setRightBorderPen(m_verticalPen);
  590. }
  591. }
  592. }
  593. if (m_properties & Format::PTopBorder)
  594. {
  595. if (isTop)
  596. {
  597. if (m_topBorderPen.color().isValid())
  598. {
  599. format->setTopBorderPen(m_topBorderPen);
  600. }
  601. }
  602. else
  603. {
  604. if (m_horizontalPen.color().isValid())
  605. {
  606. format->setTopBorderPen(m_horizontalPen);
  607. }
  608. }
  609. }
  610. if (m_properties & Format::PBottomBorder)
  611. {
  612. if (isBottom)
  613. {
  614. if (m_bottomBorderPen.color().isValid())
  615. {
  616. format->setBottomBorderPen(m_bottomBorderPen);
  617. }
  618. }
  619. else
  620. {
  621. if (m_horizontalPen.color().isValid())
  622. {
  623. format->setBottomBorderPen(m_horizontalPen);
  624. }
  625. }
  626. }
  627. if (m_properties & Format::PFallDiagonal)
  628. {
  629. format->setFallDiagonalPen(m_fallDiagonalPen);
  630. }
  631. if (m_properties & Format::PGoUpDiagonal)
  632. {
  633. format->setGoUpDiagonalPen(m_goUpDiagonalPen);
  634. }
  635. // SetSelectionAlignWorker
  636. if (m_properties & Format::PAlign)
  637. {
  638. format->setAlign( m_horAlign );
  639. }
  640. // SetSelectionAlignYWorker
  641. if (m_properties & Format::PAlignY)
  642. {
  643. format->setAlignY( m_verAlign );
  644. }
  645. if (m_properties & Format::PPrefix)
  646. {
  647. format->setPrefix(m_prefix);
  648. }
  649. if (m_properties & Format::PPostfix)
  650. {
  651. format->setPostfix(m_postfix);
  652. }
  653. if (m_properties & Format::PBackgroundBrush)
  654. {
  655. format->setBackGroundBrush(m_backgroundBrush);
  656. }
  657. if (m_properties & Format::PFloatFormat)
  658. {
  659. format->setFloatFormat(m_floatFormat);
  660. }
  661. if (m_properties & Format::PFloatColor)
  662. {
  663. format->setFloatColor(m_floatColor);
  664. }
  665. if (m_properties & Format::PMultiRow)
  666. {
  667. format->setMultiRow(m_multiRow);
  668. }
  669. if (m_properties & Format::PVerticalText)
  670. {
  671. format->setVerticalText(m_verticalText);
  672. }
  673. if (m_properties & Format::PPrecision)
  674. {
  675. format->setPrecision(m_precision);
  676. }
  677. if (m_properties & Format::PFormatType)
  678. {
  679. format->setFormatType(m_formatType);
  680. if (m_formatType == Money_format)
  681. {
  682. format->setCurrency(m_currencyType, m_currencySymbol);
  683. }
  684. }
  685. if (m_properties & Format::PComment)
  686. {
  687. format->setComment(m_comment);
  688. }
  689. if (m_properties & Format::PIndent)
  690. {
  691. format->setIndent(m_indent);
  692. }
  693. if (m_properties & Format::PDontPrintText)
  694. {
  695. format->setDontPrintText(m_dontPrintText);
  696. }
  697. if (m_properties & Format::PCustomFormat)
  698. {
  699. //TODO
  700. }
  701. if (m_properties & Format::PNotProtected)
  702. {
  703. format->setNotProtected(m_notProtected);
  704. }
  705. if (m_properties & Format::PHideAll)
  706. {
  707. format->setHideAll(m_hideAll);
  708. }
  709. if (m_properties & Format::PHideFormula)
  710. {
  711. format->setHideFormula(m_hideFormula);
  712. }
  713. }
  714. void FormatManipulator::prepareCell(Cell* cell)
  715. {
  716. for (TQ_UINT32 property = Format::PAlign;
  717. property <= Format::PHideFormula;
  718. property *= 2)
  719. {
  720. if (m_properties & property)
  721. {
  722. cell->format()->clearProperty((Format::Properties) property);
  723. cell->format()->clearNoFallBackProperties((Format::Properties) property);
  724. }
  725. }
  726. }
  727. /***************************************************************************
  728. class MergeManipulator
  729. ****************************************************************************/
  730. MergeManipulator::MergeManipulator()
  731. : Manipulator(),
  732. m_merge(true),
  733. m_mergeHorizontal(false),
  734. m_mergeVertical(false),
  735. m_unmerger(0)
  736. {
  737. }
  738. MergeManipulator::~MergeManipulator()
  739. {
  740. delete m_unmerger;
  741. }
  742. bool MergeManipulator::process(Element* element)
  743. {
  744. if (element->type() != Element::Range || element->isRow() || element->isColumn())
  745. {
  746. // TODO Stefan: remove these elements?!
  747. return true;
  748. }
  749. // sanity check
  750. if( m_sheet->isProtected() || m_sheet->workbook()->isProtected() )
  751. {
  752. return false;
  753. }
  754. TQRect range = element->rect().normalize();
  755. int left = range.left();
  756. int right = range.right();
  757. int top = range.top();
  758. int bottom = range.bottom();
  759. int height = range.height();
  760. int width = range.width();
  761. bool doMerge = m_reverse ? (!m_merge) : m_merge;
  762. if (doMerge)
  763. {
  764. if (m_mergeHorizontal)
  765. {
  766. for (int row = top; row <= bottom; ++row)
  767. {
  768. int rows = 0;
  769. for (int col = left; col <= right; ++col)
  770. {
  771. Cell *cell = m_sheet->cellAt( col, row );
  772. if (cell->doesMergeCells())
  773. {
  774. rows = TQMAX(rows, cell->mergedYCells());
  775. cell->mergeCells( col, row, 0, 0 );
  776. }
  777. }
  778. Cell *cell = m_sheet->nonDefaultCell( left, row );
  779. if (!cell->isPartOfMerged())
  780. {
  781. cell->mergeCells( left, row, width - 1, rows );
  782. }
  783. }
  784. }
  785. else if (m_mergeVertical)
  786. {
  787. for (int col = left; col <= right; ++col)
  788. {
  789. int cols = 0;
  790. for (int row = top; row <= bottom; ++row)
  791. {
  792. Cell *cell = m_sheet->cellAt( col, row );
  793. if (cell->doesMergeCells())
  794. {
  795. cols = TQMAX(cols, cell->mergedXCells());
  796. cell->mergeCells( col, row, 0, 0 );
  797. }
  798. }
  799. Cell *cell = m_sheet->nonDefaultCell( col, top );
  800. if (!cell->isPartOfMerged())
  801. {
  802. cell->mergeCells( col, top, cols, height - 1);
  803. }
  804. }
  805. }
  806. else
  807. {
  808. Cell *cell = m_sheet->nonDefaultCell( left, top );
  809. cell->mergeCells( left, top, width - 1, height - 1);
  810. }
  811. }
  812. else // dissociate
  813. {
  814. for (int col = left; col <= right; ++col)
  815. {
  816. for (int row = top; row <= bottom; ++row)
  817. {
  818. Cell *cell = m_sheet->cellAt( col, row );
  819. if (!cell->doesMergeCells())
  820. {
  821. continue;
  822. }
  823. cell->mergeCells( col, row, 0, 0 );
  824. }
  825. }
  826. }
  827. return true;
  828. }
  829. TQString MergeManipulator::name() const
  830. {
  831. if (m_merge) // MergeManipulator
  832. {
  833. if (m_mergeHorizontal)
  834. {
  835. return i18n("Merge CellsQt::Horizontally");
  836. }
  837. else if (m_mergeVertical)
  838. {
  839. return i18n("Merge CellsQt::Vertically");
  840. }
  841. else
  842. {
  843. return i18n("Merge Cells");
  844. }
  845. }
  846. return i18n("Dissociate Cells");
  847. }
  848. bool MergeManipulator::preProcessing()
  849. {
  850. if (isColumnOrRowSelected())
  851. {
  852. KMessageBox::information( 0, i18n( "Merging of columns or rows is not supported." ) );
  853. return false;
  854. }
  855. if (m_firstrun)
  856. {
  857. // reduce the region to the region occupied by merged cells
  858. Region mergedCells;
  859. ConstIterator endOfList = constEnd();
  860. for (ConstIterator it = constBegin(); it != endOfList; ++it)
  861. {
  862. Element* element = *it;
  863. TQRect range = element->rect().normalize();
  864. int right = range.right();
  865. int bottom = range.bottom();
  866. for (int row = range.top(); row <= bottom; ++row)
  867. {
  868. for (int col = range.left(); col <= right; ++col)
  869. {
  870. Cell *cell = m_sheet->cellAt(col, row);
  871. if (cell->doesMergeCells())
  872. {
  873. TQRect rect(col, row, cell->mergedXCells() + 1, cell->mergedYCells() + 1);
  874. mergedCells.add(rect);
  875. }
  876. }
  877. }
  878. }
  879. if (m_merge) // MergeManipulator
  880. {
  881. // we're in the manipulator's first execution
  882. // initialize the undo manipulator
  883. m_unmerger = new MergeManipulator();
  884. if (!m_mergeHorizontal && !m_mergeVertical)
  885. {
  886. m_unmerger->setReverse(true);
  887. }
  888. m_unmerger->setSheet(m_sheet);
  889. m_unmerger->setRegisterUndo(false);
  890. m_unmerger->add(mergedCells);
  891. }
  892. else // DissociateManipulator
  893. {
  894. clear();
  895. add(mergedCells);
  896. }
  897. }
  898. if (m_merge) // MergeManipulator
  899. {
  900. if (m_reverse) // dissociate
  901. {
  902. }
  903. else // merge
  904. {
  905. // Dissociate cells before merging the whole region.
  906. // For horizontal/vertical merging the cells stay
  907. // as they are. E.g. the region contains a merged cell
  908. // occupying two rows. Then the horizontal merge should
  909. // keep the height of two rows and extend the merging to the
  910. // region's width. In this case the unmerging is done while
  911. // processing each region element.
  912. if (!m_mergeHorizontal && !m_mergeVertical)
  913. {
  914. m_unmerger->execute();
  915. }
  916. }
  917. }
  918. return true;
  919. }
  920. bool MergeManipulator::postProcessing()
  921. {
  922. if (m_merge) // MergeManipulator
  923. {
  924. if (m_reverse) // dissociate
  925. {
  926. // restore the old merge status
  927. if (m_mergeHorizontal || m_mergeVertical)
  928. {
  929. m_unmerger->execute();
  930. }
  931. else
  932. {
  933. m_unmerger->unexecute();
  934. }
  935. }
  936. }
  937. if (!m_reverse)
  938. {
  939. if (m_sheet->getAutoCalc())
  940. {
  941. m_sheet->recalc();
  942. }
  943. }
  944. else
  945. {
  946. m_sheet->refreshMergedCell();
  947. }
  948. return true;
  949. }
  950. /***************************************************************************
  951. class DilationManipulator
  952. ****************************************************************************/
  953. DilationManipulator::DilationManipulator()
  954. : Manipulator()
  955. {
  956. }
  957. DilationManipulator::~DilationManipulator()
  958. {
  959. }
  960. void DilationManipulator::execute()
  961. {
  962. Region extendedRegion;
  963. ConstIterator end(cells().constEnd());
  964. for (ConstIterator it = cells().constBegin(); it != end; ++it)
  965. {
  966. Element* element = *it;
  967. TQRect area = element->rect().normalize();
  968. ColumnFormat *col;
  969. RowFormat *rl;
  970. //look at if column is hiding.
  971. //if it's hiding refreshing column+1 (or column -1 )
  972. int left = area.left();
  973. int right = area.right();
  974. int top = area.top();
  975. int bottom = area.bottom();
  976. // a merged cells is selected
  977. if (element->type() == Region::Element::Point)
  978. {
  979. Cell* cell = m_sheet->cellAt(left, top);
  980. if (cell->doesMergeCells())
  981. {
  982. // extend to the merged region
  983. // prevents artefacts of the selection rectangle
  984. right += cell->mergedXCells();
  985. bottom += cell->mergedYCells();
  986. }
  987. }
  988. if ( right < KS_colMax )
  989. {
  990. do
  991. {
  992. right++;
  993. col = m_sheet->columnFormat( right );
  994. } while ( col->isHide() && right != KS_colMax );
  995. }
  996. if ( left > 1 )
  997. {
  998. do
  999. {
  1000. left--;
  1001. col = m_sheet->columnFormat( left );
  1002. } while ( col->isHide() && left != 1);
  1003. }
  1004. if ( bottom < KS_rowMax )
  1005. {
  1006. do
  1007. {
  1008. bottom++;
  1009. rl = m_sheet->rowFormat( bottom );
  1010. } while ( rl->isHide() && bottom != KS_rowMax );
  1011. }
  1012. if ( top > 1 )
  1013. {
  1014. do
  1015. {
  1016. top--;
  1017. rl = m_sheet->rowFormat( top );
  1018. } while ( rl->isHide() && top != 1);
  1019. }
  1020. area.setLeft(left);
  1021. area.setRight(right);
  1022. area.setTop(top);
  1023. area.setBottom(bottom);
  1024. extendedRegion.add(area, element->sheet());
  1025. }
  1026. clear();
  1027. add(extendedRegion);
  1028. }
  1029. void DilationManipulator::unexecute()
  1030. {
  1031. kdError() << "DilationManipulator::unexecute(): "
  1032. << "An undo of dilating a region is not possible." << endl;
  1033. }
  1034. /***************************************************************************
  1035. class ResizeColumnManipulator
  1036. ****************************************************************************/
  1037. ResizeColumnManipulator::ResizeColumnManipulator()
  1038. {
  1039. }
  1040. ResizeColumnManipulator::~ResizeColumnManipulator()
  1041. {
  1042. }
  1043. bool ResizeColumnManipulator::process(Element* element)
  1044. {
  1045. TQRect range = element->rect().normalize();
  1046. for (int col = range.right(); col >= range.left(); --col)
  1047. {
  1048. ColumnFormat *format = m_sheet->nonDefaultColumnFormat( col );
  1049. format->setDblWidth( TQMAX( 2.0, m_reverse ? m_oldSize : m_newSize ) );
  1050. }
  1051. return true;
  1052. }
  1053. /***************************************************************************
  1054. class ResizeRowManipulator
  1055. ****************************************************************************/
  1056. ResizeRowManipulator::ResizeRowManipulator()
  1057. {
  1058. }
  1059. ResizeRowManipulator::~ResizeRowManipulator()
  1060. {
  1061. }
  1062. bool ResizeRowManipulator::process(Element* element)
  1063. {
  1064. TQRect range = element->rect().normalize();
  1065. for (int row = range.bottom(); row >= range.top(); --row)
  1066. {
  1067. RowFormat* rl = m_sheet->nonDefaultRowFormat( row );
  1068. rl->setDblHeight( TQMAX( 2.0, m_reverse ? m_oldSize : m_newSize ) );
  1069. }
  1070. return true;
  1071. }
  1072. /***************************************************************************
  1073. class AdjustColumnRowManipulator
  1074. ****************************************************************************/
  1075. AdjustColumnRowManipulator::AdjustColumnRowManipulator()
  1076. : Manipulator(),
  1077. m_adjustColumn(false),
  1078. m_adjustRow(false)
  1079. {
  1080. }
  1081. AdjustColumnRowManipulator::~AdjustColumnRowManipulator()
  1082. {
  1083. }
  1084. bool AdjustColumnRowManipulator::process(Element* element)
  1085. {
  1086. Sheet* sheet = m_sheet; // TODO Stefan: element->sheet();
  1087. if (m_sheet && sheet != m_sheet)
  1088. {
  1089. return true;
  1090. }
  1091. TQMap<int,double> heights;
  1092. TQMap<int,double> widths;
  1093. if (m_reverse)
  1094. {
  1095. heights = m_oldHeights;
  1096. widths = m_oldWidths;
  1097. }
  1098. else
  1099. {
  1100. heights = m_newHeights;
  1101. widths = m_newWidths;
  1102. }
  1103. TQRect range = element->rect().normalize();
  1104. if (m_adjustColumn)
  1105. {
  1106. if (element->isRow())
  1107. {
  1108. for (int row = range.top(); row <= range.bottom(); ++row)
  1109. {
  1110. Cell* cell = sheet->getFirstCellRow( row );
  1111. while ( cell )
  1112. {
  1113. int col = cell->column();
  1114. if ( !cell->isEmpty() && !cell->isObscured())
  1115. {
  1116. if (widths.contains(col) && widths[col] != -1.0)
  1117. {
  1118. ColumnFormat* format = sheet->nonDefaultColumnFormat(col);
  1119. if ( kAbs(format->dblWidth() - widths[col] ) > DBL_EPSILON )
  1120. {
  1121. format->setDblWidth( TQMAX( 2.0, widths[col] ) );
  1122. }
  1123. }
  1124. }
  1125. cell = sheet->getNextCellRight(col, row);
  1126. }
  1127. }
  1128. }
  1129. else
  1130. {
  1131. for (int col = range.left(); col <= range.right(); ++col)
  1132. {
  1133. if (widths.contains(col) && widths[col] != -1.0)
  1134. {
  1135. ColumnFormat* format = sheet->nonDefaultColumnFormat(col);
  1136. if ( kAbs(format->dblWidth() - widths[col] ) > DBL_EPSILON )
  1137. {
  1138. format->setDblWidth( TQMAX( 2.0, widths[col] ) );
  1139. }
  1140. }
  1141. }
  1142. }
  1143. }
  1144. if (m_adjustRow)
  1145. {
  1146. if (element->isColumn())
  1147. {
  1148. for (int col = range.left(); col <= range.right(); ++col)
  1149. {
  1150. Cell* cell = sheet->getFirstCellColumn( col );
  1151. while ( cell )
  1152. {
  1153. int row = cell->row();
  1154. if ( !cell->isEmpty() && !cell->isObscured())
  1155. {
  1156. if (heights.contains(row) && heights[row] != -1.0)
  1157. {
  1158. RowFormat* format = sheet->nonDefaultRowFormat(row);
  1159. if ( kAbs(format->dblHeight() - heights[row] ) > DBL_EPSILON )
  1160. {
  1161. format->setDblHeight( TQMAX( 2.0, heights[row] ) );
  1162. }
  1163. }
  1164. }
  1165. cell = sheet->getNextCellDown( col, row );
  1166. }
  1167. }
  1168. }
  1169. else
  1170. {
  1171. for (int row = range.top(); row <= range.bottom(); ++row)
  1172. {
  1173. if (heights.contains(row) && heights[row] != -1.0)
  1174. {
  1175. RowFormat* format = sheet->nonDefaultRowFormat(row);
  1176. if ( kAbs(format->dblHeight() - heights[row] ) > DBL_EPSILON )
  1177. {
  1178. format->setDblHeight( TQMAX( 2.0, heights[row] ) );
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }
  1184. return true;
  1185. }
  1186. bool AdjustColumnRowManipulator::preProcessing()
  1187. {
  1188. if (m_reverse)
  1189. {
  1190. }
  1191. else
  1192. {
  1193. if (!m_newHeights.isEmpty() || !m_newWidths.isEmpty())
  1194. {
  1195. return true;
  1196. }
  1197. // createUndo();
  1198. ConstIterator endOfList(cells().end());
  1199. for (ConstIterator it = cells().begin(); it != endOfList; ++it)
  1200. {
  1201. Element* element = *it;
  1202. TQRect range = element->rect().normalize();
  1203. if (element->isColumn())
  1204. {
  1205. for (int col = range.left(); col <= range.right(); ++col)
  1206. {
  1207. Cell* cell = m_sheet->getFirstCellColumn( col );
  1208. while ( cell )
  1209. {
  1210. int row = cell->row();
  1211. if (m_adjustColumn)
  1212. {
  1213. if (!m_newWidths.contains(col))
  1214. {
  1215. m_newWidths[col] = -1.0;
  1216. ColumnFormat* format = m_sheet->columnFormat(col);
  1217. m_oldWidths[col] = format->dblWidth();
  1218. }
  1219. if (!cell->isEmpty() && !cell->isObscured())
  1220. {
  1221. m_newWidths[col] = TQMAX(adjustColumnHelper(cell, col, row),
  1222. m_newWidths[col] );
  1223. }
  1224. }
  1225. if (m_adjustRow)
  1226. {
  1227. if (!m_newHeights.contains(row))
  1228. {
  1229. m_newHeights[row] = -1.0;
  1230. RowFormat* format = m_sheet->rowFormat(row);
  1231. m_oldHeights[row] = format->dblHeight();
  1232. }
  1233. if (!cell->isEmpty() && !cell->isObscured())
  1234. {
  1235. m_newHeights[row] = TQMAX(adjustRowHelper(cell, col, row),
  1236. m_newHeights[row]);
  1237. }
  1238. }
  1239. cell = m_sheet->getNextCellDown( col, row );
  1240. }
  1241. }
  1242. }
  1243. else if (element->isRow())
  1244. {
  1245. for (int row = range.top(); row <= range.bottom(); ++row)
  1246. {
  1247. Cell* cell = m_sheet->getFirstCellRow( row );
  1248. while ( cell )
  1249. {
  1250. int col = cell->column();
  1251. if (m_adjustColumn)
  1252. {
  1253. if (!m_newWidths.contains(col))
  1254. {
  1255. m_newWidths[col] = -1.0;
  1256. ColumnFormat* format = m_sheet->columnFormat(col);
  1257. m_oldWidths[col] = format->dblWidth();
  1258. }
  1259. if (cell != m_sheet->defaultCell() && !cell->isEmpty() && !cell->isObscured())
  1260. {
  1261. m_newWidths[col] = TQMAX(adjustColumnHelper(cell, col, row),
  1262. m_newWidths[col] );
  1263. }
  1264. }
  1265. if (m_adjustRow)
  1266. {
  1267. if (!m_newHeights.contains(row))
  1268. {
  1269. m_newHeights[row] = -1.0;
  1270. RowFormat* format = m_sheet->rowFormat(row);
  1271. m_oldHeights[row] = format->dblHeight();
  1272. }
  1273. if (cell != m_sheet->defaultCell() && !cell->isEmpty() && !cell->isObscured())
  1274. {
  1275. m_newHeights[row] = TQMAX(adjustRowHelper(cell, col, row),
  1276. m_newHeights[row]);
  1277. }
  1278. }
  1279. cell = m_sheet->getNextCellRight(col, row);
  1280. }
  1281. }
  1282. }
  1283. else
  1284. {
  1285. Cell* cell;
  1286. for (int col = range.left(); col <= range.right(); ++col)
  1287. {
  1288. for ( int row = range.top(); row <= range.bottom(); ++row )
  1289. {
  1290. cell = m_sheet->cellAt( col, row );
  1291. if (m_adjustColumn)
  1292. {
  1293. if (!m_newWidths.contains(col))
  1294. {
  1295. m_newWidths[col] = -1.0;
  1296. ColumnFormat* format = m_sheet->columnFormat(col);
  1297. m_oldWidths[col] = format->dblWidth();
  1298. }
  1299. if (cell != m_sheet->defaultCell() && !cell->isEmpty() && !cell->isObscured())
  1300. {
  1301. m_newWidths[col] = TQMAX(adjustColumnHelper(cell, col, row),
  1302. m_newWidths[col] );
  1303. }
  1304. }
  1305. if (m_adjustRow)
  1306. {
  1307. if (!m_newHeights.contains(row))
  1308. {
  1309. m_newHeights[row] = -1.0;
  1310. RowFormat* format = m_sheet->rowFormat(row);
  1311. m_oldHeights[row] = format->dblHeight();
  1312. }
  1313. if (cell != m_sheet->defaultCell() && !cell->isEmpty() && !cell->isObscured())
  1314. {
  1315. m_newHeights[row] = TQMAX(adjustRowHelper(cell, col, row),
  1316. m_newHeights[row]);
  1317. }
  1318. }
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. return true;
  1325. }
  1326. double AdjustColumnRowManipulator::adjustColumnHelper(Cell* cell, int col, int row )
  1327. {
  1328. double long_max = 0.0;
  1329. cell->calculateTextParameters( m_sheet->painter(), col, row );
  1330. if ( cell->textWidth() > long_max )
  1331. {
  1332. double indent = 0.0;
  1333. Format::Align tqalignment = cell->format()->align(cell->column(), cell->row());
  1334. if (tqalignment == Format::Undefined)
  1335. {
  1336. if (cell->value().isNumber() || cell->isDate() || cell->isTime())
  1337. {
  1338. tqalignment = Format::Right;
  1339. }
  1340. else
  1341. {
  1342. tqalignment = Format::Left;
  1343. }
  1344. }
  1345. if (tqalignment == Format::Left)
  1346. {
  1347. indent = cell->format()->getIndent( cell->column(), cell->row() );
  1348. }
  1349. long_max = indent + cell->textWidth()
  1350. + cell->format()->leftBorderWidth( cell->column(), cell->row() )
  1351. + cell->format()->rightBorderWidth( cell->column(), cell->row() );
  1352. }
  1353. // add 4 because long_max is the length of the text
  1354. // but column has borders
  1355. if ( long_max == 0.0 )
  1356. {
  1357. return -1.0;
  1358. }
  1359. else
  1360. {
  1361. return long_max + 4;
  1362. }
  1363. }
  1364. double AdjustColumnRowManipulator::adjustRowHelper(Cell* cell, int col, int row)
  1365. {
  1366. double long_max = 0.0;
  1367. cell->calculateTextParameters( m_sheet->painter(), col, row);
  1368. if ( cell->textHeight() > long_max )
  1369. {
  1370. long_max = cell->textHeight()
  1371. + cell->format()->topBorderWidth(col, row)
  1372. + cell->format()->bottomBorderWidth(col, row);
  1373. }
  1374. // add 1 because long_max is the height of the text
  1375. // but row has borders
  1376. if ( long_max == 0.0 )
  1377. {
  1378. return -1.0;
  1379. }
  1380. else
  1381. {
  1382. return long_max + 1;
  1383. }
  1384. }
  1385. TQString AdjustColumnRowManipulator::name() const
  1386. {
  1387. if (m_adjustColumn && m_adjustRow)
  1388. {
  1389. return i18n("Adjust Columns/Rows");
  1390. }
  1391. else if (m_adjustColumn)
  1392. {
  1393. return i18n("Adjust Columns");
  1394. }
  1395. else
  1396. {
  1397. return i18n("Adjust Rows");
  1398. }
  1399. }
  1400. /***************************************************************************
  1401. class HideShowManipulator
  1402. ****************************************************************************/
  1403. HideShowManipulator::HideShowManipulator()
  1404. : m_manipulateColumns(false),
  1405. m_manipulateRows(false)
  1406. {
  1407. }
  1408. HideShowManipulator::~HideShowManipulator()
  1409. {
  1410. }
  1411. bool HideShowManipulator::process(Element* element)
  1412. {
  1413. TQRect range = element->rect().normalize();
  1414. if (m_manipulateColumns)
  1415. {
  1416. for (int col = range.left(); col <= range.right(); ++col)
  1417. {
  1418. ColumnFormat* format = m_sheet->nonDefaultColumnFormat(col);
  1419. format->setHide(!m_reverse);
  1420. }
  1421. }
  1422. if (m_manipulateRows)
  1423. {
  1424. for (int row = range.top(); row <= range.bottom(); ++row)
  1425. {
  1426. RowFormat* format = m_sheet->nonDefaultRowFormat(row);
  1427. format->setHide(!m_reverse);
  1428. }
  1429. }
  1430. return true;
  1431. }
  1432. bool HideShowManipulator::preProcessing()
  1433. {
  1434. Region region;
  1435. ConstIterator endOfList = cells().constEnd();
  1436. for (ConstIterator it = cells().constBegin(); it != endOfList; ++it)
  1437. {
  1438. if (m_reverse)
  1439. {
  1440. TQRect range = (*it)->rect().normalize();
  1441. if (m_manipulateColumns)
  1442. {
  1443. if (range.left() > 1)
  1444. {
  1445. int col;
  1446. for (col = 1; col < range.left(); ++col)
  1447. {
  1448. ColumnFormat* format = m_sheet->columnFormat(col);
  1449. if (!format->isHide())
  1450. {
  1451. break;
  1452. }
  1453. }
  1454. if (col == range.left())
  1455. {
  1456. region.add(TQRect(1, 1, range.left()-1, KS_rowMax));
  1457. }
  1458. }
  1459. for (int col = range.left(); col <= range.right(); ++col)
  1460. {
  1461. ColumnFormat* format = m_sheet->columnFormat(col);
  1462. if (format->isHide())
  1463. {
  1464. region.add(TQRect(col, 1, 1, KS_rowMax));
  1465. }
  1466. }
  1467. }
  1468. if (m_manipulateRows)
  1469. {
  1470. if (range.top() > 1)
  1471. {
  1472. int row;
  1473. for (row = 1; row < range.top(); ++row)
  1474. {
  1475. RowFormat* format = m_sheet->rowFormat(row);
  1476. if (!format->isHide())
  1477. {
  1478. break;
  1479. }
  1480. }
  1481. if (row == range.top())
  1482. {
  1483. region.add(TQRect(1, 1, KS_colMax, range.top()-1));
  1484. }
  1485. }
  1486. for (int row = range.top(); row <= range.bottom(); ++row)
  1487. {
  1488. RowFormat* format = m_sheet->rowFormat(row);
  1489. if (format->isHide())
  1490. {
  1491. region.add(TQRect(1, row, KS_colMax, 1));
  1492. }
  1493. }
  1494. }
  1495. }
  1496. if (((*it)->isRow() && m_manipulateColumns) ||
  1497. ((*it)->isColumn() && m_manipulateRows))
  1498. {
  1499. /* KMessageBox::error( this, i18n( "Area is too large." ) );*/
  1500. return false;
  1501. }
  1502. }
  1503. if (m_reverse)
  1504. {
  1505. clear();
  1506. add(region);
  1507. }
  1508. return true;
  1509. }
  1510. bool HideShowManipulator::postProcessing()
  1511. {
  1512. if (m_manipulateColumns)
  1513. {
  1514. m_sheet->emitHideColumn();
  1515. }
  1516. if (m_manipulateRows)
  1517. {
  1518. m_sheet->emitHideRow();
  1519. }
  1520. return true;
  1521. }
  1522. TQString HideShowManipulator::name() const
  1523. {
  1524. TQString name;
  1525. if (m_reverse)
  1526. {
  1527. name = "Show ";
  1528. }
  1529. else
  1530. {
  1531. name = "Hide ";
  1532. }
  1533. if (m_manipulateColumns)
  1534. {
  1535. name += "Columns";
  1536. }
  1537. if (m_manipulateColumns && m_manipulateRows)
  1538. {
  1539. name += "/";
  1540. }
  1541. if (m_manipulateRows)
  1542. {
  1543. name += "Rows";
  1544. }
  1545. return name;
  1546. }
  1547. /***************************************************************************
  1548. class ManipulatorManager
  1549. ****************************************************************************/
  1550. ManipulatorManager* ManipulatorManager::m_self = 0;
  1551. static KStaticDeleter<ManipulatorManager> staticManipulatorManagerDeleter;
  1552. ManipulatorManager* ManipulatorManager::self()
  1553. {
  1554. if (!m_self)
  1555. {
  1556. staticManipulatorManagerDeleter.setObject(m_self, new ManipulatorManager());
  1557. }
  1558. return m_self;
  1559. }
  1560. ManipulatorManager::ManipulatorManager()
  1561. {
  1562. }
  1563. ManipulatorManager::~ManipulatorManager()
  1564. {
  1565. }
  1566. Manipulator* ManipulatorManager::create(const TQString& type)
  1567. {
  1568. if (type == "bgcolor")
  1569. {
  1570. kdDebug() << "Background color manipulator created." << endl;
  1571. // return new FontColorManipulator();
  1572. }
  1573. else if (type == "textcolor")
  1574. {
  1575. kdDebug() << "Text color manipulator created." << endl;
  1576. // return new FontColorManipulator();
  1577. }
  1578. // no manipulator of this type found
  1579. kdError() << "Unknown manipulator!" << endl;
  1580. return 0;
  1581. }