21#include "KPrBackground.h"
22#include "KPrDocument.h"
23#include <tdeapplication.h>
24#include "KPrGradientCollection.h"
25#include "KPrTextObject.h"
32#include <tqfileinfo.h>
34#include <KoRichText.h>
35#include <KoTextObject.h>
40#include <tdeglobalsettings.h>
42#include <KoStoreDevice.h>
43#include <KoGenStyles.h>
46KPrBackGround::KPrBackGround( KPrPage *_page )
51 backColor1 = TQt::white;
52 backColor2 = TQt::white;
62void KPrBackGround::setBackPicture(
const KoPicture& picture )
64 if ( backType != BT_PICTURE )
66 backPicture = pictureCollection()->insertPicture(picture);
69void KPrBackGround::setBackPicture (
const KoPictureKey& key )
71 if ( backType != BT_PICTURE )
73 backPicture = pictureCollection()->findOrLoad(key.filename(), key.lastModified() );
76void KPrBackGround::drawBackground( TQPainter *_painter,
const KoTextZoomHandler* zoomHandler,
77 const TQRect& _crect,
bool _drawBorders )
79 TQRect pageRect = zoomHandler->zoomRect( m_page->getPageRect() );
80 TQRect crect = pageRect.intersect( _crect );
81 if ( crect.isEmpty() )
83 TQSize ext = pageRect.size();
84 drawBackground( _painter, ext, crect, _drawBorders );
87void KPrBackGround::drawBackground( TQPainter *_painter,
const TQSize& ext,
const TQRect& crect,
bool _drawBorders )
89 if ( m_page->displayBackground() )
95 drawBackColor( _painter, ext, crect );
100 if ( backView == BV_CENTER )
101 drawBackColor( _painter, ext, crect );
102 drawBackPix( _painter, ext, crect );
110 drawBorders( _painter, ext, crect );
116void KPrBackGround::reload()
118 if ( backType == BT_PICTURE || backType == BT_CLIPART )
119 backPicture = pictureCollection()->insertPicture(backPicture);
125TQDomElement KPrBackGround::save( TQDomDocument &doc )
127 TQString tag = m_page->masterPage() == 0 ?
"MASTERPAGE" :
"PAGE";
128 TQDomElement page=doc.createElement( tag );
129 TQDomElement element;
131 element=doc.createElement(
"BACKMASTER");
132 element.setAttribute(
"useMasterBackground",
static_cast<int>(m_page->useMasterBackground() ) );
133 element.setAttribute(
"displayMasterPageObject",
static_cast<int>( m_page->displayObjectFromMasterPage() ));
134 element.setAttribute(
"displayBackground",
static_cast<int>( m_page->displayBackground() ));
135 page.appendChild(element);
136 if ( !m_page->useMasterBackground() )
139 if (backType!=BT_COLOR) {
140 element=doc.createElement(
"BACKTYPE");
141 element.setAttribute(
"value",
static_cast<int>( backType ));
142 page.appendChild(element);
145 if (backView!=BV_CENTER) {
146 element=doc.createElement(
"BACKVIEW");
147 element.setAttribute(
"value",
static_cast<int>( backView ));
148 page.appendChild(element);
151 if (backColor1!=TQt::white) {
152 element=doc.createElement(
"BACKCOLOR1");
153 element.setAttribute(
"color", backColor1.name());
154 page.appendChild(element);
157 if (backColor2!=TQt::white) {
158 element=doc.createElement(
"BACKCOLOR2");
159 element.setAttribute(
"color", backColor2.name());
160 page.appendChild(element);
163 if (bcType!=BCT_PLAIN) {
164 element=doc.createElement(
"BCTYPE");
165 element.setAttribute(
"value",
static_cast<int>( bcType ));
166 page.appendChild(element);
169 if (xfactor!=100 || yfactor!=100 || unbalanced) {
170 element=doc.createElement(
"BGRADIENT");
171 element.setAttribute(
"unbalanced",
static_cast<int>( unbalanced ));
172 element.setAttribute(
"xfactor", xfactor);
173 element.setAttribute(
"yfactor", yfactor);
174 page.appendChild(element);
177 if ( !backPicture.isNull() && ( ( backType == BT_PICTURE ) || ( backType == BT_CLIPART ) ) )
179 element = doc.createElement(
"BACKPICTUREKEY" );
181 backPicture.getKey().saveAttributes( element );
182 page.appendChild( element );
190void KPrBackGround::saveOasisBackgroundPageStyle( KoGenStyle& stylepageauto, KoGenStyles& mainStyles )
196 TQBrush qbrush( backColor1 );
197 KPrBrush brush( qbrush, backColor1, backColor2, bcType,
198 bcType == BCT_PLAIN ? FT_BRUSH : FT_GRADIENT,
199 unbalanced, xfactor, yfactor );
200 brush.saveOasisFillStyle( stylepageauto, mainStyles );
206 stylepageauto.addProperty(
"draw:fill",
"bitmap" );
207 stylepageauto.addProperty(
"draw:fill-image-name", saveOasisPictureStyle( mainStyles ) );
208 TQString repeat =
"repeat";
215 repeat =
"no-repeat";
221 stylepageauto.addProperty(
"style:repeat", repeat );
227TQString KPrBackGround::saveOasisPictureStyle( KoGenStyles& mainStyles )
230 KoGenStyle pictureStyle( KPrDocument::STYLE_PICTURE );
231 pictureStyle.addAttribute(
"xlink:show",
"embed" );
232 pictureStyle.addAttribute(
"xlink:actuate",
"onLoad" );
233 pictureStyle.addAttribute(
"xlink:type",
"simple" );
234 pictureStyle.addAttribute(
"xlink:href", pictureCollection()->getOasisFileName(backPicture ) );
235 return mainStyles.lookup( pictureStyle,
"picture" );
239void KPrBackGround::loadOasis(KoOasisContext & context )
241 KoStyleStack& styleStack = context.styleStack();
242 kdDebug()<<
"KPrBackGround::loadOasis()\n";
243 styleStack.setTypeProperties(
"drawing-page" );
244 if ( styleStack.hasAttributeNS( KoXmlNS::draw,
"fill" ) )
246 const TQString fill = styleStack.attributeNS( KoXmlNS::draw,
"fill" );
247 kdDebug(33001) <<
"fill page type :" << fill << endl;
248 if ( fill ==
"solid" || fill ==
"gradient" )
251 brush.loadOasisFillStyle( context,
"drawing-page" );
252 if ( brush.getFillType() == FT_BRUSH )
254 setBackColor1( brush.getBrush().color() );
255 setBackColorType( BCT_PLAIN );
259 setBackColor1( brush.getGColor1() );
260 setBackColor2( brush.getGColor2() );
261 setBackColorType( brush.getGType() );
262 setBackUnbalanced( brush.getGUnbalanced() );
263 setBackXFactor( brush.getGXFactor() );
264 setBackYFactor( brush.getGYFactor() );
266 setBackType( BT_COLOR );
268 else if ( fill ==
"bitmap" )
270 TQString style = styleStack.attributeNS( KoXmlNS::draw,
"fill-image-name" );
271 TQDomElement* draw =context.oasisStyles().drawStyles()[style];
273 const TQString href( draw->attributeNS( KoXmlNS::xlink,
"href", TQString()) );
274 kdDebug()<<
" href: "<<href<<endl;
275 if ( !href.isEmpty() )
277 TQString strExtension;
278 const int result=href.findRev(
".");
281 strExtension=href.mid(result+1);
283 TQString filename(href);
284 const KoPictureKey key(filename, TQDateTime::currentDateTime(TQt::UTC));
285 backPicture.setKey(key);
287 KoStore* store = context.store();
288 if ( store->open( filename ) )
290 KoStoreDevice dev(store);
291 if ( !backPicture.load( &dev, strExtension ) )
292 kdWarning() <<
"Cannot load picture: " << filename <<
" " << href << endl;
295 pictureCollection()->insertPicture( key, backPicture );
298 if ( styleStack.hasAttributeNS( KoXmlNS::style,
"repeat" ) )
300 TQString repeat = styleStack.attributeNS( KoXmlNS::style,
"repeat" );
301 if ( repeat ==
"stretch" )
302 setBackView( BV_ZOOM );
303 else if ( repeat ==
"no-repeat" )
304 setBackView( BV_CENTER );
306 setBackView( BV_TILED );
309 setBackView( BV_TILED );;
311 setBackType(BT_PICTURE);
316void KPrBackGround::load(
const TQDomElement &element )
319 TQDomElement e=element.namedItem(
"BACKMASTER").toElement();
322 if ( e.hasAttribute(
"useMasterBackground" ) )
324 m_page->setUseMasterBackground( (
bool)e.attribute(
"useMasterBackground").toInt());
327 m_page->setUseMasterBackground(
true );
329 if(e.hasAttribute(
"displayMasterPageObject"))
331 m_page->setDisplayObjectFromMasterPage( (
bool)e.attribute(
"displayMasterPageObject").toInt());
333 if(e.hasAttribute(
"displayBackground"))
335 m_page->setDisplayBackground( (
bool)e.attribute(
"displayBackground").toInt());
339 m_page->setUseMasterBackground(
false );
341 e=element.namedItem(
"BACKTYPE").toElement();
344 if(e.hasAttribute(
"value"))
345 tmp=e.attribute(
"value").toInt();
346 setBackType(
static_cast<BackType
>(tmp));
348 e=element.namedItem(
"BACKVIEW").toElement();
351 if(e.hasAttribute(
"value"))
352 tmp=e.attribute(
"value").toInt();
353 setBackView(
static_cast<BackView
>(tmp));
355 e=element.namedItem(
"BACKCOLOR1").toElement();
357 int red=0, green=0, blue=0;
358 if(e.hasAttribute(
"red"))
359 red=e.attribute(
"red").toInt();
360 if(e.hasAttribute(
"green"))
361 green=e.attribute(
"green").toInt();
362 if(e.hasAttribute(
"blue"))
363 blue=e.attribute(
"blue").toInt();
364 if(e.hasAttribute(
"color"))
365 setBackColor1(TQColor(e.attribute(
"color")));
367 setBackColor1(TQColor(red, green, blue));
369 e=element.namedItem(
"BACKCOLOR2").toElement();
371 int red=0, green=0, blue=0;
372 if(e.hasAttribute(
"red"))
373 red=e.attribute(
"red").toInt();
374 if(e.hasAttribute(
"green"))
375 green=e.attribute(
"green").toInt();
376 if(e.hasAttribute(
"blue"))
377 blue=e.attribute(
"blue").toInt();
378 if(e.hasAttribute(
"color"))
379 setBackColor2(TQColor(e.attribute(
"color")));
381 setBackColor2(TQColor(red, green, blue));
383 e=element.namedItem(
"BGRADIENT").toElement();
385 int xf=0, yf=0, unbalanced=0;
386 if(e.hasAttribute(
"xfactor"))
387 xf=e.attribute(
"xfactor").toInt();
389 if(e.hasAttribute(
"yfactor"))
390 yf=e.attribute(
"yfactor").toInt();
392 if(e.hasAttribute(
"unbalanced"))
393 unbalanced=e.attribute(
"unbalanced").toInt();
394 setBackUnbalanced(
static_cast<bool>(unbalanced));
396 e=element.namedItem(
"BCTYPE").toElement();
399 if(e.hasAttribute(
"value"))
400 tmp=e.attribute(
"value").toInt();
401 setBackColorType(
static_cast<BCType
>(tmp));
403 e=element.namedItem(
"BACKPICTUREKEY").toElement();
406 key.loadAttributes( e );
408 backPicture.setKey( key );
411 e=element.namedItem(
"BACKPIXKEY").toElement();
414 key.loadAttributes( e );
416 backPicture.setKey( key );
422 e=element.namedItem(
"BACKPIX").toElement();
427 if(e.hasAttribute(
"data"))
428 _data=e.attribute(
"data");
429 if ( _data.isEmpty() )
433 if(e.hasAttribute(
"filename"))
434 _fileName=e.attribute(
"filename");
435 if ( !_fileName.isEmpty() )
437 if (
int _envVarB = _fileName.find(
'$' ) >= 0 )
439 int _envVarE = _fileName.find(
'/', _envVarB );
440 TQString path = getenv( TQFile::encodeName(_fileName.mid( _envVarB, _envVarE-_envVarB )) );
441 _fileName.replace( _envVarB-1, _envVarE-_envVarB+1, path );
446 backPicture = pictureCollection()->loadPicture( _fileName );
449 KoPictureKey key( _fileName );
451 backPicture.setKey(key);
452 TQByteArray rawData=_data.utf8();
453 rawData[rawData.size()-1]=char(10);
454 TQBuffer buffer(rawData);
455 backPicture.loadXpm(&buffer);
459 if ( ext == orig_size.toTQSize() )
460 ext = backPicture.size();
462 backPicture = backPicture.scale( ext );
466 e=element.namedItem(
"BACKCLIPKEY").toElement();
468 KoPictureKey clipKey;
469 clipKey.loadAttributes( e );
471 backPicture.setKey(clipKey);
476 e=element.namedItem(
"BACKCLIP").toElement();
479 if(e.hasAttribute(
"filename"))
480 _fileName=e.attribute(
"filename");
481 if ( !_fileName.isEmpty() )
483 if (
int _envVarB = _fileName.find(
'$' ) >= 0 )
485 int _envVarE = _fileName.find(
'/', _envVarB );
486 TQString path = getenv( TQFile::encodeName(_fileName.mid( _envVarB, _envVarE-_envVarB )) );
487 _fileName.replace( _envVarB-1, _envVarE-_envVarB+1, path );
492 backPicture = pictureCollection()->loadPicture( _fileName );
497void KPrBackGround::drawBackColor( TQPainter *_painter,
const TQSize& ext,
const TQRect& crect )
499 if ( (backType == BT_COLOR && bcType == BCT_PLAIN) || backColor1 == backColor2 )
502 _painter->fillRect( crect, TQBrush( getBackColor1() ) );
504 else if (backType == BT_COLOR && bcType != BCT_PLAIN) {
505 if ( !gradientPixmap || gradientPixmap->size() != ext )
506 generateGradient( ext );
507 _painter->drawPixmap( crect.topLeft(), *gradientPixmap, crect );
510 _painter->fillRect( crect, TQBrush( TQt::white ) );
514void KPrBackGround::drawBackPix( TQPainter *_painter,
const TQSize& ext,
const TQRect& )
519 if ( !backPicture.isNull() )
522 const TQSize _origSize = backPicture.getOriginalSize();
524 double w = _origSize.width();
526#if KDE_IS_VERSION(3,1,90)
527 TQRect desk = TDEGlobalSettings::desktopGeometry(kapp->activeWindow());
529 TQRect desk = TQApplication::desktop()->screenGeometry();
532 double h = _origSize.height();
535 const TQSize _pixSize = TQSize( (
int)w, (
int)h );
541 backPix=backPicture.generatePixmap( ext,
true );
542 _painter->drawPixmap( TQRect( 0, 0, ext.width(), ext.height() ), backPix );
545 backPix=backPicture.generatePixmap( _pixSize,
true );
546 _painter->drawTiledPixmap( 0, 0, ext.width(), ext.height(), backPix );
550 backPix=backPicture.generatePixmap( _pixSize,
true );
552 TQPixmap *pix =
new TQPixmap( ext.width(), ext.height() );
556 if ( backPix.width() > pix->width() && backPix.height() > pix->height() )
557 bitBlt( pix, 0, 0, &backPix, backPix.width() - pix->width(), backPix.height() - pix->height(),
558 pix->width(), pix->height() );
559 else if ( backPix.width() > pix->width() )
561 bitBlt( pix, 0, 0, &backPix, backPix.width() - pix->width(), 0,
562 pix->width(), backPix.height() );
563 _y = ( pix->height() - backPix.height() ) / 2;
565 else if ( backPix.height() > pix->height() )
567 bitBlt( pix, 0, 0, &backPix, 0, backPix.height() - pix->height(),
568 backPix.width(), pix->height() );
569 _x = ( pix->width() - backPix.width() ) / 2;
573 _x = ( pix->width() - backPix.width() ) / 2;
574 _y = ( pix->height() - backPix.height() ) / 2;
580 if ( pix && !pix->isNull() )
581 _painter->drawPixmap( _x, _y, *pix );
582 if ( delPix )
delete pix;
589void KPrBackGround::drawBorders( TQPainter *_painter,
const TQSize& ,
const TQRect& )
591 _painter->setPen( TQApplication::palette().active().color( TQColorGroup::Dark ) );
592 _painter->setBrush( TQt::NoBrush );
594 TQRect rect = m_page->getZoomPageRect();
596 _painter->drawRect( rect.x()-1,rect.y()-1,rect.width()+1,rect.height()+1 );
599void KPrBackGround::generateGradient(
const TQSize& size )
601 if ( backType == BT_COLOR || backType == BT_CLIPART ||
602 backType == BT_PICTURE && backView == BV_CENTER ) {
604 gradientPixmap = &gradientCollection()->getGradient( backColor1, backColor2, bcType, size,
605 unbalanced, xfactor, yfactor );
609 if ( (backType == BT_PICTURE || backType==BT_CLIPART) && gradientPixmap )
613void KPrBackGround::removeGradient()
615 if ( gradientPixmap ) {
616 gradientCollection()->removeRef( backColor1, backColor2, bcType, gradientPixmap->size(),
617 unbalanced, xfactor, yfactor);
622KoPictureCollection * KPrBackGround::pictureCollection()
const
624 return m_page->kPresenterDoc()->pictureCollection();
629 return m_page->kPresenterDoc()->gradientCollection();
632void KPrBackGround::setBackGround(
const struct Settings &settings )
634 backType = settings.m_backType;
635 backColor1 = settings.m_backColor1;
636 backColor2 = settings.m_backColor2;
637 bcType = settings.m_bcType;
638 unbalanced = settings.m_unbalanced;
639 xfactor = settings.m_xfactor;
640 yfactor = settings.m_yfactor;
642 setBackPicture( settings.m_backPictureKey );
643 backView = settings.m_backView;
647struct KPrBackGround::Settings KPrBackGround::getBackGround() const
649 return Settings( backType, backColor1, backColor2,
650 bcType, unbalanced, xfactor, yfactor,
651 backPicture.getKey(), backView);
Class: KPrGradientCollection Holds a collection of gradients, and is able to provide a gradient for a...