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.

1450 lines
33KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>
  3. Copyright (C) 2000 Torben Weis <weis@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 <stdlib.h>
  18. #include <kdebug.h>
  19. #include "kspread_cell.h"
  20. #include "kspread_format.h"
  21. #include "kspread_cluster.h"
  22. using namespace KSpread;
  23. /****************************************************
  24. *
  25. * Cluster
  26. *
  27. ****************************************************/
  28. /* Generate a matrix LEVEL1 with the size LEVEL1*LEVEL1 */
  29. Cluster::Cluster()
  30. : m_first( 0 ), m_autoDelete( false ), m_biggestX(0), m_biggestY(0)
  31. {
  32. m_cluster = (Cell***)malloc( KSPREAD_CLUSTER_LEVEL1 * KSPREAD_CLUSTER_LEVEL1 * sizeof( Cell** ) );
  33. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  34. for( int y = 0; y < KSPREAD_CLUSTER_LEVEL1; ++y )
  35. m_cluster[ y * KSPREAD_CLUSTER_LEVEL1 + x ] = 0;
  36. }
  37. /* Delete the matrix LEVEL1 and all existing LEVEL2 matrizes */
  38. Cluster::~Cluster()
  39. {
  40. // Can't we use clear(), to remove double code - Philipp?
  41. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  42. for( int y = 0; y < KSPREAD_CLUSTER_LEVEL1; ++y )
  43. {
  44. Cell** cl = m_cluster[ y * KSPREAD_CLUSTER_LEVEL1 + x ];
  45. if ( cl )
  46. {
  47. free( cl );
  48. m_cluster[ y * KSPREAD_CLUSTER_LEVEL1 + x ] = 0;
  49. }
  50. }
  51. if ( m_autoDelete )
  52. {
  53. Cell* cell = m_first;
  54. while( cell )
  55. {
  56. Cell* n = cell->nextCell();
  57. delete cell;
  58. cell = n;
  59. }
  60. }
  61. free( m_cluster );
  62. }
  63. void Cluster::clear()
  64. {
  65. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  66. for( int y = 0; y < KSPREAD_CLUSTER_LEVEL1; ++y )
  67. {
  68. Cell** cl = m_cluster[ y * KSPREAD_CLUSTER_LEVEL1 + x ];
  69. if ( cl )
  70. {
  71. free( cl );
  72. m_cluster[ y * KSPREAD_CLUSTER_LEVEL1 + x ] = 0;
  73. }
  74. }
  75. if ( m_autoDelete )
  76. {
  77. Cell* cell = m_first;
  78. while( cell )
  79. {
  80. Cell* n = cell->nextCell();
  81. delete cell;
  82. cell = n;
  83. }
  84. }
  85. m_first = 0;
  86. m_biggestX = m_biggestY = 0;
  87. }
  88. Cell* Cluster::lookup( int x, int y ) const
  89. {
  90. if ( x >= KSPREAD_CLUSTER_MAX || x < 0 || y >= KSPREAD_CLUSTER_MAX || y < 0 )
  91. {
  92. kdDebug(36001) << "Cluster::lookup: invalid column or row value (col: "
  93. << x << " | row: " << y << ")" << endl;
  94. return 0;
  95. }
  96. int cx = x / KSPREAD_CLUSTER_LEVEL2;
  97. int cy = y / KSPREAD_CLUSTER_LEVEL2;
  98. int dx = x % KSPREAD_CLUSTER_LEVEL2;
  99. int dy = y % KSPREAD_CLUSTER_LEVEL2;
  100. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ];
  101. if ( !cl )
  102. return 0;
  103. return cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ];
  104. }
  105. /* Paste a cell in LEVEL2 (it's more paste than insert) */
  106. void Cluster::insert( Cell* cell, int x, int y )
  107. {
  108. if ( x >= KSPREAD_CLUSTER_MAX || x < 0 || y >= KSPREAD_CLUSTER_MAX || y < 0 )
  109. {
  110. kdDebug(36001) << "Cluster::insert: invalid column or row value (col: "
  111. << x << " | row: " << y << ")" << endl;
  112. return;
  113. }
  114. int cx = x / KSPREAD_CLUSTER_LEVEL2;
  115. int cy = y / KSPREAD_CLUSTER_LEVEL2;
  116. int dx = x % KSPREAD_CLUSTER_LEVEL2;
  117. int dy = y % KSPREAD_CLUSTER_LEVEL2;
  118. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ];
  119. if ( !cl )
  120. {
  121. cl = (Cell**)malloc( KSPREAD_CLUSTER_LEVEL2 * KSPREAD_CLUSTER_LEVEL2 * sizeof( Cell* ) );
  122. m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ] = cl;
  123. for( int a = 0; a < KSPREAD_CLUSTER_LEVEL2; ++a )
  124. for( int b = 0; b < KSPREAD_CLUSTER_LEVEL2; ++b )
  125. cl[ b * KSPREAD_CLUSTER_LEVEL2 + a ] = 0;
  126. }
  127. if ( cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ] )
  128. remove( x, y );
  129. cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ] = cell;
  130. if ( m_first )
  131. {
  132. cell->setNextCell( m_first );
  133. m_first->setPreviousCell( cell );
  134. }
  135. m_first = cell;
  136. if (x > m_biggestX) m_biggestX = x;
  137. if (y > m_biggestY) m_biggestY = y;
  138. }
  139. /* Removes the cell of a matrix, the matrix itself keeps unchanged */
  140. void Cluster::remove( int x, int y )
  141. {
  142. if ( x >= KSPREAD_CLUSTER_MAX || x < 0 || y >= KSPREAD_CLUSTER_MAX || y < 0 )
  143. {
  144. kdDebug(36001) << "Cluster::remove: invalid column or row value (col: "
  145. << x << " | row: " << y << ")" << endl;
  146. return;
  147. }
  148. int cx = x / KSPREAD_CLUSTER_LEVEL2;
  149. int cy = y / KSPREAD_CLUSTER_LEVEL2;
  150. int dx = x % KSPREAD_CLUSTER_LEVEL2;
  151. int dy = y % KSPREAD_CLUSTER_LEVEL2;
  152. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ];
  153. if ( !cl )
  154. return;
  155. Cell* c = cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ];
  156. if ( !c )
  157. return;
  158. cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ] = 0;
  159. if ( m_autoDelete )
  160. {
  161. if ( m_first == c )
  162. m_first = c->nextCell();
  163. if(c->doesMergeCells())
  164. {
  165. c->mergeCells(c->column(),c->row(),0,0);
  166. }
  167. delete c;
  168. }
  169. else
  170. {
  171. if ( m_first == c )
  172. m_first = c->nextCell();
  173. if ( c->previousCell() )
  174. c->previousCell()->setNextCell( c->nextCell() );
  175. if ( c->nextCell() )
  176. c->nextCell()->setPreviousCell( c->previousCell() );
  177. c->setNextCell( 0 );
  178. c->setPreviousCell( 0 );
  179. }
  180. }
  181. bool Cluster::shiftRow( const TQPoint& marker )
  182. {
  183. bool dummy;
  184. return shiftRow( marker, dummy );
  185. }
  186. bool Cluster::shiftColumn( const TQPoint& marker )
  187. {
  188. bool dummy;
  189. return shiftColumn( marker, dummy );
  190. }
  191. void Cluster::unshiftColumn( const TQPoint& marker )
  192. {
  193. bool dummy;
  194. unshiftColumn( marker, dummy );
  195. }
  196. void Cluster::unshiftRow( const TQPoint& marker )
  197. {
  198. bool dummy;
  199. unshiftRow( marker, dummy );
  200. }
  201. void Cluster::setAutoDelete( bool b )
  202. {
  203. m_autoDelete = b;
  204. }
  205. bool Cluster::autoDelete() const
  206. {
  207. return m_autoDelete;
  208. }
  209. Cell* Cluster::firstCell() const
  210. {
  211. return m_first;
  212. }
  213. bool Cluster::shiftRow( const TQPoint& marker, bool& work )
  214. {
  215. work = false;
  216. if ( marker.x() >= KSPREAD_CLUSTER_MAX || marker.x() < 0 ||
  217. marker.y() >= KSPREAD_CLUSTER_MAX || marker.y() < 0 )
  218. {
  219. kdDebug(36001) << "Cluster::shiftRow: invalid column or row value (col: "
  220. << marker.x() << " | row: " << marker.y() << ")" << endl;
  221. return false;
  222. }
  223. int cx = marker.x() / KSPREAD_CLUSTER_LEVEL2;
  224. int cy = marker.y() / KSPREAD_CLUSTER_LEVEL2;
  225. int dx = marker.x() % KSPREAD_CLUSTER_LEVEL2;
  226. int dy = marker.y() % KSPREAD_CLUSTER_LEVEL2;
  227. // Is there a cell at the bottom most position ?
  228. // In this case the shift is impossible.
  229. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + KSPREAD_CLUSTER_LEVEL1 - 1 ];
  230. if ( cl && cl[ dy * KSPREAD_CLUSTER_LEVEL2 + KSPREAD_CLUSTER_LEVEL2 - 1 ] )
  231. return false;
  232. bool a = autoDelete();
  233. setAutoDelete( false );
  234. // Move cells in this row one down.
  235. for( int i = KSPREAD_CLUSTER_LEVEL1 - 1; i >= cx ; --i )
  236. {
  237. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + i ];
  238. if ( cl )
  239. {
  240. work = true;
  241. int left = 0;
  242. if ( i == cx )
  243. left = dx;
  244. int right = KSPREAD_CLUSTER_LEVEL2 - 1;
  245. if ( i == KSPREAD_CLUSTER_LEVEL1 - 1 )
  246. right = KSPREAD_CLUSTER_LEVEL2 - 2;
  247. for( int k = right; k >= left; --k )
  248. {
  249. Cell* c = cl[ dy * KSPREAD_CLUSTER_LEVEL2 + k ];
  250. if ( c )
  251. {
  252. remove( c->column(), c->row() );
  253. c->move( c->column() + 1, c->row() );
  254. insert( c, c->column(), c->row() );
  255. }
  256. }
  257. }
  258. }
  259. setAutoDelete( a );
  260. return true;
  261. }
  262. bool Cluster::shiftColumn( const TQPoint& marker, bool& work )
  263. {
  264. work = false;
  265. if ( marker.x() >= KSPREAD_CLUSTER_MAX || marker.x() < 0 ||
  266. marker.y() >= KSPREAD_CLUSTER_MAX || marker.y() < 0 )
  267. {
  268. kdDebug(36001) << "Cluster::shiftColumn: invalid column or row value (col: "
  269. << marker.x() << " | row: " << marker.y() << ")" << endl;
  270. return false;
  271. }
  272. int cx = marker.x() / KSPREAD_CLUSTER_LEVEL2;
  273. int cy = marker.y() / KSPREAD_CLUSTER_LEVEL2;
  274. int dx = marker.x() % KSPREAD_CLUSTER_LEVEL2;
  275. int dy = marker.y() % KSPREAD_CLUSTER_LEVEL2;
  276. // Is there a cell at the right most position ?
  277. // In this case the shift is impossible.
  278. Cell** cl = m_cluster[ KSPREAD_CLUSTER_LEVEL1 * ( KSPREAD_CLUSTER_LEVEL1 - 1 ) + cx ];
  279. if ( cl && cl[ KSPREAD_CLUSTER_LEVEL2 * ( KSPREAD_CLUSTER_LEVEL2 - 1 ) + dx ] )
  280. return false;
  281. bool a = autoDelete();
  282. setAutoDelete( false );
  283. // Move cells in this column one right.
  284. for( int i = KSPREAD_CLUSTER_LEVEL1 - 1; i >= cy ; --i )
  285. {
  286. Cell** cl = m_cluster[ i * KSPREAD_CLUSTER_LEVEL1 + cx ];
  287. if ( cl )
  288. {
  289. work = true;
  290. int top = 0;
  291. if ( i == cy )
  292. top = dy;
  293. int bottom = KSPREAD_CLUSTER_LEVEL2 - 1;
  294. if ( i == KSPREAD_CLUSTER_LEVEL1 - 1 )
  295. bottom = KSPREAD_CLUSTER_LEVEL2 - 2;
  296. for( int k = bottom; k >= top; --k )
  297. {
  298. Cell* c = cl[ k * KSPREAD_CLUSTER_LEVEL2 + dx ];
  299. if ( c )
  300. {
  301. remove( c->column(), c->row() );
  302. c->move( c->column(), c->row() + 1 );
  303. insert( c, c->column(), c->row() );
  304. }
  305. }
  306. }
  307. }
  308. setAutoDelete( a );
  309. return true;
  310. }
  311. bool Cluster::insertColumn( int col )
  312. {
  313. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  314. {
  315. kdDebug(36001) << "Cluster::insertColumn: invalid column value (col: "
  316. << col << ")" << endl;
  317. return false;
  318. }
  319. // Is there a cell at the right most position ?
  320. // In this case the shift is impossible.
  321. for( int t1 = 0; t1 < KSPREAD_CLUSTER_LEVEL1; ++t1 )
  322. {
  323. Cell** cl = m_cluster[ t1 * KSPREAD_CLUSTER_LEVEL1 + KSPREAD_CLUSTER_LEVEL1 - 1 ];
  324. if ( cl )
  325. for( int t2 = 0; t2 < KSPREAD_CLUSTER_LEVEL2; ++t2 )
  326. if ( cl[ t2 * KSPREAD_CLUSTER_LEVEL2 + KSPREAD_CLUSTER_LEVEL2 - 1 ] )
  327. return false;
  328. }
  329. for( int t1 = 0; t1 < KSPREAD_CLUSTER_LEVEL1; ++t1 )
  330. {
  331. bool work = true;
  332. for( int t2 = 0; work && t2 < KSPREAD_CLUSTER_LEVEL2; ++t2 )
  333. shiftRow( TQPoint( col, t1 * KSPREAD_CLUSTER_LEVEL2 + t2 ), work );
  334. }
  335. return true;
  336. }
  337. bool Cluster::insertRow( int row )
  338. {
  339. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  340. {
  341. kdDebug(36001) << "Cluster::insertRow: invalid row value (row: "
  342. << row << ")" << endl;
  343. return false;
  344. }
  345. // Is there a cell at the bottom most position ?
  346. // In this case the shift is impossible.
  347. for( int t1 = 0; t1 < KSPREAD_CLUSTER_LEVEL1; ++t1 )
  348. {
  349. Cell** cl = m_cluster[ KSPREAD_CLUSTER_LEVEL1 * ( KSPREAD_CLUSTER_LEVEL1 - 1 ) + t1 ];
  350. if ( cl )
  351. for( int t2 = 0; t2 < KSPREAD_CLUSTER_LEVEL2; ++t2 )
  352. if ( cl[ KSPREAD_CLUSTER_LEVEL2 * ( KSPREAD_CLUSTER_LEVEL2 - 1 ) + t2 ] )
  353. return false;
  354. }
  355. for( int t1 = 0; t1 < KSPREAD_CLUSTER_LEVEL1; ++t1 )
  356. {
  357. bool work = true;
  358. for( int t2 = 0; work && t2 < KSPREAD_CLUSTER_LEVEL2; ++t2 )
  359. shiftColumn( TQPoint( t1 * KSPREAD_CLUSTER_LEVEL2 + t2, row ), work );
  360. }
  361. return true;
  362. }
  363. void Cluster::unshiftColumn( const TQPoint& marker, bool& work )
  364. {
  365. work = false;
  366. if ( marker.x() >= KSPREAD_CLUSTER_MAX || marker.x() < 0 ||
  367. marker.y() >= KSPREAD_CLUSTER_MAX || marker.y() < 0 )
  368. {
  369. kdDebug(36001) << "Cluster::unshiftColumn: invalid column or row value (col: "
  370. << marker.x() << " | row: " << marker.y() << ")" << endl;
  371. return;
  372. }
  373. int cx = marker.x() / KSPREAD_CLUSTER_LEVEL2;
  374. int cy = marker.y() / KSPREAD_CLUSTER_LEVEL2;
  375. int dx = marker.x() % KSPREAD_CLUSTER_LEVEL2;
  376. int dy = marker.y() % KSPREAD_CLUSTER_LEVEL2;
  377. bool a = autoDelete();
  378. setAutoDelete( false );
  379. // Move cells in this column one column to the left.
  380. for( int i = cy; i < KSPREAD_CLUSTER_LEVEL1; ++i )
  381. {
  382. Cell** cl = m_cluster[ i * KSPREAD_CLUSTER_LEVEL1 + cx ];
  383. if ( cl )
  384. {
  385. work = true;
  386. int top = 0;
  387. if ( i == cy )
  388. top = dy + 1;
  389. int bottom = KSPREAD_CLUSTER_LEVEL2 - 1;
  390. for( int k = top; k <= bottom; ++k )
  391. {
  392. Cell* c = cl[ k * KSPREAD_CLUSTER_LEVEL2 + dx ];
  393. if ( c )
  394. {
  395. remove( c->column(), c->row() );
  396. c->move( c->column(), c->row() - 1 );
  397. insert( c, c->column(), c->row() );
  398. }
  399. }
  400. }
  401. }
  402. setAutoDelete( a );
  403. }
  404. void Cluster::unshiftRow( const TQPoint& marker, bool& work )
  405. {
  406. work = false;
  407. if ( marker.x() >= KSPREAD_CLUSTER_MAX || marker.x() < 0 ||
  408. marker.y() >= KSPREAD_CLUSTER_MAX || marker.y() < 0 )
  409. {
  410. kdDebug(36001) << "Cluster::unshiftRow: invalid column or row value (col: "
  411. << marker.x() << " | row: " << marker.y() << ")" << endl;
  412. return;
  413. }
  414. int cx = marker.x() / KSPREAD_CLUSTER_LEVEL2;
  415. int cy = marker.y() / KSPREAD_CLUSTER_LEVEL2;
  416. int dx = marker.x() % KSPREAD_CLUSTER_LEVEL2;
  417. int dy = marker.y() % KSPREAD_CLUSTER_LEVEL2;
  418. bool a = autoDelete();
  419. setAutoDelete( false );
  420. // Move cells in this row one row up.
  421. for( int i = cx; i < KSPREAD_CLUSTER_LEVEL1; ++i )
  422. {
  423. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + i ];
  424. if ( cl )
  425. {
  426. work = true;
  427. int left = 0;
  428. if ( i == cx )
  429. left = dx + 1;
  430. int right = KSPREAD_CLUSTER_LEVEL2 - 1;
  431. for( int k = left; k <= right; ++k )
  432. {
  433. Cell* c = cl[ dy * KSPREAD_CLUSTER_LEVEL2 + k ];
  434. if ( c )
  435. {
  436. remove( c->column(), c->row() );
  437. c->move( c->column() - 1, c->row() );
  438. insert( c, c->column(), c->row() );
  439. }
  440. }
  441. }
  442. }
  443. setAutoDelete( a );
  444. }
  445. void Cluster::removeColumn( int col )
  446. {
  447. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  448. {
  449. kdDebug(36001) << "Cluster::removeColumn: invalid column value (col: "
  450. << col << ")" << endl;
  451. return;
  452. }
  453. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  454. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  455. for( int y1 = 0; y1 < KSPREAD_CLUSTER_LEVEL1; ++y1 )
  456. {
  457. Cell** cl = m_cluster[ y1 * KSPREAD_CLUSTER_LEVEL1 + cx ];
  458. if ( cl )
  459. for( int y2 = 0; y2 < KSPREAD_CLUSTER_LEVEL2; ++y2 )
  460. if ( cl[ y2 * KSPREAD_CLUSTER_LEVEL2 + dx ] )
  461. remove( col, y1 * KSPREAD_CLUSTER_LEVEL1 + y2 );
  462. }
  463. for( int t1 = 0; t1 < KSPREAD_CLUSTER_LEVEL1; ++t1 )
  464. {
  465. bool work = true;
  466. for( int t2 = 0; work && t2 < KSPREAD_CLUSTER_LEVEL2; ++t2 )
  467. unshiftRow( TQPoint( col, t1 * KSPREAD_CLUSTER_LEVEL2 + t2 ), work );
  468. }
  469. }
  470. void Cluster::removeRow( int row )
  471. {
  472. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  473. {
  474. kdDebug(36001) << "Cluster::removeRow: invalid row value (row: "
  475. << row << ")" << endl;
  476. return;
  477. }
  478. int cy = row / KSPREAD_CLUSTER_LEVEL2;
  479. int dy = row % KSPREAD_CLUSTER_LEVEL2;
  480. for( int x1 = 0; x1 < KSPREAD_CLUSTER_LEVEL1; ++x1 )
  481. {
  482. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + x1 ];
  483. if ( cl )
  484. for( int x2 = 0; x2 < KSPREAD_CLUSTER_LEVEL2; ++x2 )
  485. if ( cl[ dy * KSPREAD_CLUSTER_LEVEL2 + x2 ] )
  486. remove( x1 * KSPREAD_CLUSTER_LEVEL2 + x2, row );
  487. }
  488. for( int t1 = 0; t1 < KSPREAD_CLUSTER_LEVEL1; ++t1 )
  489. {
  490. bool work = true;
  491. for( int t2 = 0; work && t2 < KSPREAD_CLUSTER_LEVEL2; ++t2 )
  492. unshiftColumn( TQPoint( t1 * KSPREAD_CLUSTER_LEVEL2 + t2, row ), work );
  493. }
  494. }
  495. void Cluster::clearColumn( int col )
  496. {
  497. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  498. {
  499. kdDebug(36001) << "Cluster::clearColumn: invalid column value (col: "
  500. << col << ")" << endl;
  501. return;
  502. }
  503. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  504. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  505. for( int cy = 0; cy < KSPREAD_CLUSTER_LEVEL1; ++cy )
  506. {
  507. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ];
  508. if ( cl )
  509. for( int dy = 0; dy < KSPREAD_CLUSTER_LEVEL2; ++dy )
  510. if ( cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ] )
  511. {
  512. int row = cy * KSPREAD_CLUSTER_LEVEL2 + dy ;
  513. remove( col, row );
  514. }
  515. }
  516. }
  517. void Cluster::clearRow( int row )
  518. {
  519. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  520. {
  521. kdDebug(36001) << "Cluster::clearRow: invalid row value (row: "
  522. << row << ")" << endl;
  523. return;
  524. }
  525. int cy = row / KSPREAD_CLUSTER_LEVEL2;
  526. int dy = row % KSPREAD_CLUSTER_LEVEL2;
  527. for( int cx = 0; cx < KSPREAD_CLUSTER_LEVEL1; ++cx )
  528. {
  529. Cell** cl = m_cluster[ cy * KSPREAD_CLUSTER_LEVEL2 + cx ];
  530. if ( cl )
  531. for( int dx = 0; dx < KSPREAD_CLUSTER_LEVEL2; ++dx )
  532. if ( cl[ dy * KSPREAD_CLUSTER_LEVEL2 + dx ] )
  533. {
  534. int column = cx * KSPREAD_CLUSTER_LEVEL2 + dx ;
  535. remove( column, row );
  536. }
  537. }
  538. }
  539. Value Cluster::valueRange (int col1, int row1,
  540. int col2, int row2) const
  541. {
  542. Value empty;
  543. //swap first/second values if needed
  544. if (col1 > col2)
  545. {
  546. int p = col1; col1 = col2; col2 = p;
  547. }
  548. if (row1 > row2)
  549. {
  550. int p = row1; row1 = col2; row2 = p;
  551. }
  552. if ((row1 < 0) || (col1 < 0) || (row2 > KSPREAD_CLUSTER_MAX) ||
  553. (col2 > KSPREAD_CLUSTER_MAX))
  554. return empty;
  555. // if we are out of range occupied by cells, we return an empty
  556. // array of the requested size
  557. if ((row1 > m_biggestY) || (col1 > m_biggestX))
  558. {
  559. int cols = col2 - col1 + 1;
  560. int rows = row2 - row1 + 1;
  561. Value array (cols, rows);
  562. return array;
  563. }
  564. return makeArray (col1, row1, col2, row2);
  565. }
  566. Value Cluster::makeArray (int col1, int row1,
  567. int col2, int row2) const
  568. {
  569. // this generates an array of values
  570. // TODO: make this thing faster by skipping empty regions
  571. int cols = col2 - col1 + 1;
  572. int rows = row2 - row1 + 1;
  573. Value array (cols, rows);
  574. for (int row = row1; row <= row2; ++row)
  575. for (int col = col1; col <= col2; ++col)
  576. {
  577. Cell *cell = lookup (col, row);
  578. if (cell)
  579. {
  580. Value val = cell->value();
  581. array.setElement (col-col1, row-row1, val);
  582. }
  583. }
  584. //return the result
  585. return array;
  586. }
  587. Cell* Cluster::getFirstCellColumn(int col) const
  588. {
  589. Cell* cell = lookup(col, 1);
  590. if (cell == NULL)
  591. {
  592. cell = getNextCellDown(col, 1);
  593. }
  594. return cell;
  595. }
  596. Cell* Cluster::getLastCellColumn(int col) const
  597. {
  598. Cell* cell = lookup(col, KS_rowMax);
  599. if (cell == NULL)
  600. {
  601. cell = getNextCellUp(col, KS_rowMax);
  602. }
  603. return cell;
  604. }
  605. Cell* Cluster::getFirstCellRow(int row) const
  606. {
  607. Cell* cell = lookup(1, row);
  608. if (cell == NULL)
  609. {
  610. cell = getNextCellRight(1, row);
  611. }
  612. return cell;
  613. }
  614. Cell* Cluster::getLastCellRow(int row) const
  615. {
  616. Cell* cell = lookup(KS_colMax, row);
  617. if (cell == NULL)
  618. {
  619. cell = getNextCellLeft(KS_colMax, row);
  620. }
  621. return cell;
  622. }
  623. Cell* Cluster::getNextCellUp(int col, int row) const
  624. {
  625. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  626. int cy = (row - 1) / KSPREAD_CLUSTER_LEVEL2;
  627. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  628. int dy = (row - 1) % KSPREAD_CLUSTER_LEVEL2;
  629. while (cy >= 0)
  630. {
  631. if ( m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ] != NULL )
  632. {
  633. while (dy >= 0)
  634. {
  635. if ( m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx]
  636. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx] != NULL )
  637. {
  638. return m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx ]
  639. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx];
  640. }
  641. dy--;
  642. }
  643. }
  644. cy--;
  645. dy = KSPREAD_CLUSTER_LEVEL2 - 1;
  646. }
  647. return NULL;
  648. }
  649. Cell* Cluster::getNextCellDown(int col, int row) const
  650. {
  651. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  652. int cy = (row + 1) / KSPREAD_CLUSTER_LEVEL2;
  653. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  654. int dy = (row + 1) % KSPREAD_CLUSTER_LEVEL2;
  655. while (cy < KSPREAD_CLUSTER_LEVEL1)
  656. {
  657. if ( m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ] != NULL )
  658. {
  659. while (dy < KSPREAD_CLUSTER_LEVEL2)
  660. {
  661. if ( m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx]
  662. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx] != NULL )
  663. {
  664. return m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx ]
  665. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx];
  666. }
  667. dy++;
  668. }
  669. }
  670. cy++;
  671. dy = 0;
  672. }
  673. return NULL;
  674. }
  675. Cell* Cluster::getNextCellLeft(int col, int row) const
  676. {
  677. int cx = (col - 1) / KSPREAD_CLUSTER_LEVEL2;
  678. int cy = row / KSPREAD_CLUSTER_LEVEL2;
  679. int dx = (col - 1) % KSPREAD_CLUSTER_LEVEL2;
  680. int dy = row % KSPREAD_CLUSTER_LEVEL2;
  681. while (cx >= 0)
  682. {
  683. if ( m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ] != NULL )
  684. {
  685. while (dx >= 0)
  686. {
  687. if ( m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx]
  688. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx] != NULL )
  689. {
  690. return m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx ]
  691. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx];
  692. }
  693. dx--;
  694. }
  695. }
  696. cx--;
  697. dx = KSPREAD_CLUSTER_LEVEL2 - 1;
  698. }
  699. return NULL;
  700. }
  701. Cell* Cluster::getNextCellRight(int col, int row) const
  702. {
  703. int cx = (col + 1) / KSPREAD_CLUSTER_LEVEL2;
  704. int cy = row / KSPREAD_CLUSTER_LEVEL2;
  705. int dx = (col + 1) % KSPREAD_CLUSTER_LEVEL2;
  706. int dy = row % KSPREAD_CLUSTER_LEVEL2;
  707. while (cx < KSPREAD_CLUSTER_LEVEL1)
  708. {
  709. if ( m_cluster[ cy * KSPREAD_CLUSTER_LEVEL1 + cx ] != NULL )
  710. {
  711. while (dx < KSPREAD_CLUSTER_LEVEL2)
  712. {
  713. if ( m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx]
  714. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx] != NULL )
  715. {
  716. return m_cluster[ cy*KSPREAD_CLUSTER_LEVEL1 + cx ]
  717. [ dy*KSPREAD_CLUSTER_LEVEL2 + dx];
  718. }
  719. dx++;
  720. }
  721. }
  722. cx++;
  723. dx = 0;
  724. }
  725. return NULL;
  726. }
  727. /****************************************************
  728. *
  729. * ColumnCluster
  730. *
  731. ****************************************************/
  732. ColumnCluster::ColumnCluster()
  733. : m_first( 0 ), m_autoDelete( false )
  734. {
  735. m_cluster = (ColumnFormat***)malloc( KSPREAD_CLUSTER_LEVEL1 * sizeof( ColumnFormat** ) );
  736. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  737. m_cluster[ x ] = 0;
  738. }
  739. ColumnCluster::~ColumnCluster()
  740. {
  741. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  742. {
  743. ColumnFormat** cl = m_cluster[ x ];
  744. if ( cl )
  745. {
  746. free( cl );
  747. m_cluster[ x ] = 0;
  748. }
  749. }
  750. if ( m_autoDelete )
  751. {
  752. ColumnFormat* cell = m_first;
  753. while( cell )
  754. {
  755. ColumnFormat* n = cell->next();
  756. delete cell;
  757. cell = n;
  758. }
  759. }
  760. free( m_cluster );
  761. }
  762. ColumnFormat* ColumnCluster::lookup( int col )
  763. {
  764. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  765. {
  766. kdDebug(36001) << "ColumnCluster::lookup: invalid column value (col: "
  767. << col << ")" << endl;
  768. return 0;
  769. }
  770. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  771. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  772. ColumnFormat** cl = m_cluster[ cx ];
  773. if ( !cl )
  774. return 0;
  775. return cl[ dx ];
  776. }
  777. const ColumnFormat* ColumnCluster::lookup( int col ) const
  778. {
  779. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  780. {
  781. kdDebug(36001) << "ColumnCluster::lookup: invalid column value (col: "
  782. << col << ")" << endl;
  783. return 0;
  784. }
  785. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  786. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  787. ColumnFormat** cl = m_cluster[ cx ];
  788. if ( !cl )
  789. return 0;
  790. return cl[ dx ];
  791. }
  792. void ColumnCluster::clear()
  793. {
  794. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  795. {
  796. ColumnFormat** cl = m_cluster[ x ];
  797. if ( cl )
  798. {
  799. free( cl );
  800. m_cluster[ x ] = 0;
  801. }
  802. }
  803. if ( m_autoDelete )
  804. {
  805. ColumnFormat* cell = m_first;
  806. while( cell )
  807. {
  808. ColumnFormat* n = cell->next();
  809. delete cell;
  810. cell = n;
  811. }
  812. }
  813. m_first = 0;
  814. }
  815. void ColumnCluster::insertElement( ColumnFormat* lay, int col )
  816. {
  817. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  818. {
  819. kdDebug(36001) << "ColumnCluster::insertElement: invalid column value (col: "
  820. << col << ")" << endl;
  821. return;
  822. }
  823. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  824. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  825. ColumnFormat** cl = m_cluster[ cx ];
  826. if ( !cl )
  827. {
  828. cl = (ColumnFormat**)malloc( KSPREAD_CLUSTER_LEVEL2 * sizeof( ColumnFormat* ) );
  829. m_cluster[ cx ] = cl;
  830. for( int a = 0; a < KSPREAD_CLUSTER_LEVEL2; ++a )
  831. cl[ a ] = 0;
  832. }
  833. if ( cl[ dx ] )
  834. removeElement( col );
  835. cl[ dx ] = lay;
  836. if ( m_first )
  837. {
  838. lay->setNext( m_first );
  839. m_first->setPrevious( lay );
  840. }
  841. m_first = lay;
  842. }
  843. void ColumnCluster::removeElement( int col )
  844. {
  845. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  846. {
  847. kdDebug(36001) << "ColumnCluster::removeElement: invalid column value (col: "
  848. << col << ")" << endl;
  849. return;
  850. }
  851. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  852. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  853. ColumnFormat** cl = m_cluster[ cx ];
  854. if ( !cl )
  855. return;
  856. ColumnFormat* c = cl[ dx ];
  857. if ( !c )
  858. return;
  859. cl[ dx ] = 0;
  860. if ( m_autoDelete )
  861. {
  862. if ( m_first == c )
  863. m_first = c->next();
  864. delete c;
  865. }
  866. else
  867. {
  868. if ( m_first == c )
  869. m_first = c->next();
  870. if ( c->previous() )
  871. c->previous()->setNext( c->next() );
  872. if ( c->next() )
  873. c->next()->setPrevious( c->previous() );
  874. c->setNext( 0 );
  875. c->setPrevious( 0 );
  876. }
  877. }
  878. bool ColumnCluster::insertColumn( int col )
  879. {
  880. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  881. {
  882. kdDebug(36001) << "ColumnCluster::insertColumn: invalid column value (col: "
  883. << col << ")" << endl;
  884. return false;
  885. }
  886. int cx = col / KSPREAD_CLUSTER_LEVEL2;
  887. int dx = col % KSPREAD_CLUSTER_LEVEL2;
  888. // Is there a column layout at the right most position ?
  889. // In this case the shift is impossible.
  890. ColumnFormat** cl = m_cluster[ KSPREAD_CLUSTER_LEVEL1 - 1 ];
  891. if ( cl && cl[ KSPREAD_CLUSTER_LEVEL2 - 1 ] )
  892. return false;
  893. bool a = autoDelete();
  894. setAutoDelete( false );
  895. for( int i = KSPREAD_CLUSTER_LEVEL1 - 1; i >= cx ; --i )
  896. {
  897. ColumnFormat** cl = m_cluster[ i ];
  898. if ( cl )
  899. {
  900. int left = 0;
  901. if ( i == cx )
  902. left = dx;
  903. int right = KSPREAD_CLUSTER_LEVEL2 - 1;
  904. if ( i == KSPREAD_CLUSTER_LEVEL1 - 1 )
  905. right = KSPREAD_CLUSTER_LEVEL2 - 2;
  906. for( int k = right; k >= left; --k )
  907. {
  908. ColumnFormat* c = cl[ k ];
  909. if ( c )
  910. {
  911. removeElement( c->column() );
  912. c->setColumn( c->column() + 1 );
  913. insertElement( c, c->column() );
  914. }
  915. }
  916. }
  917. }
  918. setAutoDelete( a );
  919. return true;
  920. }
  921. bool ColumnCluster::removeColumn( int column )
  922. {
  923. if ( column >= KSPREAD_CLUSTER_MAX || column < 0 )
  924. {
  925. kdDebug(36001) << "ColumnCluster::removeColumn: invalid column value (col: "
  926. << column << ")" << endl;
  927. return false;
  928. }
  929. int cx = column / KSPREAD_CLUSTER_LEVEL2;
  930. int dx = column % KSPREAD_CLUSTER_LEVEL2;
  931. removeElement( column );
  932. bool a = autoDelete();
  933. setAutoDelete( false );
  934. for( int i = cx; i < KSPREAD_CLUSTER_LEVEL1; ++i )
  935. {
  936. ColumnFormat** cl = m_cluster[ i ];
  937. if ( cl )
  938. {
  939. int left = 0;
  940. if ( i == cx )
  941. left = dx + 1;
  942. int right = KSPREAD_CLUSTER_LEVEL2 - 1;
  943. for( int k = left; k <= right; ++k )
  944. {
  945. ColumnFormat* c = cl[ k ];
  946. if ( c )
  947. {
  948. removeElement( c->column() );
  949. c->setColumn( c->column() - 1 );
  950. insertElement( c, c->column() );
  951. }
  952. }
  953. }
  954. }
  955. setAutoDelete( a );
  956. return true;
  957. }
  958. void ColumnCluster::setAutoDelete( bool a )
  959. {
  960. m_autoDelete = a;
  961. }
  962. bool ColumnCluster::autoDelete() const
  963. {
  964. return m_autoDelete;
  965. }
  966. ColumnFormat* ColumnCluster::next( int col ) const
  967. {
  968. if ( col >= KSPREAD_CLUSTER_MAX || col < 0 )
  969. {
  970. kdDebug(36001) << "ColumnCluster::next: invalid column value (col: "
  971. << col << ")" << endl;
  972. return 0;
  973. }
  974. int cx = (col + 1) / KSPREAD_CLUSTER_LEVEL2;
  975. int dx = (col + 1) % KSPREAD_CLUSTER_LEVEL2;
  976. while ( cx < KSPREAD_CLUSTER_LEVEL1 )
  977. {
  978. if ( m_cluster[ cx ] )
  979. {
  980. while ( dx < KSPREAD_CLUSTER_LEVEL2 )
  981. {
  982. if ( m_cluster[ cx ][ dx ] )
  983. {
  984. return m_cluster[ cx ][ dx ];
  985. }
  986. ++dx;
  987. }
  988. }
  989. ++cx;
  990. dx = 0;
  991. }
  992. return 0;
  993. }
  994. /****************************************************
  995. *
  996. * RowCluster
  997. *
  998. ****************************************************/
  999. RowCluster::RowCluster()
  1000. : m_first( 0 ), m_autoDelete( false )
  1001. {
  1002. m_cluster = (RowFormat***)malloc( KSPREAD_CLUSTER_LEVEL1 * sizeof( RowFormat** ) );
  1003. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  1004. m_cluster[ x ] = 0;
  1005. }
  1006. RowCluster::~RowCluster()
  1007. {
  1008. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  1009. {
  1010. RowFormat** cl = m_cluster[ x ];
  1011. if ( cl )
  1012. {
  1013. free( cl );
  1014. m_cluster[ x ] = 0;
  1015. }
  1016. }
  1017. if ( m_autoDelete )
  1018. {
  1019. RowFormat* cell = m_first;
  1020. while( cell )
  1021. {
  1022. RowFormat* n = cell->next();
  1023. delete cell;
  1024. cell = n;
  1025. }
  1026. }
  1027. free( m_cluster );
  1028. }
  1029. const RowFormat* RowCluster::lookup( int row ) const
  1030. {
  1031. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  1032. {
  1033. kdDebug(36001) << "RowCluster::lookup: invalid row value (row: "
  1034. << row << ")" << endl;
  1035. return 0;
  1036. }
  1037. int cx = row / KSPREAD_CLUSTER_LEVEL2;
  1038. int dx = row % KSPREAD_CLUSTER_LEVEL2;
  1039. RowFormat** cl = m_cluster[ cx ];
  1040. if ( !cl )
  1041. return 0;
  1042. return cl[ dx ];
  1043. }
  1044. RowFormat* RowCluster::lookup( int row )
  1045. {
  1046. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  1047. {
  1048. kdDebug(36001) << "RowCluster::lookup: invalid row value (row: "
  1049. << row << ")" << endl;
  1050. return 0;
  1051. }
  1052. int cx = row / KSPREAD_CLUSTER_LEVEL2;
  1053. int dx = row % KSPREAD_CLUSTER_LEVEL2;
  1054. RowFormat** cl = m_cluster[ cx ];
  1055. if ( !cl )
  1056. return 0;
  1057. return cl[ dx ];
  1058. }
  1059. void RowCluster::clear()
  1060. {
  1061. for( int x = 0; x < KSPREAD_CLUSTER_LEVEL1; ++x )
  1062. {
  1063. RowFormat** cl = m_cluster[ x ];
  1064. if ( cl )
  1065. {
  1066. free( cl );
  1067. m_cluster[ x ] = 0;
  1068. }
  1069. }
  1070. if ( m_autoDelete )
  1071. {
  1072. RowFormat* cell = m_first;
  1073. while( cell )
  1074. {
  1075. RowFormat* n = cell->next();
  1076. delete cell;
  1077. cell = n;
  1078. }
  1079. }
  1080. m_first = 0;
  1081. }
  1082. void RowCluster::insertElement( RowFormat* lay, int row )
  1083. {
  1084. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  1085. {
  1086. kdDebug(36001) << "RowCluster::insertElement: invalid row value (row: "
  1087. << row << ")" << endl;
  1088. return;
  1089. }
  1090. int cx = row / KSPREAD_CLUSTER_LEVEL2;
  1091. int dx = row % KSPREAD_CLUSTER_LEVEL2;
  1092. RowFormat** cl = m_cluster[ cx ];
  1093. if ( !cl )
  1094. {
  1095. cl = (RowFormat**)malloc( KSPREAD_CLUSTER_LEVEL2 * sizeof( RowFormat* ) );
  1096. m_cluster[ cx ] = cl;
  1097. for( int a = 0; a < KSPREAD_CLUSTER_LEVEL2; ++a )
  1098. cl[ a ] = 0;
  1099. }
  1100. if ( cl[ dx ] )
  1101. removeElement( row );
  1102. cl[ dx ] = lay;
  1103. if ( m_first )
  1104. {
  1105. lay->setNext( m_first );
  1106. m_first->setPrevious( lay );
  1107. }
  1108. m_first = lay;
  1109. }
  1110. void RowCluster::removeElement( int row )
  1111. {
  1112. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  1113. {
  1114. kdDebug(36001) << "RowCluster::removeElement: invalid row value (row: "
  1115. << row << ")" << endl;
  1116. return;
  1117. }
  1118. int cx = row / KSPREAD_CLUSTER_LEVEL2;
  1119. int dx = row % KSPREAD_CLUSTER_LEVEL2;
  1120. RowFormat** cl = m_cluster[ cx ];
  1121. if ( !cl )
  1122. return;
  1123. RowFormat* c = cl[ dx ];
  1124. if ( !c )
  1125. return;
  1126. cl[ dx ] = 0;
  1127. if ( m_autoDelete )
  1128. {
  1129. if ( m_first == c )
  1130. m_first = c->next();
  1131. delete c;
  1132. }
  1133. else
  1134. {
  1135. if ( m_first == c )
  1136. m_first = c->next();
  1137. if ( c->previous() )
  1138. c->previous()->setNext( c->next() );
  1139. if ( c->next() )
  1140. c->next()->setPrevious( c->previous() );
  1141. c->setNext( 0 );
  1142. c->setPrevious( 0 );
  1143. }
  1144. }
  1145. bool RowCluster::insertRow( int row )
  1146. {
  1147. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  1148. {
  1149. kdDebug(36001) << "RowCluster::insertRow: invalid row value (row: "
  1150. << row << ")" << endl;
  1151. return false;
  1152. }
  1153. int cx = row / KSPREAD_CLUSTER_LEVEL2;
  1154. int dx = row % KSPREAD_CLUSTER_LEVEL2;
  1155. // Is there a row layout at the bottom most position ?
  1156. // In this case the shift is impossible.
  1157. RowFormat** cl = m_cluster[ KSPREAD_CLUSTER_LEVEL1 - 1 ];
  1158. if ( cl && cl[ KSPREAD_CLUSTER_LEVEL2 - 1 ] )
  1159. return false;
  1160. bool a = autoDelete();
  1161. setAutoDelete( false );
  1162. for( int i = KSPREAD_CLUSTER_LEVEL1 - 1; i >= cx ; --i )
  1163. {
  1164. RowFormat** cl = m_cluster[ i ];
  1165. if ( cl )
  1166. {
  1167. int left = 0;
  1168. if ( i == cx )
  1169. left = dx;
  1170. int right = KSPREAD_CLUSTER_LEVEL2 - 1;
  1171. if ( i == KSPREAD_CLUSTER_LEVEL1 - 1 )
  1172. right = KSPREAD_CLUSTER_LEVEL2 - 2;
  1173. for( int k = right; k >= left; --k )
  1174. {
  1175. RowFormat* c = cl[ k ];
  1176. if ( c )
  1177. {
  1178. removeElement( c->row() );
  1179. c->setRow( c->row() + 1 );
  1180. insertElement( c, c->row() );
  1181. }
  1182. }
  1183. }
  1184. }
  1185. setAutoDelete( a );
  1186. return true;
  1187. }
  1188. bool RowCluster::removeRow( int row )
  1189. {
  1190. if ( row >= KSPREAD_CLUSTER_MAX || row < 0 )
  1191. {
  1192. kdDebug(36001) << "RowCluster::removeRow: invalid row value (row: "
  1193. << row << ")" << endl;
  1194. return false;
  1195. }
  1196. int cx = row / KSPREAD_CLUSTER_LEVEL2;
  1197. int dx = row % KSPREAD_CLUSTER_LEVEL2;
  1198. removeElement( row );
  1199. bool a = autoDelete();
  1200. setAutoDelete( false );
  1201. for( int i = cx; i < KSPREAD_CLUSTER_LEVEL1; ++i )
  1202. {
  1203. RowFormat** cl = m_cluster[ i ];
  1204. if ( cl )
  1205. {
  1206. int left = 0;
  1207. if ( i == cx )
  1208. left = dx + 1;
  1209. int right = KSPREAD_CLUSTER_LEVEL2 - 1;
  1210. for( int k = left; k <= right; ++k )
  1211. {
  1212. RowFormat* c = cl[ k ];
  1213. if ( c )
  1214. {
  1215. removeElement( c->row() );
  1216. c->setRow( c->row() - 1 );
  1217. insertElement( c, c->row() );
  1218. }
  1219. }
  1220. }
  1221. }
  1222. setAutoDelete( a );
  1223. return true;
  1224. }
  1225. void RowCluster::setAutoDelete( bool a )
  1226. {
  1227. m_autoDelete = a;
  1228. }
  1229. bool RowCluster::autoDelete() const
  1230. {
  1231. return m_autoDelete;
  1232. }