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     connect( KarbonFactory::rServer(), SIGNAL( patternAdded( KoIconItem * )), pPatternChooser, SLOT( addPattern( KoIconItem * )));
00088     connect( KarbonFactory::rServer(), SIGNAL( patternRemoved( KoIconItem * )), pPatternChooser, SLOT( removePattern( KoIconItem * )));
00089     mTabWidget->addTab( pPatternChooser, i18n( "Patterns" ) );
00090 
00091     //Clipart
00092     ClipartWidget *pClipartWidget = new ClipartWidget( KarbonFactory::rServer()->cliparts(), part, mTabWidget );
00093     mTabWidget->addTab( pClipartWidget, i18n( "Clipart" ) );
00094 
00095     QVBoxLayout *mainWidgetLayout = new QVBoxLayout( this, 2 );
00096     mainWidgetLayout->addWidget( mTabWidget );
00097     mainWidgetLayout->activate();
00098     setMinimumHeight( 174 );
00099     setMinimumWidth( 194 );
00100 }
00101 
00102 VStyleDocker::~VStyleDocker()
00103 {
00104 }
00105 
00106 void VStyleDocker::slotItemSelected( KoIconItem *item )
00107 {
00108     VPattern *pattern = (VPattern *)item;
00109     if( !pattern ) return;
00110     kdDebug(38000) << "loading pattern : " << pattern->tilename().latin1() << endl;
00111     if( m_part && m_part->document().selection() )
00112     {
00113         VFill fill;
00114         fill.pattern() = *pattern;//.load( pattern->tilename() );
00115         //fill.setColor( *m_color );
00116         fill.setType( VFill::patt );
00117         m_part->addCommand( new VFillCmd( &m_part->document(), fill ), true );
00118     }
00119 }
00120 
00121 void
00122 VStyleDocker::mouseReleaseEvent( QMouseEvent * )
00123 {
00124 }
00125 
00126 ClipartWidget::ClipartWidget( QPtrList<VClipartIconItem>* clipartItems, KarbonPart *part, QWidget* parent )
00127     : QWidget( parent ), m_part( part )
00128 {
00129     KIconLoader il;
00130 
00131     QVBoxLayout* layout = new QVBoxLayout( this );
00132     layout->addWidget( m_clipartChooser = new ClipartChooser( QSize( 32, 32 ), this ) );
00133     layout->addWidget( m_buttonGroup = new QHButtonGroup( this ) );
00134     QToolButton* m_addClipartButton;
00135     m_buttonGroup->insert( m_addClipartButton = new QToolButton( m_buttonGroup ) );
00136     m_buttonGroup->insert( m_importClipartButton = new QToolButton( m_buttonGroup ) );
00137     m_buttonGroup->insert( m_deleteClipartButton = new QToolButton( m_buttonGroup ) );
00138     m_addClipartButton->setIconSet( SmallIcon( "14_layer_newlayer" ) );
00139     m_addClipartButton->setTextLabel( i18n( "Add" ) );
00140     m_importClipartButton->setIconSet( SmallIcon( "fileimport" ) );
00141     m_importClipartButton->setTextLabel( i18n( "Import" ) );
00142     m_deleteClipartButton->setIconSet( SmallIcon( "14_layer_deletelayer" ) );
00143     m_deleteClipartButton->setTextLabel( i18n( "Delete" ) );
00144 
00145     m_buttonGroup->setInsideMargin( 3 );
00146 
00147     //setFrameStyle( Box | Sunken );
00148     layout->setMargin( 3 );
00149 
00150     connect( m_buttonGroup, SIGNAL( clicked( int ) ), this, SLOT( slotButtonClicked( int ) ) );
00151     //connect( m_deleteClipartButton, SIGNAL( clicked() ), this, SLOT( deleteClipart() ) );
00152     connect( m_clipartChooser, SIGNAL( selected( KoIconItem* ) ), this, SLOT( clipartSelected( KoIconItem* ) ) );
00153 
00154     m_clipartChooser->setAutoDelete( false );
00155     VClipartIconItem* item = 0L;
00156 
00157     for( item = clipartItems->first(); item; item = clipartItems->next() )
00158         m_clipartChooser->addItem( item );
00159 
00160     m_clipartItem = ( clipartItems->first() ) ? clipartItems->first()->clone() : 0;
00161     if( !m_clipartItem )
00162         m_deleteClipartButton->setEnabled( false );
00163 }
00164 
00165 ClipartWidget::~ClipartWidget()
00166 {
00167     delete m_clipartItem;
00168 }
00169 
00170 VClipartIconItem* ClipartWidget::selectedClipart()
00171 {
00172     return m_clipartItem;
00173 }
00174 
00175 void
00176 ClipartWidget::clipartSelected( KoIconItem* item )
00177 {
00178     if( item )
00179     {
00180         delete m_clipartItem;
00181         VClipartIconItem* clipartItem = ( VClipartIconItem* ) item;
00182         m_deleteClipartButton->setEnabled( clipartItem->canDelete() );
00183         m_selectedItem = clipartItem;
00184         m_clipartItem = clipartItem->clone();
00185     }
00186 }
00187 
00188 void
00189 ClipartWidget::addClipart()
00190 {
00191     VObject* clipart = 0L;
00192     VSelection* selection = m_part->document().selection();
00193 
00194     if( selection->objects().count() == 1 )
00195     {
00196         clipart = selection->objects().getFirst()->clone();
00197         clipart->setParent( 0L );
00198     }
00199 
00200     if( selection->objects().count() > 1 )
00201     {
00202         QPtrVector<VObject> objects;
00203         selection->objects().toVector( &objects );
00204         VGroup* group = new VGroup( 0L );
00205 
00206         for( unsigned int i = 0; i < objects.count(); i++ )
00207         {
00208             VObject *obj = objects[ i ]->clone();
00209             obj->setParent( 0L );
00210             group->append( obj );
00211         }
00212 
00213         clipart = group;
00214     }
00215 
00216     if( clipart )
00217     {
00218         KoRect clipartBox = clipart->boundingBox();
00219         double scaleFactor = 1. / kMax( clipartBox.width(), clipartBox.height() );
00220         QWMatrix trMatrix( scaleFactor, 0, 0, scaleFactor, -clipartBox.x() * scaleFactor, -clipartBox.y() * scaleFactor );
00221 
00222         VTransformCmd trafo( 0L, trMatrix );
00223         trafo.visit( *clipart );
00224 
00225         // center the clipart
00226         trMatrix.reset();
00227         double size = kMax( clipart->boundingBox().width(), clipart->boundingBox().height() );
00228         trMatrix.translate( ( size - clipart->boundingBox().width() ) / 2, ( size - clipart->boundingBox().height() ) / 2 );
00229 
00230         trafo.setMatrix( trMatrix );
00231         trafo.visit( *clipart );
00232 
00233         // remove Y-mirroring
00234         trMatrix.reset();
00235         trMatrix.scale( 1, -1 );
00236         trMatrix.translate( 0, -1 );
00237 
00238         trafo.setMatrix( trMatrix );
00239         trafo.visit( *clipart );
00240 
00241         m_clipartChooser->addItem( KarbonFactory::rServer()->addClipart( clipart, clipartBox.width(), clipartBox.height() ) );
00242     }
00243 
00244     m_clipartChooser->updateContents();
00245 }
00246 
00247 void
00248 ClipartWidget::importClipart()
00249 {
00250     QStringList filter;
00251     filter << "application/x-karbon" << "image/svg+xml" << "image/x-wmf" << "image/x-eps" << "application/postscript";
00252     KFileDialog *dialog = new KFileDialog( "foo", QString::null, 0L, "Choose Graphic to Add", true);
00253     dialog->setMimeFilter( filter, "application/x-karbon" );
00254     if( dialog->exec()!=QDialog::Accepted )
00255     {
00256         delete dialog;
00257         return;
00258     }
00259     QString fname = dialog->selectedFile();
00260     delete dialog;
00261     if( m_part->nativeFormatMimeType() == dialog->currentMimeFilter().latin1() )
00262         m_part->mergeNativeFormat( fname );
00263     else
00264     {
00265         KoFilterManager man( m_part );
00266         KoFilter::ConversionStatus status;
00267         QString importedFile = man.import( fname, status );
00268         if( status == KoFilter::OK )
00269             m_part->mergeNativeFormat( importedFile );
00270         if( !importedFile.isEmpty() )
00271             unlink( QFile::encodeName( importedFile ) );
00272         if( status != KoFilter::OK )
00273             return;
00274     }
00275     m_part->document().selection()->clear();
00276     m_part->document().selection()->append( m_part->document().activeLayer()->objects() );
00277     addClipart();
00278     m_part->document().selection()->clear();
00279     m_part->document().removeLayer( m_part->document().activeLayer() );
00280 }
00281 
00282 void
00283 ClipartWidget::deleteClipart()
00284 {
00285     VClipartIconItem* clipartItem = m_clipartItem;
00286     KarbonFactory::rServer()->removeClipart( clipartItem );
00287     m_clipartChooser->removeItem( m_selectedItem );
00288     m_clipartChooser->updateContents();
00289 }
00290 
00291 void
00292 ClipartWidget::slotButtonClicked( int id )
00293 {
00294     switch( id )
00295     {
00296         case 0: addClipart(); break;
00297         case 1: importClipart(); break;
00298         case 2: deleteClipart();
00299     }
00300 }
00301 
00302 #include "vstyledocker.moc"
00303 
KDE Home | KDE Accessibility Home | Description of Access Keys