summaryrefslogtreecommitdiff
path: root/src/V2d/V2d_Viewer.cxx
blob: 10e9f4b8322c52f30fb3fb251729e9a600e5bc9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//Updates:
//	GG 24/03/98	Add useMFT parameter to SetFontMap method.
//	GG 07/07/98	BUC60258 Add SetMarkMap() method

#define PRO10988	//DCB Resets grid color indices after colormap change

#define IMP080300	//GG 
//			-> Optimize the SetColorMap() and InitializeColor() methods
//			Review the 14/09/01 to avoid regression
//			See new deferred method in Aspect_ColorMap

#include <V2d_Viewer.ixx>
#include <V2d_View.hxx>
#include <V2d_DefaultMap.hxx>
#include <Graphic2d_View.hxx>
#include <Aspect_GenericColorMap.hxx>
#include <Aspect_WindowDriver.hxx>
#include <TColStd_MapOfInteger.hxx>
#include <Aspect_ColorMapEntry.hxx>

/*=================================================================*/
V2d_Viewer::V2d_Viewer(const Handle(Aspect_GraphicDevice)& aGraphicDevice,
		       const Standard_ExtString aName,
		       const Standard_CString aDomain) 
:Viewer_Viewer(aGraphicDevice,aName,aDomain,1),
myColorMap(V2d_DefaultMap::ColorMap()),
myTypeMap(V2d_DefaultMap::TypeMap()),
myWidthMap(V2d_DefaultMap::WidthMap()),
myFontMap(V2d_DefaultMap::FontMap()),
myMarkMap(V2d_DefaultMap::MarkMap()),
myGraphicView(new Graphic2d_View()),
myViews(),
myViewsIterator(),
myRGrid(),
myCGrid()
{
  Init();
}

/*=================================================================*/
V2d_Viewer::V2d_Viewer(const Handle(Aspect_GraphicDevice)& aGraphicDevice,
		       const Handle(Graphic2d_View)& aView,
		       const Standard_ExtString aName,
		       const Standard_CString aDomain) 
:Viewer_Viewer(aGraphicDevice,aName,aDomain,1),
myColorMap(V2d_DefaultMap::ColorMap()),
myTypeMap(V2d_DefaultMap::TypeMap()),
myWidthMap(V2d_DefaultMap::WidthMap()),
myFontMap(V2d_DefaultMap::FontMap()),
myMarkMap(V2d_DefaultMap::MarkMap()),
myGraphicView(aView),
myViews(),
myViewsIterator(),
myRGrid(),
myCGrid()
{
  Init();
}

/*=================================================================*/
void V2d_Viewer::Init() {
  myHitPointMarkerIndex = 1;
  myHitPointColorIndex = 0;
  Standard_Integer i1 = InitializeColor(Quantity_NOC_GRAY50);
  Standard_Integer i2 = InitializeColor(Quantity_NOC_GRAY70);
  myHitPointColorIndex = InitializeColor(Quantity_NOC_WHITE);
  myCoordinatesColorIndex = myHitPointColorIndex ;
  myGridType = Aspect_GT_Rectangular; 

  myUseMFT = Standard_True;
#ifdef PRO10988
  if( myRGrid.IsNull() ) myRGrid = new V2d_RectangularGrid(this,i1,i2);
  else myRGrid -> SetColorIndices (i1, i2);
  if( myCGrid.IsNull() ) myCGrid = new V2d_CircularGrid(this,i1,i2);
  else myCGrid -> SetColorIndices (i1, i2);
#else
  myRGrid = new V2d_RectangularGrid(this,i1,i2);
  myCGrid = new V2d_CircularGrid(this,i1,i2);
#endif
}


/*=================================================================*/
void V2d_Viewer::AddView(const Handle(V2d_View)& aView) {
  myViews.Append(aView);
  IncrCount();
}

/*=================================================================*/
void V2d_Viewer::RemoveView(const Handle(V2d_View)& aView) {

  myViewsIterator.Initialize(myViews);
  while(myViewsIterator.More())
    if(aView == myViewsIterator.Value()) {
      myViews.Remove(myViewsIterator);}
  else
    myViewsIterator.Next();
}

/*=================================================================*/
void V2d_Viewer::SetColorMap(const Handle(Aspect_ColorMap)& aColorMap) {
  myColorMap = aColorMap;
#ifdef PRO10988
  // Need to call to redefine myColorIndex1 and myColorIndex2,
  // which may not be available in new colormap.
#ifdef IMP080300
  if( myColorMap != aColorMap ) Init ();
#else
  Init();
#endif
  // We need to pass myColorMap in the views because
  // it could be changed in Init() method by InitializeColor().
  for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
    ActiveView()->Driver()->SetColorMap(myColorMap);
  }
#else
  for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
    ActiveView()->Driver()->SetColorMap(aColorMap);
  }
#endif
}

/*=================================================================*/
Handle(Aspect_ColorMap) V2d_Viewer::ColorMap() const {
  return myColorMap;
}

/*=================================================================*/
void V2d_Viewer::SetTypeMap(const Handle(Aspect_TypeMap)& aTypeMap) {
  myTypeMap = aTypeMap;
  for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
    ActiveView()->Driver()->SetTypeMap(aTypeMap);
  }
}

/*=================================================================*/
Handle(Aspect_TypeMap) V2d_Viewer::TypeMap() const {
  return myTypeMap;
}

/*=================================================================*/
void V2d_Viewer::SetWidthMap(const Handle(Aspect_WidthMap)& aWidthMap) {
  myWidthMap = aWidthMap;
  for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
    ActiveView()->Driver()->SetWidthMap(aWidthMap);
  }
}

/*=================================================================*/
Handle(Aspect_WidthMap) V2d_Viewer::WidthMap() const {
  return myWidthMap;
}

/*=================================================================*/
void V2d_Viewer::SetFontMap(const Handle(Aspect_FontMap)& aFontMap,
			    const Standard_Boolean useMFT) {
  myFontMap = aFontMap;
  for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
    ActiveView()->Driver()->SetFontMap(aFontMap,useMFT);
  }
}

/*=================================================================*/
void V2d_Viewer::SetMarkMap(const Handle(Aspect_MarkMap)& aMarkMap) {
  myMarkMap = aMarkMap;
  for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
    ActiveView()->Driver()->SetMarkMap(aMarkMap);
  }
}

/*=================================================================*/
Handle(Aspect_FontMap) V2d_Viewer::FontMap() const {
  return myFontMap;
}

/*=================================================================*/
Handle(Aspect_MarkMap) V2d_Viewer::MarkMap() const {
  return myMarkMap;
}

/*=================================================================*/
Handle(Graphic2d_View) V2d_Viewer::View () const {
  return myGraphicView;
}

/*=================================================================*/
Standard_Boolean V2d_Viewer::UseMFT() const {
  return myUseMFT;
}

/*=================================================================*/
void V2d_Viewer::Update() {
  for (InitActiveViews();MoreActiveViews();NextActiveViews()){
    ActiveView()->Update();
  }
}

/*=================================================================*/
void V2d_Viewer::UpdateNew() {
  for (InitActiveViews();MoreActiveViews();NextActiveViews()){
    ActiveView()->UpdateNew();
  }
}

/*=================================================================*/
Standard_Integer V2d_Viewer::InitializeColor (const Quantity_NameOfColor aColor) {
  Standard_Integer Size = myColorMap->Size();
  Quantity_Color color(aColor);
#ifdef IMP080300
  Standard_Integer index = myColorMap->AddEntry(color);
  if( Size != myColorMap->Size() ) {
    for (InitActiveViews();MoreActiveViews();NextActiveViews()){
      ActiveView()->Driver()->SetColorMap(myColorMap);
    }
  }
#else	// ??? why this gazworks ???
  Standard_Integer i,index=0;
  for ( i=1; i<= Size; i++) {
   if(myColorMap->Entry(i).Color().IsEqual(color)) 
	index=myColorMap->Entry(i).Index();
  }
  if (index == 0) {
    Handle(Aspect_GenericColorMap) map = new Aspect_GenericColorMap;
    for (i=1; i<= Size; i++) {
      map->AddEntry(myColorMap->Entry(i));
    }
// finding a free index.
    TColStd_MapOfInteger M;
    for ( i=1; i<= Size; i++) {
      M.Add(myColorMap->Entry(i).Index());
    }
#ifndef PRO10988
    index = Size + 1;
#endif
    i=1;
    while (index == 0) {
      if(!M.Contains(i)) index =i;
      i++;
    }
    map->AddEntry(Aspect_ColorMapEntry(index,color));
    for (InitActiveViews();MoreActiveViews();NextActiveViews()){
      ActiveView()->Driver()->SetColorMap(map);
    }
    myColorMap = map;
  }
#endif
  return index;
}

/*=================================================================*/
void V2d_Viewer::InitActiveViews() {
myViewsIterator.Initialize(myViews);
}

/*=================================================================*/
Standard_Boolean V2d_Viewer::MoreActiveViews () const {
  return myViewsIterator.More();
}

/*=================================================================*/
void V2d_Viewer::NextActiveViews () {
  myViewsIterator.Next();
}

/*=================================================================*/
Handle(V2d_View) V2d_Viewer::ActiveView() const {
  return (Handle(V2d_View)&)(myViewsIterator.Value());
}

/*=================================================================*/
Handle(Aspect_Grid) V2d_Viewer::Grid () const {
Handle(Aspect_Grid) grid;

  switch (myGridType) {
  case Aspect_GT_Circular:
    grid = myCGrid;
    break;
  case Aspect_GT_Rectangular:
  default:
    grid = myRGrid;
  }
  return grid;
}

/*=================================================================*/
void V2d_Viewer::ActivateGrid(const Aspect_GridType aType,
			      const Aspect_GridDrawMode aMode){
  
  Grid()->Erase();
  myGridType = aType;
  Grid()->SetDrawMode(aMode);
  Grid()->Display();
  Grid()->Activate();
  Update();
}

/*=================================================================*/
void V2d_Viewer::DeactivateGrid() {
  Grid()->Erase();
  Grid()->Deactivate();
  Update();
}

/*=================================================================*/
Standard_Boolean V2d_Viewer::IsActive () const {
  return Grid()->IsActive();
}

/*=================================================================*/
void V2d_Viewer::RectangularGridValues 
(Quantity_Length& theXOrigin,
 Quantity_Length& theYOrigin,
 Quantity_Length& theXStep,
 Quantity_Length& theYStep,
 Quantity_PlaneAngle& theRotationAngle) const {
   
   theXOrigin        = myRGrid->XOrigin();
   theYOrigin        = myRGrid->YOrigin();
   theXStep          = myRGrid->XStep();
   theYStep          = myRGrid->YStep();
   theRotationAngle  = myRGrid->RotationAngle();
}

/*=================================================================*/
void V2d_Viewer::SetRectangularGridValues
(const Quantity_Length theXOrigin,
 const Quantity_Length theYOrigin,
 const Quantity_Length theXStep,
 const Quantity_Length theYStep,
 const Quantity_PlaneAngle theRotationAngle) {
   
   myRGrid->SetXOrigin(theXOrigin);
   myRGrid->SetYOrigin(theYOrigin);
   myRGrid->SetXStep(theXStep);
   myRGrid->SetYStep(theYStep);
   myRGrid->SetRotationAngle(theRotationAngle);
   Update();
}

/*=================================================================*/
void V2d_Viewer::CircularGridValues 
(Quantity_Length& theXOrigin,
 Quantity_Length& theYOrigin,
 Quantity_Length& theRadiusStep,
 Standard_Integer& theDivisionNumber,
 Quantity_PlaneAngle& theRotationAngle) const {
   
   theXOrigin        = myCGrid->XOrigin();
   theYOrigin        = myCGrid->YOrigin();
   theRadiusStep    = myCGrid->RadiusStep();
   theDivisionNumber = myCGrid->DivisionNumber();
   theRotationAngle  = myCGrid->RotationAngle();
}

/*=================================================================*/
void V2d_Viewer::SetCircularGridValues
(const Quantity_Length theXOrigin,
 const Quantity_Length theYOrigin,
 const Quantity_Length theRadiusStep,
 const Standard_Integer theDivisionNumber,
 const Quantity_PlaneAngle theRotationAngle) {
   
   myCGrid->SetXOrigin(theXOrigin);
   myCGrid->SetYOrigin(theYOrigin);
   myCGrid->SetRadiusStep(theRadiusStep);
   myCGrid->SetDivisionNumber(theDivisionNumber);
   myCGrid->SetRotationAngle(theRotationAngle);
   Update();
}

/*=================================================================*/
Standard_Integer V2d_Viewer::HitPointColor() const {
  return myHitPointColorIndex;
}

/*=================================================================*/
Standard_Integer V2d_Viewer::CoordinatesColor() const {
  return myCoordinatesColorIndex;
}

/*=================================================================*/
Standard_Integer V2d_Viewer::HitPointMarkerIndex() const {
  return myHitPointMarkerIndex;
}

/*=================================================================*/
void V2d_Viewer::Hit(const Quantity_Length rx,
		   const Quantity_Length ry,
		   Quantity_Length& gx,
		   Quantity_Length& gy) const {

  Grid()->Hit(rx,ry,gx,gy);
}

/*=================================================================*/
Aspect_GridType V2d_Viewer::GridType() const {
  return myGridType;
}

/*=================================================================*/
Standard_Boolean V2d_Viewer::IsEmpty() const {
  
  return myViews.IsEmpty();
}

//SAV
void V2d_Viewer::SetGridColor( const Quantity_Color& color1,
			       const Quantity_Color& color2 )
{
  Standard_Integer i1 = InitializeColor( color1.Name() );
  Standard_Integer i2 = InitializeColor( color2.Name() );

  Standard_Boolean restart = IsActive();
  if ( restart ) {
    DeactivateGrid();
    Grid()->Erase();
  }

  Quantity_Length xOrigin, yOrigin, xStep, yStep;
  Quantity_PlaneAngle angle;
  Standard_Integer theDivisionNumber;
  Aspect_GridDrawMode gMode = myRGrid->DrawMode();

  RectangularGridValues( xOrigin, yOrigin, xStep, yStep, angle );
  myRGrid.Nullify();
  if ( myRGrid.IsNull() ) {
    myRGrid = new V2d_RectangularGrid( this, i1, i2 );
    myRGrid->SetDrawMode( gMode );
    SetRectangularGridValues( xOrigin, yOrigin, xStep, yStep, angle );
  }

  gMode = myCGrid->DrawMode();
  CircularGridValues( xOrigin, yOrigin, xStep, theDivisionNumber, angle );
  myCGrid.Nullify();
  if ( myCGrid.IsNull() ) {
    myCGrid = new V2d_CircularGrid( this, i1, i2 );
    myCGrid->SetDrawMode( gMode );
    SetCircularGridValues( xOrigin, yOrigin, xStep, theDivisionNumber, angle );
  }
  if ( restart ) {
    Grid()->Display();
    Grid()->Activate();
    Update();
  }    
}