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