summaryrefslogtreecommitdiffstats
path: root/karbon/dockers/vstyledocker.cc
blob: 5b7633b152b62668ac5509d4939c5e7214524db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* This file is part of the KDE project
   Made by Tomislav Lukman ([email protected])
   Copyright (C) 2002, 2003 The Karbon Developers

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqtabwidget.h>
#include <tqsize.h>
#include <tqhbuttongroup.h>
#include <tqtoolbutton.h>

#include <klocale.h>
#include <kiconloader.h>
#include <KoMainWindow.h>
#include <KoFilterManager.h>
#include <kfiledialog.h>

#include "karbon_part.h"
#include "karbon_view.h"
#include "karbon_factory.h"
#include "karbon_resourceserver.h"
#include "karbon_drag.h"
#include "vselection.h"
#include "vlayer.h"
#include "vfill.h"
#include "vfillcmd.h"
#include "vtransformcmd.h"

#include "vstyledocker.h"

#include <unistd.h>

ClipartChooser::ClipartChooser( TQSize iconSize, TQWidget *parent, const char *name )
	: KoIconChooser( iconSize, parent, name )
{
	setDragEnabled( true );
}

void
ClipartChooser::startDrag()
{
	KoIconChooser::startDrag();
	KarbonDrag* kd = new KarbonDrag( this );
	VObjectList objects;
	VClipartIconItem *selectedClipart = (VClipartIconItem *)currentItem();
	double s = kMax( selectedClipart->originalWidth(), selectedClipart->originalHeight() );
	VObject *clipart = selectedClipart->clipart()->clone();

	TQWMatrix mat( s, 0, 0, -s, -( s / 2 ), ( s / 2 ) );

	VTransformCmd trafo( 0L, mat );
	trafo.visit( *clipart );

	objects.append( clipart );
	kd->setObjectList( objects );
	kd->dragCopy();
}

VStyleDocker::VStyleDocker( KarbonPart* part, KarbonView* parent, const char* /*name*/ )
	: TQWidget(), m_part ( part ), m_view( parent )
{
	setCaption( i18n( "Resources" ) );

	mTabWidget = new TQTabWidget( this );

	//Pattern
	KoPatternChooser *pPatternChooser = new KoPatternChooser( KarbonFactory::rServer()->patterns(), mTabWidget );
	pPatternChooser->setCaption( i18n( "Patterns" ) );

	connect( pPatternChooser, TQT_SIGNAL(selected( KoIconItem * ) ), this, TQT_SLOT( slotItemSelected( KoIconItem * )));
	connect( KarbonFactory::rServer(), TQT_SIGNAL( patternAdded( KoIconItem * )), pPatternChooser, TQT_SLOT( addPattern( KoIconItem * )));
	connect( KarbonFactory::rServer(), TQT_SIGNAL( patternRemoved( KoIconItem * )), pPatternChooser, TQT_SLOT( removePattern( KoIconItem * )));
	mTabWidget->addTab( pPatternChooser, i18n( "Patterns" ) );

	//Clipart
	ClipartWidget *pClipartWidget = new ClipartWidget( KarbonFactory::rServer()->cliparts(), part, mTabWidget );
	mTabWidget->addTab( pClipartWidget, i18n( "Clipart" ) );

	TQVBoxLayout *mainWidgetLayout = new TQVBoxLayout( this, 2 );
	mainWidgetLayout->addWidget( mTabWidget );
	mainWidgetLayout->activate();
	setMinimumHeight( 174 );
	setMinimumWidth( 194 );
}

VStyleDocker::~VStyleDocker()
{
}

void VStyleDocker::slotItemSelected( KoIconItem *item )
{
	VPattern *pattern = (VPattern *)item;
	if( !pattern ) return;
	kdDebug(38000) << "loading pattern : " << pattern->tilename().latin1() << endl;
	if( m_part && m_part->document().selection() )
	{
		VFill fill;
		fill.pattern() = *pattern;//.load( pattern->tilename() );
		//fill.setColor( *m_color );
		fill.setType( VFill::patt );
		m_part->addCommand( new VFillCmd( &m_part->document(), fill ), true );
	}
}

void
VStyleDocker::mouseReleaseEvent( TQMouseEvent * )
{
}

ClipartWidget::ClipartWidget( TQPtrList<VClipartIconItem>* clipartItems, KarbonPart *part, TQWidget* parent )
	: TQWidget( parent ), m_part( part )
{
	KIconLoader il;

	TQVBoxLayout* tqlayout = new TQVBoxLayout( this );
	tqlayout->addWidget( m_clipartChooser = new ClipartChooser( TQSize( 32, 32 ), this ) );
	tqlayout->addWidget( m_buttonGroup = new TQHButtonGroup( this ) );
	TQToolButton* m_addClipartButton;
	m_buttonGroup->insert( m_addClipartButton = new TQToolButton( m_buttonGroup ) );
	m_buttonGroup->insert( m_importClipartButton = new TQToolButton( m_buttonGroup ) );
	m_buttonGroup->insert( m_deleteClipartButton = new TQToolButton( m_buttonGroup ) );
	m_addClipartButton->setIconSet( SmallIcon( "14_layer_newlayer" ) );
	m_addClipartButton->setTextLabel( i18n( "Add" ) );
	m_importClipartButton->setIconSet( SmallIcon( "fileimport" ) );
	m_importClipartButton->setTextLabel( i18n( "Import" ) );
	m_deleteClipartButton->setIconSet( SmallIcon( "14_layer_deletelayer" ) );
	m_deleteClipartButton->setTextLabel( i18n( "Delete" ) );

	m_buttonGroup->setInsideMargin( 3 );

	//setFrameStyle( Box | Sunken );
	tqlayout->setMargin( 3 );

	connect( m_buttonGroup, TQT_SIGNAL( clicked( int ) ), this, TQT_SLOT( slotButtonClicked( int ) ) );
	//connect( m_deleteClipartButton, TQT_SIGNAL( clicked() ), this, TQT_SLOT( deleteClipart() ) );
	connect( m_clipartChooser, TQT_SIGNAL( selected( KoIconItem* ) ), this, TQT_SLOT( clipartSelected( KoIconItem* ) ) );

	m_clipartChooser->setAutoDelete( false );
	VClipartIconItem* item = 0L;

	for( item = clipartItems->first(); item; item = clipartItems->next() )
		m_clipartChooser->addItem( item );

	m_clipartItem = ( clipartItems->first() ) ? clipartItems->first()->clone() : 0;
	if( !m_clipartItem )
		m_deleteClipartButton->setEnabled( false );
}

ClipartWidget::~ClipartWidget()
{
	delete m_clipartItem;
}

VClipartIconItem* ClipartWidget::selectedClipart()
{
	return m_clipartItem;
}

void
ClipartWidget::clipartSelected( KoIconItem* item )
{
	if( item )
	{
		delete m_clipartItem;
		VClipartIconItem* clipartItem = ( VClipartIconItem* ) item;
		m_deleteClipartButton->setEnabled( clipartItem->canDelete() );
		m_selectedItem = clipartItem;
		m_clipartItem = clipartItem->clone();
	}
}

void
ClipartWidget::addClipart()
{
	VObject* clipart = 0L;
	VSelection* selection = m_part->document().selection();

	if( selection->objects().count() == 1 )
	{
		clipart = selection->objects().getFirst()->clone();
		clipart->setParent( 0L );
	}

	if( selection->objects().count() > 1 )
	{
		TQPtrVector<VObject> objects;
		selection->objects().toVector( &objects );
		VGroup* group = new VGroup( 0L );

		for( unsigned int i = 0; i < objects.count(); i++ )
		{
			VObject *obj = objects[ i ]->clone();
			obj->setParent( 0L );
			group->append( obj );
		}

		clipart = group;
	}

	if( clipart )
	{
		KoRect clipartBox = clipart->boundingBox();
		double scaleFactor = 1. / kMax( clipartBox.width(), clipartBox.height() );
		TQWMatrix trMatrix( scaleFactor, 0, 0, scaleFactor, -clipartBox.x() * scaleFactor, -clipartBox.y() * scaleFactor );

		VTransformCmd trafo( 0L, trMatrix );
		trafo.visit( *clipart );

		// center the clipart
		trMatrix.reset();
		double size = kMax( clipart->boundingBox().width(), clipart->boundingBox().height() );
		trMatrix.translate( ( size - clipart->boundingBox().width() ) / 2, ( size - clipart->boundingBox().height() ) / 2 );

		trafo.setMatrix( trMatrix );
		trafo.visit( *clipart );

		// remove Y-mirroring
		trMatrix.reset();
		trMatrix.scale( 1, -1 );
		trMatrix.translate( 0, -1 );

		trafo.setMatrix( trMatrix );
		trafo.visit( *clipart );

		m_clipartChooser->addItem( KarbonFactory::rServer()->addClipart( clipart, clipartBox.width(), clipartBox.height() ) );
	}

	m_clipartChooser->updateContents();
}

void
ClipartWidget::importClipart()
{
	TQStringList filter;
	filter << "application/x-karbon" << "image/svg+xml" << "image/x-wmf" << "image/x-eps" << "application/postscript";
	KFileDialog *dialog = new KFileDialog( "foo", TQString(), 0L, "Choose Graphic to Add", true);
	dialog->setMimeFilter( filter, "application/x-karbon" );
	if( dialog->exec()!=TQDialog::Accepted )
	{
		delete dialog;
		return;
	}
	TQString fname = dialog->selectedFile();
	delete dialog;
	if( m_part->nativeFormatMimeType() == dialog->currentMimeFilter().latin1() )
		m_part->mergeNativeFormat( fname );
	else
	{
		KoFilterManager man( m_part );
		KoFilter::ConversiontqStatus status;
		TQString importedFile = man.import( fname, status );
		if( status == KoFilter::OK )
			m_part->mergeNativeFormat( importedFile );
		if( !importedFile.isEmpty() )
			unlink( TQFile::encodeName( importedFile ) );
		if( status != KoFilter::OK )
			return;
	}
	m_part->document().selection()->clear();
	m_part->document().selection()->append( m_part->document().activeLayer()->objects() );
	addClipart();
	m_part->document().selection()->clear();
	m_part->document().removeLayer( m_part->document().activeLayer() );
}

void
ClipartWidget::deleteClipart()
{
	VClipartIconItem* clipartItem = m_clipartItem;
	KarbonFactory::rServer()->removeClipart( clipartItem );
	m_clipartChooser->removeItem( m_selectedItem );
	m_clipartChooser->updateContents();
}

void
ClipartWidget::slotButtonClicked( int id )
{
	switch( id )
	{
		case 0: addClipart(); break;
		case 1: importClipart(); break;
		case 2: deleteClipart();
	}
}

#include "vstyledocker.moc"