karbon

vstrokefillpreview.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 - 2005, 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 <qcolor.h>
00021 
00022 #include <kdebug.h>
00023 #include <KoPoint.h>
00024 
00025 #include "karbon_part.h"
00026 #include "vcolordlg.h"
00027 #include "vfill.h"
00028 #include "vfillcmd.h"
00029 #include "vkopainter.h"
00030 #include "vselection.h"
00031 #include "vstroke.h"
00032 #include "vstrokecmd.h"
00033 #include "vstrokefillpreview.h"
00034 
00035 #define PANEL_SIZEX     50.0
00036 #define PANEL_SIZEY     50.0
00037 
00038 #define FILL_TOPX       15.0
00039 #define FILL_TOPY       15.0
00040 #define FILL_BOTTOMX        45.0
00041 #define FILL_BOTTOMY        45.0
00042 
00043 #define STROKE_TOPX     5.0
00044 #define STROKE_TOPY     5.0
00045 #define STROKE_BOTTOMX      35.0
00046 #define STROKE_BOTTOMY      35.0
00047 
00048 #define STROKE_TOPX_INNER   STROKE_TOPX + 4
00049 #define STROKE_TOPY_INNER   STROKE_TOPY + 4
00050 #define STROKE_BOTTOMX_INNER    STROKE_BOTTOMX - 4
00051 #define STROKE_BOTTOMY_INNER    STROKE_BOTTOMY - 4
00052 
00053 
00054 VStrokeFillPreview::VStrokeFillPreview(
00055     KarbonPart *part, QWidget* parent, const char* name )
00056         : QFrame( parent, name ), m_part( part )
00057 {
00058     m_strokeWidget = false;
00059     setFocusPolicy( QWidget::NoFocus );
00060 
00061 #if QT_VERSION < 0x030100
00062     setFrameStyle( QFrame::Panel | QFrame::Sunken );
00063 #else
00064     setFrameStyle( QFrame::GroupBoxPanel | QFrame::Sunken );
00065 #endif
00066 
00067     installEventFilter( this );
00068     m_pixmap.resize( int( PANEL_SIZEX ), int( PANEL_SIZEY ) );
00069     m_painter = new VKoPainter( &m_pixmap, uint( PANEL_SIZEX ), uint( PANEL_SIZEY ) );
00070 }
00071 
00072 VStrokeFillPreview::~VStrokeFillPreview()
00073 {
00074     delete( m_painter );
00075 }
00076 
00077 void
00078 VStrokeFillPreview::paintEvent( QPaintEvent* event )
00079 {
00080         bitBlt( this,
00081                 (int)( width() - PANEL_SIZEX ) / 2, (int)( height() - PANEL_SIZEY ) / 2,
00082         &m_pixmap,
00083                 0, 0, (int)PANEL_SIZEX, (int)PANEL_SIZEY );
00084 
00085     QFrame::paintEvent( event );
00086 }
00087 
00088 bool
00089 VStrokeFillPreview::eventFilter( QObject *, QEvent *event )
00090 {
00091     QMouseEvent* e = static_cast<QMouseEvent *>( event );
00092 
00093     int ex = e->x() - int( ( width() - PANEL_SIZEX ) / 2 );
00094     int ey = e->y() - int( ( height() - PANEL_SIZEY ) / 2 );
00095 
00096     if( event && event->type() == QEvent::MouseButtonPress )
00097     {
00098         if ( m_strokeWidget )
00099         {
00100             if(
00101                 ex >= STROKE_TOPX && ex <= STROKE_BOTTOMX &&
00102                 ey >= STROKE_TOPY && ey <= STROKE_BOTTOMY )
00103             {
00104                 m_strokeWidget = true;
00105                 emit strokeSelected();
00106             }
00107             else if(
00108                 ex >= FILL_TOPX && ex <= FILL_BOTTOMX &&
00109                 ey >= FILL_TOPY && ey <= FILL_BOTTOMY )
00110             {
00111                 m_strokeWidget = false;
00112                 emit fillSelected();
00113             }
00114         }
00115         else
00116         {
00117             if(
00118                 ex >= FILL_TOPX && ex <= FILL_BOTTOMX &&
00119                 ey >= FILL_TOPY && ey <= FILL_BOTTOMY )
00120             {
00121                 m_strokeWidget = false;
00122                 emit fillSelected();
00123             }
00124             else if(
00125                 ex >= STROKE_TOPX && ex <= STROKE_BOTTOMX &&
00126                 ey >= STROKE_TOPY && ey <= STROKE_BOTTOMY )
00127             {
00128                 m_strokeWidget = true;
00129                 emit strokeSelected();
00130             }
00131         }
00132         update( m_stroke, m_fill );
00133     }
00134 
00135     if( event && event->type() == QEvent::MouseButtonDblClick )
00136     {
00137         if(
00138             ex >= FILL_TOPX && ex <= FILL_BOTTOMX &&
00139             ey >= FILL_TOPY && ey <= FILL_BOTTOMY )
00140         {
00141             VColorDlg* dialog = new VColorDlg( m_fill.color(), this );
00142             if( dialog->exec() == QDialog::Accepted )
00143             {
00144                 if( m_part && m_part->document().selection() ) m_part->addCommand( new VFillCmd( &m_part->document(), VFill( dialog->Color() ) ), true );
00145             }
00146             delete dialog;
00147         }
00148         else if(
00149             ex >= STROKE_TOPX && ex <= STROKE_BOTTOMX
00150             && ey >= STROKE_TOPY && ey <= STROKE_BOTTOMY )
00151         {
00152             VColorDlg* dialog = new VColorDlg( m_stroke.color(), this );
00153             if( dialog->exec() == QDialog::Accepted )
00154             {
00155                 if( m_part && m_part->document().selection() ) m_part->addCommand( new VStrokeCmd( &m_part->document(), dialog->Color() ), true );
00156             }
00157             delete dialog;
00158         }
00159     }
00160     return false;
00161 }
00162 
00163 void
00164 VStrokeFillPreview::update( const VStroke &s, const VFill &f )
00165 {
00166     m_painter->begin();
00167 
00168     if( &f )
00169         m_fill = f;
00170     else
00171         m_fill = VFill();
00172     if( &s )
00173         m_stroke = s;
00174     else
00175         m_stroke = VStroke();
00176 
00177     // draw checkerboard
00178     VFill fill;
00179     m_painter->setPen( Qt::NoPen );
00180 
00181     for( unsigned char y = 0; y < PANEL_SIZEY; y += 10 )
00182         for( unsigned char x = 0; x < PANEL_SIZEX; x += 10 )
00183         {
00184             fill.setColor( ( ( ( x + y ) % 20 ) == 0 ) ? QColor( 180, 180, 180 ) : QColor( 100, 100, 100 ) );
00185             m_painter->setBrush( fill );
00186             m_painter->drawRect( x, y, 10, 10 );
00187         }
00188 
00189     if ( m_strokeWidget )
00190     {
00191         drawFill( m_fill );
00192         drawStroke( m_stroke );
00193     }
00194     else
00195     {
00196         drawStroke( m_stroke );
00197         drawFill( m_fill );
00198     }
00199 
00200     m_painter->end();
00201 
00202     repaint();
00203 }
00204 
00205 void
00206 VStrokeFillPreview::drawFill( const VFill &f )
00207 {
00208     VStroke stroke;
00209 
00210     if( f.type() != VFill::none )
00211     {
00212         if( f.type() != VFill::solid )
00213         {
00214             VFill fill;
00215             fill = f;
00216 
00217             if( f.type() == VFill::grad )
00218             {
00219                 if( f.gradient().type() == VGradient::linear )
00220                 {
00221                     fill.gradient().setOrigin( KoPoint( 30, 20 ) );
00222                     fill.gradient().setVector( KoPoint( 30, 50 ) );
00223                 }
00224                 else if( f.gradient().type() == VGradient::radial ||
00225                          f.gradient().type() == VGradient::conic )
00226                 {
00227                     fill.gradient().setOrigin( KoPoint( 30, 35 ) );
00228                     fill.gradient().setFocalPoint( KoPoint( 30, 35 ) );
00229                     fill.gradient().setVector( KoPoint( 30, 50 ) );
00230                 }
00231             }
00232             if( f.type() == VFill::patt )
00233             {
00234                 fill.pattern() = f.pattern();
00235                 fill.pattern().setOrigin( KoPoint( 20, 10 ) );
00236                 fill.pattern().setVector( KoPoint( 30, 10 ) );
00237                 fill.setType( VFill::patt );
00238             }
00239 
00240             m_painter->setBrush( fill );
00241         }
00242         else
00243             m_painter->setBrush( f );
00244         m_painter->setPen( Qt::NoPen );
00245         m_painter->drawRect( KoRect( FILL_TOPX, FILL_TOPY, FILL_BOTTOMX - FILL_TOPX, FILL_BOTTOMY - FILL_TOPY ) );
00246     }
00247     else
00248     {
00249         VFill fill;
00250         fill.setColor( Qt::white );
00251         m_painter->setBrush( fill );
00252         m_painter->setPen( Qt::NoPen );
00253 
00254         m_painter->drawRect( KoRect(    FILL_TOPX, FILL_TOPY,
00255                                         FILL_BOTTOMX - FILL_TOPX,
00256                                         FILL_BOTTOMY - FILL_TOPY ) );
00257     }
00258 
00259     // show 3D outline of fill part
00260     VColor color;
00261 
00262     m_painter->setBrush( Qt::NoBrush );
00263     color.set( 1.0, 1.0, 1.0 );
00264     stroke.setColor( color );
00265     m_painter->setPen( stroke );
00266 
00267     m_painter->newPath();
00268     m_painter->moveTo( KoPoint( FILL_BOTTOMX, FILL_TOPY ) );
00269     m_painter->lineTo( KoPoint( FILL_TOPX, FILL_TOPY ) );
00270     m_painter->lineTo( KoPoint( FILL_TOPX, FILL_BOTTOMY ) );
00271     m_painter->strokePath();
00272 
00273     color.set( 0.5, 0.5, 0.5 );
00274     stroke.setColor( color );
00275     m_painter->setPen( stroke );
00276 
00277     m_painter->newPath();
00278     m_painter->moveTo( KoPoint( FILL_BOTTOMX, FILL_TOPY ) );
00279     m_painter->lineTo( KoPoint( FILL_BOTTOMX, FILL_BOTTOMY ) );
00280     m_painter->lineTo( KoPoint( FILL_TOPX, FILL_BOTTOMY ) );
00281     m_painter->strokePath();
00282 
00283     if( f.type() == VFill::none )
00284     {
00285         stroke.setColor( Qt::red );
00286         m_painter->setPen( stroke );
00287         m_painter->newPath();
00288         m_painter->moveTo( KoPoint( FILL_BOTTOMX, FILL_TOPY ) );
00289         m_painter->lineTo( KoPoint( FILL_TOPX, FILL_BOTTOMY ) );
00290         m_painter->strokePath();
00291     }
00292 }
00293 
00294 void
00295 VStrokeFillPreview::drawStroke( const VStroke &s )
00296 {
00297     VStroke stroke;
00298     stroke.setLineWidth( 2.0 );
00299 
00300     m_painter->setPen( Qt::NoPen );
00301 
00302     if( s.type() != VStroke::none )
00303     {
00304         VFill fill;
00305 
00306         if( s.type() != VStroke::solid )
00307         {
00308             if( s.type() == VStroke::grad )
00309             {
00310                 fill.gradient() = s.gradient();
00311 
00312                 if( s.gradient().type() == VGradient::linear )
00313                 {
00314                     fill.gradient().setOrigin( KoPoint( FILL_TOPX, 10 ) );
00315                     fill.gradient().setVector( KoPoint( FILL_TOPX, 40 ) );
00316                 }
00317                 else if( s.gradient().type() == VGradient::radial ||
00318                          s.gradient().type() == VGradient::conic )
00319                 {
00320                     fill.gradient().setOrigin( KoPoint( FILL_TOPX, 25 ) );
00321                     fill.gradient().setFocalPoint( KoPoint( FILL_TOPX, 25 ) );
00322                     fill.gradient().setVector( KoPoint( FILL_TOPX, 40 ) );
00323                 }
00324 
00325                 fill.setType( VFill::grad );
00326             }
00327             if( s.type() == VStroke::patt )
00328             {
00329                 fill.pattern() = s.pattern();
00330                 fill.pattern().setOrigin( KoPoint( FILL_TOPX, 10 ) );
00331                 fill.pattern().setVector( KoPoint( FILL_TOPX, 40 ) );
00332                 fill.setType( VFill::patt );
00333             }
00334         }
00335         else
00336             fill.setColor( s.color() );
00337 
00338         m_painter->setFillRule( evenOdd );
00339 
00340         m_painter->setBrush( fill );
00341 
00342         m_painter->newPath();
00343         m_painter->moveTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
00344         m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_TOPY ) );
00345         m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_BOTTOMY ) );
00346         m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_BOTTOMY ) );
00347         m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
00348 
00349         m_painter->moveTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
00350         m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_TOPY_INNER ) );
00351         m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_BOTTOMY_INNER ) );
00352         m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_BOTTOMY_INNER ) );
00353         m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
00354         m_painter->fillPath();
00355     }
00356     else
00357     {
00358         VFill fill;
00359         m_painter->setFillRule( evenOdd );
00360         fill.setColor( Qt::white );
00361 
00362         m_painter->setBrush( fill );
00363         m_painter->setPen( Qt::NoPen );
00364 
00365         m_painter->newPath();
00366         m_painter->moveTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
00367         m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_TOPY ) );
00368         m_painter->lineTo( KoPoint( STROKE_BOTTOMX, STROKE_BOTTOMY ) );
00369         m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_BOTTOMY ) );
00370         m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_TOPY ) );
00371 
00372         m_painter->moveTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
00373         m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_TOPY_INNER ) );
00374         m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER, STROKE_BOTTOMY_INNER ) );
00375         m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_BOTTOMY_INNER ) );
00376         m_painter->lineTo( KoPoint( STROKE_TOPX_INNER, STROKE_TOPY_INNER ) );
00377         m_painter->fillPath();
00378     }
00379 
00380     // show 3D outline of stroke part
00381     VColor color;
00382 
00383     color.set( 1.0, 1.0, 1.0 );
00384     stroke.setColor( color );
00385     m_painter->setBrush( Qt::NoBrush );
00386     m_painter->setPen( stroke );
00387 
00388     m_painter->newPath();
00389     m_painter->moveTo( KoPoint( STROKE_BOTTOMX + 1, STROKE_TOPY - 1 ) );
00390     m_painter->lineTo( KoPoint( STROKE_TOPX - 1, STROKE_TOPY - 1 ) );
00391     m_painter->lineTo( KoPoint( STROKE_TOPX - 1, STROKE_BOTTOMY + 1 ) );
00392     m_painter->strokePath();
00393 
00394     color.set( 0.5, 0.5, 0.5 );
00395     stroke.setColor( color );
00396     m_painter->setPen( stroke );
00397 
00398     m_painter->newPath();
00399     m_painter->moveTo( KoPoint( STROKE_BOTTOMX + 1, STROKE_TOPY - 1 ) );
00400     m_painter->lineTo( KoPoint( STROKE_BOTTOMX + 1, STROKE_BOTTOMY + 1 ) );
00401     m_painter->lineTo( KoPoint( STROKE_TOPX - 1, STROKE_BOTTOMY + 1 ) );
00402     m_painter->strokePath();
00403 
00404     //stroke.setColor( Qt::black.rgb() );
00405     //m_painter->setPen( stroke );
00406     m_painter->newPath();
00407     m_painter->moveTo( KoPoint( STROKE_BOTTOMX_INNER - 1, STROKE_TOPY_INNER + 1 ) );
00408     m_painter->lineTo( KoPoint( STROKE_TOPX_INNER + 1, STROKE_TOPY_INNER + 1 ) );
00409     m_painter->lineTo( KoPoint( STROKE_TOPX_INNER + 1, STROKE_BOTTOMY_INNER - 1 ) );
00410     m_painter->strokePath();
00411 
00412     color.set( 1.0, 1.0, 1.0 );
00413     stroke.setColor( color );
00414     m_painter->setPen( stroke );
00415 
00416     m_painter->newPath();
00417     m_painter->moveTo( KoPoint( STROKE_BOTTOMX_INNER - 1, STROKE_TOPY_INNER + 1 ) );
00418     m_painter->lineTo( KoPoint( STROKE_BOTTOMX_INNER - 1, STROKE_BOTTOMY_INNER - 1 ) );
00419     m_painter->lineTo( KoPoint( STROKE_TOPX_INNER + 1, STROKE_BOTTOMY_INNER - 1 ) );
00420     m_painter->strokePath();
00421 
00422     if( s.type() == VStroke::none )
00423     {
00424         stroke.setColor( Qt::red );
00425         m_painter->setPen( stroke );
00426 
00427         m_painter->newPath();
00428         m_painter->moveTo( KoPoint( STROKE_BOTTOMX, STROKE_TOPY ) );
00429         m_painter->lineTo( KoPoint( STROKE_TOPX, STROKE_BOTTOMY ) );
00430         m_painter->strokePath();
00431     }
00432 }
00433 
00434 void
00435 VStrokeFillPreview::setFillSelected()
00436 {
00437     m_strokeWidget = false;
00438     update( m_stroke, m_fill );
00439     emit fillSelected();
00440 }
00441 
00442 void
00443 VStrokeFillPreview::setStrokeSelected()
00444 {
00445     m_strokeWidget = true;
00446     update( m_stroke, m_fill );
00447     emit strokeSelected();
00448 }
00449 
00450 #include "vstrokefillpreview.moc"
00451 
KDE Home | KDE Accessibility Home | Description of Access Keys