karbon

vgradienttabwidget.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001, 2002, 2003 The Karbon Developers
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qlabel.h>
00021 #include <qpainter.h>
00022 #include <qlayout.h>
00023 #include <qgroupbox.h>
00024 #include <qpushbutton.h>
00025 #include <qfileinfo.h>
00026 #include <qpixmap.h>
00027 
00028 #include <knuminput.h>
00029 #include <kcombobox.h>
00030 #include <klocale.h>
00031 #include <klistbox.h>
00032 #include <kiconloader.h>
00033 
00034 #include "vgradientwidget.h"
00035 #include "vgradienttabwidget.h"
00036 #include "karbon_resourceserver.h"
00037 #include "vkopainter.h"
00038 #include "vfill.h"
00039 
00040 VGradientListItem::VGradientListItem( const VGradient& gradient, QString filename )
00041         : QListBoxItem( 0L ), m_filename( filename )
00042 {
00043     m_gradient = new VGradient( gradient );
00044 
00045     m_pixmap.resize( 200, 16 );
00046     VKoPainter gp( &m_pixmap, m_pixmap.width(), m_pixmap.height() );
00047     gp.setRasterOp( Qt::XorROP );
00048     gp.newPath();
00049     VGradient grad( *m_gradient );
00050     grad.setOrigin( KoPoint( 0, 0 ) );
00051     grad.setVector( KoPoint( m_pixmap.width() - 1, 0 ) );
00052     grad.setType( VGradient::linear );
00053     VFill fill;
00054     fill.gradient() = grad;
00055     fill.setType( VFill::grad );
00056     gp.setBrush( fill );
00057     gp.moveTo( KoPoint( 0, 0 ) );
00058     gp.lineTo( KoPoint( 0, m_pixmap.height() - 1 ) );
00059     gp.lineTo( KoPoint( m_pixmap.width() - 1, m_pixmap.height() - 1 ) );
00060     gp.lineTo( KoPoint( m_pixmap.width() - 1, 0 ) );
00061     gp.lineTo( KoPoint( 0, 0 ) );
00062     gp.fillPath();
00063     gp.end();
00064 
00065     m_delete = QFileInfo( filename ).isWritable();
00066 } // VGradientListItem::VGradientListItem
00067 
00068 VGradientListItem::VGradientListItem( const VGradientListItem& gradient )
00069         : QListBoxItem( 0L )
00070 {
00071     m_pixmap = gradient.m_pixmap;
00072     m_delete = gradient.m_delete;
00073     m_gradient = new VGradient( *gradient.gradient() );
00074     m_filename = gradient.m_filename;
00075 } // VGradientListItem::VGradientListItem
00076 
00077 VGradientListItem::~VGradientListItem()
00078 {
00079     delete m_gradient;
00080 } // VGradientListItem::~VGradientListItem
00081 
00082 int VGradientListItem::width( const QListBox* lb ) const
00083 {
00084     return lb->width() - 25;
00085 } // VGradientListItem::width
00086 
00087 void VGradientListItem::paint( QPainter* painter )
00088 {
00089     painter->save();
00090     painter->setRasterOp( Qt::CopyROP );
00091     QRect r ( 0, 0, width( listBox() ), height( listBox() ) );
00092     painter->scale( ( (float)( width( listBox() ) ) ) / 200., 1. );
00093     painter->drawPixmap( 0, 0, m_pixmap );
00094     painter->restore();
00095     if ( isSelected() )
00096         painter->setPen( listBox()->colorGroup().highlightedText() );
00097     else
00098         painter->setPen( listBox()->colorGroup().base() );
00099     painter->drawRect( r );
00100     painter->flush();
00101 } // VGradientListItem::paint
00102 
00103 VGradientPreview::VGradientPreview( VGradient& gradient, double& opacity, QWidget* parent, const char* name )
00104         : QWidget( parent, name ), m_gradient( &gradient ), m_opacity( &opacity )
00105 {
00106     setBackgroundMode( Qt::NoBackground );
00107     setMinimumSize( 70, 70 );
00108 } // VGradientPreview::VGradientPreview
00109 
00110 VGradientPreview::~VGradientPreview()
00111 {
00112 } // VGradientPreview::~VGradientPreview
00113 
00114 void VGradientPreview::paintEvent( QPaintEvent* )
00115 {
00116     QPixmap pixmap( width(), height() );
00117     VKoPainter gp( &pixmap, width(), height() );
00118     gp.setRasterOp( Qt::XorROP );
00119     gp.newPath();
00120     VGradient gradient( *m_gradient );
00121     if( gradient.type() == VGradient::radial || gradient.type() == VGradient::conic )
00122     {
00123         gradient.setOrigin( KoPoint( width() / 2, height() / 2 ) );
00124         gradient.setFocalPoint( KoPoint( width() / 2, height() / 2 ) );
00125         gradient.setVector( KoPoint( width() / 4, height() / 4 ) );
00126     }
00127     else
00128     {
00129         gradient.setOrigin( KoPoint( width() / 3, 2 * ( height() / 3 ) ) );
00130         gradient.setVector( KoPoint( 2 * ( width() / 3 ), height() / 3 ) );
00131     }
00132     VFill fill;
00133     KIconLoader il;
00134     fill.pattern() = VPattern( il.iconPath( "karbon.png", KIcon::Small ) );
00135     fill.setType( VFill::patt );
00136     gp.setBrush( fill );
00137     gp.fillPath();
00138     fill.gradient() = gradient;
00139     fill.setType( VFill::grad );
00140     VColor c = fill.color();
00141     c.setOpacity( *m_opacity );
00142     fill.setColor( c, false );
00143     gp.setBrush( fill );
00144     gp.moveTo( KoPoint( 2, 2 ) );
00145     gp.lineTo( KoPoint( 2, height() - 2 ) );
00146     gp.lineTo( KoPoint( width() - 2, height() - 2 ) );
00147     gp.lineTo( KoPoint( width() - 2, 2 ) );
00148     gp.lineTo( KoPoint( 2, 2 ) );
00149     gp.fillPath();
00150     gp.end();
00151 
00152     QPainter p( &pixmap );
00153 
00154     p.setPen( colorGroup().light() );
00155     p.moveTo( 1, height() - 1 );
00156     p.lineTo( 1, 1 );
00157     p.lineTo( width() - 1, 1 );
00158     p.lineTo( width() - 1, height() - 1 );
00159     p.lineTo( 1, height() - 1 );
00160     p.setPen( colorGroup().dark() );
00161     p.moveTo( 0, height() - 1 );
00162     p.lineTo( 0, 0 );
00163     p.lineTo( width() - 1, 0 );
00164     p.moveTo( width() - 2, 2 );
00165     p.lineTo( width() - 2, height() - 2 );
00166     p.lineTo( 2, height() - 2 );
00167     bitBlt( this, 0, 0, &pixmap, 0, 0, width(), height() );
00168 } // VGradientPreview::paintEvent
00169 
00170 VGradientTabWidget::VGradientTabWidget( VGradient& gradient, KarbonResourceServer* server, QWidget* parent, const char* name )
00171         : QTabWidget( parent, name ), m_gradient( gradient ), m_resourceServer( server )
00172 {
00173     setupUI();
00174     setupConnections();
00175     initUI();
00176 } // VGradientTabWidget::VGradientTabWidget
00177 
00178 VGradientTabWidget::~VGradientTabWidget()
00179 {
00180 } // VGradientTabWidget::~VGradientTabWidget
00181 
00182 void VGradientTabWidget::setupUI()
00183 {
00184     m_editGroup = new QGroupBox( i18n( "Edit Gradient" ) );
00185     QGridLayout* editLayout = new QGridLayout( m_editGroup, 7, 3 );
00186     editLayout->setSpacing( 3 );
00187     editLayout->setMargin( 6 );
00188     editLayout->addRowSpacing( 0, 12 );
00189     editLayout->addMultiCellWidget( m_gradientPreview = new VGradientPreview( m_gradient, m_gradOpacity, m_editGroup ), 1, 3, 0, 0 );
00190     editLayout->addWidget( new QLabel( i18n( "Type:" ), m_editGroup ), 1, 1 );
00191     editLayout->addWidget( new QLabel( i18n( "Repeat:" ), m_editGroup ), 2, 1 );
00192     editLayout->addWidget( new QLabel( i18n( "Target:" ), m_editGroup ), 3, 1 );
00193     editLayout->addWidget( m_gradientType = new KComboBox( false, m_editGroup ), 1, 2 );
00194     m_gradientType->insertItem( i18n( "Linear" ), 0 );
00195     m_gradientType->insertItem( i18n( "Radial" ), 1 );
00196     m_gradientType->insertItem( i18n( "Conical" ), 2 );
00197     editLayout->addWidget( m_gradientRepeat = new KComboBox( false, m_editGroup ), 2, 2 );
00198     m_gradientRepeat->insertItem( i18n( "None" ), 0 );
00199     m_gradientRepeat->insertItem( i18n( "Reflect" ), 1 );
00200     m_gradientRepeat->insertItem( i18n( "Repeat" ), 2 );
00201     editLayout->addWidget( m_gradientTarget = new KComboBox( false, m_editGroup ), 3, 2 );
00202     m_gradientTarget->insertItem( i18n( "Stroke" ), 0 );
00203     m_gradientTarget->insertItem( i18n( "Fill" ), 1 );
00204     editLayout->addMultiCellWidget( m_addToPredefs = new QPushButton( i18n( "&Add to Predefined Gradients" ), m_editGroup ), 6, 6, 0, 2 );
00205     editLayout->addMultiCellWidget( m_gradientWidget = new VGradientWidget( m_gradient, m_editGroup ), 4, 4, 0, 2 );
00206     editLayout->addWidget( new QLabel( i18n( "Overall opacity:" ), m_editGroup ), 5, 0 );
00207     m_opacity = new KIntNumInput( 100, m_editGroup );
00208     m_opacity->setRange( 0, 100, 1, true );
00209     m_opacity->setValue( 100 );
00210     editLayout->addMultiCellWidget( m_opacity, 5, 5, 1, 2 );
00211     addTab( m_editGroup, i18n( "Edit" ) );
00212 
00213     QGroupBox* predefGroup  = new QGroupBox( i18n( "Predefined Gradients" ) );
00214     QGridLayout* predefLayout = new QGridLayout( predefGroup, 3, 2 );
00215     predefLayout->setSpacing( 3 );
00216     predefLayout->setMargin( 6 );
00217     predefLayout->addRowSpacing( 0, 12 );
00218     predefLayout->addMultiCellWidget( m_predefGradientsView = new KListBox( predefGroup ), 1, 1, 0, 2 );
00219     predefLayout->addWidget( m_predefDelete = new QPushButton( i18n( "&Delete" ), predefGroup ), 2, 0 );
00220     predefLayout->addWidget( m_predefImport = new QPushButton( i18n( "&Import" ), predefGroup ), 2, 1 );
00221     m_predefImport->setEnabled( false );
00222     addTab( predefGroup, i18n( "Predefined" ) );
00223 } // VGradientTabWidget::setupUI
00224 
00225 void VGradientTabWidget::setupConnections()
00226 {
00227     connect( m_gradientType,        SIGNAL( activated( int ) ),             this, SLOT( combosChange( int ) ) );
00228     connect( m_gradientRepeat,      SIGNAL( activated( int ) ),             this, SLOT( combosChange( int ) ) );
00229     connect( m_gradientWidget,      SIGNAL( changed() ),                    m_gradientPreview, SLOT( update() ) );
00230     connect( m_addToPredefs,        SIGNAL( clicked() ),                    this, SLOT( addGradientToPredefs() ) );
00231     connect( m_predefGradientsView, SIGNAL( doubleClicked( QListBoxItem *, const QPoint & ) ),  this, SLOT( changeToPredef( QListBoxItem* ) ) );
00232     connect( m_predefDelete,        SIGNAL( clicked() ),                    this, SLOT( deletePredef() ) );
00233     connect( m_opacity,             SIGNAL( valueChanged( int ) ),          this, SLOT( opacityChanged( int ) ) );
00234 } // VGradientTabWidget::setupConnection
00235 
00236 void VGradientTabWidget::initUI()
00237 {
00238     m_gradientType->setCurrentItem( m_gradient.type() );
00239     m_gradientRepeat->setCurrentItem( m_gradient.repeatMethod() );
00240     m_gradientTarget->setCurrentItem( FILL );
00241     m_opacity->setValue( 100 );
00242 
00243     m_predefGradientsView->clear();
00244     QPtrList<VGradientListItem>* gradientList = m_resourceServer->gradients();
00245     if( gradientList->count() > 0 )
00246         for( VGradientListItem* g = gradientList->first(); g != NULL; g = gradientList->next() )
00247             m_predefGradientsView->insertItem( new VGradientListItem( *g ) );
00248 } // VGradientTabWidget::initUI
00249 
00250 double
00251 VGradientTabWidget::opacity() const
00252 {
00253     return m_opacity->value() / 100.0;
00254 }
00255 
00256 void
00257 VGradientTabWidget::setOpacity( double opacity )
00258 {
00259     if( opacity < 0.0 || opacity > 1.0 )
00260         return;
00261 
00262     m_gradOpacity = opacity;
00263     m_opacity->setValue( int(opacity*100.0) );
00264 }
00265 
00266 const VGradient&
00267 VGradientTabWidget::gradient()
00268 {
00269     return m_gradient;
00270 } // VGradientTabWidget::gradient
00271 
00272 void VGradientTabWidget::setGradient( VGradient& gradient )
00273 {
00274     m_gradient = gradient;
00275 
00276     initUI();
00277 } // VGradientTabWidget::setGradient
00278 
00279 VGradientTabWidget::VGradientTarget VGradientTabWidget::target()
00280 {
00281     return (VGradientTarget)m_gradientTarget->currentItem();
00282 } // VGradientTabWidget::target
00283 
00284 void VGradientTabWidget::setTarget( VGradientTarget target )
00285 {
00286     m_gradientTarget->setCurrentItem( target );
00287 } // VGradientTabWidget::setTarget
00288 
00289 void VGradientTabWidget::combosChange( int )
00290 {
00291     m_gradient.setType( (VGradient::VGradientType)m_gradientType->currentItem() );
00292     m_gradient.setRepeatMethod( (VGradient::VGradientRepeatMethod)m_gradientRepeat->currentItem() );
00293 
00294     m_gradientPreview->update();
00295 } // VGradientTabWidget::combosChange
00296 
00297 void VGradientTabWidget::opacityChanged( int value )
00298 {
00299     m_gradOpacity = value / 100.0;
00300     m_gradientPreview->update();
00301 }
00302 
00303 void VGradientTabWidget::addGradientToPredefs()
00304 {
00305     VGradientListItem* item = m_resourceServer->addGradient( new VGradient( m_gradient ) );
00306     m_predefGradientsView->insertItem( item );
00307 } // VGradientTabWidget::addGradientToPredefs()
00308 
00309 void VGradientTabWidget::predefSelected( QListBoxItem* item )
00310 {
00311     if( item )
00312     {
00313         VGradientListItem* gradientItem = (VGradientListItem*)item;
00314         m_predefDelete->setEnabled( gradientItem->canDelete() );
00315     }
00316 } // VGradientTabWidget::predefSelected
00317 
00318 void VGradientTabWidget::changeToPredef( QListBoxItem* item )
00319 {
00320     if( item )
00321     {
00322         VGradientListItem* gradientItem = (VGradientListItem*)item;
00323         m_gradient = *gradientItem->gradient();
00324         m_gradientType->setCurrentItem( m_gradient.type() );
00325         m_gradientRepeat->setCurrentItem( m_gradient.repeatMethod() );
00326         m_opacity->setValue( 100 );
00327         m_gradientPreview->update();
00328         m_gradientWidget->update();
00329         showPage( m_editGroup );
00330     }
00331 } // VGradientTabWidget::changeToPredef
00332 
00333 void VGradientTabWidget::deletePredef()
00334 {
00335     int i = m_predefGradientsView->currentItem();
00336     if( !m_predefGradientsView->item( i ) )
00337         return;
00338     m_resourceServer->removeGradient( (VGradientListItem*)m_predefGradientsView->item( i ) );
00339     m_predefGradientsView->removeItem( i );
00340 } // VGradientTabWidget::deletePredef
00341 
00342 #include "vgradienttabwidget.moc"
00343 
KDE Home | KDE Accessibility Home | Description of Access Keys