|
1 /* |
|
2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Implementation of CHuiLineVisual, Visual that is able to draw |
|
15 * lines onto a curve path. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include "uiacceltk/HuiLineVisual.h" // Class definition |
|
22 #include "uiacceltk/HuiControl.h" |
|
23 #include "uiacceltk/HuiDrawing.h" |
|
24 #include "uiacceltk/HuiUtil.h" |
|
25 |
|
26 |
|
27 EXPORT_C CHuiLineVisual* CHuiLineVisual::AddNewL(CHuiControl& aOwnerControl, |
|
28 CHuiLayout* aParentLayout) |
|
29 { |
|
30 CHuiLineVisual* line = STATIC_CAST(CHuiLineVisual*, |
|
31 aOwnerControl.AppendVisualL(EHuiVisualTypeLine, aParentLayout)); |
|
32 return line; |
|
33 } |
|
34 |
|
35 |
|
36 CHuiLineVisual::CHuiLineVisual(MHuiVisualOwner& aOwner) |
|
37 : CHuiVisual(aOwner), iColor(KRgbWhite) |
|
38 { |
|
39 iThickness.Set(12.f); |
|
40 iEndPos.Set(1.f); |
|
41 } |
|
42 |
|
43 |
|
44 void CHuiLineVisual::ConstructL() |
|
45 { |
|
46 CHuiVisual::ConstructL(); |
|
47 |
|
48 // Use the default line texture by default. |
|
49 iImage = THuiImage(Env().Skin().TextureL(EHuiSkinLineTexture)); |
|
50 } |
|
51 |
|
52 |
|
53 CHuiLineVisual::~CHuiLineVisual() |
|
54 { |
|
55 iAlphaFunction = NULL; |
|
56 iWidthFunction = NULL; |
|
57 } |
|
58 |
|
59 |
|
60 /*EXPORT_C void CHuiLineVisual::LayoutToPath() |
|
61 { |
|
62 TRect bounds(TPoint(0, 0), TSize(1, 1)); |
|
63 |
|
64 for(TInt i = 0; i < iPath.Count(); ++i) |
|
65 { |
|
66 bounds.BoundingRect(TRect(iPath[i].iPoint, TSize(1, 1))); |
|
67 } |
|
68 |
|
69 SetSize(bounds.Size()); |
|
70 }*/ |
|
71 |
|
72 |
|
73 EXPORT_C void CHuiLineVisual::SetPath(CHuiCurvePath* aPath, |
|
74 THuiOwnership aOwnership) |
|
75 { |
|
76 iPath.Set(aPath, aOwnership); |
|
77 SetChanged(); |
|
78 } |
|
79 |
|
80 |
|
81 EXPORT_C CHuiCurvePath& CHuiLineVisual::Path() |
|
82 { |
|
83 SetChanged(); |
|
84 return iPath.Ref(); |
|
85 } |
|
86 |
|
87 |
|
88 void CHuiLineVisual::DrawSelf(CHuiGc& aGc, const TRect& aDisplayRect) const |
|
89 { |
|
90 if(!iPath.Ptr()) |
|
91 { |
|
92 // If the path is empty, ie has no data, return without drawing anything. |
|
93 return; |
|
94 } |
|
95 |
|
96 aGc.Enable(CHuiGc::EFeatureBlending); |
|
97 aGc.SetPenAlpha(TInt(EffectiveOpacity() * 255)); |
|
98 aGc.SetPenColor(iColor); |
|
99 |
|
100 // By default, use the current thickness for the entire path. |
|
101 if ( !HuiUtil::RealCompare( iDefaultWidthFunction.iValue, iThickness.Now() ) ) |
|
102 { |
|
103 iPath.NonConstRef().SetNeedsUpdate(); |
|
104 iDefaultWidthFunction.iValue = iThickness.Now(); |
|
105 } |
|
106 |
|
107 aGc.Enable(CHuiGc::EFeatureTexturing); |
|
108 if(iImage.HasTexture()) |
|
109 { |
|
110 iImage.Texture().Bind(); |
|
111 } |
|
112 |
|
113 iPath->SetVisual(this); |
|
114 aGc.DrawPath(iPath.Ref(), aDisplayRect.iTl, |
|
115 iStartPos.Now(), iEndPos.Now(), |
|
116 iAlphaFunction, |
|
117 iWidthFunction == 0? &iDefaultWidthFunction : 0); |
|
118 iPath->SetVisual(NULL); |
|
119 } |
|
120 |
|
121 |
|
122 TBool CHuiLineVisual::Changed() const |
|
123 { |
|
124 if(CHuiVisual::Changed()) |
|
125 { |
|
126 return ETrue; |
|
127 } |
|
128 |
|
129 return iThickness.Changed() || iShadowThickness.Changed() || |
|
130 iStartPos.Changed() || iEndPos.Changed() || iImage.Changed(); |
|
131 } |
|
132 |
|
133 |
|
134 void CHuiLineVisual::ClearChanged() |
|
135 { |
|
136 CHuiVisual::ClearChanged(); |
|
137 iThickness.ClearChanged(); |
|
138 iShadowThickness.ClearChanged(); |
|
139 iStartPos.ClearChanged(); |
|
140 iEndPos.ClearChanged(); |
|
141 iImage.ClearChanged(); |
|
142 } |
|
143 |
|
144 |
|
145 EXPORT_C void CHuiLineVisual::SetImage(const THuiImage& aImage) |
|
146 { |
|
147 iImage = aImage; |
|
148 SetChanged(); |
|
149 } |
|
150 |
|
151 |
|
152 EXPORT_C void CHuiLineVisual::SetAlphaFunction(MHuiMappingFunction* aFunction) |
|
153 { |
|
154 iAlphaFunction = aFunction; |
|
155 SetChanged(); |
|
156 } |
|
157 |
|
158 |
|
159 EXPORT_C void CHuiLineVisual::SetWidthFunction(MHuiMappingFunction* aFunction) |
|
160 { |
|
161 iWidthFunction = aFunction; |
|
162 SetChanged(); |
|
163 } |
|
164 |
|
165 EXPORT_C void CHuiLineVisual::SetColor(const TRgb& aColor) |
|
166 { |
|
167 iColor = aColor; |
|
168 SetChanged(); |
|
169 } |
|
170 |
|
171 |
|
172 void CHuiLineVisual::ExpandRectWithContent(TRect& aRect) const |
|
173 { |
|
174 if(!iPath.Ptr()) |
|
175 { |
|
176 // If the path is empty, ie has no data, return directly |
|
177 return; |
|
178 } |
|
179 |
|
180 const TInt segmentCount = iPath->SegmentCount(); |
|
181 if(!Clipping() && segmentCount) |
|
182 { |
|
183 |
|
184 // calculate each segment |
|
185 THuiRealRect segmentsCoverRectInMetrics; |
|
186 for ( TInt i = 0 ; i < segmentCount ; i++ ) |
|
187 { |
|
188 const CHuiCurvePath::TSegment segment = iPath->Segment(i); |
|
189 THuiRealRect segmentAreaInMetrics; |
|
190 |
|
191 if ( segment.iType == CHuiCurvePath::ESegmentTypeLine ) |
|
192 { |
|
193 segmentAreaInMetrics = THuiRealRect( |
|
194 THuiRealPoint( segment.iOrigin.iX,segment.iOrigin.iY ), |
|
195 THuiRealSize( segment.iDelta.AsSize().iWidth, segment.iDelta.AsSize().iHeight ) ); |
|
196 } |
|
197 else // ESegmentTypeArc |
|
198 { |
|
199 segmentAreaInMetrics = THuiRealRect( |
|
200 THuiRealPoint( |
|
201 segment.iOrigin.iX - segment.iDelta.AsSize().iWidth, |
|
202 segment.iOrigin.iY - segment.iDelta.AsSize().iHeight ), |
|
203 THuiRealPoint( |
|
204 segment.iOrigin.iX + segment.iDelta.AsSize().iWidth, |
|
205 segment.iOrigin.iY + segment.iDelta.AsSize().iHeight )); |
|
206 } |
|
207 |
|
208 if ( i == 0 ) |
|
209 { |
|
210 // first rect |
|
211 segmentsCoverRectInMetrics = segmentAreaInMetrics; |
|
212 } |
|
213 else |
|
214 { |
|
215 // append to previous rect combination |
|
216 segmentsCoverRectInMetrics.BoundingRect( segmentAreaInMetrics ); |
|
217 } |
|
218 } |
|
219 |
|
220 // Change metrics to pixels |
|
221 THuiRealRect segmentsCoverRectInPixels( |
|
222 LocalPointInPixels( segmentsCoverRectInMetrics.iTl ), |
|
223 LocalPointInPixels( segmentsCoverRectInMetrics.iBr ) ); |
|
224 |
|
225 // check that both height and width != zero |
|
226 if ( !HuiUtil::RealCompare( segmentsCoverRectInPixels.Height(), 0.f ) && |
|
227 !HuiUtil::RealCompare( segmentsCoverRectInPixels.Width(), 0.f ) ) |
|
228 { |
|
229 // move by offset: assume that iOffset is in pixels |
|
230 const THuiRealPoint offsetNowInPixels = iPath->iOffset.Now(); |
|
231 segmentsCoverRectInPixels.Move( offsetNowInPixels.iX,offsetNowInPixels.iY); |
|
232 |
|
233 // move into relation of the visual area (aRect in pixels) |
|
234 segmentsCoverRectInPixels.Move( aRect.iTl.iX, aRect.iTl.iY ); |
|
235 |
|
236 // add the thickness |
|
237 THuiRealSize thicknessInPixels = LocalPointInPixels(THuiRealPoint( iThickness.Now()/2.f, iThickness.Now()/2.f )).AsSize(); |
|
238 |
|
239 // Take the average of the x and y dimensions - what would be the correct behaviour? |
|
240 const TInt averageThincknessInPixels = |
|
241 HUI_ROUND_FLOAT_TO_INT( ( |
|
242 thicknessInPixels.iWidth + |
|
243 thicknessInPixels.iHeight |
|
244 )/2.f ); |
|
245 |
|
246 segmentsCoverRectInPixels.Grow( averageThincknessInPixels, averageThincknessInPixels ); |
|
247 |
|
248 // Finally round to TRect |
|
249 const TRect finalSegmentsCoverArea( |
|
250 HUI_ROUND_FLOAT_TO_INT(segmentsCoverRectInPixels.iTl.iX), |
|
251 HUI_ROUND_FLOAT_TO_INT(segmentsCoverRectInPixels.iTl.iY), |
|
252 HUI_ROUND_FLOAT_TO_INT(segmentsCoverRectInPixels.iBr.iX), |
|
253 HUI_ROUND_FLOAT_TO_INT(segmentsCoverRectInPixels.iBr.iY) ); |
|
254 |
|
255 // combine the calculated segment area and the |
|
256 aRect.BoundingRect(finalSegmentsCoverArea); |
|
257 } |
|
258 } |
|
259 |
|
260 CHuiVisual::ExpandRectWithContent(aRect); |
|
261 } |
|
262 |