karbon

vsmallpreview.cc

00001 /* This file is part of the KDE project
00002    Made by Tomislav Lukman (tomislav.lukman@ck.t-com.hr)
00003    Copyright (C) 2005, The Karbon Developers
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 /* vsmallpreview.cc */
00022 #include <qcolor.h>
00023 #include <qframe.h>
00024 #include <qlabel.h>
00025 #include <qlayout.h>
00026 #include <qpixmap.h>
00027 
00028 #include <klocale.h>
00029 #include <KoPoint.h>
00030 
00031 #include "vcolor.h"
00032 #include "vfill.h"
00033 #include "vgradient.h"
00034 #include "vkopainter.h"
00035 #include "vpattern.h"
00036 #include "vstroke.h"
00037 
00038 #include "vsmallpreview.h"
00039 
00040 #define FRAMEWIDTH      40
00041 
00042 VSmallPreview::VSmallPreview( QWidget* parent, const char* name )
00043     : QWidget( parent, name )
00044 {
00045     /* Create widget layout */
00046     QHBoxLayout *layout = new QHBoxLayout( this, 4 );
00047     m_strokeLabel = new QLabel( i18n( "Stroke: None" ), this );
00048     layout->addWidget( m_strokeLabel );
00049     m_strokeFrame = new QFrame( this );
00050     m_strokeFrame->setFixedWidth ( FRAMEWIDTH );
00051     m_strokeFrame->setFrameStyle( QFrame::GroupBoxPanel | QFrame::Plain );
00052     layout->addWidget( m_strokeFrame );
00053     m_fillLabel = new QLabel( i18n( "Fill: None" ), this );
00054     layout->addWidget( m_fillLabel );
00055     m_fillFrame = new QFrame( this );
00056     m_fillFrame->setFixedWidth ( FRAMEWIDTH );
00057     m_fillFrame->setFrameStyle( QFrame::GroupBoxPanel | QFrame::Plain );
00058     layout->addWidget( m_fillFrame );
00059     layout->activate();
00060 
00061     m_fill = VFill();
00062     m_stroke = VStroke();
00063 }
00064 
00065 VSmallPreview::~VSmallPreview()
00066 {
00067 }
00068 
00069 void
00070 VSmallPreview::update( const VStroke &s, const VFill &f )
00071 {
00072     if( &f )
00073         m_fill = f;
00074     else
00075         m_fill = VFill();
00076     if( &s )
00077         m_stroke = s;
00078     else
00079         m_stroke = VStroke();
00080 
00081     drawStroke( m_stroke );
00082     drawFill( m_fill );
00083 }
00084 
00085 void
00086 VSmallPreview::paintEvent( QPaintEvent* /*event*/ )
00087 {
00088     drawStroke( m_stroke );
00089     drawFill( m_fill );
00090 }
00091 
00092 void
00093 VSmallPreview::drawFill( const VFill &f )
00094 {
00095     VFill fill;
00096     VStroke stroke;
00097     
00098     QPixmap m_pixmap;
00099     m_pixmap.resize( m_fillFrame->width(), m_fillFrame->height() );
00100     VKoPainter* m_painter = new VKoPainter( &m_pixmap, m_fillFrame->width(), m_fillFrame->height() );
00101 
00102     m_painter->begin();
00103     m_painter->setPen( Qt::NoPen );
00104     fill.setColor( Qt::white );
00105     m_painter->setBrush( fill );
00106     m_painter->drawRect( KoRect( 0, 0, m_fillFrame->width(), m_fillFrame->height() ) );
00107 
00108     switch ( f.type() )
00109     {
00110         case VFill::solid:
00111         {
00112             switch ( f.color().colorSpace() )
00113             {
00114                 case VColor::rgb:
00115                     m_fillLabel->setText( i18n( "Fill: RGB") ); break;
00116                 case VColor::cmyk:
00117                     m_fillLabel->setText( i18n( "Fill: CMYK") ); break;
00118                 case VColor::hsb:
00119                     m_fillLabel->setText( i18n( "Fill: HSB") ); break;
00120                 case VColor::gray:
00121                     m_fillLabel->setText( i18n( "Fill: Grayscale") ); break;
00122                 default:
00123                     m_fillLabel->setText( i18n( "Fill: Color") );
00124             }
00125             fill.setColor( f.color() );
00126             break;
00127         }
00128         case VFill::grad:
00129         {
00130             fill.gradient() = f.gradient();
00131             fill.setType( VFill::grad );
00132             m_fillLabel->setText( i18n( "Fill: Gradient") );
00133             if( f.gradient().type() == VGradient::linear )
00134             {
00135                 fill.gradient().setOrigin( KoPoint( m_fillFrame->width() * 0.5, 0 ) );
00136                 fill.gradient().setVector( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() ) );
00137             }
00138             else if( f.gradient().type() == VGradient::radial ||
00139                 f.gradient().type() == VGradient::conic )
00140             {
00141                 fill.gradient().setOrigin( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() * 0.5 ) );
00142                 fill.gradient().setFocalPoint( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() * 0.5 ) );
00143                 fill.gradient().setVector( KoPoint( m_fillFrame->width() * 0.5, m_fillFrame->height() ) );
00144             }
00145             break;
00146 
00147         }
00148         case VFill::patt:
00149         {
00150             fill.pattern() = f.pattern();
00151             fill.pattern().setOrigin( KoPoint( 0, 0 ) );
00152             fill.pattern().setVector( KoPoint( m_fillFrame->width() * 0.5, 0 ) );
00153             fill.setType( VFill::patt );
00154             m_fillLabel->setText( i18n( "Fill: Pattern") );
00155             break;
00156         }
00157         default: //None or unknown
00158         {
00159             m_fillLabel->setText( i18n( "Fill: None") );
00160             fill.setColor( Qt::white );
00161             m_painter->setBrush( fill );
00162             m_painter->drawRect( KoRect( 0, 0, m_fillFrame->width(), m_fillFrame->height() ) );
00163             stroke.setColor( Qt::red );
00164             stroke.setLineWidth( 2.0 );
00165             m_painter->setPen( stroke );
00166             m_painter->newPath();
00167             m_painter->moveTo( KoPoint( 4, m_fillFrame->height() - 4 ) );
00168             m_painter->lineTo( KoPoint( m_fillFrame->width() - 4, 4 ) );
00169             m_painter->strokePath();
00170         }
00171     }
00172 
00173     if( f.type() != VFill::none )
00174     {
00175         m_painter->setPen( stroke );
00176         m_painter->setBrush( fill );
00177         m_painter->drawRect( KoRect( 0, 0, m_fillFrame->width(), m_fillFrame->height() ) ); 
00178     }
00179 
00180     m_painter->end();
00181 
00182     bitBlt( m_fillFrame, m_fillFrame->frameWidth(), m_fillFrame->frameWidth(), &m_pixmap, m_fillFrame->frameWidth(), m_fillFrame->frameWidth(), m_fillFrame->width() - m_fillFrame->frameWidth(), m_fillFrame->height() - m_fillFrame->frameWidth(), CopyROP );
00183 
00184     delete ( m_painter );
00185 }
00186 
00187 void
00188 VSmallPreview::drawStroke( const VStroke &s )
00189 {
00190     VFill fill;
00191     VStroke stroke;
00192 
00193     QPixmap m_pixmap;
00194     m_pixmap.resize( m_fillFrame->width(), m_fillFrame->height() );
00195     VKoPainter* m_painter = new VKoPainter( &m_pixmap, m_fillFrame->width(), m_fillFrame->height() );
00196 
00197     m_painter->begin();
00198     m_painter->setPen( Qt::NoPen );
00199     fill.setColor( Qt::white );
00200     m_painter->setBrush( fill );
00201     m_painter->drawRect( KoRect( 0, 0, m_strokeFrame->width(), m_strokeFrame->height() ) );
00202     
00203     switch ( s.type() )
00204     {
00205         case VStroke::solid:
00206         {
00207             switch ( s.color().colorSpace() )
00208             {
00209                 case VColor::rgb:
00210                     m_strokeLabel->setText( i18n( "Stroke: RGB") ); break;
00211                 case VColor::cmyk:
00212                     m_strokeLabel->setText( i18n( "Stroke: CMYK") ); break;
00213                 case VColor::hsb:
00214                     m_strokeLabel->setText( i18n( "Stroke: HSB") ); break;
00215                 case VColor::gray:
00216                     m_strokeLabel->setText( i18n( "Stroke: Grayscale") ); break;
00217                 default:
00218                     m_strokeLabel->setText( i18n( "Stroke: Color") );
00219             }
00220             fill.setColor( s.color() );
00221             break;
00222         }
00223         case VStroke::grad:
00224         {
00225             fill.gradient() = s.gradient();
00226             fill.setType( VFill::grad );
00227             m_strokeLabel->setText( i18n( "Stroke: Gradient") );
00228             if( s.gradient().type() == VGradient::linear )
00229             {
00230                 fill.gradient().setOrigin( KoPoint( m_strokeFrame->width() * 0.5, 0 ) );
00231                 fill.gradient().setVector( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() ) );
00232             }
00233             else if( s.gradient().type() == VGradient::radial ||
00234                 s.gradient().type() == VGradient::conic )
00235             {
00236                 fill.gradient().setOrigin( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() * 0.5 ) );
00237                 fill.gradient().setFocalPoint( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() * 0.5 ) );
00238                 fill.gradient().setVector( KoPoint( m_strokeFrame->width() * 0.5, m_strokeFrame->height() ) );
00239             }
00240             break;
00241         }
00242         case VStroke::patt:
00243         {
00244             fill.pattern() = s.pattern();
00245             fill.pattern().setOrigin( KoPoint( 0, 0 ) );
00246             fill.pattern().setVector( KoPoint( m_strokeFrame->width() * 0.5, 0 ) );
00247             fill.setType( VFill::patt );
00248             m_strokeLabel->setText( i18n( "Stroke: Pattern") );
00249             break;
00250         }
00251         default: //None or unknown
00252         {
00253             m_strokeLabel->setText( i18n( "Stroke: None") );
00254             fill.setColor( Qt::white );
00255             m_painter->setBrush( fill );
00256             m_painter->drawRect( KoRect( 0, 0, m_strokeFrame->width(), m_strokeFrame->height() ) );
00257             stroke.setColor( Qt::red );
00258             stroke.setLineWidth( 2.0 );
00259             m_painter->setPen( stroke );
00260             m_painter->newPath();
00261             m_painter->moveTo( KoPoint( 4, m_strokeFrame->height() - 4 ) );
00262             m_painter->lineTo( KoPoint( m_strokeFrame->width() - 4, 4 ) );
00263             m_painter->strokePath();
00264         }
00265     }
00266 
00267     if( s.type() != VStroke::none )
00268     {
00269         m_painter->setPen( stroke );
00270         m_painter->setBrush( fill );
00271         m_painter->drawRect( KoRect( 0, 0, m_strokeFrame->width(), m_strokeFrame->height() ) );
00272     }
00273 
00274     m_painter->end();
00275 
00276     bitBlt( m_strokeFrame, m_strokeFrame->frameWidth(), m_strokeFrame->frameWidth(), &m_pixmap, m_strokeFrame->frameWidth(), m_strokeFrame->frameWidth(), m_strokeFrame->width() - m_strokeFrame->frameWidth(), m_strokeFrame->height() - m_strokeFrame->frameWidth(), CopyROP );
00277 
00278     delete ( m_painter );
00279 }
00280 
00281 #include "vsmallpreview.moc"
00282 
KDE Home | KDE Accessibility Home | Description of Access Keys