Salome HOME
2818884c390688c02d33a285450d2a4f21c6545c
[modules/gui.git] / src / OCCViewer / OCCViewer_Utilities.cxx
1 // Copyright (C) 2014-2015  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 OCCViewer_ViewWindow::Mode2dType OCCViewer_Utilities::setViewer2DMode
78                                          ( OCCViewer_Viewer* theViewer,
79                                            const OCCViewer_ViewWindow::Mode2dType& theMode )
80 {
81   OCCViewer_ViewWindow::Mode2dType anOldMode = OCCViewer_ViewWindow::No2dMode;
82   OCCViewer_ViewFrame* aFrame = dynamic_cast<OCCViewer_ViewFrame*>
83                                      ( theViewer->getViewManager()->getActiveView() );
84   OCCViewer_ViewWindow* aView = aFrame ? aFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW ) : 0;
85   if ( !aView )
86     return anOldMode;
87
88   // set a view mode
89   anOldMode = aView->get2dMode();
90   aView->set2dMode( theMode );
91   bool is2dMode = theMode != OCCViewer_ViewWindow::No2dMode;
92
93   // enable/disable view actions
94   QList<int> aNo2dActions;
95   aNo2dActions << OCCViewer_ViewWindow::ChangeRotationPointId
96                << OCCViewer_ViewWindow::RotationId
97                << OCCViewer_ViewWindow::FrontId
98                << OCCViewer_ViewWindow::BackId
99                //<< OCCViewer_ViewWindow::TopId
100                << OCCViewer_ViewWindow::BottomId
101                << OCCViewer_ViewWindow::LeftId
102                << OCCViewer_ViewWindow::RightId
103                << OCCViewer_ViewWindow::AntiClockWiseId
104                << OCCViewer_ViewWindow::ClockWiseId
105                << OCCViewer_ViewWindow::OrthographicId
106                << OCCViewer_ViewWindow::PerspectiveId
107                << OCCViewer_ViewWindow::ResetId;
108
109   QtxActionToolMgr* aToolMgr = aView->toolMgr();
110   QAction* anAction;
111   for ( int i = 0, aNb = aNo2dActions.size(); i < aNb; i++ ) {
112     anAction = aToolMgr->action( aNo2dActions[i] );
113     if ( anAction )
114       anAction->setEnabled( !is2dMode );
115   }
116   QAction* aTop = aToolMgr->action( OCCViewer_ViewWindow::TopId );
117   QtxMultiAction* aMulti = dynamic_cast<QtxMultiAction*>( aTop->parent() );
118   aMulti->setActiveAction( aTop );
119
120   // change view position
121   Handle(V3d_View) aView3d = aView->getViewPort()->getView();
122   if ( !aView3d.IsNull() ) {
123     switch ( theMode ) {
124       case OCCViewer_ViewWindow::XYPlane:
125         aView3d->SetProj (V3d_Zpos);
126         break;
127       case OCCViewer_ViewWindow::XZPlane:
128         aView3d->SetProj (V3d_Yneg);
129         break;
130       case OCCViewer_ViewWindow::YZPlane:
131         aView3d->SetProj (V3d_Xpos);
132         break;
133     }
134   }
135
136   return anOldMode;
137 }