karbon

vstyledocker.cpp
1/* This file is part of the KDE project
2 Made by Tomislav Lukman (tomislav.lukman@ck.tel.hr)
3 Copyright (C) 2002, 2003 The Karbon Developers
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include <tqlabel.h>
22#include <tqlayout.h>
23#include <tqtabwidget.h>
24#include <tqsize.h>
25#include <tqhbuttongroup.h>
26#include <tqtoolbutton.h>
27
28#include <tdelocale.h>
29#include <kiconloader.h>
30#include <KoMainWindow.h>
31#include <KoFilterManager.h>
32#include <tdefiledialog.h>
33
34#include "karbon_part.h"
35#include "karbon_view.h"
36#include "karbon_factory.h"
37#include "karbon_resourceserver.h"
38#include "karbon_drag.h"
39#include "vselection.h"
40#include "vlayer.h"
41#include "vfill.h"
42#include "vfillcmd.h"
43#include "vtransformcmd.h"
44
45#include "vstyledocker.h"
46
47#include <unistd.h>
48
49ClipartChooser::ClipartChooser( TQSize iconSize, TQWidget *parent, const char *name )
50 : KoIconChooser( iconSize, parent, name )
51{
52 setDragEnabled( true );
53}
54
55void
56ClipartChooser::startDrag()
57{
58 KoIconChooser::startDrag();
59 KarbonDrag* kd = new KarbonDrag( this );
60 VObjectList objects;
61 VClipartIconItem *selectedClipart = (VClipartIconItem *)currentItem();
62 double s = kMax( selectedClipart->originalWidth(), selectedClipart->originalHeight() );
63 VObject *clipart = selectedClipart->clipart()->clone();
64
65 TQWMatrix mat( s, 0, 0, -s, -( s / 2 ), ( s / 2 ) );
66
67 VTransformCmd trafo( 0L, mat );
68 trafo.visit( *clipart );
69
70 objects.append( clipart );
71 kd->setObjectList( objects );
72 kd->dragCopy();
73}
74
75VStyleDocker::VStyleDocker( KarbonPart* part, KarbonView* parent, const char* /*name*/ )
76 : TQWidget(), m_part ( part ), m_view( parent )
77{
78 setCaption( i18n( "Resources" ) );
79
80 mTabWidget = new TQTabWidget( this );
81
82 //Pattern
83 KoPatternChooser *pPatternChooser = new KoPatternChooser( KarbonFactory::rServer()->patterns(), mTabWidget );
84 pPatternChooser->setCaption( i18n( "Patterns" ) );
85
86 connect( pPatternChooser, TQ_SIGNAL(selected( KoIconItem * ) ), this, TQ_SLOT( slotItemSelected( KoIconItem * )));
87 connect( KarbonFactory::rServer(), TQ_SIGNAL( patternAdded( KoIconItem * )), pPatternChooser, TQ_SLOT( addPattern( KoIconItem * )));
88 connect( KarbonFactory::rServer(), TQ_SIGNAL( patternRemoved( KoIconItem * )), pPatternChooser, TQ_SLOT( removePattern( KoIconItem * )));
89 mTabWidget->addTab( pPatternChooser, i18n( "Patterns" ) );
90
91 //Clipart
92 ClipartWidget *pClipartWidget = new ClipartWidget( KarbonFactory::rServer()->cliparts(), part, mTabWidget );
93 mTabWidget->addTab( pClipartWidget, i18n( "Clipart" ) );
94
95 TQVBoxLayout *mainWidgetLayout = new TQVBoxLayout( this, 2 );
96 mainWidgetLayout->addWidget( mTabWidget );
97 mainWidgetLayout->activate();
98 setMinimumHeight( 174 );
99 setMinimumWidth( 194 );
100}
101
102VStyleDocker::~VStyleDocker()
103{
104}
105
106void VStyleDocker::slotItemSelected( KoIconItem *item )
107{
108 VPattern *pattern = (VPattern *)item;
109 if( !pattern ) return;
110 kdDebug(38000) << "loading pattern : " << pattern->tilename().latin1() << endl;
111 if( m_part && m_part->document().selection() )
112 {
113 VFill fill;
114 fill.pattern() = *pattern;//.load( pattern->tilename() );
115 //fill.setColor( *m_color );
116 fill.setType( VFill::patt );
117 m_part->addCommand( new VFillCmd( &m_part->document(), fill ), true );
118 }
119}
120
121void
122VStyleDocker::mouseReleaseEvent( TQMouseEvent * )
123{
124}
125
126ClipartWidget::ClipartWidget( TQPtrList<VClipartIconItem>* clipartItems, KarbonPart *part, TQWidget* parent )
127 : TQWidget( parent ), m_part( part )
128{
129 TDEIconLoader il;
130
131 TQVBoxLayout* layout = new TQVBoxLayout( this );
132 layout->addWidget( m_clipartChooser = new ClipartChooser( TQSize( 32, 32 ), this ) );
133 layout->addWidget( m_buttonGroup = new TQHButtonGroup( this ) );
134 TQToolButton* m_addClipartButton;
135 m_buttonGroup->insert( m_addClipartButton = new TQToolButton( m_buttonGroup ) );
136 m_buttonGroup->insert( m_importClipartButton = new TQToolButton( m_buttonGroup ) );
137 m_buttonGroup->insert( m_deleteClipartButton = new TQToolButton( m_buttonGroup ) );
138 m_addClipartButton->setIconSet( SmallIcon( "14_layer_newlayer" ) );
139 m_addClipartButton->setTextLabel( i18n( "Add" ) );
140 m_importClipartButton->setIconSet( SmallIcon( "fileimport" ) );
141 m_importClipartButton->setTextLabel( i18n( "Import" ) );
142 m_deleteClipartButton->setIconSet( SmallIcon( "14_layer_deletelayer" ) );
143 m_deleteClipartButton->setTextLabel( i18n( "Delete" ) );
144
145 m_buttonGroup->setInsideMargin( 3 );
146
147 //setFrameStyle( Box | Sunken );
148 layout->setMargin( 3 );
149
150 connect( m_buttonGroup, TQ_SIGNAL( clicked( int ) ), this, TQ_SLOT( slotButtonClicked( int ) ) );
151 //connect( m_deleteClipartButton, TQ_SIGNAL( clicked() ), this, TQ_SLOT( deleteClipart() ) );
152 connect( m_clipartChooser, TQ_SIGNAL( selected( KoIconItem* ) ), this, TQ_SLOT( clipartSelected( KoIconItem* ) ) );
153
154 m_clipartChooser->setAutoDelete( false );
155 VClipartIconItem* item = 0L;
156
157 for( item = clipartItems->first(); item; item = clipartItems->next() )
158 m_clipartChooser->addItem( item );
159
160 m_clipartItem = ( clipartItems->first() ) ? clipartItems->first()->clone() : 0;
161 if( !m_clipartItem )
162 m_deleteClipartButton->setEnabled( false );
163}
164
165ClipartWidget::~ClipartWidget()
166{
167 delete m_clipartItem;
168}
169
170VClipartIconItem* ClipartWidget::selectedClipart()
171{
172 return m_clipartItem;
173}
174
175void
176ClipartWidget::clipartSelected( KoIconItem* item )
177{
178 if( item )
179 {
180 delete m_clipartItem;
181 VClipartIconItem* clipartItem = ( VClipartIconItem* ) item;
182 m_deleteClipartButton->setEnabled( clipartItem->canDelete() );
183 m_selectedItem = clipartItem;
184 m_clipartItem = clipartItem->clone();
185 }
186}
187
188void
189ClipartWidget::addClipart()
190{
191 VObject* clipart = 0L;
192 VSelection* selection = m_part->document().selection();
193
194 if( selection->objects().count() == 1 )
195 {
196 clipart = selection->objects().getFirst()->clone();
197 clipart->setParent( 0L );
198 }
199
200 if( selection->objects().count() > 1 )
201 {
202 TQPtrVector<VObject> objects;
203 selection->objects().toVector( &objects );
204 VGroup* group = new VGroup( 0L );
205
206 for( unsigned int i = 0; i < objects.count(); i++ )
207 {
208 VObject *obj = objects[ i ]->clone();
209 obj->setParent( 0L );
210 group->append( obj );
211 }
212
213 clipart = group;
214 }
215
216 if( clipart )
217 {
218 KoRect clipartBox = clipart->boundingBox();
219 double scaleFactor = 1. / kMax( clipartBox.width(), clipartBox.height() );
220 TQWMatrix trMatrix( scaleFactor, 0, 0, scaleFactor, -clipartBox.x() * scaleFactor, -clipartBox.y() * scaleFactor );
221
222 VTransformCmd trafo( 0L, trMatrix );
223 trafo.visit( *clipart );
224
225 // center the clipart
226 trMatrix.reset();
227 double size = kMax( clipart->boundingBox().width(), clipart->boundingBox().height() );
228 trMatrix.translate( ( size - clipart->boundingBox().width() ) / 2, ( size - clipart->boundingBox().height() ) / 2 );
229
230 trafo.setMatrix( trMatrix );
231 trafo.visit( *clipart );
232
233 // remove Y-mirroring
234 trMatrix.reset();
235 trMatrix.scale( 1, -1 );
236 trMatrix.translate( 0, -1 );
237
238 trafo.setMatrix( trMatrix );
239 trafo.visit( *clipart );
240
241 m_clipartChooser->addItem( KarbonFactory::rServer()->addClipart( clipart, clipartBox.width(), clipartBox.height() ) );
242 }
243
244 m_clipartChooser->updateContents();
245}
246
247void
248ClipartWidget::importClipart()
249{
250 TQStringList filter;
251 filter << "application/x-karbon" << "image/svg+xml" << "image/x-wmf" << "image/x-eps" << "application/postscript";
252 KFileDialog *dialog = new KFileDialog( "foo", TQString(), 0L, "Choose Graphic to Add", true);
253 dialog->setMimeFilter( filter, "application/x-karbon" );
254 if( dialog->exec()!=TQDialog::Accepted )
255 {
256 delete dialog;
257 return;
258 }
259 TQString fname = dialog->selectedFile();
260 delete dialog;
261 if( m_part->nativeFormatMimeType() == dialog->currentMimeFilter().latin1() )
262 m_part->mergeNativeFormat( fname );
263 else
264 {
265 KoFilterManager man( m_part );
266 KoFilter::ConversionStatus status;
267 TQString importedFile = man.import( fname, status );
268 if( status == KoFilter::OK )
269 m_part->mergeNativeFormat( importedFile );
270 if( !importedFile.isEmpty() )
271 unlink( TQFile::encodeName( importedFile ) );
272 if( status != KoFilter::OK )
273 return;
274 }
275 m_part->document().selection()->clear();
276 m_part->document().selection()->append( m_part->document().activeLayer()->objects() );
277 addClipart();
278 m_part->document().selection()->clear();
279 m_part->document().removeLayer( m_part->document().activeLayer() );
280}
281
282void
283ClipartWidget::deleteClipart()
284{
285 VClipartIconItem* clipartItem = m_clipartItem;
286 KarbonFactory::rServer()->removeClipart( clipartItem );
287 m_clipartChooser->removeItem( m_selectedItem );
288 m_clipartChooser->updateContents();
289}
290
291void
292ClipartWidget::slotButtonClicked( int id )
293{
294 switch( id )
295 {
296 case 0: addClipart(); break;
297 case 1: importClipart(); break;
298 case 2: deleteClipart();
299 }
300}
301
302#include "vstyledocker.moc"
303
Keeps track of visual per document properties.
Definition karbon_part.h:46
VSelection * selection() const
Returns a pointer to the selection.
Definition vdocument.h:238
@ patt
gradient fill
Definition vfill.h:52
void append(VObject *object)
Appends a new child object.
Definition vgroup.cpp:372
virtual VObject * clone() const =0
Create an exact copy of this object.
void setParent(VObject *parent)
Sets a new parent object.
Definition vobject.h:140
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
Definition vobject.h:111
VDocument * document() const
Return document the object belongs to.
Definition vobject.cpp:216
const VObjectList & objects() const
Read only access to the selected objects.
Definition vselection.h:136