Salome HOME
1037bb3a3d7ff2262376e9407198635529209cc1
[modules/visu.git] / src / VISUGUI / VisuGUI_ShrinkFactorDlg.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : VisuGUI_ShrinkFactorDlg.cxx
23 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
24 //
25
26 #include "VisuGUI_ShrinkFactorDlg.h"
27
28 #include "VisuGUI.h"
29 #include "VisuGUI_ViewTools.h"
30 #include "VisuGUI_Tools.h"
31 #include "VISU_Actor.h"
32
33 #include "SUIT_Desktop.h"
34 #include "SUIT_OverrideCursor.h"
35 #include "SUIT_Session.h"
36 #include "SUIT_MessageBox.h"
37 #include "SUIT_ResourceMgr.h"
38
39 #include "SALOME_ListIO.hxx"
40 #include "SALOME_ListIteratorOfListIO.hxx"
41 #include "SALOME_InteractiveObject.hxx"
42
43 #include "LightApp_Study.h"
44 #include "LightApp_SelectionMgr.h"
45 #include "LightApp_Application.h"
46
47 #include "SVTK_ViewWindow.h"
48
49 // QT Includes
50 #include <QLabel>
51 #include <QPushButton>
52 #include <QSpinBox>
53 #include <QLayout>
54 #include <QGroupBox>
55 #include <QKeyEvent>
56
57 const int DEFAULT_SHRINK = 80;
58
59 //=================================================================================
60 // class    : VisuGUI_ShrinkFactorDlg()
61 // purpose  :
62 //
63 //=================================================================================
64 VisuGUI_ShrinkFactorDlg::VisuGUI_ShrinkFactorDlg( VisuGUI* theModule, bool modal )
65   : QDialog( VISU::GetDesktop( theModule ), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
66     myModule(theModule),
67     mySelectionMgr( VISU::GetSelectionMgr( theModule ) ),
68     myViewWindow( VISU::GetActiveViewWindow<SVTK_ViewWindow>(theModule) )
69 {
70   setModal( modal );
71   setAttribute( Qt::WA_DeleteOnClose );
72     
73   setWindowTitle( tr( "SHRINKFACTOR_TITLE" ) );
74   setSizeGripEnabled( true );
75   QVBoxLayout* topLayout = new QVBoxLayout( this );
76   topLayout->setSpacing( 6 );
77   topLayout->setMargin( 11 );
78
79   /*************************************************************************/
80   GroupC1 = new QGroupBox( this );
81   QHBoxLayout* GroupC1Layout = new QHBoxLayout( GroupC1 );
82   GroupC1Layout->setSpacing( 6 );
83   GroupC1Layout->setMargin( 11 );
84
85   ValueLab = new QLabel( tr( "SHRINKFACTOR_VALUE" ), GroupC1 );
86   ValueSpin = new QSpinBox( GroupC1 );
87   ValueSpin->setRange( 20, 100 ); 
88   ValueSpin->setSingleStep( 10 );
89   ValueSpin->setMinimumWidth( 70 );
90
91   GroupC1Layout->addWidget( ValueLab );
92   GroupC1Layout->addWidget( ValueSpin );
93
94   /*************************************************************************/
95   QGroupBox* GroupButtons = new QGroupBox( this );
96   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons );
97   GroupButtonsLayout->setSpacing( 6 );
98   GroupButtonsLayout->setMargin( 11 );
99
100   buttonOk = new QPushButton( tr( "BUT_CLOSE" ), GroupButtons );
101   buttonOk->setAutoDefault( true );
102   buttonOk->setDefault( true );
103
104   buttonHelp = new QPushButton( tr( "BUT_HELP" ), GroupButtons);
105   buttonHelp->setAutoDefault( true );
106
107   GroupButtonsLayout->addWidget( buttonOk );
108   GroupButtonsLayout->addSpacing( 10 );
109   GroupButtonsLayout->addStretch();
110   GroupButtonsLayout->addWidget( buttonHelp );
111
112   topLayout->addWidget( GroupC1 );
113   topLayout->addWidget( GroupButtons );
114
115   // Initial state
116   onSelectionChanged();
117
118   // signals and slots connections : after ValueHasChanged()
119   connect(buttonOk,       SIGNAL( clicked() ),                 this, SLOT( close() ) );
120   connect(buttonHelp,     SIGNAL( clicked() ),                 this, SLOT( help() ) );
121   connect(ValueSpin,      SIGNAL( valueChanged( int ) ),       this, SLOT( setShrinkFactor() ) );
122   connect(mySelectionMgr, SIGNAL( currentSelectionChanged() ), this, SLOT( onSelectionChanged() ) );
123 }
124
125 //=================================================================================
126 // function : ~VisuGUI_ShrinkFactorDlg()
127 // purpose  :
128 //=================================================================================
129 VisuGUI_ShrinkFactorDlg::~VisuGUI_ShrinkFactorDlg()
130 {
131 }
132
133 //=======================================================================
134 // function : help()
135 // purpose  :
136 //=======================================================================
137 void VisuGUI_ShrinkFactorDlg::help()
138 {
139   QString aHelpFileName = "viewing_3d_presentations_page.html#width_shrink_factor_anchor";
140   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
141   if (app) {
142     VisuGUI* aVisuGUI = dynamic_cast<VisuGUI*>( app->activeModule() );
143     app->onHelpContextModule( aVisuGUI ? app->moduleName( aVisuGUI->moduleName() ) : QString(""), aHelpFileName );
144   }
145   else {
146     QString platform;
147 #ifdef WIN32
148     platform = "winapplication";
149 #else
150     platform = "application";
151 #endif
152     SUIT_MessageBox::warning( this,
153                               tr( "WRN_WARNING" ),
154                               tr( "EXTERNAL_BROWSER_CANNOT_SHOW_PAGE" ).
155                               arg( app->resourceMgr()->stringValue( "ExternalBrowser", platform ) ).arg( aHelpFileName ),
156                               tr( "BUT_OK" ) );
157   }
158 }
159
160 //=================================================================================
161 // function : setShrinkFactor()
162 // purpose  : Called when value of spin box is changed
163 //          : or the first time as initilisation
164 //=================================================================================
165 void VisuGUI_ShrinkFactorDlg::setShrinkFactor()
166 {
167   if ( myViewWindow ) {
168     SUIT_OverrideCursor wc;
169     float shrink = ValueSpin->value() / 100.;
170
171     SALOME_ListIO aList;
172     mySelectionMgr->selectedObjects( aList );
173
174     SALOME_ListIteratorOfListIO anIter( aList );
175     for ( ; anIter.More(); anIter.Next() ) {
176       Handle(SALOME_InteractiveObject) anIO = anIter.Value();
177       VISU_Actor* anActor = VISU::FindActor( VISU::GetAppStudy( myModule ), myViewWindow, anIO->getEntry() );
178       if ( anActor && anActor->IsShrunkable() )
179         anActor->SetShrinkFactor( shrink );
180     }
181   myViewWindow->Repaint();
182   }
183 }
184
185 //=================================================================================
186 // function : onSelectionChanged()
187 // purpose  : Called when selection is changed
188 //=================================================================================
189 void VisuGUI_ShrinkFactorDlg::onSelectionChanged()
190 {
191   bool shrinkable = false;
192   if ( myViewWindow ) {
193     int shrink = DEFAULT_SHRINK;
194
195     SALOME_ListIO aList;
196     mySelectionMgr->selectedObjects( aList );
197
198     if ( aList.Extent() == 1 ) {
199       Handle(SALOME_InteractiveObject) FirstIOS = aList.First();
200       if ( !FirstIOS.IsNull() ) {
201         VISU_Actor* anActor = VISU::FindActor( VISU::GetAppStudy( myModule ), myViewWindow, FirstIOS->getEntry() );
202         if ( anActor ) {
203           shrinkable = anActor->IsShrunkable();
204           if ( shrinkable )
205             shrink = int( anActor->GetShrinkFactor() * 100. + 0.5 );
206         }
207       }
208     } 
209     else if ( aList.Extent() > 1 ) {
210       SALOME_ListIteratorOfListIO It( aList );
211       int setShr = -1;
212       for ( ; It.More(); It.Next() ) {
213         Handle(SALOME_InteractiveObject) IO = It.Value();
214         if (!IO.IsNull()) {
215           VISU_Actor* anActor = VISU::FindActor( VISU::GetAppStudy( myModule ), myViewWindow, IO->getEntry() );
216           if ( anActor ) {
217             bool lshrinkable = anActor->IsShrunkable();
218             shrinkable = shrinkable || lshrinkable;
219             if ( lshrinkable ) {
220               int shr = int( anActor->GetShrinkFactor() * 100. + 0.5 );
221               if ( setShr < 0 )
222                 setShr = shr;
223               else if ( setShr != shr ) {
224                 setShr = DEFAULT_SHRINK;
225                 break;
226               }
227             }
228           }
229         }
230       }
231       if ( setShr >= 0 )
232         shrink = setShr;
233     }
234     ValueSpin->blockSignals( true );
235     ValueSpin->setValue( shrink );
236     ValueSpin->blockSignals( false );
237   }
238   GroupC1->setEnabled( shrinkable );
239 }
240
241 //=================================================================================
242 // function : keyPressEvent()
243 // purpose  :
244 //=================================================================================
245 void VisuGUI_ShrinkFactorDlg::keyPressEvent( QKeyEvent* e )
246 {
247   QDialog::keyPressEvent( e );
248   if ( e->isAccepted() )
249     return;
250
251   if ( e->key() == Qt::Key_F1 ) {
252     e->accept();
253     help();
254   }
255 }