Salome HOME
Merge remote branch 'origin/hydro/imps_2015'
[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 #include <QDialog>
40
41 Handle(Image_PixMap) OCCViewer_Utilities::imageToPixmap( const QImage& anImage )
42 {
43   Handle(Image_PixMap) aPixmap = new Image_PixMap();
44   if ( !anImage.isNull() ) {
45     aPixmap->InitTrash( Image_PixMap::ImgBGRA, anImage.width(), anImage.height() );
46     aPixmap->SetTopDown( Standard_True );
47
48     const uchar* aImageBytes = anImage.bits();
49       
50     for ( int aLine = anImage.height() - 1; aLine >= 0; --aLine ) {
51 #if OCC_VERSION_LARGE > 0x06070100
52       // convert pixels from ARGB to renderer-compatible RGBA
53       for ( int aByte = 0; aByte < anImage.width(); ++aByte ) {
54             Image_ColorBGRA& aPixmapBytes = aPixmap->ChangeValue<Image_ColorBGRA>(aLine, aByte);
55
56             aPixmapBytes.b() = (Standard_Byte) *aImageBytes++;
57             aPixmapBytes.g() = (Standard_Byte) *aImageBytes++;
58             aPixmapBytes.r() = (Standard_Byte) *aImageBytes++;
59             aPixmapBytes.a() = (Standard_Byte) *aImageBytes++;
60           }
61 #else
62           Image_ColorBGRA* aPixmapBytes = aPixmap->EditData<Image_ColorBGRA>().ChangeRow(aLine);
63         
64       // convert pixels from ARGB to renderer-compatible RGBA
65       for ( int aByte = 0; aByte < anImage.width(); ++aByte ) {
66             aPixmapBytes->b() = (Standard_Byte) *aImageBytes++;
67             aPixmapBytes->g() = (Standard_Byte) *aImageBytes++;
68             aPixmapBytes->r() = (Standard_Byte) *aImageBytes++;
69             aPixmapBytes->a() = (Standard_Byte) *aImageBytes++;
70             aPixmapBytes++;
71       }
72 #endif
73     }
74   }
75   return aPixmap;
76 }
77
78 OCCViewer_ViewWindow::Mode2dType OCCViewer_Utilities::setViewer2DMode
79                                          ( OCCViewer_Viewer* theViewer,
80                                            const OCCViewer_ViewWindow::Mode2dType& theMode )
81 {
82   OCCViewer_ViewWindow::Mode2dType anOldMode = OCCViewer_ViewWindow::No2dMode;
83   OCCViewer_ViewFrame* aFrame = dynamic_cast<OCCViewer_ViewFrame*>
84                                      ( theViewer->getViewManager()->getActiveView() );
85   OCCViewer_ViewWindow* aView = aFrame ? aFrame->getView( OCCViewer_ViewFrame::MAIN_VIEW ) : 0;
86   if ( !aView )
87     return anOldMode;
88
89   // set a view mode
90   anOldMode = aView->get2dMode();
91   aView->set2dMode( theMode );
92   bool is2dMode = theMode != OCCViewer_ViewWindow::No2dMode;
93
94   // enable/disable view actions
95   QList<int> aNo2dActions;
96   aNo2dActions << OCCViewer_ViewWindow::ChangeRotationPointId
97                << OCCViewer_ViewWindow::RotationId
98                << OCCViewer_ViewWindow::FrontId
99                << OCCViewer_ViewWindow::BackId
100                //<< OCCViewer_ViewWindow::TopId
101                << OCCViewer_ViewWindow::BottomId
102                << OCCViewer_ViewWindow::LeftId
103                << OCCViewer_ViewWindow::RightId
104                << OCCViewer_ViewWindow::AntiClockWiseId
105                << OCCViewer_ViewWindow::ClockWiseId
106                << OCCViewer_ViewWindow::OrthographicId
107                << OCCViewer_ViewWindow::PerspectiveId
108                << OCCViewer_ViewWindow::ResetId;
109
110   QtxActionToolMgr* aToolMgr = aView->toolMgr();
111   QAction* anAction;
112   for ( int i = 0, aNb = aNo2dActions.size(); i < aNb; i++ ) {
113     anAction = aToolMgr->action( aNo2dActions[i] );
114     if ( anAction )
115       anAction->setEnabled( !is2dMode );
116   }
117   QAction* aTop = aToolMgr->action( OCCViewer_ViewWindow::TopId );
118   QtxMultiAction* aMulti = dynamic_cast<QtxMultiAction*>( aTop->parent() );
119   aMulti->setActiveAction( aTop );
120
121   // change view position
122   Handle(V3d_View) aView3d = aView->getViewPort()->getView();
123   if ( !aView3d.IsNull() ) {
124     switch ( theMode ) {
125       case OCCViewer_ViewWindow::XYPlane:
126         aView3d->SetProj (V3d_Zpos);
127         break;
128       case OCCViewer_ViewWindow::XZPlane:
129         aView3d->SetProj (V3d_Yneg);
130         break;
131       case OCCViewer_ViewWindow::YZPlane:
132         aView3d->SetProj (V3d_Xpos);
133         break;
134     }
135   }
136
137   return anOldMode;
138 }
139
140 bool OCCViewer_Utilities::isDialogOpened( OCCViewer_ViewWindow* theView, const QString& theName )
141 {
142   bool isFound = false;
143   OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( theView->parent()->parent() );
144   QList<QDialog*> allDialogs = qFindChildren<QDialog*>( aViewFrame );
145   foreach ( QDialog* d, allDialogs )
146     if ( d->objectName() == theName )
147       isFound = true;
148   return isFound;
149 }