20#include "vpatterntool.h"
22#include <tqtoolbutton.h>
24#include <tqhbuttongroup.h>
26#include <tqfileinfo.h>
30#include <kiconloader.h>
31#include <koIconChooser.h>
32#include <tdefiledialog.h>
33#include <tdemessagebox.h>
35#include <karbon_factory.h>
36#include <karbon_resourceserver.h>
37#include <karbon_view.h>
38#include <karbon_part.h>
39#include <render/vpainter.h>
40#include <render/vpainterfactory.h>
41#include <core/vselection.h>
42#include <core/vfill.h>
43#include <core/vstroke.h>
44#include <commands/vfillcmd.h>
45#include <commands/vstrokecmd.h>
46#include <widgets/vstrokefillpreview.h>
48VPatternWidget::VPatternWidget( TQPtrList<KoIconItem>* patterns,
VTool*, TQWidget* parent )
49 : KDialogBase( parent,
"", true, i18n(
"Choose Pattern" ), Ok | Cancel ), m_pattern( 0 )
51 TQWidget *base =
new TQWidget(
this );
52 TQVBoxLayout* layout =
new TQVBoxLayout( base );
53 layout->addWidget( m_patternChooser =
new KoIconChooser( TQSize( 32, 32 ), base ) );
54 layout->addWidget( m_buttonGroup =
new TQHButtonGroup( base ) );
55 m_buttonGroup->insert( m_importPatternButton =
new TQToolButton( m_buttonGroup ) );
56 m_buttonGroup->insert( m_deletePatternButton =
new TQToolButton( m_buttonGroup ) );
57 m_patternChooser->setFixedSize( 180, 120 );
58 m_importPatternButton->setIconSet( SmallIconSet(
"14_layer_newlayer" ) );
59 m_importPatternButton->setTextLabel( i18n(
"Import" ) );
60 m_deletePatternButton->setIconSet( SmallIconSet(
"14_layer_deletelayer" ) );
61 m_deletePatternButton->setTextLabel( i18n(
"Delete" ) );
63 m_buttonGroup->setInsideMargin( 3 );
64 m_importPatternButton->setEnabled(
true );
65 m_deletePatternButton->setEnabled(
false );
68 layout->setMargin( 3 );
70 connect( m_buttonGroup, TQ_SIGNAL( clicked(
int ) ),
this, TQ_SLOT( slotButtonClicked(
int ) ) );
71 connect( m_patternChooser, TQ_SIGNAL( selected( KoIconItem* ) ),
this, TQ_SLOT( patternSelected( KoIconItem* ) ) );
74 for( item = patterns->first(); item; item = patterns->next() )
75 m_patternChooser->addItem( item );
76 m_pattern = (VPattern*)patterns->first();
78 setMainWidget( base );
81VPatternWidget::~VPatternWidget()
85VPattern* VPatternWidget::selectedPattern()
90void VPatternWidget::importPattern()
92 VPattern* pattern = KarbonFactory::rServer()->addPattern( KFileDialog::getOpenFileName( TQString(),
93 "*.jpg *.gif *.png *.tif *.xpm *.bmp",
this, i18n(
"Choose Pattern to Add" ) ) );
95 m_patternChooser->addItem( pattern );
98void VPatternWidget::deletePattern()
100 m_patternChooser->removeItem( m_pattern );
101 KarbonFactory::rServer()->removePattern( m_pattern );
102 m_patternChooser->updateContents();
103 m_pattern =
static_cast<VPattern*
>( m_patternChooser->currentItem() );
106void VPatternWidget::slotButtonClicked(
int id )
110 case 0: importPattern();
112 case 1: deletePattern();
117void VPatternWidget::patternSelected( KoIconItem* item )
119 m_pattern = (VPattern*)item;
120 m_deletePatternButton->setEnabled( TQFileInfo( m_pattern->tilename() ).isWritable() );
123VPatternTool::VPatternTool( KarbonView *view )
124 :
VTool( view,
"tool_pattern" ), m_state( normal ), m_handleSize( 3 ), m_active( false )
126 TQPtrList<KoIconItem> patterns = KarbonFactory::rServer()->patterns();
127 m_optionsWidget =
new VPatternWidget( &patterns,
this );
128 registerTool(
this );
131VPatternTool::~VPatternTool()
133 delete m_optionsWidget;
142 view()->statusMessage()->setText( i18n(
"Pattern" ) );
143 view()->setCursor( TQCursor( TQt::crossCursor ) );
150 VStrokeFillPreview* preview =
view()->strokeFillPreview();
153 connect( preview, TQ_SIGNAL( fillSelected() ),
this, TQ_SLOT( targetChanged() ) );
154 connect( preview, TQ_SIGNAL( strokeSelected() ),
this, TQ_SLOT( targetChanged() ) );
156 view()->repaintAll(
view()->part()->document().selection()->boundingBox() );
169 VStrokeFillPreview* preview =
view()->strokeFillPreview();
172 disconnect( preview, TQ_SIGNAL( fillSelected() ),
this, TQ_SLOT( targetChanged() ) );
173 disconnect( preview, TQ_SIGNAL( strokeSelected() ),
this, TQ_SLOT( targetChanged() ) );
175 view()->repaintAll(
view()->part()->document().selection()->boundingBox() );
183 TQString s = i18n(
"<qt><b>Pattern tool:</b><br>" );
184 s += i18n(
"<i>Click</i> on the pattern you want in the chooser.<br>" );
185 s += i18n(
"<i>Click and drag</i> to choose the pattern vector.</qt>" );
186 s += i18n(
"<i>Press i or Shift+i</i> to decrease or increase the handle size.<br>" );
190void VPatternTool::draw()
192 if( !
view() ||
view()->part()->document().selection()->objects().count() == 0 )
195 VPainter *painter =
view()->painterFactory()->editpainter();
196 painter->setRasterOp( TQt::NotROP );
198 painter->setPen( TQt::DotLine );
201 if( m_state == moveOrigin || m_state == moveVector )
204 painter->moveTo( m_fixed );
205 painter->lineTo( m_current );
206 painter->strokePath();
208 painter->drawNode( m_fixed, m_handleSize );
209 painter->drawNode( m_current, m_handleSize );
211 else if( m_state == createNew )
214 painter->moveTo(
first() );
215 painter->lineTo( m_current );
216 painter->strokePath();
218 painter->drawNode(
first(), m_handleSize );
219 painter->drawNode( m_current, m_handleSize );
224VPatternTool::getPattern( VPattern &pattern )
232 bool strokeSelected =
false;
234 VSelection* selection =
view()->part()->document().
selection();
235 if( selection->
objects().count() != 1 )
238 VObject *obj = selection->
objects().getFirst();
241 pattern = obj->
stroke()->pattern();
243 pattern = obj->
fill()->pattern();
251VPatternTool::draw( VPainter* painter )
256 if( m_state != normal )
259 if( ! getPattern( m_pattern ) )
262 KoPoint s = m_pattern.origin();
263 KoPoint e = m_pattern.vector();
266 m_origin = KoRect( s.x()-m_handleSize, s.y()-m_handleSize, 2*m_handleSize, 2*m_handleSize );
267 m_vector = KoRect( e.x()-m_handleSize, e.y()-m_handleSize, 2*m_handleSize, 2*m_handleSize );
269 painter->setPen( TQt::blue.light() );
270 painter->setBrush( TQt::blue.light() );
271 painter->setRasterOp( TQt::XorROP );
275 painter->moveTo( s );
276 painter->lineTo( e );
277 painter->strokePath();
280 painter->drawNode( m_origin.center(), m_handleSize );
281 painter->drawNode( m_vector.center(), m_handleSize );
287 if( m_state == normal )
303 if( m_origin.contains( m_current ) )
305 m_state = moveOrigin;
306 m_fixed = m_vector.center();
308 else if( m_vector.contains( m_current ) )
310 m_state = moveVector;
311 m_fixed = m_origin.center();
322 if(
view()->part()->document().selection()->objects().count() == 0 )
326 VPattern oldPattern = m_pattern;
334 if( !m_optionsWidget->selectedPattern() )
336 KMessageBox::error( 0L, i18n(
"Please select a pattern." ),
"" );
340 bool strokeSelected =
false;
348 if(
view()->part()->document().selection()->objects().count() == 1 )
355 oldPattern.setOrigin( bbox.bottomLeft() + 0.5*(bbox.topLeft()-bbox.bottomLeft()) );
356 oldPattern.setVector( bbox.bottomRight() + 0.5*(bbox.topRight()-bbox.bottomRight()) );
360 m_pattern = *m_optionsWidget->selectedPattern();
363 m_pattern.setOrigin( oldPattern.origin() );
364 m_pattern.setVector( oldPattern.vector() );
366 if( ! strokeSelected )
369 fill.pattern() = m_pattern;
372 new VFillCmd( &
view()->part()->document(), fill,
"14_pattern" ),
true );
377 stroke.pattern() = m_pattern;
380 new VStrokeCmd( &
view()->part()->document(), &stroke,
"14_pattern" ),
true );
387 if( !
view() || m_state == normal )
390 if(
view()->part()->document().selection()->objects().count() == 0 )
396 if( !m_optionsWidget->selectedPattern() )
399 KMessageBox::error( 0L, i18n(
"Please select a pattern." ),
"" );
404 if( m_state == moveOrigin )
405 m_pattern.setOrigin(
last() );
406 else if( m_state == moveVector )
407 m_pattern.setVector(
last() );
408 else if( m_state == createNew )
410 m_pattern.setOrigin(
first() );
411 m_pattern.setVector(
last() );
416 VStrokeFillPreview* preview =
view()->strokeFillPreview();
423 fill.pattern() = m_pattern;
426 new VFillCmd( &
view()->part()->document(), fill,
"14_pattern" ),
true );
452 return m_optionsWidget->exec() == TQDialog::Accepted;
456VPatternTool::setup( TDEActionCollection *collection )
458 m_action =
static_cast<TDERadioAction *
>(collection ->
action( name() ) );
462 m_action =
new TDERadioAction( i18n(
"Pattern Tool" ),
"14_pattern", TQt::SHIFT+TQt::Key_H,
this, TQ_SLOT(
activate() ), collection, name() );
463 m_action->setToolTip( i18n(
"Pattern" ) );
464 m_action->setExclusiveGroup(
"misc" );
472 if( !
view() )
return;
475 if( m_origin.contains(
last() ) || m_vector.contains(
last() ) )
476 view()->setCursor( TQCursor( TQt::SizeAllCursor ) );
478 view()->setCursor( TQCursor( TQt::arrowCursor ) );
482VPatternTool::keyReleased( TQt::Key key )
490 else if( m_handleSize > 3 )
493 default:
return false;
497 view()->repaintAll(
view()->part()->document().selection()->boundingBox() );
503VPatternTool::targetChanged()
506 view()->repaintAll(
view()->part()->document().selection()->boundingBox() );
509#include "vpatterntool.moc"
void addCommand(VCommand *cmd, bool repaint=false)
insert a command into the undo/redo-history:
VSelection * selection() const
Returns a pointer to the selection.
virtual const KoRect & boundingBox() const
Calculates the tightest bounding box around the object.
virtual VFill * fill() const
Gets the object's actual fill.
virtual VStroke * stroke() const
Gets the object's actual stroke.
const VObjectList & objects() const
Read only access to the selected objects.
void showHandle(bool handle=true)
Toggle selection handles on/off.