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         emit patternAdded( m_patterns.last() );
00220         return static_cast<VPattern*>( m_patterns.last() );
00221     }
00222 
00223     return 0;
00224 } // KarbonResourceServer::addPattern
00225 
00226 void
00227 KarbonResourceServer::removePattern( VPattern* pattern )
00228 {
00229     QFile file( pattern->tilename() );
00230 
00231     if( file.remove() )
00232     {
00233         m_patterns.remove( pattern );
00234         emit patternRemoved( pattern );
00235     }
00236 } // KarbonResourceServer::removePattern
00237 
00238 VGradientListItem*
00239 KarbonResourceServer::addGradient( VGradient* gradient )
00240 {
00241     int i = 1;
00242     char buffer[ 20 ];
00243     QFileInfo fi;
00244 
00245     sprintf( buffer, "%04d.kgr", i++ );
00246     fi.setFile( KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer );
00247 
00248     while( fi.exists() == true )
00249     {
00250         sprintf( buffer, "%04d.kgr", i++ );
00251         fi.setFile( KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer );
00252         kdDebug(38000) << fi.fileName() << endl;
00253     }
00254 
00255     QString filename = KarbonFactory::instance()->dirs()->saveLocation( "karbon_gradient" ) + buffer;
00256 
00257     saveGradient( gradient, filename );
00258 
00259     m_gradients->append( new VGradientListItem( *gradient, filename ) );
00260 
00261     return m_gradients->last();
00262 } // KarbonResourceServer::addGradient
00263 
00264 void
00265 KarbonResourceServer::removeGradient( VGradientListItem* gradient )
00266 {
00267     QFile file( gradient->filename() );
00268 
00269     if( file.remove() )
00270         m_gradients->remove( gradient );
00271 } // KarbonResourceServer::removeGradient
00272 
00273 void
00274 KarbonResourceServer::loadGradient( const QString& filename )
00275 {
00276     KoGradientManager gradLoader;
00277     
00278     KoGradient* grad = gradLoader.loadGradient(filename);
00279     
00280     if( !grad )
00281         return;
00282 
00283     if( grad->colorStops.count() > 1 )
00284     {
00285         VGradient vgrad;
00286 
00287         vgrad.setOrigin(KoPoint(grad->originX, grad->originY));
00288         vgrad.setVector(KoPoint(grad->vectorX, grad->vectorY));
00289         vgrad.setFocalPoint(KoPoint(grad->focalpointX, grad->focalpointY));
00290         
00291         switch(grad->gradientType)
00292         {
00293             case KoGradientManager::gradient_type_linear:
00294                 vgrad.setType(VGradient::linear);
00295                 break;
00296             case KoGradientManager::gradient_type_radial:
00297                 vgrad.setType(VGradient::radial);
00298                 break;
00299             case KoGradientManager::gradient_type_conic:
00300                 vgrad.setType(VGradient::conic);
00301                 break;
00302             default: return;
00303         }
00304 
00305         switch(grad->gradientRepeatMethod)
00306         {
00307             case KoGradientManager::repeat_method_none:
00308                 vgrad.setRepeatMethod(VGradient::none);
00309                 break;
00310             case KoGradientManager::repeat_method_reflect:
00311                 vgrad.setRepeatMethod(VGradient::reflect);
00312                 break;
00313             case KoGradientManager::repeat_method_repeat:
00314                 vgrad.setRepeatMethod(VGradient::repeat);
00315                 break;
00316             default: return;
00317         }
00318 
00319         vgrad.clearStops();
00320 
00321         KoColorStop *colstop;
00322         for(colstop = grad->colorStops.first(); colstop; colstop = grad->colorStops.next())
00323         {
00324             VColor col;
00325 
00326             switch(colstop->colorType)
00327             {
00328                 case KoGradientManager::color_type_hsv_ccw:
00329                 case KoGradientManager::color_type_hsv_cw:
00330                     col.setColorSpace(VColor::hsb, false);
00331                     col.set(colstop->color1, colstop->color2, colstop->color3);
00332                     break;
00333                 case KoGradientManager::color_type_gray:
00334                     col.setColorSpace(VColor::gray, false);
00335                     col.set(colstop->color1);
00336                     break;
00337                 case KoGradientManager::color_type_cmyk:
00338                     col.setColorSpace(VColor::cmyk, false);
00339                     col.set(colstop->color1, colstop->color2, colstop->color3, colstop->color4);
00340                     break;
00341                 case KoGradientManager::color_type_rgb:
00342                 default:
00343                     col.set(colstop->color1, colstop->color2, colstop->color3);
00344             }
00345             col.setOpacity(colstop->opacity);
00346 
00347             vgrad.addStop(col, colstop->offset, colstop->midpoint);
00348         }
00349         m_gradients->append( new VGradientListItem( vgrad, filename ) );
00350     }
00351 } // KarbonResourceServer::loadGradient
00352 
00353 void
00354 KarbonResourceServer::saveGradient( VGradient* gradient, const QString& filename )
00355 {
00356     QFile file( filename );
00357     QDomDocument doc;
00358     QDomElement me = doc.createElement( "PREDEFGRADIENT" );
00359     doc.appendChild( me );
00360     gradient->save( me );
00361 
00362     if( !( file.open( IO_WriteOnly ) ) )
00363         return ;
00364 
00365     QTextStream ts( &file );
00366 
00367     doc.save( ts, 2 );
00368 
00369     file.flush();
00370 
00371     file.close();
00372 } // KarbonResourceServer::saveGradient
00373 
00374 VClipartIconItem*
00375 KarbonResourceServer::addClipart( VObject* clipart, double width, double height )
00376 {
00377     int i = 1;
00378     char buffer[ 20 ];
00379     sprintf( buffer, "%04d.kclp", i++ );
00380 
00381     while( KStandardDirs::exists( KarbonFactory::instance()->dirs()->saveLocation( "karbon_clipart" ) + buffer ) )
00382         sprintf( buffer, "%04d.kclp", i++ );
00383 
00384     QString filename = KarbonFactory::instance()->dirs()->saveLocation( "karbon_clipart" ) + buffer;
00385 
00386     saveClipart( clipart, width, height, filename );
00387 
00388     m_cliparts->append( new VClipartIconItem( clipart, width, height, filename ) );
00389 
00390     return m_cliparts->last();
00391 } // KarbonResourceServer::addClipart
00392 
00393 void
00394 KarbonResourceServer::removeClipart( VClipartIconItem* clipart )
00395 {
00396     QFile file( clipart->filename() );
00397 
00398     if( file.remove() )
00399         m_cliparts->remove
00400         ( clipart );
00401 }
00402 
00403 void
00404 KarbonResourceServer::loadClipart( const QString& filename )
00405 {
00406     QFile f( filename );
00407 
00408     if( f.open( IO_ReadOnly ) )
00409     {
00410         QDomDocument doc;
00411 
00412         if( !( doc.setContent( &f ) ) )
00413             f.close();
00414         else
00415         {
00416             QDomElement de = doc.documentElement();
00417 
00418             if( !de.isNull() && de.tagName() == "PREDEFCLIPART" )
00419             {
00420                 VObject* clipart = 0L;
00421                 double width = de.attribute( "width", "100.0" ).toFloat();
00422                 double height = de.attribute( "height", "100.0" ).toFloat();
00423 
00424                 QDomNode n = de.firstChild();
00425 
00426                 if( !n.isNull() )
00427                 {
00428                     QDomElement e;
00429                     e = n.toElement();
00430 
00431                     if( !e.isNull() )
00432                     {
00433                         if( e.tagName() == "TEXT" )
00434                             clipart = new VText( 0L );
00435                         else if( e.tagName() == "COMPOSITE" || e.tagName() == "PATH" )
00436                             clipart = new VPath( 0L );
00437                         else if( e.tagName() == "GROUP" )
00438                             clipart = new VGroup( 0L );
00439                         else if( e.tagName() == "ELLIPSE" )
00440                             clipart = new VEllipse( 0L );
00441                         else if( e.tagName() == "POLYGON" )
00442                             clipart = new VPolygon( 0L );
00443                         else if( e.tagName() == "POLYLINE" )
00444                             clipart = new VPolyline( 0L );
00445                         else if( e.tagName() == "RECT" )
00446                             clipart = new VRectangle( 0L );
00447                         else if( e.tagName() == "SINUS" )
00448                             clipart = new VSinus( 0L );
00449                         else if( e.tagName() == "SPIRAL" )
00450                             clipart = new VSpiral( 0L );
00451                         else if( e.tagName() == "STAR" )
00452                             clipart = new VStar( 0L );
00453 #ifdef HAVE_KARBONTEXT
00454                         else if( e.tagName() == "TEXT" )
00455                             clipart = new VText( 0L );
00456 #endif
00457                         if( clipart )
00458                             clipart->load( e );
00459                     }
00460 
00461                     if( clipart )
00462                         m_cliparts->append( new VClipartIconItem( clipart, width, height, filename ) );
00463 
00464                     delete clipart;
00465                 }
00466             }
00467         }
00468     }
00469 }
00470 
00471 void
00472 KarbonResourceServer::saveClipart( VObject* clipart, double width, double height, const QString& filename )
00473 {
00474     QFile file( filename );
00475     QDomDocument doc;
00476     QDomElement me = doc.createElement( "PREDEFCLIPART" );
00477     doc.appendChild( me );
00478     me.setAttribute( "width", width );
00479     me.setAttribute( "height", height );
00480     clipart->save( me );
00481 
00482     if( !( file.open( IO_WriteOnly ) ) )
00483         return ;
00484 
00485     QTextStream ts( &file );
00486 
00487     doc.save( ts, 2 );
00488 
00489     file.flush();
00490 
00491     file.close();
00492 }
00493 
00494 QPixmap *
00495 KarbonResourceServer::cachePixmap( const QString &key, int group_or_size )
00496 {
00497     QPixmap *result = 0L;
00498     if( !( result = m_pixmaps[ key ] ) )
00499     {
00500         result = new QPixmap( KGlobal::iconLoader()->iconPath( key, group_or_size ) );
00501         m_pixmaps.insert( key, result );
00502     }
00503     return result;
00504 }
00505 
00506 VClipartIconItem::VClipartIconItem( const VObject* clipart, double width, double height, QString filename )
00507         : m_filename( filename ), m_width( width ), m_height( height )
00508 {
00509     m_clipart = clipart->clone();
00510     m_clipart->setState( VObject::normal );
00511 
00512     m_pixmap.resize( 64, 64 );
00513     VKoPainter p( &m_pixmap, 64, 64 );
00514     QWMatrix mat( 64., 0, 0, 64., 0, 0 );
00515 
00516     VTransformCmd trafo( 0L, mat );
00517     trafo.visit( *m_clipart );
00518 
00519     m_clipart->draw( &p, &m_clipart->boundingBox() );
00520 
00521     trafo.setMatrix( mat.invert() );
00522     trafo.visit( *m_clipart );
00523 
00524     p.end();
00525 
00526     m_thumbPixmap.resize( 32, 32 );
00527     VKoPainter p2( &m_thumbPixmap, 32, 32 );
00528     mat.setMatrix( 32., 0, 0, 32., 0, 0 );
00529 
00530     trafo.setMatrix( mat );
00531     trafo.visit( *m_clipart );
00532 
00533     m_clipart->draw( &p2, &m_clipart->boundingBox() );
00534 
00535     trafo.setMatrix( mat.invert() );
00536     trafo.visit( *m_clipart );
00537 
00538     p2.end();
00539 
00540     validPixmap = true;
00541     validThumb = true;
00542 
00543     m_delete = QFileInfo( filename ).isWritable();
00544 }
00545 
00546 
00547 VClipartIconItem::VClipartIconItem( const VClipartIconItem& item )
00548         : KoIconItem( item )
00549 {
00550     m_clipart = item.m_clipart->clone();
00551     m_filename = item.m_filename;
00552     m_delete = item.m_delete;
00553     m_pixmap = item.m_pixmap;
00554     m_thumbPixmap = item.m_thumbPixmap;
00555     validPixmap = item.validPixmap;
00556     validThumb = item.validThumb;
00557     m_width = item.m_width;
00558     m_height = item.m_height;
00559 }
00560 
00561 VClipartIconItem::~VClipartIconItem()
00562 {
00563     delete m_clipart;
00564 }
00565 
00566 VClipartIconItem* VClipartIconItem::clone()
00567 {
00568     return new VClipartIconItem( *this );
00569 }
00570 
00571 #include "karbon_resourceserver.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys