Salome HOME
Merge from V5_1_main 14/05/2010
[modules/visu.git] / src / VISUGUI / VisuGUI_CacheDlg.cxx
1 //  Copyright (C) 2007-2010  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
23 //  VISU VISUGUI : GUI of VISU component
24 //  File   : VisuGUI_CacheDlg.cxx
25 //  Author : Oleg UVAROV
26 //  Module : VISU
27 //
28 #include "VisuGUI_CacheDlg.h"
29
30 #include "VisuGUI_Tools.h"
31
32 #include "VISU_PipeLine.hxx"
33
34 #include "SUIT_Desktop.h"
35 #include "SUIT_MessageBox.h"
36 #include "SUIT_Session.h"
37 #include "SUIT_ResourceMgr.h"
38
39 #include "SalomeApp_Module.h"
40 #include <SalomeApp_DoubleSpinBox.h>
41
42 #include "LightApp_Application.h"
43
44 #include <QButtonGroup>
45 #include <QGroupBox>
46 #include <QLabel>
47 #include <QLayout>
48 #include <QLineEdit>
49 #include <QPushButton>
50 #include <QRadioButton>
51
52 using namespace std;
53
54 VisuGUI_CacheDlg::VisuGUI_CacheDlg( VISU::ColoredPrs3dCache_var theCache,
55                                     SalomeApp_Module* theModule )
56   : QDialog( VISU::GetDesktop( theModule ), Qt::WindowTitleHint | Qt::WindowSystemMenuHint ),
57     myCache( theCache )
58 {
59   setModal( true );
60   setWindowTitle( tr( "CACHE_TITLE" ) );
61   setAttribute( Qt::WA_DeleteOnClose );
62
63   QVBoxLayout* aTopLayout = new QVBoxLayout( this );
64   aTopLayout->setSpacing( 6 );
65   aTopLayout->setMargin( 6 );
66   //aTopLayout->setAutoAdd( true );
67
68   long aMb = 1024 * 1024;
69   bool isLimitedMemory = myCache->GetMemoryMode() == VISU::ColoredPrs3dCache::LIMITED;
70   double aLimitedMemory = myCache->GetLimitedMemory();
71   double aFreeMemory = (double)VISU_PipeLine::GetAvailableMemory( 8192 * aMb ) / (double)aMb;
72   double anUsedMemory = myCache->GetMemorySize();
73   double aLimitedMemoryMax = 
74 #ifdef WNT
75           max
76 #else
77           std::max
78 #endif
79           (anUsedMemory + aFreeMemory, aLimitedMemory);
80
81   // Settings
82   QButtonGroup* aMemoryGroup = new QButtonGroup(  this );
83   QGroupBox* aGB = new QGroupBox( tr( "MEMORY_MODE" ), this );
84   aTopLayout->addWidget( aGB );
85   QGridLayout* aGridLay = new QGridLayout( aGB );
86   //aMemoryGroup->setRadioButtonExclusive( true );
87
88   myLimitedMemoryButton = new QRadioButton( tr( "LIMITED_MEMORY" ), aGB );
89   myLimitedMemoryButton->setChecked( isLimitedMemory );
90   aGridLay->addWidget( myLimitedMemoryButton, 0, 0 );
91
92   myMimimalMemoryButton = new QRadioButton( tr( "MINIMAL_MEMORY" ), aGB );
93   myMimimalMemoryButton->setChecked( !isLimitedMemory );
94   aGridLay->addWidget( myMimimalMemoryButton, 1, 0 );
95
96   myLimitedMemory = new SalomeApp_DoubleSpinBox( aGB );
97   VISU::initSpinBox( myLimitedMemory, 1.0, aLimitedMemoryMax, 10.0, "memory_precision" );
98   myLimitedMemory->setSuffix( " Mb" );
99   myLimitedMemory->setValue( aLimitedMemory );
100   myLimitedMemory->setEnabled( isLimitedMemory );
101   aGridLay->addWidget( myLimitedMemory, 0, 1 );
102   
103
104   connect( myLimitedMemoryButton, SIGNAL( toggled( bool ) ), myLimitedMemory, SLOT( setEnabled( bool ) ) );
105
106   // Current state
107   QGroupBox* aStateGroup = new QGroupBox( tr( "MEMORY STATE" ), this );
108   aTopLayout->addWidget( aStateGroup );
109   //aStateGroup->setColumnLayout( 0, Qt::Vertical );
110   //aStateGroup->layout()->setSpacing( 0 );
111   //aStateGroup->layout()->setMargin( 0 );
112
113   QGridLayout* aStateLayout = new QGridLayout( aStateGroup );
114   aStateLayout->setSpacing(6);
115   aStateLayout->setMargin(6);
116
117   QLabel* aUsedMemoryLabel = new QLabel( tr( "USED_BY_CACHE" ), aStateGroup );
118   myUsedMemory = new QLineEdit( aStateGroup );
119   myUsedMemory->setText( QString::number( anUsedMemory ) + " Mb" );
120   myUsedMemory->setReadOnly( true );
121   myUsedMemory->setEnabled( false );
122   QPalette aPal = myUsedMemory->palette();
123   aPal.setColor( myUsedMemory->foregroundRole(), Qt::black );
124   myUsedMemory->setPalette( aPal );
125   //myUsedMemory->setPaletteForegroundColor( Qt::black );
126
127   QLabel* aFreeMemoryLabel = new QLabel( tr( "FREE" ), aStateGroup );
128   myFreeMemory = new QLineEdit( aStateGroup );
129   myFreeMemory->setText( QString::number( aFreeMemory ) + " Mb" );
130   myFreeMemory->setReadOnly( true );
131   myFreeMemory->setEnabled( false );
132   aPal = myFreeMemory->palette();
133   aPal.setColor( myFreeMemory->foregroundRole(), Qt::black );
134   myFreeMemory->setPalette( aPal );
135   //myFreeMemory->setPaletteForegroundColor( Qt::black );
136
137   aStateLayout->addWidget( aUsedMemoryLabel, 0, 0 );
138   aStateLayout->addWidget( myUsedMemory, 0, 1 );
139   aStateLayout->addWidget( aFreeMemoryLabel, 1, 0 );
140   aStateLayout->addWidget( myFreeMemory, 1, 1 );
141
142   // Ok / Cancel
143   QGroupBox* GroupButtons = new QGroupBox( this );
144   aTopLayout->addWidget( GroupButtons );
145   //GroupButtons->setColumnLayout(0, Qt::Vertical );
146   //GroupButtons->layout()->setSpacing( 0 );
147   //GroupButtons->layout()->setMargin( 0 );
148   QGridLayout* GroupButtonsLayout = new QGridLayout( GroupButtons );
149   GroupButtonsLayout->setAlignment( Qt::AlignTop );
150   GroupButtonsLayout->setSpacing( 6 );
151   GroupButtonsLayout->setMargin( 11 );
152
153   QPushButton* buttonOk = new QPushButton( tr( "BUT_OK" ), GroupButtons );
154   buttonOk->setAutoDefault( TRUE );
155   buttonOk->setDefault( TRUE );
156   GroupButtonsLayout->addWidget( buttonOk, 0, 0 );
157   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ), 0, 1 );
158
159   QPushButton* buttonCancel = new QPushButton( tr( "BUT_CANCEL" ) , GroupButtons );
160   buttonCancel->setAutoDefault( TRUE );
161   GroupButtonsLayout->addWidget( buttonCancel, 0, 2 );
162
163   QPushButton* buttonHelp = new QPushButton( tr( "BUT_HELP" ) , GroupButtons );
164   buttonHelp->setAutoDefault( TRUE );
165   GroupButtonsLayout->addWidget( buttonHelp, 0, 3 );
166
167   connect( buttonOk,     SIGNAL( clicked() ), this, SLOT( accept() ) );
168   connect( buttonCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
169   connect( buttonHelp,   SIGNAL( clicked() ), this, SLOT( onHelp() ) );
170 }
171
172 VisuGUI_CacheDlg::~VisuGUI_CacheDlg()
173 {
174 }
175
176 bool VisuGUI_CacheDlg::isLimitedMemory()
177 {
178   return myLimitedMemoryButton->isChecked();
179 }
180
181 double VisuGUI_CacheDlg::getLimitedMemory()
182 {
183   return myLimitedMemory->value();
184 }
185
186 void VisuGUI_CacheDlg::accept()
187 {
188   if( isLimitedMemory() )
189   {
190     myCache->SetMemoryMode( VISU::ColoredPrs3dCache::LIMITED );
191     myCache->SetLimitedMemory( (float)getLimitedMemory() );
192   }
193   else
194     myCache->SetMemoryMode( VISU::ColoredPrs3dCache::MINIMAL );
195
196
197   QDialog::accept();
198 }
199
200 void VisuGUI_CacheDlg::onHelp()
201 {
202   QString aHelpFileName;// = "types_of_gauss_points_presentations_page.html";
203   LightApp_Application* app = (LightApp_Application*)(SUIT_Session::session()->activeApplication());
204   if (app)
205     app->onHelpContextModule(app->activeModule() ?
206                              app->moduleName(app->activeModule()->moduleName()) : QString(""), aHelpFileName);
207   else {
208     SUIT_MessageBox::warning(0, tr("WRN_WARNING"),
209                              tr("EXTERNAL_BROWSER_CANNOT_SHOW_PAGE").
210                              arg(app->resourceMgr()->stringValue("ExternalBrowser", "application")).arg(aHelpFileName),
211                              tr("BUT_OK"));
212   }
213 }