karbon

vcolordocker.cc

00001 /* This file is part of the KDE project
00002    Made by Tomislav Lukman (tomislav.lukman@ck.tel.hr)
00003    Copyright (C) 2002 - 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 #include <qlabel.h>
00022 #include <qlayout.h>
00023 #include <qtabwidget.h>
00024 #include <qwidget.h>
00025 #include <qcolor.h>
00026 #include <qtooltip.h>
00027 #include <qevent.h>
00028 #include <qptrlist.h>
00029 
00030 #include <klocale.h>
00031 #include <KoMainWindow.h>
00032 
00033 #include "karbon_part.h"
00034 #include "karbon_view.h"
00035 #include "karbon_factory.h"
00036 #include "karbon_resourceserver.h"
00037 #include "vcolor.h"
00038 #include "vcolorslider.h"
00039 #include "vselection.h"
00040 #include "vfillcmd.h"
00041 #include "vstrokecmd.h"
00042 #include "vcommand.h"
00043 #include "vobject.h"
00044 
00045 #include "vcolordocker.h"
00046 
00047 #include <ko_hsv_widget.h>
00048 #include <ko_cmyk_widget.h>
00049 #include <ko_rgb_widget.h>
00050 #include <koColor.h>
00051 
00052 #include <kdebug.h>
00053 
00054 VColorDocker::VColorDocker( KarbonPart* part, KarbonView* parent, const char* /*name*/ )
00055     : QWidget(), m_part ( part ), m_view( parent )
00056 {
00057     m_isStrokeDocker = false;
00058     setCaption( i18n( "Color Chooser" ) );
00059 
00060     m_opacity = 1;
00061 
00062     m_fillCmd = 0;
00063     m_strokeCmd = 0;
00064 
00065     mTabWidget = new QTabWidget( this );
00066 
00067     /* ##### HSV WIDGET ##### */
00068     mHSVWidget = new KoHSVWidget( mTabWidget );
00069     connect( mHSVWidget, SIGNAL( sigFgColorChanged( const QColor &) ), this, SLOT( updateFgColor( const QColor &) ) );
00070     connect( mHSVWidget, SIGNAL( sigBgColorChanged( const QColor &) ), this, SLOT( updateBgColor( const QColor &) ) );
00071     connect(this, SIGNAL(fgColorChanged(const QColor &)), mHSVWidget, SLOT(setFgColor(const QColor &)));
00072     connect(this, SIGNAL(bgColorChanged(const QColor &)), mHSVWidget, SLOT(setBgColor(const QColor &)));
00073     mTabWidget->addTab( mHSVWidget, i18n( "HSV" ) );
00074 
00075     /* ##### RGB WIDGET ##### */
00076     mRGBWidget = new KoRGBWidget( mTabWidget );
00077     connect( mRGBWidget, SIGNAL( sigFgColorChanged( const QColor &) ), this, SLOT( updateFgColor( const QColor &) ) );
00078     connect( mRGBWidget, SIGNAL( sigBgColorChanged( const QColor &) ), this, SLOT( updateBgColor( const QColor &) ) );
00079     connect(this, SIGNAL(fgColorChanged(const QColor &)), mRGBWidget, SLOT(setFgColor(const QColor &)));
00080     connect(this, SIGNAL(bgColorChanged(const QColor &)), mRGBWidget, SLOT(setBgColor(const QColor &)));
00081     mTabWidget->addTab( mRGBWidget, i18n( "RGB" ) );
00082 
00083     /* ##### CMYK WIDGET ##### */
00084     /*mCMYKWidget = new KoCMYKWidget( mTabWidget );
00085     connect( mCMYKWidget, SIGNAL( sigFgColorChanged( const QColor &) ), this, SLOT( updateFgColor( const QColor &) ) );
00086     connect( mCMYKWidget, SIGNAL( sigBgColorChanged( const QColor &) ), this, SLOT( updateBgColor( const QColor &) ) );
00087     mTabWidget->addTab( mCMYKWidget, i18n( "CMYK" ) );*/
00088 
00089     //Opacity
00090     mOpacity = new VColorSlider( i18n( "Opacity:" ), QColor( "white" ), QColor( "black" ), 0, 100, 100, this );
00091     //TODO: Make "white" a transparent color
00092     connect( mOpacity, SIGNAL( valueChanged ( int ) ), this, SLOT( updateOpacity() ) );
00093     QToolTip::add( mOpacity, i18n( "Alpha (opacity)" ) );
00094 
00095     QVBoxLayout *mainWidgetLayout = new QVBoxLayout( this, 3 );
00096     mainWidgetLayout->addWidget( mTabWidget );
00097     mainWidgetLayout->addWidget( mOpacity );
00098     mainWidgetLayout->activate();
00099     setMaximumHeight( 174 );
00100     setMinimumWidth( 194 );
00101     
00102 }
00103 
00104 VColorDocker::~VColorDocker()
00105 {
00106 }
00107 
00108 void VColorDocker::updateFgColor(const QColor &c)
00109 {
00110     mHSVWidget->blockSignals(true);
00111     mRGBWidget->blockSignals(true);
00112     //mCMYKWidget->blockSignals(true);
00113 
00114     m_oldColor = m_color;
00115 
00116     m_color = c;
00117 
00118     VColor v = VColor(c);
00119     v.setOpacity( m_opacity );
00120 
00121     VCommandHistory* history = m_part->commandHistory();
00122     const QPtrList<VCommand>* commandList = history->commands();
00123     VStrokeCmd* command = dynamic_cast<VStrokeCmd*>(commandList->getLast());
00124 
00125     if(command == 0 || m_strokeCmd == 0)
00126     {
00127         m_strokeCmd = new VStrokeCmd( &m_part->document(), v );
00128         m_part->addCommand( m_strokeCmd, true );
00129     }
00130     else
00131     {
00132 
00133         QPtrList<VObject> VOldObjectList = command->getSelection()->objects();
00134         QPtrList<VObject> VNewObjectList = m_part->document().selection()->objects();
00135 
00136         if( VOldObjectList == VNewObjectList )
00137         {
00138             m_strokeCmd->changeStroke(v);
00139             m_part->repaintAllViews();
00140         }
00141         else
00142         {
00143             m_strokeCmd = new VStrokeCmd( &m_part->document(), v );
00144             m_part->addCommand( m_strokeCmd, true );
00145         }
00146     }
00147  
00148     emit fgColorChanged( c );
00149 
00150     mHSVWidget->blockSignals(false);
00151     mRGBWidget->blockSignals(false);
00152     //mCMYKWidget->blockSignals(false);
00153 }
00154 
00155 void VColorDocker::updateBgColor(const QColor &c)
00156 {
00157     mHSVWidget->blockSignals(true);
00158     mRGBWidget->blockSignals(true);
00159     //mCMYKWidget->blockSignals(true);
00160 
00161     m_oldColor = m_color;
00162 
00163     m_color = c;
00164 
00165     VColor v = VColor(c);
00166     v.setOpacity( m_opacity );
00167 
00168     VCommandHistory* history = m_part->commandHistory();
00169     const QPtrList<VCommand>* commandList = history->commands();
00170     VFillCmd* command = dynamic_cast<VFillCmd*>(commandList->getLast());
00171 
00172     if(command == 0 || m_fillCmd == 0)
00173     {
00174         m_fillCmd = new VFillCmd( &m_part->document(), VFill(v) );
00175         m_part->addCommand( m_fillCmd, true );
00176     }
00177     else
00178     {
00179 
00180         QPtrList<VObject> VOldObjectList = command->getSelection()->objects();
00181         QPtrList<VObject> VNewObjectList = m_part->document().selection()->objects();
00182 
00183         if( VOldObjectList == VNewObjectList )
00184         {
00185             m_fillCmd->changeFill(VFill(v));
00186             m_part->repaintAllViews();
00187         }
00188         else
00189         {
00190             m_fillCmd = new VFillCmd( &m_part->document(), VFill(v) );
00191             m_part->addCommand( m_fillCmd, true );
00192         }
00193     }
00194  
00195     emit bgColorChanged( c );
00196 
00197     mHSVWidget->blockSignals(false);
00198     mRGBWidget->blockSignals(false);
00199     //mCMYKWidget->blockSignals(false);
00200 }
00201 
00202 void VColorDocker::updateOpacity()
00203 {
00204     m_opacity = mOpacity->value() / 100.0;
00205 
00206     m_oldColor = m_color;
00207 
00208     VColor c = VColor(m_color);
00209     c.setOpacity( m_opacity );
00210 
00211     if ( isStrokeDocker() )
00212         m_part->addCommand( new VStrokeCmd( &m_part->document(), c ), true );
00213     else
00214         m_part->addCommand( new VFillCmd( &m_part->document(), VFill( c ) ), true );
00215 }
00216 
00217 void
00218 VColorDocker::mouseReleaseEvent( QMouseEvent * )
00219 {
00220     //changeColor();
00221 }
00222 
00223 void VColorDocker::setFillDocker()
00224 {
00225     m_isStrokeDocker = false;
00226 }
00227 
00228 void VColorDocker::setStrokeDocker()
00229 {
00230     m_isStrokeDocker = true;
00231 }
00232 
00233 void VColorDocker::update()
00234 {
00235 
00236     mHSVWidget->blockSignals(true);
00237     mRGBWidget->blockSignals(true);
00238     //mCMYKWidget->blockSignals(true);
00239 
00240     int objCnt = m_part->document().selection()->objects().count();
00241 
00242     if( objCnt > 0 )
00243     {
00244         VObject *obj = m_part->document().selection()->objects().getFirst();
00245 
00246         QColor fgColor = QColor(obj->stroke()->color());
00247         QColor bgColor = QColor(obj->fill()->color());
00248 
00249         mHSVWidget->setFgColor(fgColor);
00250         mRGBWidget->setFgColor(fgColor);
00251         //mCMYKWidget->setFgColor(fgColor);
00252             
00253         mHSVWidget->setBgColor(bgColor);
00254         mRGBWidget->setBgColor(bgColor);
00255         //mCMYKWidget->setBgColor(bgColor);
00256     }
00257 
00258     mHSVWidget->blockSignals(false);
00259     mRGBWidget->blockSignals(false);
00260     //mCMYKWidget->blockSignals(false);
00261 }
00262 
00263 #include "vcolordocker.moc"
00264 
KDE Home | KDE Accessibility Home | Description of Access Keys