23#include <tqfileinfo.h>
24#include <tqstringlist.h>
28#include <tdeinstance.h>
30#include <kstandarddirs.h>
31#include <kiconloader.h>
32#include <kogradientmanager.h>
34#include "karbon_factory.h"
35#include "karbon_resourceserver.h"
36#include "vcomposite.h"
38#include "vgradienttabwidget.h"
42#include "vkopainter.h"
43#include "vtransformcmd.h"
44#include "shapes/vellipse.h"
45#include "shapes/vrectangle.h"
46#include "shapes/vsinus.h"
47#include "shapes/vspiral.h"
48#include "shapes/vstar.h"
49#include "shapes/vpolyline.h"
50#include "shapes/vpolygon.h"
53KarbonResourceServer::KarbonResourceServer()
55 kdDebug(38000) <<
"-- Karbon ResourceServer --" << endl;
58 kdDebug(38000) <<
"Loading patterns:" << endl;
59 m_patterns.setAutoDelete(
true );
63 formats <<
"*.png" <<
"*.tif" <<
"*.xpm" <<
"*.bmp" <<
"*.jpg" <<
"*.gif";
67 TQString format, file;
71 for( TQStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
74 TQStringList l = KarbonFactory::instance()->dirs()->findAllResources(
75 "kis_pattern", format,
false,
true );
80 for( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
83 kdDebug(38000) <<
" - " << file << endl;
87 kdDebug(38000) << m_patterns.count() <<
" patterns loaded." << endl;
90 kdDebug(38000) <<
"Loading gradients:" << endl;
91 m_gradients =
new TQPtrList<VGradientListItem>();
92 m_gradients->setAutoDelete(
true );
96 formats = KoGradientManager::filters();
100 for( TQStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
103 TQStringList l = KarbonFactory::instance()->dirs()->findAllResources(
104 "karbon_gradient", format,
false,
true );
109 for( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
112 kdDebug(38000) <<
" - " << file << endl;
113 loadGradient( file );
116 kdDebug(38000) << m_gradients->count() <<
" gradients loaded." << endl;
119 kdDebug(38000) <<
"Loading cliparts:" << endl;
120 m_cliparts =
new TQPtrList<VClipartIconItem>();
121 m_cliparts->setAutoDelete(
true );
129 for( TQStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
132 TQStringList l = KarbonFactory::instance()->dirs()->findAllResources(
133 "karbon_clipart", format,
false,
true );
138 for( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
141 kdDebug(38000) <<
" - " << file << endl;
145 m_pixmaps.setAutoDelete(
true );
147 kdDebug(38000) << m_cliparts->count() <<
" cliparts loaded." << endl;
150KarbonResourceServer::~KarbonResourceServer()
153 m_gradients->clear();
160KarbonResourceServer::loadPattern(
const TQString& filename )
162 VPattern* pattern =
new VPattern( filename );
164 if( pattern->isValid() )
165 m_patterns.append( pattern );
176KarbonResourceServer::addPattern(
const TQString& tilename )
180 fi.setFile( tilename );
182 if( fi.exists() ==
false )
185 TQString name = fi.baseName();
187 TQString ext =
'.' + fi.extension(
false );
189 TQString filename = KarbonFactory::instance()->dirs()->saveLocation(
190 "kis_pattern" ) + name + ext;
194 fi.setFile( filename );
196 while( fi.exists() ==
true )
198 filename = KarbonFactory::instance()->dirs()->saveLocation(
"kis_pattern" ) + name + i + ext;
199 fi.setFile( filename );
200 kdDebug(38000) << fi.fileName() << endl;
204 TQFile in( tilename );
205 in.open( IO_ReadOnly );
206 TQFile out( filename );
207 out.open( IO_WriteOnly );
210 out.writeBlock( buffer, in.readBlock( buffer, 1024 ) );
215 const VPattern* pattern = loadPattern( filename );
219 emit patternAdded( m_patterns.last() );
220 return static_cast<VPattern*
>( m_patterns.last() );
227KarbonResourceServer::removePattern( VPattern* pattern )
229 TQFile file( pattern->tilename() );
233 m_patterns.remove( pattern );
234 emit patternRemoved( pattern );
239KarbonResourceServer::addGradient( VGradient* gradient )
245 sprintf( buffer,
"%04d.kgr", i++ );
246 fi.setFile( KarbonFactory::instance()->dirs()->saveLocation(
"karbon_gradient" ) + buffer );
248 while( fi.exists() ==
true )
250 sprintf( buffer,
"%04d.kgr", i++ );
251 fi.setFile( KarbonFactory::instance()->dirs()->saveLocation(
"karbon_gradient" ) + buffer );
252 kdDebug(38000) << fi.fileName() << endl;
255 TQString filename = KarbonFactory::instance()->dirs()->saveLocation(
"karbon_gradient" ) + buffer;
257 saveGradient( gradient, filename );
259 m_gradients->append(
new VGradientListItem( *gradient, filename ) );
261 return m_gradients->last();
265KarbonResourceServer::removeGradient( VGradientListItem* gradient )
267 TQFile file( gradient->filename() );
270 m_gradients->remove( gradient );
274KarbonResourceServer::loadGradient(
const TQString& filename )
276 KoGradientManager gradLoader;
278 KoGradient* grad = gradLoader.loadGradient(filename);
283 if( grad->colorStops.count() > 1 )
287 vgrad.setOrigin(KoPoint(grad->originX, grad->originY));
288 vgrad.setVector(KoPoint(grad->vectorX, grad->vectorY));
289 vgrad.setFocalPoint(KoPoint(grad->focalpointX, grad->focalpointY));
291 switch(grad->gradientType)
293 case KoGradientManager::gradient_type_linear:
294 vgrad.setType(VGradient::linear);
296 case KoGradientManager::gradient_type_radial:
297 vgrad.setType(VGradient::radial);
299 case KoGradientManager::gradient_type_conic:
300 vgrad.setType(VGradient::conic);
305 switch(grad->gradientRepeatMethod)
307 case KoGradientManager::repeat_method_none:
308 vgrad.setRepeatMethod(VGradient::none);
310 case KoGradientManager::repeat_method_reflect:
311 vgrad.setRepeatMethod(VGradient::reflect);
313 case KoGradientManager::repeat_method_repeat:
314 vgrad.setRepeatMethod(VGradient::repeat);
321 KoColorStop *colstop;
322 for(colstop = grad->colorStops.first(); colstop; colstop = grad->colorStops.next())
326 switch(colstop->colorType)
328 case KoGradientManager::color_type_hsv_ccw:
329 case KoGradientManager::color_type_hsv_cw:
331 col.
set(colstop->color1, colstop->color2, colstop->color3);
333 case KoGradientManager::color_type_gray:
335 col.
set(colstop->color1);
337 case KoGradientManager::color_type_cmyk:
339 col.
set(colstop->color1, colstop->color2, colstop->color3, colstop->color4);
341 case KoGradientManager::color_type_rgb:
343 col.
set(colstop->color1, colstop->color2, colstop->color3);
347 vgrad.addStop(col, colstop->offset, colstop->midpoint);
349 m_gradients->append(
new VGradientListItem( vgrad, filename ) );
354KarbonResourceServer::saveGradient( VGradient* gradient,
const TQString& filename )
356 TQFile file( filename );
358 TQDomElement me = doc.createElement(
"PREDEFGRADIENT" );
359 doc.appendChild( me );
360 gradient->save( me );
362 if( !( file.open( IO_WriteOnly ) ) )
365 TQTextStream ts( &file );
375KarbonResourceServer::addClipart(
VObject* clipart,
double width,
double height )
379 sprintf( buffer,
"%04d.kclp", i++ );
381 while( TDEStandardDirs::exists( KarbonFactory::instance()->dirs()->saveLocation(
"karbon_clipart" ) + buffer ) )
382 sprintf( buffer,
"%04d.kclp", i++ );
384 TQString filename = KarbonFactory::instance()->dirs()->saveLocation(
"karbon_clipart" ) + buffer;
386 saveClipart( clipart, width, height, filename );
388 m_cliparts->append(
new VClipartIconItem( clipart, width, height, filename ) );
390 return m_cliparts->last();
394KarbonResourceServer::removeClipart( VClipartIconItem* clipart )
396 TQFile file( clipart->filename() );
404KarbonResourceServer::loadClipart(
const TQString& filename )
406 TQFile f( filename );
408 if( f.open( IO_ReadOnly ) )
412 if( !( doc.setContent( &f ) ) )
416 TQDomElement de = doc.documentElement();
418 if( !de.isNull() && de.tagName() ==
"PREDEFCLIPART" )
420 VObject* clipart = 0L;
421 double width = de.attribute(
"width",
"100.0" ).toFloat();
422 double height = de.attribute(
"height",
"100.0" ).toFloat();
424 TQDomNode n = de.firstChild();
433 if( e.tagName() ==
"TEXT" )
434 clipart =
new VText( 0L );
435 else if( e.tagName() ==
"COMPOSITE" || e.tagName() ==
"PATH" )
436 clipart =
new VPath( 0L );
437 else if( e.tagName() ==
"GROUP" )
438 clipart =
new VGroup( 0L );
439 else if( e.tagName() ==
"ELLIPSE" )
440 clipart =
new VEllipse( 0L );
441 else if( e.tagName() ==
"POLYGON" )
442 clipart =
new VPolygon( 0L );
443 else if( e.tagName() ==
"POLYLINE" )
444 clipart =
new VPolyline( 0L );
445 else if( e.tagName() ==
"RECT" )
446 clipart =
new VRectangle( 0L );
447 else if( e.tagName() ==
"SINUS" )
448 clipart =
new VSinus( 0L );
449 else if( e.tagName() ==
"SPIRAL" )
450 clipart =
new VSpiral( 0L );
451 else if( e.tagName() ==
"STAR" )
452 clipart =
new VStar( 0L );
453#ifdef HAVE_KARBONTEXT
454 else if( e.tagName() ==
"TEXT" )
455 clipart =
new VText( 0L );
462 m_cliparts->append(
new VClipartIconItem( clipart, width, height, filename ) );
472KarbonResourceServer::saveClipart(
VObject* clipart,
double width,
double height,
const TQString& filename )
474 TQFile file( filename );
476 TQDomElement me = doc.createElement(
"PREDEFCLIPART" );
477 doc.appendChild( me );
478 me.setAttribute(
"width", width );
479 me.setAttribute(
"height", height );
482 if( !( file.open( IO_WriteOnly ) ) )
485 TQTextStream ts( &file );
495KarbonResourceServer::cachePixmap(
const TQString &key,
int group_or_size )
497 TQPixmap *result = 0L;
498 if( !( result = m_pixmaps[ key ] ) )
500 result =
new TQPixmap( TDEGlobal::iconLoader()->iconPath( key, group_or_size ) );
501 m_pixmaps.insert( key, result );
506VClipartIconItem::VClipartIconItem(
const VObject* clipart,
double width,
double height, TQString filename )
507 : m_filename( filename ), m_width( width ), m_height( height )
509 m_clipart = clipart->
clone();
512 m_pixmap.resize( 64, 64 );
513 VKoPainter p( &m_pixmap, 64, 64 );
514 TQWMatrix mat( 64., 0, 0, 64., 0, 0 );
516 VTransformCmd trafo( 0L, mat );
517 trafo.visit( *m_clipart );
519 m_clipart->draw( &p, &m_clipart->boundingBox() );
521 trafo.setMatrix( mat.invert() );
522 trafo.visit( *m_clipart );
526 m_thumbPixmap.resize( 32, 32 );
527 VKoPainter p2( &m_thumbPixmap, 32, 32 );
528 mat.setMatrix( 32., 0, 0, 32., 0, 0 );
530 trafo.setMatrix( mat );
531 trafo.visit( *m_clipart );
533 m_clipart->draw( &p2, &m_clipart->boundingBox() );
535 trafo.setMatrix( mat.invert() );
536 trafo.visit( *m_clipart );
543 m_delete = TQFileInfo( filename ).isWritable();
547VClipartIconItem::VClipartIconItem(
const VClipartIconItem& item )
550 m_clipart = item.m_clipart->
clone();
551 m_filename = item.m_filename;
552 m_delete = item.m_delete;
553 m_pixmap = item.m_pixmap;
554 m_thumbPixmap = item.m_thumbPixmap;
555 validPixmap = item.validPixmap;
556 validThumb = item.validThumb;
557 m_width = item.m_width;
558 m_height = item.m_height;
561VClipartIconItem::~VClipartIconItem()
566VClipartIconItem* VClipartIconItem::clone()
568 return new VClipartIconItem( *
this );
571#include "karbon_resourceserver.moc"
void set(float v1)
Sets the first color component.
void setOpacity(float opacity)
Sets the color opacity.
void setColorSpace(const VColorSpace colorSpace, bool convert=true)
Sets the color's colorspace.
@ gray
the Gray colorspace (gray from black to white)
@ cmyk
the CMYK colorspace (cyan, magenta, yellow and black components)
@ hsb
the HSB colorspace (hue, saturation and brightnes components)
The base class for all karbon objects.
virtual VObject * clone() const =0
Create an exact copy of this object.
virtual void setState(const VState state)
Sets the state to a specified new state.
@ normal
visible, not active
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
virtual void save(TQDomElement &element) const
Save this object's state to xml.