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.

1622 lines
52KB

  1. /* This file is part of the KDE project
  2. Copyright (C) 2006 Fredrik Edemar <f_edemar@linux.se>
  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 "kspread_object.h"
  17. #include "kspread_canvas.h"
  18. #include "kspread_doc.h"
  19. #include "kspread_sheet.h"
  20. #include "kspread_view.h"
  21. #include <assert.h>
  22. #include <tdeapplication.h>
  23. #include <kdebug.h>
  24. #include <kimageeffect.h>
  25. #include <tdeparts/partmanager.h>
  26. #include <koChart.h>
  27. #include <tqbitmap.h>
  28. #include <tqbuffer.h>
  29. #include <tqcursor.h>
  30. #include <tqdom.h>
  31. #include <tqfileinfo.h>
  32. #include <tqimage.h>
  33. #include <tqpainter.h>
  34. #include <tqpixmap.h>
  35. #include <tqwmatrix.h>
  36. #include <KoDocument.h>
  37. #include <KoDocumentChild.h>
  38. #include <KoDom.h>
  39. #include <KoXmlWriter.h>
  40. #include <KoZoomHandler.h>
  41. using namespace KSpread;
  42. class Sheet;
  43. class View;
  44. /**********************************************************
  45. *
  46. * EmbeddedObject
  47. *
  48. **********************************************************/
  49. EmbeddedObject::EmbeddedObject( Sheet *_sheet, const KoRect& _geometry )
  50. : m_geometry( _geometry), m_sheet(_sheet), m_objectName(""), m_selected(false), m_protect(false), m_keepRatio(false), pen( TQt::black, 1, Qt::SolidLine )
  51. {
  52. angle = 0.0;
  53. inObjList = true;
  54. cmds = 0;
  55. }
  56. EmbeddedObject::~EmbeddedObject()
  57. {
  58. }
  59. KoRect EmbeddedObject::geometry()
  60. {
  61. return m_geometry;
  62. }
  63. void EmbeddedObject::setGeometry( const KoRect &rect )
  64. {
  65. m_geometry = rect;
  66. }
  67. void EmbeddedObject::moveBy( const KoPoint &_point )
  68. {
  69. m_geometry.moveTopLeft( m_geometry.topLeft() + _point );
  70. }
  71. void EmbeddedObject::moveBy( double _dx, double _dy )
  72. {
  73. m_geometry.moveTopLeft( m_geometry.topLeft() + KoPoint( _dx, _dy ) );
  74. }
  75. void EmbeddedObject::resizeBy( const KoSize & _size )
  76. {
  77. resizeBy( _size.width(), _size.height() );
  78. }
  79. void EmbeddedObject::resizeBy( double _dx, double _dy)
  80. {
  81. m_geometry.setSize( KoSize( m_geometry.width()+_dx, m_geometry.height()+_dy) );
  82. } // call (possibly reimplemented) setSize
  83. bool EmbeddedObject::load( const TQDomElement& /*element*/ )
  84. {
  85. kdDebug() << "Loading EmbeddedObject" << endl;
  86. return false;
  87. }
  88. void EmbeddedObject::loadOasis(const TQDomElement &element, KoOasisLoadingContext & context )
  89. {
  90. if(element.hasAttributeNS( KoXmlNS::draw, "name" ))
  91. m_objectName = element.attributeNS( KoXmlNS::draw, "name", TQString());
  92. m_geometry.setX( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "x", TQString() ) ) );
  93. m_geometry.setY( KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "y", TQString() ) ) );
  94. m_geometry.setWidth(KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "width", TQString() )) );
  95. m_geometry.setHeight(KoUnit::parseValue( element.attributeNS( KoXmlNS::svg, "height", TQString() ) ) );
  96. //kdDebug()<<" orig.x() :"<<orig.x() <<" orig.y() :"<<orig.y() <<"ext.width() :"<<ext.width()<<" ext.height(): "<<ext.height()<<endl;
  97. KoStyleStack &styleStack = context.styleStack();
  98. styleStack.setTypeProperties( "" ); //no type default type
  99. }
  100. TQDomElement EmbeddedObject::save( TQDomDocument& /*doc*/ )
  101. {
  102. kdDebug() << "Saving EmbeddedObject..." << endl;
  103. return TQDomElement();
  104. }
  105. void EmbeddedObject::saveOasisPosObject( KoXmlWriter &xmlWriter, int indexObj ) const
  106. {
  107. xmlWriter.addAttribute( "draw:id", "object" + TQString::number( indexObj ) );
  108. //save all into pt
  109. xmlWriter.addAttributePt( "svg:x", sheet()->doc()->savingWholeDocument() ? m_geometry.x() : m_geometry.x() + 20.0 );
  110. xmlWriter.addAttributePt( "svg:y", sheet()->doc()->savingWholeDocument() ? m_geometry.y() : m_geometry.y() + 20.0 );
  111. xmlWriter.addAttributePt( "svg:width", m_geometry.width() );
  112. xmlWriter.addAttributePt( "svg:height", m_geometry.height() );
  113. // if ( kAbs( angle ) > 1E-6 )
  114. // {
  115. // double value = -1 * ( ( double )angle* M_PI )/180.0;
  116. // TQString str=TQString( "rotate (%1)" ).arg( value );
  117. // xmlWriter.addAttribute( "draw:transform", str );
  118. // }
  119. }
  120. bool EmbeddedObject::saveOasisObjectAttributes( KSpreadOasisSaveContext &/* sc */ ) const
  121. {
  122. kdDebug() << "bool saveOasisObjectAttributes not implemented";
  123. return true;
  124. }
  125. bool EmbeddedObject::saveOasisObject( KSpreadOasisSaveContext &sc ) const
  126. {
  127. sc.xmlWriter.startElement( getOasisElementName() );
  128. //sc.xmlWriter.addAttribute( "draw:style-name", getStyle( sc ) );
  129. saveOasisPosObject( sc.xmlWriter, sc.indexObj );
  130. if( !getObjectName().isEmpty())
  131. sc.xmlWriter.addAttribute( "draw:name", getObjectName() );
  132. saveOasisObjectAttributes( sc );
  133. sc.xmlWriter.endElement();
  134. return true;
  135. }
  136. void EmbeddedObject::draw( TQPainter *_painter )
  137. {
  138. paintSelection(_painter, SM_MOVERESIZE );
  139. }
  140. TQPixmap EmbeddedObject::toPixmap()
  141. {
  142. return toPixmap( 1.0 , 1.0 );
  143. }
  144. TQPixmap EmbeddedObject::toPixmap(TQSize size)
  145. {
  146. double xZoom;
  147. double yZoom;
  148. calculateRequiredZoom( size , xZoom , yZoom );
  149. return toPixmap(xZoom,yZoom);
  150. }
  151. TQPixmap EmbeddedObject::toPixmap(double /*xZoom*/ , double /*yZoom*/)
  152. {
  153. return TQPixmap();
  154. }
  155. void EmbeddedObject::calculateRequiredZoom( TQSize desiredSize, double& xZoom, double& yZoom)
  156. {
  157. TQSize actualSize = geometry().size().toTQSize();
  158. xZoom = (double) desiredSize.width() / (double)actualSize.width();
  159. yZoom = (double) desiredSize.height() / (double)actualSize.height();
  160. }
  161. void EmbeddedObject::paintSelection( TQPainter *_painter, SelectionMode mode )
  162. {
  163. if ( !m_selected || mode == SM_NONE )
  164. return;
  165. _painter->save();
  166. KoRect bound( geometry().left(), geometry().top(),
  167. geometry().width() , geometry().height() );
  168. TQRect zoomedBound = sheet()->doc()->zoomRect( bound ) ;
  169. //_painter->setPen( TQPen( TQt::black, 1, TQPen::SolidLine ) );
  170. _painter->setPen( pen );
  171. _painter->setBrush( kapp->palette().color( TQPalette::Active, TQColorGroup::Highlight ) );
  172. //KoRect r = rotateRectObject(); // TODO: rotation
  173. KoRect r = /*KoRect::fromTQRect*/( bound );
  174. int x = sheet()->doc()->zoomItX( r.left() /*- orig.x()*/);
  175. int y = sheet()->doc()->zoomItY( r.top() /*- orig.y()*/);
  176. int zX6 = /*sheet()->doc()->zoomItX*/( 6 );
  177. int zY6 = /*sheet()->doc()->zoomItY*/( 6 );
  178. int w = sheet()->doc()->zoomItX(r.width()) - 6;
  179. int h = sheet()->doc()->zoomItY(r.height()) - 6;
  180. if ( mode == SM_MOVERESIZE ) {
  181. _painter->drawRect( x, y, zX6, zY6 );
  182. _painter->drawRect( x, y + h / 2, zX6, zY6 );
  183. _painter->drawRect( x, y + h, zX6, zY6 );
  184. _painter->drawRect( x + w, y, zX6, zY6 );
  185. _painter->drawRect( x + w, y + h / 2, zX6, zY6 );
  186. _painter->drawRect( x + w, y + h, zX6, zY6 );
  187. _painter->drawRect( x + w / 2, y,zX6, zY6 );
  188. _painter->drawRect( x + w / 2, y + h, zX6, zY6 );
  189. }
  190. else if ( mode == SM_PROTECT) {
  191. _painter->drawRect( x, y, zX6, zY6 );
  192. _painter->drawRect( x, y + h / 2, zX6, zY6 );
  193. _painter->drawRect( x, y + h, zX6, zY6 );
  194. _painter->drawRect( x + w, y, zX6, zY6 );
  195. _painter->drawRect( x + w, y + h / 2, zX6, zY6 );
  196. _painter->drawRect( x + w, y + h, zX6, zY6 );
  197. _painter->drawRect( x + w / 2, y,zX6, zY6 );
  198. _painter->drawRect( x + w / 2, y + h, zX6, zY6 );
  199. x= x + 1;
  200. y= y + 1;
  201. zX6=zX6-2;
  202. zY6=zY6-2;
  203. TQBrush brush=kapp->palette().color( TQPalette::Active,TQColorGroup::Base );
  204. _painter->fillRect( x, y, zX6, zY6, brush );
  205. _painter->fillRect( x, y + h / 2, zX6, zY6, brush);
  206. _painter->fillRect( x, y + h, zX6, zY6, brush );
  207. _painter->fillRect( x + w, y, zX6, zY6, brush );
  208. _painter->fillRect( x + w, y + h / 2, zX6, zY6, brush );
  209. _painter->fillRect( x + w , y + h , zX6 , zY6 , brush );
  210. _painter->fillRect( x + w / 2 , y ,zX6 , zY6 , brush );
  211. _painter->fillRect( x + w / 2, y + h , zX6 , zY6 , brush );
  212. }
  213. else if ( mode == SM_ROTATE ) {
  214. _painter->drawEllipse( x, y, zX6, zY6 );
  215. _painter->drawEllipse( x, y + h, zX6, zY6 );
  216. _painter->drawEllipse( x + w, y, zX6, zY6 );
  217. _painter->drawEllipse( x + w, y + h, zX6, zY6 );
  218. }
  219. _painter->restore();
  220. }
  221. TQCursor EmbeddedObject::getCursor( const TQPoint &_point, ModifyType &_modType, TQRect &geometry) const
  222. {
  223. int px = /*sheet()->doc()->zoomItX*/(_point.x());
  224. int py = /*sheet()->doc()->zoomItY*/(_point.y());
  225. int ox = /*sheet()->doc()->zoomItX*/(/*orig*/geometry.x());
  226. int oy = /*sheet()->doc()->zoomItY*/(/*orig*/geometry.y());
  227. int ow = /*sheet()->doc()->zoomItX*/(/*ext*/geometry.width());
  228. int oh = /*sheet()->doc()->zoomItY*/(/*ext*/geometry.height());
  229. // if ( angle != 0.0 )
  230. // {
  231. // TQRect rr = sheet()->doc()->zoomRect( rotateRectObject() );
  232. // ox = rr.x();
  233. // oy = rr.y();
  234. // ow = rr.width();
  235. // oh = rr.height();
  236. // }
  237. int sz = 4;
  238. if ( px >= ox && py >= oy && px <= ox + TQMIN( ow / 3, sz ) && py <= oy + TQMIN( oh / 3, sz ) )
  239. {
  240. _modType = MT_RESIZE_LU;
  241. if ( m_protect )
  242. return TQt::ForbiddenCursor;
  243. return TQt::sizeFDiagCursor;
  244. }
  245. if ( px >= ox && py >= oy + oh / 2 - TQMIN( oh / 6, sz / 2 )
  246. && px <= ox + TQMIN( ow / 3, sz)
  247. && py <= oy + oh / 2 + TQMIN( oh / 6, sz / 2 ) )
  248. {
  249. _modType = MT_RESIZE_LF;
  250. if ( m_protect)
  251. return TQt::ForbiddenCursor;
  252. return TQt::sizeHorCursor;
  253. }
  254. if ( px >= ox && py >= oy + oh - TQMIN( oh / 3, sz ) && px <= ox + TQMIN( ow / 3, sz ) && py <= oy + oh )
  255. {
  256. _modType = MT_RESIZE_LD;
  257. if ( m_protect )
  258. return TQt::ForbiddenCursor;
  259. return TQt::sizeBDiagCursor;
  260. }
  261. if ( px >= ox + ow / 2 - TQMIN( ow / 6, sz / 2 ) && py >= oy
  262. && px <= ox + ow / 2 + TQMIN( ow / 6, sz / 2 )
  263. && py <= oy + TQMIN( oh / 3, sz ) )
  264. {
  265. _modType = MT_RESIZE_UP;
  266. if ( m_protect )
  267. return TQt::ForbiddenCursor;
  268. return TQt::sizeVerCursor;
  269. }
  270. if ( px >= ox + ow / 2 - TQMIN( ow / 6, sz / 2 ) && py >= oy + oh - TQMIN( oh / 3, sz )
  271. && px <= ox + ow / 2 + TQMIN( ow / 6, sz / 2 ) && py <= oy + oh )
  272. {
  273. _modType = MT_RESIZE_DN;
  274. if ( m_protect )
  275. return TQt::ForbiddenCursor;
  276. return TQt::sizeVerCursor;
  277. }
  278. if ( px >= ox + ow - TQMIN( ow / 3, sz ) && py >= oy && px <= ox + ow && py <= oy + TQMIN( oh / 3, sz) )
  279. {
  280. _modType = MT_RESIZE_RU;
  281. if ( m_protect )
  282. return TQt::ForbiddenCursor;
  283. return TQt::sizeBDiagCursor;
  284. }
  285. if ( px >= ox + ow - TQMIN( ow / 3, sz ) && py >= oy + oh / 2 - TQMIN( oh / 6, sz / 2 )
  286. && px <= ox + ow && py <= oy + oh / 2 + TQMIN( oh / 6, sz / 2) )
  287. {
  288. _modType = MT_RESIZE_RT;
  289. if ( m_protect )
  290. return TQt::ForbiddenCursor;
  291. return TQt::sizeHorCursor;
  292. }
  293. if ( px >= ox + ow - TQMIN( ow / 3, sz ) && py >= oy + oh - TQMIN( oh / 3, sz)
  294. && px <= ox + ow && py <= oy + oh )
  295. {
  296. _modType = MT_RESIZE_RD;
  297. if ( m_protect )
  298. return TQt::ForbiddenCursor;
  299. return TQt::sizeFDiagCursor;
  300. }
  301. _modType = MT_MOVE;
  302. return TQt::sizeAllCursor;
  303. }
  304. void EmbeddedObject::doDelete()
  305. {
  306. if ( cmds == 0 && !inObjList )
  307. delete this;
  308. }
  309. /**********************************************************
  310. *
  311. * EmbeddedKOfficeObject
  312. *
  313. **********************************************************/
  314. EmbeddedKOfficeObject::EmbeddedKOfficeObject( Doc *parent, Sheet *_sheet, KoDocument* doc, const KoRect& geometry )
  315. : EmbeddedObject( _sheet, geometry ), m_parent(parent)
  316. {
  317. m_embeddedObject = new KoDocumentChild(parent, doc, geometry.toTQRect() );
  318. }
  319. EmbeddedKOfficeObject::EmbeddedKOfficeObject( Doc *parent, Sheet *_sheet )
  320. : EmbeddedObject( _sheet, KoRect() ), m_parent(parent)
  321. {
  322. m_embeddedObject = new KoDocumentChild( parent );
  323. }
  324. EmbeddedKOfficeObject::~EmbeddedKOfficeObject()
  325. {
  326. delete m_embeddedObject;
  327. }
  328. Doc* EmbeddedKOfficeObject::parent()
  329. {
  330. return m_parent;
  331. }
  332. KoDocumentChild* EmbeddedKOfficeObject::embeddedObject()
  333. {
  334. return m_embeddedObject;
  335. }
  336. bool EmbeddedKOfficeObject::saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const
  337. {
  338. kdDebug() << "EmbeddedKOfficeObject::saveOasisPart " << sc.partIndexObj << endl;
  339. sc.xmlWriter.startElement( "draw:object" );
  340. const TQString name = TQString( "Object_%1" ).arg( sc.partIndexObj + 1 );
  341. ++sc.partIndexObj;
  342. m_embeddedObject->saveOasisAttributes( sc.xmlWriter, name );
  343. if ( getType() != OBJECT_CHART )
  344. sc.xmlWriter.endElement();
  345. return true;
  346. }
  347. const char * EmbeddedKOfficeObject::getOasisElementName() const
  348. {
  349. return "draw:frame";
  350. }
  351. bool EmbeddedKOfficeObject::load( const TQDomElement& element )
  352. {
  353. kdDebug() << "Loading EmbeddedKOfficeObject" << endl;
  354. bool result = embeddedObject()->load( element );
  355. setGeometry( KoRect::fromTQRect( embeddedObject()->geometry() ) );
  356. return result;
  357. }
  358. void EmbeddedKOfficeObject::loadOasis(const TQDomElement &element, KoOasisLoadingContext &context/*, KPRLoadingInfo *info*/)
  359. {
  360. kdDebug()<<"void EmbeddedKOfficeObject::loadOasis(const TQDomElement &element)******************\n";
  361. EmbeddedObject::loadOasis( element, context );
  362. TQDomElement objectElement = KoDom::namedItemNS( element, KoXmlNS::draw, "object" );
  363. m_embeddedObject->loadOasis( element, objectElement );
  364. if( element.hasAttributeNS( KoXmlNS::draw, "name" ) )
  365. m_objectName = element.attributeNS( KoXmlNS::draw, "name", TQString());
  366. (void)m_embeddedObject->loadOasisDocument( context.store(), context.manifestDocument() );
  367. }
  368. TQDomElement EmbeddedKOfficeObject::save( TQDomDocument& doc )
  369. {
  370. kdDebug() << "Saving EmbeddedKOfficeObject" << endl;
  371. embeddedObject()->setGeometry( geometry().toTQRect() );
  372. return m_embeddedObject->save( doc );
  373. }
  374. void EmbeddedKOfficeObject::draw( TQPainter *_painter )
  375. {
  376. kdDebug() << "Painting..." << endl;
  377. int const penw = pen.width() ;
  378. KoRect bound( 0, 0,
  379. geometry().width() - ( 2 * penw ), geometry().height() - ( 2 * penw ) );
  380. TQRect const zoomedBound = sheet()->doc()->zoomRect( bound );
  381. _painter->save();
  382. int const xOffset = sheet()->doc()->zoomItX( geometry().left() + penw);
  383. int const yOffset = sheet()->doc()->zoomItY( geometry().top() + penw );
  384. TQRect new_geometry = zoomedBound;
  385. //if ( translate )
  386. //{
  387. _painter->translate( xOffset , yOffset );
  388. new_geometry.moveBy( xOffset , yOffset );
  389. new_geometry.moveBy( -_painter->window().x(), -_painter->window().y() );
  390. //}
  391. _painter->setClipRect( zoomedBound, TQPainter::CoordPainter );
  392. assert( embeddedObject()->document() != 0 );
  393. double zoomX = static_cast<double>( sheet()->doc()->zoom() ) / 100;
  394. double zoomY = static_cast<double>( sheet()->doc()->zoom() ) / 100;
  395. embeddedObject()->document()->paintEverything( *_painter,
  396. zoomedBound,
  397. embeddedObject()->isTransparent(),
  398. 0 /* View isn't known from here - is that a problem? */,
  399. zoomX,
  400. zoomY );
  401. embeddedObject()->setGeometry( new_geometry );
  402. _painter->restore();
  403. EmbeddedObject::draw( _painter );
  404. }
  405. TQPixmap EmbeddedKOfficeObject::toPixmap( double xZoom , double yZoom )
  406. {
  407. TQPixmap pixmap( (int)( geometry().width()*xZoom ), (int)( geometry().height()*yZoom ) );
  408. TQPainter painter(&pixmap);
  409. TQRect bound( 0,0,(int)( geometry().width()*xZoom ), (int)(geometry().height()*yZoom) );
  410. embeddedObject()->document()->paintEverything(painter,bound,
  411. embeddedObject()->isTransparent(),
  412. 0,
  413. xZoom,
  414. yZoom);
  415. return pixmap;
  416. }
  417. void EmbeddedKOfficeObject::activate( View *_view, Canvas* /* canvas */ )
  418. {
  419. KoDocument* part = embeddedObject()->document();
  420. if ( !part )
  421. return;
  422. kdDebug() << "Activating..." << endl;
  423. _view->partManager()->addPart( part, false );
  424. _view->partManager()->setActivePart( part, _view );
  425. }
  426. void EmbeddedKOfficeObject::deactivate()
  427. {
  428. }
  429. void EmbeddedKOfficeObject::updateChildGeometry()
  430. {
  431. // KoZoomHandler* zh = m_sheet->doc();
  432. // embeddedObject()->setGeometry( zh->zoomRect( geometry() ), true );
  433. // return;
  434. // TQRect r = sheet()->doc()->zoomRect( geometry() );
  435. // if ( _canvas )
  436. // {
  437. // kdDebug() << "_canvas->xOffset():" << _canvas->xOffset() << endl;
  438. // kdDebug() << " _canvas->xOffset()*_canvas->doc()->zoomedResolutionX():" << _canvas->xOffset() * _canvas->doc()->zoomedResolutionX() << endl;
  439. // kdDebug() << "_canvas->yOffset():" << _canvas->yOffset() << endl;
  440. // kdDebug() << " _canvas->yOffset()*_canvas->doc()->zoomedResolutionY():" << _canvas->yOffset() * _canvas->doc()->zoomedResolutionY() << endl;
  441. // r.moveBy( -_canvas->xOffset() / _canvas->doc()->zoomedResolutionX() , -_canvas->yOffset() / _canvas->doc()->zoomedResolutionY() );
  442. // }
  443. // embeddedObject()->setGeometry( r , true );
  444. }
  445. /**********************************************************
  446. *
  447. * EmbeddedChart
  448. *
  449. **********************************************************/
  450. EmbeddedChart::EmbeddedChart( Doc *_spread, Sheet *_sheet, KoDocument* doc, const KoRect& geometry )
  451. : EmbeddedKOfficeObject( _spread, _sheet, doc, geometry )
  452. {
  453. m_pBinding = 0;
  454. }
  455. EmbeddedChart::EmbeddedChart( Doc *_spread, Sheet *_sheet )
  456. : EmbeddedKOfficeObject( _spread, _sheet )
  457. {
  458. m_pBinding = 0;
  459. }
  460. EmbeddedChart::~EmbeddedChart()
  461. {
  462. if ( m_embeddedObject->isDeleted() )
  463. delete m_pBinding;
  464. }
  465. void EmbeddedChart::setDataArea( const TQRect& _data )
  466. {
  467. if ( m_pBinding == 0L )
  468. m_pBinding = new ChartBinding( m_sheet, _data, this );
  469. else
  470. m_pBinding->setDataArea( _data );
  471. }
  472. void EmbeddedChart::update()
  473. {
  474. if ( m_pBinding )
  475. m_pBinding->cellChanged( 0 );
  476. }
  477. const char * EmbeddedChart::getOasisElementName() const
  478. {
  479. return "draw:frame";
  480. }
  481. bool EmbeddedChart::load( const TQDomElement& element )
  482. {
  483. kdDebug() << "Loading EmbeddedChart" << endl;
  484. if ( !EmbeddedKOfficeObject::load( element ) )
  485. return false;
  486. if ( element.hasAttribute( "left-cell" ) &&
  487. element.hasAttribute( "top-cell" ) &&
  488. element.hasAttribute( "right-cell" ) &&
  489. element.hasAttribute( "bottom-cell" ) )
  490. {
  491. TQRect r;
  492. r.setCoords( element.attribute( "left-cell" ).toInt(),
  493. element.attribute( "top-cell" ).toInt(),
  494. element.attribute( "right-cell" ).toInt(),
  495. element.attribute( "bottom-cell" ).toInt() );
  496. setDataArea( r );
  497. }
  498. return true;
  499. }
  500. void EmbeddedChart::loadOasis(const TQDomElement &element, KoOasisLoadingContext &context/*, KPRLoadingInfo *info*/)
  501. {
  502. kdDebug()<<"void EmbeddedChart::loadOasis(const TQDomElement &element)******************\n";
  503. EmbeddedKOfficeObject::loadOasis( element, context );
  504. TQDomElement objectElement = KoDom::namedItemNS( element, KoXmlNS::draw, "object" );
  505. TQString str_range = objectElement.attributeNS( KoXmlNS::draw, "notify-on-update-of-ranges", TQString());
  506. if ( !str_range.isNull() )
  507. {
  508. str_range = Oasis::decodeFormula( str_range );
  509. Range range( str_range );
  510. if ( range.isValid() )
  511. setDataArea( range.range() );
  512. }
  513. KoChart::Part* chartPart = chart();
  514. if ( chartPart )
  515. chartPart->setCanChangeValue( false );
  516. }
  517. bool EmbeddedChart::saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const
  518. {
  519. kdDebug() << "EmbeddedChart::saveOasisPart " << sc.partIndexObj << endl;
  520. EmbeddedKOfficeObject::saveOasisObjectAttributes( sc );
  521. if(m_pBinding) { // see http://bugs.kde.org/show_bug.cgi?id=120395
  522. TQRect dataArea = m_pBinding->dataArea();
  523. TQString rangeName = util_rangeName( dataArea);
  524. rangeName.insert( rangeName.find(':') +1, sheet()->sheetName() + "." );
  525. rangeName.prepend( sheet()->sheetName() + "." );
  526. sc.xmlWriter.addAttribute( "draw:notify-on-update-of-ranges", rangeName );
  527. }
  528. else {
  529. kdDebug() << "EmbeddedChart::saveOasisPart m_pBinding is NULL" << endl;
  530. }
  531. sc.xmlWriter.endElement();
  532. return true;
  533. }
  534. TQDomElement EmbeddedChart::save( TQDomDocument& doc )
  535. {
  536. kdDebug() << "Saving EmbeddedChart" << endl;
  537. TQDomElement element = EmbeddedKOfficeObject::save( doc );
  538. element.setTagName( "chart" );
  539. element.setAttribute( "left-cell", m_pBinding->dataArea().left() );
  540. element.setAttribute( "right-cell", m_pBinding->dataArea().right() );
  541. element.setAttribute( "top-cell", m_pBinding->dataArea().top() );
  542. element.setAttribute( "bottom-cell", m_pBinding->dataArea().bottom() );
  543. return element;
  544. }
  545. void EmbeddedChart::draw( TQPainter *_painter )
  546. {
  547. EmbeddedKOfficeObject::draw( _painter );
  548. }
  549. bool EmbeddedChart::loadDocument( KoStore* _store )
  550. {
  551. bool res = /*EmbeddedKOfficeObject::*/m_embeddedObject->loadDocument( _store );
  552. if ( !res )
  553. return res;
  554. // Did we see a cell rectangle ?
  555. if ( !m_pBinding )
  556. return true;
  557. update();
  558. KoChart::Part* chartPart = chart();
  559. if ( chartPart )
  560. chartPart->setCanChangeValue( false );
  561. return true;
  562. }
  563. KoChart::Part* EmbeddedChart::chart()
  564. {
  565. // Returns 0 when the chart couldn't be loaded and we get KoUnavailPart instead.
  566. return ::tqqt_cast<KoChart::Part *>( m_embeddedObject->document() );
  567. }
  568. /**********************************************************
  569. *
  570. * EmbeddedPictureObject
  571. *
  572. **********************************************************/
  573. EmbeddedPictureObject::EmbeddedPictureObject( Sheet *_sheet, const KoRect& _geometry, KoPictureCollection *_imageCollection )
  574. : EmbeddedObject( _sheet, _geometry )
  575. {
  576. imageCollection = _imageCollection;
  577. pen = KoPen( TQt::black, 1.0, Qt::NoPen );
  578. mirrorType = PM_NORMAL;
  579. depth = 0;
  580. swapRGB = false;
  581. grayscal = false;
  582. bright = 0;
  583. m_effect = IE_NONE;
  584. m_ie_par1 = TQVariant();
  585. m_ie_par2 = TQVariant();
  586. m_ie_par3 = TQVariant();
  587. // Forbid TQPixmap to cache the X-Window resources (Yes, it is slower!)
  588. m_cachedPixmap.setOptimization(TQPixmap::MemoryOptim);
  589. }
  590. EmbeddedPictureObject::EmbeddedPictureObject( Sheet *_sheet, const KoRect& _geometry, KoPictureCollection *_imageCollection, const KoPictureKey & key )
  591. : EmbeddedObject( _sheet, _geometry )
  592. {
  593. imageCollection = _imageCollection;
  594. //ext = KoSize(); // invalid size means unset
  595. pen = KoPen( TQt::black, 1.0, Qt::NoPen );
  596. mirrorType = PM_NORMAL;
  597. depth = 0;
  598. swapRGB = false;
  599. grayscal = false;
  600. bright = 0;
  601. m_effect = IE_NONE;
  602. m_ie_par1 = TQVariant();
  603. m_ie_par2 = TQVariant();
  604. m_ie_par3 = TQVariant();
  605. // Forbid TQPixmap to cache the X-Window resources (Yes, it is slower!)
  606. m_cachedPixmap.setOptimization(TQPixmap::MemoryOptim);
  607. setPicture( key );
  608. }
  609. EmbeddedPictureObject::EmbeddedPictureObject( Sheet *_sheet, KoPictureCollection *_imageCollection )
  610. : EmbeddedObject( _sheet, KoRect(0,0,0,0) )
  611. {
  612. imageCollection = _imageCollection;
  613. //ext = KoSize(); // invalid size means unset
  614. pen = KoPen( TQt::black, 1.0, Qt::NoPen );
  615. mirrorType = PM_NORMAL;
  616. depth = 0;
  617. swapRGB = false;
  618. grayscal = false;
  619. bright = 0;
  620. m_effect = IE_NONE;
  621. m_ie_par1 = TQVariant();
  622. m_ie_par2 = TQVariant();
  623. m_ie_par3 = TQVariant();
  624. // Forbid TQPixmap to cache the X-Window resources (Yes, it is slower!)
  625. m_cachedPixmap.setOptimization(TQPixmap::MemoryOptim);
  626. }
  627. EmbeddedPictureObject::~EmbeddedPictureObject()
  628. {
  629. }
  630. bool EmbeddedPictureObject::load( const TQDomElement& /*element*/ )
  631. {
  632. return false;
  633. }
  634. TQDomElement EmbeddedPictureObject::save( TQDomDocument& /*doc*/ )
  635. {
  636. kdDebug() << "Saving EmbeddedPictureObject" << endl;
  637. return TQDomElement();
  638. }
  639. TQString EmbeddedPictureObject::convertValueToPercent( int val ) const
  640. {
  641. return TQString::number( val )+"%";
  642. }
  643. void EmbeddedPictureObject::saveOasisPictureElement( KoGenStyle &styleobjectauto ) const
  644. {
  645. if ( bright != 0 )
  646. {
  647. styleobjectauto.addProperty( "draw:luminance", convertValueToPercent( bright ) );
  648. }
  649. if ( grayscal )
  650. {
  651. styleobjectauto.addProperty( "draw:color-mode","greyscale" );
  652. }
  653. switch (m_effect)
  654. {
  655. case IE_NONE:
  656. //nothing
  657. break;
  658. case IE_CHANNEL_INTENSITY:
  659. {
  660. //for the moment kpresenter support just one channel
  661. TQString percent = convertValueToPercent( m_ie_par1.toInt() );
  662. KImageEffect::RGBComponent channel = static_cast<KImageEffect::RGBComponent>( m_ie_par2.toInt() );
  663. switch( channel )
  664. {
  665. case KImageEffect::Red:
  666. styleobjectauto.addProperty( "draw:red", percent );
  667. styleobjectauto.addProperty( "draw:blue", "0%" );
  668. styleobjectauto.addProperty( "draw:green", "0%" );
  669. break;
  670. case KImageEffect::Green:
  671. styleobjectauto.addProperty( "draw:green", percent );
  672. styleobjectauto.addProperty( "draw:red", "0%" );
  673. styleobjectauto.addProperty( "draw:blue", "0%" );
  674. break;
  675. case KImageEffect::Blue:
  676. styleobjectauto.addProperty( "draw:blue", percent );
  677. styleobjectauto.addProperty( "draw:red", "0%" );
  678. styleobjectauto.addProperty( "draw:green", "0%" );
  679. break;
  680. case KImageEffect::Gray:
  681. break;
  682. case KImageEffect::All:
  683. break;
  684. }
  685. }
  686. break;
  687. case IE_FADE:
  688. break;
  689. case IE_FLATTEN:
  690. break;
  691. case IE_INTENSITY:
  692. break;
  693. case IE_DESATURATE:
  694. break;
  695. case IE_CONTRAST:
  696. {
  697. //kpresenter use value between -255 and 255
  698. //oo impress between -100% and 100%
  699. int val = m_ie_par1.toInt();
  700. val = ( int )( ( double )val*100.0/255.0 );
  701. styleobjectauto.addProperty( "draw:contrast", convertValueToPercent( val ) );
  702. }
  703. break;
  704. case IE_NORMALIZE:
  705. break;
  706. case IE_EQUALIZE:
  707. break;
  708. case IE_THRESHOLD:
  709. break;
  710. case IE_SOLARIZE:
  711. break;
  712. case IE_EMBOSS:
  713. break;
  714. case IE_DESPECKLE:
  715. break;
  716. case IE_CHARCOAL:
  717. break;
  718. case IE_NOISE:
  719. break;
  720. case IE_BLUR:
  721. break;
  722. case IE_EDGE:
  723. break;
  724. case IE_IMPLODE:
  725. break;
  726. case IE_OIL_PAINT:
  727. break;
  728. case IE_SHARPEN:
  729. break;
  730. case IE_SPREAD:
  731. break;
  732. case IE_SHADE:
  733. break;
  734. case IE_SWIRL:
  735. break;
  736. case IE_WAVE:
  737. break;
  738. }
  739. }
  740. bool EmbeddedPictureObject::saveOasisObjectAttributes( KSpreadOasisSaveContext &sc ) const
  741. {
  742. sc.xmlWriter.startElement( "draw:image" );
  743. sc.xmlWriter.addAttribute( "xlink:type", "simple" );
  744. sc.xmlWriter.addAttribute( "xlink:show", "embed" );
  745. sc.xmlWriter.addAttribute( "xlink:actuate", "onLoad" );
  746. sc.xmlWriter.addAttribute( "xlink:href", imageCollection->getOasisFileName( image ) );
  747. sc.xmlWriter.endElement();
  748. return true;
  749. }
  750. const char * EmbeddedPictureObject::getOasisElementName() const
  751. {
  752. return "draw:frame";
  753. }
  754. void EmbeddedPictureObject::loadPicture( const TQString & fileName )
  755. {
  756. image = imageCollection->loadPicture( fileName );
  757. }
  758. EmbeddedPictureObject &EmbeddedPictureObject::operator=( const EmbeddedPictureObject & )
  759. {
  760. return *this;
  761. }
  762. void EmbeddedPictureObject::setPicture( const KoPictureKey & key )
  763. {
  764. image = imageCollection->findPicture( key );
  765. }
  766. void EmbeddedPictureObject::reload( void )
  767. {
  768. // ### FIXME: this seems wrong, KoPictureCollection will never reload it (or perhaps it is the function name that is wrong)
  769. setPicture( image.getKey() );
  770. }
  771. // KSpread doesn't support pictures in it's old XML file format.
  772. // TQDomDocumentFragment EmbeddedPictureObject::save( TQDomDocument& doc, double offset )
  773. // {
  774. // TQDomDocumentFragment fragment=KP2DObject::save(doc, offset);
  775. // TQDomElement elem=doc.createElement("KEY");
  776. // image.getKey().saveAttributes(elem);
  777. // fragment.appendChild(elem);
  778. //
  779. // TQDomElement elemSettings = doc.createElement( "PICTURESETTINGS" );
  780. //
  781. // elemSettings.setAttribute( "mirrorType", static_cast<int>( mirrorType ) );
  782. // elemSettings.setAttribute( "depth", depth );
  783. // elemSettings.setAttribute( "swapRGB", static_cast<int>( swapRGB ) );
  784. // elemSettings.setAttribute( "grayscal", static_cast<int>( grayscal ) );
  785. // elemSettings.setAttribute( "bright", bright );
  786. // fragment.appendChild( elemSettings );
  787. //
  788. // if (m_effect!=IE_NONE) {
  789. // TQDomElement imageEffects = doc.createElement("EFFECTS");
  790. // imageEffects.setAttribute("type", static_cast<int>(m_effect));
  791. // if (m_ie_par1.isValid())
  792. // imageEffects.setAttribute("param1", m_ie_par1.toString());
  793. // if (m_ie_par2.isValid())
  794. // imageEffects.setAttribute("param2", m_ie_par2.toString());
  795. // if (m_ie_par3.isValid())
  796. // imageEffects.setAttribute("param3", m_ie_par3.toString());
  797. // fragment.appendChild( imageEffects );
  798. // }
  799. //
  800. // return fragment;
  801. // }
  802. void EmbeddedPictureObject::loadOasisPictureEffect(KoOasisLoadingContext & context )
  803. {
  804. KoStyleStack &styleStack = context.styleStack();
  805. styleStack.setTypeProperties( "graphic" );
  806. if ( styleStack.hasAttributeNS( KoXmlNS::draw, "color-mode" ) && ( styleStack.attributeNS( KoXmlNS::draw, "color-mode" )=="greyscale" ) )
  807. {
  808. grayscal = true;
  809. }
  810. if ( styleStack.hasAttributeNS( KoXmlNS::draw, "contrast" ) )
  811. {
  812. TQString str( styleStack.attributeNS( KoXmlNS::draw, "contrast" ) );
  813. str = str.remove( '%' );
  814. int val = str.toInt();
  815. m_effect = IE_CONTRAST;
  816. val = ( int )( 255.0 *val/100.0 );
  817. m_ie_par1 = TQVariant(val);
  818. }
  819. if ( styleStack.hasAttributeNS( KoXmlNS::draw, "red" ) && styleStack.attributeNS( KoXmlNS::draw, "red" ) != "0%" )
  820. {
  821. TQString str( styleStack.attributeNS( KoXmlNS::draw, "red" ) );
  822. str = str.remove( '%' );
  823. int val = str.toInt();
  824. m_effect = IE_CHANNEL_INTENSITY;
  825. m_ie_par1 = TQVariant(val);
  826. m_ie_par2 = TQVariant( ( int )KImageEffect::Red );
  827. }
  828. if ( styleStack.hasAttributeNS( KoXmlNS::draw, "green" ) && styleStack.attributeNS( KoXmlNS::draw, "green" ) != "0%" )
  829. {
  830. TQString str( styleStack.attributeNS( KoXmlNS::draw, "green" ) );
  831. str = str.remove( '%' );
  832. int val = str.toInt();
  833. m_effect = IE_CHANNEL_INTENSITY;
  834. m_ie_par1 = TQVariant(val);
  835. m_ie_par2 = TQVariant( ( int )KImageEffect::Green );
  836. }
  837. if ( styleStack.hasAttributeNS( KoXmlNS::draw, "blue" ) && styleStack.attributeNS( KoXmlNS::draw, "blue" ) != "0%" )
  838. {
  839. TQString str( styleStack.attributeNS( KoXmlNS::draw, "blue" ) );
  840. str = str.remove( '%' );
  841. int val = str.toInt();
  842. m_effect = IE_CHANNEL_INTENSITY;
  843. m_ie_par1 = TQVariant(val);
  844. m_ie_par2 = TQVariant( ( int )KImageEffect::Blue );
  845. }
  846. if ( styleStack.hasAttributeNS( KoXmlNS::draw, "luminance" ) )
  847. {
  848. TQString str( styleStack.attributeNS( KoXmlNS::draw, "luminance" ) );
  849. str = str.remove( '%' );
  850. bright = str.toInt();
  851. }
  852. }
  853. void EmbeddedPictureObject::fillStyle( KoGenStyle& styleObjectAuto, KoGenStyles& /*mainStyles*/ ) const
  854. {
  855. //KP2DObject::fillStyle( styleObjectAuto, mainStyles );
  856. saveOasisPictureElement( styleObjectAuto );
  857. }
  858. void EmbeddedPictureObject::loadOasis(const TQDomElement &element, KoOasisLoadingContext & context/*, KPRLoadingInfo *info*/)
  859. {
  860. //load it into kpresenter_doc
  861. EmbeddedObject::loadOasis( element, context );
  862. loadOasisPictureEffect( context );
  863. TQDomNode imageBox = KoDom::namedItemNS( element, KoXmlNS::draw, "image" );
  864. const TQString href( imageBox.toElement().attributeNS( KoXmlNS::xlink, "href", TQString()) );
  865. kdDebug()<<" href: "<<href<<endl;
  866. if ( !href.isEmpty() /*&& href[0] == '#'*/ )
  867. {
  868. TQString strExtension;
  869. const int result=href.findRev(".");
  870. if (result>=0)
  871. {
  872. strExtension=href.mid(result+1); // As we are using KoPicture, the extension should be without the dot.
  873. }
  874. TQString filename(href/*.mid(1)*/);
  875. const KoPictureKey key(filename, TQDateTime::currentDateTime(Qt::UTC));
  876. image.setKey(key);
  877. KoStore* store = context.store();
  878. if ( store->open( filename ) )
  879. {
  880. KoStoreDevice dev(store);
  881. if ( !image.load( &dev, strExtension ) )
  882. kdWarning() << "Cannot load picture: " << filename << " " << href << endl;
  883. store->close();
  884. }
  885. imageCollection->insertPicture( key, image );
  886. }
  887. // ### TODO: load remote file
  888. }
  889. // double EmbeddedPictureObject::load(const TQDomElement &element)
  890. // {
  891. // double offset=KP2DObject::load(element);
  892. // TQDomElement e=element.namedItem("KEY").toElement();
  893. // if(!e.isNull()) {
  894. // KoPictureKey key;
  895. // key.loadAttributes( e );
  896. // image.clear();
  897. // image.setKey(key);
  898. // }
  899. // else {
  900. // // try to find a PIXMAP tag if the KEY is not available...
  901. // e=element.namedItem("PIXMAP").toElement();
  902. // if (e.isNull()) {
  903. // // try to find a FILENAME tag (old cliparts...)
  904. // e=element.namedItem("FILENAME").toElement();
  905. // if(!e.isNull()) {
  906. // // Loads from the disk directly (unless it's in the collection already?)
  907. // image = imageCollection->loadPicture( e.attribute("filename") );
  908. // }
  909. // } else {
  910. // bool openPic = true;
  911. // TQString _data;
  912. // TQString _fileName;
  913. // if(e.hasAttribute("data"))
  914. // _data=e.attribute("data");
  915. // if ( _data.isEmpty() )
  916. // openPic = true;
  917. // else
  918. // openPic = false;
  919. // if(e.hasAttribute("filename"))
  920. // _fileName=e.attribute("filename");
  921. // if ( !_fileName.isEmpty() )
  922. // {
  923. // if ( int _envVarB = _fileName.find( '$' ) >= 0 )
  924. // {
  925. // int _envVarE = _fileName.find( '/', _envVarB );
  926. // // ### FIXME: it should be TQString::local8Bit instead of TQFile::encodeName, shouldn't it?
  927. // TQString path = getenv( TQFile::encodeName(_fileName.mid( _envVarB, _envVarE-_envVarB )) );
  928. // _fileName.replace( _envVarB-1, _envVarE-_envVarB+1, path );
  929. // }
  930. // }
  931. //
  932. // if ( openPic )
  933. // // !! this loads it from the disk (unless it's in the image collection already)
  934. // image = imageCollection->loadPicture( _fileName );
  935. // else
  936. // {
  937. // KoPictureKey key( _fileName );
  938. // image.clear();
  939. // image.setKey(key);
  940. // TQByteArray rawData=_data.utf8(); // XPM is normally ASCII, therefore UTF-8
  941. // rawData[rawData.size()-1]=char(10); // Replace the NULL character by a LINE FEED
  942. // TQBuffer buffer(rawData); // ### TODO: open?
  943. // image.loadXpm(&buffer);
  944. // }
  945. // }
  946. // }
  947. //
  948. // e = element.namedItem( "PICTURESETTINGS" ).toElement();
  949. // if ( !e.isNull() ) {
  950. // PictureMirrorType _mirrorType = PM_NORMAL;
  951. // int _depth = 0;
  952. // bool _swapRGB = false;
  953. // bool _grayscal = false;
  954. // int _bright = 0;
  955. //
  956. // if ( e.hasAttribute( "mirrorType" ) )
  957. // _mirrorType = static_cast<PictureMirrorType>( e.attribute( "mirrorType" ).toInt() );
  958. // if ( e.hasAttribute( "depth" ) )
  959. // _depth = e.attribute( "depth" ).toInt();
  960. // if ( e.hasAttribute( "swapRGB" ) )
  961. // _swapRGB = static_cast<bool>( e.attribute( "swapRGB" ).toInt() );
  962. // if ( e.hasAttribute( "grayscal" ) )
  963. // _grayscal = static_cast<bool>( e.attribute( "grayscal" ).toInt() );
  964. // if ( e.hasAttribute( "bright" ) )
  965. // _bright = e.attribute( "bright" ).toInt();
  966. //
  967. // mirrorType = _mirrorType;
  968. // depth = _depth;
  969. // swapRGB = _swapRGB;
  970. // grayscal = _grayscal;
  971. // bright = _bright;
  972. // }
  973. // else {
  974. // mirrorType = PM_NORMAL;
  975. // depth = 0;
  976. // swapRGB = false;
  977. // grayscal = false;
  978. // bright = 0;
  979. // }
  980. //
  981. // e = element.namedItem( "EFFECTS" ).toElement();
  982. // if (!e.isNull()) {
  983. // if (e.hasAttribute("type"))
  984. // m_effect = static_cast<ImageEffect>(e.attribute("type").toInt());
  985. // if (e.hasAttribute("param1"))
  986. // m_ie_par1 = TQVariant(e.attribute("param1"));
  987. // else
  988. // m_ie_par1 = TQVariant();
  989. // if (e.hasAttribute("param2"))
  990. // m_ie_par2 = TQVariant(e.attribute("param2"));
  991. // else
  992. // m_ie_par2 = TQVariant();
  993. // if (e.hasAttribute("param3"))
  994. // m_ie_par3 = TQVariant(e.attribute("param3"));
  995. // else
  996. // m_ie_par3 = TQVariant();
  997. // }
  998. // else
  999. // m_effect = IE_NONE;
  1000. //
  1001. // return offset;
  1002. // }
  1003. void EmbeddedPictureObject::drawShadow( TQPainter* /*_painter*/, KoZoomHandler* /*_zoomHandler*/)
  1004. {
  1005. // const double ox = /*orig*/m_geometry.x();
  1006. // const double oy = /*orig*/m_geometry.y();
  1007. // const double ow = /*ext*/m_geometry.width();
  1008. // const double oh = /*ext*/m_geometry.height();
  1009. //
  1010. // _painter->save();
  1011. //
  1012. // TQPen pen2 = pen.zoomedPen( _zoomHandler );
  1013. // _painter->setPen( pen2 );
  1014. // _painter->setBrush( getBrush() );
  1015. //
  1016. // double sx = 0;
  1017. // double sy = 0;
  1018. //
  1019. // getShadowCoords( sx, sy );
  1020. //
  1021. // _painter->translate( _zoomHandler->zoomItX( ox ), _zoomHandler->zoomItY( oy ) );
  1022. // _painter->setPen( TQPen( shadowColor ) );
  1023. // _painter->setBrush( shadowColor );
  1024. // if ( kAbs(angle) <= DBL_EPSILON )
  1025. // _painter->drawRect( _zoomHandler->zoomItX( sx ), _zoomHandler->zoomItY( sy ),
  1026. // _zoomHandler->zoomItX( ext.width() ), _zoomHandler->zoomItY( ext.height() ) );
  1027. // else
  1028. // {
  1029. // TQSize bs = TQSize( _zoomHandler->zoomItX( ow ), _zoomHandler->zoomItY( oh ) );
  1030. // TQRect br = TQRect( 0, 0, bs.width(), bs.height() );
  1031. // int pw = br.width();
  1032. // int ph = br.height();
  1033. // TQRect rr = br;
  1034. // int pixYPos = -rr.y();
  1035. // int pixXPos = -rr.x();
  1036. // br.moveTopLeft( TQPoint( -br.width() / 2, -br.height() / 2 ) );
  1037. // rr.moveTopLeft( TQPoint( -rr.width() / 2, -rr.height() / 2 ) );
  1038. //
  1039. // TQWMatrix m;
  1040. // m.translate( pw / 2, ph / 2 );
  1041. // m.rotate( angle );
  1042. // m.translate( rr.left() + pixXPos + _zoomHandler->zoomItX( sx ),
  1043. // rr.top() + pixYPos + _zoomHandler->zoomItY( sy ) );
  1044. //
  1045. // _painter->setWorldMatrix( m, true );
  1046. //
  1047. // _painter->drawRect( 0, 0, bs.width(), bs.height() );
  1048. // }
  1049. //
  1050. // _painter->restore();
  1051. }
  1052. TQPixmap EmbeddedPictureObject::toPixmap( double xZoom , double yZoom )
  1053. {
  1054. KoZoomHandler zoomHandler;
  1055. zoomHandler.setZoomedResolution( xZoom /* *zoomHandler.resolutionX()*/ , yZoom /* *zoomHandler.resolutionY()*/ );
  1056. return generatePixmap( &zoomHandler );
  1057. }
  1058. TQPixmap EmbeddedPictureObject::generatePixmap(KoZoomHandler*_zoomHandler)
  1059. {
  1060. const double penw = _zoomHandler->zoomItX( ( ( pen.style() == Qt::NoPen ) ? 1 : pen.width() ) / 2.0 );
  1061. TQSize size( _zoomHandler->zoomSize( m_geometry.size() /*ext*/ ) );
  1062. //kdDebug(33001) << "EmbeddedPictureObject::generatePixmap size= " << size << endl;
  1063. TQPixmap pixmap(size);
  1064. TQPainter paint;
  1065. paint.begin( &pixmap );
  1066. pixmap.fill( TQt::white );
  1067. // Draw background
  1068. paint.setPen( Qt::NoPen );
  1069. paint.setBrush( getBrush() );
  1070. TQRect rect( (int)( penw ), (int)( penw ),
  1071. (int)( _zoomHandler->zoomItX( /*ext*/m_geometry.width() ) - 2.0 * penw ),
  1072. (int)( _zoomHandler->zoomItY( /*ext*/m_geometry.height() ) - 2.0 * penw ) );
  1073. // if ( getFillType() == FT_BRUSH || !gradient )
  1074. paint.drawRect( rect );
  1075. // else {
  1076. // ### TODO: this was also drawn for drawContour==true, but why?
  1077. // gradient->setSize( size );
  1078. // paint.drawPixmap( (int)( penw ), (int)( penw ),
  1079. // gradient->pixmap(), 0, 0,
  1080. // (int)( _zoomHandler->zoomItX( m_geometry/*ext*/.width() ) - 2 * penw ),
  1081. // (int)( _zoomHandler->zoomItY( m_geometry/*ext*/.height() ) - 2 * penw ) );
  1082. // }
  1083. image.draw(paint, 0, 0, size.width(), size.height(), 0, 0, -1, -1, false); // Always slow mode!
  1084. image.clearCache(); // Release the memoy of the picture cache
  1085. // image.setAlphaBuffer(true);
  1086. // TQBitmap tmpMask;
  1087. // tmpMask = image.createAlphaMask().scale(size);
  1088. // pixmap.setMask(tmpMask);
  1089. paint.end();
  1090. return pixmap;
  1091. }
  1092. void EmbeddedPictureObject::draw( TQPainter *_painter/*, KoZoomHandler*_zoomHandler,
  1093. int pageNum, SelectionMode selectionMode, bool drawContour*/ )
  1094. {
  1095. bool drawContour = false;
  1096. KoZoomHandler*_zoomHandler = sheet()->doc();
  1097. if ( image.isNull() ) return;
  1098. // if ( shadowDistance > 0 && !drawContour )
  1099. // drawShadow(_painter, _zoomHandler);
  1100. const double ox = /*orig*/m_geometry.x();
  1101. const double oy = /*orig*/m_geometry.y();
  1102. const double ow = /*ext*/m_geometry.width();
  1103. const double oh = /*ext*/m_geometry.height();
  1104. //const double penw = _zoomHandler->zoomItX( ( ( pen.style() == Qt::NoPen ) ? 1.0 : pen.width() ) / 2.0 );
  1105. _painter->save();
  1106. _painter->translate( _zoomHandler->zoomItX( ox ), _zoomHandler->zoomItY( oy ) );
  1107. if ( kAbs(angle)> DBL_EPSILON ) {
  1108. TQSize bs = TQSize( _zoomHandler->zoomItX( ow ), _zoomHandler->zoomItY( oh ) );
  1109. TQRect br = TQRect( 0, 0, bs.width(), bs.height() );
  1110. int pw = br.width();
  1111. int ph = br.height();
  1112. TQRect rr = br;
  1113. int pixYPos = -rr.y();
  1114. int pixXPos = -rr.x();
  1115. br.moveTopLeft( TQPoint( -br.width() / 2, -br.height() / 2 ) );
  1116. rr.moveTopLeft( TQPoint( -rr.width() / 2, -rr.height() / 2 ) );
  1117. TQWMatrix m;
  1118. m.translate( pw / 2, ph / 2 );
  1119. m.rotate( angle );
  1120. m.translate( rr.left() + pixXPos, rr.top() + pixYPos );
  1121. _painter->setWorldMatrix( m, true );
  1122. }
  1123. if ( !drawContour )
  1124. {
  1125. TQRect rect( 0, 0, (int)( _zoomHandler->zoomItX( ow ) ),
  1126. (int)( _zoomHandler->zoomItY( oh ) ) );
  1127. // ### HACK QT seems not to be able to correctly compare TQVariant
  1128. bool variants1;
  1129. if (m_ie_par1.isNull())
  1130. variants1=m_cachedPar1.isNull();
  1131. else
  1132. variants1=(m_ie_par1 == m_cachedPar1);
  1133. bool variants2;
  1134. if (m_ie_par2.isNull())
  1135. variants2=m_cachedPar2.isNull();
  1136. else
  1137. variants2=(m_ie_par2 == m_cachedPar2);
  1138. bool variants3;
  1139. if (m_ie_par3.isNull())
  1140. variants3=m_cachedPar3.isNull();
  1141. else
  1142. variants3=(m_ie_par3 == m_cachedPar3);
  1143. if (m_cachedRect == rect
  1144. // All what EmbeddedPictureObject::changePictureSettings needs
  1145. && m_cachedMirrorType == mirrorType && m_cachedSwapRGB == swapRGB && m_cachedGrayscal == grayscal
  1146. && m_cachedBright == bright && m_cachedEffect == m_effect
  1147. // Who needs it?
  1148. && m_cachedDepth == depth
  1149. #if 0
  1150. && m_ie_par1 == m_cachedPar1 && m_ie_par2 == m_cachedPar2 && m_ie_par3 == m_cachedPar3
  1151. #else
  1152. && variants1 && variants2 && variants3
  1153. #endif
  1154. )
  1155. {
  1156. //kdDebug(33001) << "Drawing cached pixmap " << (void*) this << " " << k_funcinfo << endl;
  1157. }
  1158. else
  1159. {
  1160. if (mirrorType != PM_NORMAL || depth != 0 || swapRGB || grayscal || bright != 0 || m_effect!=IE_NONE)
  1161. m_cachedPixmap = changePictureSettings( generatePixmap( _zoomHandler ) );
  1162. else
  1163. m_cachedPixmap = generatePixmap( _zoomHandler );
  1164. m_cachedRect = rect;
  1165. m_cachedMirrorType = mirrorType;
  1166. m_cachedSwapRGB = swapRGB;
  1167. m_cachedGrayscal = grayscal;
  1168. m_cachedBright = bright;
  1169. m_cachedEffect = m_effect;
  1170. m_cachedDepth = depth;
  1171. m_cachedPar1 = m_ie_par1;
  1172. m_cachedPar2 = m_ie_par2;
  1173. m_cachedPar3 = m_ie_par3;
  1174. //kdDebug(33001) << "Drawing non-cached pixmap " << (void*) this << " " << k_funcinfo << endl;
  1175. }
  1176. _painter->eraseRect( rect );
  1177. _painter->drawPixmap( rect, m_cachedPixmap);
  1178. }
  1179. // Draw border
  1180. // ### TODO port to KoBorder::drawBorders() (after writing a simplified version of it, that takes the same border on each size)
  1181. // TQPen pen2;
  1182. // if ( drawContour ) {
  1183. // pen2 = TQPen( TQt::black, 1, TQt::DotLine );
  1184. // _painter->setRasterOp( TQt::NotXorROP );
  1185. // }
  1186. // else {
  1187. // pen2 = pen;
  1188. // pen2.setWidth( _zoomHandler->zoomItX( ( pen.style() == Qt::NoPen ) ? 1.0 : (double)pen.width() ) );
  1189. // }
  1190. // _painter->setPen( pen2 );
  1191. // _painter->setBrush( TQt::NoBrush );
  1192. // _painter->drawRect( (int)( penw ), (int)( penw ),
  1193. // (int)( _zoomHandler->zoomItX( ow ) - 2.0 * penw ),
  1194. // (int)( _zoomHandler->zoomItY( oh ) - 2.0 * penw ) );
  1195. _painter->restore();
  1196. //KPObject::draw( _painter, _zoomHandler, pageNum, selectionMode, drawContour );
  1197. EmbeddedObject::draw( _painter );
  1198. }
  1199. TQPixmap EmbeddedPictureObject::getOriginalPixmap()
  1200. {
  1201. TQSize _pixSize = image.getOriginalSize();
  1202. kdDebug(33001) << "EmbeddedPictureObject::getOriginalPixmap size= " << _pixSize << endl;
  1203. TQPixmap _pixmap = image.generatePixmap( _pixSize, true );
  1204. image.clearCache(); // Release the memoy of the picture cache
  1205. return _pixmap;
  1206. }
  1207. TQPixmap EmbeddedPictureObject::changePictureSettings( TQPixmap _tmpPixmap )
  1208. {
  1209. TQImage _tmpImage = _tmpPixmap.convertToImage();
  1210. if (_tmpImage.isNull())
  1211. return _tmpPixmap;
  1212. bool _horizontal = false;
  1213. bool _vertical = false;
  1214. if ( mirrorType == PM_HORIZONTAL )
  1215. _horizontal = true;
  1216. else if ( mirrorType == PM_VERTICAL )
  1217. _vertical = true;
  1218. else if ( mirrorType == PM_HORIZONTALANDVERTICAL ) {
  1219. _horizontal = true;
  1220. _vertical = true;
  1221. }
  1222. _tmpImage = _tmpImage.mirror( _horizontal, _vertical );
  1223. if ( depth != 0 ) {
  1224. TQImage tmpImg = _tmpImage.convertDepth( depth );
  1225. if ( !tmpImg.isNull() )
  1226. _tmpImage = tmpImg;
  1227. }
  1228. if ( swapRGB )
  1229. _tmpImage = _tmpImage.swapRGB();
  1230. if ( grayscal ) {
  1231. if ( depth == 1 || depth == 8 ) {
  1232. for ( int i = 0; i < _tmpImage.numColors(); ++i ) {
  1233. TQRgb rgb = _tmpImage.color( i );
  1234. int gray = tqGray( rgb );
  1235. rgb = tqRgb( gray, gray, gray );
  1236. _tmpImage.setColor( i, rgb );
  1237. }
  1238. }
  1239. else {
  1240. int _width = _tmpImage.width();
  1241. int _height = _tmpImage.height();
  1242. int _x = 0;
  1243. int _y = 0;
  1244. for ( _x = 0; _x < _width; ++_x ) {
  1245. for ( _y = 0; _y < _height; ++_y ) {
  1246. if ( _tmpImage.valid( _x, _y ) ) {
  1247. TQRgb rgb = _tmpImage.pixel( _x, _y );
  1248. int gray = tqGray( rgb );
  1249. rgb = tqRgb( gray, gray, gray );
  1250. _tmpImage.setPixel( _x, _y, rgb );
  1251. }
  1252. }
  1253. }
  1254. }
  1255. }
  1256. if ( bright != 0 ) {
  1257. if ( depth == 1 || depth == 8 ) {
  1258. for ( int i = 0; i < _tmpImage.numColors(); ++i ) {
  1259. TQRgb rgb = _tmpImage.color( i );
  1260. TQColor c( rgb );
  1261. if ( bright > 0 )
  1262. rgb = c.light( 100 + bright ).rgb();
  1263. else
  1264. rgb = c.dark( 100 + abs( bright ) ).rgb();
  1265. _tmpImage.setColor( i, rgb );
  1266. }
  1267. }
  1268. else {
  1269. int _width = _tmpImage.width();
  1270. int _height = _tmpImage.height();
  1271. int _x = 0;
  1272. int _y = 0;
  1273. for ( _x = 0; _x < _width; ++_x ) {
  1274. for ( _y = 0; _y < _height; ++_y ) {
  1275. if ( _tmpImage.valid( _x, _y ) ) {
  1276. TQRgb rgb = _tmpImage.pixel( _x, _y );
  1277. TQColor c( rgb );
  1278. if ( bright > 0 )
  1279. rgb = c.light( 100 + bright ).rgb();
  1280. else
  1281. rgb = c.dark( 100 + abs( bright ) ).rgb();
  1282. _tmpImage.setPixel( _x, _y, rgb );
  1283. }
  1284. }
  1285. }
  1286. }
  1287. }
  1288. switch (m_effect) {
  1289. case IE_CHANNEL_INTENSITY: {
  1290. _tmpImage = KImageEffect::channelIntensity(_tmpImage, m_ie_par1.toDouble()/100.0,
  1291. static_cast<KImageEffect::RGBComponent>(m_ie_par2.toInt()));
  1292. break;
  1293. }
  1294. case IE_FADE: {
  1295. _tmpImage = KImageEffect::fade(_tmpImage, m_ie_par1.toDouble(), m_ie_par2.toColor());
  1296. break;
  1297. }
  1298. case IE_FLATTEN: {
  1299. _tmpImage = KImageEffect::flatten(_tmpImage, m_ie_par1.toColor(), m_ie_par2.toColor());
  1300. break;
  1301. }
  1302. case IE_INTENSITY: {
  1303. _tmpImage = KImageEffect::intensity(_tmpImage, m_ie_par1.toDouble()/100.0);
  1304. break;
  1305. }
  1306. case IE_DESATURATE: {
  1307. _tmpImage = KImageEffect::desaturate(_tmpImage, m_ie_par1.toDouble());
  1308. break;
  1309. }
  1310. case IE_CONTRAST: {
  1311. _tmpImage = KImageEffect::contrast(_tmpImage, m_ie_par1.toInt());
  1312. break;
  1313. }
  1314. case IE_NORMALIZE: {
  1315. KImageEffect::normalize(_tmpImage);
  1316. break;
  1317. }
  1318. case IE_EQUALIZE: {
  1319. KImageEffect::equalize(_tmpImage);
  1320. break;
  1321. }
  1322. case IE_THRESHOLD: {
  1323. KImageEffect::threshold(_tmpImage, m_ie_par1.toInt());
  1324. break;
  1325. }
  1326. case IE_SOLARIZE: {
  1327. KImageEffect::solarize(_tmpImage, m_ie_par1.toDouble());
  1328. break;
  1329. }
  1330. case IE_EMBOSS: {
  1331. _tmpImage = KImageEffect::emboss(_tmpImage);
  1332. break;
  1333. }
  1334. case IE_DESPECKLE: {
  1335. _tmpImage = KImageEffect::despeckle(_tmpImage);
  1336. break;
  1337. }
  1338. case IE_CHARCOAL: {
  1339. _tmpImage = KImageEffect::charcoal(_tmpImage, m_ie_par1.toDouble());
  1340. break;
  1341. }
  1342. case IE_NOISE: {
  1343. _tmpImage = KImageEffect::addNoise(_tmpImage, static_cast<KImageEffect::NoiseType>(m_ie_par1.toInt()));
  1344. break;
  1345. }
  1346. case IE_BLUR: {
  1347. _tmpImage = KImageEffect::blur(_tmpImage, m_ie_par1.toDouble());
  1348. break;
  1349. }
  1350. case IE_EDGE: {
  1351. _tmpImage = KImageEffect::edge(_tmpImage, m_ie_par1.toDouble());
  1352. break;
  1353. }
  1354. case IE_IMPLODE: {
  1355. _tmpImage = KImageEffect::implode(_tmpImage, m_ie_par1.toDouble());
  1356. break;
  1357. }
  1358. case IE_OIL_PAINT: {
  1359. _tmpImage = KImageEffect::oilPaint(_tmpImage, m_ie_par1.toInt());
  1360. break;
  1361. }
  1362. case IE_SHARPEN: {
  1363. _tmpImage = KImageEffect::sharpen(_tmpImage, m_ie_par1.toDouble());
  1364. break;
  1365. }
  1366. case IE_SPREAD: {
  1367. _tmpImage = KImageEffect::spread(_tmpImage, m_ie_par1.toInt());
  1368. break;
  1369. }
  1370. case IE_SHADE: {
  1371. _tmpImage = KImageEffect::shade(_tmpImage, m_ie_par1.toBool(), m_ie_par2.toDouble(), m_ie_par3.toDouble());
  1372. break;
  1373. }
  1374. case IE_SWIRL: {
  1375. _tmpImage = KImageEffect::swirl(_tmpImage, m_ie_par1.toDouble());
  1376. break;
  1377. }
  1378. case IE_WAVE: {
  1379. _tmpImage = KImageEffect::wave(_tmpImage, m_ie_par1.toDouble(), m_ie_par2.toDouble());
  1380. break;
  1381. }
  1382. case IE_NONE:
  1383. default:
  1384. break;
  1385. }
  1386. _tmpPixmap.convertFromImage( _tmpImage );
  1387. return _tmpPixmap;
  1388. }
  1389. void EmbeddedPictureObject::flip( bool /*horizontal*/ )
  1390. {
  1391. // KP2DObject::flip( horizontal );
  1392. // if ( horizontal )
  1393. // {
  1394. // switch ( mirrorType )
  1395. // {
  1396. // case PM_NORMAL:
  1397. // mirrorType = PM_HORIZONTAL;
  1398. // break;
  1399. // case PM_HORIZONTAL:
  1400. // mirrorType = PM_NORMAL;
  1401. // break;
  1402. // case PM_VERTICAL:
  1403. // mirrorType = PM_HORIZONTALANDVERTICAL;
  1404. // break;
  1405. // case PM_HORIZONTALANDVERTICAL:
  1406. // mirrorType = PM_VERTICAL;
  1407. // break;
  1408. // }
  1409. // }
  1410. // else
  1411. // {
  1412. // switch ( mirrorType )
  1413. // {
  1414. // case PM_NORMAL:
  1415. // mirrorType = PM_VERTICAL;
  1416. // break;
  1417. // case PM_HORIZONTAL:
  1418. // mirrorType = PM_HORIZONTALANDVERTICAL;
  1419. // break;
  1420. // case PM_VERTICAL:
  1421. // mirrorType = PM_NORMAL;
  1422. // break;
  1423. // case PM_HORIZONTALANDVERTICAL:
  1424. // mirrorType = PM_HORIZONTAL;
  1425. // break;
  1426. // }
  1427. // }
  1428. }