Back to index

scribus-ng  1.3.4.dfsg+svn20071115
vgradient.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 /* This file is part of the KDE project
00008    Copyright (C) 2002, The Karbon Developers
00009  
00010    This library is free software; you can redistribute it and/or
00011    modify it under the terms of the GNU Library General Public
00012    License as published by the Free Software Foundation; either
00013    version 2 of the License, or (at your option) any later version.
00014  
00015    This library is distributed in the hope that it will be useful,
00016    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018    Library General Public License for more details.
00019  
00020    You should have received a copy of the GNU Library General Public License
00021    along with this library; see the file COPYING.LIB.  If not, write to
00022    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023    Boston, MA 02111-1307, USA.
00024 */
00025 #include "vgradient.h"
00026 #include <algorithm>
00027 
00028 // colorStop comparison function for stable_sort function
00029 bool compareStops( const VColorStop* item1, const VColorStop* item2 ) 
00030 {
00031        double r1 = item1->rampPoint;
00032        double r2 = item2->rampPoint;
00033        return ( r1 < r2 ? true : false );
00034 }
00035 
00036 int VGradient::VColorStopList::compareItems( QPtrCollection::Item item1, QPtrCollection::Item item2 ) const
00037 {
00038        double r1 = ( (VColorStop*)item1 )->rampPoint;
00039        double r2 = ( (VColorStop*)item2 )->rampPoint;
00040 
00041        return ( r1 == r2 ? 0 : r1 < r2 ? -1 : 1 );
00042 } // VGradient::VColorStopList::compareItems
00043 
00044 
00045 void VGradient::VColorStopList::inSort( QPtrCollection::Item d )
00046 {
00047        int index = 0;
00048        first();
00049        register VColorStop *n = first();
00050        while ( n && compareItems((QPtrCollection::Item) n,d) <= 0 ){ // find position in list
00051               n = next();
00052               index++;
00053        }
00054        insertAt( index, d );
00055 }
00056 
00057 VGradient::VGradient( VGradientType type )
00058               : m_type( type )
00059 {
00060        m_colorStops.setAutoDelete( true );
00061 
00062        // set up dummy gradient
00063        QColor color;
00064 
00065        color = QColor(255,0,0);
00066        addStop( color, 0.0, 0.5, 1.0 );
00067 
00068        color = QColor(255,255,0);
00069        addStop( color, 1.0, 0.5, 1.0 );
00070 
00071        setOrigin( FPoint( 0, 0 ) );
00072        setVector( FPoint( 0, 50 ) );
00073        setRepeatMethod( VGradient::reflect );
00074 }
00075 
00076 VGradient::VGradient( const VGradient& gradient )
00077 {
00078        m_colorStops.setAutoDelete( true );
00079 
00080        m_origin             = gradient.m_origin;
00081        m_focalPoint  = gradient.m_focalPoint;
00082        m_vector             = gradient.m_vector;
00083        m_type               = gradient.m_type;
00084        m_repeatMethod       = gradient.m_repeatMethod;
00085 
00086        m_colorStops.clear();
00087        QPtrVector<VColorStop> cs = gradient.colorStops();
00088        std::stable_sort(cs.data(), cs.data() + cs.count(), compareStops);
00089        for( uint i = 0; i < cs.count(); ++i)
00090               m_colorStops.append( new VColorStop( *cs[i] ) );
00091 } // VGradient::VGradient
00092 
00093 VGradient& VGradient::operator=( const VGradient& gradient )
00094 {
00095        m_colorStops.setAutoDelete( true );
00096 
00097        if ( this == &gradient )
00098               return *this;
00099 
00100        m_origin             = gradient.m_origin;
00101        m_focalPoint  = gradient.m_focalPoint;
00102        m_vector             = gradient.m_vector;
00103        m_type               = gradient.m_type;
00104        m_repeatMethod       = gradient.m_repeatMethod;
00105 
00106        m_colorStops.clear();
00107        QPtrVector<VColorStop> cs = gradient.colorStops();
00108        std::stable_sort(cs.data(), cs.data() + cs.count(), compareStops);
00109        for( uint i = 0; i < cs.count(); ++i )
00110               m_colorStops.append( new VColorStop( *cs[i] ) );
00111        return *this;
00112 } // VGradient::operator=
00113 
00114 const QPtrVector<VColorStop> VGradient::colorStops() const
00115 {
00116        QPtrVector<VColorStop> v;
00117        m_colorStops.toVector( &v );
00118        v.setAutoDelete( false );
00119        return v;
00120 } // VGradient::colorStops()
00121 
00122 void
00123 VGradient::clearStops()
00124 {
00125        m_colorStops.clear();
00126 }
00127 
00128 void
00129 VGradient::addStop( const VColorStop& colorStop )
00130 {
00131        m_colorStops.inSort( new VColorStop( colorStop ) );
00132 } // VGradient::addStop
00133 
00134 void
00135 VGradient::addStop( const QColor &color, double rampPoint, double midPoint, double opa, QString name, int shade )
00136 {
00137        // Clamping between 0.0 and 1.0
00138        rampPoint = QMAX( 0.0f, rampPoint );
00139        rampPoint = QMIN( 1.0f, rampPoint );
00140        // Clamping between 0.0 and 1.0
00141        midPoint = QMAX( 0.0f, midPoint );
00142        midPoint = QMIN( 1.0f, midPoint );
00143 
00144        m_colorStops.inSort( new VColorStop( rampPoint, midPoint, color, opa, name, shade ) );
00145 }
00146 
00147 void VGradient::removeStop( const VColorStop& colorstop )
00148 {
00149        m_colorStops.remove( &colorstop );
00150 }
00151 
00152 void VGradient::removeStop( uint n )
00153 {
00154        m_colorStops.remove( n );
00155 }
00156 
00157 void VGradient::filterStops(void)
00158 {
00159        VColorStop* colorStop = NULL;
00160        bool zeroFound = false;
00161        colorStop = m_colorStops.last();
00162        while(colorStop != NULL)
00163        {
00164               if (colorStop->rampPoint == 0.0 && zeroFound)
00165                      m_colorStops.remove();
00166               else if (colorStop->rampPoint == 0.0)
00167                      zeroFound = true;
00168               colorStop = m_colorStops.prev();
00169        }
00170        bool oneFound = false;
00171        colorStop = m_colorStops.first();
00172        while(colorStop != NULL)
00173        {
00174               if (colorStop->rampPoint == 1.0 && oneFound)
00175               {
00176                      bool isLast = (colorStop == m_colorStops.getLast());
00177                      m_colorStops.remove();
00178                      if (isLast)
00179                             colorStop = m_colorStops.next();
00180                      colorStop = m_colorStops.current();
00181               }
00182               else if (colorStop->rampPoint == 1.0)
00183               {
00184                      oneFound = true;
00185                      colorStop = m_colorStops.next();
00186               }
00187               else
00188                      colorStop = m_colorStops.next();
00189        }
00190 }
00191 
00192 void VGradient::transform( const QWMatrix &m )
00193 {
00194        double mx, my;
00195        mx = m.m11() * m_origin.x() + m.m21() * m_origin.y() + m.dx();
00196        my = m.m22() * m_origin.y() + m.m12() * m_origin.x() + m.dy();
00197        m_origin = FPoint(mx, my);
00198        mx = m.m11() * m_focalPoint.x() + m.m21() * m_focalPoint.y() + m.dx();
00199        my = m.m22() * m_focalPoint.y() + m.m12() * m_focalPoint.x() + m.dy();
00200        m_focalPoint = FPoint(mx, my);
00201        mx = m.m11() * m_vector.x() + m.m21() * m_vector.y() + m.dx();
00202        my = m.m22() * m_vector.y() + m.m12() * m_vector.x() + m.dy();
00203        m_vector = FPoint(mx, my);
00204 }