Back to index

scribus-ng  1.3.4.dfsg+svn20071115
vgradientex.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 "vgradientex.h"
00026 #include <algorithm>
00027 
00028 // comparison function for use with stable_sort
00029 bool compareStopsEx( const VColorStopEx* item1, const VColorStopEx* item2 )
00030 {
00031        double r1 = item1->rampPoint;
00032        double r2 = item2->rampPoint;
00033        return ( r1 < r2 ? true : false );
00034 }
00035 
00036 int VGradientEx::VColorStopExList::compareItems( QPtrCollection::Item item1, QPtrCollection::Item item2 )
00037 {
00038        double r1 = ( (VColorStopEx*)item1 )->rampPoint;
00039        double r2 = ( (VColorStopEx*)item2 )->rampPoint;
00040 
00041        return ( r1 == r2 ? 0 : r1 < r2 ? -1 : 1 );
00042 } // VGradientEx::VColorStopList::compareItems
00043 
00044 void VGradientEx::VColorStopExList::inSort( QPtrCollection::Item d )
00045 {
00046        int index = 0;
00047        first();
00048        register VColorStopEx *n = first();
00049        while ( n && compareItems((QPtrCollection::Item) n,d) <= 0 ){ // find position in list
00050               n = next();
00051               index++;
00052        }
00053        insertAt( index, d );
00054 }
00055 
00056 VGradientEx::VGradientEx( VGradientEx::Type type ) : m_type( type )
00057 {
00058        m_colorStops.setAutoDelete( true );
00059 
00060        // set up dummy gradient
00061        addStop( ScColor(255,0,0) , 0.0, 0.5, 1.0 );
00062        addStop( ScColor(255,255,0) , 1.0, 0.5, 1.0 );
00063 
00064        setOrigin( FPoint( 0, 0 ) );
00065        setVector( FPoint( 0, 50 ) );
00066        setRepeatMethod( VGradientEx::reflect );
00067 }
00068 
00069 VGradientEx::VGradientEx( const VGradientEx& gradient )
00070 {
00071        m_colorStops.setAutoDelete( true );
00072 
00073        m_origin             = gradient.m_origin;
00074        m_focalPoint  = gradient.m_focalPoint;
00075        m_vector             = gradient.m_vector;
00076        m_type               = gradient.m_type;
00077        m_repeatMethod       = gradient.m_repeatMethod;
00078 
00079        m_colorStops.clear();
00080        QPtrVector<VColorStopEx> cs = gradient.colorStops();
00081        std::stable_sort( cs.data(), cs.data() + cs.count(), compareStopsEx);
00082        for( uint i = 0; i < cs.count(); ++i)
00083               m_colorStops.append( new VColorStopEx( *cs[i] ) );
00084 }
00085 
00086 VGradientEx::VGradientEx( const VGradient& gradient, ScribusDoc& doc )
00087 {
00088        m_colorStops.setAutoDelete( true );
00089 
00090        m_origin             = gradient.origin();
00091        m_focalPoint  = gradient.focalPoint();
00092        m_vector             = gradient.vector();
00093        m_type               = (VGradientEx::Type) gradient.type();
00094        m_repeatMethod       = (VGradientEx::RepeatMethod) gradient.repeatMethod();
00095 
00096        m_colorStops.clear();
00097        QPtrVector<VColorStop> stops = gradient.colorStops();
00098        std::stable_sort( stops.data(), stops.data() + stops.count(), compareStops);
00099        for( uint i = 0; i < stops.count(); ++i)
00100        {
00101               VColorStop stop( *stops[i] );
00102               ScColor color = doc.PageColors[stop.name];
00103               double ramp = stop.rampPoint;
00104               double mid = stop.midPoint;
00105               double opacity = stop.opacity;
00106               int shade = stop.shade;
00107               QString name = stop.name;
00108               m_colorStops.append( new VColorStopEx(ramp, mid, color, opacity, name, shade) );
00109        }
00110 }
00111 
00112 VGradientEx& VGradientEx::operator=( const VGradientEx& gradient )
00113 {
00114        m_colorStops.setAutoDelete( true );
00115 
00116        if ( this == &gradient )
00117               return *this;
00118 
00119        m_origin             = gradient.m_origin;
00120        m_focalPoint  = gradient.m_focalPoint;
00121        m_vector             = gradient.m_vector;
00122        m_type               = gradient.m_type;
00123        m_repeatMethod       = gradient.m_repeatMethod;
00124 
00125        m_colorStops.clear();
00126        QPtrVector<VColorStopEx> cs = gradient.colorStops();
00127        std::stable_sort( cs.data(), cs.data() + cs.count(), compareStopsEx);
00128        for( uint i = 0; i < cs.count(); ++i )
00129               m_colorStops.append( new VColorStopEx( *cs[i] ) );
00130        return *this;
00131 } // VGradientEx::operator=
00132 
00133 const QPtrVector<VColorStopEx> VGradientEx::colorStops() const
00134 {
00135        QPtrVector<VColorStopEx> v;
00136        m_colorStops.toVector( &v );
00137        v.setAutoDelete( false );
00138        return v;
00139 } // VGradientEx::colorStops()
00140 
00141 void
00142 VGradientEx::clearStops()
00143 {
00144        m_colorStops.clear();
00145 }
00146 
00147 void
00148 VGradientEx::addStop( const VColorStopEx& colorStop )
00149 {
00150        m_colorStops.inSort( new VColorStopEx( colorStop ) );
00151 } // VGradientEx::addStop
00152 
00153 void
00154 VGradientEx::addStop( const ScColor &color, double rampPoint, double midPoint, double opa, QString name, int shade )
00155 {
00156        // Clamping between 0.0 and 1.0
00157        rampPoint = QMAX( 0.0f, rampPoint );
00158        rampPoint = QMIN( 1.0f, rampPoint );
00159        // Clamping between 0.0 and 1.0
00160        midPoint = QMAX( 0.0f, midPoint );
00161        midPoint = QMIN( 1.0f, midPoint );
00162 
00163        m_colorStops.inSort( new VColorStopEx( rampPoint, midPoint, color, opa, name, shade ) );
00164 }
00165 
00166 void VGradientEx::removeStop( const VColorStopEx& colorstop )
00167 {
00168        m_colorStops.remove( &colorstop );
00169 }
00170 
00171 void VGradientEx::removeStop( uint n )
00172 {
00173        m_colorStops.remove( n );
00174 }
00175 
00176 void VGradientEx::transform( const QWMatrix &m )
00177 {
00178        double mx, my;
00179        mx = m.m11() * m_origin.x() + m.m21() * m_origin.y() + m.dx();
00180        my = m.m22() * m_origin.y() + m.m12() * m_origin.x() + m.dy();
00181        m_origin = FPoint(mx, my);
00182        mx = m.m11() * m_focalPoint.x() + m.m21() * m_focalPoint.y() + m.dx();
00183        my = m.m22() * m_focalPoint.y() + m.m12() * m_focalPoint.x() + m.dy();
00184        m_focalPoint = FPoint(mx, my);
00185        mx = m.m11() * m_vector.x() + m.m21() * m_vector.y() + m.dx();
00186        my = m.m22() * m_vector.y() + m.m12() * m_vector.x() + m.dy();
00187        m_vector = FPoint(mx, my);
00188 }