Salome HOME
Patch from Christian CAREMOLI for bug PAL10339 : random GUI freeze.
[modules/superv.git] / src / SUPERVGUI / SUPERVGUI_Notification.cxx
1 //  SUPERV SUPERVGUI : GUI for Supervisor component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SUPERVGUI_Notification.cxx
25 //  Author : Francis KLOSS
26 //  Module : SUPERV
27
28 using namespace std;
29
30 #include "SUPERVGUI_Notification.h"
31 #include "SUPERVGUI_Main.h"
32 #include "SUPERVGUI_CanvasNode.h"
33
34 #include "NOTIFICATION.hxx"
35 #include "SUIT_Desktop.h"
36 #include "SUIT_FileDlg.h"
37 #include "SUIT_Session.h"
38
39 #include <qlayout.h>
40 #include <qlabel.h>
41 #include <qgroupbox.h>
42
43 #define SPACING_SIZE   6
44 #define MARGIN_SIZE   11
45 #define MAX( x, y ) ( x ) > ( y ) ? ( x ) : ( y )
46 #define DLG_SIZE_WIDTH   300
47 #define DLG_SIZE_HEIGHT  300
48
49 /******************************************
50  * Notification settings dialog box
51  ******************************************/
52 /*!
53   Constructor
54 */
55 SUPERVGUI_Notification::SUPERVGUI_Notification( SUPERVGUI_Main* m )
56      : QDialog( SUIT_Session::session()->activeApplication()->desktop(), "", TRUE, WStyle_Customize | WStyle_NormalBorder | WStyle_Title | WStyle_SysMenu ),
57        myMain( m )
58 {
59   setCaption( tr( "TLT_FILTER_NOTIFY" ) );
60   setSizeGripEnabled( TRUE );
61   QVBoxLayout* topLayout = new QVBoxLayout( this ); 
62   topLayout->setSpacing( SPACING_SIZE );
63   topLayout->setMargin( MARGIN_SIZE );
64
65   /* Creating notofication widgets*/
66   /* Top scroll view */
67   myView = new QScrollView( this );
68 //  myView->setMinimumSize( NOTIFICATION_WIDTH, NOTIFICATION_HEIGHT );
69   myBox = new QVBox( myView );
70   myBox->setMargin( MARGIN_SIZE );
71   myBox->setSpacing( SPACING_SIZE );
72   QFrame* lin;
73
74   /* Notification logging */
75   QLabel* logg = new QLabel( tr( "NOTIFICATION_LOGGING_LBL" ), myBox );
76   QFont fnt = logg->font(); fnt.setBold( true ); logg->setFont( fnt );
77   QWidget* dumb = new QWidget( myBox );
78   QHBoxLayout* dumbLayout = new QHBoxLayout( dumb ); dumbLayout->setMargin( 0 ); dumbLayout->setSpacing( SPACING_SIZE );
79   myLogCheck = new QCheckBox( tr( "SAVE_LOG_LBL" ), dumb );
80   myFileEdit = new QLineEdit( dumb ); 
81   myFileEdit->setMinimumSize( 200, 0 );
82   myBrowseBtn = new QPushButton( tr( "BUT_BROWSE"), dumb );
83   dumbLayout->addWidget( myLogCheck );
84   dumbLayout->addWidget( myFileEdit );
85   dumbLayout->addWidget( myBrowseBtn );
86   myFilterCheck = new QCheckBox( tr( "FILTER_LOG_LBL" ), myBox );
87
88   lin = new QFrame( myBox );
89   lin->setFrameStyle( QFrame::HLine | QFrame::Plain );
90
91   /* dataflow notification */
92   myMainNotif = new SUPERVGUI_NotifWidget( myBox, QString( myMain->getDataflow()->Name() ) );
93   myMainNotif->setWarning( false );
94   myMainNotif->setStep( false );
95   myMainNotif->setTrace( false );
96   myMainNotif->setVerbose( false );
97
98   /* nodes notification */
99   QObjectList* ihmList = myMain->/*getGraph()->*/queryList( "SUPERVGUI_CanvasNode" );
100   QObjectListIt i( *ihmList );
101   SUPERVGUI_CanvasNode* theNode;
102   while ( ( theNode = ( ( SUPERVGUI_CanvasNode* )i.current() ) ) != 0) {
103     lin = new QFrame( myBox );
104     lin->setFrameStyle( QFrame::HLine | QFrame::Plain );
105
106     SUPERVGUI_NotifWidget* nw = new SUPERVGUI_NotifWidget( myBox, theNode );
107     nw->setWarning( theNode->isWarning() );
108     nw->setStep( theNode->isStep() );
109     nw->setTrace( theNode->isTrace() );
110     nw->setVerbose( theNode->isVerbose() );
111     myNotifList.append( nw );
112     ++i;
113   };
114   delete ihmList;
115
116   QWidget* dumb1 = new QWidget( myBox );
117   myBox->setStretchFactor( dumb1, 10 );
118
119   myView->addChild( myBox, 0, 0 );
120   myView->setResizePolicy( QScrollView::AutoOneFit );
121
122   int w = myLogCheck->sizeHint().width() + myFileEdit->minimumSize().width() + myBrowseBtn->sizeHint().width() + SPACING_SIZE * 2;
123   w = MAX( w, logg->sizeHint().width() );
124   w = MAX( w, myMainNotif->sizeHint().width() );
125   w = MAX( w, myFilterCheck->sizeHint().width() );
126   myView->setMinimumWidth( w + MARGIN_SIZE * 4 );
127
128   /* OK/Cancel buttons */
129   QGroupBox* GroupButtons = new QGroupBox( this );
130   GroupButtons->setColumnLayout(0, Qt::Vertical );
131   GroupButtons->layout()->setSpacing( 0 );  GroupButtons->layout()->setMargin( 0 );
132   QHBoxLayout* GroupButtonsLayout = new QHBoxLayout( GroupButtons->layout() );
133   GroupButtonsLayout->setAlignment( Qt::AlignTop );
134   GroupButtonsLayout->setSpacing( SPACING_SIZE );
135   GroupButtonsLayout->setMargin( MARGIN_SIZE );
136   myOkBtn = new QPushButton( tr( "BUT_OK" ), GroupButtons, "buttonOk" );
137   myOkBtn->setAutoDefault( TRUE );
138   myOkBtn->setDefault( TRUE );
139   GroupButtonsLayout->addWidget( myOkBtn );
140   GroupButtonsLayout->addItem( new QSpacerItem( 5, 5, QSizePolicy::Expanding, QSizePolicy::Minimum ) );
141   myCancelBtn = new QPushButton( tr( "BUT_CANCEL" ), GroupButtons, "buttonCancel" );
142   myCancelBtn->setAutoDefault( TRUE );
143   GroupButtonsLayout->addWidget( myCancelBtn );
144
145   topLayout->addWidget( myView );
146   topLayout->addWidget( GroupButtons );
147   
148   connect( myOkBtn,     SIGNAL( clicked() ), this, SLOT( accept() ) );
149   connect( myCancelBtn, SIGNAL( clicked() ), this, SLOT( reject() ) );
150   connect( myLogCheck,  SIGNAL( clicked() ), this, SLOT( loggedClicked() ) );
151   connect( myBrowseBtn, SIGNAL( clicked() ), this, SLOT( browseClicked() ) );
152   
153   QString file = QString( "/tmp/Notif_" );
154   file += myMain->getDataflow()->Name();
155   file += ".log";
156   myFileEdit->setText( file );
157
158   loggedClicked();
159   resize( MAX( w + MARGIN_SIZE * 4, DLG_SIZE_WIDTH ), DLG_SIZE_HEIGHT  );
160 }
161
162 /*!
163   Destructor
164 */
165 SUPERVGUI_Notification::~SUPERVGUI_Notification() 
166 {
167 }
168
169 /*!
170   <OK> button slot
171 */
172 void SUPERVGUI_Notification::accept() 
173 {
174   Trace("SUPERVGUI_Notification::accept");
175   for ( int i = 0; i < myNotifList.count(); i++ ) {
176     if ( myNotifList.at( i )->getNode() ) {
177       myNotifList.at( i )->getNode()->setWarning( myNotifList.at( i )->getWarning() );
178       myNotifList.at( i )->getNode()->setStep( myNotifList.at( i )->getStep() );
179       myNotifList.at( i )->getNode()->setTrace( myNotifList.at( i )->getTrace() );
180       myNotifList.at( i )->getNode()->setVerbose( myNotifList.at( i )->getVerbose() );
181     }
182   }
183   QDialog::accept();
184 }
185
186 /*!
187    <Cancel> button slot
188 */
189 void SUPERVGUI_Notification::reject() 
190 {
191   Trace("SUPERVGUI_Notification::reject");
192   QDialog::reject();
193 }
194
195 /*!
196   Called when <Save to file> check box is clicked
197 */
198 void SUPERVGUI_Notification::loggedClicked()
199 {
200   myFileEdit->setEnabled( myLogCheck->isChecked() );
201   myBrowseBtn->setEnabled( myLogCheck->isChecked() );
202   myFilterCheck->setEnabled( myLogCheck->isChecked() );
203 }
204
205 /*!
206   <Browse> button slot
207 */
208 void SUPERVGUI_Notification::browseClicked()
209 {
210   QString fn = SUIT_FileDlg::getFileName( this, 
211                                           myFileEdit->text(), 
212                                           tr( "ALL_FILES" ),
213                                           tr( "CHOOSE_LOG_FILES_TLT" ),
214                                           false );
215   if ( !fn.isEmpty() )
216     myFileEdit->setText( fn );
217 }
218
219 /*!
220   Set/get methods
221 */
222 bool SUPERVGUI_Notification::getLogged() 
223 {
224   return myLogCheck->isChecked();
225 }
226 void SUPERVGUI_Notification::setLogged( bool on, QString logFile ) 
227 {
228   myLogCheck->setChecked( on );
229   if ( !logFile.isNull() ) {
230     myFileEdit->setText( logFile );
231     myFileEdit->home( false ); 
232   } 
233   loggedClicked();
234 }
235 QString SUPERVGUI_Notification::getLogFile() 
236 {
237   return myFileEdit->text().stripWhiteSpace();
238 }
239 bool SUPERVGUI_Notification::getFiltered() 
240 {
241   return myFilterCheck->isChecked();
242 }
243 void SUPERVGUI_Notification::setFiltered( bool on ) 
244 {
245   myFilterCheck->setChecked( on );
246 }
247 bool SUPERVGUI_Notification::getWarning()
248 {
249   return myMainNotif->getWarning();
250 }
251 void SUPERVGUI_Notification::setWarning( bool on )
252 {
253   myMainNotif->setWarning( on );
254 }
255 bool SUPERVGUI_Notification::getStep()
256 {
257   return myMainNotif->getStep();
258 }
259 void SUPERVGUI_Notification::setStep( bool on )
260 {
261   myMainNotif->setStep( on );
262 }
263 bool SUPERVGUI_Notification::getTrace()
264 {
265   return myMainNotif->getTrace();
266 }
267 void SUPERVGUI_Notification::setTrace( bool on )
268 {
269   myMainNotif->setTrace( on );
270 }
271 bool SUPERVGUI_Notification::getVerbose()
272 {
273   return myMainNotif->getVerbose();
274 }
275 void SUPERVGUI_Notification::setVerbose( bool on )
276 {
277   myMainNotif->setVerbose( on );
278 }
279
280 /******************************************
281  * Widget containing title and  checkboxes
282  ******************************************/
283 /*!
284   Constructor
285 */
286 SUPERVGUI_NotifWidget::SUPERVGUI_NotifWidget( QWidget* parent, SUPERVGUI_CanvasNode* n )
287      : QWidget( parent ), myNode ( n )
288 {
289   init();
290 }
291 /*!
292   Another constructor
293 */
294 SUPERVGUI_NotifWidget::SUPERVGUI_NotifWidget( QWidget* parent, QString title )
295      : QWidget( parent ), myNode ( 0 ), myTitle( title )
296 {
297   init();
298 }
299 /*!
300   Destructor
301 */
302 SUPERVGUI_NotifWidget::~SUPERVGUI_NotifWidget()
303 {
304 }
305 /*!
306   Creates widget's layout 
307 */
308 void SUPERVGUI_NotifWidget::init()
309 {
310   QGridLayout* topLayout = new QGridLayout( this );
311   topLayout->setMargin( 0 );
312   topLayout->setSpacing( SPACING_SIZE );
313
314   QLabel* tltLab = new QLabel( this );
315   if ( myNode )
316     tltLab->setText( myNode->name() );
317   else
318     tltLab->setText( myTitle );
319   QFont fnt = tltLab->font(); fnt.setBold( true ); tltLab->setFont( fnt );
320
321   myWarningCheck = new QCheckBox( tr( NOTIF_WARNING ), this );
322   myStepCheck    = new QCheckBox( tr( NOTIF_STEP ),    this );
323   myTraceCheck   = new QCheckBox( tr( NOTIF_TRACE ),   this );
324   myVerboseCheck = new QCheckBox( tr( NOTIF_VERBOSE ), this );
325   
326   topLayout->addMultiCellWidget( tltLab, 0, 0, 0, 4 );
327   topLayout->addWidget( myWarningCheck, 1, 1 );
328   topLayout->addWidget( myStepCheck,    1, 2 );
329   topLayout->addWidget( myTraceCheck,   1, 3 );
330   topLayout->addWidget( myVerboseCheck, 1, 4 );
331   topLayout->addColSpacing( 0, 15 );
332   topLayout->setColStretch( 1, 5 );
333   topLayout->setColStretch( 2, 5 );
334   topLayout->setColStretch( 3, 5 );
335   topLayout->setColStretch( 4, 5 );
336 }
337