karbon

karbon_resourceserver.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, 2002, 2003 The Karbon Developers
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qdir.h>
00021 #include <qdom.h>
00022 #include <qfile.h>
00023 #include <qfileinfo.h>
00024 #include <qstringlist.h>
00025 
00026 #include <kdebug.h>
00027 #include <kglobal.h>
00028 #include <kinstance.h>
00029 #include <klocale.h>
00030 #include <kstandarddirs.h>
00031 #include <kiconloader.h>
00032 #include <kogradientmanager.h>
00033 
00034 #include "karbon_factory.h"
00035 #include "karbon_resourceserver.h"
00036 #include "vcomposite.h"
00037 #include "vgradient.h"
00038 #include "vgradienttabwidget.h"
00039 #include "vgroup.h"
00040 #include "vobject.h"
00041 #include "vtext.h"
00042 #include "vkopainter.h"
00043 #include "vtransformcmd.h"
00044 #include "shapes/vellipse.h"
00045 #include "shapes/vrectangle.h"
00046 #include "shapes/vsinus.h"
00047 #include "shapes/vspiral.h"
00048 #include "shapes/vstar.h"
00049 #include "shapes/vpolyline.h"
00050 #include "shapes/vpolygon.h"
00051 
00052 
00053 KarbonResourceServer::KarbonResourceServer()
00054 {
00055     kdDebug(38000) << "-- Karbon ResourceServer --" << endl;
00056 
00057     // PATTERNS
00058     kdDebug(38000) << "Loading patterns:" << endl;
00059     m_patterns.setAutoDelete( true );
00060 
00061     // image formats
00062     QStringList formats;
00063     formats << "*.png" << "*.tif" << "*.xpm" << "*.bmp" << "*.jpg" << "*.gif";
00064 
00065     // init vars
00066     QStringList lst;
00067     QString format, file;
00068 
00069     // find patterns
00070 
00071     for( QStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
00072     {
00073         format = *it;
00074         QStringList l = KarbonFactory::instance()->dirs()->findAllResources(
00075                             "kis_pattern", format, false, true );
00076         lst += l;
00077     }
00078 
00079     // load patterns
00080     for( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
00081     {
00082         file = *it;
00083         kdDebug(38000) << " - " << file << endl;
00084         loadPattern( file );
00085     }
00086 
00087     kdDebug(38000) << m_patterns.count() << " patterns loaded." << endl;
00088 
00089     // GRADIENTS
00090     kdDebug(38000) << "Loading gradients:" << endl;
00091     m_gradients = new QPtrList<VGradientListItem>();
00092     m_gradients->setAutoDelete( true );
00093 
00094     formats.clear();
00095     lst.clear();
00096     formats = KoGradientManager::filters();
00097 
00098     // find Gradients
00099 
00100     for( QStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
00101     {
00102         format = *it;
00103         QStringList l = KarbonFactory::instance()->dirs()->findAllResources(
00104                             "karbon_gradient", format, false, true );
00105         lst += l;
00106     }
00107 
00108     // load Gradients
00109     for( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
00110     {
00111         file = *it;
00112         kdDebug(38000) << " - " << file << endl;
00113         loadGradient( file );
00114     }
00115 
00116     kdDebug(38000) << m_gradients->count() << " gradients loaded." << endl;
00117 
00118     // CLIPARTS
00119     kdDebug(38000) << "Loading cliparts:" << endl;
00120     m_cliparts = new QPtrList<VClipartIconItem>();
00121     m_cliparts->setAutoDelete( true );
00122 
00123     formats.clear();
00124     lst.clear();
00125     formats << "*.kclp";
00126 
00127     // find cliparts
00128 
00129     for( QStringList::Iterator it = formats.begin(); it != formats.end(); ++it )
00130     {
00131         format = *it;
00132         QStringList l = KarbonFactory::instance()->dirs()->findAllResources(
00133                             "karbon_clipart", format, false, true );
00134         lst += l;
00135     }
00136 
00137     // load cliparts
00138     for( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it )
00139     {
00140         file = *it;
00141         kdDebug(38000) << " - " << file << endl;
00142         loadClipart( file );
00143     }
00144 
00145     m_pixmaps.setAutoDelete( true );
00146 
00147     kdDebug(38000) << m_cliparts->count() << " cliparts loaded." << endl;
00148 } // KarbonResourceServer::KarbonResourceServer
00149 
00150 KarbonResourceServer::~KarbonResourceServer()
00151 {
00152     m_patterns.clear();
00153     m_gradients->clear();
00154     delete m_gradients;
00155     m_cliparts->clear();
00156     delete m_cliparts;
00157 } // KarbonResourceServer::~KarbonResourceServer
00158 
00159 const VPattern*
00160 KarbonResourceServer::loadPattern( const QString& filename )
00161 {
00162     VPattern* pattern = new VPattern( filename );
00163 
00164     if( pattern->isValid() )
00165         m_patterns.append( pattern );
00166     else
00167     {
00168         delete pattern;
00169         pattern = 0L;
00170     }
00171 
00172     return pattern;
00173 }
00174 
00175 VPattern*
00176 KarbonResourceServer::addPattern( const QString& tilename )
00177 {
00178     int i = 1;
00179     QFileInfo fi;
00180     fi.setFile( tilename );
00181 
00182     if( fi.exists() == false )
00183         return 0L;
00184 
00185     QString name = fi.baseName();
00186 
00187     QString ext = '.' + fi.extension( false );
00188 
00189     QString filename = KarbonFactory::instance()->dirs()->saveLocation(
00190                            "kis_pattern" ) + name + ext;
00191 
00192     i = 1;
00193 
00194     fi.setFile( filename );
00195 
00196     while( fi.exists() == true )
00197     {
00198         filename = KarbonFactory::instance()->dirs()->saveLocation("kis_pattern" ) + name + i + ext;
00199         fi.setFile( filename );
00200         kdDebug(38000) << fi.fileName() << endl;
00201     }
00202 
00203     char buffer[ 1024 ];
00204     QFile in( tilename );
00205     in.open( IO_ReadOnly );
00206     QFile out( filename );
00207     out.open( IO_WriteOnly );
00208 
00209     while( !in.atEnd() )
00210         out.writeBlock( buffer, in.readBlock( buffer, 1024 ) );
00211 
00212     out.close();
00213     in.close();
00214 
00215     const VPattern* pattern = loadPattern( filename );
00216 
00217     if( pattern )
00218     {
00219         return static_cast<VPattern*>( m_patterns.last() );
00220     }
00221 
00222     return 0;
00223 } // KarbonResourceServer::addPattern
00224 
00225 void
00226 KarbonResourceServer::removePattern( VPattern* pattern )
00227 {
00228     QFile file( pattern->tilename() );
00229 
00230     if( file.remove() )
00231         m_patterns.remove( pattern );
00232 } // KarbonResourceServer::removePattern
00233 
00234 VGradientListItem*
00235 KarbonResourceServer::addGradient( VGradient* gradient )
00236 {
00237     int i = 1;
00238     char buffer[ 20 ];
00239     QFileInfo fi;
00240 
00241     sprintf( buffer, "%04d.kgr", i++ );
00242     fi.setFile( KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer );
00243 
00244     while( fi.exists() == true )
00245     {
00246         sprintf( buffer, "%04d.kgr", i++ );
00247         fi.setFile( KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer );
00248         kdDebug(38000) << fi.fileName() << endl;
00249     }
00250 
00251     QString filename = KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer;
00252 
00253     saveGradient( gradient, filename );
00254 
00255     m_gradients->append( new VGradientListItem( *gradient, filename ) );
00256 
00257     return m_gradients->last();
00258 } // KarbonResourceServer::addGradient
00259 
00260 void
00261 KarbonResourceServer::removeGradient( VGradientListItem* gradient )
00262 {
00263     QFile file( gradient->filename() );
00264 
00265     if( file.remove() )
00266         m_gradients->remove( gradient );
00267 } // KarbonResourceServer::removeGradient
00268 
00269 void
00270 KarbonResourceServer::loadGradient( const QString& filename )
00271 {
00272     KoGradientManager gradLoader;
00273     
00274     KoGradient* grad = gradLoader.loadGradient(filename);
00275     
00276     if( !grad )
00277         return;
00278 
00279     if( grad->colorStops.count() > 1 )
00280     {
00281         VGradient vgrad;
00282 
00283         vgrad.setOrigin(KoPoint(grad->originX, grad->originY));
00284         vgrad.setVector(KoPoint(grad->vectorX, grad->vectorY));
00285         vgrad.setFocalPoint(KoPoint(grad->focalpointX, grad->focalpointY));
00286         
00287         switch(grad->gradientType)
00288         {
00289             case KoGradientManager::gradient_type_linear:
00290                 vgrad.setType(VGradient::linear);
00291                 break;
00292             case KoGradientManager::gradient_type_radial:
00293                 vgrad.setType(VGradient::radial);
00294                 break;
00295             case KoGradientManager::gradient_type_conic:
00296                 vgrad.setType(VGradient::conic);
00297                 break;
00298             default: return;
00299         }
00300 
00301         switch(grad->gradientRepeatMethod)
00302         {
00303             case KoGradientManager::repeat_method_none:
00304                 vgrad.setRepeatMethod(VGradient::none);
00305                 break;
00306             case KoGradientManager::repeat_method_reflect:
00307                 vgrad.setRepeatMethod(VGradient::reflect);
00308                 break;
00309             case KoGradientManager::repeat_method_repeat:
00310                 vgrad.setRepeatMethod(VGradient::repeat);
00311                 break;
00312             default: return;
00313         }
00314 
00315         vgrad.clearStops();
00316 
00317         KoColorStop *colstop;
00318         for(colstop = grad->colorStops.first(); colstop; colstop = grad->colorStops.next())
00319         {
00320             VColor col;
00321 
00322             switch(colstop->colorType)
00323             {
00324                 case KoGradientManager::color_type_hsv_ccw:
00325                 case KoGradientManager::color_type_hsv_cw:
00326                     col.setColorSpace(VColor::hsb, false);
00327                     col.set(colstop->color1, colstop->color2, colstop->color3);
00328                     break;
00329                 case KoGradientManager::color_type_gray:
00330                     col.setColorSpace(VColor::gray, false);
00331                     col.set(colstop->color1);
00332                     break;
00333                 case KoGradientManager::color_type_cmyk:
00334                     col.setColorSpace(VColor::cmyk, false);
00335                     col.set(colstop->color1, colstop->color2, colstop->color3, colstop->color4);
00336                     break;
00337                 case KoGradientManager::color_type_rgb:
00338                 default:
00339                     col.set(colstop->color1, colstop->color2, colstop->color3);
00340             }
00341             col.setOpacity(colstop->opacity);
00342 
00343             vgrad.addStop(col, colstop->offset, colstop->midpoint);
00344         }
00345         m_gradients->append( new VGradientListItem( vgrad, filename ) );
00346     }
00347 } // KarbonResourceServer::loadGradient
00348 
00349 void
00350 KarbonResourceServer::saveGradient( VGradient* gradient, const QString& filename )
00351 {
00352     QFile file( filename );
00353     QDomDocument doc;
00354     QDomElement me = doc.createElement( "PREDEFGRADIENT" );
00355     doc.appendChild( me );
00356     gradient->save( me );
00357 
00358     if( !( file.open( IO_WriteOnly ) ) )
00359         return ;
00360 
00361     QTextStream ts( &file );
00362 
00363     doc.save( ts, 2 );
00364 
00365     file.flush();
00366 
00367     file.close();
00368 } // KarbonResourceServer::saveGradient
00369 
00370 VClipartIconItem*
00371 KarbonResourceServer::addClipart( VObject* clipart, double width, double height )
00372 {
00373     int i = 1;
00374     char buffer[ 20 ];
00375     sprintf( buffer, "%04d.kclp", i++ );
00376 
00377     while( KStandardDirs::exists( KarbonFactory::instance()->dirs()->saveLocation( "karbon_clipart" ) + buffer ) )
00378         sprintf( buffer, "%04d.kclp", i++ );
00379 
00380     QString filename = KarbonFactory::instance()->dirs()->saveLocation( "karbon_clipart" ) + buffer;
00381 
00382     saveClipart( clipart, width, height, filename );
00383 
00384     m_cliparts->append( new VClipartIconItem( clipart, width, height, filename ) );
00385 
00386     return m_cliparts->last();
00387 } // KarbonResourceServer::addClipart
00388 
00389 void
00390 KarbonResourceServer::removeClipart( VClipartIconItem* clipart )
00391 {
00392     QFile file( clipart->filename() );
00393 
00394     if( file.remove() )
00395         m_cliparts->remove
00396         ( clipart );
00397 }
00398 
00399 void
00400 KarbonResourceServer::loadClipart( const QString& filename )
00401 {
00402     QFile f( filename );
00403 
00404     if( f.open( IO_ReadOnly ) )
00405     {
00406         QDomDocument doc;
00407 
00408         if( !( doc.setContent( &f ) ) )
00409             f.close();
00410         else
00411         {
00412             QDomElement de = doc.documentElement();
00413 
00414             if( !de.isNull() && de.tagName() == "PREDEFCLIPART" )
00415             {
00416                 VObject* clipart = 0L;
00417                 double width = de.attribute( "width", "100.0" ).toFloat();
00418                 double height = de.attribute( "height", "100.0" ).toFloat();
00419 
00420                 QDomNode n = de.firstChild();
00421 
00422                 if( !n.isNull() )
00423                 {
00424                     QDomElement e;
00425                     e = n.toElement();
00426 
00427                     if( !e.isNull() )
00428                     {
00429                         if( e.tagName() == "TEXT" )
00430                             clipart = new VText( 0L );
00431                         else if( e.tagName() == "COMPOSITE" || e.tagName() == "PATH" )
00432                             clipart = new VPath( 0L );
00433                         else if( e.tagName() == "GROUP" )
00434                             clipart = new VGroup( 0L );
00435                         else if( e.tagName() == "ELLIPSE" )
00436                             clipart = new VEllipse( 0L );
00437                         else if( e.tagName() == "POLYGON" )
00438                             clipart = new VPolygon( 0L );
00439                         else if( e.tagName() == "POLYLINE" )
00440                             clipart = new VPolyline( 0L );
00441                         else if( e.tagName() == "RECT" )
00442                             clipart = new VRectangle( 0L );
00443                         else if( e.tagName() == "SINUS" )
00444                             clipart = new VSinus( 0L );
00445                         else if( e.tagName() == "SPIRAL" )
00446                             clipart = new VSpiral( 0L );
00447                         else if( e.tagName() == "STAR" )
00448                             clipart = new VStar( 0L );
00449 #ifdef HAVE_KARBONTEXT
00450                         else if( e.tagName() == "TEXT" )
00451                             clipart = new VText( 0L );
00452 #endif
00453                         if( clipart )
00454                             clipart->load( e );
00455                     }
00456 
00457                     if( clipart )
00458                         m_cliparts->append( new VClipartIconItem( clipart, width, height, filename ) );
00459 
00460                     delete clipart;
00461                 }
00462             }
00463         }
00464     }
00465 }
00466 
00467 void
00468 KarbonResourceServer::saveClipart( VObject* clipart, double width, double height, const QString& filename )
00469 {
00470     QFile file( filename );
00471     QDomDocument doc;
00472     QDomElement me = doc.createElement( "PREDEFCLIPART" );
00473     doc.appendChild( me );
00474     me.setAttribute( "width", width );
00475     me.setAttribute( "height", height );
00476     clipart->save( me );
00477 
00478     if( !( file.open( IO_WriteOnly ) ) )
00479         return ;
00480 
00481     QTextStream ts( &file );
00482 
00483     doc.save( ts, 2 );
00484 
00485     file.flush();
00486 
00487     file.close();
00488 }
00489 
00490 QPixmap *
00491 KarbonResourceServer::cachePixmap( const QString &key, int group_or_size )
00492 {
00493     QPixmap *result = 0L;
00494     if( !( result = m_pixmaps[ key ] ) )
00495     {
00496         result = new QPixmap( KGlobal::iconLoader()->iconPath( key, group_or_size ) );
00497         m_pixmaps.insert( key, result );
00498     }
00499     return result;
00500 }
00501 
00502 VClipartIconItem::VClipartIconItem( const VObject* clipart, double width, double height, QString filename )
00503         : m_filename( filename ), m_width( width ), m_height( height )
00504 {
00505     m_clipart = clipart->clone();
00506     m_clipart->setState( VObject::normal );
00507 
00508     m_pixmap.resize( 64, 64 );
00509     VKoPainter p( &m_pixmap, 64, 64 );
00510     QWMatrix mat( 64., 0, 0, 64., 0, 0 );
00511 
00512     VTransformCmd trafo( 0L, mat );
00513     trafo.visit( *m_clipart );
00514 
00515     m_clipart->draw( &p, &m_clipart->boundingBox() );
00516 
00517     trafo.setMatrix( mat.invert() );
00518     trafo.visit( *m_clipart );
00519 
00520     p.end();
00521 
00522     m_thumbPixmap.resize( 32, 32 );
00523     VKoPainter p2( &m_thumbPixmap, 32, 32 );
00524     mat.setMatrix( 32., 0, 0, 32., 0, 0 );
00525 
00526     trafo.setMatrix( mat );
00527     trafo.visit( *m_clipart );
00528 
00529     m_clipart->draw( &p2, &m_clipart->boundingBox() );
00530 
00531     trafo.setMatrix( mat.invert() );
00532     trafo.visit( *m_clipart );
00533 
00534     p2.end();
00535 
00536     validPixmap = true;
00537     validThumb = true;
00538 
00539     m_delete = QFileInfo( filename ).isWritable();
00540 }
00541 
00542 
00543 VClipartIconItem::VClipartIconItem( const VClipartIconItem& item )
00544         : KoIconItem( item )
00545 {
00546     m_clipart = item.m_clipart->clone();
00547     m_filename = item.m_filename;
00548     m_delete = item.m_delete;
00549     m_pixmap = item.m_pixmap;
00550     m_thumbPixmap = item.m_thumbPixmap;
00551     validPixmap = item.validPixmap;
00552     validThumb = item.validThumb;
00553     m_width = item.m_width;
00554     m_height = item.m_height;
00555 }
00556 
00557 VClipartIconItem::~VClipartIconItem()
00558 {
00559     delete m_clipart;
00560 }
00561 
00562 VClipartIconItem* VClipartIconItem::clone()
00563 {
00564     return new VClipartIconItem( *this );
00565 }
00566 
KDE Home | KDE Accessibility Home | Description of Access Keys