vgradient.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <QMutableListIterator>
00027 #include <QtAlgorithms>
00028
00029 #include "vgradient.h"
00030
00031
00032 bool compareStops(const VColorStop* item1, const VColorStop* item2 )
00033 {
00034 double r1 = item1->rampPoint;
00035 double r2 = item2->rampPoint;
00036 return ( r1 < r2 ? true : false );
00037 }
00038
00039 int VGradient::compareItems(const VColorStop* item1, const VColorStop* item2 ) const
00040 {
00041 double r1 = item1->rampPoint;
00042 double r2 = item2->rampPoint;
00043
00044 return ( r1 == r2 ? 0 : r1 < r2 ? -1 : 1 );
00045 }
00046
00047
00048 void VGradient::inSort( VColorStop* d )
00049 {
00050 int index = 0;
00051 register VColorStop *n = m_colorStops.value(index);
00052 while (n && compareItems(n,d) <= 0)
00053 {
00054 ++index;
00055 n = m_colorStops.value(index);
00056 }
00057 m_colorStops.insert( qMin(index, m_colorStops.size()), d );
00058 }
00059
00060 VGradient::VGradient( VGradientType type ) : m_type( type )
00061 {
00062
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_origin = gradient.m_origin;
00079 m_focalPoint = gradient.m_focalPoint;
00080 m_vector = gradient.m_vector;
00081 m_type = gradient.m_type;
00082 m_repeatMethod = gradient.m_repeatMethod;
00083 clearStops();
00084 QList<VColorStop*> cs = gradient.colorStops();
00085 qStableSort(cs.begin(), cs.end(), compareStops);
00086 for( int i = 0; i < cs.count(); ++i)
00087 m_colorStops.append( new VColorStop( *cs[i] ) );
00088 }
00089
00090 VGradient::~VGradient()
00091 {
00092 clearStops();
00093 }
00094
00095 VGradient& VGradient::operator=( const VGradient& gradient )
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 clearStops();
00107 QList<VColorStop*> cs = gradient.colorStops();
00108 qStableSort(cs.begin(), cs.end(), compareStops);
00109 for( int i = 0; i < cs.count(); ++i )
00110 m_colorStops.append( new VColorStop( *cs[i] ) );
00111 return *this;
00112 }
00113
00114 const QList<VColorStop*>& VGradient::colorStops() const
00115 {
00116 return m_colorStops;
00117 }
00118
00119 void
00120 VGradient::clearStops()
00121 {
00122 while (!m_colorStops.isEmpty())
00123 delete m_colorStops.takeFirst();
00124 }
00125
00126 void
00127 VGradient::addStop( const VColorStop& colorStop )
00128 {
00129 inSort( new VColorStop( colorStop ) );
00130 }
00131
00132 void
00133 VGradient::addStop( const QColor &color, double rampPoint, double midPoint, double opa, QString name, int shade )
00134 {
00135
00136 rampPoint = qMax( 0.0, rampPoint );
00137 rampPoint = qMin( 1.0, rampPoint );
00138
00139 midPoint = qMax( 0.0, midPoint );
00140 midPoint = qMin( 1.0, midPoint );
00141
00142 inSort( new VColorStop( rampPoint, midPoint, color, opa, name, shade ) );
00143 }
00144
00145 void VGradient::removeStop( VColorStop& colorstop )
00146 {
00147 int n = m_colorStops.indexOf(&colorstop);
00148 delete m_colorStops.takeAt(n);
00149 }
00150
00151 void VGradient::removeStop( uint n )
00152 {
00153 delete m_colorStops.takeAt(n);
00154 }
00155
00156 void VGradient::filterStops(void)
00157 {
00158 VColorStop* colorStop = NULL;
00159 bool zeroFound = false;
00160 QMutableListIterator<VColorStop*> i(m_colorStops);
00161 i.toBack();
00162 while (i.hasPrevious())
00163 {
00164 colorStop = i.previous();
00165 if (colorStop->rampPoint == 0.0 && zeroFound)
00166 {
00167 delete i.value();
00168 i.remove();
00169 }
00170 else if (colorStop->rampPoint == 0.0)
00171 zeroFound = true;
00172 }
00173 bool oneFound = false;
00174 i.toFront();
00175 while (i.hasNext())
00176 {
00177 colorStop = i.next();
00178 if (colorStop->rampPoint == 1.0 && oneFound)
00179 {
00180 delete i.value();
00181 i.remove();
00182 }
00183 else if (colorStop->rampPoint == 1.0)
00184 oneFound = true;
00185 }
00186 }
00187
00188 void VGradient::transform( const QMatrix &m )
00189 {
00190 double mx, my;
00191 mx = m.m11() * m_origin.x() + m.m21() * m_origin.y() + m.dx();
00192 my = m.m22() * m_origin.y() + m.m12() * m_origin.x() + m.dy();
00193 m_origin = FPoint(mx, my);
00194 mx = m.m11() * m_focalPoint.x() + m.m21() * m_focalPoint.y() + m.dx();
00195 my = m.m22() * m_focalPoint.y() + m.m12() * m_focalPoint.x() + m.dy();
00196 m_focalPoint = FPoint(mx, my);
00197 mx = m.m11() * m_vector.x() + m.m21() * m_vector.y() + m.dx();
00198 my = m.m22() * m_vector.y() + m.m12() * m_vector.x() + m.dy();
00199 m_vector = FPoint(mx, my);
00200 }