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