PageRenderTime 113ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/V2d/V2d_Viewer.cxx

https://github.com/hmeyer/oce
C++ | 445 lines | 338 code | 57 blank | 50 comment | 34 complexity | ceba5ecba3201f2890519e5e79713ff1 MD5 | raw file
  1. //Updates:
  2. // GG 24/03/98 Add useMFT parameter to SetFontMap method.
  3. // GG 07/07/98 BUC60258 Add SetMarkMap() method
  4. #define PRO10988 //DCB Resets grid color indices after colormap change
  5. #define IMP080300 //GG
  6. // -> Optimize the SetColorMap() and InitializeColor() methods
  7. // Review the 14/09/01 to avoid regression
  8. // See new deferred method in Aspect_ColorMap
  9. #include <V2d_Viewer.ixx>
  10. #include <V2d_View.hxx>
  11. #include <V2d_DefaultMap.hxx>
  12. #include <Graphic2d_View.hxx>
  13. #include <Aspect_GenericColorMap.hxx>
  14. #include <Aspect_WindowDriver.hxx>
  15. #include <TColStd_MapOfInteger.hxx>
  16. #include <Aspect_ColorMapEntry.hxx>
  17. /*=================================================================*/
  18. V2d_Viewer::V2d_Viewer(const Handle(Aspect_GraphicDevice)& aGraphicDevice,
  19. const Standard_ExtString aName,
  20. const Standard_CString aDomain)
  21. :Viewer_Viewer(aGraphicDevice,aName,aDomain,1),
  22. myColorMap(V2d_DefaultMap::ColorMap()),
  23. myTypeMap(V2d_DefaultMap::TypeMap()),
  24. myWidthMap(V2d_DefaultMap::WidthMap()),
  25. myFontMap(V2d_DefaultMap::FontMap()),
  26. myMarkMap(V2d_DefaultMap::MarkMap()),
  27. myGraphicView(new Graphic2d_View()),
  28. myViews(),
  29. myViewsIterator(),
  30. myRGrid(),
  31. myCGrid()
  32. {
  33. Init();
  34. }
  35. /*=================================================================*/
  36. V2d_Viewer::V2d_Viewer(const Handle(Aspect_GraphicDevice)& aGraphicDevice,
  37. const Handle(Graphic2d_View)& aView,
  38. const Standard_ExtString aName,
  39. const Standard_CString aDomain)
  40. :Viewer_Viewer(aGraphicDevice,aName,aDomain,1),
  41. myColorMap(V2d_DefaultMap::ColorMap()),
  42. myTypeMap(V2d_DefaultMap::TypeMap()),
  43. myWidthMap(V2d_DefaultMap::WidthMap()),
  44. myFontMap(V2d_DefaultMap::FontMap()),
  45. myMarkMap(V2d_DefaultMap::MarkMap()),
  46. myGraphicView(aView),
  47. myViews(),
  48. myViewsIterator(),
  49. myRGrid(),
  50. myCGrid()
  51. {
  52. Init();
  53. }
  54. /*=================================================================*/
  55. void V2d_Viewer::Init() {
  56. myHitPointMarkerIndex = 1;
  57. myHitPointColorIndex = 0;
  58. Standard_Integer i1 = InitializeColor(Quantity_NOC_GRAY50);
  59. Standard_Integer i2 = InitializeColor(Quantity_NOC_GRAY70);
  60. myHitPointColorIndex = InitializeColor(Quantity_NOC_WHITE);
  61. myCoordinatesColorIndex = myHitPointColorIndex ;
  62. myGridType = Aspect_GT_Rectangular;
  63. myUseMFT = Standard_True;
  64. #ifdef PRO10988
  65. if( myRGrid.IsNull() ) myRGrid = new V2d_RectangularGrid(this,i1,i2);
  66. else myRGrid -> SetColorIndices (i1, i2);
  67. if( myCGrid.IsNull() ) myCGrid = new V2d_CircularGrid(this,i1,i2);
  68. else myCGrid -> SetColorIndices (i1, i2);
  69. #else
  70. myRGrid = new V2d_RectangularGrid(this,i1,i2);
  71. myCGrid = new V2d_CircularGrid(this,i1,i2);
  72. #endif
  73. }
  74. /*=================================================================*/
  75. void V2d_Viewer::AddView(const Handle(V2d_View)& aView) {
  76. myViews.Append(aView);
  77. IncrCount();
  78. }
  79. /*=================================================================*/
  80. void V2d_Viewer::RemoveView(const Handle(V2d_View)& aView) {
  81. myViewsIterator.Initialize(myViews);
  82. while(myViewsIterator.More())
  83. if(aView == myViewsIterator.Value()) {
  84. myViews.Remove(myViewsIterator);}
  85. else
  86. myViewsIterator.Next();
  87. }
  88. /*=================================================================*/
  89. void V2d_Viewer::SetColorMap(const Handle(Aspect_ColorMap)& aColorMap) {
  90. myColorMap = aColorMap;
  91. #ifdef PRO10988
  92. // Need to call to redefine myColorIndex1 and myColorIndex2,
  93. // which may not be available in new colormap.
  94. #ifdef IMP080300
  95. if( myColorMap != aColorMap ) Init ();
  96. #else
  97. Init();
  98. #endif
  99. // We need to pass myColorMap in the views because
  100. // it could be changed in Init() method by InitializeColor().
  101. for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
  102. ActiveView()->Driver()->SetColorMap(myColorMap);
  103. }
  104. #else
  105. for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
  106. ActiveView()->Driver()->SetColorMap(aColorMap);
  107. }
  108. #endif
  109. }
  110. /*=================================================================*/
  111. Handle(Aspect_ColorMap) V2d_Viewer::ColorMap() const {
  112. return myColorMap;
  113. }
  114. /*=================================================================*/
  115. void V2d_Viewer::SetTypeMap(const Handle(Aspect_TypeMap)& aTypeMap) {
  116. myTypeMap = aTypeMap;
  117. for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
  118. ActiveView()->Driver()->SetTypeMap(aTypeMap);
  119. }
  120. }
  121. /*=================================================================*/
  122. Handle(Aspect_TypeMap) V2d_Viewer::TypeMap() const {
  123. return myTypeMap;
  124. }
  125. /*=================================================================*/
  126. void V2d_Viewer::SetWidthMap(const Handle(Aspect_WidthMap)& aWidthMap) {
  127. myWidthMap = aWidthMap;
  128. for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
  129. ActiveView()->Driver()->SetWidthMap(aWidthMap);
  130. }
  131. }
  132. /*=================================================================*/
  133. Handle(Aspect_WidthMap) V2d_Viewer::WidthMap() const {
  134. return myWidthMap;
  135. }
  136. /*=================================================================*/
  137. void V2d_Viewer::SetFontMap(const Handle(Aspect_FontMap)& aFontMap,
  138. const Standard_Boolean useMFT) {
  139. myFontMap = aFontMap;
  140. for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
  141. ActiveView()->Driver()->SetFontMap(aFontMap,useMFT);
  142. }
  143. }
  144. /*=================================================================*/
  145. void V2d_Viewer::SetMarkMap(const Handle(Aspect_MarkMap)& aMarkMap) {
  146. myMarkMap = aMarkMap;
  147. for (InitActiveViews();MoreActiveViews();NextActiveViews()) {
  148. ActiveView()->Driver()->SetMarkMap(aMarkMap);
  149. }
  150. }
  151. /*=================================================================*/
  152. Handle(Aspect_FontMap) V2d_Viewer::FontMap() const {
  153. return myFontMap;
  154. }
  155. /*=================================================================*/
  156. Handle(Aspect_MarkMap) V2d_Viewer::MarkMap() const {
  157. return myMarkMap;
  158. }
  159. /*=================================================================*/
  160. Handle(Graphic2d_View) V2d_Viewer::View () const {
  161. return myGraphicView;
  162. }
  163. /*=================================================================*/
  164. Standard_Boolean V2d_Viewer::UseMFT() const {
  165. return myUseMFT;
  166. }
  167. /*=================================================================*/
  168. void V2d_Viewer::Update() {
  169. for (InitActiveViews();MoreActiveViews();NextActiveViews()){
  170. ActiveView()->Update();
  171. }
  172. }
  173. /*=================================================================*/
  174. void V2d_Viewer::UpdateNew() {
  175. for (InitActiveViews();MoreActiveViews();NextActiveViews()){
  176. ActiveView()->UpdateNew();
  177. }
  178. }
  179. /*=================================================================*/
  180. Standard_Integer V2d_Viewer::InitializeColor (const Quantity_NameOfColor aColor) {
  181. Standard_Integer Size = myColorMap->Size();
  182. Quantity_Color color(aColor);
  183. #ifdef IMP080300
  184. Standard_Integer index = myColorMap->AddEntry(color);
  185. if( Size != myColorMap->Size() ) {
  186. for (InitActiveViews();MoreActiveViews();NextActiveViews()){
  187. ActiveView()->Driver()->SetColorMap(myColorMap);
  188. }
  189. }
  190. #else // ??? why this gazworks ???
  191. Standard_Integer i,index=0;
  192. for ( i=1; i<= Size; i++) {
  193. if(myColorMap->Entry(i).Color().IsEqual(color))
  194. index=myColorMap->Entry(i).Index();
  195. }
  196. if (index == 0) {
  197. Handle(Aspect_GenericColorMap) map = new Aspect_GenericColorMap;
  198. for (i=1; i<= Size; i++) {
  199. map->AddEntry(myColorMap->Entry(i));
  200. }
  201. // finding a free index.
  202. TColStd_MapOfInteger M;
  203. for ( i=1; i<= Size; i++) {
  204. M.Add(myColorMap->Entry(i).Index());
  205. }
  206. #ifndef PRO10988
  207. index = Size + 1;
  208. #endif
  209. i=1;
  210. while (index == 0) {
  211. if(!M.Contains(i)) index =i;
  212. i++;
  213. }
  214. map->AddEntry(Aspect_ColorMapEntry(index,color));
  215. for (InitActiveViews();MoreActiveViews();NextActiveViews()){
  216. ActiveView()->Driver()->SetColorMap(map);
  217. }
  218. myColorMap = map;
  219. }
  220. #endif
  221. return index;
  222. }
  223. /*=================================================================*/
  224. void V2d_Viewer::InitActiveViews() {
  225. myViewsIterator.Initialize(myViews);
  226. }
  227. /*=================================================================*/
  228. Standard_Boolean V2d_Viewer::MoreActiveViews () const {
  229. return myViewsIterator.More();
  230. }
  231. /*=================================================================*/
  232. void V2d_Viewer::NextActiveViews () {
  233. myViewsIterator.Next();
  234. }
  235. /*=================================================================*/
  236. Handle(V2d_View) V2d_Viewer::ActiveView() const {
  237. return (Handle(V2d_View)&)(myViewsIterator.Value());
  238. }
  239. /*=================================================================*/
  240. Handle(Aspect_Grid) V2d_Viewer::Grid () const {
  241. Handle(Aspect_Grid) grid;
  242. switch (myGridType) {
  243. case Aspect_GT_Circular:
  244. grid = myCGrid;
  245. break;
  246. case Aspect_GT_Rectangular:
  247. default:
  248. grid = myRGrid;
  249. }
  250. return grid;
  251. }
  252. /*=================================================================*/
  253. void V2d_Viewer::ActivateGrid(const Aspect_GridType aType,
  254. const Aspect_GridDrawMode aMode){
  255. Grid()->Erase();
  256. myGridType = aType;
  257. Grid()->SetDrawMode(aMode);
  258. Grid()->Display();
  259. Grid()->Activate();
  260. Update();
  261. }
  262. /*=================================================================*/
  263. void V2d_Viewer::DeactivateGrid() {
  264. Grid()->Erase();
  265. Grid()->Deactivate();
  266. Update();
  267. }
  268. /*=================================================================*/
  269. Standard_Boolean V2d_Viewer::IsActive () const {
  270. return Grid()->IsActive();
  271. }
  272. /*=================================================================*/
  273. void V2d_Viewer::RectangularGridValues
  274. (Quantity_Length& theXOrigin,
  275. Quantity_Length& theYOrigin,
  276. Quantity_Length& theXStep,
  277. Quantity_Length& theYStep,
  278. Quantity_PlaneAngle& theRotationAngle) const {
  279. theXOrigin = myRGrid->XOrigin();
  280. theYOrigin = myRGrid->YOrigin();
  281. theXStep = myRGrid->XStep();
  282. theYStep = myRGrid->YStep();
  283. theRotationAngle = myRGrid->RotationAngle();
  284. }
  285. /*=================================================================*/
  286. void V2d_Viewer::SetRectangularGridValues
  287. (const Quantity_Length theXOrigin,
  288. const Quantity_Length theYOrigin,
  289. const Quantity_Length theXStep,
  290. const Quantity_Length theYStep,
  291. const Quantity_PlaneAngle theRotationAngle) {
  292. myRGrid->SetXOrigin(theXOrigin);
  293. myRGrid->SetYOrigin(theYOrigin);
  294. myRGrid->SetXStep(theXStep);
  295. myRGrid->SetYStep(theYStep);
  296. myRGrid->SetRotationAngle(theRotationAngle);
  297. Update();
  298. }
  299. /*=================================================================*/
  300. void V2d_Viewer::CircularGridValues
  301. (Quantity_Length& theXOrigin,
  302. Quantity_Length& theYOrigin,
  303. Quantity_Length& theRadiusStep,
  304. Standard_Integer& theDivisionNumber,
  305. Quantity_PlaneAngle& theRotationAngle) const {
  306. theXOrigin = myCGrid->XOrigin();
  307. theYOrigin = myCGrid->YOrigin();
  308. theRadiusStep = myCGrid->RadiusStep();
  309. theDivisionNumber = myCGrid->DivisionNumber();
  310. theRotationAngle = myCGrid->RotationAngle();
  311. }
  312. /*=================================================================*/
  313. void V2d_Viewer::SetCircularGridValues
  314. (const Quantity_Length theXOrigin,
  315. const Quantity_Length theYOrigin,
  316. const Quantity_Length theRadiusStep,
  317. const Standard_Integer theDivisionNumber,
  318. const Quantity_PlaneAngle theRotationAngle) {
  319. myCGrid->SetXOrigin(theXOrigin);
  320. myCGrid->SetYOrigin(theYOrigin);
  321. myCGrid->SetRadiusStep(theRadiusStep);
  322. myCGrid->SetDivisionNumber(theDivisionNumber);
  323. myCGrid->SetRotationAngle(theRotationAngle);
  324. Update();
  325. }
  326. /*=================================================================*/
  327. Standard_Integer V2d_Viewer::HitPointColor() const {
  328. return myHitPointColorIndex;
  329. }
  330. /*=================================================================*/
  331. Standard_Integer V2d_Viewer::CoordinatesColor() const {
  332. return myCoordinatesColorIndex;
  333. }
  334. /*=================================================================*/
  335. Standard_Integer V2d_Viewer::HitPointMarkerIndex() const {
  336. return myHitPointMarkerIndex;
  337. }
  338. /*=================================================================*/
  339. void V2d_Viewer::Hit(const Quantity_Length rx,
  340. const Quantity_Length ry,
  341. Quantity_Length& gx,
  342. Quantity_Length& gy) const {
  343. Grid()->Hit(rx,ry,gx,gy);
  344. }
  345. /*=================================================================*/
  346. Aspect_GridType V2d_Viewer::GridType() const {
  347. return myGridType;
  348. }
  349. /*=================================================================*/
  350. Standard_Boolean V2d_Viewer::IsEmpty() const {
  351. return myViews.IsEmpty();
  352. }
  353. //SAV
  354. void V2d_Viewer::SetGridColor( const Quantity_Color& color1,
  355. const Quantity_Color& color2 )
  356. {
  357. Standard_Integer i1 = InitializeColor( color1.Name() );
  358. Standard_Integer i2 = InitializeColor( color2.Name() );
  359. Standard_Boolean restart = IsActive();
  360. if ( restart ) {
  361. DeactivateGrid();
  362. Grid()->Erase();
  363. }
  364. Quantity_Length xOrigin, yOrigin, xStep, yStep;
  365. Quantity_PlaneAngle angle;
  366. Standard_Integer theDivisionNumber;
  367. Aspect_GridDrawMode gMode = myRGrid->DrawMode();
  368. RectangularGridValues( xOrigin, yOrigin, xStep, yStep, angle );
  369. myRGrid.Nullify();
  370. if ( myRGrid.IsNull() ) {
  371. myRGrid = new V2d_RectangularGrid( this, i1, i2 );
  372. myRGrid->SetDrawMode( gMode );
  373. SetRectangularGridValues( xOrigin, yOrigin, xStep, yStep, angle );
  374. }
  375. gMode = myCGrid->DrawMode();
  376. CircularGridValues( xOrigin, yOrigin, xStep, theDivisionNumber, angle );
  377. myCGrid.Nullify();
  378. if ( myCGrid.IsNull() ) {
  379. myCGrid = new V2d_CircularGrid( this, i1, i2 );
  380. myCGrid->SetDrawMode( gMode );
  381. SetCircularGridValues( xOrigin, yOrigin, xStep, theDivisionNumber, angle );
  382. }
  383. if ( restart ) {
  384. Grid()->Display();
  385. Grid()->Activate();
  386. Update();
  387. }
  388. }