karbon

karbon_resourceserver.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
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
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqdir.h>
21#include <tqdom.h>
22#include <tqfile.h>
23#include <tqfileinfo.h>
24#include <tqstringlist.h>
25
26#include <kdebug.h>
27#include <tdeglobal.h>
28#include <tdeinstance.h>
29#include <tdelocale.h>
30#include <kstandarddirs.h>
31#include <kiconloader.h>
32#include <kogradientmanager.h>
33
34#include "karbon_factory.h"
35#include "karbon_resourceserver.h"
36#include "vcomposite.h"
37#include "vgradient.h"
38#include "vgradienttabwidget.h"
39#include "vgroup.h"
40#include "vobject.h"
41#include "vtext.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"
51
52
53KarbonResourceServer::KarbonResourceServer()
54{
55 kdDebug(38000) << "-- Karbon ResourceServer --" << endl;
56
57 // PATTERNS
58 kdDebug(38000) << "Loading patterns:" << endl;
59 m_patterns.setAutoDelete( true );
60
61 // image formats
62 TQStringList formats;
63 formats << "*.png" << "*.tif" << "*.xpm" << "*.bmp" << "*.jpg" << "*.gif";
64
65 // init vars
66 TQStringList lst;
67 TQString format, file;
68
69 // find patterns
70
71 for( TQStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
72 {
73 format = *it;
74 TQStringList l = KarbonFactory::instance()->dirs()->findAllResources(
75 "kis_pattern", format, false, true );
76 lst += l;
77 }
78
79 // load patterns
80 for( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
81 {
82 file = *it;
83 kdDebug(38000) << " - " << file << endl;
84 loadPattern( file );
85 }
86
87 kdDebug(38000) << m_patterns.count() << " patterns loaded." << endl;
88
89 // GRADIENTS
90 kdDebug(38000) << "Loading gradients:" << endl;
91 m_gradients = new TQPtrList<VGradientListItem>();
92 m_gradients->setAutoDelete( true );
93
94 formats.clear();
95 lst.clear();
96 formats = KoGradientManager::filters();
97
98 // find Gradients
99
100 for( TQStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
101 {
102 format = *it;
103 TQStringList l = KarbonFactory::instance()->dirs()->findAllResources(
104 "karbon_gradient", format, false, true );
105 lst += l;
106 }
107
108 // load Gradients
109 for( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
110 {
111 file = *it;
112 kdDebug(38000) << " - " << file << endl;
113 loadGradient( file );
114 }
115
116 kdDebug(38000) << m_gradients->count() << " gradients loaded." << endl;
117
118 // CLIPARTS
119 kdDebug(38000) << "Loading cliparts:" << endl;
120 m_cliparts = new TQPtrList<VClipartIconItem>();
121 m_cliparts->setAutoDelete( true );
122
123 formats.clear();
124 lst.clear();
125 formats << "*.kclp";
126
127 // find cliparts
128
129 for( TQStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
130 {
131 format = *it;
132 TQStringList l = KarbonFactory::instance()->dirs()->findAllResources(
133 "karbon_clipart", format, false, true );
134 lst += l;
135 }
136
137 // load cliparts
138 for( TQStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
139 {
140 file = *it;
141 kdDebug(38000) << " - " << file << endl;
142 loadClipart( file );
143 }
144
145 m_pixmaps.setAutoDelete( true );
146
147 kdDebug(38000) << m_cliparts->count() << " cliparts loaded." << endl;
148} // KarbonResourceServer::KarbonResourceServer
149
150KarbonResourceServer::~KarbonResourceServer()
151{
152 m_patterns.clear();
153 m_gradients->clear();
154 delete m_gradients;
155 m_cliparts->clear();
156 delete m_cliparts;
157} // KarbonResourceServer::~KarbonResourceServer
158
159const VPattern*
160KarbonResourceServer::loadPattern( const TQString& filename )
161{
162 VPattern* pattern = new VPattern( filename );
163
164 if( pattern->isValid() )
165 m_patterns.append( pattern );
166 else
167 {
168 delete pattern;
169 pattern = 0L;
170 }
171
172 return pattern;
173}
174
175VPattern*
176KarbonResourceServer::addPattern( const TQString& tilename )
177{
178 int i = 1;
179 TQFileInfo fi;
180 fi.setFile( tilename );
181
182 if( fi.exists() == false )
183 return 0L;
184
185 TQString name = fi.baseName();
186
187 TQString ext = '.' + fi.extension( false );
188
189 TQString filename = KarbonFactory::instance()->dirs()->saveLocation(
190 "kis_pattern" ) + name + ext;
191
192 i = 1;
193
194 fi.setFile( filename );
195
196 while( fi.exists() == true )
197 {
198 filename = KarbonFactory::instance()->dirs()->saveLocation("kis_pattern" ) + name + i + ext;
199 fi.setFile( filename );
200 kdDebug(38000) << fi.fileName() << endl;
201 }
202
203 char buffer[ 1024 ];
204 TQFile in( tilename );
205 in.open( IO_ReadOnly );
206 TQFile out( filename );
207 out.open( IO_WriteOnly );
208
209 while( !in.atEnd() )
210 out.writeBlock( buffer, in.readBlock( buffer, 1024 ) );
211
212 out.close();
213 in.close();
214
215 const VPattern* pattern = loadPattern( filename );
216
217 if( pattern )
218 {
219 emit patternAdded( m_patterns.last() );
220 return static_cast<VPattern*>( m_patterns.last() );
221 }
222
223 return 0;
224} // KarbonResourceServer::addPattern
225
226void
227KarbonResourceServer::removePattern( VPattern* pattern )
228{
229 TQFile file( pattern->tilename() );
230
231 if( file.remove() )
232 {
233 m_patterns.remove( pattern );
234 emit patternRemoved( pattern );
235 }
236} // KarbonResourceServer::removePattern
237
238VGradientListItem*
239KarbonResourceServer::addGradient( VGradient* gradient )
240{
241 int i = 1;
242 char buffer[ 20 ];
243 TQFileInfo fi;
244
245 sprintf( buffer, "%04d.kgr", i++ );
246 fi.setFile( KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer );
247
248 while( fi.exists() == true )
249 {
250 sprintf( buffer, "%04d.kgr", i++ );
251 fi.setFile( KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer );
252 kdDebug(38000) << fi.fileName() << endl;
253 }
254
255 TQString filename = KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer;
256
257 saveGradient( gradient, filename );
258
259 m_gradients->append( new VGradientListItem( *gradient, filename ) );
260
261 return m_gradients->last();
262} // KarbonResourceServer::addGradient
263
264void
265KarbonResourceServer::removeGradient( VGradientListItem* gradient )
266{
267 TQFile file( gradient->filename() );
268
269 if( file.remove() )
270 m_gradients->remove( gradient );
271} // KarbonResourceServer::removeGradient
272
273void
274KarbonResourceServer::loadGradient( const TQString& filename )
275{
276 KoGradientManager gradLoader;
277
278 KoGradient* grad = gradLoader.loadGradient(filename);
279
280 if( !grad )
281 return;
282
283 if( grad->colorStops.count() > 1 )
284 {
285 VGradient vgrad;
286
287 vgrad.setOrigin(KoPoint(grad->originX, grad->originY));
288 vgrad.setVector(KoPoint(grad->vectorX, grad->vectorY));
289 vgrad.setFocalPoint(KoPoint(grad->focalpointX, grad->focalpointY));
290
291 switch(grad->gradientType)
292 {
293 case KoGradientManager::gradient_type_linear:
294 vgrad.setType(VGradient::linear);
295 break;
296 case KoGradientManager::gradient_type_radial:
297 vgrad.setType(VGradient::radial);
298 break;
299 case KoGradientManager::gradient_type_conic:
300 vgrad.setType(VGradient::conic);
301 break;
302 default: return;
303 }
304
305 switch(grad->gradientRepeatMethod)
306 {
307 case KoGradientManager::repeat_method_none:
308 vgrad.setRepeatMethod(VGradient::none);
309 break;
310 case KoGradientManager::repeat_method_reflect:
311 vgrad.setRepeatMethod(VGradient::reflect);
312 break;
313 case KoGradientManager::repeat_method_repeat:
314 vgrad.setRepeatMethod(VGradient::repeat);
315 break;
316 default: return;
317 }
318
319 vgrad.clearStops();
320
321 KoColorStop *colstop;
322 for(colstop = grad->colorStops.first(); colstop; colstop = grad->colorStops.next())
323 {
324 VColor col;
325
326 switch(colstop->colorType)
327 {
328 case KoGradientManager::color_type_hsv_ccw:
329 case KoGradientManager::color_type_hsv_cw:
330 col.setColorSpace(VColor::hsb, false);
331 col.set(colstop->color1, colstop->color2, colstop->color3);
332 break;
333 case KoGradientManager::color_type_gray:
334 col.setColorSpace(VColor::gray, false);
335 col.set(colstop->color1);
336 break;
337 case KoGradientManager::color_type_cmyk:
338 col.setColorSpace(VColor::cmyk, false);
339 col.set(colstop->color1, colstop->color2, colstop->color3, colstop->color4);
340 break;
341 case KoGradientManager::color_type_rgb:
342 default:
343 col.set(colstop->color1, colstop->color2, colstop->color3);
344 }
345 col.setOpacity(colstop->opacity);
346
347 vgrad.addStop(col, colstop->offset, colstop->midpoint);
348 }
349 m_gradients->append( new VGradientListItem( vgrad, filename ) );
350 }
351} // KarbonResourceServer::loadGradient
352
353void
354KarbonResourceServer::saveGradient( VGradient* gradient, const TQString& filename )
355{
356 TQFile file( filename );
357 TQDomDocument doc;
358 TQDomElement me = doc.createElement( "PREDEFGRADIENT" );
359 doc.appendChild( me );
360 gradient->save( me );
361
362 if( !( file.open( IO_WriteOnly ) ) )
363 return ;
364
365 TQTextStream ts( &file );
366
367 doc.save( ts, 2 );
368
369 file.flush();
370
371 file.close();
372} // KarbonResourceServer::saveGradient
373
374VClipartIconItem*
375KarbonResourceServer::addClipart( VObject* clipart, double width, double height )
376{
377 int i = 1;
378 char buffer[ 20 ];
379 sprintf( buffer, "%04d.kclp", i++ );
380
381 while( TDEStandardDirs::exists( KarbonFactory::instance()->dirs()->saveLocation( "karbon_clipart" ) + buffer ) )
382 sprintf( buffer, "%04d.kclp", i++ );
383
384 TQString filename = KarbonFactory::instance()->dirs()->saveLocation( "karbon_clipart" ) + buffer;
385
386 saveClipart( clipart, width, height, filename );
387
388 m_cliparts->append( new VClipartIconItem( clipart, width, height, filename ) );
389
390 return m_cliparts->last();
391} // KarbonResourceServer::addClipart
392
393void
394KarbonResourceServer::removeClipart( VClipartIconItem* clipart )
395{
396 TQFile file( clipart->filename() );
397
398 if( file.remove() )
399 m_cliparts->remove
400 ( clipart );
401}
402
403void
404KarbonResourceServer::loadClipart( const TQString& filename )
405{
406 TQFile f( filename );
407
408 if( f.open( IO_ReadOnly ) )
409 {
410 TQDomDocument doc;
411
412 if( !( doc.setContent( &f ) ) )
413 f.close();
414 else
415 {
416 TQDomElement de = doc.documentElement();
417
418 if( !de.isNull() && de.tagName() == "PREDEFCLIPART" )
419 {
420 VObject* clipart = 0L;
421 double width = de.attribute( "width", "100.0" ).toFloat();
422 double height = de.attribute( "height", "100.0" ).toFloat();
423
424 TQDomNode n = de.firstChild();
425
426 if( !n.isNull() )
427 {
428 TQDomElement e;
429 e = n.toElement();
430
431 if( !e.isNull() )
432 {
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 );
456#endif
457 if( clipart )
458 clipart->load( e );
459 }
460
461 if( clipart )
462 m_cliparts->append( new VClipartIconItem( clipart, width, height, filename ) );
463
464 delete clipart;
465 }
466 }
467 }
468 }
469}
470
471void
472KarbonResourceServer::saveClipart( VObject* clipart, double width, double height, const TQString& filename )
473{
474 TQFile file( filename );
475 TQDomDocument doc;
476 TQDomElement me = doc.createElement( "PREDEFCLIPART" );
477 doc.appendChild( me );
478 me.setAttribute( "width", width );
479 me.setAttribute( "height", height );
480 clipart->save( me );
481
482 if( !( file.open( IO_WriteOnly ) ) )
483 return ;
484
485 TQTextStream ts( &file );
486
487 doc.save( ts, 2 );
488
489 file.flush();
490
491 file.close();
492}
493
494TQPixmap *
495KarbonResourceServer::cachePixmap( const TQString &key, int group_or_size )
496{
497 TQPixmap *result = 0L;
498 if( !( result = m_pixmaps[ key ] ) )
499 {
500 result = new TQPixmap( TDEGlobal::iconLoader()->iconPath( key, group_or_size ) );
501 m_pixmaps.insert( key, result );
502 }
503 return result;
504}
505
506VClipartIconItem::VClipartIconItem( const VObject* clipart, double width, double height, TQString filename )
507 : m_filename( filename ), m_width( width ), m_height( height )
508{
509 m_clipart = clipart->clone();
510 m_clipart->setState( VObject::normal );
511
512 m_pixmap.resize( 64, 64 );
513 VKoPainter p( &m_pixmap, 64, 64 );
514 TQWMatrix mat( 64., 0, 0, 64., 0, 0 );
515
516 VTransformCmd trafo( 0L, mat );
517 trafo.visit( *m_clipart );
518
519 m_clipart->draw( &p, &m_clipart->boundingBox() );
520
521 trafo.setMatrix( mat.invert() );
522 trafo.visit( *m_clipart );
523
524 p.end();
525
526 m_thumbPixmap.resize( 32, 32 );
527 VKoPainter p2( &m_thumbPixmap, 32, 32 );
528 mat.setMatrix( 32., 0, 0, 32., 0, 0 );
529
530 trafo.setMatrix( mat );
531 trafo.visit( *m_clipart );
532
533 m_clipart->draw( &p2, &m_clipart->boundingBox() );
534
535 trafo.setMatrix( mat.invert() );
536 trafo.visit( *m_clipart );
537
538 p2.end();
539
540 validPixmap = true;
541 validThumb = true;
542
543 m_delete = TQFileInfo( filename ).isWritable();
544}
545
546
547VClipartIconItem::VClipartIconItem( const VClipartIconItem& item )
548 : KoIconItem( item )
549{
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;
559}
560
561VClipartIconItem::~VClipartIconItem()
562{
563 delete m_clipart;
564}
565
566VClipartIconItem* VClipartIconItem::clone()
567{
568 return new VClipartIconItem( *this );
569}
570
571#include "karbon_resourceserver.moc"
This class keeps track of color properties.
Definition vcolor.h:41
void set(float v1)
Sets the first color component.
Definition vcolor.h:91
void setOpacity(float opacity)
Sets the color opacity.
Definition vcolor.h:138
void setColorSpace(const VColorSpace colorSpace, bool convert=true)
Sets the color's colorspace.
Definition vcolor.cpp:72
@ gray
the Gray colorspace (gray from black to white)
Definition vcolor.h:48
@ cmyk
the CMYK colorspace (cyan, magenta, yellow and black components)
Definition vcolor.h:46
@ hsb
the HSB colorspace (hue, saturation and brightnes components)
Definition vcolor.h:47
Base class for all sort of VObject conglomerats.
Definition vgroup.h:35
The base class for all karbon objects.
Definition vobject.h:50
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.
Definition vobject.h:163
@ normal
visible, not active
Definition vobject.h:54
virtual void load(const TQDomElement &element)
Load this object's state from xml and initialize this object accordingly.
Definition vobject.cpp:157
virtual void save(TQDomElement &element) const
Save this object's state to xml.
Definition vobject.cpp:102
A composite path consists of one or many subpaths.
Definition vcomposite.h:48
This shape offers star-like shapes with a lot of parameters :
Definition vstar.h:48