Salome HOME
Merge branch 'master' into abn/paravis_rearch
[modules/gui.git] / src / OCCViewer / OCCViewer_Utilities.cxx
1 // Copyright (C) 2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "OCCViewer_Utilities.h"
22 #include "OCCViewer_ViewFrame.h"
23 #include "OCCViewer_ViewModel.h"
24 #include "OCCViewer_ViewPort3d.h"
25
26 #include "SUIT_ViewManager.h"
27 #include "QtxActionToolMgr.h"
28 #include "QtxMultiAction.h"
29
30 // KERNEL includes
31 #include <Basics_OCCTVersion.hxx>
32
33 // OCC includes
34 #include <V3d_View.hxx>
35
36 // QT includes
37 #include <QImage>
38 #include <QAction>
39
40 Handle(Image_PixMap) OCCViewer_Utilities::imageToPixmap( const QImage& anImage )
41 {
42   Handle(Image_PixMap) aPixmap = new Image_PixMap();
43   if ( !anImage.isNull() ) {
44     aPixmap->InitTrash( Image_PixMap::ImgBGRA, anImage.width(), anImage.height() );
45     aPixmap->SetTopDown( Standard_True );
46
47     const uchar* aImageBytes = anImage.bits();
48       
49     for ( int aLine = anImage.height() - 1; aLine >= 0; --aLine ) {
50 #if OCC_VERSION_LARGE > 0x06070100
51       // convert pixels from ARGB to renderer-compatible RGBA
52       for ( int aByte = 0; aByte < anImage.width(); ++aByte ) {
53             Image_ColorBGRA& aPixmapBytes = aPixmap->ChangeValue<Image_ColorBGRA>(aLine, aByte);
54
55             aPixmapBytes.b() = (Standard_Byte) *aImageBytes++;
56             aPixmapBytes.g() = (Standard_Byte) *aImageBytes++;
57             aPixmapBytes.r() = (Standard_Byte) *aImageBytes++;
58             aPixmapBytes.a() = (Standard_Byte) *aImageBytes++;
59           }
60 #else
61           Image_ColorBGRA* aPixmapBytes = aPixmap->EditData<Image_ColorBGRA>().ChangeRow(aLine);
62         
63       // convert pixels from ARGB to renderer-compatible RGBA
64       for ( int aByte = 0; aByte < anImage.width(); ++aByte ) {
65             aPixmapBytes->b() = (Standard_Byte) *aImageBytes++;
66             aPixmapBytes->g() = (Standard_Byte) *aImageBytes++;
67             aPixmapBytes->r() = (Standard_Byte) *aImageBytes++;
68             aPixmapBytes->a() = (Standard_Byte) *aImageBytes++;
69             aPixmapBytes++;
70       }
71 #endif
72     }
73   }
74   return aPixmap;
75 }
76
77 void OCCViewer_Utilities::setViewer2DMode( OCCViewer_Viewer* theViewer,
78                                            const OCCViewer_ViewWindow::Mode2dType& theMode )
79 {
80   OCCViewer_ViewFrame* aFrame = dynamic_cast<OCCViewer_ViewFrame*>
81                                      ( theViewer->getViewManager()->getActiveView() );
82   OCCViewer_ViewWindow* aView = aFrame ? aFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW ) : 0;
83   if ( !aView )
84     return;
85
86   // set a view mode
87   aView->set2dMode( theMode );
88   bool is2dMode = theMode != OCCViewer_ViewWindow::No2dMode;
89
90   // enable/disable view actions
91   QList<int> aNo2dActions;
92   aNo2dActions << OCCViewer_ViewWindow::ChangeRotationPointId
93                << OCCViewer_ViewWindow::RotationId
94                << OCCViewer_ViewWindow::FrontId
95                << OCCViewer_ViewWindow::BackId
96                //<< OCCViewer_ViewWindow::TopId
97                << OCCViewer_ViewWindow::BottomId
98                << OCCViewer_ViewWindow::LeftId
99                << OCCViewer_ViewWindow::RightId
100                << OCCViewer_ViewWindow::AntiClockWiseId
101                << OCCViewer_ViewWindow::ClockWiseId
102                << OCCViewer_ViewWindow::ResetId;
103
104   QtxActionToolMgr* aToolMgr = aView->toolMgr();
105   QAction* anAction;
106   for ( int i = 0, aNb = aNo2dActions.size(); i < aNb; i++ ) {
107     anAction = aToolMgr->action( aNo2dActions[i] );
108     if ( anAction )
109       anAction->setEnabled( !is2dMode );
110   }
111   QAction* aTop = aToolMgr->action( OCCViewer_ViewWindow::TopId );
112   QtxMultiAction* aMulti = dynamic_cast<QtxMultiAction*>( aTop->parent() );
113   aMulti->setActiveAction( aTop );
114
115   // change view position
116   Handle(V3d_View) aView3d = aView->getViewPort()->getView();
117   if ( !aView3d.IsNull() ) {
118     switch ( theMode ) {
119       case OCCViewer_ViewWindow::XYPlane:
120         aView3d->SetProj (V3d_Zpos);
121         break;
122       case OCCViewer_ViewWindow::XZPlane:
123         aView3d->SetProj (V3d_Yneg);
124         break;
125       case OCCViewer_ViewWindow::YZPlane:
126         aView3d->SetProj (V3d_Xpos);
127         break;
128     }
129   }
130 }