karbon

vstyledocker.cc

00001 /* This file is part of the KDE project
00002    Made by Tomislav Lukman (tomislav.lukman@ck.tel.hr)
00003    Copyright (C) 2002, 2003 The Karbon Developers
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qlabel.h>
00022 #include <qlayout.h>
00023 #include <qtabwidget.h>
00024 #include <qsize.h>
00025 #include <qhbuttongroup.h>
00026 #include <qtoolbutton.h>
00027 
00028 #include <klocale.h>
00029 #include <kiconloader.h>
00030 #include <KoMainWindow.h>
00031 #include <KoFilterManager.h>
00032 #include <kfiledialog.h>
00033 
00034 #include "karbon_part.h"
00035 #include "karbon_view.h"
00036 #include "karbon_factory.h"
00037 #include "karbon_resourceserver.h"
00038 #include "karbon_drag.h"
00039 #include "vselection.h"
00040 #include "vlayer.h"
00041 #include "vfill.h"
00042 #include "vfillcmd.h"
00043 #include "vtransformcmd.h"
00044 
00045 #include "vstyledocker.h"
00046 
00047 #include <unistd.h>
00048 
00049 ClipartChooser::ClipartChooser( QSize iconSize, QWidget *parent, const char *name )
00050     : KoIconChooser( iconSize, parent, name )
00051 {
00052     setDragEnabled( true );
00053 }
00054 
00055 void
00056 ClipartChooser::startDrag()
00057 {
00058     KoIconChooser::startDrag();
00059     KarbonDrag* kd = new KarbonDrag( this );
00060     VObjectList objects;
00061     VClipartIconItem *selectedClipart = (VClipartIconItem *)currentItem();
00062     double s = kMax( selectedClipart->originalWidth(), selectedClipart->originalHeight() );
00063     VObject *clipart = selectedClipart->clipart()->clone();
00064 
00065     QWMatrix mat( s, 0, 0, -s, -( s / 2 ), ( s / 2 ) );
00066 
00067     VTransformCmd trafo( 0L, mat );
00068     trafo.visit( *clipart );
00069 
00070     objects.append( clipart );
00071     kd->setObjectList( objects );
00072     kd->dragCopy();
00073 }
00074 
00075 VStyleDocker::VStyleDocker( KarbonPart* part, KarbonView* parent, const char* /*name*/ )
00076     : QWidget(), m_part ( part ), m_view( parent )
00077 {
00078     setCaption( i18n( "Resources" ) );
00079 
00080     mTabWidget = new QTabWidget( this );
00081 
00082     //Pattern
00083     KoPatternChooser *pPatternChooser = new KoPatternChooser( KarbonFactory::rServer()->patterns(), mTabWidget );
00084     pPatternChooser->setCaption( i18n( "Patterns" ) );
00085 
00086     connect( pPatternChooser, SIGNAL(selected( KoIconItem * ) ), this, SLOT( slotItemSelected( KoIconItem * )));
00087     mTabWidget->addTab( pPatternChooser, i18n( "Patterns" ) );
00088 
00089     //Clipart
00090     ClipartWidget *pClipartWidget = new ClipartWidget( KarbonFactory::rServer()->cliparts(), part, mTabWidget );
00091     mTabWidget->addTab( pClipartWidget, i18n( "Clipart" ) );
00092 
00093     QVBoxLayout *mainWidgetLayout = new QVBoxLayout( this, 2 );
00094     mainWidgetLayout->addWidget( mTabWidget );
00095     mainWidgetLayout->activate();
00096     setMinimumHeight( 174 );
00097     setMinimumWidth( 194 );
00098 }
00099 
00100 VStyleDocker::~VStyleDocker()
00101 {
00102 }
00103 
00104 void VStyleDocker::slotItemSelected( KoIconItem *item )
00105 {
00106     VPattern *pattern = (VPattern *)item;
00107     if( !pattern ) return;
00108     kdDebug(38000) << "loading pattern : " << pattern->tilename().latin1() << endl;
00109     if( m_part && m_part->document().selection() )
00110     {
00111         VFill fill;
00112         fill.pattern() = *pattern;//.load( pattern->tilename() );
00113         //fill.setColor( *m_color );
00114         fill.setType( VFill::patt );
00115         m_part->addCommand( new VFillCmd( &m_part->document(), fill ), true );
00116     }
00117 }
00118 
00119 void
00120 VStyleDocker::mouseReleaseEvent( QMouseEvent * )
00121 {
00122 }
00123 
00124 ClipartWidget::ClipartWidget( QPtrList<VClipartIconItem>* clipartItems, KarbonPart *part, QWidget* parent )
00125     : QWidget( parent ), m_part( part )
00126 {
00127     KIconLoader il;
00128 
00129     QVBoxLayout* layout = new QVBoxLayout( this );
00130     layout->addWidget( m_clipartChooser = new ClipartChooser( QSize( 32, 32 ), this ) );
00131     layout->addWidget( m_buttonGroup = new QHButtonGroup( this ) );
00132     QToolButton* m_addClipartButton;
00133     m_buttonGroup->insert( m_addClipartButton = new QToolButton( m_buttonGroup ) );
00134     m_buttonGroup->insert( m_importClipartButton = new QToolButton( m_buttonGroup ) );
00135     m_buttonGroup->insert( m_deleteClipartButton = new QToolButton( m_buttonGroup ) );
00136     m_addClipartButton->setIconSet( SmallIcon( "14_layer_newlayer" ) );
00137     m_addClipartButton->setTextLabel( i18n( "Add" ) );
00138     m_importClipartButton->setIconSet( SmallIcon( "fileimport" ) );
00139     m_importClipartButton->setTextLabel( i18n( "Import" ) );
00140     m_deleteClipartButton->setIconSet( SmallIcon( "14_layer_deletelayer" ) );
00141     m_deleteClipartButton->setTextLabel( i18n( "Delete" ) );
00142 
00143     m_buttonGroup->setInsideMargin( 3 );
00144 
00145     //setFrameStyle( Box | Sunken );
00146     layout->setMargin( 3 );
00147 
00148     connect( m_buttonGroup, SIGNAL( clicked( int ) ), this, SLOT( slotButtonClicked( int ) ) );
00149     //connect( m_deleteClipartButton, SIGNAL( clicked() ), this, SLOT( deleteClipart() ) );
00150     connect( m_clipartChooser, SIGNAL( selected( KoIconItem* ) ), this, SLOT( clipartSelected( KoIconItem* ) ) );
00151 
00152     m_clipartChooser->setAutoDelete( false );
00153     VClipartIconItem* item = 0L;
00154 
00155     for( item = clipartItems->first(); item; item = clipartItems->next() )
00156         m_clipartChooser->addItem( item );
00157 
00158     m_clipartItem = ( clipartItems->first() ) ? clipartItems->first()->clone() : 0;
00159     if( !m_clipartItem )
00160         m_deleteClipartButton->setEnabled( false );
00161 }
00162 
00163 ClipartWidget::~ClipartWidget()
00164 {
00165     delete m_clipartItem;
00166 }
00167 
00168 VClipartIconItem* ClipartWidget::selectedClipart()
00169 {
00170     return m_clipartItem;
00171 }
00172 
00173 void
00174 ClipartWidget::clipartSelected( KoIconItem* item )
00175 {
00176     if( item )
00177     {
00178         delete m_clipartItem;
00179         VClipartIconItem* clipartItem = ( VClipartIconItem* ) item;
00180         m_deleteClipartButton->setEnabled( clipartItem->canDelete() );
00181         m_selectedItem = clipartItem;
00182         m_clipartItem = clipartItem->clone();
00183     }
00184 }
00185 
00186 void
00187 ClipartWidget::addClipart()
00188 {
00189     VObject* clipart = 0L;
00190     VSelection* selection = m_part->document().selection();
00191 
00192     if( selection->objects().count() == 1 )
00193     {
00194         clipart = selection->objects().getFirst()->clone();
00195         clipart->setParent( 0L );
00196     }
00197 
00198     if( selection->objects().count() > 1 )
00199     {
00200         QPtrVector<VObject> objects;
00201         selection->objects().toVector( &objects );
00202         VGroup* group = new VGroup( 0L );
00203 
00204         for( unsigned int i = 0; i < objects.count(); i++ )
00205         {
00206             VObject *obj = objects[ i ]->clone();
00207             obj->setParent( 0L );
00208             group->append( obj );
00209         }
00210 
00211         clipart = group;
00212     }
00213 
00214     if( clipart )
00215     {
00216         KoRect clipartBox = clipart->boundingBox();
00217         double scaleFactor = 1. / kMax( clipartBox.width(), clipartBox.height() );
00218         QWMatrix trMatrix( scaleFactor, 0, 0, scaleFactor, -clipartBox.x() * scaleFactor, -clipartBox.y() * scaleFactor );
00219 
00220         VTransformCmd trafo( 0L, trMatrix );
00221         trafo.visit( *clipart );
00222 
00223         // center the clipart
00224         trMatrix.reset();
00225         double size = kMax( clipart->boundingBox().width(), clipart->boundingBox().height() );
00226         trMatrix.translate( ( size - clipart->boundingBox().width() ) / 2, ( size - clipart->boundingBox().height() ) / 2 );
00227 
00228         trafo.setMatrix( trMatrix );
00229         trafo.visit( *clipart );
00230 
00231         // remove Y-mirroring
00232         trMatrix.reset();
00233         trMatrix.scale( 1, -1 );
00234         trMatrix.translate( 0, -1 );
00235 
00236         trafo.setMatrix( trMatrix );
00237         trafo.visit( *clipart );
00238 
00239         m_clipartChooser->addItem( KarbonFactory::rServer()->addClipart( clipart, clipartBox.width(), clipartBox.height() ) );
00240     }
00241 
00242     m_clipartChooser->updateContents();
00243 }
00244 
00245 void
00246 ClipartWidget::importClipart()
00247 {
00248     QStringList filter;
00249     filter << "application/x-karbon" << "image/svg+xml" << "image/x-wmf" << "image/x-eps" << "application/postscript";
00250     KFileDialog *dialog = new KFileDialog( "foo", QString::null, 0L, "Choose Graphic to Add", true);
00251     dialog->setMimeFilter( filter, "application/x-karbon" );
00252     if( dialog->exec()!=QDialog::Accepted )
00253     {
00254         delete dialog;
00255         return;
00256     }
00257     QString fname = dialog->selectedFile();
00258     delete dialog;
00259     if( m_part->nativeFormatMimeType() == dialog->currentMimeFilter().latin1() )
00260         m_part->mergeNativeFormat( fname );
00261     else
00262     {
00263         KoFilterManager man( m_part );
00264         KoFilter::ConversionStatus status;
00265         QString importedFile = man.import( fname, status );
00266         if( status == KoFilter::OK )
00267             m_part->mergeNativeFormat( importedFile );
00268         if( !importedFile.isEmpty() )
00269             unlink( QFile::encodeName( importedFile ) );
00270         if( status != KoFilter::OK )
00271             return;
00272     }
00273     m_part->document().selection()->clear();
00274     m_part->document().selection()->append( m_part->document().activeLayer()->objects() );
00275     addClipart();
00276     m_part->document().selection()->clear();
00277     m_part->document().removeLayer( m_part->document().activeLayer() );
00278 }
00279 
00280 void
00281 ClipartWidget::deleteClipart()
00282 {
00283     VClipartIconItem* clipartItem = m_clipartItem;
00284     KarbonFactory::rServer()->removeClipart( clipartItem );
00285     m_clipartChooser->removeItem( m_selectedItem );
00286     m_clipartChooser->updateContents();
00287 }
00288 
00289 void
00290 ClipartWidget::slotButtonClicked( int id )
00291 {
00292     switch( id )
00293     {
00294         case 0: addClipart(); break;
00295         case 1: importClipart(); break;
00296         case 2: deleteClipart();
00297     }
00298 }
00299 
00300 #include "vstyledocker.moc"
00301 
KDE Home | KDE Accessibility Home | Description of Access Keys