karbon

vgradienttabwidget.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tqlabel.h>
21#include <tqpainter.h>
22#include <tqlayout.h>
23#include <tqgroupbox.h>
24#include <tqpushbutton.h>
25#include <tqfileinfo.h>
26#include <tqpixmap.h>
27
28#include <knuminput.h>
29#include <kcombobox.h>
30#include <tdelocale.h>
31#include <tdelistbox.h>
32#include <kiconloader.h>
33
34#include "vgradientwidget.h"
35#include "vgradienttabwidget.h"
36#include "karbon_resourceserver.h"
37#include "vkopainter.h"
38#include "vfill.h"
39
40VGradientListItem::VGradientListItem( const VGradient& gradient, TQString filename )
41 : TQListBoxItem( 0L ), m_filename( filename )
42{
43 m_gradient = new VGradient( gradient );
44
45 m_pixmap.resize( 200, 16 );
46 VKoPainter gp( &m_pixmap, m_pixmap.width(), m_pixmap.height() );
47 gp.setRasterOp( TQt::XorROP );
48 gp.newPath();
49 VGradient grad( *m_gradient );
50 grad.setOrigin( KoPoint( 0, 0 ) );
51 grad.setVector( KoPoint( m_pixmap.width() - 1, 0 ) );
52 grad.setType( VGradient::linear );
53 VFill fill;
54 fill.gradient() = grad;
55 fill.setType( VFill::grad );
56 gp.setBrush( fill );
57 gp.moveTo( KoPoint( 0, 0 ) );
58 gp.lineTo( KoPoint( 0, m_pixmap.height() - 1 ) );
59 gp.lineTo( KoPoint( m_pixmap.width() - 1, m_pixmap.height() - 1 ) );
60 gp.lineTo( KoPoint( m_pixmap.width() - 1, 0 ) );
61 gp.lineTo( KoPoint( 0, 0 ) );
62 gp.fillPath();
63 gp.end();
64
65 m_delete = TQFileInfo( filename ).isWritable();
66} // VGradientListItem::VGradientListItem
67
68VGradientListItem::VGradientListItem( const VGradientListItem& gradient )
69 : TQListBoxItem( 0L )
70{
71 m_pixmap = gradient.m_pixmap;
72 m_delete = gradient.m_delete;
73 m_gradient = new VGradient( *gradient.gradient() );
74 m_filename = gradient.m_filename;
75} // VGradientListItem::VGradientListItem
76
77VGradientListItem::~VGradientListItem()
78{
79 delete m_gradient;
80} // VGradientListItem::~VGradientListItem
81
82int VGradientListItem::width( const TQListBox* lb ) const
83{
84 return lb->width() - 25;
85} // VGradientListItem::width
86
87void VGradientListItem::paint( TQPainter* painter )
88{
89 painter->save();
90 painter->setRasterOp( TQt::CopyROP );
91 TQRect r ( 0, 0, width( listBox() ), height( listBox() ) );
92 painter->scale( ( (float)( width( listBox() ) ) ) / 200., 1. );
93 painter->drawPixmap( 0, 0, m_pixmap );
94 painter->restore();
95 if ( isSelected() )
96 painter->setPen( listBox()->colorGroup().highlightedText() );
97 else
98 painter->setPen( listBox()->colorGroup().base() );
99 painter->drawRect( r );
100 painter->flush();
101} // VGradientListItem::paint
102
103VGradientPreview::VGradientPreview( VGradient& gradient, double& opacity, TQWidget* parent, const char* name )
104 : TQWidget( parent, name ), m_gradient( &gradient ), m_opacity( &opacity )
105{
106 setBackgroundMode( TQt::NoBackground );
107 setMinimumSize( 70, 70 );
108} // VGradientPreview::VGradientPreview
109
110VGradientPreview::~VGradientPreview()
111{
112} // VGradientPreview::~VGradientPreview
113
114void VGradientPreview::paintEvent( TQPaintEvent* )
115{
116 TQPixmap pixmap( width(), height() );
117 VKoPainter gp( &pixmap, width(), height() );
118 gp.setRasterOp( TQt::XorROP );
119 gp.newPath();
120 VGradient gradient( *m_gradient );
121 if( gradient.type() == VGradient::radial || gradient.type() == VGradient::conic )
122 {
123 gradient.setOrigin( KoPoint( width() / 2, height() / 2 ) );
124 gradient.setFocalPoint( KoPoint( width() / 2, height() / 2 ) );
125 gradient.setVector( KoPoint( width() / 4, height() / 4 ) );
126 }
127 else
128 {
129 gradient.setOrigin( KoPoint( width() / 3, 2 * ( height() / 3 ) ) );
130 gradient.setVector( KoPoint( 2 * ( width() / 3 ), height() / 3 ) );
131 }
132 VFill fill;
133 TDEIconLoader il;
134 fill.pattern() = VPattern( il.iconPath( "karbon.png", TDEIcon::Small ) );
135 fill.setType( VFill::patt );
136 gp.setBrush( fill );
137 gp.fillPath();
138 fill.gradient() = gradient;
139 fill.setType( VFill::grad );
140 VColor c = fill.color();
141 c.setOpacity( *m_opacity );
142 fill.setColor( c, false );
143 gp.setBrush( fill );
144 gp.moveTo( KoPoint( 2, 2 ) );
145 gp.lineTo( KoPoint( 2, height() - 2 ) );
146 gp.lineTo( KoPoint( width() - 2, height() - 2 ) );
147 gp.lineTo( KoPoint( width() - 2, 2 ) );
148 gp.lineTo( KoPoint( 2, 2 ) );
149 gp.fillPath();
150 gp.end();
151
152 TQPainter p( &pixmap );
153
154 p.setPen( colorGroup().light() );
155 p.moveTo( 1, height() - 1 );
156 p.lineTo( 1, 1 );
157 p.lineTo( width() - 1, 1 );
158 p.lineTo( width() - 1, height() - 1 );
159 p.lineTo( 1, height() - 1 );
160 p.setPen( colorGroup().dark() );
161 p.moveTo( 0, height() - 1 );
162 p.lineTo( 0, 0 );
163 p.lineTo( width() - 1, 0 );
164 p.moveTo( width() - 2, 2 );
165 p.lineTo( width() - 2, height() - 2 );
166 p.lineTo( 2, height() - 2 );
167 bitBlt( this, 0, 0, &pixmap, 0, 0, width(), height() );
168} // VGradientPreview::paintEvent
169
170VGradientTabWidget::VGradientTabWidget( VGradient& gradient, KarbonResourceServer* server, TQWidget* parent, const char* name )
171 : TQTabWidget( parent, name ), m_gradient( gradient ), m_resourceServer( server )
172{
173 setupUI();
174 setupConnections();
175 initUI();
176} // VGradientTabWidget::VGradientTabWidget
177
178VGradientTabWidget::~VGradientTabWidget()
179{
180} // VGradientTabWidget::~VGradientTabWidget
181
182void VGradientTabWidget::setupUI()
183{
184 m_editGroup = new TQGroupBox( i18n( "Edit Gradient" ) );
185 TQGridLayout* editLayout = new TQGridLayout( m_editGroup, 7, 3 );
186 editLayout->setSpacing( 3 );
187 editLayout->setMargin( 6 );
188 editLayout->addRowSpacing( 0, 12 );
189 editLayout->addMultiCellWidget( m_gradientPreview = new VGradientPreview( m_gradient, m_gradOpacity, m_editGroup ), 1, 3, 0, 0 );
190 editLayout->addWidget( new TQLabel( i18n( "Type:" ), m_editGroup ), 1, 1 );
191 editLayout->addWidget( new TQLabel( i18n( "Repeat:" ), m_editGroup ), 2, 1 );
192 editLayout->addWidget( new TQLabel( i18n( "Target:" ), m_editGroup ), 3, 1 );
193 editLayout->addWidget( m_gradientType = new KComboBox( false, m_editGroup ), 1, 2 );
194 m_gradientType->insertItem( i18n( "Linear" ), 0 );
195 m_gradientType->insertItem( i18n( "Radial" ), 1 );
196 m_gradientType->insertItem( i18n( "Conical" ), 2 );
197 editLayout->addWidget( m_gradientRepeat = new KComboBox( false, m_editGroup ), 2, 2 );
198 m_gradientRepeat->insertItem( i18n( "None" ), 0 );
199 m_gradientRepeat->insertItem( i18n( "Reflect" ), 1 );
200 m_gradientRepeat->insertItem( i18n( "Repeat" ), 2 );
201 editLayout->addWidget( m_gradientTarget = new KComboBox( false, m_editGroup ), 3, 2 );
202 m_gradientTarget->insertItem( i18n( "Stroke" ), 0 );
203 m_gradientTarget->insertItem( i18n( "Fill" ), 1 );
204 editLayout->addMultiCellWidget( m_addToPredefs = new TQPushButton( i18n( "&Add to Predefined Gradients" ), m_editGroup ), 6, 6, 0, 2 );
205 editLayout->addMultiCellWidget( m_gradientWidget = new VGradientWidget( m_gradient, m_editGroup ), 4, 4, 0, 2 );
206 editLayout->addWidget( new TQLabel( i18n( "Overall opacity:" ), m_editGroup ), 5, 0 );
207 m_opacity = new KIntNumInput( 100, m_editGroup );
208 m_opacity->setRange( 0, 100, 1, true );
209 m_opacity->setValue( 100 );
210 editLayout->addMultiCellWidget( m_opacity, 5, 5, 1, 2 );
211 addTab( m_editGroup, i18n( "Edit" ) );
212
213 TQGroupBox* predefGroup = new TQGroupBox( i18n( "Predefined Gradients" ) );
214 TQGridLayout* predefLayout = new TQGridLayout( predefGroup, 3, 2 );
215 predefLayout->setSpacing( 3 );
216 predefLayout->setMargin( 6 );
217 predefLayout->addRowSpacing( 0, 12 );
218 predefLayout->addMultiCellWidget( m_predefGradientsView = new TDEListBox( predefGroup ), 1, 1, 0, 2 );
219 predefLayout->addWidget( m_predefDelete = new TQPushButton( i18n( "&Delete" ), predefGroup ), 2, 0 );
220 predefLayout->addWidget( m_predefImport = new TQPushButton( i18n( "&Import" ), predefGroup ), 2, 1 );
221 m_predefImport->setEnabled( false );
222 addTab( predefGroup, i18n( "Predefined" ) );
223} // VGradientTabWidget::setupUI
224
225void VGradientTabWidget::setupConnections()
226{
227 connect( m_gradientType, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( combosChange( int ) ) );
228 connect( m_gradientRepeat, TQ_SIGNAL( activated( int ) ), this, TQ_SLOT( combosChange( int ) ) );
229 connect( m_gradientWidget, TQ_SIGNAL( changed() ), m_gradientPreview, TQ_SLOT( update() ) );
230 connect( m_addToPredefs, TQ_SIGNAL( clicked() ), this, TQ_SLOT( addGradientToPredefs() ) );
231 connect( m_predefGradientsView, TQ_SIGNAL( doubleClicked( TQListBoxItem *, const TQPoint & ) ), this, TQ_SLOT( changeToPredef( TQListBoxItem* ) ) );
232 connect( m_predefDelete, TQ_SIGNAL( clicked() ), this, TQ_SLOT( deletePredef() ) );
233 connect( m_opacity, TQ_SIGNAL( valueChanged( int ) ), this, TQ_SLOT( opacityChanged( int ) ) );
234} // VGradientTabWidget::setupConnection
235
236void VGradientTabWidget::initUI()
237{
238 m_gradientType->setCurrentItem( m_gradient.type() );
239 m_gradientRepeat->setCurrentItem( m_gradient.repeatMethod() );
240 m_gradientTarget->setCurrentItem( FILL );
241 m_opacity->setValue( 100 );
242
243 m_predefGradientsView->clear();
244 TQPtrList<VGradientListItem>* gradientList = m_resourceServer->gradients();
245 if( gradientList->count() > 0 )
246 for( VGradientListItem* g = gradientList->first(); g != NULL; g = gradientList->next() )
247 m_predefGradientsView->insertItem( new VGradientListItem( *g ) );
248} // VGradientTabWidget::initUI
249
250double
251VGradientTabWidget::opacity() const
252{
253 return m_opacity->value() / 100.0;
254}
255
256void
257VGradientTabWidget::setOpacity( double opacity )
258{
259 if( opacity < 0.0 || opacity > 1.0 )
260 return;
261
262 m_gradOpacity = opacity;
263 m_opacity->setValue( int(opacity*100.0) );
264}
265
266const VGradient&
267VGradientTabWidget::gradient()
268{
269 return m_gradient;
270} // VGradientTabWidget::gradient
271
272void VGradientTabWidget::setGradient( VGradient& gradient )
273{
274 m_gradient = gradient;
275
276 initUI();
277} // VGradientTabWidget::setGradient
278
279VGradientTabWidget::VGradientTarget VGradientTabWidget::target()
280{
281 return (VGradientTarget)m_gradientTarget->currentItem();
282} // VGradientTabWidget::target
283
284void VGradientTabWidget::setTarget( VGradientTarget target )
285{
286 m_gradientTarget->setCurrentItem( target );
287} // VGradientTabWidget::setTarget
288
289void VGradientTabWidget::combosChange( int )
290{
291 m_gradient.setType( (VGradient::VGradientType)m_gradientType->currentItem() );
292 m_gradient.setRepeatMethod( (VGradient::VGradientRepeatMethod)m_gradientRepeat->currentItem() );
293
294 m_gradientPreview->update();
295} // VGradientTabWidget::combosChange
296
297void VGradientTabWidget::opacityChanged( int value )
298{
299 m_gradOpacity = value / 100.0;
300 m_gradientPreview->update();
301}
302
303void VGradientTabWidget::addGradientToPredefs()
304{
305 VGradientListItem* item = m_resourceServer->addGradient( new VGradient( m_gradient ) );
306 m_predefGradientsView->insertItem( item );
307} // VGradientTabWidget::addGradientToPredefs()
308
309void VGradientTabWidget::predefSelected( TQListBoxItem* item )
310{
311 if( item )
312 {
313 VGradientListItem* gradientItem = (VGradientListItem*)item;
314 m_predefDelete->setEnabled( gradientItem->canDelete() );
315 }
316} // VGradientTabWidget::predefSelected
317
318void VGradientTabWidget::changeToPredef( TQListBoxItem* item )
319{
320 if( item )
321 {
322 VGradientListItem* gradientItem = (VGradientListItem*)item;
323 m_gradient = *gradientItem->gradient();
324 m_gradientType->setCurrentItem( m_gradient.type() );
325 m_gradientRepeat->setCurrentItem( m_gradient.repeatMethod() );
326 m_opacity->setValue( 100 );
327 m_gradientPreview->update();
328 m_gradientWidget->update();
329 showPage( m_editGroup );
330 }
331} // VGradientTabWidget::changeToPredef
332
333void VGradientTabWidget::deletePredef()
334{
335 int i = m_predefGradientsView->currentItem();
336 if( !m_predefGradientsView->item( i ) )
337 return;
338 m_resourceServer->removeGradient( (VGradientListItem*)m_predefGradientsView->item( i ) );
339 m_predefGradientsView->removeItem( i );
340} // VGradientTabWidget::deletePredef
341
342#include "vgradienttabwidget.moc"
343
This class keeps track of color properties.
Definition vcolor.h:41
void setOpacity(float opacity)
Sets the color opacity.
Definition vcolor.h:138
Manages the fill of shapes.
Definition vfill.h:45
@ grad
solid fill
Definition vfill.h:51
@ patt
gradient fill
Definition vfill.h:52