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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/* This file is part of the KDE project
Copyright (C) 2001, 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 <qlabel.h>
#include <qpainter.h>
#include <qlayout.h>
#include <qgroupbox.h>
#include <qpushbutton.h>
#include <qfileinfo.h>
#include <qpixmap.h>
#include <knuminput.h>
#include <kcombobox.h>
#include <klocale.h>
#include <klistbox.h>
#include <kiconloader.h>
#include "vgradientwidget.h"
#include "vgradienttabwidget.h"
#include "karbon_resourceserver.h"
#include "vkopainter.h"
#include "vfill.h"
VGradientListItem::VGradientListItem( const VGradient& gradient, QString filename )
: QListBoxItem( 0L ), m_filename( filename )
{
m_gradient = new VGradient( gradient );
m_pixmap.resize( 200, 16 );
VKoPainter gp( &m_pixmap, m_pixmap.width(), m_pixmap.height() );
gp.setRasterOp( Qt::XorROP );
gp.newPath();
VGradient grad( *m_gradient );
grad.setOrigin( KoPoint( 0, 0 ) );
grad.setVector( KoPoint( m_pixmap.width() - 1, 0 ) );
grad.setType( VGradient::linear );
VFill fill;
fill.gradient() = grad;
fill.setType( VFill::grad );
gp.setBrush( fill );
gp.moveTo( KoPoint( 0, 0 ) );
gp.lineTo( KoPoint( 0, m_pixmap.height() - 1 ) );
gp.lineTo( KoPoint( m_pixmap.width() - 1, m_pixmap.height() - 1 ) );
gp.lineTo( KoPoint( m_pixmap.width() - 1, 0 ) );
gp.lineTo( KoPoint( 0, 0 ) );
gp.fillPath();
gp.end();
m_delete = QFileInfo( filename ).isWritable();
} // VGradientListItem::VGradientListItem
VGradientListItem::VGradientListItem( const VGradientListItem& gradient )
: QListBoxItem( 0L )
{
m_pixmap = gradient.m_pixmap;
m_delete = gradient.m_delete;
m_gradient = new VGradient( *gradient.gradient() );
m_filename = gradient.m_filename;
} // VGradientListItem::VGradientListItem
VGradientListItem::~VGradientListItem()
{
delete m_gradient;
} // VGradientListItem::~VGradientListItem
int VGradientListItem::width( const QListBox* lb ) const
{
return lb->width() - 25;
} // VGradientListItem::width
void VGradientListItem::paint( QPainter* painter )
{
painter->save();
painter->setRasterOp( Qt::CopyROP );
QRect r ( 0, 0, width( listBox() ), height( listBox() ) );
painter->scale( ( (float)( width( listBox() ) ) ) / 200., 1. );
painter->drawPixmap( 0, 0, m_pixmap );
painter->restore();
if ( isSelected() )
painter->setPen( listBox()->colorGroup().highlightedText() );
else
painter->setPen( listBox()->colorGroup().base() );
painter->drawRect( r );
painter->flush();
} // VGradientListItem::paint
VGradientPreview::VGradientPreview( VGradient& gradient, double& opacity, QWidget* parent, const char* name )
: QWidget( parent, name ), m_gradient( &gradient ), m_opacity( &opacity )
{
setBackgroundMode( Qt::NoBackground );
setMinimumSize( 70, 70 );
} // VGradientPreview::VGradientPreview
VGradientPreview::~VGradientPreview()
{
} // VGradientPreview::~VGradientPreview
void VGradientPreview::paintEvent( QPaintEvent* )
{
QPixmap pixmap( width(), height() );
VKoPainter gp( &pixmap, width(), height() );
gp.setRasterOp( Qt::XorROP );
gp.newPath();
VGradient gradient( *m_gradient );
if( gradient.type() == VGradient::radial || gradient.type() == VGradient::conic )
{
gradient.setOrigin( KoPoint( width() / 2, height() / 2 ) );
gradient.setFocalPoint( KoPoint( width() / 2, height() / 2 ) );
gradient.setVector( KoPoint( width() / 4, height() / 4 ) );
}
else
{
gradient.setOrigin( KoPoint( width() / 3, 2 * ( height() / 3 ) ) );
gradient.setVector( KoPoint( 2 * ( width() / 3 ), height() / 3 ) );
}
VFill fill;
KIconLoader il;
fill.pattern() = VPattern( il.iconPath( "karbon.png", KIcon::Small ) );
fill.setType( VFill::patt );
gp.setBrush( fill );
gp.fillPath();
fill.gradient() = gradient;
fill.setType( VFill::grad );
VColor c = fill.color();
c.setOpacity( *m_opacity );
fill.setColor( c, false );
gp.setBrush( fill );
gp.moveTo( KoPoint( 2, 2 ) );
gp.lineTo( KoPoint( 2, height() - 2 ) );
gp.lineTo( KoPoint( width() - 2, height() - 2 ) );
gp.lineTo( KoPoint( width() - 2, 2 ) );
gp.lineTo( KoPoint( 2, 2 ) );
gp.fillPath();
gp.end();
QPainter p( &pixmap );
p.setPen( colorGroup().light() );
p.moveTo( 1, height() - 1 );
p.lineTo( 1, 1 );
p.lineTo( width() - 1, 1 );
p.lineTo( width() - 1, height() - 1 );
p.lineTo( 1, height() - 1 );
p.setPen( colorGroup().dark() );
p.moveTo( 0, height() - 1 );
p.lineTo( 0, 0 );
p.lineTo( width() - 1, 0 );
p.moveTo( width() - 2, 2 );
p.lineTo( width() - 2, height() - 2 );
p.lineTo( 2, height() - 2 );
bitBlt( this, 0, 0, &pixmap, 0, 0, width(), height() );
} // VGradientPreview::paintEvent
VGradientTabWidget::VGradientTabWidget( VGradient& gradient, KarbonResourceServer* server, QWidget* parent, const char* name )
: QTabWidget( parent, name ), m_gradient( gradient ), m_resourceServer( server )
{
setupUI();
setupConnections();
initUI();
} // VGradientTabWidget::VGradientTabWidget
VGradientTabWidget::~VGradientTabWidget()
{
} // VGradientTabWidget::~VGradientTabWidget
void VGradientTabWidget::setupUI()
{
m_editGroup = new QGroupBox( i18n( "Edit Gradient" ) );
QGridLayout* editLayout = new QGridLayout( m_editGroup, 7, 3 );
editLayout->setSpacing( 3 );
editLayout->setMargin( 6 );
editLayout->addRowSpacing( 0, 12 );
editLayout->addMultiCellWidget( m_gradientPreview = new VGradientPreview( m_gradient, m_gradOpacity, m_editGroup ), 1, 3, 0, 0 );
editLayout->addWidget( new QLabel( i18n( "Type:" ), m_editGroup ), 1, 1 );
editLayout->addWidget( new QLabel( i18n( "Repeat:" ), m_editGroup ), 2, 1 );
editLayout->addWidget( new QLabel( i18n( "Target:" ), m_editGroup ), 3, 1 );
editLayout->addWidget( m_gradientType = new KComboBox( false, m_editGroup ), 1, 2 );
m_gradientType->insertItem( i18n( "Linear" ), 0 );
m_gradientType->insertItem( i18n( "Radial" ), 1 );
m_gradientType->insertItem( i18n( "Conical" ), 2 );
editLayout->addWidget( m_gradientRepeat = new KComboBox( false, m_editGroup ), 2, 2 );
m_gradientRepeat->insertItem( i18n( "None" ), 0 );
m_gradientRepeat->insertItem( i18n( "Reflect" ), 1 );
m_gradientRepeat->insertItem( i18n( "Repeat" ), 2 );
editLayout->addWidget( m_gradientTarget = new KComboBox( false, m_editGroup ), 3, 2 );
m_gradientTarget->insertItem( i18n( "Stroke" ), 0 );
m_gradientTarget->insertItem( i18n( "Fill" ), 1 );
editLayout->addMultiCellWidget( m_addToPredefs = new QPushButton( i18n( "&Add to Predefined Gradients" ), m_editGroup ), 6, 6, 0, 2 );
editLayout->addMultiCellWidget( m_gradientWidget = new VGradientWidget( m_gradient, m_editGroup ), 4, 4, 0, 2 );
editLayout->addWidget( new QLabel( i18n( "Overall opacity:" ), m_editGroup ), 5, 0 );
m_opacity = new KIntNumInput( 100, m_editGroup );
m_opacity->setRange( 0, 100, 1, true );
m_opacity->setValue( 100 );
editLayout->addMultiCellWidget( m_opacity, 5, 5, 1, 2 );
addTab( m_editGroup, i18n( "Edit" ) );
QGroupBox* predefGroup = new QGroupBox( i18n( "Predefined Gradients" ) );
QGridLayout* predefLayout = new QGridLayout( predefGroup, 3, 2 );
predefLayout->setSpacing( 3 );
predefLayout->setMargin( 6 );
predefLayout->addRowSpacing( 0, 12 );
predefLayout->addMultiCellWidget( m_predefGradientsView = new KListBox( predefGroup ), 1, 1, 0, 2 );
predefLayout->addWidget( m_predefDelete = new QPushButton( i18n( "&Delete" ), predefGroup ), 2, 0 );
predefLayout->addWidget( m_predefImport = new QPushButton( i18n( "&Import" ), predefGroup ), 2, 1 );
m_predefImport->setEnabled( false );
addTab( predefGroup, i18n( "Predefined" ) );
} // VGradientTabWidget::setupUI
void VGradientTabWidget::setupConnections()
{
connect( m_gradientType, SIGNAL( activated( int ) ), this, SLOT( combosChange( int ) ) );
connect( m_gradientRepeat, SIGNAL( activated( int ) ), this, SLOT( combosChange( int ) ) );
connect( m_gradientWidget, SIGNAL( changed() ), m_gradientPreview, SLOT( update() ) );
connect( m_addToPredefs, SIGNAL( clicked() ), this, SLOT( addGradientToPredefs() ) );
connect( m_predefGradientsView, SIGNAL( doubleClicked( QListBoxItem *, const QPoint & ) ), this, SLOT( changeToPredef( QListBoxItem* ) ) );
connect( m_predefDelete, SIGNAL( clicked() ), this, SLOT( deletePredef() ) );
connect( m_opacity, SIGNAL( valueChanged( int ) ), this, SLOT( opacityChanged( int ) ) );
} // VGradientTabWidget::setupConnection
void VGradientTabWidget::initUI()
{
m_gradientType->setCurrentItem( m_gradient.type() );
m_gradientRepeat->setCurrentItem( m_gradient.repeatMethod() );
m_gradientTarget->setCurrentItem( FILL );
m_opacity->setValue( 100 );
m_predefGradientsView->clear();
QPtrList<VGradientListItem>* gradientList = m_resourceServer->gradients();
if( gradientList->count() > 0 )
for( VGradientListItem* g = gradientList->first(); g != NULL; g = gradientList->next() )
m_predefGradientsView->insertItem( new VGradientListItem( *g ) );
} // VGradientTabWidget::initUI
double
VGradientTabWidget::opacity() const
{
return m_opacity->value() / 100.0;
}
void
VGradientTabWidget::setOpacity( double opacity )
{
if( opacity < 0.0 || opacity > 1.0 )
return;
m_gradOpacity = opacity;
m_opacity->setValue( int(opacity*100.0) );
}
const VGradient&
VGradientTabWidget::gradient()
{
return m_gradient;
} // VGradientTabWidget::gradient
void VGradientTabWidget::setGradient( VGradient& gradient )
{
m_gradient = gradient;
initUI();
} // VGradientTabWidget::setGradient
VGradientTabWidget::VGradientTarget VGradientTabWidget::target()
{
return (VGradientTarget)m_gradientTarget->currentItem();
} // VGradientTabWidget::target
void VGradientTabWidget::setTarget( VGradientTarget target )
{
m_gradientTarget->setCurrentItem( target );
} // VGradientTabWidget::setTarget
void VGradientTabWidget::combosChange( int )
{
m_gradient.setType( (VGradient::VGradientType)m_gradientType->currentItem() );
m_gradient.setRepeatMethod( (VGradient::VGradientRepeatMethod)m_gradientRepeat->currentItem() );
m_gradientPreview->update();
} // VGradientTabWidget::combosChange
void VGradientTabWidget::opacityChanged( int value )
{
m_gradOpacity = value / 100.0;
m_gradientPreview->update();
}
void VGradientTabWidget::addGradientToPredefs()
{
VGradientListItem* item = m_resourceServer->addGradient( new VGradient( m_gradient ) );
m_predefGradientsView->insertItem( item );
} // VGradientTabWidget::addGradientToPredefs()
void VGradientTabWidget::predefSelected( QListBoxItem* item )
{
if( item )
{
VGradientListItem* gradientItem = (VGradientListItem*)item;
m_predefDelete->setEnabled( gradientItem->canDelete() );
}
} // VGradientTabWidget::predefSelected
void VGradientTabWidget::changeToPredef( QListBoxItem* item )
{
if( item )
{
VGradientListItem* gradientItem = (VGradientListItem*)item;
m_gradient = *gradientItem->gradient();
m_gradientType->setCurrentItem( m_gradient.type() );
m_gradientRepeat->setCurrentItem( m_gradient.repeatMethod() );
m_opacity->setValue( 100 );
m_gradientPreview->update();
m_gradientWidget->update();
showPage( m_editGroup );
}
} // VGradientTabWidget::changeToPredef
void VGradientTabWidget::deletePredef()
{
int i = m_predefGradientsView->currentItem();
if( !m_predefGradientsView->item( i ) )
return;
m_resourceServer->removeGradient( (VGradientListItem*)m_predefGradientsView->item( i ) );
m_predefGradientsView->removeItem( i );
} // VGradientTabWidget::deletePredef
#include "vgradienttabwidget.moc"
|